user3978632
user3978632

Reputation: 283

extract from NetCDF files using X, Y coordinates

I am new to R. I had a NetCDf file which has 3 Dimenstions viz time,x,y and its has got six variables viz rain,tair,swin,lwin,rh and wind. I want to extract the time series information of the each variable according to the time dimenstion and export them to csv format. I have used following sets of the codes to do that but i couldnt do it for a single point. I want to extract the data at two points (30000000,810000) and (3100000,820000)

setwd("C:/Users/a/Desktop/pd/1/")
f<-open.ncdf("merra.rfe.90m.200208.nc")
t<-get.var.ncdf(f,varid="time")
B<-get.var.ncdf(f,varid="rain")
c<-get.var.ncdf(f,varid="tair")
write.table(t(rbind(A,B,c)),file="output.csv")

But i dont know how to inject these coordinates. I have found the codes for lat and long format but i have x and y coordinates. I would be quite generous of you if any one of you could stepout and help me with this problem.

Upvotes: 1

Views: 3390

Answers (1)

AF7
AF7

Reputation: 3242

I guess you are using the ncdf package. It is better to use the ncdf4 or raster packages.

I'm not sure I understand what you are asking. If you could post your original file (trimmed down if it is too big) it would be better.

If I understood what you mean, you want something on the lines of this:

library(ncdf4)
f <- nc_open("file.nc")
t <- ncvar_get(f, "rain")
point1_t <- t[30000000,810000,] #Notice the extra ","
point2_t <- t[3100000,820000,]
#etcetera

You didn't specify if 30000000,810000 are the X,Y coordinates or are in some unit system. If they are X,Y coordinates and the file has correctly set X-Y dimensions then you should be good to go with the above.

If those values are expressed in some units, thus not X-Y pixel values (e.g. pixel [30,12]=(100000,400000)meters ), then you should have them stored in some variables.
If this is the case:

x <- ncvar_get(f, "x")
y <- ncvar_get(f, "y")
which(x == 30000000 && y == 810000, arr.ind=T)

...will return the XY coordinates of all the pixels that have x=30000000 and y=810000. Once you know the XY pixel coordinates it's easy enough to extract the values as done before. I hope I made myself clear, it's not easy missing the input file.

Upvotes: 1

Related Questions