Skip to content
Snippets Groups Projects
get_CAPE_spc_17June2015.py 1.72 KiB
#!/usr/bin/env python
#python 2.7

# get_CAPE_spc_17June2015.py
#
# this python script reads csv files in spc format and create a profile object 
#
# class Parcel(object):       
#        Parameters
#       ----------
#       pbot : number
#       Lower-bound (pressure; hPa) that the parcel is lifted
#       ptop : number
#       Upper-bound (pressure; hPa) that the parcel is lifted
#       pres : number
#       Pressure of the parcel to lift (hPa)
#       tmpc : number
#       Temperature of the parcel to lift (C)
#       dwpc : number
#       Dew Point of the parcel to lift (C)
#       bplus : number
#       CAPE
#       bminus : number
#       CIN
#
# revised: 17June2015 UWSPARC SSEC GP create profile object
# 

from StringIO import StringIO
import sharppy.sharptab.profile as profile
import urllib
import time as gmtime
import datetime
import sys
import glob
import numpy as np
import csv
from sharppy.sharptab.params import cape

files = glob.glob("UWSPARC_2*.csv")
# loop over input files
for filename in files:
    print ('... reading file:  ' + filename)
    url = open(filename)
    data = np.array(url.read().split('\n'))
    title_idx = np.where( data == '%TITLE%\r')[0][0]
    start_idx = np.where( data == '%RAW%\r' )[0] + 1
    finish_idx = np.where( data == '%END%\r')[0]
    plot_title = data[title_idx + 1]
    full_data = '\n'.join(data[start_idx : finish_idx][:])
    sound_data = StringIO( full_data )
    P, h, T, Td, wdir, wspd = np.genfromtxt( sound_data, delimiter=',', comments="%", unpack=True )
    prof = profile.create_profile( profile='convective', pres=P, hght=h, tmpc=T, dwpc=Td, wdir=wdir, wspd=wspd, location='SPARC')
    pcl = cape(prof)
    print ('CAPE = ' + str(pcl.bplus))
    print ('CIN = ' + str(pcl.bminus))