|
39 | 39 | },
|
40 | 40 | "outputs": [],
|
41 | 41 | "source": [
|
42 |
| - "import tempfile\n", |
43 |
| - "from io import BytesIO\n", |
44 |
| - "from os import environ\n", |
45 | 42 | "from pathlib import Path\n",
|
46 |
| - "from zipfile import ZipFile\n", |
47 | 43 | "\n",
|
48 |
| - "import numpy as np\n", |
49 | 44 | "import pandas as pd\n",
|
50 | 45 | "import requests\n",
|
51 | 46 | "from cartopy.io import shapereader\n",
|
52 |
| - "from fsspec import FSTimeoutError\n", |
53 |
| - "from fsspec.implementations.zip import ZipFileSystem\n", |
| 47 | + "from pyCIAM.io import (\n", |
| 48 | + " download_and_extract_from_zenodo,\n", |
| 49 | + " download_and_extract_partial_zip,\n", |
| 50 | + " get_zenodo_file_list,\n", |
| 51 | + ")\n", |
| 52 | + "from pyCIAM.utils import copy\n", |
54 | 53 | "from shared import (\n",
|
55 | 54 | " DIR_SHP,\n",
|
56 | 55 | " DIR_SLR_AR5_IFILES_RAW,\n",
|
|
71 | 70 | " PATH_SLR_HIST_TREND_MAP,\n",
|
72 | 71 | " PATHS_SURGE_LOOKUP,\n",
|
73 | 72 | " save,\n",
|
74 |
| - ")\n", |
75 |
| - "\n", |
76 |
| - "from pyCIAM.utils import copy" |
| 73 | + ")" |
77 | 74 | ]
|
78 | 75 | },
|
79 | 76 | {
|
|
123 | 120 | "Z_URL_SLIIDERS_PC = Z_URL_RECORDS"
|
124 | 121 | ]
|
125 | 122 | },
|
126 |
| - { |
127 |
| - "cell_type": "code", |
128 |
| - "execution_count": 49, |
129 |
| - "id": "8d519f83-eb91-4cb0-b2e7-5918b91d5143", |
130 |
| - "metadata": { |
131 |
| - "tags": [] |
132 |
| - }, |
133 |
| - "outputs": [], |
134 |
| - "source": [ |
135 |
| - "def get_download_link(files, prefix):\n", |
136 |
| - " links = [\n", |
137 |
| - " i[\"links\"]\n", |
138 |
| - " for i in files\n", |
139 |
| - " if i.get(\"filename\", \"\").startswith(prefix)\n", |
140 |
| - " or i.get(\"key\", \"\").startswith(prefix)\n", |
141 |
| - " ]\n", |
142 |
| - " assert len(links) == 1\n", |
143 |
| - " links = links[0]\n", |
144 |
| - " return links.get(\"download\", links[\"self\"])\n", |
145 |
| - "\n", |
146 |
| - "\n", |
147 |
| - "def download_and_extract_full_zip(lpath, url):\n", |
148 |
| - " if lpath.exists():\n", |
149 |
| - " return None\n", |
150 |
| - " lpath.parent.mkdir(exist_ok=True, parents=True)\n", |
151 |
| - "\n", |
152 |
| - " content = BytesIO(requests.get(url, params=PARAMS).content)\n", |
153 |
| - " if isinstance(lpath, Path):\n", |
154 |
| - " with ZipFile(content, \"r\") as zip_ref:\n", |
155 |
| - " zip_ref.extractall(lpath)\n", |
156 |
| - " else:\n", |
157 |
| - " with tempfile.TemporaryDirectory() as tmpdir:\n", |
158 |
| - " with ZipFile(content, \"r\") as zip_ref:\n", |
159 |
| - " zip_ref.extractall(tmpdir)\n", |
160 |
| - " copy(Path(tmpdir), lpath)\n", |
161 |
| - "\n", |
162 |
| - "\n", |
163 |
| - "def download_and_extract_partial_zip(lpath, url, zip_glob, n_retries=5):\n", |
164 |
| - " lpath.mkdir(exist_ok=True, parents=True)\n", |
165 |
| - " z = ZipFileSystem(url)\n", |
166 |
| - " if isinstance(zip_glob, (list, set, tuple, np.ndarray)):\n", |
167 |
| - " files_remote = zip_glob\n", |
168 |
| - " else:\n", |
169 |
| - " files_remote = [p for p in z.glob(zip_glob) if not p.endswith(\"/\")]\n", |
170 |
| - " files_local = [lpath / Path(f).name for f in files_remote]\n", |
171 |
| - " for fr, fl in list(zip(files_remote, files_local)):\n", |
172 |
| - " if not fl.is_file():\n", |
173 |
| - " retries = 0\n", |
174 |
| - " while retries < n_retries:\n", |
175 |
| - " print(f\"...Downloading {fl.name} (attempt {retries+1}/{n_retries})\")\n", |
176 |
| - " try:\n", |
177 |
| - " data = z.cat_file(fr)\n", |
178 |
| - " break\n", |
179 |
| - " except FSTimeoutError:\n", |
180 |
| - " if retries < (n_retries - 1):\n", |
181 |
| - " retries += 1\n", |
182 |
| - " else:\n", |
183 |
| - " raise\n", |
184 |
| - " print(f\"...Writing {fl.name}\")\n", |
185 |
| - " fl.write_bytes(data)\n", |
186 |
| - "\n", |
187 |
| - "\n", |
188 |
| - "def download_and_extract_from_zenodo(lpath, files, prefix, zip_glob=None):\n", |
189 |
| - " dl = get_download_link(files, prefix)\n", |
190 |
| - " if zip_glob is None:\n", |
191 |
| - " return download_and_extract_full_zip(lpath, dl)\n", |
192 |
| - " else:\n", |
193 |
| - " return download_and_extract_partial_zip(lpath, dl, zip_glob)" |
194 |
| - ] |
195 |
| - }, |
196 | 123 | {
|
197 | 124 | "cell_type": "code",
|
198 | 125 | "execution_count": 5,
|
|
202 | 129 | },
|
203 | 130 | "outputs": [],
|
204 | 131 | "source": [
|
205 |
| - "pyciam_files = requests.get(\n", |
206 |
| - " Z_URL_SLIIDERS_PC.format(doi=Z_PYCIAM_DOI), params=PARAMS\n", |
207 |
| - ").json()[\"files\"]" |
| 132 | + "pyciam_files = get_zenodo_file_list(Z_PYCIAM_DOI)" |
208 | 133 | ]
|
209 | 134 | },
|
210 | 135 | {
|
|
628 | 553 | "name": "python",
|
629 | 554 | "nbconvert_exporter": "python",
|
630 | 555 | "pygments_lexer": "ipython3",
|
631 |
| - "version": "3.10.8" |
| 556 | + "version": "3.12.2" |
632 | 557 | },
|
633 | 558 | "widgets": {
|
634 | 559 | "application/vnd.jupyter.widget-state+json": {
|
|
0 commit comments