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

Thursday, May 26, 2016

Spanish Fork Climatology

I just used the MesoWest API to create a climatology of weather variables at my weather station in Spanish Fork. This shows the max, mean, and min values for various variables of each month of the year. The data collected ranges from  2013 to 2016.

See code: https://github.com/blaylockbk/Ute_MesoWest/blob/master/station_month_climatology.py
The freezing temperature is marked in a dashed light blue.




And looking at the daily averages (again this is only a few years of data, so in no way an official "climatological" average of temperature on each day)

See code: https://github.com/blaylockbk/Ute_MesoWest/blob/master/station_day_climatology.py
We haven't had a day below freezing after the first few days of May.




Tuesday, May 24, 2016

MetPy pythons gift to meteorologists :)

HRRR 700mb wind field and vorticity over Utah

I just installed MetPy and started using it to calculate and plot maps of HRRR vorticity! This will be a very useful package in the future :)

HRRR 700 mb winds and vorticity over Utah

Monday, May 23, 2016

Edit netCDF files with Photoshop

http://home.chpc.utah.edu/~u0553130/Brian_Blaylock/lake_surgery.html#PHOTO

I always thought it would be nice if I could load a netCDF file into photoshop and make changes to fields with the paint brush and pencil tool.
Well, now you can, with this three step process using Python (this only works with landuse or categorical data sets so far).
This is a work in progress and will get more attention after I defend my thesis, but it's a really handy concept.
  1. Convert a netCDF array into a bitmap image netcdf_to_bitmap.py.
  2. Open image in PhotoShop and use the pencil tool to change colors (i.e. land use categories). Save image as bitmap.
  3. Open the modified land use image in Python bitmap_to_netcdf.py. and extract the colors as categories. Save array into the WRF netCDF file

Below you can see that I added more lake and urban area using the pencil tool to the WRF landuse array.

Thursday, May 19, 2016

www.pytroll.org

A python library for dealing with Satellite data. I haven't used this, but might find it useful in the future.

http://www.pytroll.org/

Tuesday, May 17, 2016

Date incrementor in JavaScript

I know, this isn't Python, this is JavaScript, but I need to back up this code snippet.

I have an HTML page with a date input box that I need to increment by one day forward or backward. See example here: http://home.chpc.utah.edu/~u0553130/Brian_Blaylock/ksl_ozone_viewer.php

So, I here are some JavaScript functions that change the date input +/- one day.

function pad(number, length) {
    //I just use this to pad the day or month integer with zeros
    var str = '' + number;
    while (str.length < length) {
        str = '0' + str;
    }
    return str;
}

function next_day(){
    //                    dec   jan   feb   mar   arp   may   jun   jul   aug   set   oct   nov   dec   jan
    var days_per_month = ["31", "31", "28", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31", "31"];
    
    year = parseInt(document.getElementById('dateinput').value.slice(0,4));
    month = parseInt(document.getElementById('dateinput').value.slice(5,7));
    day = parseInt(document.getElementById('dateinput').value.slice(8,10));
        
    if (day < parseInt(days_per_month[month])){
       day = day+1; 
       day = pad(day,2);
       month = pad(month,2);
        }
    else{
        day = '01';
         if (month==12){
            month = '01';
            year = year +1
            year = pad(year,4)
         }
         else{
            month = month + 1;
            month = pad(month,2);     
         }
        
    }
    
    
    document.getElementById('dateinput').value = year+'-'+month+'-'+day
}

function previous_day(){
    //                    dec   jan   feb   mar   arp   may   jun   jul   aug   set   oct   nov   dec
    var days_per_month = ["31", "31", "28", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31"];
    
    year = parseInt(document.getElementById('dateinput').value.slice(0,4));
    month = parseInt(document.getElementById('dateinput').value.slice(5,7));
    day = parseInt(document.getElementById('dateinput').value.slice(8,10));
    
    if (day == 1){
       day = days_per_month[month-1];
        if (month==1){
            month = '12';
            year = year -1
            year = pad(year,4)
        }
        else{
          month = month-1;
           month = pad(month,2);     
        }
       
    }
    else{
        day = day-1;
        day = pad(day,2);
        month = pad(month,2);
    }
    
    
    document.getElementById('dateinput').value = year+'-'+month+'-'+day;

}

Note: this doesn't allow for a leap year date.

Friday, May 13, 2016

Python Matplotlib available colors

Python's matplotlib defines a bunch of colors you can use in you plots. The way colors look on a computer screen looks different than how it looks when you print it, so you can print out the following page as a reference to what colors are available and what they really look like when printed.

Created using a script from http://stackoverflow.com/questions/22408237/named-colors-in-matplotlib

# plot python colors for printing

import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.colors as colors
import math
import matplotlib
for name, hex in matplotlib.colors.cnames.iteritems():
    print(name, hex)
    
width= 8
height= 11

fig = plt.figure(figsize=(width,height))
ax = fig.add_subplot(111)

ratio = 1.0 / 3.0
count = math.ceil(math.sqrt(len(colors.cnames)))
print count
x_count = count * ratio
y_count = count / ratio
x = 0
y = 0
w = 1 / x_count
h = 1 / y_count

for c in sorted(colors.cnames):
    pos = (x / x_count, y / y_count)
    ax.add_patch(patches.Rectangle(pos, w, h, color=c))
    ax.annotate(c, xy=pos,fontsize=10)
    if y >= y_count-1:
        x += 1
        y = 0
    else:
        y += 1
        

plt.yticks([])
plt.xticks([])
plt.savefig('allcolors',bbox_inches='tight',dpi=300)
#plt.show()