Skip to content

Commit

Permalink
Code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
wpreimes committed Oct 25, 2024
1 parent 1dfd85d commit 3988a49
Show file tree
Hide file tree
Showing 10 changed files with 252 additions and 242 deletions.
5 changes: 2 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
ecmwf_models
============


|ci| |cov| |pip| |doc|

.. |ci| image:: https://github.com/TUW-GEO/ecmwf_models/actions/workflows/ci.yml/badge.svg?branch=master
:target: https://github.com/ecmwf_models/c3s_sm/actions
:target: https://github.com/TUW-GEO/ecmwf_models/actions

.. |cov| image:: https://coveralls.io/repos/TUW-GEO/ecmwf_models/badge.png?branch=master
:target: https://coveralls.io/r/TUW-GEO/ecmwf_models?branch=master
Expand All @@ -18,7 +17,7 @@ ecmwf_models
:target: https://ecmwf-models.readthedocs.io/en/latest/


Readers and converters for data from the `ECMWF reanalysis models
Readers and converters for `ECMWF reanalysis (ERA) data
<http://apps.ecmwf.int/datasets/>`_. Written in Python.

Works great in combination with `pytesmo <https://github.com/TUW-GEO/pytesmo>`_.
Expand Down
200 changes: 107 additions & 93 deletions src/ecmwf_models/cli.py

Large diffs are not rendered by default.

29 changes: 14 additions & 15 deletions src/ecmwf_models/era5/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
from ecmwf_models.utils import (
lookup,
update_image_summary_file,
default_variables, split_array, check_api_ready,
default_variables,
split_array,
check_api_ready,
)
from ecmwf_models.extract import save_ncs_from_nc, save_gribs_from_grib

Expand Down Expand Up @@ -329,8 +331,7 @@ def download_and_move(
n_hsteps=len(h_steps),
max_req_size=n_max_request,
reduce=True,
daily_request=True if stepsize == "day" else False
)
daily_request=True if stepsize == "day" else False)

logger.info(f"Request is split into {len(req_periods)} chunks")
logger.info(f"Target directory {target_path}")
Expand All @@ -348,8 +349,7 @@ def _download(curr_start, curr_end):
fname = "{start}_{end}.{ext}".format(
start=curr_start.strftime("%Y%m%d"),
end=curr_end.strftime("%Y%m%d"),
ext="grb" if grb else "nc"
)
ext="grb" if grb else "nc")

dl_file = os.path.join(downloaded_data_path, fname)

Expand Down Expand Up @@ -394,8 +394,7 @@ def _download(curr_start, curr_end):
target_path,
product_name=product.upper(),
keep_original=keep_original,
keep_prelim=keep_prelim
)
keep_prelim=keep_prelim)
else:
save_ncs_from_nc(
dl_file,
Expand All @@ -404,13 +403,10 @@ def _download(curr_start, curr_end):
grid=grid,
remap_method=remap_method,
keep_original=keep_original,
keep_prelim=keep_prelim
)
keep_prelim=keep_prelim)

return status_code



# Since we download month/month or day/day we need to
# collect all the status codes to return a valid
# status code for the whole time period
Expand All @@ -420,8 +416,8 @@ def _download(curr_start, curr_end):
'curr_start': [p[0] for p in req_periods],
'curr_end': [p[1] for p in req_periods]
},
logger_name='cdsapi', loglevel='DEBUG'
)
logger_name='cdsapi',
loglevel='DEBUG')

# remove temporary files
if not keep_original:
Expand Down Expand Up @@ -501,6 +497,9 @@ def download_record_extension(path, dry_run=False, cds_token=None):
- timedelta(days=1) # yesterday

return download_and_move(
path, startdate=startdate, enddate=enddate,
cds_token=cds_token, dry_run=dry_run,
path,
startdate=startdate,
enddate=enddate,
cds_token=cds_token,
dry_run=dry_run,
**props['download_settings'])
1 change: 0 additions & 1 deletion src/ecmwf_models/era5/reader.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-

