1

I am trying to write out a time series of dates to a netCDF variable. My dates are stored as a Pandas DatetimeIndex called date_array. The code I'm trying use is as follows:

Times = w_nc_fid.createVariable('Times','S1',('time',))
Times[:] = str(date_array)

The result is a [72,1] array all with just the string D. What I'm actually trying to write out are the strings of dates (72 consecutive dates):

'2015-07-19 10:00:00'
'2015-07-19 11:00:00'
'2015-07-19 12:00:00'

Can anyone help me with this?

1
  • Is there a reason you want to write the dates as strings and not as floats? Typically, time in netCDFs is stored as a array of times since a reference time (e.g. "says since 1970-01-01"). Commented Aug 9, 2015 at 3:30

1 Answer 1

3

Using netCDF4, it can be accomplished using the following:

import numpy as np
import netCDF4

times = np.array(['2015-07-19 10:00:00', '2015-07-19 11:00:00', '2015-07-19 12:00:00'], dtype=object)

fileout = netCDF4.Dataset('./string_out.nc', 'w')
fileout.createDimension('time', len(times))
times_out = fileout.createVariable('times', str, 'time',)
times_out[:] = times
fileout.close()

Which you can confirm using ncks/ncdump:

>>> ncks string_out.nc | more
...
time[0] times[0]=2015-07-19 10:00:00 
time[1] times[1]=2015-07-19 11:00:00 
time[2] times[2]=2015-07-19 12:00:00 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.