6 def __init__(self, dbfile=None, local=False):
7 """A small set of functions to generate, update, and read from a local SQLite database of ancillary
16 Open connection to the ancillary DB and initiate a cursor
18 conn = sqlite3.connect(self.
dbfile, timeout=30)
21 c.execute(
'''PRAGMA foreign_keys = ON''')
27 Close the DB connection, committing changes.
36 Create the ancillary DB
39 print(
"No connection to database!")
44 c.execute(
'''CREATE TABLE IF NOT EXISTS satfiles
45 (satid INTEGER PRIMARY KEY,
50 attephstat INTEGER)''')
53 c.execute(
'''CREATE TABLE IF NOT EXISTS ancfiles
54 (ancid INTEGER PRIMARY KEY,
60 c.execute(
'''CREATE TABLE IF NOT EXISTS satancinfo
64 FOREIGN KEY(satID) REFERENCES satfiles(satid),
65 FOREIGN KEY(ancID) REFERENCES ancfiles(ancid))''')
67 def insert_record(self, satfile=None, starttime=None, stoptime=None, dbstat=0,
68 ancfile=None, ancpath=None, anctype=None, atteph=False):
70 Insert record into ancillary DB
73 print(
"No connection to database!")
77 satid = self.
check_file(satfile, starttime=starttime)
78 ancid = self.
check_file(ancfile, anctype=anctype)
87 c.execute(
'INSERT INTO satfiles VALUES (NULL,?,?,?,?,?)',
88 [satfile, starttime, stoptime, inputdbstat, attephstat])
90 satid = ancDB.check_file(self, satfile, starttime=starttime)
94 c.execute(
'''UPDATE satfiles SET attephstat = ?
95 WHERE satid = ?''', [dbstat, satid])
97 c.execute(
'''UPDATE satfiles SET status = ?
98 WHERE satid = ?''', [dbstat, satid])
103 c.execute(
'INSERT INTO ancfiles VALUES (NULL,?,?,?)', [ancfile, ancpath, anctype])
105 ancid = ancDB.check_file(self, ancfile, anctype=anctype)
109 result = c.execute(
'SELECT * from satancinfo where satid = ? and ancid = ?', [satid, ancid])
110 r = result.fetchone()
113 c.execute(
'INSERT INTO satancinfo VALUES (?,?,?)', [satid, ancid, opt])
118 Deletes records from ancillary DB
119 If given a satellite filename, deletes all records associated with it
120 If given an ancillary filename and keyword anc is set true, deletes only that ancillary record
122 if self.
conn is None:
123 print(
"No connection to database!")
130 ancid = self.
check_file(filename, anctype=anctype)
131 c.execute(
'DELETE from satancinfo where ancid = ?', [ancid])
132 c.execute(
'DELETE from ancfiles where ancid = ?', [ancid])
135 satid = self.
check_file(filename, starttime=starttime)
136 ancids = conn.execute(
'select ancid from satancinfo where satid = ?', [satid])
138 c.execute(
'DELETE from satancinfo where ancid = ?', [a[0]])
139 c.execute(
'DELETE from ancfiles where ancid = ?', [a[0]])
141 c.execute(
'DELETE from satfiles where satid = ?', [satid])
147 Check the database return status.
148 DB return status bitwise values:
149 all bits off means all is well in the world
150 value of -1 means have not checked for ancfiles yet
152 bit 0 - missing one or more MET
153 bit 1 - missing one or more OZONE
158 bit 0 - predicted attitude selected
159 bit 1 - predicted ephemeris selected
160 bit 2 - no attitude found
161 bit 3 - no ephemeris found
162 bit 4 - invalid mission
165 statchk = {
'atm': 1,
'met': 1,
188 if re.search(
"\d$", anctype):
189 anctype = anctype[0:len(anctype) - 1]
190 if dbstat & statchk[anctype]:
196 def check_file(self, filename, anctype=None, starttime=None):
198 Check database for existing file, return ID if exists
200 if self.
conn is None:
201 print(
"No connection to database!")
210 query =
' '.join([
'select', id,
'from', table,
'where filename =',
'"' + filename +
'"'])
212 query =
' '.join([
'select', id,
'from', table,
'where starttime =',
'"' + starttime +
'"'])
218 query =
' '.join([
'select', id,
'from', table,
'where filename =',
'"' + filename +
'"',
" and type = ",
219 '"' + anctype +
'"'])
223 result = c.execute(query)
224 r = result.fetchone()
230 print(
'more than one entry for this starttime - this may be a problem.?')
233 def get_status(self, filename, atteph=False, starttime=None):
235 Check the stored database return status
237 if self.
conn is None:
238 print(
"No connection to database!")
244 query =
' '.join([
'select attephstat from satfiles where filename =',
'"' + filename +
'"'])
246 query =
' '.join([
'select attephstat from satfiles where starttime =',
'"' + starttime +
'"'])
249 query =
' '.join([
'select status from satfiles where filename =',
'"' + filename +
'"'])
251 query =
' '.join([
'select status from satfiles where starttime =',
'"' + starttime +
'"'])
253 result = c.execute(query)
254 r = result.fetchone()
263 return the stored file start and stop times
265 if self.
conn is None:
266 print(
"No connection to database!")
271 query =
' '.join([
'select starttime,stoptime from satfiles where filename =',
'"' + filename +
'"'])
273 query =
' '.join([
'select starttime,stoptime from satfiles where starttime =',
'"' + starttime +
'"'])
275 result = c.execute(query)
276 r = result.fetchone()
281 Return the ancillary files associated with a given input file
284 if self.
conn is None:
285 print(
"No connection to database!")
290 satID = self.
check_file(filename, starttime=starttime)
296 'SELECT a.type, a.path, a.filename from ancfiles a, satancinfo s where a.ancid = s.ancid and s.satid = ?',
300 if atteph
and not re.search(
'(att|eph)', anctype, re.IGNORECASE):
302 elif not atteph
and re.search(
'(att|eph)', anctype, re.IGNORECASE):
305 filehash[row[0]] = os.path.join(row[1], row[2])
309 if __name__ ==
"__main__":
310 db =
ancDB(dbfile=
'/tmp/testDB.sqlite.db')
314 db.insert_record(satfile=
'A2002365234500.L1A_LAC', starttime=
'2002365234500', stoptime=
'2002365235000',
315 ancfile=
'N200236518_MET_NCEPN_6h.hdf', ancpath=
'/Users/Shared/python/OCSSW_Scripts',
317 db.insert_record(satfile=
'A2002365234500.L1A_LAC', starttime=
'2002365234500', stoptime=
'2002365235000',
318 ancfile=
'N200300100_MET_NCEPN_6h.hdf', ancpath=
'/Users/Shared/python/OCSSW_Scripts',
319 anctype=
'att1', atteph=
True)
320 print(db.check_file(
'A2002365234500.L1A_LAC'))
321 print(db.check_file(
'N200236518_MET_NCEPN_6h.hdf', anctype=
'met1'))
322 files = db.get_ancfiles(
'A2002365234500.L1A_LAC', atteph=
True)
324 db.delete_record(filename=
'A2002365234500.L1A_LAC')
325 print(db.check_file(
'N200236518_MET_NCEPN_6h.hdf', anctype=
'met1'))