"""
This module contains ERA5/ERA5-Land specific child classes of the netcdf
and grib base classes, that are used for reading all ecmwf products.
Expand Down
18 changes: 9 additions & 9 deletions src/ecmwf_models/era5/reshuffle.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-

"""
Image to time series conversion tools.
"""
Expand All @@ -17,6 +16,7 @@
from ecmwf_models.grid import ERA_RegularImgGrid, ERA5_RegularImgLandGrid
from ecmwf_models.utils import parse_filetype, parse_product, get_first_last_image_date


def extend_ts(ts_path, **img2ts_kwargs):
"""
Append any new data from the image path to the time series data.
Expand All @@ -41,8 +41,7 @@ def extend_ts(ts_path, **img2ts_kwargs):
"No overview.yml file found in the passed directory. "
"This file is required to use the same settings to extend an "
"existing record. NOTE: Use the `era5 download` or "
"`era5land download` programs first."
)
"`era5land download` programs first.")

ts_props = read_summary_yml(ts_path)

Expand All @@ -59,7 +58,7 @@ def extend_ts(ts_path, **img2ts_kwargs):
img2ts_kwargs[k] = v

if 'enddate' not in img2ts_kwargs:
img2ts_kwargs['enddate'] = None # All available images
img2ts_kwargs['enddate'] = None # All available images

img2ts(outputpath=ts_path, **img2ts_kwargs)

