1 | """Locates input files and calculates namelist attributes |
---|
2 | |
---|
3 | Typically a user will have files distributed throughout the hard drive |
---|
4 | and it makes little or no sense to try to capture all use cases. |
---|
5 | |
---|
6 | >>> date = "20130101" |
---|
7 | >>> namobs = observations(date, types=["profbfiles"]) |
---|
8 | |
---|
9 | >>> namooo, namcl4 = forecasts(date, types=["forecast", "persistence"], lead_times=[12, 36, 60]) |
---|
10 | """ |
---|
11 | |
---|
12 | def observations(date, types=None): |
---|
13 | """Responsible for locating observation files |
---|
14 | |
---|
15 | Valid **namobs** observation types. |
---|
16 | |
---|
17 | * profbfiles |
---|
18 | * sstfbfiles |
---|
19 | * slafbfiles |
---|
20 | |
---|
21 | :param date: The verification date in string format ``'%Y%m%d'`` |
---|
22 | :param types: A list of namobs observation types |
---|
23 | |
---|
24 | :returns: namobs namelist dictionary |
---|
25 | """ |
---|
26 | namobs = {"ln_t3d": False, |
---|
27 | "ln_s3d": False, |
---|
28 | "ln_ena": False, |
---|
29 | "ln_profb": False, |
---|
30 | "ln_sst": False, |
---|
31 | "ln_sstfb": False, |
---|
32 | "ln_reysst": False, |
---|
33 | "ln_ghrsst": False, |
---|
34 | "ln_sla": False, |
---|
35 | "ln_slafb": False, |
---|
36 | "ln_sladt": False} |
---|
37 | if types is None: types = [] |
---|
38 | for obtype in types: |
---|
39 | if obtype == "profbfiles": |
---|
40 | namobs[obtype] = profbfiles(date) |
---|
41 | namobs["ln_t3d"] = True |
---|
42 | namobs["ln_s3d"] = True |
---|
43 | namobs["ln_profb"] = True |
---|
44 | elif obtype == "sstfbfiles": |
---|
45 | namobs[obtype] = sstfbfiles(date) |
---|
46 | namobs["ln_sst"] = True |
---|
47 | namobs["ln_sstfb"] = True |
---|
48 | elif obtype == "slafbfiles": |
---|
49 | namobs[obtype] = slafbfiles(date) |
---|
50 | namobs["ln_sla"] = True |
---|
51 | namobs["ln_slafb"] = True |
---|
52 | return namobs |
---|
53 | |
---|
54 | def profbfiles(date): |
---|
55 | """Observation file locator stub |
---|
56 | |
---|
57 | .. note:: User-specified stub |
---|
58 | |
---|
59 | :param date: The verification date in string format ``'%Y%m%d'`` |
---|
60 | :returns: List of files |
---|
61 | """ |
---|
62 | files = ['profb.nc'] |
---|
63 | return files |
---|
64 | |
---|
65 | def sstfbfiles(date): |
---|
66 | """Observation file locator stub |
---|
67 | |
---|
68 | .. note:: User-specified stub |
---|
69 | |
---|
70 | :param date: The verification date in string format ``'%Y%m%d'`` |
---|
71 | :returns: List of files |
---|
72 | """ |
---|
73 | files = ['sstfb.nc'] |
---|
74 | return files |
---|
75 | |
---|
76 | def slafbfiles(date): |
---|
77 | """Observation file locator stub |
---|
78 | |
---|
79 | .. note:: User-specified stub |
---|
80 | |
---|
81 | :param date: The verification date in string format ``'%Y%m%d'`` |
---|
82 | :returns: List of files |
---|
83 | """ |
---|
84 | files = ['slafb.nc'] |
---|
85 | return files |
---|
86 | |
---|
87 | def forecasts(date, types=None, lead_times=None): |
---|
88 | """Responsible for locating forecast fields |
---|
89 | |
---|
90 | :param date: The verification date in string format ``'%Y%m%d'`` |
---|
91 | :param types: A list of forecast system types |
---|
92 | :param lead_times: A list of lead_times to search for |
---|
93 | |
---|
94 | :returns: tuple of namelist data, (namooo, namcl4) |
---|
95 | """ |
---|
96 | namooo = {} |
---|
97 | namcl4 = {} |
---|
98 | if types is None: types = [] |
---|
99 | if lead_times is None: lead_times = [] |
---|
100 | |
---|
101 | # Initialise data |
---|
102 | ooo_files = [] |
---|
103 | nn_ooo_idx = [] |
---|
104 | cl4_vars = [] |
---|
105 | cl4_fcst_idx = [] |
---|
106 | |
---|
107 | # Search for files |
---|
108 | for type in types: |
---|
109 | files = [] |
---|
110 | in_indices = [] |
---|
111 | out_indices = [] |
---|
112 | for ilt, lead_time in enumerate(lead_times): |
---|
113 | file, index = field(date, type=type, lead_time=lead_time) |
---|
114 | files.append(file) |
---|
115 | in_indices.append(index) |
---|
116 | out_indices.append(ilt + 1) |
---|
117 | # Expand return lists |
---|
118 | ooo_files += files |
---|
119 | nn_ooo_idx += in_indices |
---|
120 | cl4_fcst_idx += out_indices |
---|
121 | cl4_vars += len(files) * [type] |
---|
122 | |
---|
123 | # Namoo |
---|
124 | namooo["ooo_files"] = ooo_files |
---|
125 | namooo["nn_ooo_idx"] = nn_ooo_idx |
---|
126 | |
---|
127 | # Namcl4 |
---|
128 | namcl4["cl4_vars"] = cl4_vars |
---|
129 | namcl4["cl4_fcst_idx"] = cl4_fcst_idx |
---|
130 | |
---|
131 | return namooo, namcl4 |
---|
132 | |
---|
133 | def field(date, type=None, lead_time=None): |
---|
134 | """Forecast field locator |
---|
135 | |
---|
136 | Maps verification date and lead time off set to file name and |
---|
137 | index along file *time_counter* |
---|
138 | |
---|
139 | .. note:: User-specified stub |
---|
140 | |
---|
141 | :param date: The verification date in string format ``'%Y%m%d'`` |
---|
142 | :param type: Forecast type |
---|
143 | :param lead_time: Forecast off set |
---|
144 | |
---|
145 | :returns: (**path**, **index**) |
---|
146 | """ |
---|
147 | # Worker function |
---|
148 | file = 'nofile' |
---|
149 | index = -1 |
---|
150 | return file, index |
---|
151 | |
---|