Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added argparsing and cleaned up strings concatenation #74

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,26 @@
from pathlib import Path
from shutil import copyfile
from datetime import datetime
import argparse

'''
Downloads 1 year of ERA5 data as monthly chunks.
Usage: python download_ERA5_pressureLevel_annual.py <year> <coordinates> <path/to/save/data>
'''

# Get the year we're downloading from command line argument
year = int(sys.argv[1]) # arguments are string by default; string to integer
parser = argparse.ArgumentParser(description='Downloads 1 year of ERA5 data as monthly chunks')
parser.add_argument('--year', type = int, required=True)
parser.add_argument('--bounding-box', '-bb', type = str, help='Input north/east/south/west coordinates', required=True)
parser.add_argument('--forcing-path', '-fp', type = str, required=True)

args = parser.parse_args()

# Get the spatial coordinates as the second command line argument
bounding_box = sys.argv[2] # string
bounding_box = bounding_box.split('/') # split string
bounding_box = args.bounding_box.split('/') # split string
bounding_box = [float(value) for value in bounding_box] # string to array

# Get the path as the second command line argument
forcingPath = Path(sys.argv[3]) # string to Path()
forcingPath = Path(args.forcing_path) # string to Path()

# --- Convert the bounding box to download coordinates
# function to round coordinates of a bounding box to ERA5s 0.25 degree resolution
Expand Down Expand Up @@ -61,17 +65,18 @@ def round_coords_to_ERA5(coords):
for month in range (1,13): # this loops through numbers 1 to 12

# find the number of days in this month
daysInMonth = calendar.monthrange(year,month)
daysInMonth = calendar.monthrange(args.year,month)

# compile the date string in the required format. Append 0's to the month number if needed (zfill(2))
date = str(year) + '-' + str(month).zfill(2) + '-01/to/' + \
str(year) + '-' + str(month).zfill(2) + '-' + str(daysInMonth[1]).zfill(2)

date = "{year}-{month}-01/to/{year}-{month}-{days}".format(year=args.year,
month=str(month).zfill(2),
days=str(daysInMonth[1]).zfill(2))

# compile the file name string
file = forcingPath / ('ERA5_pressureLevel137_' + str(year) + str(month).zfill(2) + '.nc')
file = forcingPath / ('ERA5_pressureLevel137_' + str(args.year) + str(month).zfill(2) + '.nc')

# track progress
print('Trying to download ' + date + ' into ' + str(file))
print(f'Trying to download {date} into {file}')

# if file doesn't yet exist, download the data
if not os.path.isfile(file):
Expand Down Expand Up @@ -102,10 +107,10 @@ def round_coords_to_ERA5(coords):
}, file)

# track progress
print('Successfully downloaded ' + str(file))
print(f'Successfully downloaded {file}')

except Exception as e:
print('Error downloading ' + str(file) + ' on try ' + str(retries_cur))
print(f'Error downloading {file} on try {retries_cur}')
print(str(e))
retries_cur += 1
continue
Expand Down