Expand Down Expand Up @@ -136,8 +135,7 @@ def img2ts(

if land_points:
if product == "era5":
grid = ERA5_RegularImgLandGrid(
resolution=0.25, bbox=bbox)
grid = ERA5_RegularImgLandGrid(resolution=0.25, bbox=bbox)
elif product == "era5-land":
grid = ERA5_RegularImgLandGrid(resolution=0.1, bbox=bbox)
else:
Expand Down Expand Up @@ -187,8 +185,11 @@ def img2ts(
data = input_dataset.read(first_date_time)
ts_attributes = data.metadata

props = {'product': product, 'filetype': filetype,
'last_update': datetime.now()}
props = {
'product': product,
'filetype': filetype,
'last_update': datetime.now()
}

kwargs = {
'input_root': input_root,
Expand Down Expand Up @@ -225,4 +226,3 @@ def img2ts(
reshuffler.calc()

update_ts_summary_file(outputpath, props)

56 changes: 27 additions & 29 deletions src/ecmwf_models/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,26 @@
import xarray as xr
from datedown.fname_creator import create_dt_fpath

from ecmwf_models.globals import (IMG_FNAME_TEMPLATE,
IMG_FNAME_DATETIME_FORMAT, EXPVER, SUBDIRS)
from ecmwf_models.globals import (
IMG_FNAME_TEMPLATE,
IMG_FNAME_DATETIME_FORMAT,
EXPVER, SUBDIRS
)
from ecmwf_models.globals import (
Cdo, cdo_available, CdoNotFoundError,
pygrib, pygrib_available, PygribNotFoundError,
Cdo,
cdo_available,
CdoNotFoundError,
pygrib,
pygrib_available,
PygribNotFoundError,
)


def save_ncs_from_nc(
input_nc,
output_path,
product_name,
grid=None,
keep_original=True,
remap_method="bil",
keep_prelim=True,
input_nc,
output_path,
product_name,
grid=None,
keep_original=True,
remap_method="bil",
keep_prelim=True,
):
"""
Split the downloaded netcdf file into daily files and add to folder
Expand All @@ -48,8 +49,7 @@ def save_ncs_from_nc(
product="{product}",
type='AN',
datetime=IMG_FNAME_DATETIME_FORMAT,
ext='nc'
)
ext='nc')

nc_in = xr.open_dataset(input_nc, mask_and_scale=True)
if 'valid_time' in nc_in.variables:
Expand Down Expand Up @@ -86,8 +86,8 @@ def save_ncs_from_nc(
continue

if len(ext) > 0:
filename_templ = _filename_templ.format(
product=product_name + '-' + ext)
filename_templ = _filename_templ.format(product=product_name +
'-' + ext)
else:
filename_templ = _filename_templ.format(product=product_name)

Expand Down Expand Up @@ -130,11 +130,11 @@ def save_ncs_from_nc(


def save_gribs_from_grib(
input_grib,
output_path,
product_name,
keep_original=True,
keep_prelim=True,
input_grib,
output_path,
product_name,
keep_original=True,
keep_prelim=True,
):
"""
Split the downloaded grib file into daily files and add to folder structure
Expand Down Expand Up @@ -163,8 +163,7 @@ def save_gribs_from_grib(
product="{product}",
type='AN',
datetime=IMG_FNAME_DATETIME_FORMAT,
ext='grb'
)
ext='grb')

grib_in.seek(0)
prev_date = None
Expand All @@ -184,14 +183,13 @@ def save_gribs_from_grib(
continue

if len(ext) > 0:
filename_templ = _filename_templ.format(
product=product_name + '-' + ext)
filename_templ = _filename_templ.format(product=product_name +
'-' + ext)
else:
filename_templ = _filename_templ.format(product=product_name)

filepath = create_dt_fpath(
filedate, root=output_path, fname=filename_templ,
subdirs=SUBDIRS)
filedate, root=output_path, fname=filename_templ, subdirs=SUBDIRS)

if not os.path.exists(os.path.dirname(filepath)):
os.makedirs(os.path.dirname(filepath))
Expand Down
12 changes: 7 additions & 5 deletions src/ecmwf_models/globals.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
from pathlib import Path


IMG_FNAME_TEMPLATE = "{product}_{type}_{datetime}.{ext}"
IMG_FNAME_DATETIME_FORMAT = "%Y%m%d_%H%M"

Expand Down Expand Up @@ -33,6 +32,7 @@


class PygribNotFoundError(ModuleNotFoundError):

def __init__(self, msg=None):
_default_msg = ("pygrib could not be imported. "
"Pleas run `conda install -c conda-forge pygrib` to "
Expand All @@ -41,8 +41,10 @@ def __init__(self, msg=None):


class CdoNotFoundError(ModuleNotFoundError):

def __init__(self, msg=None):
_default_msg = ("cdo and/or python-cdo not installed. "
"Pleas run `conda install -c conda-forge cdo` and also "
"`pip install cdo`.")
self.msg = _default_msg if msg is None else msg
_default_msg = (
"cdo and/or python-cdo not installed. "
"Pleas run `conda install -c conda-forge cdo` and also "
"`pip install cdo`.")
self.msg = _default_msg if msg is None else msg
20 changes: 12 additions & 8 deletions src/ecmwf_models/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import Tuple
import xarray as xr


def trafo_lon(lon):
"""
0...360 -> 0...180...-180
Expand All @@ -27,6 +28,7 @@ def trafo_lon(lon):
lon[lons_gt_180] = lon[lons_gt_180] - 360.0
return lon


def safe_arange(start, stop, step):
"""
Like numpy.arange, but floating point precision is kept.
Expand All @@ -47,10 +49,13 @@ def safe_arange(start, stop, step):
Range of values in interval at the given step size / sampling
"""
f_step = (1. / float(step))
vals = np.arange(float(start) * f_step, float(stop) * f_step,
float(step) * f_step)
vals = np.arange(
float(start) * f_step,
float(stop) * f_step,
float(step) * f_step)
return vals / f_step


def get_grid_resolution(lats: np.ndarray, lons: np.ndarray) -> (float, float):
"""
try to derive the grid resolution from given coords.
Expand Down Expand Up @@ -113,10 +118,9 @@ def ERA5_RegularImgLandGrid(
ds = ds.assign_coords({'longitude': trafo_lon(ds['longitude'].values)})
if bbox is not None:
ds = ds.sel(latitude=slice(bbox[3], bbox[1]))
ds = ds.isel(longitude=np.where((
(ds['longitude'].values >= bbox[0]) &
(ds['longitude'].values <= bbox[2])
))[0])
ds = ds.isel(
longitude=np.where(((ds['longitude'].values >= bbox[0])
& (ds['longitude'].values <= bbox[2])))[0])

land_mask = np.array(ds.values == 1.0)

Expand Down Expand Up @@ -167,8 +171,8 @@ def ERA_RegularImgGrid(
grid = grid.to_cell_grid(cellsize=5.0)

if bbox is not None:
subgpis = grid.get_bbox_grid_points(latmin=bbox[1], latmax=bbox[3],
lonmin=bbox[0], lonmax=bbox[2])
subgpis = grid.get_bbox_grid_points(
latmin=bbox[1], latmax=bbox[3], lonmin=bbox[0], lonmax=bbox[2])
grid = grid.subgrid_from_gpis(subgpis)

return grid
Loading

0 comments on commit 3988a49

Please sign in to comment.