简体   繁体   English

如何将日期时间信息写入 netcdf4?

[英]How to write datetime information to netcdf4?

Essentially, I would like to open a netcdf file, read out the time stamps for individual pixels and then write the timestamps into a new file.本质上,我想打开一个 netcdf 文件,读出单个像素的时间戳,然后将时间戳写入一个新文件。 Here is my pseudo-code:这是我的伪代码:

f10 = Dataset(nc_f10, 'r')
Time_UTC_10 = np.transpose(f10.variables['TIME_UTC'][:]) #shape is [92,104]
radiance_10 = f10.variables['RADIANCE'][:] #shape is [92,104]
f10.close()

#Manipulate Radiance Information

#python separates the characters in the timestamp, so join it back up:
for i in np.arange(92):
    for j in np.arange(104):
        joined_16 = ''.join(Time_UTC_16[:,i,j])
        datetime_16[i,j] = datetime.datetime.strptime(joined_16, '%Y-%m-%dT%H:%M:%S.%fZ')

#Create and fill the netcdf 
nc_out = Dataset(output_directory+nc_out_file, 'w', format='NETCDF4')

y = nc_out.createDimension('y',104)
x = nc_out.createDimension('x',92)

times = nc_out.createVariable('time', np.unicode_, ('x','y'))
O5s = nc_out.createVariable('O5s', np.float32, ('x', 'y'))

times[:] = datetime_16
O5s[:] = radiance_10

nc_out.close()

But when I try to run this, I get the following error: TypeError: only numpy string, unicode or object arrays can be assigned to VLEN str var slices但是当我尝试运行它时,出现以下错误: TypeError: only numpy string, unicode or object arrays can be assigned to VLEN str var slices

I feel like I may be misunderstanding something important here.我觉得我可能在这里误解了一些重要的事情。 Any thoughts on how I can correct this code to write the timestamps to a variable in a netcdf?关于如何更正此代码以将时间戳写入 netcdf 中的变量的任何想法?

I really do not know why you want to keep your time variables as a string (this is what the error message says: the values can be either strings, unicode or objects), but one example is like this:我真的不知道为什么要将时间变量保留为字符串(这是错误消息所说的:值可以是字符串、unicode 或对象),但一个例子是这样的:

#!/usr/bin/env ipython
# ----------------------
import numpy as np
from netCDF4 import Dataset,num2date,date2num
# ----------------------
ny=104;
nx=92
# ----------------------
radiance_10=np.random.random((ny,nx));
datetime_16=np.ones((ny,nx))
# ----------------------
nc_out = Dataset('test.nc', 'w', format='NETCDF4')

y = nc_out.createDimension('y',ny)
x = nc_out.createDimension('x',nx)

times = nc_out.createVariable('time', np.unicode_, ('x','y'))
O5s = nc_out.createVariable('O5s', np.float32, ('x', 'y'))

O5s[:] = radiance_10
for ii in range(ny):
    for jj in range(nx):
        times[jj,ii] = "2011-01-01 00:00:00"

nc_out.close()

Basically the values that are written to the time variable are now strings with value at every grid point "2011-01-01 00:00:00".基本上,写入时间变量的值现在是在每个网格点“2011-01-01 00:00:00”处具有值的字符串。

Nevertheless, I would use timevalues as time elapsed from arbitarily selected timemoment.不过,我会使用时间值作为从任意选择的时间经过的时间。 That is the most common way how to keep time in the netCDF file.这是在 netCDF 文件中保持时间的最常用方法。 Let us assume our data in every point is for time moment 2014-04-11 23:59.让我们假设每个点的数据都是 2014-04-11 23:59 的时间点。 Then I could save it as seconds since 2014-04-01.然后我可以将其保存为自 2014-04-01 以来的秒数。 Here is the code that I would use:这是我将使用的代码:

import numpy as np
from netCDF4 import Dataset,num2date,date2num
import datetime
# ----------------------
ny=104;
nx=92
# ----------------------
radiance_10=np.random.random((ny,nx));
# ---------------------------------------------------
timevalue = datetime.datetime(2014,4,11,23,59)
time_unit_out= "seconds since 2014-04-01 00:00:00"
# ---------------------------------------------------
nc_out = Dataset('test_b.nc', 'w', format='NETCDF4')
y = nc_out.createDimension('y',ny)
x = nc_out.createDimension('x',nx)
times = nc_out.createVariable('time', np.float64, ('x','y'))
times.setncattr('unit',time_unit_out);

O5s = nc_out.createVariable('O5s', np.float32, ('x', 'y'))

O5s[:] = radiance_10
times[:] = date2num(timevalue,time_unit_out);
nc_out.close()

If you check the value that is now in the time variable, it is 950340, which is the number of seconds from 2014-04-01 00:00 to 2014-04-11 23:59.如果您检查现在时间变量中的值,它是 950340,这是从 2014 年 4 月 1 日 00:00 到 2014 年 4 月 11 日 23:59 的秒数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM