Skip to content

Commit

Permalink
created new tests for winds, GUI, SARS, and binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Blumberg committed Jan 11, 2019
1 parent 96344ed commit 3cbb28e
Show file tree
Hide file tree
Showing 11 changed files with 225 additions and 62 deletions.
10 changes: 7 additions & 3 deletions runsharp/full_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ def __init__(self, config, **kwargs):

urls = data_source.pingURLs(self.data_sources)
self.has_connection = any(urls.values())
self.strictQC = True

# initialize the UI
self.__initUI()
Expand Down Expand Up @@ -739,7 +740,7 @@ def skewApp(self, filename=None, ntry=0):
logging.debug("Focusing on the SkewApp")
self.focusSkewApp()
logging.debug("Adding the profile collection to SPCWindow")
self.skew.addProfileCollection(prof_collection)
self.skew.addProfileCollection(prof_collection, check_integrity=self.strictQC)
else:
print("There was an exception:", exc)

Expand Down Expand Up @@ -790,6 +791,8 @@ def loadArchive(self, filename):
def hasConnection(self):
return self.has_connection

def setStrictQC(self, val):
self.strictQC = val

@progress(Picker.async_obj)
def loadData(data_source, loc, run, indexes, ntry=0, __text__=None, __prog__=None):
Expand Down Expand Up @@ -1044,11 +1047,12 @@ def newerRelease(latest):
QDesktopServices.openUrl(QUrl(latest[2]))

@crasher(exit=True)
def createWindow(file_names, collect=False, close=True, output='./'):
def createWindow(file_names, collect=False, close=True, output='./', strictQC=False):
main_win = Main()
for fname in file_names:
txt = OKGREEN + "Creating image for '%s' ..." + ENDC
print(txt % fname)
main_win.picker.setStrictQC(strictQC)
main_win.picker.skewApp(filename=fname)
if not collect:
fpath, fbase = os.path.split(fname)
Expand Down Expand Up @@ -1099,7 +1103,7 @@ def test(fn):
app = QApplication([])
else:
app = QApplication.instance()
win = createWindow([fn])
win = createWindow(fn, strictQC=False)
win.close()

def parseArgs():
Expand Down
2 changes: 1 addition & 1 deletion sharppy/databases/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__all__ = ['sars', 'pwv', 'inset_data']
__all__ = ['sars', 'pwv', 'inset_data', 'sars_cal']
48 changes: 48 additions & 0 deletions sharppy/databases/fix_sars_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import sars
import numpy as np
from datetime import datetime

supercell_db = np.loadtxt('sars_supercell.txt', skiprows=1, dtype=bytes, comments="%%%%")
hail_db = np.loadtxt('sars_hail.txt', skiprows=1, dtype=bytes)
print(supercell_db)
print(hail_db)

for f in supercell_db[:,0]:
f = f.decode("utf-8")
try:
raw = sars.getSounding(str(f), 'supercell')
except:
print("NO DATA FILE FOR:", f)
continue
lines = open(raw, 'r').readlines()
title = lines[1]
loc = f.split('.')[1]
print(title.strip(), f, loc)
if 'RUC' in title.strip():
#print("it's a model")
new_title = title.replace(loc.lower(), '')
new_title = new_title.replace('RUC', loc)
new_title = new_title.replace('F000', '')
new_title = new_title.replace(';', ',')
elif "MRF" in title.strip():
new_title = title.replace("MRF", loc.upper())
fname = f.split('.')[0]
new_title = loc.upper() + ' ' + fname[:6] + '/' + fname[6:8] + '00\n\n'
else:
continue
note = '\n----- Note -----\nOld Title: ' + title + '\n'
#print("MODIFY:")
#print("Old Title:", title)
#print("New Title:", new_title)
print("BEFORE:")
print(''.join(lines))
lines[1] = " " + new_title
new_data = ''.join(lines) + note

print("AFTER:")
print(new_data)
#if "MRF" in title:
# break
f = open(raw, 'w')
f.write(new_data)
f.close()
17 changes: 16 additions & 1 deletion sharppy/databases/sars.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,21 @@ def hail(database_fn, mumr, mucape, h5_temp, lr, shr6, shr9, shr3, srh):
return quality_match_dates, quality_match_sizes, num_loose_matches, num_sig_reports, prob_sig_hail


def get_sars_dir(match_type):
"""
Returns the directory where the raw SARS files are.
Parameters
----------
match_type : str
'supercell' or 'hail'
Returns
-------
string
"""
return os.path.join(os.path.dirname(__file__), "sars/" + match_type.lower() + "/")

