4
Find gaps in Helioviewer data archive.
6
Works by creating a temporary table with the images sorted by date, and then
7
finding all instances where the difference in timestamps from one image to the
8
next is greater than some specified amount. This is primarily useful for finding
9
gaps in SDO data for which a complete list of the expected files is not easily
10
computable. For cases like SOHO and STEREO a simpler approach is to get a
11
complete list of all files available and compare it to what is found in the db.
13
keith.hughitt@nasa.gov
23
cursor = get_dbcursor()
24
datasources = get_datasources(cursor)
26
for source in datasources:
31
print "Processing %s %s" % (inst, meas)
38
create_temp_table(cursor, sourceid)
39
gaps = query_temp_table(cursor, gap_size=gap_size)
41
output_file = "%s_%s.csv" % (inst, meas)
42
c = csv.writer(open(output_file,"wb"))
43
c.writerow(("start", "end", "elapsed (s)"))
46
delete_temp_table(cursor)
48
def create_temp_table(cursor, sourceid):
49
"""Creates a temporary table used to find gaps"""
51
"""CREATE TABLE t (id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY)
54
WHERE sourceId=%d AND date > '2011-02-01 00:00:00'
55
ORDER BY date;""" % sourceid)
57
def query_temp_table(cursor, gap_size=60):
58
"""Queries the temporary table for gaps"""
60
"""SELECT a.date as start,
62
(UNIX_TIMESTAMP(b.date) - UNIX_TIMESTAMP(a.date)) AS gap
64
JOIN t AS b ON b.id = a.id + 1
65
HAVING gap >= %d;""" % gap_size
68
return cursor.fetchall()
70
def delete_temp_table(cursor):
71
"""Removes the temporary table"""
72
cursor.execute("DROP TABLE t")
74
def get_datasources(cursor, obs="SDO"):
75
"""Retrieves a list of the known datasources"""
79
datasources.enabled as enabled,
80
observatories.name as observatory,
81
instruments.name as instrument,
82
detectors.name as detector,
83
measurements.name as measurement
85
LEFT JOIN observatories
86
ON datasources.observatoryId=observatories.id
88
ON datasources.instrumentId=instruments.id
90
ON datasources.detectorId=detectors.id
91
LEFT JOIN measurements
92
ON datasources.measurementId=measurements.id
93
WHERE observatories.name='%s';""" % obs
96
return cursor.fetchall()
99
"""Prompts the user for database info and returns a database cursor"""
100
print("Please enter existing database login information:")
101
dbname, dbuser, dbpass = get_dbinfo()
103
db = MySQLdb.connect(host="localhost", db=dbname, user=dbuser,
110
"""Gets database type and administrator login information"""
112
dbname = raw_input(" Database [helioviewer]: ") or "helioviewer"
113
dbuser = raw_input(" Username [helioviewer]: ") or "helioviewer"
114
dbpass = getpass.getpass(" Password: ")
116
if not check_db_info(dbname, dbuser, dbpass):
117
print("Unable to connect to the database. Please check your "
118
"login information and try again.")
120
return dbname, dbuser,dbpass
122
def check_db_info(dbname, dbuser, dbpass):
123
"""Validate database login information"""
125
db = MySQLdb.connect(db=dbname, user=dbuser, passwd=dbpass)
126
except MySQLdb.Error as e:
133
if __name__ == '__main__':