Skip to content

Commit

Permalink
Apply black -S -l 99
Browse files Browse the repository at this point in the history
  • Loading branch information
alexamici committed May 14, 2019
1 parent 9eccfb6 commit 095b855
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 79 deletions.
38 changes: 23 additions & 15 deletions cf2cdm/cfcoords.py
Expand Up @@ -55,8 +55,13 @@ def translate_direction(data, out_name, stored_direction):


def coord_translator(
default_out_name, default_units, default_direction, is_cf_type, cf_type, data,
coord_model=COORD_MODEL
default_out_name,
default_units,
default_direction,
is_cf_type,
cf_type,
data,
coord_model=COORD_MODEL,
):
# type: (str, str, str, T.Callable, str, xr.DataArray, dict) -> xr.DataArray
out_name = coord_model.get(cf_type, {}).get('out_name', default_out_name)
Expand Down Expand Up @@ -91,7 +96,7 @@ def is_latitude(coord):


COORD_TRANSLATORS['latitude'] = functools.partial(
coord_translator, 'latitude', 'degrees_north', 'decreasing', is_latitude,
coord_translator, 'latitude', 'degrees_north', 'decreasing', is_latitude
)


Expand All @@ -104,7 +109,7 @@ def is_longitude(coord):


COORD_TRANSLATORS['longitude'] = functools.partial(
coord_translator, 'longitude', 'degrees_east', 'increasing', is_longitude,
coord_translator, 'longitude', 'degrees_east', 'increasing', is_longitude
)


Expand All @@ -117,7 +122,7 @@ def is_time(coord):


COORD_TRANSLATORS['time'] = functools.partial(
coord_translator, 'time', TIME_CF_UNITS, 'increasing', is_time,
coord_translator, 'time', TIME_CF_UNITS, 'increasing', is_time
)


Expand All @@ -126,9 +131,7 @@ def is_step(coord):
return coord.attrs.get('standard_name') == 'forecast_period'


COORD_TRANSLATORS['step'] = functools.partial(
coord_translator, 'step', 'h', 'increasing', is_step,
)
COORD_TRANSLATORS['step'] = functools.partial(coord_translator, 'step', 'h', 'increasing', is_step)


def is_valid_time(coord):
Expand All @@ -141,7 +144,7 @@ def is_valid_time(coord):


COORD_TRANSLATORS['valid_time'] = functools.partial(
coord_translator, 'valid_time', TIME_CF_UNITS, 'increasing', is_valid_time,
coord_translator, 'valid_time', TIME_CF_UNITS, 'increasing', is_valid_time
)


Expand All @@ -151,7 +154,7 @@ def is_depth(coord):


COORD_TRANSLATORS['depthBelowLand'] = functools.partial(
coord_translator, 'depthBelowLand', 'm', 'decreasing', is_depth,
coord_translator, 'depthBelowLand', 'm', 'decreasing', is_depth
)


Expand All @@ -161,7 +164,7 @@ def is_isobaric(coord):


COORD_TRANSLATORS['isobaricInhPa'] = functools.partial(
coord_translator, 'isobaricInhPa', 'hPa', 'decreasing', is_isobaric,
coord_translator, 'isobaricInhPa', 'hPa', 'decreasing', is_isobaric
)


Expand All @@ -171,12 +174,12 @@ def is_number(coord):


COORD_TRANSLATORS['number'] = functools.partial(
coord_translator, 'number', '1', 'increasing', is_number,
coord_translator, 'number', '1', 'increasing', is_number
)


def translate_coords(
data, coord_model=COORD_MODEL, errors='warn', coord_translators=COORD_TRANSLATORS
data, coord_model=COORD_MODEL, errors='warn', coord_translators=COORD_TRANSLATORS
):
# type: (xr.Dataset, T.Dict, str, T.Dict) -> xr.Dataset
for cf_name, translator in coord_translators.items():
Expand Down Expand Up @@ -233,8 +236,13 @@ def ensure_valid_time(data):
if step and step in data.dims and data.coords[step].size == data.coords[valid_time].size:
return data.swap_dims({step: valid_time})
# also convert is valid_time can index all times and steps
if step and time and step in data.dims and time in data.dims and \
data.coords[step].size * data.coords[time].size == data.coords[valid_time].size:
if (
step
and time
and step in data.dims
and time in data.dims
and data.coords[step].size * data.coords[time].size == data.coords[valid_time].size
):
data = data.stack(tmp_coord=(time, step))
data = data.swap_dims({'tmp_coord': valid_time}).drop('tmp_coord').dropna(valid_time)
return data
16 changes: 8 additions & 8 deletions cf2cdm/cfunits.py
Expand Up @@ -21,17 +21,17 @@


