Skip to content

Commit

Permalink
Updates to downloading to support specification of AOI location #20
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-laptop-vm committed Feb 8, 2023
1 parent 8d9b3a1 commit 4767616
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
57 changes: 44 additions & 13 deletions pixutils/era_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ class Var(Enum):
def download_era5_reanalysis_data(variables: Union[Var, List[Var], List[str]],
dates: Union[date, List[date]],
times: Union[time, List[time]],
area: str,
file_path: str) -> None:
"""
Download data from the the Copernicus Climate Data Store
:param variables: a list of fields to be downloaded on the specified dates and times
:param dates: a single date or list of dates to be included in the file
:param times: a single time or list of times to be included in the file
:param area: an area of interest to be included in the file
:param file_path: a path to the output file containing all of the downloaded data
:return: a Boolean value; true, if the download completed successfully
"""
Expand Down Expand Up @@ -105,19 +107,43 @@ def parse_variables() -> List[str]:
variables = parse_variables()
file_format = ext_to_file_type[extension]

# Extract AOI box values
vals = area[0].replace('[','').replace(']','').split(',')
area_box = False
for val in vals:
if float(val) != 0:
area_box = True

# Run C3S API
c = cdsapi.Client()
c.retrieve(
'reanalysis-era5-single-levels',
{
'product_type': 'reanalysis',
'variable': variables,
'year': years,
'month': months,
'day': days,
'time': times,
'format': file_format,
},
file_path)
if area_box:
c.retrieve(
'reanalysis-era5-single-levels',
{
'product_type': 'reanalysis',
'variable': variables,
'year': years,
'month': months,
'day': days,
'time': times,
'area': [vals[0], vals[1], vals[2], vals[3]],
'format': file_format,
},
file_path)
else:
c.retrieve(
'reanalysis-era5-single-levels',
{
'product_type': 'reanalysis',
'variable': variables,
'year': years,
'month': months,
'day': days,
'time': times,
'format': file_format,
},
file_path)


if not os.path.isfile(file_path):
raise RuntimeError("Unable to locate output file '{}'.".format(file_path))
Expand All @@ -130,6 +156,7 @@ def main() -> int:
parser.add_argument("variables", nargs="+", help="Specify one or more variables to be downloaded."
" Supported variables: [{}]"
.format(", ".join(map_var_names.values())))
parser.add_argument("-a", "--area", nargs="+", help="Area to be downloaded (format: [x, x, x, x])")
parser.add_argument("-d", "--dates", nargs="+", help="Date of the data set to be downloaded (format: YYYY-MM-DD)")
parser.add_argument("-t", "--times", nargs="+", help="Time of the data set to be downloaded (format: HH:MM)")
parser.add_argument("-o", "--out_file", nargs=1, help="Filename for the downloaded data.")
Expand All @@ -151,8 +178,12 @@ def main() -> int:
print("Missing 'times' argument.")
return 1

# Optional area
if not args.area:
args.area = [0, 0, 0, 0]

try:
download_era5_reanalysis_data(dates=dates, times=times, variables=args.variables, file_path=file_path)
download_era5_reanalysis_data(dates=dates, times=times, variables=args.variables, area=args.area, file_path=file_path)

return 0
except ValueError as ex:
Expand Down
7 changes: 3 additions & 4 deletions pixutils/raster_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
from rsgislib.imagecalc import BandDefn
from rsgislib import imageutils
from rsgislib import imagecalc
from rsgislib.imageutils import maskImage, genValidMask
from rsgislib import TYPE_32FLOAT
import gdal
from osgeo import gdal


ValueRange = namedtuple("ValueRange", ["min", "max"])
Expand Down Expand Up @@ -99,12 +98,12 @@ def apply_mask(input_file_path: str,
mask_file_path = Path(input_file_path).with_suffix(".mask.tif")

# create the mask file and check it exists
genValidMask(input_file_path, str(mask_file_path), data_format.format, data_min)
imageutils.genValidMask(input_file_path, str(mask_file_path), data_format.format, data_min)
if not os.path.isfile(mask_file_path):
raise FileNotFoundError("Unable to find generated mask file: '{}'.".format(mask_file_path))

# apply the mask file
maskImage(input_file_path,
imageutils.maskImage(input_file_path,
str(mask_file_path),
output_file_path,
data_format.format,
Expand Down

0 comments on commit 4767616

Please sign in to comment.