Skip to content

Commit

Permalink
improved python2/3 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Mommert committed Apr 13, 2017
1 parent a079e9e commit 49be8bb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
30 changes: 22 additions & 8 deletions catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
#import urllib.request, urllib.error, urllib.parse
import time
import sqlite3 as sql

# translates numpy datatypes to sql-readable datatypes
sql.register_adapter(numpy.float64, float)
sql.register_adapter(numpy.float32, float)
sql.register_adapter(numpy.int64, int)
sql.register_adapter(numpy.int32, int)

try:
from scipy import spatial
except ImportError:
Expand Down Expand Up @@ -719,11 +726,13 @@ def write_database (self, filename):
if type(self.data[key][0]) == numpy.float32 \
or type(self.data[key][0]) == numpy.float64:
table_cmd += "'%s' REAL" % db_key
if type(self.data[key][0]) == numpy.int16 \
elif type(self.data[key][0]) == numpy.int16 \
or type(self.data[key][0]) == numpy.int32:
table_cmd += "'%s' INTEGER" % db_key
if type(self.data[key][0]) == numpy.string_:
elif type(self.data[key][0]) == numpy.string_:
table_cmd += "'%s' TEXT" % db_key
else:
print('unknown data type: ' + type(self.data[key][0]))
if key_idx < len(self.fields)-1:
table_cmd += ", "

Expand All @@ -733,6 +742,7 @@ def write_database (self, filename):
# create a data array in which data types are converted to SQL types
sqltypes = {numpy.float32:numpy.float64, numpy.float64:numpy.float64,
numpy.int16:numpy.int64, numpy.int32:numpy.int64}

data_cols = [self.data[key].astype(sqltypes[type(self.data[key][0])]) \
for key in self.fields]
data = [[data_cols[j][i] for j in range(len(data_cols))] \
Expand Down Expand Up @@ -772,23 +782,27 @@ def read_database (self, filename):
db.execute("SELECT * FROM header")
rows = db.fetchall()

self.catalogname = rows[0][0].encode('ascii')
self.origin = rows[0][1].encode('ascii')
self.history = rows[0][2].encode('ascii')
self.magsys = rows[0][3].encode('ascii')
self.catalogname = rows[0][0]#.decode('utf-8')
self.origin = rows[0][1]#.decode('utf-8')
self.history = rows[0][2]#.decode('utf-8')
self.magsys = rows[0][3]#.decode('utf-8')
self.obstime[0] = rows[0][4]
self.obstime[1] = rows[0][5]
self.obj = rows[0][6].encode('ascii')
self.obj = rows[0][6]#.decode('utf-8')

# query database sources
db.execute("SELECT * FROM data")
rows = db.fetchall()

# read in field names and types
fieldnames, types = [], []
type_dict = {float:numpy.float64, int:numpy.int64, str:numpy.string_}
for key_idx, key in enumerate(db.description):
fieldnames.append(key[0])
# if isinstance(rows[0][key_idx], bytes):
# continue
# # print(rows[0][key_idx])
# # rows[0][key_idx] = rows[0][key_idx].decode('utf-8')
types.append(type_dict[type(rows[0][key_idx])])

# read in data in FITS_rec structure by creating a
Expand Down
12 changes: 12 additions & 0 deletions diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,10 @@ def add_results(data):

data['lightcurveplots'] = {}
for target in data['targetnames']:

if sys.version_info < (3, 0):
target = str(target)

logging.info('create lightcurve plot for %s' % target)
plt.plot()
plt.title(target)
Expand All @@ -775,6 +779,10 @@ def add_results(data):
data['gifs'] = {}
boxsize = 300 # thumbnail boxsize
for target in data['targetnames']:

if sys.version_info < (3, 0):
target = str(target)

data['thumbnailplots'][target] = []
for dat in data[target]:
for fitsfilename in ['.fits', '.fit']:
Expand Down Expand Up @@ -915,6 +923,10 @@ def add_results(data):
### create results website for each target
data['resultswebsites'] = {}
for target in data['targetnames']:

if sys.version_info < (3, 0):
target = str(target)

html = "<H2>%s - Photometric Results</H2>\n" % target
html += "<P><IMG SRC=\"%s\">\n" % \
data['lightcurveplots'][target].split('.diagnostics/')[1]
Expand Down
8 changes: 6 additions & 2 deletions pp_distill.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def fixed_targets(fixed_targets_file, catalogs, display=True):
objects = []
for obj in fixed_targets:
for cat_idx, cat in enumerate(catalogs):
objects.append({'ident': obj['name'],
objects.append({'ident': obj['name'].decode('utf-8'),
'obsdate.jd': cat.obstime[0],
'cat_idx' : cat_idx,
'ra.deg' : obj['ra'],
Expand Down Expand Up @@ -339,7 +339,7 @@ def distill(catalogs, man_targetname, offset, fixed_targets_file, posfile,
output = {}

### read in database files (if necessary)
if type(catalogs[0]) == str:
if isinstance(catalogs[0], str):
filenames = catalogs[:]
catalogs = []
for filename in filenames:
Expand Down Expand Up @@ -493,10 +493,14 @@ def distill(catalogs, man_targetname, offset, fixed_targets_file, posfile,

for target in targetnames:

if sys.version_info < (3, 0):
target = str(target)

output[target] = []

if display:
print('write photometry results for %s' % target)

outf = open('photometry_%s.dat' %
target.translate(_pp_conf.target2filename), 'w')
outf.write('# filename julian_date '
Expand Down

0 comments on commit 49be8bb

Please sign in to comment.