Skip to content

Commit 3a51c7b

Browse files
committed
migration to sqlite - all checks passing
1 parent cc75d2d commit 3a51c7b

26 files changed

+178
-297
lines changed

AutoQC.py

+7-16
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,15 @@ def process_row(uid):
3232
return
3333

3434
# run tests
35+
results = []
3536
query = "UPDATE " + sys.argv[1] + " SET "
3637
for itest, test in enumerate(testNames):
3738
result = run(test, [profile], parameterStore)[0]
38-
print test, type(result)
39-
result = pickle.dumps(result, -1)
40-
query += test.lower() + " = " + str(psycopg2.Binary(result)) + ', '
41-
query = query[:-2]
42-
query += " WHERE uid = " + str(profile.uid()) + ";"
43-
main.dbinteract(query, usePostgres=True)
39+
results.append(main.pack_array(result))
40+
query += test.lower() + "=?,"
4441

45-
# # run tests
46-
# for itest, test in enumerate(testNames):
47-
# result = run(test, [profile], parameterStore)[0]
48-
# result = pickle.dumps(result, -1)
49-
# query = "UPDATE " + sys.argv[1] + " SET "
50-
# query += test.lower() + " = " + str(psycopg2.Binary(result)) + " WHERE uid = " + str(profile.uid()) + ";"
51-
# main.dbinteract(query, usePostgres=True)
42+
query = query[:-1] + " WHERE uid = " + str(profile.uid()) + ";"
43+
main.dbinteract(query, results)
5244
########################################
5345
# main
5446
########################################
@@ -69,8 +61,7 @@ def process_row(uid):
6961

7062
# set up global parmaeter store
7163
parameterStore = {
72-
"table": sys.argv[1],
73-
"postgres": True
64+
"table": sys.argv[1]
7465
}
7566
for test in testNames:
7667
exec('from qctests import ' + test)
@@ -81,7 +72,7 @@ def process_row(uid):
8172

8273
# connect to database & fetch list of all uids
8374
query = 'SELECT uid FROM ' + sys.argv[1] + ' ORDER BY uid;'
84-
uids = main.dbinteract(query, usePostgres=True)
75+
uids = main.dbinteract(query)
8576

8677
# launch async processes
8778
pool = Pool(processes=int(sys.argv[2]))

build-db.py

+13-14
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
11
# usage: python build-db.py <wod ascii file name> <table name to append to>
22

33
from wodpy import wod
4-
import sys, psycopg2
4+
import sys, sqlite3
55
import util.main as main
66

77
if len(sys.argv) == 3:
88

9-
# connect to database and create a cursor by which to interact with it.
10-
try:
11-
conn = psycopg2.connect("dbname='root' user='root'")
12-
except:
13-
print "I am unable to connect to the database"
14-
9+
conn = sqlite3.connect('iquod.db', isolation_level=None)
1510
cur = conn.cursor()
1611

1712
# Identify tests
1813
testNames = main.importQC('qctests')
1914
testNames.sort()
2015

