Skip to content

Commit

Permalink
Convert prsn data from mm to cm
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Yvorchuk committed May 12, 2020
1 parent ead2234 commit 5b81f23
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
11 changes: 7 additions & 4 deletions dp/generate_climos.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,11 +442,14 @@ def convert_flux_var_units(input_file, climo_data):
flux_variable = input_file.variables[flux_var]
units = Unit.from_udunits_str(flux_variable.units)

if units in [Unit('kg / m**2 / s'), Unit('mm / s')]:
logger.info("Converting {} variable to units mm/day".format(flux_var))
if units in [Unit('kg / m**2 / s'), Unit('mm / s'), Unit('g / cm**2 / s'), Unit('cm / s')]:
if units in [Unit('kg / m**2 / s'), Unit('mm / s')]:
logger.info("Converting {} variable to units mm/day".format(flux_var))
else:
logger.info("Converting {} variable to units cm/day".format(flux_var))
# Update units attribute
attributes['units'] = (units * Unit('s / day')).to_udunits_str()
# Multiply values by 86400 to convert from mm/s to mm/day
# Multiply values by 86400 to convert from mm/s to mm/day or from cm/s to cm/day
seconds_per_day = 86400

if hasattr(flux_variable, 'scale_factor') or hasattr(flux_variable, 'add_offset'):
Expand All @@ -463,7 +466,7 @@ def convert_flux_var_units(input_file, climo_data):
# This is not a packed file; modify the values proper
# Extract variable
var_only = cdo.select('name={}'.format(flux_var), input=climo_data)
# Multiply values by 86400 to convert from mm/s to mm/day
# Multiply values by 86400 to convert from mm/s to mm/day or from cm/s to cm/day
var_only = cdo.mulc(str(seconds_per_day), input=var_only)
# Replace pr in all-variables file
climo_data = cdo.replace(input=[climo_data, var_only])
Expand Down
14 changes: 13 additions & 1 deletion dp/generate_prsn.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def create_prsn_netcdf_from_source(src, dst, to_delete=['original_name, comment'

prsn_var.standard_name = 'snowfall_flux'
prsn_var.long_name = 'Precipitation as Snow'
prsn_var.units = 'g cm-2 s-1'

for item in set(to_delete):
if hasattr(prsn_var, item):
Expand Down Expand Up @@ -166,8 +167,14 @@ def check_pr_units(pr_units):
valid_units = [
Unit('kg / m**2 / s'),
Unit('mm / s'),
Unit('mm / d'),
Unit('kg / d / m**2'),
Unit('kg / m**2 / d')
Unit('kg / m**2 / d'),
Unit('g / cm**2 / s'),
Unit('cm / s'),
Unit('cm / d'),
Unit('g / d / cm**2'),
Unit('g / cm**2 / d')
]
units = Unit.from_udunits_str(pr_units)
return units in valid_units
Expand Down Expand Up @@ -222,6 +229,11 @@ def process_to_prsn(variables, output_dataset, max_chunk_len):
}
means = np.mean([chunk_data['tasmin'], chunk_data['tasmax']], axis=0)
prsn_data = np.where(means < freezing, chunk_data['pr'], 0)

prsn_units_from = ureg.parse_units('kg/m^2/s')
prsn_units_to = ureg.parse_units('g/cm^2/s')
prsn_data = prsn_data * Q_(1.0, prsn_units_from).to(prsn_units_to).magnitude

output_dataset.variables['prsn'][chunk] = prsn_data


Expand Down
2 changes: 1 addition & 1 deletion tests/test_generate_prsn.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def test_create_prsn_netcdf_from_source(tiny_dataset, fake_dataset):
assert np.shape(fake_dataset.variables['prsn']) == (11688, 4, 2)
assert fake_dataset.variables['prsn'].standard_name == 'snowfall_flux'
assert fake_dataset.variables['prsn'].long_name == 'Precipitation as Snow'

assert fake_dataset.variables['prsn'].units == 'g cm-2 s-1'

@pytest.mark.parametrize('tiny_dataset, new_var, expected', [
('downscaled_pr', 'prsn', 'prsn_day_BCCAQ2_ACCESS1-0_ACCESS1-0+historical+rcp45+r1i1p1_r1i1p1_19600101-19911231.nc'),
Expand Down

0 comments on commit 5b81f23

Please sign in to comment.