Skip to content

Commit

Permalink
refactor for setLevelType at weatherModel.py
Browse files Browse the repository at this point in the history
  • Loading branch information
bbuzz31 committed Jul 26, 2023
1 parent ab2f478 commit 242f86a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 46 deletions.
13 changes: 0 additions & 13 deletions tools/RAiDER/models/ecmwf.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,6 @@ def __init__(self):
self._model_level_type = 'ml' # Default


def setLevelType(self, levelType):
'''Set the level type to model levels or pressure levels'''
if levelType in ['ml', 'pl']:
self._model_level_type = levelType
else:
raise RuntimeError('Level type {} is not recognized'.format(levelType))

if levelType == 'ml':
self.__model_levels__()
else:
self.__pressure_levels__()


def __pressure_levels__(self):
self._zlevels = np.flipud(LEVELS_25_HEIGHTS)
self._levels = len(self._zlevels)
Expand Down
45 changes: 12 additions & 33 deletions tools/RAiDER/models/hrrr.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ def load_weather_hrrr(filename):
return _xs, _ys, lons, lats, qs, temps, pres, geo_hgt, proj



class HRRR(WeatherModel):
def __init__(self):
# initialize a weather model
Expand Down Expand Up @@ -236,27 +235,18 @@ def __init__(self):
f'+lon_0={lon0} +x_0={x0} +y_0={y0} +a={earth_radius} '\
f'+b={earth_radius} +units=m +no_defs')
self._valid_bounds = HRRR_CONUS_COVERAGE_POLYGON
self.setLevelType('ml')


def setLevelType(self, levelType):
'''Set the level type to model levels or pressure levels'''
if levelType in ['ml', 'pl']:
self._model_level_type = levelType
else:
raise RuntimeError(f'Level type {levelType} is not recognized')

if levelType == 'ml':
self.__model_levels__()
else:
raise NotImplementedError('Pressure levels do not go high enough for HRRR.')
self.setLevelType('nat')


def __model_levels__(self):
self._levels = 50
self._zlevels = np.flipud(LEVELS_50_HEIGHTS)


def __pressure_levels__(self):
raise NotImplementedError('Pressure levels do not go high enough for HRRR.')


def _fetch(self, out):
'''
Fetch weather model data from HRRR
Expand All @@ -271,8 +261,7 @@ def _fetch(self, out):
bounds = self._ll_bounds.copy()
bounds[2:] = np.mod(bounds[2:], 360)

hlt = 'prs' if self._model_level_type == 'pl' else 'nat'
download_hrrr_file(bounds, corrected_DT, out, model='hrrr', product=hlt)
download_hrrr_file(bounds, corrected_DT, out, 'hrrr', self._model_level_type)


def load_weather(self, f=None, *args, **kwargs):
Expand Down Expand Up @@ -367,27 +356,18 @@ def __init__(self):
# The projection information gets read directly from the weather model file but we
# keep this here for object instantiation.
self._proj = HRRR_AK_PROJ
self.setLevelType('ml')


def setLevelType(self, levelType):
'''Set the level type to model levels or pressure levels'''
if levelType in ['ml', 'pl']:
self._model_level_type = levelType
else:
raise RuntimeError(f'Level type {levelType} is not recognized')

if levelType == 'ml':
self.__model_levels__()
else:
raise NotImplementedError('Pressure levels do not go high enough for HRRR.')
self.setLevelType('nat')


def __model_levels__(self):
self._levels = 50
self._zlevels = np.flipud(LEVELS_50_HEIGHTS)


def __pressure_levels__(self):
raise NotImplementedError('Pressure levels do not go high enough for HRRR.')


def _fetch(self, out):
bounds = self._ll_bounds.copy()
bounds[2:] = np.mod(bounds[2:], 360)
Expand All @@ -396,8 +376,7 @@ def _fetch(self, out):
if not corrected_DT == self._time:
logger.info('Rounded given datetime from {} to {}'.format(self._time, corrected_DT))

hlt = 'prs' if self._model_level_type == 'pl' else 'nat'
download_hrrr_file(bounds, corrected_DT, out, model='hrrrak', product=hlt)
download_hrrr_file(bounds, corrected_DT, out, 'hrrrak', self._model_level_type)


def load_weather(self, f=None, *args, **kwargs):
Expand Down
13 changes: 13 additions & 0 deletions tools/RAiDER/models/weatherModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,19 @@ def checkTime(self, time):
raise RuntimeError(msg)


def setLevelType(self, levelType):
'''Set the level type to model levels or pressure levels'''
if levelType in 'ml pl nat prs'.split():
self._model_level_type = levelType
else:
raise RuntimeError(f'Level type {levelType} is not recognized')

if levelType in 'ml nat'.split():
self.__model_levels__()
else:
self.__pressure_levels__()


def _convertmb2Pa(self, pres):
'''
Convert pressure in millibars to Pascals
Expand Down

0 comments on commit 242f86a

Please sign in to comment.