This page demonstrates Python tips and tricks that I use in my everyday programming as an atmospheric science graduate student.
-Brian Blaylock

Thursday, April 13, 2017

Python Colormaps

Before using the python matplotlib color maps, please read this documentation:
http://matplotlib.org/users/colormaps.html

../_images/lightness_00.png
../_images/lightness_01.png
../_images/lightness_02.png
../_images/lightness_03.png
../_images/lightness_05.png

Friday, March 24, 2017

Multiprocessing vs Multithreading

I need to download files fast. I want to swamp my network cables to download as much as it can consume. In the past I've used multiprocessing because it was easy to split jobs to several processors. But it is more efficient to use multithreading.

Multithreading uses one processor, but you can set up a queue so that it will continuously run as much as it can handle.

As an example, here is some code...

# Brian Blaylock
# March 24, 2017                           Yesterday Salt Lake had lots of rain

"""
Fast download of HRRR grib2 files with threading
"""

from queue import Queue 
from threading import Thread
from datetime import datetime, timedelta
import numpy as np
import urllib2
import re
import os

var='TMP:2 m'

def download(URL):
    # Code to download something based on a URL
    # ...
def worker():
    while True:
        item = q.get()
        print "number:", item
        download(item)
        q.task_done()

num_of_threads = 10

q = Queue()
for i in range(num_of_threads):
    t = Thread(target=worker)
    t.daemon = True
    t.start()

# List of URL's to download from
URL_list = ['...','...','...']

timer1 = datetime.now()
for item in URL_list:
    q.put(item)

q.join()       # block until all tasks are done


There is speed up when downloading, and the copper wires are saturated with as much as it can handle with about 10 threads or ten processors.
Here is a visual of what I learned:



Tuesday, February 14, 2017

not python, but I made a handy image viewer

This isn't Python, but I made a handy web page tool for viewing images in a directory. Just dump this photo_viewer.php page in a public_html web directory with images in it, and this page will let you view the images by hovering a mouse over buttons with the image names, clicking the buttons, or selecting images from an option box.

https://github.com/blaylockbk/Web-Homepage/blob/master/photo_viewer/photo_viewer.php

Example page: http://home.chpc.utah.edu/~u0553130/PhD/UWFPS_2017/time-height/photo_viewer.php


Thursday, January 5, 2017

Simple Basemap Example

You can do a lot with Python basemaps. Here is an example of a few things you can do...