fwd.py 4.42 KB
Newer Older
1
import copy
2
import os
Antoine Berchet's avatar
Antoine Berchet committed
3
import pandas as pd
4
import pytest
5
import shutil
Antoine Berchet's avatar
Antoine Berchet committed
6
import xarray as xr
7
from pycif.utils.yml import ordered_dump
8
9


Antoine Berchet's avatar
Antoine Berchet committed
10
11
12
13
@pytest.fixture(
    params=
    [
        {"perturb_CH4": True, "datef": "2010-01-02", "no_MCF": True},
Antoine Berchet's avatar
Antoine Berchet committed
14
15
16
17
18
        {"perturb_CH4": False, "datef": "2010-01-02", "no_MCF": True},
        {"perturb_CH4": True, "datef": "2010-01-05", "no_MCF": True},
        {"perturb_CH4": False, "datef": "2010-01-05", "no_MCF": True},
        {"perturb_CH4": True, "datef": "2010-01-02"},
        {"perturb_CH4": False, "datef": "2010-01-02"},
19
20
        pytest.param({"perturb_CH4": True, "datef": "2010-01-05"},
                     marks=pytest.mark.ref_config),
21
22
        pytest.param({"perturb_CH4": False, "datef": "2010-01-05"},
                     marks=pytest.mark.inversion)
Antoine Berchet's avatar
Antoine Berchet committed
23
24
    ])
def dummy_config_fwd(ref_dummy, request):
25
26
27
28
    """
    Fixture that returns a temporary folder and a configuration file for the
    dummy_forward model.
    """
Antoine Berchet's avatar
Antoine Berchet committed
29
    tmpdir_str, config = ref_dummy
30
31

    # Adding perturbation to reference fluxes
Antoine Berchet's avatar
Antoine Berchet committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
    if request.param.get("perturb_CH4", False):
        params = config["datavect"]["components"]["fluxes"]["parameters"]
        params["CH4_ref"] = copy.copy(params["CH4"])
        params["CH4_perturb"] = copy.copy(params["CH4"])
        params["CH4_perturb"].update(
            {
                "xb_scale": 0.2,
                "flx_formula":
                    [{"sum":
                        [
                            {"cos": None,
                             "variable": "zlat",
                             "period": 200},
                            {"sin": None,
                             "variable": "zlon",
                             "period": 300}]}]
            }
        )
        del params["CH4"]
51
    
Antoine Berchet's avatar
Antoine Berchet committed
52
53
54
55
56
57
58
59
60
61
62
63
64
        # Adding families in controlvect
        config["controlvect"] = {
            "plugin": {"name": "standard", "version": "std"},
            "save_out_netcdf": True,
            "transform_pipe": {
                "trans1": {
                    "plugin": {"name": "families",
                               "version": "std",
                               "type": "transform"},
                    "component": "fluxes",
                    "parameters_in": ["CH4_ref", "CH4_perturb"],
                    "parameter_out": "CH4"
                }
65
66
            }
        }
Antoine Berchet's avatar
Antoine Berchet committed
67
68
69
70
71
72
73
74
75
76
77
    
    # Removing MCF if requires
    if request.param.get("no_MCF", False):
        del config["model"]["chemistry"]["acspecies"]["MCF"]
        del config["datavect"]["components"]["fluxes"]["parameters"]["MCF"]
        del config["datavect"]["components"]["concs"]["parameters"]["MCF"]
    
    # Changing end date
    if "datef" in request.param:
        config["datef"] = pd.to_datetime(request.param["datef"]).to_pydatetime()
    
78
79
80
81
    # Tag for config file to dump in examples
    tag = ("long" if "datef" in request.param else "short") \
          + ("_families" if request.param.get("perturb_CH4", False) else "") \
          + ("_noMCF" if request.param.get("no_MCF", False) else "")
82
    
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
    yield tmpdir_str, config, tag
    
    # Change config to be compatible in the docker
    config["workdir"] = "/workdir/"
    config["model"]["file_pg"] = \
        "/tmp/CIF/model_sources/dummy_gauss/Pasquill-Gifford.txt"
    
    # Dump configuration into CIF examples
    current_dir = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
    example_dir = \
        os.path.abspath(os.path.join(current_dir, "../../../examples/dummy/"))
    
    dummy_config_file = \
        os.path.join(example_dir, "config_fwd_{}.yml".format(tag))
    
    # Write only if mode is forward
    if config["mode"]["plugin"]["name"] == "forward":
        with open(dummy_config_file, "w") as outfile:
            ordered_dump(outfile, config)
102
    
Antoine Berchet's avatar
Antoine Berchet committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
        # Copy simulated observation vector for reading by inversions
        pytest_dir = tmpdir_str + "/../"
        target_dir = pytest_dir + "ref_obsvect"
        if os.path.isdir(target_dir):
            shutil.rmtree(target_dir)
    
        shutil.copytree(
            "{}/obsvect".format(tmpdir_str),
            target_dir
        )
        
        # Copy the H matrix to avoid re-compiling it every time
        Hfile_ref = "{}/model/H_matrix.pickle".format(tmpdir_str)
        Hfile_target = "{}/../H_matrix.pickle".format(tmpdir_str)
        
        if os.path.isfile(Hfile_target):
            shutil.rmtree(Hfile_target)
            
        shutil.copy(Hfile_ref, Hfile_target)