get_domain.py 1.96 KB
Newer Older
1
import datetime
Antoine Berchet's avatar
Antoine Berchet committed
2
import glob
3
import os
4
import numpy as np
Antoine Berchet's avatar
Antoine Berchet committed
5
6
import xarray as xr

7
from .....utils.classes.setup import Setup
8
9
10
11
12


def get_domain(ref_dir, ref_file, input_dates, target_dir, tracer=None):

    # Looking for a reference file to read lon/lat in
Antoine Berchet's avatar
Antoine Berchet committed
13
    list_file = glob.glob("{}/*nc".format(ref_dir))
14
15
16
17

    domain_file = None
    # Either a file is specified in the Yaml
    if ref_file in list_file:
Antoine Berchet's avatar
Antoine Berchet committed
18
        domain_file = "{}/{}".format(ref_dir, ref_file)
19
20
21
22
23

    # Or loop over available file regarding file pattern
    else:
        for flx_file in list_file:
            try:
Antoine Berchet's avatar
Antoine Berchet committed
24
25
26
                date = datetime.datetime.strptime(
                    os.path.basename(flx_file), ref_file
                )
27
28
29
30
31
32
33
                domain_file = flx_file
                break
            except ValueError:
                continue

    if domain_file is None:
        raise Exception(
Antoine Berchet's avatar
Antoine Berchet committed
34
35
            "EDGARv5 domain could not be initialized as no file was found"
        )
36
37

    # Read lon/lat in
38
    nc = xr.open_dataset(domain_file)
39

Antoine Berchet's avatar
Antoine Berchet committed
40
41
    lon = nc["lon"]
    lat = nc["lat"]
42
43
44
45
46
47
48
49
50
51

    lon_min = lon.min() - (lon[1] - lon[0]) / 2
    lon_max = lon.max() + (lon[-1] - lon[-2]) / 2
    lat_min = lat.min() - (lat[1] - lat[0]) / 2
    lat_max = lat.max() + (lat[-1] - lat[-2]) / 2

    nlon = lon.size
    nlat = lat.size

    # Initializes domain
Antoine Berchet's avatar
Antoine Berchet committed
52
53
54
55
56
57
58
59
60
61
62
63
64
    setup = Setup.from_dict(
        {
            "domain": {
                "plugin": {
                    "name": "dummy",
                    "version": "std",
                    "type": "domain",
                },
                "xmin": lon_min,
                "xmax": lon_max,
                "ymin": lat_min,
                "ymax": lat_max,
                "nlon": nlon,
65
                "nlat": nlat
Antoine Berchet's avatar
Antoine Berchet committed
66
67
68
            }
        }
    )
69
70
71

    Setup.load_setup(setup, level=1)

72
73
74
75
76
77
    # Vertical definition
    setup.domain.pressure_unit = "Pa"
    setup.domain.nlev = 1
    setup.domain.sigma_a = np.array([0])
    setup.domain.sigma_b = np.array([1])
    
Antoine Berchet's avatar
Antoine Berchet committed
78
    return setup.domain