2116
# set up our table
22-
query = "CREATE TABLE IF NOT EXISTS " + sys.argv[2] + """(
17+
query = "DROP TABLE IF EXISTS " + sys.argv[2] + ";"
18+
cur.execute(query)
19+
query = "CREATE TABLE " + sys.argv[2] + """(
2320
raw text,
24-
truth boolean,
21+
truth integer,
2522
uid integer,
2623
year integer,
2724
month integer,
@@ -33,7 +30,7 @@
3330
probe integer,
3431
"""
3532
for i in range(len(testNames)):
36-
query += testNames[i].lower() + ' BYTEA'
33+
query += testNames[i].lower() + ' BLOB'
3734
if i<len(testNames)-1:
3835
query += ','
3936
else:
@@ -44,8 +41,7 @@
4441
# populate table from wod-ascii data
4542
fid = open(sys.argv[1])
4643

47-
#while True:
48-
for xx in range(500):
44+
while True:
4945
# extract profile as wodpy object and raw text
5046
start = fid.tell()
5147
profile = wod.WodProfile(fid)
@@ -54,8 +50,9 @@
5450
raw = fid.read(end-start)
5551
fid.seek(end)
5652

57-
if profile.uid() != 65781603 and profile.uid() != 563906:
58-
continue
53+
#if profile.uid() != 65781603 and profile.uid() != 563906:
54+
# continue
55+
5956
# set up dictionary for populating query string
6057
wodDict = profile.npdict()
6158
wodDict['raw'] = "'" + raw + "'"
@@ -83,6 +80,8 @@
8380
)""".format(p=wodDict)
8481
query = query.replace('--', 'NULL')
8582
query = query.replace('None', 'NULL')
83+
query = query.replace('True', '1')
84+
query = query.replace('False', '0')
8685
cur.execute(query)
8786
if profile.is_last_profile_in_file(fid) == True:
8887
break

data/ds.py

-89
This file was deleted.

qctests/Argo_impossible_date_test.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
The date criterion has been altered so that the test can be applied to all data types.
55
"""
66

7-
import numpy
8-
import calendar
7+
import numpy, calendar
98

109
def test(p, parameters):
1110
"""

qctests/Argo_impossible_location_test.py

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"""
44

55
import numpy
6-
import calendar
76

87
def test(p, parameters):
98
"""

qctests/Argo_pressure_increasing_test.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
http://w3.jcommops.org/FTPRoot/Argo/Doc/argo-quality-control-manual.pdf page 8.
77
"""
88

9-
import numpy as np
9+
import numpy
1010
from util import obs_utils
1111

1212
def test(p, parameters):
@@ -21,7 +21,7 @@ def test(p, parameters):
2121

2222
# Make the quality control decisions. This should
2323
# return true where z decreases or stays the same.
24-
qc = np.ndarray(p.n_levels(), dtype=bool)
24+
qc = numpy.ndarray(p.n_levels(), dtype=bool)
2525
qc[:] = False
2626
iRef = -1
2727
for i in range(0, p.n_levels()):

qctests/Argo_regional_range_test.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
Implements the regional range test on page 7 of http://w3.jcommops.org/FTPRoot/Argo/Doc/argo-quality-control-manual.pdf
33
"""
44

5-
import numpy
6-
import pyproj
5+
import numpy, pyproj
76
from shapely.geometry import Polygon, Point
87

98
def test(p, parameters):

qctests/EN_background_check.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import numpy as np
88
import util.obs_utils as outils
99
from netCDF4 import Dataset
10-
import sqlite3, sys
1110
import util.main as main
1211

1312
def test(p, parameters):
@@ -20,8 +19,8 @@ def test(p, parameters):
2019
# Check if the QC of this profile was already done and if not
2120
# run the QC.
2221
query = 'SELECT en_background_check FROM ' + parameters["table"] + ' WHERE uid = ' + str(p.uid()) + ';'
23-
qc_log = main.dbinteract(query, usePostgres=parameters['postgres'])
24-
qc_log = main.unpack_row(qc_log[0], usePostgres=parameters['postgres'])
22+
qc_log = main.dbinteract(query)
23+
qc_log = main.unpack_row(qc_log[0])
2524

2625
if qc_log[0] is not None:
2726
return qc_log[0]

qctests/EN_constant_value_check.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ def test(p, parameters):
1616
# Check if the QC of this profile was already done and if not
1717
# run the QC.
1818
query = 'SELECT en_constant_value_check FROM ' + parameters["table"] + ' WHERE uid = ' + str(p.uid()) + ';'
19-
qc_log = main.dbinteract(query, usePostgres=parameters['postgres'])
20-
qc_log = main.unpack_row(qc_log[0], usePostgres=parameters['postgres'])
19+
qc_log = main.dbinteract(query)
20+
qc_log = main.unpack_row(qc_log[0])
2121
if qc_log[0] is not None:
2222
return qc_log[0]
2323

qctests/EN_increasing_depth_check.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ def test(p, parameters):
1717
# Check if the QC of this profile was already done and if not
1818
# run the QC.
1919
query = 'SELECT en_increasing_depth_check FROM ' + parameters["table"] + ' WHERE uid = ' + str(p.uid()) + ';'
20-
qc_log = main.dbinteract(query, usePostgres=parameters['postgres'])
21-
qc_log = main.unpack_row(qc_log[0], usePostgres=parameters['postgres'])
20+
qc_log = main.dbinteract(query)
21+
qc_log = main.unpack_row(qc_log[0])
2222
if qc_log[0] is not None:
2323
return qc_log[0]
2424

qctests/EN_spike_and_step_check.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
"""
1010

1111
import numpy as np
12-
import StringIO, sqlite3, psycopg2
1312
import util.main as main
1413

1514
def test(p, parameters, suspect=False):
@@ -28,10 +27,10 @@ def run_qc(p, suspect):
2827

2928
# check for pre-registered suspect tabulation, if that's what we want:
3029
if suspect:
31-
query = 'SELECT suspect FROM enspikeandstep WHERE uid = ' + str(p.uid())
30+
query = 'SELECT suspect FROM enspikeandstep WHERE uid = ' + str(p.uid()) + ';'
3231
susp = main.dbinteract(query)
3332
if len(susp) > 0:
34-
return main.parse_sqlite_row(susp[0])[0]
33+
return main.unpack_row(susp[0])[0]
3534

3635
# Define tolerances used.
3736
tolD = np.array([0, 200, 300, 500, 600])
@@ -103,6 +102,7 @@ def run_qc(p, suspect):
103102

104103
# register suspects, if computed, to db
105104
if suspect:
105+
query = "INSERT INTO enspikeandstep VALUES(?,?);"
106106
main.dbinteract(query, [p.uid(), main.pack_array(qc)] )
107107

108108
return qc

qctests/EN_stability_check.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ def test(p, parameters):
1616
# Check if the QC of this profile was already done and if not
1717
# run the QC.
1818
query = 'SELECT en_stability_check FROM ' + parameters["table"] + ' WHERE uid = ' + str(p.uid()) + ';'
19-
qc_log = main.dbinteract(query, usePostgres=parameters['postgres'])
20-
qc_log = main.unpack_row(qc_log[0], usePostgres=parameters['postgres'])
19+
qc_log = main.dbinteract(query)
20+
qc_log = main.unpack_row(qc_log[0])
2121
if qc_log[0] is not None:
2222
return qc_log[0]
2323

qctests/EN_std_lev_bkg_and_buddy_check.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import EN_stability_check
1515
import util.main as main
1616
import numpy as np
17-
import sys, pickle, StringIO
1817

1918
def test(p, parameters, allow_level_reinstating=True):
2019
"""
@@ -382,4 +381,4 @@ def get_profile_info(parameters):
382381
# Gets information about the profiles from the database.
383382

384383
query = 'SELECT uid,year,month,cruise,lat,long FROM ' + parameters['table']
385-
return main.dbinteract(query, usePostgres=parameters['postgres'])
384+
return main.dbinteract(query)

0 commit comments

Comments
 (0)