smrf.utils.gradient.
aspect
Calculate the aspect from the finite difference. Aspect is degrees clockwise from North (0/360 degrees)
See below for a referance to how ArcGIS calculates slope http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/How_Aspect_works/00q900000023000000/
dz_dx – finite difference in the x direction
dz_dy – finite difference in the y direction
aspect in degrees
aspect_to_ipw_radians
IPW defines aspect differently than most GIS programs so convert an aspect in degrees from due North (0/360) to the IPW definition.
Aspect is radians from south (aspect 0 is toward the south) with range from -pi to pi, with negative values to the west and positive values to the east
a – aspect in degrees from due North
a: aspect in radians from due South
gradient_d4
Calculate the slope and aspect for provided dem, this will mimic the original IPW gradient method that does a finite difference in the x/y direction
Given a center cell e and it’s neighbors:
The rate of change in the x direction is [dz/dx] = (f - d ) / (2 * dx)
The rate of change in the y direction is [dz/dy] = (h - b ) / (2 * dy)
The slope is calculated as slope_radians = arctan ( sqrt ([dz/dx]^2 + [dz/dy]^2) )
dem – array of elevation values
dx – cell size along the x axis
dy – cell size along the y axis
aspect_rad – turn the aspect from degrees to IPW radians
slope in radians aspect in degrees or IPW radians
gradient_d8
Calculate the slope and aspect for provided dem, using a 3x3 cell around the center
The rate of change in the x direction is [dz/dx] = ((c + 2f + i) - (a + 2d + g) / (8 * dx)
The rate of change in the y direction is [dz/dy] = ((g + 2h + i) - (a + 2b + c)) / (8 * dy)
Input/Output functions Adapted from the UW-Hydro tonic project
smrf.utils.io.
isbool
Test if str is an bolean
isfloat
Test if value is a float
isint
Test if value is an integer
isscalar
Test if a value is a scalar
smrf.utils.pycompat.
iteritems
itervalues
Create classes for running on multiple threads
20160323 Scott Havens
smrf.utils.queue.
DateQueue_Threading
Bases: queue.Queue
queue.Queue
DateQueue extends Queue.Queue module Stores the items in a dictionary with date_time keys When values are retrieved, it will not remove them and will require cleaning at the end to not have to many values
clean
Need to clean it out so mimic the original get
get
Remove and return an item from the queue.
If optional args ‘block’ is true and ‘timeout’ is None (the default), block if necessary until an item is available. If ‘timeout’ is a non-negative number, it blocks at most ‘timeout’ seconds and raises the Empty exception if no item was available within that time. Otherwise (‘block’ is false), return an item if one is immediately available, else raise the Empty exception (‘timeout’ is ignored in that case).
This is from queue.Queue but with modifcation for supplying what to get
index – datetime object representing the date/time being processed
block – boolean determining whether to wait for a variable to become available
timeout – Number of seconds to wait before dropping error, none equates to forever.
put
Put an item into the queue.
If optional args ‘block’ is true and ‘timeout’ is None (the default), block if necessary until a free slot is available. If ‘timeout’ is a non-negative number, it blocks at most ‘timeout’ seconds and raises the Full exception if no free slot was available within that time. Otherwise (‘block’ is false), put an item on the queue if a free slot is immediately available, else raise the Full exception (‘timeout’ is ignored in that case).
QueueCleaner
Bases: threading.Thread
threading.Thread
QueueCleaner that will go through all the queues and check if they all have a date in common. When this occurs, all the threads will have processed that time step and it’s not longer needed
run
Go through the date times and look for when all the queues have that date_time
QueueOutput
Takes values from the queue and outputs using ‘out_func’
Output the desired variables to a file.
20160104 Scott Havens
Collection of utility functions
smrf.utils.utils.
CheckStation
Bases: inicheck.checkers.CheckType
inicheck.checkers.CheckType
Custom check for ensuring our stations are always capitalized
type_func
Attempt to convert all the values to upper case.
value – A single string in a a config entry representing a station name
A single station name all upper case
value
backup_input
Backs up input data files so a user can rerun a run with the exact data used for a run.
data – Pandas dataframe containing the station data
config_obj – The config object produced by inicheck
check_station_colocation
Takes in a data frame representing the metadata for the weather stations as produced by smrf.framework.model_framework.SMRF.loadData and check to see if any stations have the same location.
smrf.framework.model_framework.SMRF.loadData
metadata_csv – CSV containing the metdata for weather stations
metadata – Pandas Dataframe containing the metdata for weather stations
list of station primary_id that are colocated
repeat_sta
find_configs
Searches through a directory and returns all the .ini fulll filenames.
directory – string path to directory.
list of paths pointing to the config file.
configs
getConfigHeader
Generates string for inicheck to add to config files
string for cfg headers
cfg_str
get_asc_stats
Returns header of ascii dem file
get_config_doc_section_hdr
Returns the header dictionary for linking modules in smrf to the documentation generated by inicheck auto doc functions
getgitinfo
gitignored file that contains specific SMRF version and path
git version from ‘git describe’
str
getqotw
grid_interpolate
Broken out gridded interpolation from scipy.interpolate.griddata that takes the vertices and wts from interp_weights function
values – flattened WindNinja wind speeds
vtx – vertices for interpolation
wts – weights for interpolation
shape – shape of SMRF grid
fill_value – value for extrapolated points
interpolated values
ret
grid_interpolate_deconstructed
Underlying methods from scipy grid_data broken out to pass in the tri values returned from qhull.Delaunay. This is done to improve the speed of using grid_data
tri – values returned from qhull.Delaunay
values – values at HRRR stations generally
grid_points – tuple of vectors for X,Y coords of grid stations
method – either linear or cubic
result of interpolation to gridded points
handle_run_script_options
Handle function for dealing with args in the SMRF run script
config_option – string path to a directory or a specific config file.
Full path to an existing config file.
configFile
interp_weights
Find vertices and weights of LINEAR interpolation for gridded interp. This routine follows the methods of scipy.interpolate.griddata as outlined here: https://stackoverflow.com/questions/20915502/speedup-scipy-griddata-for-multiple-interpolations-between-two-irregular-grids This function finds the vertices and weights which is the most computationally expensive part of the routine. The interpolateion can then be done quickly.
xy – n by 2 array of flattened meshgrid x and y coords of WindNinja grid
uv – n by 2 array of flattened meshgrid x and y coords of SMRF grid
d – dimensions of array (i.e. 2 for our purposes)
wts:
vertices
is_leap_year
nan_helper
Helper to handle indices and logical indices of NaNs.
Example
>>> # linear interpolation of NaNs >>> nans, x= nan_helper(y) >>> y[nans]= np.interp(x(nans), x(~nans), y[~nans])
y – 1d numpy array with possible NaNs
nans - logical indices of NaNs index - a function
tuple
set_min_max
Ensure that the data is in the bounds of min and max
data – numpy array of data to be min/maxed
min_val – minimum threshold to trim data
max_val – Maximum threshold to trim data
numpy array of data trimmed at min_val and max_val
data
water_day
Determine the decimal day in the water year
indate – datetime object
dd - decimal day from start of water year wy - Water year
20160105 Scott Havens