我有以下.nc4文件,对于每个lat和long我想提取日期/时间和值。我将每个存储为csv文件。
https://www.dropbox.com/s/p3j84rhbq7oo0o6/svc_MERRA2_100.tavg1_2d_lnd_Nx.19800101.nc4?dl=0
我的代码如下
library(ncdf4)
library(RNetCDF)
#open nc file
dat.ncdf4 <- nc_open("svc_MERRA2_100.tavg1_2d_lnd_Nx.19800101.nc4")
#extracting variable of interest
prectot <- ncvar_get(dat.ncdf4, varid="PRECSNOLAND")
# get time dimension
timeDim <- ncvar_get(nc, "time")
# Put the time in a reader friendly format
label.time <- names(dat.ncdf4$dim)[1]
date.char <- date.char <- utcal.nc(dat.ncdf4$dim[[label.time]]$units, dat.ncdf4$dim[[label.time]]$vals, type="s")
date.POSIXlt <- strptime(date.char, format="%Y-%m-%d %H:%M:%S", tz="UTC")
timeDim <- as.POSIXct(date.POSIXlt)
# get lon/lat :
lonDim <- ncvar_get(dat.ncdf4, "lon")
latDim <- ncvar_get(dat.ncdf4, "lat")
# close netcdf file
nc_close(dat.ncdf4)
# The loop is to extract data for each lat and long
for( i in seq_along(latDim))
{
for(j in seq_along(lonDim))
{
# making the time series of date, precip, lat and long
d1 <- data.frame(time = timeDim, prectot = prectot, latitude = latDim[i], longitude = lonDim[j])
write.table(d1, paste("Prp ",latDim[i],lonDim[j],".csv"), append = F, row.names= FALSE,col.names=FALSE, sep=",")
d1 = NULL
}
}
对于每个网格单元(纬度和经度),我期望有四列(日期,值,纬度和经度)但是,当我运行此代码时,我收到了超过60列,其中包含生成的每个csv文件的数据。我的指导将不胜感激,因为我无法弄清楚我做错了什么
答案 0 :(得分:1)
来自ncks
的NCOzender@aerosol:~$ ncks -H -C -v three_dmn_rec_var ~/nco/data/in.nc
time[0]=1 lat[0]=-90 lon[0]=0 three_dmn_rec_var[0]=1
time[0]=1 lat[0]=-90 lon[1]=90 three_dmn_rec_var[1]=2
time[0]=1 lat[0]=-90 lon[2]=180 three_dmn_rec_var[2]=3
time[0]=1 lat[0]=-90 lon[3]=270 three_dmn_rec_var[3]=4
和--no_dmn_var_nm
zender@aerosol:~$ ncks --no_dmn_var_nm -H -C -v three_dmn_rec_var ~/nco/data/in.nc
1 -90 0 1
1 -90 90 2
1 -90 180 3
1 -90 270 4