-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertImage.py
76 lines (53 loc) · 1.94 KB
/
insertImage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# insertImage.py
#
# Insert an image reference and metadata into the database
#
from parseImageMetaData import parseImageMetaData
import re
# Insert a single image reference and metadata into the DB
# Args: database cursor, string, astropy FITS header (dict)
# Returns: int
def insertImage(dbCursor, imageFileName, imageHeader):
#Parse info from the image header
imageData = parseImageMetaData(imageHeader)
#Add image to db if doesn't exist
sql = "INSERT OR IGNORE INTO images (imageFile, msb_title, telescope, instrument, target, type, start_time, end_time, exposure, filter, sensor_temp, air_temp, gain, n_reads, read_mode, focus, tel_ra, tel_dec, ra_rate, dec_rate, airmass, darkFile, flatFile, reduced, astrometry, pp_aperture) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"
imageData.insert(0, imageFileName)
dbCursor.execute(sql, imageData)
imageAdded = dbCursor.rowcount
#Add satellite to db if doesn't exist
satAdded = 0
msbRegex = re.search(r"^\d+", imageData[1])
if msbRegex is not None:
satID = msbRegex.group()
sql = "INSERT OR IGNORE INTO sats (norad_id) VALUES (?);"
dbCursor.execute(sql, [satID])
satAdded = dbCursor.rowcount
return (imageAdded, satAdded)
# from astropy.io import fits
# # Open a FITS file and extract data
# # Args: filename = string
# # Return: metadata dictionary, image data array
# def openFITS(filename):
# hdul = fits.open(filename)
# header = hdul[0].header
# data = hdul[0].data
# hdul.close()
# return header, data
# file = 'y20210203_00672_r_a.fits'
# header, data = openFITS(file)
# import sqlite3
# databaseFileName = "test.db"
# # Open database file (will create new one if not exist)
# try:
# print("Opening database...")
# conn = sqlite3.connect(databaseFileName)
# except Exception as e:
# print("Failed!")
# print(e)
# exit()
# print("Success!")
# cursor = conn.cursor()
# temp = insertImage(cursor, file, header)
# print(temp)
# conn.commit()