PRESSURE_CONVERSION_RULES = {
('Pa', 'pascal', 'pascals'): 1.,
('hPa', 'hectopascal', 'hectopascals', 'hpascal', 'millibar', 'millibars', 'mbar'): 100.,
('decibar', 'dbar'): 10000.,
('bar', 'bars'): 100000.,
('atmosphere', 'atmospheres', 'atm'): 101325.,
('Pa', 'pascal', 'pascals'): 1.0,
('hPa', 'hectopascal', 'hectopascals', 'hpascal', 'millibar', 'millibars', 'mbar'): 100.0,
('decibar', 'dbar'): 10000.0,
('bar', 'bars'): 100000.0,
('atmosphere', 'atmospheres', 'atm'): 101325.0,
} # type: T.Dict[T.Tuple, float]

LENGTH_CONVERSION_RULES = {
('m', 'meter', 'meters'): 1.,
('m', 'meter', 'meters'): 1.0,
('cm', 'centimeter', 'centimeters'): 0.01,
('km', 'kilometer', 'kilometers'): 1000.,
('km', 'kilometer', 'kilometers'): 1000.0,
}


Expand All @@ -41,7 +41,7 @@ class ConversionError(Exception):

def simple_conversion_factor(source_units, target_units, rules):
# type: (str, str, T.Dict[T.Tuple, float]) -> float
conversion_factor = 1.
conversion_factor = 1.0
seen = 0
for pressure_units, factor in rules.items():
if source_units in pressure_units:
Expand Down
69 changes: 13 additions & 56 deletions cf2cdm/datamodels.py
Expand Up @@ -19,67 +19,24 @@

CDS = {
# geography
'latitude': {
'out_name': 'lat',
'stored_direction': 'increasing',
},
'longitude': {
'out_name': 'lon',
'stored_direction': 'increasing',
},
'latitude': {'out_name': 'lat', 'stored_direction': 'increasing'},
'longitude': {'out_name': 'lon', 'stored_direction': 'increasing'},
# vertical
'depthBelowLand': {
'out_name': 'depth',
'units': 'm',
'stored_direction': 'increasing',
},
'isobaricInhPa': {
'out_name': 'plev',
'units': 'Pa',
'stored_direction': 'decreasing',
},
'depthBelowLand': {'out_name': 'depth', 'units': 'm', 'stored_direction': 'increasing'},
'isobaricInhPa': {'out_name': 'plev', 'units': 'Pa', 'stored_direction': 'decreasing'},
# ensemble
'number': {
'out_name': 'realization',
'stored_direction': 'increasing',
},
'number': {'out_name': 'realization', 'stored_direction': 'increasing'},
# time
'time': {
'out_name': 'forecast_reference_time',
'stored_direction': 'increasing',
},
'valid_time': {
'out_name': 'time',
'stored_direction': 'increasing',
},
'step': {
'out_name': 'leadtime',
'stored_direction': 'increasing',
},
'config': {
'preferred_time_dimension': 'valid_time',
},
'time': {'out_name': 'forecast_reference_time', 'stored_direction': 'increasing'},
'valid_time': {'out_name': 'time', 'stored_direction': 'increasing'},
'step': {'out_name': 'leadtime', 'stored_direction': 'increasing'},
'config': {'preferred_time_dimension': 'valid_time'},
}


ECMWF = {
'depthBelowLand': {
'out_name': 'level',
'units': 'm',
'stored_direction': 'increasing',
},
'isobaricInhPa': {
'out_name': 'level',
'units': 'hPa',
'stored_direction': 'decreasing',
},
'isobaricInPa': {
'out_name': 'level',
'units': 'hPa',
'stored_direction': 'decreasing',
},
'hybrid': {
'out_name': 'level',
'stored_direction': 'increasing',
},
'depthBelowLand': {'out_name': 'level', 'units': 'm', 'stored_direction': 'increasing'},
'isobaricInhPa': {'out_name': 'level', 'units': 'hPa', 'stored_direction': 'decreasing'},
'isobaricInPa': {'out_name': 'level', 'units': 'hPa', 'stored_direction': 'decreasing'},
'hybrid': {'out_name': 'level', 'stored_direction': 'increasing'},
}

0 comments on commit 095b855

Please sign in to comment.