#(1)读取NC文件小时数据信息---------
library(ncdf4)
nc <- nc_open('AQI_Grid_2020040708.nc')
print(nc)
PM25 <- ncvar_get( nc = nc, varid = 'PM25')
PM10 <- ncvar_get( nc = nc, varid = 'PM10')
O3 <- ncvar_get( nc = nc, varid = 'O3')
AQI <- ncvar_get( nc = nc, varid = 'AQI')
longitude<-ncvar_get( nc = nc, varid = 'lon')
latidue<-ncvar_get( nc = nc, varid = 'lat')
nc_close( nc )
#(2)将计算完的结果写入新的NC文件-------------------
# Define some straightforward dimensions
lon <- ncdim_def( "lon", "degrees_east", vals=longitude)
lat <- ncdim_def( "lat", "degrees_north", vals=latidue)
time <- ncdim_def( "Time", "24h-48h-72h mean", vals=c(24,48,72))
#Make varables of various dimensionality, for illustration purposes
mv <- -9999 # missing value to use
aq_fcst_PM25<- ncvar_def( name = 'PM25', units = 'ug/m3', dim = list(lon,lat,time), missval = -9999, prec = 'float' )
aq_fcst_PM10<- ncvar_def( name = 'PM10', units = 'ug/m3', dim = list(lon,lat,time), missval = -9999, prec = 'float' )
aq_fcst_O3<- ncvar_def( name = 'O3', units = 'ug/m3', dim = list(lon,lat,time), missval = -9999, prec = 'float' )
aq_fcst_AQI<- ncvar_def( name = 'AQI', units = '1', dim = list(lon,lat,time), missval = -9999, prec = 'float' )
#创建文档
ncnew <- nc_create( filename = 'AQI_GRID_2020040708_024-048-072.nc', vars =list(aq_fcst_PM25,aq_fcst_PM10,aq_fcst_O3,aq_fcst_AQI) )
#写入数据
ncvar_put( nc = ncnew, varid = aq_fcst_PM25, vals = PM25_24_48_72 )
ncvar_put( nc = ncnew, varid = aq_fcst_PM10, vals = PM10_24_48_72 )
ncvar_put( nc = ncnew, varid = aq_fcst_O3, vals = O3_24_48_72 )
ncvar_put( nc = ncnew, varid = aq_fcst_AQI, vals = O3_24_48_72 )
#写入属性
nc_close(ncnew)