简体   繁体   中英

Writing a loop to store many data variables in r

I want to begin by saying that I am not a programmer, I'm just trying to store data so it's easily readable to myself.

I just downloaded a large .nc file of weather data and I am trying to take data from the file and store it in .csv format so I can easily view it in excel. The problem with the data is that it contains 53 variables with three 'dimensions': latitude, longitude, and time. I have written some code to only take a certain latitude and longitude and every timestamp so I get one nice column for each variable (with a single latitude and longitude but every timestamp). My problem is that I want to have the loop store a column for every variable to a different (arbitrary) object in R so that I just have to run it once and then write all the data to one .csv file with the write.csv function.

Here's the code I've written so far, where janweather is the .nc file.

while( j <= 53){
v1 <- janweather$var[[j]]
varsize <- v1$varsize

ndims <- v1$ndims
nt <- varsize[ndims] # Remember timelike dim is always the LAST dimension!
j <- j +1;
for( i in 1:nt ) {
  # Initialize start and count to read one timestep of the variable.
  start <- rep(1,ndims) # begin with start=(1,1,1,...,1)
  start[1] <- i
  start[2] <- i# change to start=(i,i,1
  count <- varsize # begin w/count=(nx,ny,nz,...), reads entire var
  count[1] <- 1
  count[2] <- 1
  data3 <- get.var.ncdf( janweather, v1, start=start, count=count )
}
}

Here are the details of the nc file from print.ncdf(janweather):

[1] "file netcdf-atls04-20150304074032-33683-0853.nc has 3 dimensions:" [1] "longitude Size: 240" [1] "latitude Size: 121" [1] "time Size: 31" [1] "------------------------" [1] "file netcdf-atls04-20150304074032-33683-0853.nc has 53 variables:"

My main goal is to have all the variables stored under a different name by the get.var.ncdf function. Right now I realize that it just keeps overwritting 'data3' until it reaches the last variable so all I've accomplished is getting data3 written to the last variable. I'd like to think there is an easy solution to this but I'm not exactly sure how to generate strings to store the variables under.

Again, I'm not a programmer so I'm sorry if anything I've said doesn't make any sense, I'm not very well versed in the lingo or anything.

Thanks for any and all help you guys bring!

If your not a programmer and want only to get variables in csv format, you can use the NCO commands . With this commands you can do multiple operations on netcdf files.

So with the command ncks you can output the data from a variable with and specific dimensions slice.

ncks -H -v latitude janweather.nc 

This command will list on the screen the values in the latitude variable.

ncks -s '%f ,' -H -v temperature janweather.nc 

This command will list the values of the variable temperature, with the format specified with the -p argument (sprintf style).

So just pass the output to a file and there you have the contents of a variables in a text file.

ncks -s '%f ,' -H -v temperature janweather.nc > temperature.csv 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM