我想在netcdf文件中创建一个3维的时间序列(经度、纬度、时间无限)。timeseries应该从其他netcdf文件创建。它们中的每一个只有一个时间点,例如17856。我知道如何创建新的netcdf-file,如何从netcdf-file中提取作为2D数组的数据,以及数据的时间。我的问题是:如何将2D数组放入netcdf-file中并显示其正确的时间?"ncvar_put“函数中的开始和计数参数是如何工作的?
我使用ncdf4包并阅读以下教程:
我在http://geog.uoregon.edu/bartlein/courses/geog490/week04-netCDF.html#create-and-write-a-netcdf-file上找到了答案,但我还是不明白。我仍然没有使用netcdf文件的经验。
示例
e of my problem:
# data from other netcdf file
values = array(data = c(1:9)/10, dim = c(3,3))
values_2 = array(data = c(9:25)/10, dim = c(3,3))
time = 25
time_2 = 23
# set parameters
lon = 1:3
lat = 1:3
# define dimensions
# Longitude
londim = ncdim_def(name = "longitude", units = "degrees", vals = as.double(lon),
longname = "longitude")
# Latitude
latdim = ncdim_def(name = "latitude", units = "degrees", vals = as.double(lat),
longname = "latitude")
# Time
timedim = ncdim_def(name = "time", units ="days since 1582-10-15 00:00", vals = as.double(1),
unlim = TRUE, calendar = "gregorian")
# define variables
B01 = ncvar_def(name = "B01",
units ="percent",
list(londim,latdim,timedim),
missval = NA,
prec="double")
# create netcdf
nc_test = nc_create("test.nc", list(B01), force_v4 = TRUE)
# Add values
### Here is somethin missing --> How do I add the timestamp?
ncvar_put(nc_test, "B01", values, start=c(1,1,1), count=c(-1,-1,1))
ncvar_put(nc_test, "B01", values2, start=c(1,1,2), count=c(-1,-1,1))当我想提取数据时,我得到了3-3-2数组,但时间步长不正确,因为我没有添加它们。我该怎么做呢?我想要3-3-2数组,当我花时间,我想以正确的顺序获得正确的时间。
发布于 2019-10-11 00:02:29
我使用另一种方法向netCDF文件添加时间。这是供您参考的示例代码。
from datetime import datetime
from datetime import timedelta
from netCDF4 import date2num
from netCDF4 import Dataset
import os
# generate time for netCDF with 1 hour interval
utc_now = datetime.utcnow()
time_list = [utc_now + timedelta(hours=1*step) for step in range(6)]
trans_time = date2num(time_list, units="hours since 0001-01-01 00:00:00.0", calendar="gregorian")
with Dataset(os.getcwd() + "/sample.nc", "w") as sample:
# Create dimension for sample.nc
time = sample.createDimension("time", None)
lat = sample.createDimension("lat", 3) # 3 is the latitude size in this sample
lon = sample.createDimension("lon", 3)
# Create variable for sample.nc
time = sample.createVariable("time","f8",("time",))
lat = sample.createVariable("lat","f4",("lat",))
lon = sample.createVariable("lon","f4",("lon",))
time[:] = trans_time
variable_with_time = sample.createVariable("variable_with_time", "f4", ("time", "lat", "lon"))
for key, value in sample.variables.items():
print(key)
print(value)
print("*"*70)输出:
time
<class 'netCDF4._netCDF4.Variable'>
float64 time(time)
unlimited dimensions: time
current shape = (6,)
filling on, default _FillValue of 9.969209968386869e+36 used
**********************************************************************
lat
<class 'netCDF4._netCDF4.Variable'>
float32 lat(lat)
unlimited dimensions:
current shape = (3,)
filling on, default _FillValue of 9.969209968386869e+36 used
**********************************************************************
lon
<class 'netCDF4._netCDF4.Variable'>
float32 lon(lon)
unlimited dimensions:
current shape = (3,)
filling on, default _FillValue of 9.969209968386869e+36 used
**********************************************************************
variable_with_time
<class 'netCDF4._netCDF4.Variable'>
float32 variable_with_time(time, lat, lon)
unlimited dimensions: time
current shape = (6, 3, 3)
filling on, default _FillValue of 9.969209968386869e+36 used
**********************************************************************你可能会注意到,时间被放置为第一个维度。有关详细信息,请参阅我引用的文档的link。
https://stackoverflow.com/questions/57006443
复制相似问题