~jstys-z/helioviewer.org/client5

« back to all changes in this revision

Viewing changes to api/scripts/find_gaps.py

  • Committer: Keith Hughitt
  • Date: 2012-06-12 20:18:46 UTC
  • Revision ID: keith.hughitt@gmail.com-20120612201846-k3nm2g2sznpfumhc
Added latest movies page

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
#-*- coding:utf-8 -*-
 
3
"""
 
4
Find gaps in Helioviewer data archive.
 
5
 
 
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.
 
12
 
 
13
keith.hughitt@nasa.gov
 
14
Oct 26, 2011
 
15
"""
 
16
import sys
 
17
import getpass
 
18
import csv
 
19
import MySQLdb
 
20
 
 
21
def main():
 
22
    """Main"""
 
23
    cursor = get_dbcursor()
 
24
    datasources = get_datasources(cursor)
 
25
    
 
26
    for source in datasources:
 
27
        sourceid = source[0]
 
28
        inst = source[3]
 
29
        meas = source[5]
 
30
 
 
31
        print "Processing %s %s" % (inst, meas)
 
32
        
 
33
        if meas == "4500":
 
34
            gap_size = 3601
 
35
        else:
 
36
            gap_size = 60
 
37
 
 
38
        create_temp_table(cursor, sourceid)
 
39
        gaps = query_temp_table(cursor, gap_size=gap_size)
 
40
        
 
41
        output_file = "%s_%s.csv" % (inst, meas)
 
42
        c = csv.writer(open(output_file,"wb"))
 
43
        c.writerow(("start", "end", "elapsed (s)"))
 
44
        c.writerows(gaps)
 
45
        
 
46
        delete_temp_table(cursor)
 
47
        
 
48
def create_temp_table(cursor, sourceid):
 
49
    """Creates a temporary table used to find gaps"""
 
50
    cursor.execute(
 
51
    """CREATE TABLE t (id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY)
 
52
        SELECT date 
 
53
        FROM images 
 
54
        WHERE sourceId=%d AND date > '2011-02-01 00:00:00' 
 
55
        ORDER BY date;""" % sourceid)
 
56
        
 
57
def query_temp_table(cursor, gap_size=60):
 
58
    """Queries the temporary table for gaps"""
 
59
    sql = \
 
60
    """SELECT a.date as start,
 
61
              b.date as end, 
 
62
             (UNIX_TIMESTAMP(b.date) - UNIX_TIMESTAMP(a.date)) AS gap
 
63
       FROM t AS a
 
64
       JOIN t AS b  ON b.id = a.id + 1
 
65
       HAVING gap >= %d;""" % gap_size
 
66
       
 
67
    cursor.execute(sql)
 
68
    return cursor.fetchall()
 
69
    
 
70
def delete_temp_table(cursor):
 
71
    """Removes the temporary table"""
 
72
    cursor.execute("DROP TABLE t")
 
73
    
 
74
def get_datasources(cursor, obs="SDO"):
 
75
    """Retrieves a list of the known datasources"""
 
76
    sql = \
 
77
    """ SELECT
 
78
            datasources.id as id,
 
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
 
84
        FROM datasources
 
85
            LEFT JOIN observatories
 
86
            ON datasources.observatoryId=observatories.id 
 
87
            LEFT JOIN instruments
 
88
            ON datasources.instrumentId=instruments.id 
 
89
            LEFT JOIN detectors
 
90
            ON datasources.detectorId=detectors.id 
 
91
            LEFT JOIN measurements
 
92
            ON datasources.measurementId=measurements.id
 
93
        WHERE observatories.name='%s';""" % obs
 
94
 
 
95
    cursor.execute(sql)
 
96
    return cursor.fetchall()
 
97
            
 
98
def get_dbcursor():
 
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()
 
102
 
 
103
    db = MySQLdb.connect(host="localhost", db=dbname, user=dbuser, 
 
104
                         passwd=dbpass)
 
105
 
 
106
    db.autocommit(True)
 
107
    return db.cursor()
 
108
    
 
109
def get_dbinfo():
 
110
    """Gets database type and administrator login information"""
 
111
    while True:
 
112
        dbname = raw_input("    Database [helioviewer]: ") or "helioviewer"
 
113
        dbuser = raw_input("    Username [helioviewer]: ") or "helioviewer"
 
114
        dbpass = getpass.getpass("    Password: ")
 
115
 
 
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.")
 
119
        else:
 
120
            return dbname, dbuser,dbpass
 
121
 
 
122
def check_db_info(dbname, dbuser, dbpass):
 
123
    """Validate database login information"""
 
124
    try:
 
125
        db = MySQLdb.connect(db=dbname, user=dbuser, passwd=dbpass)
 
126
    except MySQLdb.Error as e:
 
127
        print(e)
 
128
        return False
 
129
 
 
130
    db.close()
 
131
    return True
 
132
 
 
133
if __name__ == '__main__':
 
134
    sys.exit(main())
 
135