• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Ouranosinc / miranda / 1715021976

pending completion
1715021976

Pull #21

github

GitHub
Merge 3cd491177 into 5c54767a4
Pull Request #21: Update ECMWF requests calls to newest API conventions

16 of 301 new or added lines in 6 files covered. (5.32%)

95 existing lines in 3 files now uncovered.

494 of 2418 relevant lines covered (20.43%)

0.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

20.9
/miranda/ecmwf/era5.py
1
import functools
3✔
2
import logging.config
3✔
3
import multiprocessing
3✔
4
import os
3✔
5
from datetime import date
3✔
6
from datetime import datetime as dt
3✔
7
from pathlib import Path
3✔
8
from typing import List, Mapping, Optional, Tuple
3✔
9

10
from cdsapi import Client
3✔
11

12
from miranda.scripting import LOGGING_CONFIG
3✔
13

14
logging.config.dictConfig(LOGGING_CONFIG)
3✔
15

16
__all__ = ["request_era5"]
3✔
17

18

19
def request_era5(
3✔
20
    variables: Optional[Mapping[str, str]] = None,
21
    projects: List[str] = None,
22
    domain: str = "AMNO",
23
    year_start: int = 1981,
24
    year_end: Optional[int] = None,
25
    processes: int = 4,
26
) -> None:
27
    """Request ERA5/ERA5-Land from Copernicus Data Store in NetCDF4 format.
28

29
    Parameters
30
    ----------
31
    variables: Mapping[str, str], optional
32
    projects : List[{"era5", "era5-land"}]
33
    domain : {"GLOBAL", "AMNO", "CAN", "QC"}
34
    year_start : int
35
    year_end : int, optional
36
    processes : int
37

38
    Returns
39
    -------
40
    None
41
    """
42
    # Variables of interest
43
    variable_reference = dict(
×
44
        pr="total_precipitation",
45
        vas="10m_v_component_of_wind",
46
        uas="10m_u_component_of_wind",
47
        td="2m_dewpoint_temperature",
48
        tas="2m_temperature",
49
        potevap="potential evaporation",
50
        snd="snow_depth",
51
        prsn="snowfall",
52
    )
53

54
    v_requested = dict()
×
55
    if variables:
×
56
        for v in variables:
×
57
            if v in variable_reference:
×
58
                v_requested[v] = variable_reference[v]
×
59
    else:
60
        v_requested = variable_reference
×
61

62
    if year_end is None:
×
63
        year_end = date.today().year
×
64
    years = range(year_start, year_end)
×
65

66
    months = [str(d).zfill(2) for d in range(13)]
×
67
    yearmonth = list()
×
68
    for y in years:
×
69
        for m in months:
×
70
            yearmonth.append((y, m))
×
71

72
    project_names = list()
×
73
    if "era5" in projects:
×
74
        project_names.append("reanalysis-era5-single-levels")
×
75
    if "era5-land" in projects:
×
76
        project_names.append("reanalysis-era5-land")
×
77
    product = project_names[0].split("-")[0]
×
78

79
    target = Path().cwd().joinpath("downloaded")
×
80

81
    Path(target).mkdir(exist_ok=True)
×
82
    os.chdir(target)
×
83

84
    for p in projects:
×
85
        proc = multiprocessing.Pool(processes=processes)
×
86
        func = functools.partial(_request_direct_era, v_requested, p, domain, product)
×
87

88
        logging.info([func, dt.now().strftime("%Y-%m-%d %X")])
×
89

90
        proc.map(func, yearmonth)
×
91
        proc.close()
×
92
        proc.join()
×
93

94

95
def _request_direct_era(
3✔
96
    variables: Mapping[str, str],
97
    project: str,
98
    domain: str,
99
    product: str,
100
    yearmonth: Tuple[int, str],
101
):
102
    """Launch formatted request."""
103
    year, month = yearmonth
×
104
    days = [str(d).zfill(2) for d in range(32)]
×
105
    times = ["{}:00".format(str(t).zfill(2)) for t in range(24)]
×
106

107
    if domain.upper() == "GLOBAL":
×
NEW
108
        region = [90, -180, -90, 180]
×
109
    elif domain.upper() == "AMNO":
×
NEW
110
        domain = "NAM"
×
NEW
111
        region = [90, -180, 10, -10]
×
112
    elif domain.upper() == "CAN":
×
NEW
113
        region = [83.5, -141, 41.5, -52.5]
×
114
    elif domain.upper() == "QC":
×
NEW
UNCOV
115
        region = [63, -80, 44.5, -57]
×
116
    else:
UNCOV
117
        raise ValueError()
×
118

UNCOV
119
    c = Client()
×
120

121
    for var in variables.keys():
×
NEW
UNCOV
122
        netcdf_name = f"{var}_{'-'.join(project.split('-')[1:])}_{product}_hourly_{year}{month}_{domain.upper()}.nc"
×
123

124
        if Path(netcdf_name).exists():
×
UNCOV
125
            continue
×
126

UNCOV
127
        request_kwargs = dict(
×
128
            variable=variables[var],
129
            year=year,
130
            month=month,
131
            day=days,
132
            time=times,
133
            area=region,
134
            format="netcdf",
135
        )
136

137
        if project == "reanalysis-era5-single-levels":
×
UNCOV
138
            request_kwargs.update(dict(product_type=product))
×
139

UNCOV
140
        c.retrieve(
×
141
            project,
142
            request_kwargs,
143
            netcdf_name,
144
        )
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2024 Coveralls, Inc