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

197 image data #198

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
7 changes: 5 additions & 2 deletions smrf/distribute/image_data.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from copy import deepcopy

import numpy as np

Expand Down Expand Up @@ -223,8 +224,10 @@ def _distribute(self, data, other_attribute=None, zeros=None):
"""

# get the data for the desired stations
# this will also order it correctly how air_temp was initialized
data = data[self.stations]
# this will also order it correctly how image_data was initialized and
# fill any stations not in data as NaN
data = deepcopy(data)
data = data.reindex(self.stations)

if np.sum(data.isnull()) == data.shape[0]:
raise Exception("{}: All data values are NaN"
Expand Down
2 changes: 1 addition & 1 deletion smrf/distribute/precipitation.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def distribute(self, data, dpt, precip_temp, ta, time, wind, temp,
"""

self._logger.debug('%s Distributing all precip' % data.name)
data = data[self.stations]
data = data.reindex(self.stations)

if self.config['distribution'] != 'grid':
if self.nasde_model == 'marks2017':
Expand Down
29 changes: 20 additions & 9 deletions smrf/spatial/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ def __init__(self, config, mx, my, GridX, GridY,
else:
self.mask = np.ones_like(self.mx, dtype=bool)

self.metadata['mask'] = self.mask

def detrendedInterpolation(self, data, flag=0, grid_method='linear'):
"""
Interpolate using a detrended approach
Expand All @@ -102,7 +104,10 @@ def detrendedInterpolation(self, data, flag=0, grid_method='linear'):
grid_method: scipy.interpolate.griddata interpolation method
"""

# get the trend, ensure it's positive
# remove nan values from the series
data = data.dropna()
if len(data) == 0:
raise Exception('All gridded values were NaN')

if self.config['grid_local']:
rtrend = self.detrendedInterpolationLocal(data, flag, grid_method)
Expand All @@ -123,7 +128,9 @@ def detrendedInterpolationLocal(self, data, flag=0, grid_method='linear'):

# take the new full_df and fill a data column
df = self.full_df.copy()
df['data'] = data[df['cell_local']].values
df['data'] = data.reindex(df['cell_local']).values
df.dropna(how='any', axis=0, inplace=True)

df = df.set_index('cell_id')
df['fit'] = df.groupby('cell_id').apply(
lambda x: np.polyfit(x.elevation, x.data, 1))
Expand Down Expand Up @@ -186,7 +193,9 @@ def detrendedInterpolationMask(self, data, flag=0, grid_method='linear'):
"""

# get the trend, ensure it's positive
pv = np.polyfit(self.mz[self.mask].astype(float), data[self.mask], 1)
data_mask = data[self.metadata['mask']]
elevation_mask = self.metadata.loc[data_mask.index, 'elevation']
pv = np.polyfit(elevation_mask, data_mask, 1)

# apply trend constraints
if flag == 1 and pv[0] < 0:
Expand All @@ -197,14 +206,16 @@ def detrendedInterpolationMask(self, data, flag=0, grid_method='linear'):
self.pv = pv

# detrend the data
el_trend = self.mz * pv[0] + pv[1]
dtrend = data - el_trend
el_trend = elevation_mask * pv[0] + pv[1]
dtrend = data_mask - el_trend

# interpolate over the DEM grid
idtrend = griddata((self.mx, self.my),
dtrend,
(self.GridX, self.GridY),
method=grid_method)
idtrend = griddata(
(self.metadata.loc[data_mask.index, 'utm_x'],
self.metadata.loc[data_mask.index, 'utm_y']),
dtrend,
(self.GridX, self.GridY),
method=grid_method)

# retrend the data
rtrend = idtrend + pv[0]*self.GridZ + pv[1]
Expand Down
187 changes: 187 additions & 0 deletions smrf/tests/basins/Lakes/config_issue_197.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
################################################################################

# For details on configuration file syntax see:
# https://docs.python.org/2/library/configparser.html
#
# For more SMRF related help see:
# http://smrf.readthedocs.io/en/latest/


################################################################################
# Files for DEM and vegetation
################################################################################

[topo]
filename: ./topo/topo.nc
gradient_method: gradient_d8
sky_view_factor_angles: 72


################################################################################
# Dates to run model
################################################################################

[time]
time_step: 60
start_date: 2018-12-04 12:00:00
end_date: 2018-12-04 14:00:00
time_zone: UTC


################################################################################
# Gridded dataset i.e. wrf_out
################################################################################

[gridded]
data_type: hrrr_grib
hrrr_directory: /data/snowpack/forecasts/hrrr_crop/lakes
hrrr_load_method: timestep


################################################################################
# Air temperature distribution
################################################################################

[air_temp]
distribution: grid
grid_method: cubic
detrend: True
detrend_slope: -1
grid_mask: False
grid_local: True
min: -73.0
max: 47.0

################################################################################
# Vapor pressure distribution
################################################################################

[vapor_pressure]
distribution: grid
grid_method: cubic
detrend: True
detrend_slope: -1
grid_mask: False
grid_local: True
dew_point_tolerance: 0.01
dew_point_nthreads: 8
min: 20.0
max: 5000.0

################################################################################
# Wind speed and wind direction distribution
################################################################################

[wind]
wind_model: wind_ninja
distribution: grid
min: 0.5
max: 30.0
grid_mask: False
wind_ninja_dir: /data/albedo/lakes/wy2019/lakes_test_susong1999/wind_ninja
wind_ninja_dxdy: 50
wind_ninja_pref: topo_windninja_topo
wind_ninja_tz: UTC

################################################################################
# Precipitation distribution
################################################################################

[precip]
distribution: grid
grid_method: cubic
#grid_local: True
detrend: True
detrend_slope: 1
grid_mask: False
station_adjust_for_undercatch: False
storm_mass_threshold: 1.0
storm_days_restart: None
min: 0.0
max: None
new_snow_density_model: susong1999
susong1999_timesteps_to_end_storms: 6
precip_temp_method: wet_bulb


################################################################################
# Albedo distribution
################################################################################

[albedo]
grain_size: 100.0
max_grain: 700.0
dirt: 2.0
max: 1.0
min: 0.0
grid_mask: True
# albedo decay parameters
decay_method: date_method
date_method_start_decay: 2019-03-15
date_method_end_decay: 2019-7-1
date_method_veg_default: 0.2
date_method_decay_power: 0.714


################################################################################
# Cloud Factor - Fraction used to limit solar radiation Cloudy (0) - Sunny (1)
################################################################################
[cloud_factor]
distribution: grid
grid_method: cubic
detrend: False
grid_mask: False


################################################################################
# Solar radiation distribution
################################################################################

[solar]
clear_opt_depth: 100.0
clear_tau: 0.2
clear_omega: 0.85
clear_gamma: 0.3
min: 0.0
max: 800.0


################################################################################
# Thermal radiation distribution
################################################################################

[thermal]
cloud_method: garen2005
marks1979_nthreads: 8
min: 0.0
max: 600.0
grid_mask: True
clear_sky_method: marks1979

################################################################################
# Soil temperature
################################################################################

[soil_temp]
temp: -2.5

################################################################################
# Output variables
################################################################################

[output]
frequency: 1
variables: thermal, air_temp, vapor_pressure, wind_speed, net_solar, precip, percent_snow, snow_density, precip_temp, storm_days, cloud_factor
file_type: netcdf
out_location: ./output

################################################################################
# System variables
################################################################################

[system]
threading: False
queue_max_values: 1
time_out: None
log_level: debug
log_file: ./output/log.txt
16 changes: 16 additions & 0 deletions smrf/tests/data/test_hrrr.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from copy import deepcopy
import os
import unittest

from inicheck.tools import cast_all_variables

from smrf.framework.model_framework import run_smrf
from smrf.tests.smrf_test_case import SMRFTestCase
from smrf.tests.smrf_test_case_lakes import SMRFTestCaseLakes
from smrf.tests.nwrc_check import NWRCCheck


class TestLoadHRRR(SMRFTestCase):
Expand Down Expand Up @@ -95,3 +99,15 @@ def test_load_timestep_threaded(self):
run_smrf(config)

self.compare_hrrr_gold()


@unittest.skipUnless(
NWRCCheck.in_network(),
"Skipping because we are not on the NWRC network"
)
class TestLoadHRRRLakes(SMRFTestCaseLakes):

def test_197_image_data_index(self):

config_file = os.path.join(self.basin_dir, 'config_issue_197.ini')
run_smrf(config_file)
19 changes: 19 additions & 0 deletions smrf/tests/nwrc_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import requests


class NWRCCheck(object):

HOST = '10.200.28.50/dashboard'
URL = 'http://' + HOST

@classmethod
def in_network(cls):
"""
Checks that were on the NWRC network
"""
try:
response = requests.get(cls.URL, timeout=1)
return response.ok

except requests.exceptions.RequestException:
return False