Skip to content

Instantly share code, notes, and snippets.

@pyRobShrk
pyRobShrk / dateAxis.py
Created November 30, 2017 19:30
pyqtgraph dateAxis
# dateAxis.py
# Usage: save dateAxis.py in directory with pyqtgraph py file
# Use sample code below to create plot with date axis
# from dateAxis import *
# daxis = DateAxis(orientation='bottom')
# flo = pg.PlotWidget(axisItems={'bottom': daxis, 'left': yaxis})
import time
import pyqtgraph as pg
import numpy as np
from pyqtgraph.Qt import QtGui, QtCore
@pyRobShrk
pyRobShrk / CE-QUALtopoShadingZenith.py
Last active April 9, 2019 21:45
CE-QUAL-W2 topographic shading
# Calculate 18 zenith angles (radians) for CE-QUAL-W2 Topographic Shading
# Use ArcGIS "Solar Radiation Graphics" Tool, output optional sky map
# Sky Size: 1440, Zenith Divisions: 360 (0.25 deg), Azimuth Divisions: 72 (5 deg)
# Tool returns a viewshed with as many bands as input points, and a sky map which
# has values starting at zero at the north outside edge, and spiraling clockwise inward
# However, the results are looking up at the sky so West/East are reversed
# Each sky map wedge is centered on the main direction
# Sky maps values divisible by 72 with no remainder (modulo or %), are from 357.5 to 2.5 azimuth
# Example: skymap value of 6210, where is it?
# Solution: 6210 % 72 = 18, There are 72 azimuth divisions so 18 is due west
@pyRobShrk
pyRobShrk / CE-QUALprocessing.py
Last active July 1, 2018 20:01
CE-QUAL-W2 bathymetry processing
## This script processes a bathymetric surface model into 2D blocks for CE-QUAL-W2
## "CEQUALRivSegm" is a shapefile with segment numbers, lengths, and reverse-azimuth directions
## "CEQUAL_seg" is a raster from a polygon file with segment numbers and pixels matching "DEM"
## The resulting histogram multiplied by the cell area gives the area-elevation curve for each segment
## The area-elevation curve divided by the segment length gives the average width for CE-QUAL-W2
from scipy import ndimage as ndi
import numpy as np
tabl = arcpy.da.TableToNumPyArray("CEQUALRivSegm",['OBJECTID','Length_m','Bearing'])
@pyRobShrk
pyRobShrk / reverseAzimuth.py
Created November 30, 2017 21:31
ArcMap Field Calculator - Reverse Line Azimuth Angle
# This script is used to calculate the segment angle for CE-QUAL-W2
# My segment streamline was drawn going downstream
# The CE-QUAL input wants radians east of north (CW), pointing upstream
# atan2 results are north of east (CCW), the sign change makes it CW
# adding pi/2 gives the reverse azimuth, subtracting would be azimuth
# In ArcMap Field Calculator using Python Parser:
# Pre-Logic Script Code:
def reverseAzimuth(shp):
a = -math.atan2(shp.firstPoint.Y - shp.lastPoint.Y, shp.firstPoint.X - shp.lastPoint.X) + math.pi/2
if a < 0:
@pyRobShrk
pyRobShrk / trendline.bas
Created November 30, 2017 21:44
Convert Excel Trendline to LINEST
Sub ExtractTrendline()
Dim outRng As Range
Dim useB As String
Dim poly As String
Dim poly2 As String
useB = "TRUE"
poly = "{1"
If TypeName(Selection) = "Trendline" Then
If Not Selection.Type = xlMovingAvg Then
Set outRng = Application.InputBox(Prompt:="Select uppler-left of location where coeffients are to be stored (2-6 cols, 7 rows):", _
@pyRobShrk
pyRobShrk / HEC-YEAH.py
Created November 30, 2017 22:02
HEC-DSSVue Jython script to import time series from HEC-RAS water quality output file
# name=HEC-YEAH
# description=HEC-YEAH
# description=Aimee Kindel and Rob Sherrick, HDR, Inc.
# description=This script unlocks the cryptic file storage of the HEC-RAS Water Quality Module output (*.wq##)
# description=On line 101: "range(1,ct+1,4):" says to output only every fourth WQ cell (for speed).
# description=On line 105: You may want to change "FPART" to something more descriptive.
# displayinmenu=false
# displaytouser=true
# displayinselector=false
from hec.script import *
@pyRobShrk
pyRobShrk / interp2d.bas
Created November 30, 2017 23:58
Excel UDF for bilinear interpolation
Function Int2d(rowLookup As Double, colLookup As Double, lookupRange As Range) As Double
'This function performs 2 dimensional linear interpolation using built-in linear functions
'Lookup range includes column and row lookup values
Dim Row, col As Integer
Dim RowVals, ColVals, lookup As Variant
Dim sl1, sl2, sl3, int1, int2, int3, col1, col2 As Double
Row = 1
col = Row
On Error Resume Next
Row = WorksheetFunction.Match(rowLookup, lookupRange.Offset(1, 0).Columns(1), 1)
@pyRobShrk
pyRobShrk / USGS10mElev.py
Last active July 1, 2019 21:27
Python function to query USGS 10 meter 3DEP elevation for a given point (lat, long)
from http import client
def USGS10mElev(lat,lon):
usgs = client.HTTPConnection('nationalmap.gov')
usgs.request('GET','/epqs/pqs.php?x=%.6f&y=%.6f&units=FEET&output=xml'% (lon, lat))
result = usgs.getresponse()
if result.status == 200:
xml = result.read()
return float(xml[xml.find(b'<Elevation>')+11:xml.find(b'</Elevation>')-1])
else: return request
@pyRobShrk
pyRobShrk / RivBathyInterp.py
Created December 4, 2017 21:00
Anisotropic river bathymetry interpolation
# This script interpolates river bathymetry in an anisotropic way.
# Following the stream centerline, the interpolation is weighted longitudinally.
# This is accomplished by projecting the sinuous river to a straight line (s, n) coordinates,
# where s is distance downstream and n is distance left/right perpindicular to centerline.
# In (s, n) coordinates, the river is then compressed longitudinally, and then interpolated normally.
# The compression gives interpolation weight to upstream/downstream points instead of going up the bank.
# For best results the centerline should be smoothed, otherwise the inside bend has overlapping points.
# Input Excel file has three sheets, with simple X, Y, Z columns. X, Y only for centerline.
import pandas as pd
@pyRobShrk
pyRobShrk / multipleLinearAnalysis.bas
Created December 6, 2017 18:19
PRESS Statistic, Hat Matrix, and Leverage Points for Multiple Linear Regressions in Excel
' Multiple Linear Regressions in Excel
' This group of functions uses the Hat Matrix and
' Leverages to calculate the PRESS statistic
' A column of ones must precede your X columns (exR)
' https://en.wikipedia.org/wiki/PRESS_statistic
' https://en.wikipedia.org/wiki/Projection_matrix
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function Hat(exR As Range) As Variant
'Returns a symmetrical array of dimensions n by n