vera_kkk
vera_kkk

Reputation: 47

extract time series weather forecast data from Netcdf to csv

I want to use R to extract a time series data from Netcdf dataset at every location(X and Y) and convert it to csv file. This is my first time dealing with NetCDF data. Could anyone tell me the related code using R or Matlab?

Here is my data description:

IRI FD Seasonal_Forecast Precipitation prob: Tercile Probability data

Independent Variables (Grids):

Tercile Classes grid: /C (ids) unordered [ (Below_Normal) (Normal) (Above_Normal)] :grid Month Forecast Issued

grid: /F (months since 1960-01-01) ordered [ (Sep 1997) (Dec 1997) (Mar 1998) ... (Sep 2015)] N= 187 pts :grid

Forecast Lead Time in Months grid: /L (months) ordered (1.0 months) to (4.0 months) by 1.0 N= 4 pts :grid Longitude (longitude)

grid: /X (degree_east) periodic (178.75W) to (178.75E) by 2.5 N= 144 pts :grid Latitude (latitude)

grid: /Y (degree_north) ordered (88.75N) to (88.75S) by 2.5 N= 72 pts :grid

Upvotes: 1

Views: 1178

Answers (1)

ExHunter
ExHunter

Reputation: 307

you can use netcdf4 packages

library(ncdf4)
setwd("E:\\KKIM_2017\\DES\\")

nc = nc_open("MTS_IR_201612010000.nc")       #open ncdf and read variables
lon = ncvar_get(nc,"longitude")              # Lon lat and time
lat = ncvar_get(nc,"latitude")
time = ncvar_get(nc, "time")
dname = "IR"  #variable
variabel = ncvar_get(nc,dname)
lonlat = as.matrix(expand.grid(lon,lat))
var_vektor = as.vector(variabel)
datanya = data.frame(cbind(lonlat,var_vektor))
names(datanya) = c("lon","lat",paste(dname,as.character(), sep="_"))
head(na.omit(datanya))
csvpath = "E:\\"  #directory to save file
csvname = "hasil_1v2.csv" #file_output
csvfile = paste(csvpath, csvname, sep="")
write.table(na.omit(datanya),csvfile, row.names=FALSE, sep=",")

Upvotes: 1

Related Questions