1

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 R I can then read all the variables individually e.g.

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 (i.e. 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

AntonyDW
  • 349
  • 5
  • 17
  • I'm working with NetCDF files too (rainfall data from TRMM...) I created **STFDF** files from the **spacetime** packet via the **raster** packets **brick** function. Gives me full spatial and temporal flexability and are easy to handle etc. sorry, bit I can't really write an example just right now – Ben K Dec 20 '13 at 17:01
  • try cbind(expand.grid(get.var.ncdf(nc=f,varid="lon"), get.var.ncdf(nc=f,varid="lat"), get.var.ncdf(nc=f,varid="time")), get.var.ncdf(nc=f,varid="rain")) but without a better description of the file we can only guess. – mdsumner Dec 20 '13 at 22:39
  • For info I solved this by sort of using the cbind and get.var.ncdf method. First of all I set time within Grads using: set t 1 365 i.e. the first years data, and outputted it using sdfwrite. The using R, and loading the NCDF library, I used get.var.ncdf to read the variable, the longitude and latitude. I then expanded the grid of the lon and lat using cbind(expand.grid(lon,lat)) and also expanded the variable, i.e. expand.grid(variable). I then selected one days worth from the variable and combined it with the lon,lat data using cbind again - and repeated for each day. – AntonyDW Jan 07 '14 at 06:02

1 Answers1

5

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.

Pankaj
  • 1,296
  • 2
  • 13
  • 23