def make_cpd(self): import pylab as plt import numpy as np from datasource import DataSource wifsip = DataSource(database='wifsip', user='******', host='pina.aip.de') query = """SELECT bv, period, theta FROM ngc1647stars WHERE vmag>4.762*bv + 10.4762 and theta>0.5;""" data = wifsip.query(query) bv = np.array([d[0] for d in data]) period = np.array([d[1] for d in data]) theta = np.array([d[2] for d in data]) query = """SELECT bv, period, theta FROM ngc1647stars WHERE vmag<4.762*bv + 10.4762 AND theta>0.5;""" data = wifsip.query(query) wifsip.close() bv_ms = np.array([d[0] for d in data]) period_ms = np.array([d[1] for d in data]) theta_ms = np.array([d[2] for d in data]) import gyroage bv170 = np.linspace(0.5, 1.6, num=20) P = gyroage.gyroperiod(bv170, 170.0) P01 = gyroage.gyroperiod(bv170, 170.0, P0=0.1) P33 = gyroage.gyroperiod(bv170, 170.0, P0=3.3) plt.plot(bv170, P, color='r') plt.plot(bv170, P01, color='r', linestyle='dashed') plt.plot(bv170, P33, color='r', linestyle='dashed') plt.scatter(bv - self.ebv, period, s=(1.0 - theta) * 40., edgecolor='none', alpha=0.5) plt.scatter(bv_ms - self.ebv, period_ms, s=(1.0 - theta_ms) * 40., edgecolor='none', alpha=0.75, facecolor='green') plt.xlabel('(B - V)$_0$') plt.ylabel('period [days]') plt.ylim(0.0, 10.0) plt.xlim(0.0, 2.2) plt.grid() plt.savefig('/work1/jwe/Dropbox/NGC1647/plots/ngc1647cpd.pdf') plt.show() plt.close()
class Ppmxl(dict): ''' Class to inference the PPMXL table in the wifsip database on pina ''' def __init__(self, param): ''' Constructor: initialize the database and query it according the parameter, if a string is given, then we assume, it is the name, if it is a tuple, then we take them as coordinates ''' from datasource import DataSource self.corot = DataSource(database='wifsip', user='******', host='pina.aip.de') if type(param) is str: try: values = self._byname(param)[0] except IndexError: values = [None, None, None, None, None, None, None, None, None, None] if type(param) is tuple: try: values = self._bycoord(param)[0] except IndexError: values = [None, None, None, None, None, None, None, None, None, None] keys = ['id', 'ra', 'dec', 'pmra', 'pmde', 'jmag', 'hmag', 'kmag', 'b1mag', 'b2mag', 'r1mag', 'r2mag', 'imag', 'smags', 'no', 'fl', 'coord'] for key, value in zip(keys, values): if key == 'coord': # we want to return a tuple vals = value.strip('()').split(',') self[key] = (float(vals[0]), float(vals[1])) else: self[key] = value def _byname(self, name): """query the table by name""" query = """SELECT * FROM ppmxl WHERE id = '%s';""" % name result = self.corot.query(query) return result def _bycoord(self, coord): """query the table by coordinate""" query = """SELECT * FROM ppmxl WHERE circle(coord,0.0006) @> circle(point(%f,%f),0) LIMIT 1;""" % coord result = self.corot.query(query) return result
class TwoMass(dict): ''' Class to inference the TwoMass table in the corot database on pina ''' def __init__(self, param): ''' Constructor: initialize the database and query it according the parameter, if a string is given, then we assume, it is the name, if it is a tuple, then we take them as coordinates ''' from datasource import DataSource self.corot = DataSource(database='corot', user='******', host='pina.aip.de') if type(param) is str: try: values = self._byname(param)[0] except IndexError: values = [None,None,None,None,None,None,None,None,None,None] if type(param) is tuple: try: values = self._bycoord(param)[0] except IndexError: values = [None,None,None,None,None,None,None,None,None,None] keys = ['twomass', 'raj2000', 'dej2000', 'jmag', 'e_jmag' ,'hmag' , 'e_hmag', 'kmag' , 'e_kmag', 'coord'] for key, value in zip(keys,values): if key=='coord' and not value is None: # we want to return a tuple vals = value.strip('()').split(',') self[key] = (float(vals[0]),float(vals[1])) else: self[key] = value def _byname(self, name): """query the table by name""" query = """SELECT * FROM twomass WHERE twomass = '%s';""" % name result = self.corot.query(query) return result def _bycoord(self, coord): """query the table by coordinate""" query = """SELECT * FROM twomass WHERE circle(coord,0.0006) @> point(%f,%f) LIMIT 1;""" % coord result = self.corot.query(query) return result
def make_cmd(self): import pylab as plt import numpy as np from datasource import DataSource wifsip = DataSource(database='wifsip', user='******', host='pina.aip.de') query = "SELECT vmag, bv FROM ngc1647stars;" data = wifsip.query(query) wifsip.close() vmag = np.array([d[0] for d in data]) bv = np.array([d[1] for d in data]) plt.scatter(bv, vmag, edgecolor='none', alpha=0.75) k = 8 / (2.0 - 0.32) d = 20 - 2.0 * k x = np.linspace(0.0, 2.5, 10) y = k * x + d plt.plot(x, y, linestyle='dashed', color='k') plt.ylim(21.0, 8.0) plt.xlim(0.0, 2.2) plt.xlabel('B - V') plt.ylabel('V [mag]') plt.grid() plt.savefig('/work1/jwe/Dropbox/NGC1647/plots/ngc1647cmd.pdf') #plt.show() plt.close()
def make_cmd(self): import pylab as plt import numpy as np from datasource import DataSource wifsip = DataSource(database='wifsip', user='******', host='pina.aip.de') query = "SELECT vmag, bv FROM ngc2281stars where vmag>0.0 and bv>0.0;" data = wifsip.query(query) wifsip.close() vmag = np.array([d[0] for d in data]) bv = np.array([d[1] for d in data]) plt.scatter(bv - self.ebv, vmag, edgecolor='none', alpha=0.75) k = (21.0 - 10.5) / (1.8 - 0.0) d = 10.5 x = np.linspace(0.0, 2.5, 10) y = k * x + d plt.plot(x, y, linestyle='dashed', color='k') plt.ylim(21.0, 10.0) plt.xlim(0.0, 2.2) plt.title('NGC 2281') plt.xlabel('(B - V)$_0$') plt.ylabel('V [mag]') plt.grid() plt.savefig('/work1/jwe/Dropbox/NGC2281/plots/ngc2281cmd.pdf') #plt.show() plt.close()
def make_cmd(show=False): import numpy as np import matplotlib.pyplot as plt from datasource import DataSource wifsip = DataSource(database='wifsip', user='******', host='pina.aip.de') query = "SELECT vmag, bmag FROM ngc6940 WHERE vmag>0.0 and bmag>0.0;" data = wifsip.query(query) vmag = np.array([d[0] for d in data]) * 1.0042005546775856 + 0.24536565071778343 bmag = np.array([d[1] for d in data]) * 1.0017849466111253 + 1.3145083952754286 bv = bmag - vmag plt.scatter(bv - 0.214, vmag, edgecolor='none', alpha=0.5, s=2, c='k') plt.axvline(0.653, linestyle='--', color='y') plt.title('NGC6940 Color Magnitude Diagram E(B-V)=0.214') plt.ylim(21.0, 10.0) plt.xlim(0.0, 1.6) plt.xlabel('(B - V)$_0$') plt.ylabel('V [mag]') plt.grid() plt.minorticks_on() if show: plt.show() else: plt.savefig(config.resultpath + 'ngc6940cmd.eps') plt.savefig(config.resultpath + 'ngc6940cmd.pdf') plt.close()
def make_cmd(): import pylab as plt import numpy as np from datasource import DataSource wifsip = DataSource(database='wifsip', user='******', host='pina.aip.de') query = "UPDATE m48stars SET bv = (bmag-vmag)+(0.761837-0.060177);" wifsip.execute(query) query = "SELECT vmag+0.060177, bv FROM m48stars WHERE NOT bv is NULL and nv>1 and nb>1;" data = wifsip.query(query) wifsip.close() vmag = np.array([d[0] for d in data]) bv = np.array([d[1] for d in data]) plt.scatter(bv, vmag, edgecolor='none', alpha=0.75, s=4, c='k') plt.ylim(22.0, 7.0) plt.xlim(-0.2, 2.0) plt.xlabel('B - V') plt.ylabel('V [mag]') plt.title('M 48') plt.grid() plt.savefig('/work2/jwe/m48/m48cmd.pdf') #plt.show() plt.close()
def getphot(self, ra, dec, filtercol): from datasource import DataSource wifsip = DataSource(database=config.dbname, user=config.dbuser, host=config.dbhost, dictcursor=True) params = {'filtercol': filtercol, 'ra': ra, 'dec': dec} #'IC 4756 v2 %% uvby' query = """SELECT mag_auto, zeropnt, expt, flux_auto FROM phot, frames WHERE object like 'M 67%%' AND phot.objid = frames.objid AND filter='%(filtercol)s' AND circle(phot.coord,0.0)<@circle(point(%(ra).11f,%(dec).11f),0.6/3600.0) AND flags=0""" % params data = wifsip.query(query) zeropnt = np.array([d['zeropnt'] for d in data]) mag_isocor = np.array([d['mag_auto'] for d in data]) expt = np.array([d['expt'] for d in data]) flux_auto = np.array([d['flux_auto'] for d in data]) if len(mag_isocor) == 0: return np.nan, np.nan, np.nan #mags = mag_isocor-zeropnt mags = -2.5 * np.log10(flux_auto / expt) valid = abs(mags - np.mean(mags)) < 2.0 * np.std(mags) mags = np.compress(valid, mags) mag = np.mean(mags) return mag, np.std(mags), len(mags)
def plot_cpd(self): ''' create plot for Heraeus Talk ''' from datasource import DataSource wifsip = DataSource(database='wifsip', user='******', host='oldpina.aip.de') query = """SELECT bv, period FROM m48stars WHERE good;""" data = wifsip.query(query) bv = np.array([d[0] for d in data]) period = np.array([d[1] for d in data]) import gyroage from functions import logspace bv360 = logspace(0.5, 2.0, num=100) #P = gyroage.gyroperiod(bv360, 360.0, version=2007) P, pc = gyroage.gyroperiod(bv360, 360.0, version=2003) plt.plot(bv360, pc, color='b', linestyle='--') plt.plot(bv360, P, color='r') plt.scatter(bv-self.ebv, period, s=1, edgecolor='none', c='k') plt.xlabel('(B - V)$_0$') plt.ylabel('period [days]') plt.ylim(0.0, 20.0) plt.xlim(0.0, 2.0) plt.grid() plt.savefig('/home/jwe/Documents/Talks/Heraeus2015/m48cpd.eps') plt.savefig('/home/jwe/Documents/Talks/Heraeus2015/m48cpd.pdf') plt.close()
def __init__(self): ''' Constructor ''' from datasource import DataSource table = DataSource(database='stella', user='******', host='pera.aip.de', dictcursor=True) columns = ['starid', 'ra', 'dec', '"b-y"', 'm1', 'c1', 'beta'] query = "SELECT %s FROM referencestars order by starid;" % ', '.join( columns) data = table.query(query) columns[3] = 'b-y' d0 = data[0] #for c in columns: print c dtypes = ['|S11', 'f4', 'f4', 'f4', 'f4', 'f4', 'f4'] for c in columns: dtypes.append(type(d0[c])) arraydata = [] for star in data: arraydata.append(tuple(star)) self.stars = np.array(arraydata, dtype=zip(columns, dtypes))
def lightcurve_fromdb(self): """ extract a single lightcurve from the database and return epoch (hjd), magnitude and error """ from datasource import DataSource import numpy as np wifsip = DataSource(database='wifsip', user='******', host='pina.aip.de') query = """SELECT frames.hjd, phot.mag_isocor, phot.magerr_isocor FROM frames, matched, phot WHERE id LIKE '%s' AND filter LIKE 'rp' AND frames.objid = matched.objid AND (phot.objid,phot.star) = (matched.objid,matched.star) ORDER BY hjd;""" % (self.id) # AND hjd>2455473.5 AND hjd<2455477 AND frames.good # AND frames.good # AND hjd>2455470 AND hjd<2455510 data = wifsip.query(query) wifsip.close() self.hjd = np.array([d[0] for d in data]) self.mag = np.array([d[1] for d in data]) self.err = np.array([d[2] for d in data]) self.hjd -= self.hjd[0]
def lightcurve_fromdb(self, starid): """ extract a single lightcurve from the database and return epoch (hjd), magnitude and error """ from datasource import DataSource import numpy as np import log wifsip = DataSource(database='wifsip', user='******', host='pina.aip.de') query = """SELECT frames.hjd, phot.mag_auto, phot.magerr_auto, phot.flags FROM frames, matched, phot WHERE id LIKE '%s' AND filter LIKE 'rp' AND frames.objid = matched.objid AND (phot.objid,phot.star) = (matched.objid,matched.star) ORDER BY hjd;""" % (starid) # AND hjd>2455473.5 AND hjd<2455477 AND frames.good log.log('/work1/jwe/NGC1647/analysis.log', 'fetching star %s' % starid) # AND frames.good # AND hjd>2455470 AND hjd<2455510 data = wifsip.query(query) wifsip.close() hjd = np.array([d[0] for d in data]) mag = np.array([d[1] for d in data]) err = np.array([d[2] for d in data]) return (hjd, mag, err)
def buildtable(self): """ builds the table of stars """ import numpy as np epochs = len(self.objids) stars = len(self.stars) if fileexists('/work2/jwe/NGC2281/' + self.filter + 'array.npy'): m = np.load('/work2/jwe/NGC2281/' + self.filter + 'array.npy') else: from datasource import DataSource from framecal import FrameCal fc = FrameCal(self.filter) m = np.zeros([epochs, stars]) # objid is specific to a filter so we only need to query the objid wifsip = DataSource(host='pina', database='wifsip', user='******') for objid in self.objids: k = self.objids.index(objid) print k, epochs, objid, query = """SELECT matched.id, phot.mag_auto, phot.mag_errauto FROM phot, matched WHERE phot.objid like '%s' AND (matched.objid,matched.star) = (phot.objid,phot.star) AND phot.flags = 0;""" % objid result = wifsip.query(query) starids = [s[0] for s in result] mags = [s[1] for s in result] err = [s[2] for s in result] slope, intercept, _, _, _ = fc.calframe(objid) print len(mags) for starid in starids: i = self.stars.index(starid) m[k, i] = mags[starids.index(starid)] * slope + intercept np.save('/work2/jwe/NGC2281/' + self.filter + 'array.npy', m) wifsip.close() i = np.where(m == 0.0) m[i] = np.nan from scipy import stats # calculate the observed average for the stars avg = stats.nanmean(m, axis=0) for k in range(epochs): print k, epochs, self.objids[k] # calculate the mean of offsets off = stats.nanmedian(m[k, :] - avg) # correct epoch for mean of offsets m[k, :] += off # calculate new corrected means avg = stats.nanmean(m, axis=0) std = stats.nanstd(m, axis=0) for i in range(len(self.stars)): print self.stars[i], avg[i], std[i]
def __init__(self, objid): self.objid = objid table = DataSource(database='stella', user='******', host='pera.aip.de', dictcursor=True) query = """SELECT * FROM frames WHERE objid = '%s';""" % objid result = table.query(query)[0] print(result.keys()) self.exposure = result['expt']
def getcoords(objid): table = DataSource(database='stella', user='******', host='pera.aip.de') query = """SELECT alphawin_j2000, deltawin_j2000, flux_auto FROM phot WHERE objid = '%s' ORDER BY star;""" % objid result = table.query(query) ra = np.array([r[0] for r in result]) dec = np.array([r[1] for r in result]) flux = np.array([r[2] for r in result]) return ra, dec, flux
def getframes(obj, targetdir='/work2/jwe/stella/wifsip/', filtercol='V', conditions=None, imcopy=False, listonly=False): """ :param obj: name of the object to retrieve :param targetdir: target directory, where the files are copied to :param filtercol: Filter color to choose e.g. 'V' or 'B' or 'I' :param conditions: additional conditions like airmass limit, background limit (Moon!) :param imcopy: uses imcopy to convert fitz files to fits files :param listonly: does not scp, just lists the files :return: nothing """ params = {'object': obj, 'filtercol': filtercol} wifsip = DataSource(host='pera', database='stella', user='******') query = """SELECT path, filename, fwhm, backgrnd, airmass FROM frames, science WHERE (filter LIKE '%(filtercol)s') AND object LIKE '%(object)s' AND frames.objid = science.objid""" % params if 'fwhm' in conditions: params['fwhm'] = conditions['fwhm'] query += '\nAND fwhm_image < %(fwhm)f ' % params if 'background' in conditions: params['background'] = conditions['background'] query += '\nAND backgrnd < %(background)f ' % params if 'airmass' in conditions: params['airmass'] = conditions['airmass'] query += '\nAND airmass < %(airmass)f ' % params if 'expt' in conditions: params['expt'] = conditions['expt'] query += '\nAND expt = %(expt)f ' % params query = query + '\nORDER by frames.objid;' tab = wifsip.query(query) print(len(tab), 'files') if not os.path.exists(targetdir): os.makedirs(targetdir) for sciencepath, filename, fwhm, backgrnd, airmass in tab: # old path listed in the database: # /stella/wifsip/reduced/20160904/science20160903A-0057EXP0002 # new path on pina: # /stella/home/stella/wifsip/reduced/20160904/science20160903A-0057EXP0001.fitz newpath = sciencepath.replace('/stella/wifsip/reduced/', '/stella/home/stella/wifsip/reduced/') print("%s %.1f %.1f %.2f" % (os.path.join(newpath, filename + '.fitz'), fwhm, backgrnd, airmass)) # if listonly is set, we break the loop here if listonly: continue call(['scp', '[email protected]:' + os.path.join(newpath, filename + '.fitz'), targetdir]) source = '%s/%s.fitz[1]' % (targetdir, filename) target = '%s/%s.fits' % (targetdir, filename) if imcopy: call(['imcopy', source, target])
def __init__(self): ''' Constructor ''' wifsip = DataSource(database='stella', user='******', host='pera.aip.de') query = """SELECT distinct object FROM frames WHERE object LIKE 'NGC%rot NW' OR object LIKE 'M%rot NW' OR object LIKE 'NGC%rot' """ result = wifsip.query(query) self.objects = [r[0].rstrip(' NW') for r in result] self.data = [] for obj in self.objects: query = """select distinct floor(jd) "startdate", expt "duration" from frames where object like '%s%%' order by startdate """ % obj result = wifsip.query(query) startdates = np.array([r[0] for r in result]) # durations = np.array([timedelta(seconds=r[1]) for r in result]) durations = np.array([r[1] for r in result]) rec = { 'object': obj, 'startdates': startdates, 'durations': durations } self.data.append(rec) # self.startdates = np.array([r[1] for r in result]) # self.enddates = np.array([r[2] for r in result]) print('%d records' % len(result))
def __init__(self): ''' Constructor ''' from datasource import DataSource wifsip = DataSource(database=config.dbname, user=config.dbuser, host=config.dbhost, dictcursor=True) query = """SELECT bmag, vmag, period, good, member FROM ngc2236 WHERE NOT bv is NULL AND NOT vmag is NULL""" self.data = wifsip.query(query) self.dm = 14.0
def refstars(): from datasource import DataSource table = DataSource(database='stella', user='******', host='pera.aip.de', dictcursor=True) query = "SELECT * FROM referencestars order by starid;" data = table.query(query) columns = data[0].keys() dtypes = [type(d) for d in data[0].values()] arraydata = [] for star in data: arraydata.append(tuple(star)) return np.array(arraydata, dtype=zip(columns, dtypes))
def phot(self): """ builds the table of stars """ import numpy as np from scipy import stats epochs = len(self.objids) stars = len(self.stars) from datasource import DataSource m = np.zeros([epochs, stars]) # objid is specific to a filter so we only need to query the objid wifsip = DataSource(host='pina', database='wifsip', user='******') for star in self.stars: print star, query = """SELECT mag_auto, magerr_auto FROM frames, phot, matched WHERE matched.id like '%s' AND frames.filter like '%s' AND frames.objid = phot.objid AND (matched.objid,matched.star) = (phot.objid,phot.star) AND phot.flags = 0 AND magerr_auto > 0.0;""" % (star, self.filter) result = wifsip.query(query) mags = np.array([s[0] for s in result]) err = np.array([s[1] for s in result]) m = stats.nanmean(mags) s = stats.nanstd(mags) merr = stats.nanmean(err) stderr = stats.nanstd(err) #print mags #print err if len(mags) > 1: print '%4d %.3f %.3f %.3f %.3f' % (len(mags), m, s, merr, stderr), mags = mags[err <= merr + stderr] err = err[err <= merr + stderr] avg = np.average(mags, weights=1. / err) std = np.sqrt(np.average(abs(mags - avg)**2, weights=1. / err)) #std = np.std(mags) print '%4d %.3f %.3f' % (len(mags), avg, std) self.update_star(wifsip, star, avg, std, len(mags)) else: print 'none (%.3f, %.3f)' % (m, s) wifsip.close()
def getframes(targetdir='/work2/jwe/stella/wifsip/ngc1647'): from datasource import DataSource from subprocess import call wifsip = DataSource(host='pina', database='wifsip', user='******') query = """select path, filename from frames, science where filter like 'rp' and object like 'NGC 1647 field 1' and expt = 62.5 and frames.objid = science.objid""" tab = wifsip.query(query) for path, filename in tab: print path + '/' + filename call([ 'scp', '[email protected]:' + path + '/' + filename + '.fitz ', targetdir ])
def listframes(obj, fields=['objid']): from datasource import DataSource wifsip = DataSource(host='pera', database='stella', user='******') params = {'object': obj, 'fieldsstring': ', '.join(fields)} query = """SELECT %(fieldsstring)s FROM frames WHERE object LIKE '%(object)s' ORDER by %(fieldsstring)s """ % params tab = wifsip.query(query) print('#', '\t'.join(fields)) for t in tab: print('\t'.join([str(ti) for ti in t])) print('#', len(tab), 'records')
def getobjid(objid, targetdir=os.getcwd(), imcopy=False, listonly=False): wifsip = DataSource(host='pera', database='stella', user='******') query = "SELECT path, filename "\ " FROM science "\ " WHERE objid = '%s'" % objid path, filename = wifsip.query(query)[0] path = path.replace('stella/wifsip/reduced/','/stella/home/stella/wifsip/reduced/') # /stella/home/stella/wifsip/reduced/20140725/ print("%s/%s" % (path, filename)) scpsource = os.path.join(path,filename + '.fitz') if not listonly: call(['scp', '[email protected]:' + scpsource, targetdir]) source = os.path.join(targetdir, filename+'.fitz[1]') target = os.path.join(targetdir, filename+'.fits') if imcopy: call(['/home/jwe/bin/imcopy', source, target])
def make_cpd(self): import pylab as plt import numpy as np from datasource import DataSource wifsip = DataSource(database='wifsip', user='******', host='pina.aip.de') query = """SELECT bv, period, theta FROM ngc2281stars WHERE bv >-1.0 AND period>0 and vmag < 5.8333*(bv-0.09)+10.5;""" data = wifsip.query(query) bv = np.array([d[0] for d in data]) period = np.array([d[1] for d in data]) theta = np.array([d[2] for d in data]) wifsip.close() import gyroage bv358 = np.linspace(0.5, 1.6, num=40) #P = gyroage.gyroperiod(bv358, 358.1) #P01 = gyroage.gyroperiod(bv358, 358.1, P0=0.1) #P33 = gyroage.gyroperiod(bv358, 358.1, P0=3.3) PI, PC = gyroage.gyroperiod(bv358, 358.1) plt.plot(bv358, PI, color='r') plt.plot(bv358, PC, color='r', linestyle='dashed') plt.scatter(bv - self.ebv, period, s=40. * (1.0 - theta)**2, edgecolor='none', alpha=0.75) plt.title('NGC 2281') plt.xlabel('(B - V)$_0$') plt.ylabel('period [days]') plt.ylim(0.0, 20.0) plt.xlim(0.0, 2.2) plt.grid() plt.savefig('/work1/jwe/Dropbox/NGC2281/plots/ngc2281cpd.pdf') #plt.show() plt.close()
def getstars(self): """ build up a list of stars, where we do not have periods yet """ from datasource import DataSource import log wifsip = DataSource(host='pina', database='wifsip', user='******') query = "select id from ngc1647stars where period is NULL;" #query = """SELECT id #FROM ngc1647stars #WHERE vmag<4.762*bv + 10.4762 #AND period is NULL;""" log.log('/work1/jwe/NGC1647/analysis.log', 'fetching stars ...') result = wifsip.query(query) log.log('/work1/jwe/NGC1647/analysis.log', '... %d stars found' % len(result)) self.stars = [s[0] for s in result]
def get_frames(self): if fileexists('/work2/jwe/NGC2281/' + self.filter + 'frames.txt'): self.objids = loadfromfile('/work2/jwe/NGC2281/' + self.filter + 'frames.txt') print len(self.objids), 'frames' return from datasource import DataSource wifsip = DataSource(host='pina', database='wifsip', user='******') query = """SELECT frames.objid FROM frames WHERE frames.object like 'NGC 2281 BVI %%' AND frames.filter like '%s';""" % self.filter result = wifsip.query(query) self.objids = [s[0] for s in result] savetofile('/work2/jwe/NGC2281/' + self.filter + 'frames.txt', self.objids) print len(self.objids), 'frames' wifsip.close()
def update_coordinates(self): from datasource import DataSource corot = DataSource(database='corot', user='******', host='pina.aip.de') query = """SELECT corotid FROM ngc2236 WHERE NOT corotid IS NULL ORDER BY vmag;""" corotids = [c[0] for c in self.wifsip.query(query)] for corotid in corotids: query = """SELECT alpha, delta FROM corot WHERE corotid = %d;""" % corotid ra, dec = corot.query(query)[0] print corotid,ra,dec record = dict(zip(['corotid','ra','dec'], [corotid,ra,dec])) query = """UPDATE ngc2236 SET ra = %(ra)f, dec = %(dec)f, coord = point(%(ra)f,%(dec)f) WHERE corotid = %(corotid)d;""" % record self.wifsip.execute(query, commit=False) self.wifsip.commit()
def get_stars(self): if fileexists('/work2/jwe/NGC2281/' + self.filter + 'stars.txt'): self.stars = loadfromfile('/work2/jwe/NGC2281/' + self.filter + 'stars.txt') print len(self.stars), 'stars' return from datasource import DataSource wifsip = DataSource(host='pina', database='wifsip', user='******') query = """SELECT distinct matched.id FROM frames, phot, matched WHERE frames.object like 'NGC 2281 BVI %%' AND frames.filter like '%s' AND frames.objid = phot.objid AND (matched.objid,matched.star) = (phot.objid,phot.star);""" % self.filter result = wifsip.query(query) self.stars = [s[0] for s in result] savetofile('/work2/jwe/NGC2281/' + self.filter + 'stars.txt', self.stars) wifsip.close() print len(self.stars), 'stars'
def __init__(self, target, frot=False): ''' Constructor ''' self.target = target wifsip = DataSource(database='stella', user='******', host='pera.aip.de') if target == 'M 48': date_limit = 'TRUE' elif target == 'NGC 6940': date_limit = "datesend > '2017-01-01'" else: date_limit = 'TRUE' if frot: expt = 6 self.survey = 'frot' else: expt = 60 self.survey = 'rot' params = { 'target': self.target, 'date_limit': date_limit, 'expt': expt, "survey": self.survey, "frot": frot } query = 'SELECT datesend "datum", 1 ' \ "FROM frames " \ "WHERE (object like '%(target)s %(survey)s NW' or object like '%(target)s %(survey)s')" \ " AND expt>%(expt)d AND filter='V'" \ " AND %(date_limit)s " \ "ORDER BY datum;" % params result = wifsip.query(query) if len(result) == 0: print("No observations found") return self.dates = [r[0] for r in result] self.count = [r[1] for r in result] todates = self.dates[1:] fromdates = self.dates[0:-1] datedeltas = [ todate - fromdate for todate, fromdate in zip(todates, fromdates) ] # find gaps larger than 10 days offsets = [0] i = 0 for i, datedelta in enumerate(datedeltas): if datedelta > datetime.timedelta(days=gap_length): offsets.append(i) offsets.append(i + 1) if i not in offsets: offsets.append(i) start = 0 end = len(self.dates) if len(offsets) == 2: self.length = self.dates[-1] - self.dates[0] start = 0 end = len(self.dates) else: self.length = self.dates[1] - self.dates[0] for o1, o2 in zip(offsets[1:], offsets[:-1]): length = self.dates[o1] - self.dates[o2] print(length.days, self.dates[o1], self.dates[o2], np.sum(self.count[o2:o1])) if length.days > self.length.days and np.sum( self.count[o2:o1]) > gap_length: self.length = length start = o2 end = o1 print(start, end) self.dates = self.dates[start:end] self.count = self.count[start:end] self.cumsum = np.cumsum(self.count)
class ClusterPlan(object): ''' classdocs ''' def __init__(self, clusterlist=None): ''' Constructor: builds list of clusters either by criteria or by list given in clusterlist ''' self.wifsip = DataSource(database='stella', user='******', host='pera.aip.de') if clusterlist is None: query = self.selectclusters() else: if type(clusterlist) == str: clusterstring = "'%s'" % clusterlist else: clusterstring = ', '.join(["'%s'" % cluster for cluster in clusterlist]) query = """SELECT name,ra,dec,diam,d,ebv,logage FROM clusters WHERE name in (%s)""" % clusterstring print(clusterlist, query) result = self.wifsip.query(query) print('%d clusters found' % len(result)) if len(result) == 0: raise ValueError('no clusters meet the criteria') self.current_year = datetime.datetime.now().year self.data = [] for r in result: rec = {} rec['name'] = r[0] rec['ra'] = float(r[1]) rec['dec'] = float(r[2]) rec['diam'] = int(r[3]) rec['d'] = int(r[4]) rec['ebv'] = float(r[5]) rec['age'] = round(10 ** float(r[6]) / 1e6, -1) # convert to Myrs self.data.append(rec) def selectclusters(self): ''' defines the criteria for the cluster selection and queries the database the result of the query is stored in the data property, which needs to be improved ''' from math import log10 criteria = {'minage': log10(125e6), 'maxage': log10(2600e6), 'maxebv': 0.31, 'maxd': 1500, 'mindec': -15.0, 'mindiam': 10, 'maxdiam': 80} query = """SELECT name,ra,dec,diam,d,ebv,logage from clusters WHERE ((diam<=%(maxdiam)d and diam>=%(mindiam)d) or diam IS NULL) AND logage>=%(minage)f and logage<=%(maxage)f AND dec>=%(mindec)f AND (name LIKE 'NGC%%' OR name LIKE 'IC%%') AND (ebv <= %(maxebv)f OR ebv IS NULL) AND (d <= %(maxd)d or d IS NULL) AND not observed order by ra""" % criteria return query def _eph2dt(self, ephemdate): """converts ephem.Date to datetime""" return datetime.datetime.strptime(str(ephemdate), "%Y/%m/%d %H:%M:%S") def plot(self): ''' plots the milkyway density map and the selected clusters on the map. ''' from milkyway import MilkyWay plt.figure(figsize=(10.69, 7.27)) mw = MilkyWay('/work2/jwe/tychomap.npy', magnitudes=True) mw.plot(show=False) ra = [d['ra'] / 15. for d in self.data] dec = [d['dec'] for d in self.data] ebv = [d['ebv'] for d in self.data] name = [d['name'] for d in self.data] d = [d['diam'] * 4 for d in self.data] plt.scatter(ra, dec, edgecolor='none', c=ebv, s=d) plt.xlim(24, 0) plt.ylim(-15, 75) plt.minorticks_on() plt.grid() plt.xlabel('R.A.') plt.ylabel('Dec.') for r, d, n in zip(ra, dec, name): plt.text(r, d, n, fontsize=8) plt.savefig('/work2/jwe/SOCS/clusterplan.pdf', papertype='a4') plt.close() # plt.show() def list(self): ''' lists the clusters that fulfill the criteria ''' for d in self.data: print('%-15s %4dpc %4dMyr E(B-V)=%.2f %2d' % (d['name'], d['d'], d['age'], d['ebv'], d['diam'])) # print self.time(d) def calc(self): """ calculate the exposure for a solar like star """ isofile = '/home/jwe/data/iso_01.000_Gyr.dat' a = np.loadtxt(isofile) iso_mass = a[:, 0] iso_mv = a[:, 5] iso_bv = a[:, 8] minbv, maxbv = 0.2, 1.45 bv = np.arange(minbv, maxbv, 0.01) # mv = np.arange(min(iso_mv), max(iso_mv), 0.1) mass = np.arange(min(iso_mass), max(iso_mass), 0.01) i = np.where((iso_bv > minbv) & (iso_bv < maxbv) & (iso_mv > 2.0)) p = np.polyfit(iso_bv[i], iso_mv[i], 5) # q = np.polyfit(iso_mv, iso_bv, 11) massmvp = np.polyfit(iso_mass, iso_mv, 12) massbvp = np.polyfit(iso_mass, iso_bv, 12) mv_int = np.polyval(massmvp, mass) bv_int = np.polyval(massbvp, mass) # p1 = [-1.33, 7.27, 0.75] print('[' + ', '.join(['%.2f' % pi for pi in p]) + ']') y = np.polyval(p, bv) print(np.polyval(p, [0.4, 1.4])) plt.scatter(iso_bv, iso_mv) plt.plot(bv, y, 'g') plt.plot(bv_int, mv_int, 'r') plt.axhline(4.83, color='y') plt.axvline(0.653, color='y') plt.xlabel('(B-V)') plt.ylabel('M$_V$') plt.ylim(plt.ylim()[::-1]) plt.grid() plt.minorticks_on() plt.show() def loadfromfile(self, filename): with open(filename, 'r') as f: lines = f.readlines() dates = [] hours = [] darkhours = [] for line in lines: split_line = line.split() date = ' '.join(split_line[0:2]) dates.append(datetime.datetime.strptime(date, "%Y/%m/%d %H:%M:%S")) hours.append(float(split_line[2])) darkhours.append(float(split_line[3])) hours = np.array(hours) darkhours = np.array(darkhours) return dates, hours, darkhours def obstime(self): plt.figure(figsize=(29.6 / 2.54, 21. / 2.54)) darkhours = np.zeros(365) for c in self.data: print(c['name']) filename = '/work2/jwe/SOCS/data/obstime %(name)s.txt' % c try: dates, hours, darkhours = self.loadfromfile(filename) except IOError: date0 = ephem.Date('%d/1/1 00:00:00' % self.current_year) hours = np.zeros(365) dates = [] for day in range(365): ephemdate = ephem.Date(date0 + day) t = self.time(c, date=ephemdate) dates.append(self._eph2dt(ephemdate)) hours[day] = t if darkhours[day] == 0.0: darkhours[day] = self.darktime(ephemdate) log(filename, '%-20.20s %5.2f %5.2f %4.2f' % (ephemdate, t, darkhours[day], t / darkhours[day])) plt.plot(dates, hours, label=c['name']) plt.plot(dates, darkhours, 'k--') plt.grid() plt.yticks(np.arange(12)) plt.ylabel('hours visible') plt.xlabel('date') xticks = [datetime.date(y, m, 1) for y in np.arange(self.current_year-1, self.current_year+1) for m in range(1, 13)] xlabels = [month.strftime('%b') for month in xticks] plt.xticks(xticks, xlabels) plt.xlim(dates[0], dates[-1]) plt.title('clusterplan obstime') plt.legend(loc=9, fontsize='small') plt.savefig('/work2/jwe/SOCS/plots/clusterplan obstime %s.pdf' % c['name']) # plt.show() def darktime(self, date): izana = ephem.Observer() if date is None: date = ephem.Date('2015/8/2 00:00:00') izana.date = date izana.lat = '28.301195' izana.lon = '-16.509209' izana.horizon = '-0:34' # kpno.elevation = 2096 sun = ephem.Sun(izana) # @UndefinedVariable # set astronomical dawn izana.horizon = '-19:00' spset = izana.previous_setting(sun) snrise = izana.next_rising(sun) darktime = (ephem.Date(snrise) - ephem.Date(spset)) * 24.0 return darktime def time(self, cluster, date=None, verbose=False): izana = ephem.Observer() if date is None: date = ephem.Date('2015/8/2 00:00:00') izana.date = date # '2015/03/19 00:00:00' izana.lat = '28.301195' izana.lon = '-16.509209' izana.horizon = '-0:34' # izana.elevation = 2096 sun = ephem.Sun(izana) # @UndefinedVariable c = SkyCoord(cluster['ra'], cluster['dec'], frame='icrs', unit=(u.deg, u.deg)) # @UndefinedVariable rastr = c.ra.to_string(unit=u.hour, sep='::') # @UndefinedVariable decstr = c.dec.to_string(unit=u.deg, sep='::') # @UndefinedVariable ephemstr = '%s,f|O,%s,%s, 5.,2000' % (cluster['name'], rastr, decstr) clusterephem = ephem.readdb(ephemstr) eventlist = [] # set airmass 2.0 izana.horizon = '30:00' clusterephem.compute(izana) cnrise = izana.next_rising(clusterephem) cnset = izana.next_setting(clusterephem) cprise = izana.previous_rising(clusterephem) cpset = izana.previous_setting(clusterephem) eventlist.append([self._eph2dt(cnrise), 'cluster rise (next)']) eventlist.append([self._eph2dt(cnset), 'cluster set (next)']) eventlist.append([self._eph2dt(cprise), 'cluster rise (prev)']) eventlist.append([self._eph2dt(cpset), 'cluster set (prev)']) # set astronomical dawn izana.horizon = '-19:00' spset = izana.previous_setting(sun) snrise = izana.next_rising(sun) eventlist.append([self._eph2dt(snrise), 'sunrise']) eventlist.append([self._eph2dt(spset), 'sunset']) eventlist.sort() sundown = False clusterup = False t0, t1 = None, None for t, e in eventlist: if verbose: print(t, e) if e == 'sunset': sundown = True if clusterup: t0 = t if e == 'sunrise': sundown = False if clusterup: t1 = t if e == 'cluster rise (next)' or \ e == 'cluster rise (prev)': clusterup = True if sundown: t0 = t if e == 'cluster set (next)' or \ e == 'cluster set (prev)': clusterup = False if sundown: t1 = t if verbose: print(t0, t1) if t0 is None and t1 is None: return 0.0 return (ephem.Date(t1) - ephem.Date(t0)) * 24.0
@author: Joerg Weingrill <*****@*****.**> ''' from datasource import DataSource def getfile(sourcefile, targetdirectory): from subprocess import call call(['scp', '[email protected]:'+sourcefile,targetdirectory]) pass def convertfile(filename): from subprocess import call call(['./ses-writetxt.py', '--textfile', filename]) if __name__ == '__main__': query = "SELECT filename FROM obs WHERE object LIKE 'HN Peg' ORDER BY dateobs;" wifsip = DataSource(database='stella', user='******', host='pera.aip.de') result = wifsip.query(query) for r in result: filename = r[0] print filename, #science20151106B-0038_botzfxsEcd.fits path = filename.lstrip('science')[:8] sourcefile = '/stella/home/stella/spectra/'+path+'/'+filename+'_botzfxsEcd.fits' targetdirectory = '/work2/jwe/Projects/HNPeg/data' print sourcefile, targetdirectory #getfile(sourcefile,targetdirectory) convertfile('/work2/jwe/Projects/HNPeg/data/'+filename+'_botzfxsEcd.fits') #/stella/home/stella/spectra/20151106 print len(result)