Skip to content

Instantly share code, notes, and snippets.

View mikecharles's full-sized avatar

Mike Charles mikecharles

View GitHub Profile
@mikecharles
mikecharles / get-python-dependencies.md
Created April 11, 2024 14:11
Get all Python dependencies for a Python application

To get a unique list of all Python packages your application depends on:

grep -irhP '^import ' interp_stn_to_grid | sort | uniq

where interp_stn_to_grid is the top-level directory containing all the Python scripts in your application.

@mikecharles
mikecharles / README.md
Created July 18, 2019 19:05
Create a NetCDF file in Python

This is a simple example that creates a NetCDF file for 1 degree global 2m temperature. There is no time coordinate.

For this portion of code:

ds = xr.DataArray(data, ...)

data is a NumPy array, assumed to have a shape of num_lats x num_lons

@mikecharles
mikecharles / ldap_auth.py
Created July 28, 2017 12:00
Authenticate against LDAP in Python
#!/usr/bin/env python
import ldap
from getpass import getpass
# Set constants
HOST = '<HOST>' # eg. ldaps://my-ldap-server.com
BASE_DN = '<BASE_DN>' # eg. 'dc=example,dc=com'
# Set LDAP options
@mikecharles
mikecharles / README.md
Created January 12, 2017 16:57
Load extended GEFS dataset

To run this, just create a virtualenv and install cpc.geofiles:

$ pip install cpc.geofiles

@mikecharles
mikecharles / run.sh
Created January 11, 2017 18:01
Remove grib records from grib files where the forecast hour (fhr) doesn't belong in that file
#!/bin/sh
ROOT_DIR=/path/to/data
START_DATE='19950101'
END_DATE='20161231'
cd $ROOT_DIR
# Loop over all dates within the given date range
curr_date=$START_DATE
@mikecharles
mikecharles / convert-all.sh
Created October 24, 2016 19:57
Batch convert video files using HandbrakeCLI
#!/bin/sh
find . -type f -iname "*.avi" -print0 | while IFS= read -r -d $'\0' infile ; do
outfile="${infile%.*}.m4v"
echo "Converting ${infile} to ${outfile}..."
HandbrakeCLI -Z 'AppleTV 3' -q 25 -i "$infile" -o "$outfile"
done
@mikecharles
mikecharles / reduce_image_size.py
Created October 5, 2016 21:00
Reduce the file size of an image in Python
from PIL import Image
im = Image.open(img_file)
im.convert('P', palette=Image.ADAPTIVE, colors=256).save(img_file)
@mikecharles
mikecharles / find.sh
Created September 7, 2016 13:36
Loop over files from find output
find . -type f -iname "*.txt" -print0 | while IFS= read -r -d $'\0' line; do
echo "$line"
ls -l "$line"
done
@mikecharles
mikecharles / README.md
Created September 2, 2016 12:52
Fix Python namespace package not working with pip install --editable / python setup.py develop

Create a file z.pth in your virtual env's site-packages dir (eg. ~/.conda/envs/<env-name>/lib/python3.5/site-packages) containing this line:

import pkg_resources; pkg_resources.fixup_namespace_packages('')
@mikecharles
mikecharles / keep-trying.sh
Last active August 24, 2016 15:23
Keep trying a command every X minutes, and stop after Y minutes have passed, or the command succeeds
#!/bin/sh
#---------------------------------------------------------------------------------------------------
# Usage
#
usage() {
printf "$(basename "$0") -s <TIME_BETWEEN_TRIES> -t <TIME_TO_TRY> <COMMAND>\n"
printf " where:\n"
printf " -s TIME_BETWEEN_TRIES time (minutes) between each try\n"
printf " -t TIME_TO_TRY total time (minutes) to try the command before giving up\n"