Skip to content

Commit

Permalink
Run "isort" and "black"
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Oct 7, 2021
1 parent 631bb27 commit 0d795ea
Show file tree
Hide file tree
Showing 30 changed files with 1,096 additions and 637 deletions.
21 changes: 19 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# -------------
# Configuration
# -------------

$(eval venvpath := .venv)
$(eval pip := $(venvpath)/bin/pip)
$(eval python := $(venvpath)/bin/python)
$(eval pytest := $(venvpath)/bin/pytest)
$(eval black := $(venvpath)/bin/black)
$(eval isort := $(venvpath)/bin/isort)


MAGICS_PREFIX = /usr/local/opt/magics
PATH_TESTDATA_INPUT = $(PWD)/.gribmagic-testdata/input
PATH_TESTDATA_OUTPUT = $(PWD)/.gribmagic-testdata/output
Expand All @@ -16,14 +28,19 @@ testoutput-clean:
rm $(PATH_TESTDATA_OUTPUT)/* || true

test: testdata-download testoutput-clean
@pytest -vvv tests
@$(pytest) -vvv tests

test-coverage: testdata-download testoutput-clean
@pytest -vvv tests \
@$(pytest) -vvv tests \
--cov=gribmagic \
--cov-report=term-missing \
--cov-report=xml

format:
@$(pip) install --requirement=requirements-dev.txt --quiet
$(isort) --profile=black gribmagic tests
$(black) gribmagic tests

install-magics:
mkdir -p tmp/download tmp/build/magics
wget https://confluence.ecmwf.int/download/attachments/3473464/Magics-4.5.2-Source.tar.gz \
Expand Down
8 changes: 5 additions & 3 deletions gribmagic/commands.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Command line entry points for NWP data download"""
import logging
from datetime import datetime
from typing import Union

import click
import logging

from gribmagic.unity.enumerations.weather_models import WeatherModels
from gribmagic.unity.core import run_model_download
from gribmagic.unity.enumerations.weather_models import WeatherModels
from gribmagic.util import setup_logging

logger = logging.getLogger(__name__)
Expand All @@ -29,7 +29,9 @@ def main(ctx):
@click.option("--timestamp", required=True, help="The initialization timestamp.")
def unity(model: Union[str, WeatherModels], timestamp: Union[str, datetime]):
logger.info("Starting GribMagic")
results = run_model_download(weather_model=model, initialization_timestamp=timestamp)
results = run_model_download(
weather_model=model, initialization_timestamp=timestamp
)

# If a func call raises an exception, then that exception will be raised
# when its value is retrieved from the iterator.
Expand Down
27 changes: 14 additions & 13 deletions gribmagic/unity/core.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
""" pre defined pipelines for download """
from gribmagic.unity.modules.file_list_handling.file_list_handling import \
build_model_file_lists
from gribmagic.unity.enumerations.weather_models import WeatherModels
from datetime import datetime
from gribmagic.unity.modules.time.time_parsing import \
convert_iso_timestamp_to_date_time
from typing import Union

from gribmagic.unity.enumerations.weather_models import WeatherModels
from gribmagic.unity.modules.download.download import download
from gribmagic.unity.modules.file_list_handling.file_list_handling import (
build_model_file_lists,
)
from gribmagic.unity.modules.time.time_parsing import convert_iso_timestamp_to_date_time


def run_model_download(
weather_model: Union[str, WeatherModels],
initialization_timestamp: Union[str, datetime]
weather_model: Union[str, WeatherModels],
initialization_timestamp: Union[str, datetime],
) -> None:
"""
Run a full stack model download as defined in `model_config.yml`.
Args:
weather_model: is one of
weather_model: is one of
- dwd-icon-global, dwd-icon-eu, dwd-icon-eu-eps
- dwd-cosmo-d2, dwd-cosmo-d2-eps
- ncep-gfs-025, meteo-france-arome, knmi-harmonie
initialization_timestamp:
initialization_timestamp:
- nwp run initialization time
Returns:
Expand All @@ -32,9 +33,9 @@ def run_model_download(

if isinstance(initialization_timestamp, str):
initialization_timestamp = convert_iso_timestamp_to_date_time(
initialization_timestamp)
initialization_timestamp
)
model_file_list = build_model_file_lists(
weather_model,
initialization_timestamp.hour,
initialization_timestamp.date())
weather_model, initialization_timestamp.hour, initialization_timestamp.date()
)
return download(weather_model, model_file_list, parallel_download=True)
25 changes: 12 additions & 13 deletions gribmagic/unity/enumerations/level_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@


class LevelTypes(Enum):
"""Enumeration of weather models levels type """
SINGLE_LEVEL = 'single_level'
MULTI_LEVEL = 'multi_level'
HEIGHT_ABOVE_GROUND = 'heightAboveGround'
ENTIRE_ATMOSPHERE = 'entireAtmosphere'
ISOBARIC_HPA = 'isobaricInhPa'
ISOBARIC_PA = 'isobaricInPa'
SURFACE = 'surface'
SIGMA = 'sigma'
SIGMA_LAYER = 'sigmaLayer'
HYBRID = 'hybrid'
TROPOPAUSE = 'tropopause'

"""Enumeration of weather models levels type"""

SINGLE_LEVEL = "single_level"
MULTI_LEVEL = "multi_level"
HEIGHT_ABOVE_GROUND = "heightAboveGround"
ENTIRE_ATMOSPHERE = "entireAtmosphere"
ISOBARIC_HPA = "isobaricInhPa"
ISOBARIC_PA = "isobaricInPa"
SURFACE = "surface"
SIGMA = "sigma"
SIGMA_LAYER = "sigmaLayer"
HYBRID = "hybrid"
TROPOPAUSE = "tropopause"
111 changes: 56 additions & 55 deletions gribmagic/unity/enumerations/unified_forecast_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,56 @@

class ForecastVariables(Enum):
"""Enumeration of variables from weather_models"""
AIR_TEMPERATURE_2M = 'air_temperature_2m'
SNOW_HEIGHT = 'snow_height'
MAX_WIND_GUST_10M = 'max_wind_gust_10m'
WIND_U_10M = 'wind_u_10m'
WIND_V_10M = 'wind_v_10m'
DEWPOINT_2M = 'dewpoint_2m'
TOTAL_PRECIPITATION = 'total_precipitation'
CONVECTIVE_SNOW = 'convective_snow'
GRID_SCALE_SNOW = 'grid_scale_snow'
CONVECTIVE_RAIN = 'convective_rain'
GRID_SCALE_RAIN = 'grid_scale_rain'
GLOBAL_HORIZONTAL_IRRADIANCE = 'global_horizontal_irradiance'
DIFFUSE_HORIZONTAL_IRRADIANCE = 'diffuse_horizontal_irradiance'
DIRECT_NORMAL_IRRADIANCE = 'direct_normal_irradiance'
MIXED_LAYER_CAPE = 'mixed_layer_cape'
MOST_UNSTABLE_CAPE = 'most_unstable_cape'
SURFACE_BASED_CAPE = 'surface_based_cape'
PRESSURE_MEAN_SEA_LEVEL = 'pressure_mean_sea_level'
PRESSURE_MEAN_SEA_LEVEL_ETA_LEVEL = 'pressure_mean_sea_level_eta_level'
MAX_AIR_TEMPERATURE_2M = 'max_air_temperature_2m'
MIN_AIR_TEMPERATURE_2M = 'min_air_temperature_2m'
WEATHER_SYNOP_CODE = 'weather_synop_code'
SOIL_TEMPERATURE = 'soil_temperature'
TOTAL_CLOUD_COVER = 'total_cloud_cover'
RELATIVE_HUMIDITY_2M = 'relative_humidity_2m'
RELATIVE_HUMIDITY = 'relative_humidity'
GEOPOTENTIAL_HEIGHT = 'geopotential_height'
SUNSHINE_DURATION = 'sunshine_duration'
LOW_LEVEL_CLOUDS = 'low_level_clouds'
MEDIUM_LEVEL_CLOUDS = 'medium_level_clouds'
HIGH_LEVEL_CLOUDS = 'high_level_clouds'
TOTAL_PRECIPITATION_RATE = 'total_precipitation_rate'
CONVECTIVE_PRECIPITATION_RATE = 'convective_precipitation_rate'
SUPERCELL_DETECTION_INDEX_1 = 'supercell_detection_index_1'
SUPERCELL_DETECTION_INDEX_2 = 'supercell_detection_index_2'
ALBEDO = 'albedo'
MAX_RADAR_REFLECTIVITY = 'max_radar_reflectivity'
WIND_U = 'wind_u'
WIND_V = 'wind_v'
TURBULENT_KINETIC_ENERGY = 'turbulent_kinetic_energy'
SPECIFIC_HUMIDITY = 'specific_humidity'
SURFACE_SPECIFIC_HUMIDITY = 'surface_specific_humidity'
PRESSURE_SURFACE = 'pressure_surface'
SOIL_MOISTURE_CONTENT = 'soil_moisture_content'
APPARENT_TEMPERATURE = 'apparent_temperature'
STORM_MOTION_U = 'storm_motion_u'
STORM_MOTION_V = 'storm_motion_v'
STORM_RELATIVE_HELICITY = 'storm_relative_helicity'
ICE_GROWTH_RATE = 'ice_growth_rate'

AIR_TEMPERATURE_2M = "air_temperature_2m"
SNOW_HEIGHT = "snow_height"
MAX_WIND_GUST_10M = "max_wind_gust_10m"
WIND_U_10M = "wind_u_10m"
WIND_V_10M = "wind_v_10m"
DEWPOINT_2M = "dewpoint_2m"
TOTAL_PRECIPITATION = "total_precipitation"
CONVECTIVE_SNOW = "convective_snow"
GRID_SCALE_SNOW = "grid_scale_snow"
CONVECTIVE_RAIN = "convective_rain"
GRID_SCALE_RAIN = "grid_scale_rain"
GLOBAL_HORIZONTAL_IRRADIANCE = "global_horizontal_irradiance"
DIFFUSE_HORIZONTAL_IRRADIANCE = "diffuse_horizontal_irradiance"
DIRECT_NORMAL_IRRADIANCE = "direct_normal_irradiance"
MIXED_LAYER_CAPE = "mixed_layer_cape"
MOST_UNSTABLE_CAPE = "most_unstable_cape"
SURFACE_BASED_CAPE = "surface_based_cape"
PRESSURE_MEAN_SEA_LEVEL = "pressure_mean_sea_level"
PRESSURE_MEAN_SEA_LEVEL_ETA_LEVEL = "pressure_mean_sea_level_eta_level"
MAX_AIR_TEMPERATURE_2M = "max_air_temperature_2m"
MIN_AIR_TEMPERATURE_2M = "min_air_temperature_2m"
WEATHER_SYNOP_CODE = "weather_synop_code"
SOIL_TEMPERATURE = "soil_temperature"
TOTAL_CLOUD_COVER = "total_cloud_cover"
RELATIVE_HUMIDITY_2M = "relative_humidity_2m"
RELATIVE_HUMIDITY = "relative_humidity"
GEOPOTENTIAL_HEIGHT = "geopotential_height"
SUNSHINE_DURATION = "sunshine_duration"
LOW_LEVEL_CLOUDS = "low_level_clouds"
MEDIUM_LEVEL_CLOUDS = "medium_level_clouds"
HIGH_LEVEL_CLOUDS = "high_level_clouds"
TOTAL_PRECIPITATION_RATE = "total_precipitation_rate"
CONVECTIVE_PRECIPITATION_RATE = "convective_precipitation_rate"
SUPERCELL_DETECTION_INDEX_1 = "supercell_detection_index_1"
SUPERCELL_DETECTION_INDEX_2 = "supercell_detection_index_2"
ALBEDO = "albedo"
MAX_RADAR_REFLECTIVITY = "max_radar_reflectivity"
WIND_U = "wind_u"
WIND_V = "wind_v"
TURBULENT_KINETIC_ENERGY = "turbulent_kinetic_energy"
SPECIFIC_HUMIDITY = "specific_humidity"
SURFACE_SPECIFIC_HUMIDITY = "surface_specific_humidity"
PRESSURE_SURFACE = "pressure_surface"
SOIL_MOISTURE_CONTENT = "soil_moisture_content"
APPARENT_TEMPERATURE = "apparent_temperature"
STORM_MOTION_U = "storm_motion_u"
STORM_MOTION_V = "storm_motion_v"
STORM_RELATIVE_HELICITY = "storm_relative_helicity"
ICE_GROWTH_RATE = "ice_growth_rate"
CLOUD_MIXING_RATIO = "cloud_mixing_ratio"
ICE_WATER_MIXING_RATIO = "ice_water_mixing_ratio"
RAIN_WATER_MIXING_RATIO = "rain_water_mixing_ratio"
Expand All @@ -71,7 +72,7 @@ class ForecastVariables(Enum):
DOWNWARD_LONG_WAVE_RADIATION = "downward_long_wave_radiation"
CONVECTIVE_INHIBITION = "convective_inhibition"
PRESSURE_LEVEL_PARCEL_LIFT = "pressure_level_parcel-lift"
potential_temperature = 'potential_temperature'
potential_temperature = "potential_temperature"
SEA_ICE_COVER = "sea_ice_cover"
SURFACE_AIR_PRESSURE = "surface_air_pressure"
LAND_SEA_MASK = "land_sea_mask"
Expand Down Expand Up @@ -103,8 +104,8 @@ class ForecastVariables(Enum):
CLOUD_WORK_FUNCTION = "cloud_work_function"
OZONE_TOTALE = "ozone_totale"
VENTILATION_RATE = "ventilation_rate"
MAX_WIND_GUST_U = 'max_wind_gust_u'
MAX_WIND_GUST_V = 'max_wind_gust_v'
GRAUPEL = 'graupel'
MIXED_LAYER_DEPTH = 'mixed_layer_depth'
SNOW_RATE = 'snow_rate'
MAX_WIND_GUST_U = "max_wind_gust_u"
MAX_WIND_GUST_V = "max_wind_gust_v"
GRAUPEL = "graupel"
MIXED_LAYER_DEPTH = "mixed_layer_depth"
SNOW_RATE = "snow_rate"
27 changes: 14 additions & 13 deletions gribmagic/unity/enumerations/weather_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@

class WeatherModels(Enum):
"""Enumeration of weather models"""
DWD_ICON_EU = 'dwd-icon-eu'
DWD_ICON_EU_EPS = 'dwd-icon-eu-eps'
DWD_ICON_GLOBAL = 'dwd-icon-global'
DWD_COSMO_D2 = 'dwd-cosmo-d2'
DWD_COSMO_D2_EPS = 'dwd-cosmo-d2-eps'
NCEP_GFS_025 = 'ncep-gfs-025'
NCEP_GFS_050 = 'ncep-gfs-050'
NCEP_GFS_100 = 'ncep-gfs-100'
METEO_FRANCE_AROME = 'meteo-france-arome'
KNMI_HARMONIE = 'knmi-harmonie'
ARPEGE = 'arpege'
GEOS5 = 'geos_5'

DWD_ICON_EU = "dwd-icon-eu"
DWD_ICON_EU_EPS = "dwd-icon-eu-eps"
DWD_ICON_GLOBAL = "dwd-icon-global"
DWD_COSMO_D2 = "dwd-cosmo-d2"
DWD_COSMO_D2_EPS = "dwd-cosmo-d2-eps"
NCEP_GFS_025 = "ncep-gfs-025"
NCEP_GFS_050 = "ncep-gfs-050"
NCEP_GFS_100 = "ncep-gfs-100"
METEO_FRANCE_AROME = "meteo-france-arome"
KNMI_HARMONIE = "knmi-harmonie"
ARPEGE = "arpege"
GEOS5 = "geos_5"

# For testing purposes.
UNKNOWN = 'unknown'
UNKNOWN = "unknown"
5 changes: 3 additions & 2 deletions gribmagic/unity/exceptions/wrong_weather_model_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@


class WrongWeatherModelException(Exception):
""" Excpetion that can be thrown if a wrong weather model type was parsed """
pass
"""Excpetion that can be thrown if a wrong weather model type was parsed"""

pass
28 changes: 22 additions & 6 deletions gribmagic/unity/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import logging

from cachetools import cachedmethod

from gribmagic.unity.enumerations.weather_models import WeatherModels
from gribmagic.unity.modules.config.configurations import MODEL_CONFIG, MODEL_VARIABLES_MAPPING, MODEL_VARIABLES_LEVELS_MAPPING
from gribmagic.unity.modules.config.constants import KEY_VARIABLES, KEY_GRIB_PACKAGE_TYPES
from gribmagic.unity.modules.config.configurations import (
MODEL_CONFIG,
MODEL_VARIABLES_LEVELS_MAPPING,
MODEL_VARIABLES_MAPPING,
)
from gribmagic.unity.modules.config.constants import (
KEY_GRIB_PACKAGE_TYPES,
KEY_VARIABLES,
)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -39,7 +48,9 @@ def level(self, variable):
if self.levels_map is None:
raise KeyError(f"{self.model} has no variable->level mapping")
if variable not in self.levels_map:
raise KeyError(f"{self.model} lacks variable->level mapping for '{variable}'")
raise KeyError(
f"{self.model} lacks variable->level mapping for '{variable}'"
)
return self.levels_map[variable]

@property
Expand All @@ -51,15 +62,20 @@ def variables(self):
def generate_variables(self):

dwd_models = [
WeatherModels.DWD_ICON_GLOBAL, WeatherModels.DWD_ICON_EU, WeatherModels.DWD_ICON_EU_EPS,
WeatherModels.DWD_COSMO_D2, WeatherModels.DWD_COSMO_D2_EPS,
WeatherModels.DWD_ICON_GLOBAL,
WeatherModels.DWD_ICON_EU,
WeatherModels.DWD_ICON_EU_EPS,
WeatherModels.DWD_COSMO_D2,
WeatherModels.DWD_COSMO_D2_EPS,
]
dwd_blocklist = ["temperature", "wind_u", "wind_v", "relative_humidity"]

for variable in self.info[KEY_VARIABLES]:
logger.info(f"{self.model}: Accessing parameter '{variable}'")
if self.model in dwd_models and variable in dwd_blocklist:
variable_level = self.level(variable)
logger.warning(f"DWD ICON: Parameter '{variable}' ({variable_level}) not implemented yet")
logger.warning(
f"DWD ICON: Parameter '{variable}' ({variable_level}) not implemented yet"
)
continue
yield variable
8 changes: 5 additions & 3 deletions gribmagic/unity/modules/config/configurations.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
""" provides parsed configurations"""

from gribmagic.unity.modules.config.parse_configurations import parse_model_config, \
parse_model_variables_mapping, parse_model_variables_levels_mapping

from gribmagic.unity.modules.config.parse_configurations import (
parse_model_config,
parse_model_variables_levels_mapping,
parse_model_variables_mapping,
)

MODEL_CONFIG = parse_model_config()
MODEL_VARIABLES_MAPPING = parse_model_variables_mapping()
Expand Down

0 comments on commit 0d795ea

Please sign in to comment.