有人可以介绍如何为NetCDF文件创建无限时间维度吗?我试过用data.createDimension('t', None)
,
但是当我看t
时,它是一个Numpy阵列。如果可能,请介绍为其分配值。
我使用的是python 2.7。
我有多个NetCDF文件(3个维度),每个我必须计算一个数组(3个维度)。文件之间的时间步长为3小时。现在我必须为每个时间步创建一个带有计算数组的新NetCDF。我的问题是,我不知道如何访问时间轴,以便我可以将计算出的数组分配给它的不同时间步。
我想为时间轴指定一个日期。为了创建我使用datetime
这样的日期:
t_start = dt.datetime(1900,1,1)
t_delta = dt.timedelta(hours=3)
两个时间步之间的时间是3个小时。在循环文件时,时间步长的日期计算如下:
t_mom = t_start + i*t_delta
t_mom_str = t_mom.strftime("%d %B %Y %H %M %S")
t_mom_var = netCDF4.stringtochar(np.array([t_mom_str]))
我创建了一个像这样的变量:
time = data.createVariable('time', np.float32, ('time'))
现在我想将日期分配给时间变量:
time[i] = t_mom_var[:]
但这不是这样的。谢谢你的帮助。
答案 0 :(得分:2)
将createDimension
与None
一起使用应该有效:
import netCDF4 as nc4
import numpy as np
f = nc4.Dataset('test.nc', 'w')
# Create the unlimited time dimension:
dim_t = f.createDimension('time', None)
# Create a variable `time` using the unlimited dimension:
var_t = f.createVariable('time', 'int', ('time'))
# Add some values to the variable:
var_t[:] = np.arange(10)
f.close()
这导致(ncdump -h test.nc
):
netcdf test {
dimensions:
time = UNLIMITED ; // (10 currently)
variables:
int64 time(time) ;
}
对于更新的问题,这是一个如何通过添加新的无限维来将多个文件合并为一个文本的最小工作示例:
import netCDF4 as nc4
import numpy as np
# Lets quickly create 3 NetCDF files with 3 dimensions
for i in range(3):
f = nc4.Dataset('test_{0:1d}.nc'.format(i), 'w')
# Create the 3 dimensions
dim_x = f.createDimension('x', 2)
dim_y = f.createDimension('y', 3)
dim_z = f.createDimension('z', 4)
var_t = f.createVariable('temperature', 'double', ('x','y','z'))
# Add some dummy data
var_t[:,:,:] = np.random.random(2*3*4).reshape(2,3,4)
f.close()
# Now the actual merging:
# Get the dimensions (sizes) from the first file:
f_in = nc4.Dataset('test_0.nc', 'r')
dim_size_x = f_in.dimensions['x'].size
dim_size_y = f_in.dimensions['y'].size
dim_size_z = f_in.dimensions['z'].size
dim_size_t = 3
f_in.close()
# Create new NetCDF file:
f_out = nc4.Dataset('test_merged.nc', 'w')
# Add the dimensions, including an unlimited time dimension:
dim_x = f_out.createDimension('x', dim_size_x)
dim_y = f_out.createDimension('y', dim_size_y)
dim_z = f_out.createDimension('z', dim_size_z)
dim_t = f_out.createDimension('time', None)
# Create new variable with 4 dimensions
var_t = f_out.createVariable('temperature', 'double', ('time','x','y','z'))
# Add the data
for i in range(3):
f_in = nc4.Dataset('test_{0:1d}.nc'.format(i), 'r')
var_t[i,:,:,:] = f_in.variables['temperature'][:,:,:]
f_in.close()
f_out.close()
答案 1 :(得分:1)
@Bart是正确的,但没有回答你问题的第二部分。您需要创建一个按时间维度标注的时间变量。
import numpy as np
import dateutil.parser
# create a time variable, using the time dimension.
var_t = nc4.createVariable('time', 'int32', ('time'))
var_t.setncattr('units', 'seconds since 1970-01-01 00:00:00 UTC')
# create a start time
dt = dateutil.parser.parse("2017-05-01T00:00)
ntime = nc4.date2num(dt, var_t.units)
# add some hours
times = [ntime, ntime + 3600, ntime + 7200]
# Not sure but you may need a numpy array
times = np.array([times])
var_t[:] = times