Skip to content

Instantly share code, notes, and snippets.

@pyRobShrk
pyRobShrk / Text
Created November 29, 2022 22:45 — forked from jack-williams/Text
Excel text functions
_search = LAMBDA(txt, searchtext, [case_sensitive],
// Test the inputs for errors so that we can distinguish
// the error that comes from FIND/SEARCH as meaning "not-found".
IFS(
ISERROR(txt),
txt,
ISERROR(searchtext),
searchtext,
ISERROR(case_sensitive),
case_sensitive,
@pyRobShrk
pyRobShrk / Excel_LAMBDAs.txt
Last active June 15, 2023 09:36
A random assortment of LAMBDA functions for Excel
/*
Name: Show Moon Phase Emoji (MOONPHASE)
Description: Returns a lunar phase character closest matching to any Excel Date/Time value.
If you calculate for daily values at midnight, the lunar cycle will be the same for every 3 or 4 days (3.691 days).
🌑🌒🌓🌔🌕🌖🌗🌘
*/
MOONPHASE = LAMBDA(datetime,LET(
phase,MOD(ROUND(MOD(datetime,29.5275)/3.691,0)-2,8)+1,
UNICHAR(127760+phase)));
@pyRobShrk
pyRobShrk / wordleList.txt
Created February 3, 2022 18:16
This is a list of five letter words appearing in the wordle game (https://www.powerlanguage.co.uk/wordle/). The list has been alphabetized.
aalii
aargh
aarti
abaca
abaci
aback
abacs
abaft
abaka
abamp
@pyRobShrk
pyRobShrk / MuskyRouting.bas
Last active November 4, 2021 16:13
Muskingum hydrologic routing in Microsoft Excel (UDF)
Function Muskingum(timeSeries As Range, _
K As Double, X As Double, _
Optional Reaches As Integer = 1) As Variant
'Muskingum routing in Excel
'Returns a column array the same length as timeSeries (use array formula or spill)
'K (travel time) is in same units as the time step of the input timeSeries
'X must be between 0 and 0.5
'Reaches will be automatically adjusted to avoid negative coefficients if K is < 1 / (2*(1-X)) or K > 1/2X
Dim Coeffs(1 To 5) As Double
@pyRobShrk
pyRobShrk / interpolate.bas
Created October 21, 2021 15:38
Linear Interpolation function for Excel VBA
Function interp(X As Double, xRange As Range, yRange As Range) As Double
ascending = xRange.Cells(1) < xRange.Cells(2)
With WorksheetFunction
If ascending Then i = .Match(X, xRange) Else i = .Match(X, xRange, -1)
Set x1x2 = Range(xRange.Cells(i), xRange.Cells(i + 1))
Set y1y2 = Range(yRange.Cells(i), yRange.Cells(i + 1))
interp = X * .Slope(y1y2, x1x2) + .Intercept(y1y2, x1x2)
End With
@pyRobShrk
pyRobShrk / removeDupeVertices.py
Last active October 12, 2018 20:13
Quick function (arcpy) to remove redundant vertices from polyline layer
# Remove duplicate vertices from lines
# A point is considered duplicate if it has the same XY coordinates within a tolerance (precision)
# The precision is an integer number of decimal places
# for X,Y in lat, long, 5 decimals is 1.1 meters and 6 is 11 cm
def removeDupeVertices(feature, precision):
fields = ['SHAPE@'] + [f.name for f in arcpy.ListFields(feature)]
sr = arcpy.Describe(feature).SpatialReference
upd = arcpy.da.UpdateCursor(feature,fields)
orphanList = []
for row in upd:
@pyRobShrk
pyRobShrk / encodeLongLatCoords.py
Last active January 5, 2022 17:57
Convert lat/long coordinates into an ascii string. Useful for database storage outside of geodata formats.
# Objective: A system capable of encoding geographic points, and polylines in ASCII characters
# Obviously Google and geohash have similar algorithms, but with limited precision
#
# A 24-bit integer can store binary integer values up to 16,777,215
# Using PEM format, 24 bits can be represented as 4 base-64 characters with the alphabet:
# ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
# A polyline can be represented by a string of characters,
# where each 8 characters is a longitude/latitude location
# For maximum flexibility, a polyline or point string is prefixed with three characters
# The first two characters specifies the origin - the lower left corner of a MGRS GZD
@pyRobShrk
pyRobShrk / GIStable2Clipboard.py
Created August 6, 2018 21:19
ArcPy function to copy the attribute table to clipboard for pasting into Excel
import pandas as pd
def GIStable2Clipboard(inTable, exportFields="all"):
desc = arcpy.Describe(inTable)
if exportFields =="all":
exportFields = [f.name for f in arcpy.ListFields(inTable) if not f.name == desc.shapeFieldName]
aliases = [f.aliasName for f in arcpy.ListFields(inTable) if not f.name == desc.shapeFieldName]
pd.DataFrame.from_records(arcpy.da.TableToNumPyArray(inTable,exportFields),
index=desc.OIDFieldName, columns=aliases).to_clipboard()
@pyRobShrk
pyRobShrk / geoid.py
Last active November 14, 2022 22:40
Python classes to calculate geoid height in meters NAVD 88 (Geoid 12b) and EGM 96
import numpy as np
from scipy.interpolate import RectBivariateSpline as Spline
import pygeodesy as geo
from pygeodesy.ellipsoidalVincenty import LatLon
class Geoid12B(): #NAD 83 Ellipsoid
# https://www.ngs.noaa.gov/GEOID/GEOID12B/GEOID12B_CONUS.shtml
# Download a Geoid Grid in little endian binary format ('g2012bu5.bin')
def __init__(self, leBinFile):
glamn, glomn, dla, dlo = np.fromfile(leBinFile,'<f8',4)
@pyRobShrk
pyRobShrk / CalcAreaLength.bas
Created May 4, 2018 15:54
This module adds text to a free-form Excel line or polygon, which shows the length or area of that drawing in inches
Sub CalcLength()
'Subroutine calculates the distance of straight line or "scribble" line
'It has not been tested in any other Office software, but it should work with minor modification
'By Rob Sherrick, 4/12/2018
Dim dpi As Integer
dpi = Application.InchesToPoints(1)
Length = 0
A = 1
On Error Resume Next