|
6 | 6 | "source": [ |
7 | 7 | "# core\n", |
8 | 8 | "\n", |
9 | | - "> Here we define the core functions that will be used in ERA5 repo." |
| 9 | + "> This is a core library for the ERA5 dataset pipeline. It defines\n", |
| 10 | + "a few helpful functions such as an API tester to test your API key and connection." |
10 | 11 | ] |
11 | 12 | }, |
12 | 13 | { |
|
28 | 29 | "from nbdev.showdoc import *" |
29 | 30 | ] |
30 | 31 | }, |
| 32 | + { |
| 33 | + "cell_type": "code", |
| 34 | + "execution_count": null, |
| 35 | + "metadata": {}, |
| 36 | + "outputs": [], |
| 37 | + "source": [ |
| 38 | + "#| export\n", |
| 39 | + "import os\n", |
| 40 | + "import cdsapi\n", |
| 41 | + "import hydra\n", |
| 42 | + "\n", |
| 43 | + "from omegaconf import DictConfig, OmegaConf\n", |
| 44 | + "from pyprojroot import here" |
| 45 | + ] |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "code", |
| 49 | + "execution_count": null, |
| 50 | + "metadata": {}, |
| 51 | + "outputs": [], |
| 52 | + "source": [ |
| 53 | + "#| exporti\n", |
| 54 | + "def _expand_path(\n", |
| 55 | + " path: str # Path on user's machine\n", |
| 56 | + " )-> str: # Expanded path\n", |
| 57 | + " \"Expand the path on the user's machine for cross compatibility\"\n", |
| 58 | + "\n", |
| 59 | + " # Expand ~ to the user's home directory\n", |
| 60 | + " path = os.path.expanduser(path)\n", |
| 61 | + " # Expand environment variables\n", |
| 62 | + " path = os.path.expandvars(path)\n", |
| 63 | + " # Convert to absolute path\n", |
| 64 | + " path = os.path.abspath(path)\n", |
| 65 | + " return path" |
| 66 | + ] |
| 67 | + }, |
31 | 68 | { |
32 | 69 | "cell_type": "code", |
33 | 70 | "execution_count": null, |
|
39 | 76 | " print(\"This package fetches ERA5 data.\")" |
40 | 77 | ] |
41 | 78 | }, |
| 79 | + { |
| 80 | + "cell_type": "code", |
| 81 | + "execution_count": null, |
| 82 | + "metadata": {}, |
| 83 | + "outputs": [], |
| 84 | + "source": [ |
| 85 | + "#| export\n", |
| 86 | + "#@hydra.main(version_base=None, config_path=here() / \"conf\", config_name=\"config\")\n", |
| 87 | + "def testAPI(\n", |
| 88 | + " output_path:str=None,\n", |
| 89 | + " dataset:str=\"reanalysis-era5-pressure-levels\",\n", |
| 90 | + " remove:bool=False,\n", |
| 91 | + " cfg: DictConfig = None\n", |
| 92 | + " )-> bool: \n", |
| 93 | + " \n", |
| 94 | + " #print(OmegaConf.to_yaml(cfg))\n", |
| 95 | + "\n", |
| 96 | + " try:\n", |
| 97 | + " client = cdsapi.Client()\n", |
| 98 | + "\n", |
| 99 | + " # check the path\n", |
| 100 | + " if output_path is None:\n", |
| 101 | + " output_path = here() / \"data\"\n", |
| 102 | + " else:\n", |
| 103 | + " output_path = _expand_path(output_path)\n", |
| 104 | + "\n", |
| 105 | + " if not os.path.exists(output_path):\n", |
| 106 | + " os.makedirs(output_path)\n", |
| 107 | + "\n", |
| 108 | + " # build request\n", |
| 109 | + " request = {\n", |
| 110 | + " 'product_type': ['reanalysis'],\n", |
| 111 | + " 'variable': ['geopotential'],\n", |
| 112 | + " 'year': ['2024'],\n", |
| 113 | + " 'month': ['03'],\n", |
| 114 | + " 'day': ['01'],\n", |
| 115 | + " 'time': ['13:00'],\n", |
| 116 | + " 'pressure_level': ['1000'],\n", |
| 117 | + " 'data_format': 'grib',\n", |
| 118 | + " }\n", |
| 119 | + "\n", |
| 120 | + " target = output_path / 'download.grib'\n", |
| 121 | + " \n", |
| 122 | + " print(\"Testing API connection by downloading a dummy dataset to {}...\".format(output_path))\n", |
| 123 | + "\n", |
| 124 | + " client.retrieve(dataset, request, target)\n", |
| 125 | + "\n", |
| 126 | + " if remove:\n", |
| 127 | + " os.remove(target)\n", |
| 128 | + " \n", |
| 129 | + " print(\"API connection test successful.\")\n", |
| 130 | + " return True\n", |
| 131 | + "\n", |
| 132 | + " except Exception as e:\n", |
| 133 | + " print(\"API connection test failed.\")\n", |
| 134 | + " print(\"Did you set up your API key with CDS? If not, please visit https://cds.climate.copernicus.eu/api-how-to\")\n", |
| 135 | + " print(\"Error: {}\".format(e))\n", |
| 136 | + " return False" |
| 137 | + ] |
| 138 | + }, |
| 139 | + { |
| 140 | + "cell_type": "code", |
| 141 | + "execution_count": null, |
| 142 | + "metadata": {}, |
| 143 | + "outputs": [], |
| 144 | + "source": [ |
| 145 | + "#export\n", |
| 146 | + "if __name__ == \"__main__\":\n", |
| 147 | + " describe()\n", |
| 148 | + " testAPI()\n", |
| 149 | + "else:\n", |
| 150 | + " pass" |
| 151 | + ] |
| 152 | + }, |
42 | 153 | { |
43 | 154 | "cell_type": "code", |
44 | 155 | "execution_count": null, |
|
52 | 163 | ], |
53 | 164 | "metadata": { |
54 | 165 | "kernelspec": { |
55 | | - "display_name": "python3", |
| 166 | + "display_name": "era5_sandbox", |
56 | 167 | "language": "python", |
57 | 168 | "name": "python3" |
| 169 | + }, |
| 170 | + "language_info": { |
| 171 | + "name": "python", |
| 172 | + "version": "3.13.2" |
58 | 173 | } |
59 | 174 | }, |
60 | 175 | "nbformat": 4, |
|
0 commit comments