## written by Kelton Halbert
def getSounding(match_string, match_type, profile="default"):
"""
Expand All @@ -363,7 +378,7 @@ def getSounding(match_string, match_type, profile="default"):
#if (match_type.lower() != "supercell") or (match_type.lower() != "hail"):
# raise Exception("InvalidSARSType", match_type.lower() + " is an invalid SARS type.")
## get the directory with the data
data_dir = os.path.join(os.path.dirname(__file__), "sars/" + match_type.lower() + "/")
data_dir = get_sars_dir(match_type)

match_date, match_loc = match_string.split(".")
files = os.listdir(data_dir)
Expand Down
34 changes: 28 additions & 6 deletions sharppy/databases/sars_cal.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import sars
import sharppy.databases.sars as sars
import sharppy.sharptab as tab
from sharppy.io.spc_decoder import SPCDecoder
import numpy as np
from datetime import datetime
import os

def get_profile(fname, sars_type):
# Create a convective profile object
Expand Down Expand Up @@ -55,7 +56,8 @@ def calc_inputs(new_prof, sars_type):

def check_supercell_cal(use_db=True):
# Use to check the SARS supercell calibration
supercell_db = np.loadtxt('sars_supercell.txt', skiprows=1, dtype=bytes, comments="%%%%")
database_fn = os.path.join( os.path.dirname( __file__ ), 'sars_supercell.txt' )
supercell_db = np.loadtxt(database_fn, skiprows=1, dtype=bytes, comments="%%%%")
hits = 0
fa = 0
cn = 0
Expand Down Expand Up @@ -95,6 +97,7 @@ def check_supercell_cal(use_db=True):
miss += 1
print("--- SARS SUPERCELL CALIBRATION ---")
print_stats(hits, cn, miss, fa, match)
return {'hits': hits, 'cn': cn, 'miss':miss, 'fa': fa, 'match':match}

def print_stats(hits, cn, miss, fa, matches):
# Print out the verification stats
Expand All @@ -112,9 +115,27 @@ def print_stats(hits, cn, miss, fa, matches):
print("TSS: %.3f" % (float(hits)/float(hits+miss) - float(fa)/float(fa+cn)))
print()

def calc_verification(vals):
stats = {}
stats['num'] = vals['hits'] + vals['cn'] + vals['miss'] + vals['fa']
for key in vals.keys():
stats[key] = vals[key]
hits = stats['hits']
miss = stats['miss']
fa = stats['fa']
cn = stats['cn']
stats["ACCURACY"] = float(hits+cn)/float(hits+cn+miss+fa)
stats["BIAS"] = float(hits+fa)/float(hits+miss)
stats["POD"] = float(hits)/float(hits+miss)
stats["FAR"] = float(fa)/float(fa+hits)
stats["CSI"] = float(hits)/float(hits + miss + fa)
stats["TSS"] = float(hits)/float(hits+miss) - float(fa)/float(fa+cn)
return stats

def check_hail_cal(use_db=True):
# Check the calibration of the SARS hail
hail_db = np.loadtxt('sars_hail.txt', skiprows=1, dtype=bytes)
database_fn = os.path.join( os.path.dirname( __file__ ), 'sars_hail.txt' )
hail_db = np.loadtxt(database_fn, skiprows=1, dtype=bytes)
hits = 0
cn = 0
miss = 0
Expand Down Expand Up @@ -153,7 +174,8 @@ def check_hail_cal(use_db=True):
miss += 1
print("--- SARS HAIL CALIBRATION ---")
print_stats(hits, cn, miss, fa, match)
return {'hits': hits, 'cn': cn, 'miss':miss, 'fa': fa, 'match':match}

check_db = False
check_supercell_cal(check_db)
check_hail_cal(check_db)
#check_db = False
#check_supercell_cal(check_db)
#check_hail_cal(check_db)
15 changes: 13 additions & 2 deletions sharppy/tests/test_binary.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import os
import sys
import pytest
import glob
import sharppy.databases.sars as sars
import numpy as np
full_gui = pytest.importorskip('runsharp.full_gui')

@pytest.mark.skipif("DISPLAY_AVAIL" in os.environ and os.environ["DISPLAY_AVAIL"] == 'NO', reason="DISPLAY not set")
def test_main_entry_pt():
#sys.argv = []
#print(full_gui.sys.argv)
#full_gui.sys.argv.append('examples/data/14061619.OAX')
full_gui.test('examples/data/14061619.OAX')
hail_files = glob.glob(sars.get_sars_dir('hail') + '*')
supercell_files = glob.glob(sars.get_sars_dir('supercell') + '*')
#print(hail_files + supercell_files)
sars_files = np.asarray(hail_files + supercell_files)
idx = np.random.randint(0, len(sars_files), 10)
files = list(sars_files[idx]) + ['examples/data/14061619.OAX']
print(files)
full_gui.test(files)

#test_main_entry_pt()

test_main_entry_pt()

8 changes: 6 additions & 2 deletions sharppy/tests/test_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
dec = SPCDecoder('examples/data/14061619.OAX')
prof_coll = dec.getProfiles()
prof = prof_coll.getCurrentProfs()['']
app = QtGui.QApplication([])


if QtGui.QApplication.instance() is None:
app = QtGui.QApplication([])
else:
app = QtGui.QApplication.instance()

#@pytest.mark.skipif(True, reason="DISPLAY not set")
@pytest.mark.skipif("DISPLAY_AVAIL" in os.environ and os.environ["DISPLAY_AVAIL"] == 'NO', reason="DISPLAY not set")
def test_insets():
Expand Down
Loading

0 comments on commit 3cbb28e

Please sign in to comment.