Skip to content

Instantly share code, notes, and snippets.

from datetime import datetime, timedelta
base = datetime.today()
numdays = 5
date_list = [base - timedelta(days=x) for x in range(0,numdays)]
from datetime import datetime
dates = ['01/01/2016 04:50','01/01/2016 05:50','01/01/2016 06:50','01/01/2016 07:50']
DATE = [datetime.strptime(x,'%m/%d/%Y %H:%M') for x in dates]
DATES = np.array([])
for i in dates:
DATES = np.append(DATES,datetime.strptime(i,'%m/%d/%Y %H%M'))
@blaylockbk
blaylockbk / list of Epoch times to python datetimes
Last active November 3, 2016 21:49
Takes a list of Epoch times and converts it to a list of python datetime objects
# where dates is a list or numpy array of epoch dates...
DATE = [datetime.utcfromtimestamp(x) for x in dates]
@blaylockbk
blaylockbk / date strings list to python datetime objects
Created November 1, 2016 16:30
this method uses numpy vectorized function, which is slightly faster than using a list generator
import numpy as np
import datetime
def datestr_to_datetime(x):
return datetime.datetime.strptime(x,'%Y-%m-%dT%H:%M:%SZ')
# where dates is some list of dates as a string...
# vectorize the function
converttime = np.vectorize(datestr_to_datetime)
DATES = converttime(dates)
@blaylockbk
blaylockbk / untar HRRR
Created November 10, 2016 15:30
untar select HRRR files from the compressed MesoWest archive
# To untar just about everything
tar -xzvf models.tar.gz 20160101/models/hrrr/
# To untar just the HRRR pressure fields for analysis hours
tar -zxvf models.tar.gz --wildcards --no-anchored 'hrrr.t*z.wrfprsf00.grib2'
$ to untar just the HRRR surface fields for analysis hours
tar -zxvf models.tar.gz --wildcards --no-anchored 'hrrr.t*z.wrfsfcf00.grib2'
@blaylockbk
blaylockbk / verify_GFS_pygrib.py
Last active November 11, 2016 22:35
Verify GFS dewpoint with MesoWest observation for class assignment
# Brian Blaylock
# ATMOS 6910 Homework
# 10 November 2016
# Look at the figures here:
# http://kbkb-wx-python.blogspot.com/2016/11/verifying-gfs-dewpoint-data-with.html
"""
[X] Reads a GRiB forecast file from the GFS (15%)
[X] Extracts Temperature and Relative Humidity from ingest (5%)
@blaylockbk
blaylockbk / make_imgs_to_movie_ffmpeg
Last active March 27, 2020 16:33
make a video or movie with ffmpeg
ffmpeg -f image2 -pattern_type glob -framerate 6 -i '*.png' foo.avi
# Retain high resolution
ffmpeg -f image2 -pattern_type glob -framerate 6 -i '*.png' -vb 20M foo.avi
# Convert .mov to .avi
ffmpeg -i file.mov file.avi
@blaylockbk
blaylockbk / create_dir.py
Created November 29, 2016 16:08
Create directory if it doesn't exits
import os
OUTDIR = '/this/path/will/be/created/'
if not os.path.exists(OUTDIR):
os.makedirs(OUTDIR)
@blaylockbk
blaylockbk / multipro_template.py
Last active May 20, 2024 16:39
Template for Python multiprocessing and multithreading
import multiprocessing
from multiprocessing.dummy import Pool as ThreadPool
import numpy as np
def my_multipro(items, func, max_cpus=12):
"""Do an embarrassingly parallel task using multiprocessing.
Use this for CPU bound tasks.