|
46 | 46 | "import os\n", |
47 | 47 | "import hydra\n", |
48 | 48 | "import cdsapi\n", |
| 49 | + "import geopandas as gpd\n", |
49 | 50 | "from core import _expand_path\n", |
50 | | - "from omegaconf import DictConfig, OmegaConf" |
| 51 | + "from pyprojroot import here\n", |
| 52 | + "from omegaconf import DictConfig, ListConfig, OmegaConf" |
51 | 53 | ] |
52 | 54 | }, |
53 | 55 | { |
|
69 | 71 | " print(f\"Missing required key in query. Required keys are {required_keys}\")\n", |
70 | 72 | " print(\"Query validation failed\")\n", |
71 | 73 | " raise ValueError(\"Invalid query\")\n", |
72 | | - " \n", |
| 74 | + " \n", |
| 75 | + " if isinstance(query_body['year'], ListConfig):\n", |
| 76 | + " query_body['year'] = [str(x).zfill(2) for x in query_body['year']]\n", |
| 77 | + " else:\n", |
| 78 | + " query_body['year'] = str(query_body['year'])\n", |
| 79 | + " if isinstance(query_body['month'], ListConfig):\n", |
| 80 | + " query_body['month'] = [str(x).zfill(2) for x in query_body['month']]\n", |
| 81 | + " else:\n", |
| 82 | + " query_body['month'] = str(query_body['month']).zfill(2)\n", |
| 83 | + " \n", |
| 84 | + " if isinstance(query_body['day'], ListConfig):\n", |
| 85 | + " query_body['day'] = [str(x).zfill(2) for x in query_body['day']]\n", |
73 | 86 | " else:\n", |
74 | | - " return query_body" |
| 87 | + " query_body['day'] = str(query_body['day']).zfill(2)\n", |
| 88 | + "\n", |
| 89 | + " return OmegaConf.to_container(query_body, resolve=True)" |
75 | 90 | ] |
76 | 91 | }, |
77 | 92 | { |
|
81 | 96 | "outputs": [], |
82 | 97 | "source": [ |
83 | 98 | "#| export\n", |
84 | | - "def fetch_MDG_GADM(\n", |
| 99 | + "def fetch_GADM(\n", |
| 100 | + " url: str=\"https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/gadm41_MDG.gpkg\",\n", |
85 | 101 | " output_file: str=\"gadm41_MDG.gpkg\" # file path to save the GADM data\n", |
86 | 102 | " )-> str:\n", |
87 | 103 | " '''\n", |
|
94 | 110 | " print(\"GADM data already exists\")\n", |
95 | 111 | " return output_file_path\n", |
96 | 112 | " \n", |
97 | | - " print(\"Fetching GADM bounding box data for Madagascar\")\n", |
98 | | - " os.system(\"curl --output {} https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/gadm41_MDG.gpkg\".format(output_file))\n", |
| 113 | + " print(\"Fetching GADM bounding box data for region\")\n", |
| 114 | + " os.system(\"curl --output {} {}\".format(output_file, url))\n", |
99 | 115 | " print(\"GADM data fetched\")\n", |
100 | 116 | " \n", |
101 | 117 | " return output_file_path" |
|
108 | 124 | "outputs": [], |
109 | 125 | "source": [ |
110 | 126 | "#| export\n", |
111 | | - "def download(\n", |
| 127 | + "\n", |
| 128 | + "def create_bounding_box(\n", |
| 129 | + " gadm_file: str, \n", |
| 130 | + " round_to: int = 1, \n", |
| 131 | + " buffer: float = 0.1)->list:\n", |
| 132 | + " '''\n", |
| 133 | + " Create a bounding box from the GADM data.\n", |
| 134 | + "\n", |
| 135 | + " This function reads the GADM data from URL and extracts the bounding box of the region.\n", |
| 136 | + " '''\n", |
| 137 | + "\n", |
| 138 | + " ground_shape = gpd.read_file(gadm_file, layer = \"ADM_ADM_0\")\n", |
| 139 | + "\n", |
| 140 | + " bbox = ground_shape.total_bounds\n", |
| 141 | + "\n", |
| 142 | + " bbox[0] = round(bbox[0], round_to) - buffer\n", |
| 143 | + " bbox[1] = round(bbox[1], round_to) - buffer\n", |
| 144 | + " bbox[2] = round(bbox[2], round_to) + buffer\n", |
| 145 | + " bbox[3] = round(bbox[3], round_to) + buffer\n", |
| 146 | + " \n", |
| 147 | + " bbox = [bbox[0], bbox[2], bbox[1], bbox[3]]\n", |
| 148 | + "\n", |
| 149 | + " return bbox\n" |
| 150 | + ] |
| 151 | + }, |
| 152 | + { |
| 153 | + "cell_type": "code", |
| 154 | + "execution_count": null, |
| 155 | + "metadata": {}, |
| 156 | + "outputs": [], |
| 157 | + "source": [ |
| 158 | + "#| export\n", |
| 159 | + "def download_raw_era5(\n", |
112 | 160 | " cfg: DictConfig, # hydra configuration file\n", |
113 | | - " output_dir: str = \"data/input/\", # output directory\n", |
114 | | - " dataset: str = \"reanalysis-era5-pressure-levels\", # dataset to download\n", |
115 | | - " testing: bool = False # testing mode\n", |
| 161 | + " dataset: str = \"reanalysis-era5-land\", # dataset to download\n", |
116 | 162 | " )->None:\n", |
117 | 163 | " '''\n", |
118 | 164 | " Send the query to the API and download the data\n", |
119 | 165 | " '''\n", |
| 166 | + "\n", |
| 167 | + " # parse the cfg\n", |
| 168 | + " testing = cfg.development_mode # for testing\n", |
| 169 | + " output_dir = here(\"data/input\") # output directory\n", |
120 | 170 | " \n", |
121 | 171 | " client = cdsapi.Client()\n", |
122 | 172 | " \n", |
123 | 173 | " query = _validate_query(cfg.query)\n", |
124 | 174 | " \n", |
125 | 175 | " # Send the query to the client\n", |
126 | 176 | " if not testing:\n", |
127 | | - " client.retrieve(dataset, query).download(os.path.join(_expand_path(output_dir), \"{}_{}.nc\".format(query.year, query.month)))\n", |
| 177 | + " bounds = create_bounding_box(cfg.query['gadm_file'])\n", |
| 178 | + " query['area'] = bounds\n", |
| 179 | + " del query['gadm_file']\n", |
| 180 | + " client.retrieve(dataset, query).download(os.path.join(_expand_path(output_dir), \"{}_{}.nc\".format(query['year'], query['month'])))\n", |
128 | 181 | " else:\n", |
129 | 182 | " print(f\"Testing mode. Not downloading data. Query is {query}\")\n", |
130 | 183 | "\n", |
|
157 | 210 | "with initialize(version_base=None, config_path=\"../conf\"):\n", |
158 | 211 | " cfg = compose(config_name='config.yaml')\n", |
159 | 212 | "\n", |
160 | | - "download(cfg, testing=True)" |
| 213 | + "download_raw_era5(cfg, testing=True)" |
161 | 214 | ] |
162 | 215 | }, |
163 | 216 | { |
|
167 | 220 | "outputs": [], |
168 | 221 | "source": [ |
169 | 222 | "#| export\n", |
170 | | - "@hydra.main(config_path=\"../conf\", config_name=\"config\", version_base=None)\n", |
| 223 | + "@hydra.main(config_path=\"../../conf\", config_name=\"config\", version_base=None)\n", |
171 | 224 | "def main(cfg: DictConfig) -> None:\n", |
172 | | - " download(cfg=cfg)" |
| 225 | + " download_raw_era5(cfg=cfg)" |
173 | 226 | ] |
174 | 227 | }, |
175 | 228 | { |
|
206 | 259 | "name": "python3" |
207 | 260 | }, |
208 | 261 | "language_info": { |
| 262 | + "codemirror_mode": { |
| 263 | + "name": "ipython", |
| 264 | + "version": 3 |
| 265 | + }, |
| 266 | + "file_extension": ".py", |
| 267 | + "mimetype": "text/x-python", |
209 | 268 | "name": "python", |
| 269 | + "nbconvert_exporter": "python", |
| 270 | + "pygments_lexer": "ipython3", |
210 | 271 | "version": "3.11.11" |
211 | 272 | } |
212 | 273 | }, |
|
0 commit comments