|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +pysteps.converters |
| 4 | +================== |
| 5 | +
|
| 6 | +Module with data converter functions. |
| 7 | +
|
| 8 | +.. autosummary:: |
| 9 | + :toctree: ../generated/ |
| 10 | +
|
| 11 | + convert_to_xarray_dataset |
| 12 | +""" |
| 13 | + |
| 14 | +import numpy as np |
| 15 | +import pyproj |
| 16 | +import xarray as xr |
| 17 | + |
| 18 | +from pysteps.utils.conversion import cf_parameters_from_unit |
| 19 | + |
| 20 | +# TODO(converters): Write methods for converting Proj.4 projection definitions |
| 21 | +# into CF grid mapping attributes. Currently this has been implemented for |
| 22 | +# the stereographic projection. |
| 23 | +# The conversions implemented here are take from: |
| 24 | +# https://github.com/cf-convention/cf-convention.github.io/blob/master/wkt-proj-4.md |
| 25 | + |
| 26 | + |
| 27 | +def _convert_proj4_to_grid_mapping(proj4str): |
| 28 | + tokens = proj4str.split("+") |
| 29 | + |
| 30 | + d = {} |
| 31 | + for t in tokens[1:]: |
| 32 | + t = t.split("=") |
| 33 | + if len(t) > 1: |
| 34 | + d[t[0]] = t[1].strip() |
| 35 | + |
| 36 | + params = {} |
| 37 | + # TODO(exporters): implement more projection types here |
| 38 | + if d["proj"] == "stere": |
| 39 | + grid_mapping_var_name = "polar_stereographic" |
| 40 | + grid_mapping_name = "polar_stereographic" |
| 41 | + v = d["lon_0"] if d["lon_0"][-1] not in ["E", "W"] else d["lon_0"][:-1] |
| 42 | + params["straight_vertical_longitude_from_pole"] = float(v) |
| 43 | + v = d["lat_0"] if d["lat_0"][-1] not in ["N", "S"] else d["lat_0"][:-1] |
| 44 | + params["latitude_of_projection_origin"] = float(v) |
| 45 | + if "lat_ts" in list(d.keys()): |
| 46 | + params["standard_parallel"] = float(d["lat_ts"]) |
| 47 | + elif "k_0" in list(d.keys()): |
| 48 | + params["scale_factor_at_projection_origin"] = float(d["k_0"]) |
| 49 | + params["false_easting"] = float(d["x_0"]) |
| 50 | + params["false_northing"] = float(d["y_0"]) |
| 51 | + elif d["proj"] == "aea": # Albers Conical Equal Area |
| 52 | + grid_mapping_var_name = "proj" |
| 53 | + grid_mapping_name = "albers_conical_equal_area" |
| 54 | + params["false_easting"] = float(d["x_0"]) if "x_0" in d else float(0) |
| 55 | + params["false_northing"] = float(d["y_0"]) if "y_0" in d else float(0) |
| 56 | + v = d["lon_0"] if "lon_0" in d else float(0) |
| 57 | + params["longitude_of_central_meridian"] = float(v) |
| 58 | + v = d["lat_0"] if "lat_0" in d else float(0) |
| 59 | + params["latitude_of_projection_origin"] = float(v) |
| 60 | + v1 = d["lat_1"] if "lat_1" in d else float(0) |
| 61 | + v2 = d["lat_2"] if "lat_2" in d else float(0) |
| 62 | + params["standard_parallel"] = (float(v1), float(v2)) |
| 63 | + else: |
| 64 | + print("unknown projection", d["proj"]) |
| 65 | + return None, None, None |
| 66 | + |
| 67 | + return grid_mapping_var_name, grid_mapping_name, params |
| 68 | + |
| 69 | + |
| 70 | +def convert_to_xarray_dataset( |
| 71 | + precip: np.ndarray, |
| 72 | + quality: np.ndarray | None, |
| 73 | + metadata: dict[str, str | float | None], |
| 74 | +) -> xr.Dataset: |
| 75 | + """ |
| 76 | + Read a precip, quality, metadata tuple as returned by the importers |
| 77 | + (:py:mod:`pysteps.io.importers`) and return an xarray dataset containing |
| 78 | + this data. |
| 79 | +
|
| 80 | + Parameters |
| 81 | + ---------- |
| 82 | + precip: array |
| 83 | + 2D array containing imported precipitation data. |
| 84 | + quality: array, None |
| 85 | + 2D array containing the quality values of the imported precipitation |
| 86 | + data, can be None. |
| 87 | + metadata: dict |
| 88 | + Metadata dictionary containing the attributes described in the |
| 89 | + documentation of :py:mod:`pysteps.io.importers`. |
| 90 | +
|
| 91 | + Returns |
| 92 | + ------- |
| 93 | + out: Dataset |
| 94 | + A CF compliant xarray dataset, which contains all data and metadata. |
| 95 | +
|
| 96 | + """ |
| 97 | + var_name, attrs = cf_parameters_from_unit(metadata["unit"]) |
| 98 | + h, w = precip.shape |
| 99 | + x_r = np.linspace(metadata["x1"], metadata["x2"], w + 1)[:-1] |
| 100 | + x_r += 0.5 * (x_r[1] - x_r[0]) |
| 101 | + y_r = np.linspace(metadata["y1"], metadata["y2"], h + 1)[:-1] |
| 102 | + y_r += 0.5 * (y_r[1] - y_r[0]) |
| 103 | + |
| 104 | + # flip yr vector if yorigin is upper |
| 105 | + if metadata["yorigin"] == "upper": |
| 106 | + y_r = np.flip(y_r) |
| 107 | + |
| 108 | + x_2d, y_2d = np.meshgrid(x_r, y_r) |
| 109 | + pr = pyproj.Proj(metadata["projection"]) |
| 110 | + lon, lat = pr(x_2d.flatten(), y_2d.flatten(), inverse=True) |
| 111 | + |
| 112 | + ( |
| 113 | + grid_mapping_var_name, |
| 114 | + grid_mapping_name, |
| 115 | + grid_mapping_params, |
| 116 | + ) = _convert_proj4_to_grid_mapping(metadata["projection"]) |
| 117 | + |
| 118 | + data_vars = { |
| 119 | + var_name: ( |
| 120 | + ["y", "x"], |
| 121 | + precip, |
| 122 | + { |
| 123 | + "units": attrs["units"], |
| 124 | + "standard_name": attrs["standard_name"], |
| 125 | + "long_name": attrs["long_name"], |
| 126 | + "grid_mapping": "projection", |
| 127 | + "transform": metadata["transform"], |
| 128 | + "accutime": metadata["accutime"], |
| 129 | + "threshold": metadata["threshold"], |
| 130 | + "zerovalue": metadata["zerovalue"], |
| 131 | + "zr_a": metadata["zr_a"], |
| 132 | + "zr_b": metadata["zr_b"], |
| 133 | + }, |
| 134 | + ) |
| 135 | + } |
| 136 | + if quality is not None: |
| 137 | + data_vars["quality"] = ( |
| 138 | + ["y", "x"], |
| 139 | + quality, |
| 140 | + { |
| 141 | + "units": "1", |
| 142 | + "standard_name": "quality_flag", |
| 143 | + "grid_mapping": "projection", |
| 144 | + }, |
| 145 | + ) |
| 146 | + coords = { |
| 147 | + "y": ( |
| 148 | + ["y"], |
| 149 | + y_r, |
| 150 | + { |
| 151 | + "axis": "Y", |
| 152 | + "long_name": "y-coordinate in Cartesian system", |
| 153 | + "standard_name": "projection_y_coordinate", |
| 154 | + "units": metadata["cartesian_unit"], |
| 155 | + }, |
| 156 | + ), |
| 157 | + "x": ( |
| 158 | + ["x"], |
| 159 | + x_r, |
| 160 | + { |
| 161 | + "axis": "X", |
| 162 | + "long_name": "x-coordinate in Cartesian system", |
| 163 | + "standard_name": "projection_x_coordinate", |
| 164 | + "units": metadata["cartesian_unit"], |
| 165 | + }, |
| 166 | + ), |
| 167 | + "lon": ( |
| 168 | + ["y", "x"], |
| 169 | + lon.reshape(precip.shape), |
| 170 | + { |
| 171 | + "long_name": "longitude coordinate", |
| 172 | + "standard_name": "longitude", |
| 173 | + # TODO(converters): Don't hard-code the unit. |
| 174 | + "units": "degrees_east", |
| 175 | + }, |
| 176 | + ), |
| 177 | + "lat": ( |
| 178 | + ["y", "x"], |
| 179 | + lat.reshape(precip.shape), |
| 180 | + { |
| 181 | + "long_name": "latitude coordinate", |
| 182 | + "standard_name": "latitude", |
| 183 | + # TODO(converters): Don't hard-code the unit. |
| 184 | + "units": "degrees_north", |
| 185 | + }, |
| 186 | + ), |
| 187 | + } |
| 188 | + if grid_mapping_var_name is not None: |
| 189 | + coords[grid_mapping_name] = ( |
| 190 | + ( |
| 191 | + [], |
| 192 | + None, |
| 193 | + {"grid_mapping_name": grid_mapping_name, **grid_mapping_params}, |
| 194 | + ), |
| 195 | + ) |
| 196 | + attrs = { |
| 197 | + "Conventions": "CF-1.7", |
| 198 | + "institution": metadata["institution"], |
| 199 | + "projection": metadata["projection"], |
| 200 | + "precip_var": var_name, |
| 201 | + } |
| 202 | + return xr.Dataset(data_vars=data_vars, coords=coords, attrs=attrs) |
0 commit comments