|
| 1 | +# (C) Copyright 2020 ECMWF. |
| 2 | +# |
| 3 | +# This software is licensed under the terms of the Apache Licence Version 2.0 |
| 4 | +# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
| 5 | +# In applying this licence, ECMWF does not waive the privileges and immunities |
| 6 | +# granted to it by virtue of its status as an intergovernmental organisation |
| 7 | +# nor does it submit to any jurisdiction. |
| 8 | +# |
| 9 | + |
| 10 | +import warnings |
| 11 | + |
| 12 | +import numpy as np |
| 13 | +import tqdm |
| 14 | +from climetlab import load_source |
| 15 | +from climetlab.core.temporary import temp_file |
| 16 | +from climetlab.readers.grib.output import new_grib_output |
| 17 | + |
| 18 | +from ecml_tools.create.check import check_data_values |
| 19 | + |
| 20 | + |
| 21 | +def get_unique_field(ds, selection): |
| 22 | + ds = ds.sel(**selection) |
| 23 | + assert len(ds) == 1, (ds, selection) |
| 24 | + return ds[0] |
| 25 | + |
| 26 | + |
| 27 | +def normalise_number(number): |
| 28 | + if isinstance(number, (tuple, list, int)): |
| 29 | + return number |
| 30 | + |
| 31 | + assert isinstance(number, str), (type(number), number) |
| 32 | + |
| 33 | + number = number.split("/") |
| 34 | + if len(number) > 4 and (number[1] == "to" and number[3] == "by"): |
| 35 | + return list(range(int(number[0]), int(number[2]) + 1, int(number[4]))) |
| 36 | + |
| 37 | + if len(number) > 2 and number[1] == "to": |
| 38 | + return list(range(int(number[0]), int(number[2]) + 1)) |
| 39 | + |
| 40 | + assert isinstance(number, list), (type(number), number) |
| 41 | + return number |
| 42 | + |
| 43 | + |
| 44 | +def ensembles_perturbations(ensembles, center, mean, remapping={}, patches={}): |
| 45 | + n_ensembles = len(normalise_number(ensembles["number"])) |
| 46 | + |
| 47 | + print(f"Retrieving ensemble data with {ensembles}") |
| 48 | + ensembles = load_source(**ensembles) |
| 49 | + print(f"Retrieving center data with {center}") |
| 50 | + center = load_source(**center) |
| 51 | + print(f"Retrieving mean data with {mean}") |
| 52 | + mean = load_source(**mean) |
| 53 | + |
| 54 | + assert len(mean) * n_ensembles == len(ensembles), ( |
| 55 | + len(mean), |
| 56 | + n_ensembles, |
| 57 | + len(ensembles), |
| 58 | + ) |
| 59 | + assert len(center) * n_ensembles == len(ensembles), ( |
| 60 | + len(center), |
| 61 | + n_ensembles, |
| 62 | + len(ensembles), |
| 63 | + ) |
| 64 | + |
| 65 | + tmp = temp_file() |
| 66 | + path = tmp.path |
| 67 | + out = new_grib_output(path) |
| 68 | + |
| 69 | + keys = ["param", "level", "valid_datetime", "number", "date", "time", "step"] |
| 70 | + |
| 71 | + ensembles_coords = ensembles.unique_values(*keys) |
| 72 | + center_coords = center.unique_values(*keys) |
| 73 | + mean_coords = mean.unique_values(*keys) |
| 74 | + |
| 75 | + for k in keys: |
| 76 | + if k == "number": |
| 77 | + assert len(mean_coords[k]) == 1 |
| 78 | + assert len(center_coords[k]) == 1 |
| 79 | + assert len(ensembles_coords[k]) == n_ensembles |
| 80 | + continue |
| 81 | + assert set(center_coords[k]) == set(ensembles_coords[k]), ( |
| 82 | + k, |
| 83 | + center_coords[k], |
| 84 | + ensembles_coords[k], |
| 85 | + ) |
| 86 | + assert set(center_coords[k]) == set(mean_coords[k]), ( |
| 87 | + k, |
| 88 | + center_coords[k], |
| 89 | + mean_coords[k], |
| 90 | + ) |
| 91 | + |
| 92 | + for field in tqdm.tqdm(center): |
| 93 | + param = field.metadata("param") |
| 94 | + grid = field.metadata("grid") |
| 95 | + |
| 96 | + selection = dict( |
| 97 | + valid_datetime=field.metadata("valid_datetime"), |
| 98 | + param=field.metadata("param"), |
| 99 | + level=field.metadata("level"), |
| 100 | + date=field.metadata("date"), |
| 101 | + time=field.metadata("time"), |
| 102 | + step=field.metadata("step"), |
| 103 | + ) |
| 104 | + mean_field = get_unique_field(mean, selection) |
| 105 | + assert mean_field.metadata("grid") == grid, (mean_field.metadata("grid"), grid) |
| 106 | + |
| 107 | + m = mean_field.to_numpy() |
| 108 | + c = field.to_numpy() |
| 109 | + assert m.shape == c.shape, (m.shape, c.shape) |
| 110 | + |
| 111 | + for number in ensembles_coords["number"]: |
| 112 | + ensembles_field = get_unique_field(ensembles.sel(number=number), selection) |
| 113 | + assert ensembles_field.metadata("grid") == grid, ( |
| 114 | + ensembles_field.metadata("grid"), |
| 115 | + grid, |
| 116 | + ) |
| 117 | + |
| 118 | + e = ensembles_field.to_numpy() |
| 119 | + assert c.shape == e.shape, (c.shape, e.shape) |
| 120 | + |
| 121 | + x = c + m - e |
| 122 | + if param == "q": |
| 123 | + warnings.warn("Clipping q") |
| 124 | + x = np.maximum(x, 0) |
| 125 | + |
| 126 | + assert x.shape == c.shape, (x.shape, c.shape) |
| 127 | + |
| 128 | + check_data_values(x, name=param) |
| 129 | + out.write(x, template=ensembles_field) |
| 130 | + |
| 131 | + out.close() |
| 132 | + |
| 133 | + ds = load_source("file", path) |
| 134 | + assert len(ds) == len(ensembles), (len(ds), len(ensembles)) |
| 135 | + ds._tmp = tmp |
| 136 | + |
| 137 | + assert len(mean) * n_ensembles == len(ensembles) |
| 138 | + assert len(center) * n_ensembles == len(ensembles) |
| 139 | + |
| 140 | + final_coords = ds.unique_values(*keys) |
| 141 | + assert len(final_coords["number"]) == n_ensembles, final_coords |
| 142 | + return ds |
| 143 | + |
| 144 | + |
| 145 | +execute = ensembles_perturbations |
| 146 | + |
| 147 | +if __name__ == "__main__": |
| 148 | + import yaml |
| 149 | + |
| 150 | + config = yaml.safe_load( |
| 151 | + """ |
| 152 | +
|
| 153 | + common: &common |
| 154 | + name: mars |
| 155 | + # marser is the MARS containing ERA5 reanalysis dataset, avoid hitting the FDB server for nothing |
| 156 | + database: marser |
| 157 | + class: ea |
| 158 | + # date: $datetime_format($dates,%Y%m%d) |
| 159 | + # time: $datetime_format($dates,%H%M) |
| 160 | + date: 20221230/to/20230103 |
| 161 | + time: '0000/1200' |
| 162 | + expver: '0001' |
| 163 | + grid: 20.0/20.0 |
| 164 | + levtype: sfc |
| 165 | + param: [2t] |
| 166 | + # levtype: pl |
| 167 | + # param: [10u, 10v, 2d, 2t, lsm, msl, sdor, skt, slor, sp, tcw, z] |
| 168 | +
|
| 169 | + config: |
| 170 | + ensembles: # the ensemble data has one additional dimension |
| 171 | + <<: *common |
| 172 | + stream: enda |
| 173 | + type: an |
| 174 | + number: [0, 1] |
| 175 | + # number: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 176 | +
|
| 177 | + center: # the new center of the data |
| 178 | + <<: *common |
| 179 | + stream: oper |
| 180 | + type: an |
| 181 | +
|
| 182 | + mean: # the previous center of the data |
| 183 | + <<: *common |
| 184 | + stream: enda |
| 185 | + type: em |
| 186 | +
|
| 187 | + """ |
| 188 | + )["config"] |
| 189 | + for k, v in config.items(): |
| 190 | + print(k, v) |
| 191 | + |
| 192 | + for f in ensembles_perturbations(**config): |
| 193 | + print(f, f.to_numpy().mean()) |
0 commit comments