Skip to content

Instantly share code, notes, and snippets.

@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 March 1, 2024 20:24
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)
@blaylockbk
blaylockbk / python_colorbar_on_axes.py
Last active March 22, 2023 14:20
Adding colorbar on axes
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
data1 = np.random.random([5,5])
data2 = np.random.random([5,5])
fig = plt.figure(1, figsize=[16, 4])
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
this_plot = ax1.pcolormesh(data1, cmap='magma')
@blaylockbk
blaylockbk / datetime_axis_format.py
Last active March 30, 2018 14:47
formating datetime on axis
# Method 1: when you are using plt
from matplotlib.dates import DateFormatter
formatter = DateFormatter('%Y-%m-%d %H:%M:%S')
plt.gcf().axes[0].xaxis.set_major_formatter(formatter)
# Method 2: when you have already axessed the axes
from matplotlib.dates import DateFormatter
formatter = DateFormatter('%Y-%m-%d %H:%M:%S')
ax1.xaxis.set_major_formatter(formatter)