Tutorial on how to use timestaps in Field construction

In [1]:
from parcels import Field
from glob import glob
import numpy as np

Some NetCDF files, such as for example those from the World Ocean Atlas, have time calendars that can't be parsed by xarray. These result in a ValueError: unable to decode time units, for example when the calendar is in 'months since' a particular date.

In these cases, a workaround in Parcels is to use the timestamps argument in Field (or FieldSet) creation. Here, we show how this works for example temperature data from the World Ocean Atlas in the Pacific Ocean

The following cell would give an error, since the calendar of the World Ocean Atlas data is in "months since 1955-01-01 00:00:00"

In [2]:
# tempfield = Field.from_netcdf(glob('WOA_data/woa18_decav_*_04.nc'), 't_an', 
#                               {'lon': 'lon', 'lat': 'lat', 'time': 'time'})

However, we can create our own numpy array of timestamps associated with each of the 12 snapshots in the netcdf file

In [3]:
timestamps = np.expand_dims(np.array([np.datetime64('2001-%.2d-15' %m) for m in range(1,13)]), axis=1)

And then we can add the timestamps as an extra argument

In [4]:
tempfield = Field.from_netcdf(glob('WOA_data/woa18_decav_*_04.nc'), 't_an', 
                              {'lon': 'lon', 'lat': 'lat', 'time': 'time'}, 
                              timestamps=timestamps)
WARNING: File WOA_data/woa18_decav_t10_04.nc could not be decoded properly by xarray (version 0.12.1).
         It will be opened with no decoding. Filling values might be wrongly parsed.
WARNING: Casting depth data to np.float32

Note, by the way, that adding the time_periodic=True argument to Field.from_netcdf() will also mean that the climatology can be cycled for multiple years.