write times in NetCDF file
<p>While using the NETCDF4 package write times in netCDF file.</p>

<pre><code>dates = []
for iday in range(84):
    dates.append(datetime.datetime(2016, 10, 1) + atetime.timedelta(hours = iday))
times[:] = date2num(dates, units=times.units, calendar = imes.calendar)
# print times[:]
for ii, i in enumerate(times[:]):
    print i, num2date(i, units=times.units), dates[ii]
</code></pre>

<p>The times are right:</p>

<pre class="lang-none prettyprint-override"><code>17669815.0 2016-10-04 07:00:00 2016-10-04 07:00:00
17669816.0 2016-10-04 08:00:00.000006 2016-10-04 08:00:00
17669817.0 2016-10-04 09:00:00 2016-10-04 09:00:00
17669818.0 2016-10-04 10:00:00 2016-10-04 10:00:00
17669819.0 2016-10-04 11:00:00.000006 2016-10-04 11:00:00
</code></pre>

<p>But while reading the netcdf file:</p>

<pre><code>input_file = '/home/lovechang/test.nc'
data = Dataset(input_file)

times = data.variables['time']
# print times[:]
# print num2date(times[:], units=times.units)
for i in times[:]:
    print i, num2date(i, units=times.units)
</code></pre>

<p>Results:</p>

<pre class="lang-none prettyprint-override"><code>17669813.0 2016-10-04 05:00:00.000006
17669814.0 2016-10-04 06:00:00
17669815.0 2016-10-04 07:00:00
17669816.0 2016-10-04 08:00:00.000006
17669817.0 2016-10-04 09:00:00
17669818.0 2016-10-04 10:00:00
17669819.0 2016-10-04 11:00:00.000006
</code></pre>

<p>Ncview shows the time is not the punctually hour.</p>

<p><img src="https://i.stack.imgur.com/8Y2Ze.png" alt="screenshot of Ncview program running"></p>

<p>So what happened with the times?
And how can I write punctually hour in netcdf file?</p>

<p>Depending on the time units and datatype you choose, you may encounter floating point accuracy problems. For example, if you specify the time in <code>days since 1970-01-01 00:00</code>, 32 bit float is not sufficient and you should use a 64 bit float instead:</p>

<pre><code>import datetime
import netCDF4

times = [datetime.datetime(2016, 10, 1) + datetime.timedelta(hours=hour)
         for hour in range(84)]

# Create netCDF file
calendar = 'standard'
units = 'days since 1970-01-01 00:00'
ds = netCDF4.Dataset('test.nc', 'w')
timedim = ds.createDimension(dimname='time', size=len(times))

# Write timestamps to netCDF file using 32bit float
timevar32 = ds.createVariable(varname='time32', dimensions=('time',),
                              datatype='float32')
timevar32[:] = netCDF4.date2num(times, units=units, calendar=calendar)

# Write timestamps to netCDF file using 64bit float
timevar64 = ds.createVariable(varname='time64', dimensions=('time',),
                              datatype='float64')
timevar64[:] = netCDF4.date2num(times, units=units, calendar=calendar)

# Read timestamps from netCDF file
times32 = netCDF4.num2date(timevar32[:], units=units, calendar=calendar)
times64 = netCDF4.num2date(timevar64[:], units=units, calendar=calendar)
for time, time32, time64 in zip(times, times32, times64):
    print "original  ", time
    print "  32 bit  ", time32
    print "  64 bit  ", time64
    print
</code></pre>

<p>If you specified the time in <code>hours since 2016-10-01 00:00</code>, even an integer would suffice (in this  example).</p>

python netcdf