|
3 | 3 | from climate_toolbox.utils.utils import * |
4 | 4 |
|
5 | 5 |
|
6 | | -def standardize_climate_data(ds): |
7 | | - """ |
8 | | - Read climate data and standardize units to: |
9 | | - - lon and lat, |
10 | | - - lon to -180 to 180 and |
| 6 | +def load_climate_data(data_type, file_path): |
| 7 | + """ load_climate_data(data_type, file_path) |
| 8 | + Read and prepare climate data |
| 9 | +
|
| 10 | + :param data_type: str |
| 11 | + datatype to be read, supported types: |
| 12 | + bcsd, gmfd, best, era5 |
11 | 13 |
|
12 | | - Parameters |
13 | | - ---------- |
14 | | - ds: xr.Dataset |
| 14 | + :param file_path: str |
| 15 | + File path |
15 | 16 |
|
16 | | - Returns |
17 | | - ------- |
18 | | - xr.Dataset |
| 17 | + :return: ds: xr.Dataset |
| 18 | + xarray dataset loaded in memory |
19 | 19 | """ |
20 | 20 |
|
21 | | - ds = rename_coords_to_lon_and_lat(ds) |
22 | | - ds = convert_lons_split(ds, lon_name='lon') |
| 21 | + return _load_climate_data( |
| 22 | + _find_loader(data_type), |
| 23 | + file_path |
| 24 | + ) |
| 25 | + |
| 26 | + |
| 27 | +def _load_climate_data(loader, file_path): |
| 28 | + with xr.open_dataset(file_path) as ds: |
| 29 | + ds.load() |
| 30 | + |
| 31 | + return loader(ds) |
23 | 32 |
|
24 | | - return ds |
25 | 33 |
|
| 34 | +def load_min_max_temperatures(data_type, file_path_tmin, file_path_tmax): |
| 35 | + """ load_min_max_temperatures(data_type, file_path_tmin, file_path_tmax) |
26 | 36 |
|
27 | | -def load_bcsd(fp, varname, lon_name='lon', broadcast_dims=('time',)): |
| 37 | + :param data_type: str |
| 38 | + datatype to be read, supported types: |
| 39 | + bcsd, gmfd, best, era5 |
| 40 | +
|
| 41 | + :param file_path_tmin: path for min temperature |
| 42 | + :param file_path_tmax: path for max temperature |
| 43 | + :return: |
| 44 | + ds_tasmax: xr.Dataset, ds_tasmin: xr.Dataset |
28 | 45 | """ |
29 | | - Read and prepare climate data |
30 | 46 |
|
31 | | - After reading data, this method also fills NA values using linear |
32 | | - interpolation, and standardizes longitude to -180:180 |
| 47 | + return _load_min_max_temperatures( |
| 48 | + _find_loader(data_type), |
| 49 | + file_path_tmin, |
| 50 | + file_path_tmax |
| 51 | + ) |
33 | 52 |
|
34 | | - Parameters |
35 | | - ---------- |
36 | | - fp: str |
37 | | - File path or dataset |
38 | 53 |
|
39 | | - varname: str |
40 | | - Variable name to be read |
| 54 | +def _load_min_max_temperatures(loader, file_path_tmin, file_path_tmax): |
| 55 | + with xr.open_dataset(file_path_tmin) as ds_tasmin: |
| 56 | + ds_tasmin.load() |
| 57 | + with xr.open_dataset(file_path_tmax) as ds_tasmax: |
| 58 | + ds_tasmax.load() |
41 | 59 |
|
42 | | - lon_name : str, optional |
43 | | - Name of the longitude dimension (defualt selects from ['lon' or |
44 | | - 'longitude']) |
| 60 | + return loader(ds_tasmin), loader(ds_tasmax) |
45 | 61 |
|
46 | | - Returns |
47 | | - ------- |
48 | | - xr.Dataset |
49 | | - xarray dataset loaded into memory |
50 | | - """ |
51 | 62 |
|
52 | | - if lon_name is not None: |
53 | | - lon_names = [lon_name] |
| 63 | +def _find_loader(data_type): |
| 64 | + """ Helper function to find climate data loader """ |
54 | 65 |
|
55 | | - if hasattr(fp, 'sel_points'): |
56 | | - ds = fp |
| 66 | + data_type = data_type.lower() |
57 | 67 |
|
| 68 | + if 'bcsd' in data_type: |
| 69 | + loader = load_bcsd |
| 70 | + elif 'gmfd' in data_type: |
| 71 | + loader = load_gmfd |
| 72 | + elif 'best' in data_type: |
| 73 | + loader = load_best |
| 74 | + elif 'era' in data_type: |
| 75 | + loader = load_era5 |
58 | 76 | else: |
59 | | - with xr.open_dataset(fp) as ds: |
60 | | - ds.load() |
| 77 | + raise TypeError("'" + data_type + "' not supported. Supported data " |
| 78 | + "types are: NASA BCSD, GMFD, BEST, ERA5.") |
| 79 | + return loader |
| 80 | + |
61 | 81 |
|
62 | | - return standardize_climate_data(ds) |
| 82 | +def standardize_climate_data(ds): |
| 83 | + """ standardize_climate_data(ds) |
| 84 | + Standardize climate data units to: |
| 85 | + - lon and lat, |
| 86 | + - lon to -180 to 180 and |
| 87 | +
|
| 88 | + :param ds: xr.Dataset |
| 89 | + :return: ds: xr.Dataset |
| 90 | + """ |
63 | 91 |
|
| 92 | + ds = rename_coords_to_lon_and_lat(ds) |
| 93 | + ds = convert_lons_split(ds, lon_name='lon') |
64 | 94 |
|
65 | | -def load_gmfd(fp, varname, lon_name='lon', broadcast_dims=('time',)): |
66 | | - pass |
| 95 | + return ds |
| 96 | + |
| 97 | + |
| 98 | +def load_bcsd(ds): |
| 99 | + return ds |
67 | 100 |
|
68 | 101 |
|
69 | | -def load_best(fp, varname, lon_name='lon', broadcast_dims=('time',)): |
| 102 | +def load_gmfd(ds): |
| 103 | + if 'tmin' in ds.data_vars or 'tmax' in ds.data_vars: |
| 104 | + return standardize_climate_data(ds) |
| 105 | + if 'lat' not in ds.coords or 'lon' not in ds.coords: |
| 106 | + ds = rename_coords_to_lon_and_lat(ds) |
| 107 | + return convert_lons_split(ds, lon_name="lon") |
| 108 | + |
| 109 | + |
| 110 | +def load_best(): |
70 | 111 | pass |
71 | 112 |
|
| 113 | + |
| 114 | +def load_era5(): |
| 115 | + pass |
0 commit comments