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

Ouranosinc / miranda / 1935746677

pending completion
1935746677

Pull #24

github

GitHub
Merge b2c329efa into 250bc4dd4
Pull Request #24: Add CMIP file structure - WIP

88 of 603 new or added lines in 23 files covered. (14.59%)

3 existing lines in 2 files now uncovered.

654 of 2999 relevant lines covered (21.81%)

0.65 hits per line

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

25.64
/miranda/ecmwf/_namers.py
1
import logging
3✔
2
import os
3✔
3
import re
3✔
4
import shutil
3✔
5
from pathlib import Path
3✔
6
from typing import Union
3✔
7

8
import xarray as xr
3✔
9

10
__all__ = ["rename_ecmwf_files"]
3✔
11

12
ECMWF_PROJECT_NAMES = [
3✔
13
    "era5-land",
14
    "era5-land-monthly-means",
15
    "era5-pressure-levels",
16
    "era5-pressure-levels-preliminary-back-extension",
17
    "era5-single-levels",
18
    "era5-single-levels-preliminary-back-extension",
19
]
20

21

22
def rename_ecmwf_files(path: Union[os.PathLike, str]) -> None:
3✔
23
    files = [f for f in Path(path).glob("*.nc")]
×
24
    for f in files:
×
25
        file_name = str(f.stem)
×
26

27
        ds = xr.open_dataset(f, cache=False)
×
28
        var = [d for d in ds.data_vars]
×
29
        var_name = str(var[0])
×
30

31
        try:
×
32
            x = re.search(r"\d{6}", file_name)
×
33
            date = x.group()
×
34
        except AttributeError:
×
35
            year = int(ds.isel(time=0).time.dt.year)
×
36
            month = int(ds.isel(time=0).time.dt.month)
×
37
            date = f"{year}{str(month).zfill(2)}"
×
38

39
        names = file_name.split("_")
×
40
        projects = [name for name in names if name in ECMWF_PROJECT_NAMES]
×
41
        if len(projects) == 1:
×
42
            project = projects[0]
×
NEW
43
        elif len(projects) > 1:
×
NEW
44
            logging.warning(
×
45
                f"More than one project identified for file {f.name}. Verify file naming."
46
            )
NEW
47
            continue
×
48
        else:
49
            continue
×
50

51
        product = "reanalysis"
×
52
        freq = "1hr"
×
53
        domain = "NAM"
×
54
        institute = "ecmwf"
×
55

56
        new_name_parts = [
×
57
            var_name,
58
            freq,
59
            institute,
60
            project,
61
            product,
62
            domain,
63
            date,
64
        ]
65
        new_name = f"{'_'.join(new_name_parts)}.nc"
×
NEW
66
        logging.info(f"Moving {f.name} to {new_name}")
×
67

68
        shutil.move(f, Path(path).joinpath(new_name))
×
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