Skip to content

Commit

Permalink
Merge pull request #457 from bbuzz31/support_datelist
Browse files Browse the repository at this point in the history
Support datelist
  • Loading branch information
bbuzz31 authored Dec 22, 2022
2 parents a2c6cb6 + 8df8449 commit 053a62f
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [PEP 440](https://www.python.org/dev/peps/pep-0440/)
and uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

Corrects bug in weather filename that resulted in incorrect delay calculation
Implements a quick test to check whether a weather model is downloaded for each date in a list
Write the diagnostic weather model files to the 'output_directory' rather than PWD

## [0.3.1]
Fixes some missing imports and typing statements

Expand Down
86 changes: 86 additions & 0 deletions test/test_datelist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import os
import glob
import pytest
import subprocess
import shutil
import yaml
import numpy as np
from test import TEST_DIR


def test_datelist():
SCENARIO_DIR = os.path.join(TEST_DIR, 'datelist')
os.makedirs(SCENARIO_DIR, exist_ok=True)
dates = ['20200124', '20200130']

dct_group = {
'aoi_group': {'bounding_box': [28, 39, -123, -112]},
'date_group': {'date_list': dates},
'time_group': {'time': '00:00:00'},
'weather_model': 'GMAO',
'runtime_group': {
'output_directory': SCENARIO_DIR,
'weather_model_directory': os.path.join(SCENARIO_DIR, 'weather_files')
}
}

params = dct_group
dst = os.path.join(SCENARIO_DIR, 'temp.yaml')

with open(dst, 'w') as fh:
yaml.dump(params, fh, default_flow_style=False)


## run raider on new file (two dates)
cmd = f'raider.py {dst}'
proc = subprocess.run(cmd.split(), stdout=subprocess.PIPE, universal_newlines=True)
assert np.isclose(proc.returncode, 0)

## check that four files (2x date) were created
n_files = len(glob.glob(os.path.join(SCENARIO_DIR, 'weather_files/*.nc')))
n_dates = len(dates)
assert np.isclose(n_files, n_dates*2), 'Incorrect number of files produced'

## clean up
shutil.rmtree(SCENARIO_DIR)

return dst


def test_datestep():
SCENARIO_DIR = os.path.join(TEST_DIR, 'datelist')
os.makedirs(SCENARIO_DIR, exist_ok=True)
st, en, step = '20200124', '20200130', 3
n_dates = 3

dct_group = {
'aoi_group': {'bounding_box': [28, 39, -123, -112]},
'date_group': {'date_start': st, 'date_end': en, 'date_step': step},
'time_group': {'time': '00:00:00'},
'weather_model': 'GMAO',
'runtime_group': {
'output_directory': SCENARIO_DIR,
'weather_model_directory': os.path.join(SCENARIO_DIR, 'weather_files')
}
}

params = dct_group
dst = os.path.join(SCENARIO_DIR, 'temp.yaml')

with open(dst, 'w') as fh:
yaml.dump(params, fh, default_flow_style=False)


## run raider on new file (two dates)
cmd = f'raider.py {dst}'
proc = subprocess.run(cmd.split(), stdout=subprocess.PIPE, universal_newlines=True)
assert np.isclose(proc.returncode, 0)

## check that four files (2x date) were created
n_files = len(glob.glob(os.path.join(SCENARIO_DIR, 'weather_files/*.nc')))
assert np.isclose(n_files, n_dates*2), 'Incorrect number of files produced'

## clean up
shutil.rmtree(SCENARIO_DIR)

return dst
5 changes: 4 additions & 1 deletion tools/RAiDER/models/plotWeather.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class objects. It is not designed to be used on its own apart
from this class.
"""

import os
from RAiDER.interpolator import RegularGridInterpolator as Interpolator
from mpl_toolkits.axes_grid1 import make_axes_locatable as mal
import numpy as np
Expand Down Expand Up @@ -87,7 +88,9 @@ def plot_pqt(weatherObj, savefig=True, z1=500, z2=15000):
wspace=0.3)

if savefig:
plt.savefig('{}_weather_hgt{}_and_{}m.pdf'.format(weatherObj._Name, z1, z2))
wd = os.path.dirname(os.path.dirname(weatherObj.files[0]))
f = f'{weatherObj._Name}_weather_hgt{z1}_and_{z2}m.pdf'
plt.savefig(os.path.join(wd, f))
return f


Expand Down
1 change: 1 addition & 0 deletions tools/RAiDER/models/weatherModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,7 @@ def filename(self, time=None, outLoc='weather_files'):
)

self.files = [f]
return f

def write(
self,
Expand Down
28 changes: 11 additions & 17 deletions tools/RAiDER/processWM.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

def prepareWeatherModel(
weather_model,
time=None,
time,
wmLoc: str=None,
ll_bounds: List[float]=None,
download_only: bool=False,
Expand All @@ -35,33 +35,27 @@ def prepareWeatherModel(
download_only: bool - False if preprocessing weather model data
makePlots: bool - whether to write debug plots
force_download: bool - True if you want to download even when the weather model exists
Returns:
str: filename of the netcdf file to which the weather model has been written
str: filename of the netcdf file to which the weather model has been written
"""
# Ensure the file output location exists
if wmLoc is None:
wmLoc = os.path.join(os.getcwd(), 'weather_files')
os.makedirs(wmLoc, exist_ok=True)

# check whether weather model files are supplied or should be downloaded
f = weather_model.filename(time, wmLoc)

download_flag = True
if weather_model.files is None:
if time is None:
raise RuntimeError(
'prepareWeatherModel: Either a file or a time must be specified'
)
weather_model.filename(time, wmLoc)
if os.path.exists(weather_model.files[0]):
if not force_download:
logger.warning(
'Weather model already exists, please remove it ("%s") if you want '
'to download a new one.', weather_model.files
)
download_flag = False
else:
if os.path.exists(f) and not force_download:
logger.warning(
'Weather model already exists, please remove it ("%s") if you want '
'to download a new one.', weather_model.files
)
download_flag = False


# if no weather model files supplied, check the standard location
if download_flag:
weather_model.fetch(*weather_model.files, ll_bounds, time)
Expand Down

0 comments on commit 053a62f

Please sign in to comment.