Skip to content

Commit

Permalink
Merge branch 'copernicus' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
fcseidl committed Jan 5, 2024
2 parents cc59440 + 465bbbf commit 0ac96d5
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 2 deletions.
2 changes: 1 addition & 1 deletion GMW/gmw.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def get_mangrove_locations_from_tiles(gmwdir: str, tilenames: list[str]) -> np.n
return np.vstack([latitude, longitude]).T


# TODO: get it working approximately by returning Polygons, then switch to boxes
# TODO: actually return BoundingBox rather than Polygon
def get_tiles(tilenames: list[str]) -> list[BoundingBox]:
"""Return a list of Bounding Boxes representing the 1x1 degree tiles containing mangroves."""
return [Polygon(get_tile_corners(tn)) for tn in tilenames]
Expand Down
4 changes: 4 additions & 0 deletions Spherical/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,7 @@ def anglelatlon(p0: tuple | np.ndarray, p1: tuple | np.ndarray) -> float | np.nd
h2 = sind((lat1 - lat0) / 2.) ** 2 + cosd(lat1) * cosd(lat0) * sind((lon1 - lon0) / 2.) ** 2
return 2 * arcsind(np.sqrt(h2))


def addtolon(lon, delta):
"""Add a delta to a longitude, wrapping appropriately around [-180, 180)."""
return (lon + delta + 180) % 360 - 180
91 changes: 91 additions & 0 deletions era5tiles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
"""
This script downloads ERA5 measurements of the 10m instantaneous wind
speed and total precipitation from each 1x1 degree tile in the GMW 2020
dataset.
"""
import os.path

import cdsapi
from tqdm import tqdm
from concurrent import futures

from GMW import gmw


# path to GMW archive and ERA5 target folder
gmwdir = "/pl/active/earthlab/bioextremes/gmw_v3_2020/"; era5dir = "/pl/active/earthlab/bioextremes/era5/"; nproc = os.cpu_count()
#gmwdir = "/Users/fcseidl/Downloads/gmw_v3_2020/"; era5dir = "/Users/fcseidl/EarthLab-local/era5/"; nproc = 2


# request in netcdf format at 6h resolution
shared_params = {
'product_type': 'reanalysis',
'format': 'netcdf',
'variable': ['instantaneous_10m_wind_gust', 'total_precipitation'],
'day': [
'01', '02', '03',
'04', '05', '06',
'07', '08', '09',
'10', '11', '12',
'13', '14', '15',
'16', '17', '18',
'19', '20', '21',
'22', '23', '24',
'25', '26', '27'
'28', '29', '30',
'31',
],
'time': [
'00:00', '06:00', '12:00', '18:00'
],
'month': [
'01', '02', '03'
'04', '05', '06',
'07', '08', '09',
'10', '11', '12',
]
}

_70s = ['1978', '1979']
_80s = ['1980', '1981', '1982', '1983', '1984',
'1985', '1986', '1987', '1988', '1989']
_90s = ['1990', '1991', '1992', '1993', '1994',
'1995', '1996', '1997', '1998', '1999']
_00s = ['2000', '2001', '2002', '2003', '2004',
'2005', '2006', '2007', '2008', '2009']
_10s = ['2010', '2011', '2012', '2013', '2014',
'2015', '2016', '2017', '2018', '2019']
_20s = ['2020', '2021', '2022', '2023']

# get name of each tile (these will tell us the coordinates)
names = gmw.get_tile_names(gmwdir)

# Copernicus client
client = cdsapi.Client(quiet=True)


# method to get data from each tile
def download_tile(name):
corners = gmw.get_tile_corners(name)
north = int(corners[0, 0])
east = int(corners[1, 2])
south = int(corners[0, 1])
west = int(corners[1, 0])
params = {'area': [north, west, south, east]}
params.update(shared_params)
for years, daterange in zip(
[_70s, _80s, _90s, _00s, _10s, _20s],
['_1978_1979', '_1980_1989', '_1990_1999',
'_2000_2009', '_2010_2019', '_2020_2023']
):
params.update({'year': years})
target = era5dir + 'ERA5_' + name[4:-12] + daterange + '.netcdf'
# os.path.exists would be fine here, but futures worries about its thread safety
if not os.path.isfile(target):
client.retrieve(
'reanalysis-era5-single-levels', params, target)


# download them all (this will be time-consuming)
with futures.ThreadPoolExecutor(nproc) as executor:
list(tqdm(executor.map(download_tile, names), total=len(names)))
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ h5py~=3.7.0
pandas~=2.0.3
matplotlib~=3.7.2
scikit-learn~=1.2.2
rasterio~=1.2.10
rasterio~=1.2.10
xarray~=2023.6.0

0 comments on commit 0ac96d5

Please sign in to comment.