Skip to content

Commit

Permalink
ENH: ARM time vars can be written with write_cfradial
Browse files Browse the repository at this point in the history
* added `arm_time_variables` flag to write_cfradial function which
  when True will create netCDF files with base_time and time_offset
  variables
  • Loading branch information
jjhelmus committed Feb 11, 2014
1 parent f30f39d commit 5585d6c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
28 changes: 27 additions & 1 deletion pyart/io/cfradial.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,8 @@ def _stream_to_2d(data, sweeps, sweepe, ray_len, maxgates, nrays,
return time_range


def write_cfradial(filename, radar, format='NETCDF4', time_reference=False):
def write_cfradial(filename, radar, format='NETCDF4', time_reference=False,
arm_time_variables=False):
"""
Write a Radar object to a CF/Radial compliant netCDF file.
Expand All @@ -314,6 +315,9 @@ def write_cfradial(filename, radar, format='NETCDF4', time_reference=False):
time_reference : bool
True to include a time_reference variable, False will not include
this variable.
arm_time_variables : bool
True to create the ARM standard time variables base_time and
time_offset, False will not create these variables.
"""
dataset = netCDF4.Dataset(filename, 'w', format=format)
Expand Down Expand Up @@ -364,6 +368,28 @@ def write_cfradial(filename, radar, format='NETCDF4', time_reference=False):
# history should be the last attribute, ARM standard
dataset.setncattr('history', history)

# arm time variables base_time and time_offset if requested
if arm_time_variables:
dt = netCDF4.num2date(radar.time['data'][0], radar.time['units'])
td = dt - datetime.datetime.utcfromtimestamp(0)
base_time = {
'data': np.array([td.seconds + td.days * 24 * 3600], 'int32'),
'string': dt.strftime('%d-%b-%Y,%H:%M:%S GMT'),
'units': 'seconds since 1970-1-1 0:00:00 0:00',
'ancillary_variables': 'time_offset',
'long_name': 'Base time in Epoch',
}
_create_ncvar(base_time, dataset, 'base_time', ())

time_offset = {
'data': radar.time['data'],
'long_name': 'Time offset from base_time',
'units': radar.time['units'].replace('T', ' ').replace('Z', ''),
'ancillary_variables': 'time_offset',
'calendar': 'gregorian',
}
_create_ncvar(time_offset, dataset, 'time_offset', ('time', ))

# standard variables
_create_ncvar(radar.time, dataset, 'time', ('time', ))
_create_ncvar(radar.range, dataset, 'range', ('range', ))
Expand Down
20 changes: 20 additions & 0 deletions pyart/io/tests/test_cfradial.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,26 @@ def test_write_rhi():
os.remove(tmpfile)


def test_write_ppi_arm_time_vars():
# CF/Radial example file -> Radar object -> netCDF file
tmpfile = tempfile.mkstemp(suffix='.nc', dir='.')[1]
radar = pyart.io.read_cfradial(pyart.testing.CFRADIAL_PPI_FILE)
pyart.io.write_cfradial(tmpfile, radar, arm_time_variables=True)
dset = netCDF4.Dataset(tmpfile)
assert 'base_time' in dset.variables
assert 'time_offset' in dset.variables

base_time = dset.variables['base_time']
assert base_time[0] == 1305888856
assert base_time.string == '20-May-2011,10:54:16 GMT'

time_offset = dset.variables['time_offset']
assert time_offset.units == 'seconds since 2011-05-20 10:54:16'
assert round(time_offset[10]) == 4
dset.close()
os.remove(tmpfile)


def check_dataset_to_ref(dset, ref):
""" Check that all data in Dataset is contained in the ref Dataset. """

Expand Down

0 comments on commit 5585d6c

Please sign in to comment.