From 2de0954bd3a9d9716c3c007c488c291f6dd07da8 Mon Sep 17 00:00:00 2001 From: veenstrajelmer <60435591+veenstrajelmer@users.noreply.github.com> Date: Wed, 5 Feb 2025 00:18:57 +0100 Subject: [PATCH] added uhslc as backup for gshhs zipfile (#1112) * added uhslc as backup for gshhs zipfile * updated whatsnew --- dfm_tools/data.py | 29 ++++++++++++++++++++--------- docs/whats-new.md | 2 ++ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/dfm_tools/data.py b/dfm_tools/data.py index 1a3a773f2..11f2b0045 100644 --- a/dfm_tools/data.py +++ b/dfm_tools/data.py @@ -229,22 +229,33 @@ def gshhs_coastlines_shp() -> str: dir_testdata = get_dir_testdata() - fname = 'gshhg-shp-2.3.7.zip' - filepath_zip = os.path.join(dir_testdata,fname) - dir_gshhs = os.path.join(dir_testdata,'gshhg-shp-2.3.7') + fname = 'gshhg-shp-2.3.7' + filepath_zip = os.path.join(dir_testdata, f"{fname}.zip") + dir_gshhs = os.path.join(dir_testdata, fname) + + def download_gshhs(url): + url_base = url.split("/")[2] + fname_zip = url.split('/')[-1] + print(f'downloading "{fname_zip}" from {url_base} to cachedir') + # use allow_redirects=False to enforce this url + resp = requests.get(url, allow_redirects=False) + resp.raise_for_status() #raise HTTPError if url not exists + return resp #download zipfile if not present if not os.path.exists(filepath_zip) and not os.path.exists(dir_gshhs): - file_url = f'https://www.ngdc.noaa.gov/mgg/shorelines/data/gshhg/latest/{fname}' - print(f'downloading "{fname}" from www.ngdc.noaa.gov to cachedir') - r = requests.get(file_url, allow_redirects=True) - r.raise_for_status() #raise HTTPError if url not exists + file_url = f'https://www.ngdc.noaa.gov/mgg/shorelines/data/gshhg/latest/{fname}.zip' + resp = download_gshhs(file_url) + if resp.is_redirect: + print("failed (redirected), trying different source") + file_url = f'https://www.soest.hawaii.edu/pwessel/gshhg/{fname}.zip' + resp = download_gshhs(file_url) with open(filepath_zip, 'wb') as f: - f.write(r.content) + f.write(resp.content) #unzip zipfile if unzipped folder not present if not os.path.exists(dir_gshhs): - print(f'unzipping "{fname}"') + print(f'unzipping "{fname}.zip"') with zipfile.ZipFile(filepath_zip, 'r') as zip_ref: zip_ref.extractall(dir_gshhs) diff --git a/docs/whats-new.md b/docs/whats-new.md index 3289246ea..1d76ddaa4 100644 --- a/docs/whats-new.md +++ b/docs/whats-new.md @@ -5,6 +5,8 @@ ### Feat - usage of outside time buffer in `dfmt.cmems_nc_to_ini()` so noon-centered or monthly timestamps are also supported in [#1087](https://github.com/Deltares/dfm_tools/pull/1087) - correct CMEMS daily mean data ("P1D-m") from midnight to noon by adding a 12-hour offset in `dfmt.download_CMEMS()` in [#1088](https://github.com/Deltares/dfm_tools/pull/1088) +- updated cdsapi request to new format in [#1103](https://github.com/Deltares/dfm_tools/pull/1103) +- added UHSLC backup for GSHHS data in [#1112](https://github.com/Deltares/dfm_tools/pull/1112) ### Fix - made p-drive paths for tide models and gesla3 work on linux also in [#1083](https://github.com/Deltares/dfm_tools/pull/1083) and [#1085](https://github.com/Deltares/dfm_tools/pull/1085)