Skip to content

Instantly share code, notes, and snippets.

View spestana's full-sized avatar
❄️

Steven Pestana spestana

❄️
View GitHub Profile
@spestana
spestana / hs_resource.py
Last active March 25, 2019 00:58
Framework for creating HydroShare resources.
from pysumma.hydroshare import hydroshare
# Establish connection with hydroshare
hs_up = hydroshare.hydroshare()
# Add metadata
title = 'Title of Resource'
abstract = 'Short description of the resource'
keywords = ('key', 'words', 'list')
resource_type = 'genericresource'
@spestana
spestana / NCtoPNG.bat
Created October 15, 2018 00:27
Quick convert NetCDF (GOES imagery) to png images files using gdal
@echo off
setlocal
set NCdir=%1
for /R %NCdir% %%f in (*.nc) do (
gdal_translate -ot float32 -unscale -CO COMPRESS=deflate NETCDF:"%%f":Rad "%NCdir%%%~nf.tif"
gdal_translate -ot Byte -of png -scale 0 650 0 255 "%NCdir%%%~nf.tif" "%NCdir%%%~nf.png"
gdalwarp -t_srs EPSG:4326 -dstnodata -999.0 "%NCdir%%%~nf.tif" "%NCdir%%%~nf_geo.tif"
@spestana
spestana / CONUStoCA.bat
Created October 15, 2018 00:30
Trim CONUS GOES-16 NetCDF imagery to an area around California. Create tif and png image copies.
@echo off
setlocal
rem input directory with NetCDF files
set NCdir=%1
rem directory for outputs
set outdir=%2
for /R %NCdir% %%f in (*.nc) do (
set "fileFull=%%~nxf"
set "fileNumber=%fileFull:~26,14%"
@spestana
spestana / create_dfs.py
Created October 23, 2018 19:56
Create dataframe from SUMMA output netCDF files for SWE and snow water drainage
def create_dfs(path,file):
'''create df for swe, snow drainage from SUMMA .nc file'''
ds = xr.open_dataset(path+file)
# create swe dataframe
df_swe = ds.scalarSWE.to_dataframe()
df_swe = df_swe.reset_index(level='hru')
df_swe.drop(['hru'], 1, inplace=True)
# create snow drainage dataframe
@spestana
spestana / stop_jnb.sh
Last active April 18, 2019 16:15
template for closing port forwarding connection with remote jupyter notebook
#!/bin/bash
# Find the jupyter notebook running on the server and shut it down:
ssh -l <USERNAME> <SERVER> 'jupyter notebook stop $(jupyter notebook list | grep -E -o 'localhost:[0-9]{4}' | grep -E -o '[0-9]{4}')'
# Find port forwarding process ID to kill on the local machine:
pid=$(ps -ef | grep ssh | grep ? | awk '{print $2}')
kill $pid
@spestana
spestana / start_jnb.sh
Last active April 16, 2019 19:48
template for starting and connecting to a jupyter notebook on a remote server
#!/bin/bash
# Start jupyter notebook on the remote server:
ssh -n -f <USERNAME>@<SERVER> "sh -c 'nohup jupyter notebook --port=8888 > /dev/null 2>&1 &'"
# Find the URL with port number and token:
url=$(ssh -l <USERNAME> <SERVER> 'grep -E -oh 'http:\/\/localhost:[0-9]{4}\/tree.token=[a-z0-9]{48}' '/run/user/<userid>/jupyter/nbserver-*-open.html' | head -1')
# Get the port number from the URL:
port=$(echo $url | grep -E -o 'localhost:[0-9]{4}' | cut -d: -f2)
@spestana
spestana / xarray-rolling-xy.ipynb
Created October 4, 2019 21:39
demo/test for rolling mean with xarray
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@spestana
spestana / goes-aws-example.ipynb
Created April 2, 2020 19:19
Get a time series of GOES radiance values for a single band, accessing the data in an AWS S3 bucket.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@spestana
spestana / remote-jupyter-notebook.md
Last active July 29, 2020 18:19
Notes on setting up remote connection to a jupyter notebook server

Notes on setting up remote connection to a jupyter notebook server

Configure the server and password:

On the remote machine:

  • Check to see if you have a notebook configuration file jupyter_notebook_config.py
    • On linux this is typically located at /home/USERNAME/.jupyter/jupyter_notebook_config.py
  • If you don't have this file, generate it with the command:
jupyter notebook --generate-config
@spestana
spestana / resampled_stats.py
Last active May 16, 2023 17:46
Compute the mean, median, modes, counts from an xarray DataArrayResample object, returns a pandas dataframe
# requires: numpy, pandas, scipy.stats, xarray
def compute_modes(resampled, n=0):
'''Given a resampled xarray object (xarray.core.resample.DataArrayResample),
compute the modes ( rounding values with np.round(_,n) ),
return a pandas dataframe with the modes, counts, and groups (datetime)'''
# Compute modes
resampled_ModeResults = [stats.mode(np.round(x[1],n)) for x in resampled]