Skip to content

Instantly share code, notes, and snippets.

@blaylockbk
blaylockbk / colors.py
Created February 4, 2019 21:41
List of colors from a color map
import matplotlib
# Generate a list of colors
cmap = matplotlib.cm.get_cmap('Spectral')
# Retrieve 19 colors from the colormap
colors = cmap(np.arange(19)/19)
@blaylockbk
blaylockbk / progress.py
Created February 4, 2019 17:43
Print progress to show completion of a loop
# Print a progress bar for a loop completion
from time import sleep
F = range(10)
num = len(F)
for i in F:
sleep(1)
sys.stdout.write('\r%.1f%% Complete (%s of %s)' % (i/num*100, i, num))
@blaylockbk
blaylockbk / masked_array.py
Last active September 12, 2018 19:56
mask an array value
masked = H['value']
masked = np.ma.array(masked)
masked[masked == -10] = np.ma.masked
@blaylockbk
blaylockbk / numpy_datetime_to_datetime.py
Last active September 4, 2025 07:05
Convert numpy.datetime64 to datetime.datetime
from datetime import datetime
import numpy as np
def to_datetime(date):
"""
Converts a numpy datetime64 object to a python datetime object
Input:
date - a np.datetime64 object
Output:
DATE - a python datetime object
# Download single variable from single day
DATE = date(2017, 3, 10) # Model run date
variable = 'HGT:500 mb' # Must be part of a line in the .idx file
download_HRRR_variable_from_pando(DATE, variable,
hours=range(0, 24), fxx=[0],
model='hrrr', field='sfc',
more_vars=4,
outdir='./')
# Fast download of HRRR grib2 files (single variable) with multithreading
# import some additional modules
from queue import Queue
from threading import Thread
def worker():
# This is where the main download function is run.
# Change the hour and fxx parameters here if needed.
while True:
item = q.get()
# === User change these parameters ================================
# date range
sDATE = date(2017, 3, 10) # Start date
eDATE = date(2017, 3, 13) # End date (exclusive)
# list of variables
variables = ['TMP:2 m', 'DPT:2 m', 'UGRD:10 m', 'VGRD:10 m']
# =================================================================
days = (eDATE-sDATE).days
DATES = [sDATE + timedelta(days=d) for d in range(days)]
# === User change these parameters ============================
# date range
sDATE = date(2017, 3, 10) # Start date
eDATE = date(2017, 3, 13) # End date (exclusive)
# variable string
variable = 'TMP:2 m' # Must be part of a line in the .idx file
# =============================================================
days = (eDATE-sDATE).days
DATES = [sDATE + timedelta(days=d) for d in range(days)]
# Download single variable from single day
DATE = date(2017, 3, 10) # Model run date
variable = 'TMP:2 m' # Must be part of a line in the .idx file
download_HRRR_variable_from_pando(DATE, variable,
hours=range(0, 24), fxx=[0],
model='hrrr', field='sfc',
outdir='./')
@blaylockbk
blaylockbk / download_HRRR_variable_from_pando.py
Last active June 17, 2022 19:13
Function to download HRRR variables from the Pando archive. Includes examples to download multiple days and multiple variables with loops.
#########################################
# ✨ Use the new Herbie Package instead:
# https://github.com/blaylockbk/Herbie
#########################################
# Brian Blaylock
# February 13, 2018
# Updated December 10, 2018 for Python 3 (Thanks J.Matt for sharing)