import numpy as np
import xarray as xr
import netCDF4 as nc
import pyart
import act
## You are using the Python ARM Radar Toolkit (Py-ART), an open source
## library for working with weather radar data. Py-ART is partly
## supported by the U.S. Department of Energy as part of the Atmospheric
## Radiation Measurement (ARM) Climate Research Facility, an Office of
## Science user facility.
##
## If you use this software to prepare a publication, please cite:
##
## JJ Helmus and SM Collis, JORS 2016, doi: 10.5334/jors.119
Grab the CMAC DOD via ACT I/O¶
# Use ACT to query the ARM DOD API and initiate the CMAC DoD
dims = {'time' : 1, 'range' : 668, 'sweep' : 8, 'string_length' : 192}
dod = act.io.create_obj_from_arm_dod('xprecipradarcmacppi.c1', dims, version='2.0', fill_value=-9999)dodLoading...
Create the Encodings for NetCDF output¶
encodings = {
'time' : {'dtype' : np.double, '_FillValue' : -9999.0},
'range' : {'dtype' : np.single, '_FillValue' : -9999.0},
'azimuth' : {'dtype' : np.single, '_FillValue' : -9999.0},
'elevation' : {'dtype' : np.single, '_FillValue' : -9999.0},
'DBZ' : {'dtype' : np.double, '_FillValue' : -32768.0},
'VEL' : {'dtype' : np.double, '_FillValue' : -32768.0},
'WIDTH' : {'dtype' : np.double, '_FillValue' : -32768.0},
'ZDR' : {'dtype' : np.double, '_FillValue' : -32768.0},
'PHIDP' : {'dtype' : np.double, '_FillValue' : -32768.0},
'RHOHV' : {'dtype' : np.double, '_FillValue' : -32768.0},
'NCP' : {'dtype' : np.double, '_FillValue' : -32768.0},
'DBZhv' : {'dtype' : np.double, '_FillValue' : -32768.0},
'cbb_flag' : {'dtype' : np.double, '_FillValue' : -9999.0},
'sounding_temperature' : {'dtype' : np.single, '_FillValue' : -9999.0},
'height' : {'dtype' : np.single, '_FillValue' : -9999.0},
'signal_to_noise_ratio' : {'dtype' : np.single, '_FillValue' : -32768.0},
'velocity_texture' : {'dtype' : np.double, '_FillValue' : -9999.0},
'gate_id' : {'dtype' : 'int16', '_FillValue' : -9999},
'simulated_velocity' : {'dtype' : np.double, '_FillValue' : -32768.0},
'corrected_velocity' : {'dtype' : np.double, '_FillValue' : -32768.0},
'unfolded_differential_phase' : {'dtype' : np.double, '_FillValue' : -32768.0},
'corrected_differential_phase' : {'dtype' : np.double, '_FillValue' : -32768.0},
'filtered_corrected_differential_phase' : {'dtype' : np.double, '_FillValue' : -32768.0},
'corrected_specific_diff_phase' : {'dtype' : np.double, '_FillValue' : -9999.0},
'filtered_corrected_specific_diff_phase' : {'dtype' : np.double, '_FillValue' : -9999.0},
'corrected_differential_reflectivity' : {'dtype' : np.double, '_FillValue' : 1e20},
'corrected_reflectivity' : {'dtype' : np.double, '_FillValue' : 1e20},
'height_over_iso0' : {'dtype' : np.single, '_FillValue' : -9999},
'specific_attenuation' : {'dtype' : np.double, '_FillValue' : 1e20},
'path_integrated_attenuation' : {'dtype' : np.double, '_FillValue' : 1e20},
'specific_differential_attenuation' : {'dtype' : np.double, '_FillValue' : 1e20},
'path_integrated_differential_attenuation' : {'dtype' : np.double, '_FillValue' : 1e20},
'rain_rate_A' : {'dtype' : np.double, '_FillValue' : 1e20},
'snow_rate_ws2012' : {'dtype' : np.double ,'_FillValue' : 1e20},
'snow_rate_ws88diw' : {'dtype' : np.double, '_FillValue' : 1e20},
'snow_rate_m2009_1' : {'dtype' : np.double, '_FillValue' : 1e20},
'snow_rate_m2009_2' : {'dtype' : np.double, '_FillValue' : 1e20},
'sweep_number' : {'dtype' : 'int16', '_FillValue' : -9999},
'fixed_angle' : {'dtype' : np.single, '_FillValue' : -9999.0},
'sweep_start_ray_index' : {'dtype' : 'int16', '_FillValue' : -9999},
'sweep_end_ray_index' : {'dtype' : 'int16', '_FillValue' : -9999},
'sweep_mode' : {'dtype' : np.str_},
'nyquist_velocity' : {'dtype' : np.double, '_FillValue' : -9999.0},
'time_coverage_start' : {'dtype' : np.str_},
'time_coverage_end' : {'dtype' : np.str_},
'time_reference' : {'dtype' : np.str_},
'volume_number' : {'dtype' : 'int16', '_FillValue' : -9999},
'latitude' : {'dtype' : np.double, '_FillValue' : -9999.0},
'longitude' : {'dtype' : np.double, '_FillValue' : -9999.0},
'altitude' : {'dtype' : np.double, '_FillValue' : -9999.0}
}joe = xr.Dataset()nmask = dod['sounding_temperature'].where('time' + 'range' == '-9999', encodings['sounding_temperature']['_FillValue'])
nmask = dod['sounding_temperature'].where('time' + 'range' == -9999, encodings['sounding_temperature']['_FillValue'])nmaskLoading...
Modify the Datatype and Fill Values for the Initial DOD¶
# Create a blank DataSet to hold all the converted values
new_dod = xr.Dataset()
# Add the attributes in
new_dod.attrs = dod.attrsnew_dodLoading...
# Loop over all the variables; update the FillValue and Data Type
for var in encodings:
print(var, dod[var].dims)
# Check if the DataArray is multidimensional (can't have 3 dimensions in this DOD)
if len(dod[var].dims) == 2:
# Character variables won't have a FillValue
if encodings[var]['dtype'] != np.str_:
# Convert all values within the DataArray to the correct Fill Value
# NOTE: xr.where(condition, value_when_condition_is_not_met); so if every index is MVC, check for opposite
mask = dod[var].where(dod[var].dims[0] + dod[var].dims[1] == -9999, encodings[var]['_FillValue'])
# Append to the empty DataSet; make sure to change the data type
new_dod[var] = mask.astype(encodings[var]['dtype'])
# Delete _FillValue attribute; setting on write
if '_FillValue' in new_dod[var].attrs:
del new_dod[var].attrs['_FillValue']
else:
new_dod[var] = dod[var].astype(encodings[var]['dtype'])
# most of the dimensions are one dimensional
elif len(dod[var].dims) == 1:
# Don't want repeating range values
if var == 'range' or var == 'time':
new_dod[var] = dod[var].astype(encodings[var]['dtype'])
if '_FillValue' in new_dod[var].attrs:
del new_dod[var].attrs['_FillValue']
else:
# Character variables won't have a FillValue
if encodings[var]['dtype'] != np.str_:
# Convert all values within the DataArray to the correct Fill Value
mask = dod[var].where(dod[var].dims[0] == -9999, encodings[var]['_FillValue'])
# Append to the empty DataSet; make sure to change the data type
new_dod[var] = mask.astype(encodings[var]['dtype'])
if '_FillValue' in new_dod[var].attrs:
del new_dod[var].attrs['_FillValue']
else:
new_dod[var] = dod[var].astype(encodings[var]['dtype'])
# For the three variables without dimensions
else:
new_dod[var] = dod[var].astype(encodings[var]['dtype'])
if '_FillValue' in new_dod[var].attrs:
del new_dod[var].attrs['_FillValue']time ('time',)
range ('range',)
azimuth ('time',)
elevation ('time',)
DBZ ('time', 'range')
VEL ('time', 'range')
WIDTH ('time', 'range')
ZDR ('time', 'range')
PHIDP ('time', 'range')
RHOHV ('time', 'range')
NCP ('time', 'range')
DBZhv ('time', 'range')
cbb_flag ('time', 'range')
sounding_temperature ('time', 'range')
height ('time', 'range')
signal_to_noise_ratio ('time', 'range')
velocity_texture ('time', 'range')
gate_id ('time', 'range')
simulated_velocity ('time', 'range')
corrected_velocity ('time', 'range')
unfolded_differential_phase ('time', 'range')
corrected_differential_phase ('time', 'range')
filtered_corrected_differential_phase ('time', 'range')
corrected_specific_diff_phase ('time', 'range')
filtered_corrected_specific_diff_phase ('time', 'range')
corrected_differential_reflectivity ('time', 'range')
corrected_reflectivity ('time', 'range')
height_over_iso0 ('time', 'range')
specific_attenuation ('time', 'range')
path_integrated_attenuation ('time', 'range')
specific_differential_attenuation ('time', 'range')
path_integrated_differential_attenuation ('time', 'range')
rain_rate_A ('time', 'range')
snow_rate_ws2012 ('time', 'range')
snow_rate_ws88diw ('time', 'range')
snow_rate_m2009_1 ('time', 'range')
snow_rate_m2009_2 ('time', 'range')
sweep_number ('sweep',)
fixed_angle ('sweep',)
sweep_start_ray_index ('sweep',)
sweep_end_ray_index ('sweep',)
sweep_mode ('sweep', 'string_length')
nyquist_velocity ('time',)
time_coverage_start ('string_length',)
time_coverage_end ('string_length',)
time_reference ('string_length',)
volume_number ()
latitude ()
longitude ()
altitude ()
new_dodLoading...
# Fix some meta data issues
del new_dod['sounding_temperature'].attrs['missing_value']
del new_dod['height'].attrs['missing_value']
del new_dod['velocity_texture'].attrs['missing_value']Write the File / Read It Back in to Test¶
new_dod.to_netcdf('sail_cmac_dod.nc', encoding=encodings)test = xr.open_dataset('sail_cmac_dod.nc', mask_and_scale=False)testLoading...
test.DBZ.dataarray([[-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768., -32768., -32768., -32768., -32768.,
-32768., -32768., -32768.]])dod_v2 = nc.Dataset('/Users/jrobrien/ARM/data/CSU-XPrecipRadar/dod/cmac_dod_v2.nc')dod_v2<class 'netCDF4._netCDF4.Dataset'>
root group (NETCDF4 data model, file format HDF5):
command_line:
Conventions: ARM-1.3 CF/Radial instrument_parameters
process_version:
dod_version:
input_datastreams:
site_id:
platform_id:
facility_id:
data_level:
location_description:
datastream:
institution: U.S. Department of Energy Atmospheric Radiation Measurement (ARM) Climate Research Facility
references: See XPRECIPRADAR Instrument Handbook
doi: 10.5439/1883164
comment: This is highly experimental and initial data. There are many known and unknown issues. Please do not use before contacting the Translator responsible scollis@anl.gov
attributions: This data is collected by the ARM Climate Research facility. Radar system is operated by the radar engineering team radar@arm.gov and the data is processed by the precipitation radar products team. LP code courtesy of Scott Giangrande BNL.
vap_name: cmac
known_issues: False phidp jumps in insect regions. Still uses old Giangrande code. Issues with some snow below melting layer.
developers: Robert Jackson, ANL. Zachary Sherman, ANL. Maxwell Grover, ANL. Joseph O'Brien, ANL.
translator: https://www.arm.gov/capabilities/instruments/xprecipradar
mentors: https://www.arm.gov/connect-with-arm/organization/instrument-mentors/list#xprecipradar
source: Colorado State University's X-Band Precipitation Radar (XPRECIPRADAR) (DOI: 10.5439/1844501)
input_datastream: xprecipradarS2.00
field_names: DBZ, VEL, WIDTH, ZDR, PHIDP, RHOHV, NCP, DBZhv, cbb_flag, sounding_temperature, height, signal_to_noise_ratio, velocity_texture, gate_id, simulated_velocity, corrected_velocity, unfolded_differential_phase, corrected_differential_phase, filtered_corrected_differential_phase, corrected_specific_diff_phase, filtered_corrected_specific_diff_phase, corrected_differential_reflectivity, corrected_reflectivity, height_over_iso0, specific_attenuation, path_integrated_attenuation, specific_differential_attenuation, path_integrated_differential_attenuation, rain_rate_A, snow_rate_ws2012, snow_rate_ws88diw, snow_rate_m2009_1, snow_rate_m2009_2
history:
dimensions(sizes): time(1), range(668), sweep(8), string_length(192)
variables(dimensions): int64 time(time), int64 range(range), float64 DBZ(time, range), float64 VEL(time, range), float64 WIDTH(time, range), float64 ZDR(time, range), float64 PHIDP(time, range), float64 RHOHV(time, range), float64 NCP(time, range), float64 DBZhv(time, range), float64 sounding_temperature(time, range), float64 height(time, range), float64 signal_to_noise_ratio(time, range), float64 velocity_texture(time, range), float64 corrected_velocity(time, range), float64 unfolded_differential_phase(time, range), float64 corrected_differential_phase(time, range), float64 filtered_corrected_differential_phase(time, range), float64 corrected_specific_diff_phase(time, range), float64 filtered_corrected_specific_diff_phase(time, range), float64 corrected_differential_reflectivity(time, range), float64 corrected_reflectivity(time, range), float64 specific_attenuation(time, range), float64 path_integrated_attenuation(time, range), float64 specific_differential_attenuation(time, range), float64 path_integrated_differential_attenuation(time, range), float64 rain_rate_A(time, range), float64 snow_rate_ws2012(time, range), float64 snow_rate_ws88diw(time, range), float64 snow_rate_m2009_1(time, range), float64 snow_rate_m2009_2(time, range), float64 sweep_number(sweep), float64 fixed_angle(sweep), float64 sweep_start_ray_index(sweep), float64 sweep_end_ray_index(sweep), <class 'str'> sweep_mode(sweep, string_length), <class 'str'> time_coverage_start(string_length), <class 'str'> time_coverage_end(string_length), <class 'str'> time_reference(string_length), float64 azimuth(time), float64 elevation(time), float64 cbb_flag(time, range), float64 gate_id(time, range), float64 simulated_velocity(time, range), float64 height_over_iso0(time, range), float64 nyquist_velocity(time), float64 volume_number(), float64 latitude(), float64 longitude(), float64 altitude()
groups: