Reputation: 41
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. 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
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?
Upvotes: 2
Views: 6431
Reputation: 2078
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:
#!/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".
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. Let us assume our data in every point is for time moment 2014-04-11 23:59. Then I could save it as seconds since 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.
Upvotes: 2