简体   繁体   中英

How can I export data from GrADs to a .csv file or from NetCDF to .csv?

I'm having real difficulty with exporting data from GrADS to a .csv file although it should be really easy. The file in question is from the APHRODITE project relating to rainfall over Asia. Basically I can read this file into GrADS using:

open d:/aphro/aphro.ctl

and it tells me that:

Data file d:/aphro/APHRO_MA_025deg_V1101R2.%y4 is open as file 1
Lon set to 60.125 149.875
Lat set to -14.875 54.875
Lev set to 1 1
Time values set: 1961:1:1:0 1961:1:1:0
E set to  1 1

If I execute:

q ctlinfo

it also tells me that I have three variables:

precip 1 0 daily precipitation analysis
rstn 1 0  ratio of 0.05 degree grids with station
flag 1 0 ratio of 0.05 degree grids with snow

Okay, now all I want to do is produce a list in a .csv file (or .txt) file with the following information:

Precipitation   Lon    Lat   Time(date)

It sounds really easy but I just can't do it. One method is to use:

fprintf precip d:/output.csv %g 1

This gives me an .csv file with the entire data for that day in one long column (which is what I want). I can also do the same for lon and lat in different files and combine them. The problem is that this takes for ages for the output file - it is much faster if you don't mind lots of columns but this becomes a pain to manage. Basically, this method is too slow.

Another method is to export the data as a NetCDF file by:

Set sdfwrite -4d d:/output.nc
define var = precip
sdfwrite precip

This then very quickly writes a file called output.nc which contains all the data I need. Using RI can then read all the variables individually eg

f <- open.ncdf("D:/aphro/test.nc")
A <- get.var.ncdf(nc=f,varid="time")
B <- get.var.ncdf(nc=f,varid="rain")
D <- get.var.ncdf(nc=f,varid="lon")
E <- get.var.ncdf(nc=f,varid="lat")

But what I want is to make an output file where each row tells me the time, rain amount, lon and lat. I tried rbind but it doesn't associate the correct time(date) with the right rain amount, and similarly messes up the lon and lat as there are hundreds of thousand of rain data but only a few dates and only 360 lon points and 280 lat points (ie the rain data is a grid of data for each day over several days). I'm sure this should be easy but how to do it?

Please help

Tony

Up to my knowledge, you can change the GrAD file to NetCDF file by using climate data operator and R together. Details can be found here . Further a NetCDF file can be converted in to a .csv file. For this I am providing a dummy code.

library(ncdf)
nc <- open.ncdf("foo.nc")             #open ncdf file and read variables
lon <- get.var.ncdf(nc, "lon")         # Lon lat and Time
lat <- get.var.ncdf(nc, "lat")
time <- get.var.ncdf(nc, "time")
dname <- "t"                         # name of variable which can be found by using print(nc)
nlon <- dim(lon)
nlat<- dim(lat)
nt<- dim(time)
lonlat <- expand.grid(lon, lat)    # make grid of given longitude and latitude 
mintemp.array <- get.var.ncdf(nc, dname)
dlname <- att.get.ncdf(nc, dname, "long_name")
dunits <- att.get.ncdf(nc, dname, "units")
fillvalue <- att.get.ncdf(nc, dname, "_FillValue")
mintemp.vec.long <- as.vector(mintemp.array)
mintemp.mat <- matrix(mintemp.vec.long, nrow = nlon * nlat, ncol = nt)
mintemp.df <- data.frame(cbind(lonlat, mintemp.mat))
options(width = 110)
write.csv(mintemp.df, "mintemp_my.csv")

I hope, it explains your question.

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