init_components.py 6.27 KB
Newer Older
1
import pandas as pd
2
from ....utils.path import init_dir
Antoine Berchet's avatar
Antoine Berchet committed
3
from .fetch import default_fetch
4
from logging import warning
5
6
7


def init_components(plugin):
Antoine Berchet's avatar
Antoine Berchet committed
8
    if hasattr(plugin, "components"):
9
10
11
        components = plugin.components
        for comp in components.attributes:
            component = getattr(components, comp)
12
13
14

            # Fetch parameters
            # If no parameters, handle the component as a whole
Antoine Berchet's avatar
Antoine Berchet committed
15
            if not hasattr(component, "parameters"):
16
                params = component
Antoine Berchet's avatar
Antoine Berchet committed
17
                parameters = [""]
18
19
            else:
                params = component.parameters
Antoine Berchet's avatar
Antoine Berchet committed
20
                parameters = params.attributes[:] + [""]
21

22
            # Loop over parameters to fetch information
23
24
25
            for trcr in parameters:
                tracer = getattr(params, trcr, component)

26
27
28
                # By default is not in the target vector, nor in the obs vector
                tracer.iscontrol = getattr(tracer, "iscontrol", False)
                tracer.isobs = getattr(tracer, "isobs", False)
29
30

                # Fetch reference directory and file format
31
32
                # Replace value in parameter by value in component if no 
                # value was specified explicitly in the yaml (= is_default)
33
                for attr in ["dir", "file", "file_freq"]:
Antoine Berchet's avatar
Antoine Berchet committed
34
35
36
37
38
39
40
41
                    if hasattr(tracer, attr) \
                            and attr in tracer.is_default_value:
                        if hasattr(component, attr) \
                                and attr not in component.is_default_value:
                            setattr(tracer, attr, getattr(component, attr))
                    elif not hasattr(tracer, attr):
                        if hasattr(component, attr):
                            setattr(tracer, attr, getattr(component, attr))
42
                        elif attr != "file_freq":
Antoine Berchet's avatar
Antoine Berchet committed
43
44
45
46
47
48
49
50
51
                            setattr(tracer, attr, "")

                # Fetch reference functions from the component
                for attr in ["read", "write", "fetch"]:
                    if not hasattr(tracer, attr) \
                            or attr in tracer.is_default_value:
                        if hasattr(component, attr):
                            setattr(tracer, attr, getattr(component, attr))
                        else:
52
53
54
55
                            if attr == "fetch":
                                setattr(tracer, attr, default_fetch)
                            else:
                                setattr(tracer, attr, None)
Antoine Berchet's avatar
Antoine Berchet committed
56
    
Antoine Berchet's avatar
Antoine Berchet committed
57
                tracer.varname = getattr(tracer, "varname", "")
58
                
59
                # Initializes target directory and pass info to tracer
60
                target_dir = "{}/datavect/{}/{}/".format(
Antoine Berchet's avatar
Antoine Berchet committed
61
62
                    plugin.workdir, comp, trcr
                )
63
                init_dir(target_dir)
64

Antoine Berchet's avatar
Antoine Berchet committed
65
                tracer.orig_dir = tracer.dir
66
                tracer.dir = target_dir
67
                
68
                # Fetch files and dates
Antoine Berchet's avatar
Antoine Berchet committed
69
70
71
72
73
74
75
76
77
78
79
80
                if tracer.fetch is not None:
                    try:
                        list_files, list_dates = tracer.fetch(
                            tracer.orig_dir,
                            tracer.file,
                            [plugin.datei, plugin.datef],
                            target_dir,
                            component=component,
                            tracer=tracer,
                        )
                    except Exception as e:
                            raise e
81
82
83
84
85
86
87

                    # Crop files and dates depending on global datei and datef
                    try:
                        list_files = {
                            ddi: [
                                ff for ff, dd
                                in zip(list_files[ddi], list_dates[ddi])
Antoine Berchet's avatar
Antoine Berchet committed
88
89
                                if dd[0] <= plugin.datef
                                   and dd[1] >= plugin.datei
90
91
92
93
                            ]
                            for ddi in list_files}
                        list_dates = {
                            ddi: [dd for dd in list_dates[ddi]
Antoine Berchet's avatar
Antoine Berchet committed
94
95
                                  if dd[0] <= plugin.datef
                                  and dd[1] >= plugin.datei
96
97
98
99
100
                            ]
                            for ddi in list_dates}
                    except Exception as e:
                        print(e)
                        pass
101
                    
102
103
104
105
106
107
108
109
110
111
112
113
114
115
                    # Clean empty periods
                    # Make sure that periods are datetime objects
                    list_files = {
                         pd.DatetimeIndex([ddi]).to_pydatetime()[0]:
                             list_files[ddi] for ddi in list_files
                        # if list_dates[ddi] != []
                    }
                    list_dates = {
                        pd.DatetimeIndex([ddi]).to_pydatetime()[0]:
                            list_dates[ddi] for ddi in list_dates
                        # if list_dates[ddi] != []
                    }

                    # Attach info to the tracer
Antoine Berchet's avatar
Antoine Berchet committed
116
117
                    tracer.input_dates = list_dates
                    tracer.input_files = list_files
118

Antoine Berchet's avatar
Antoine Berchet committed
119
120
121
122
123
124
                    # Saving tracer_dir into component dir
                    # if not already available
                    if not hasattr(component, "input_dates"):
                        component.input_dates = list_dates
                    if not hasattr(component, "input_files"):
                        component.input_files = list_files
125
                        
126
127
                # Get the domain and
                # change it to the domain side if lateral conditions
Antoine Berchet's avatar
Antoine Berchet committed
128
                if hasattr(tracer, "domain"):
129
130
                    continue

Antoine Berchet's avatar
Antoine Berchet committed
131
                if hasattr(tracer, "get_domain"):
132
                    tracer.domain = tracer.get_domain(
Antoine Berchet's avatar
Antoine Berchet committed
133
134
                        tracer.orig_dir,
                        tracer.file,
Antoine Berchet's avatar
Antoine Berchet committed
135
136
137
138
                        plugin.model.input_dates,
                        target_dir,
                        tracer=tracer,
                    )
139
140

                else:
141
                    tracer.domain = plugin.domain
142
143
144
145
146
147
                
                # Pass arguments from root component to tracers if not
                # explicitly defined
                for attr in component.attributes:
                    if attr != "plugin" and not hasattr(tracer, attr):
                        setattr(tracer, attr, getattr(component, attr))