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

Thursday, June 11, 2015

Skew-T

Found this python script for plotting skew-t plots. https://pypi.python.org/pypi/SkewT/0.1.1

Python and MesoWest API

Uses the MesoWest API to get ozone concentration data and plot them. An example of formatting plot datetime plot tick marks is also shown.

# Brian Blaylock
# June 9, 2015

# Uses the MesoWest API to get find the maximum ozone
# from in-situ stations during GSLSO3S

# Tips for creating API request:
# -look at MesoWest API documentation: http://mesowest.org/api/docs/
# -use JSON viewer to see request results: http://jsonviewer.stack.hu/


import json
import numpy as np
import urllib2
import matplotlib.pyplot as plt
from datetime import datetime
from matplotlib.dates import DateFormatter, YearLocator, MonthLocator, DayLocator, HourLocator

token = '1234567890' #contact mesowest@lists.utah.edu to get your own token.

station = 'mtmet'
# dateformat YYYYMMDDHHMM
start_time = '201506050000'
end_time = '201506060000'
variables = 'ozone_concentration'
time_option = 'local'
URL = 'http://api.mesowest.net/v2/stations/timeseries?stid='+station+'&start='+start_time+'&end='+end_time+'&vars='+variables+'&obtimezone='+time_option+'&token='+token

#Open URL and read the content
f = urllib2.urlopen(URL)
data = f.read()

# convert that json string into some python readable format
data = json.loads(data)

stn_name = data['STATION'][0]['NAME']
# get ozone data convert to numpy array
ozone = data["STATION"][0]["OBSERVATIONS"]["ozone_concentration_set_1"]
#convert ozone into a numpy array: setting dtype=float replaces None value with a np.nan
ozone = np.array(ozone,dtype=float)

# get date and times and convert to datetime and put into a numpy array
dates = data["STATION"][0]["OBSERVATIONS"]["date_time"]
DATES = np.array([]) # first make an empty array
for i in dates:
   if time_option=='utc':
      converted_time = datetime.strptime(i,'%Y-%m-%dT%H:%M:%SZ')
   else:
      converted_time = datetime.strptime(i,'%Y-%m-%dT%H:%M:%S-0600')
   DATES = np.append(DATES,converted_time)


# make a simple ozone time series
#----------------------------------------------------------
ax = plt.subplot(1,1,1)
plt.plot(DATES,ozone)
plt.title('Ozone Concentration (ppb) for '+stn_name)
plt.xlabel('date')
plt.xticks(rotation=30)
#Now we format the date ticks
# Format Ticks
# Find months
months = MonthLocator()
# Find days
days = DayLocator()
# Find each 0 and 12 hours
hours = HourLocator(byhour=[0,6,12,18])
# Find all hours
hours_each = HourLocator()
# Tick label format style
dateFmt = DateFormatter('%b %d\n%H:%M')
# Set the x-axis major tick marks
ax.xaxis.set_major_locator(hours)
# Set the x-axis labels
ax.xaxis.set_major_formatter(dateFmt)
# For additional, unlabeled ticks, set x-axis minor axis
ax.xaxis.set_minor_locator(hours_each)


plt.savefig(start_time+'_'+end_time+'.png', format='png')




Wednesday, May 20, 2015

Putty Change color scheme

I've been staring at the hard-to-see blue color too long in Putty. I found this helpful info for changing the default color scheme in python here...http://www.darkrune.org/blog/?p=213. We'll see how I like the change.

Before logging into a Putty session, click Window/Colours, and reassign the colors

Putty RGB colors/options for the Zenburn color scheme are as follows -
  • Default Foreground - 255/255/255
  • Default Background - 51/51/51
  • ANSI Black - 77/77/77
  • ANSI Green - 152/251/152
  • ANSI Yellow - 240/230/140
  • ANSI Blue - 205/133/63
  • ANSI Blue Bold -135/206/235
  • ANSI Magenta - 255/222/173 or 205/92/92
  • ANSI Cyan - 255/160/160
  • ANSI Cyan Bold - 255/215/0
  • ANSI White - 245/222/179

Tuesday, April 7, 2015

Bootstrapping in Python

Python code for regression significance testing

#------------------------------------------
#Bootstrap test for significance
#------------------------------------------
import numpy as np
import matplotlib.pyplot as plt

x = compositeL # some numpy vector
y = compositeF # another numpy vector

# we will fill this vector with the regression coefficient as we find them
b1_vector = np.zeros(5000)

#bootstraping
for i in np.arange(5000):
     index = np.random.randint(0,len(x),len(x))
     #pull out the (x,y) pair for each index
     sample_x = x[index]
     sample_y = y[index]
     #calculate the b1 value and store it in the b_vector
     b_values = np.polyfit(sample_x,sample_y,1)
     #b1 is the first element result of polyfit 
     b1=b_values[0]
     b1_vector[i]=b1

plt.figure(6)

plt.hist(b1_vector,50)


In this example there is 99.96% confidence that the regression coefficient is positive.