Example #1
0
class CoRoTId(object):
    '''
    classdocs
    '''
    def __init__(self):
        '''
        Constructor
        '''
        self.data = []
        from datasource import DataSource
        self.wifsip = DataSource(database='wifsip',
                                 user='******',
                                 host='pina.aip.de')

    def fromfile(self, filename=None):
        import pyfits

        if filename is None:
            filename = '/work2/jwe/NGC2236/NGC2236corotid.fits'
        hdulist = pyfits.open(filename)
        self.data = hdulist[1].data
        hdulist.close()

    def todatabase(self):
        for d in self.data:
            print d['starid'], d['corotid_2']
            self.wifsip.execute("""UPDATE ngc2236
                          SET corotid = %d
                          WHERE starid = '%s';""" %
                                (d['corotid_2'], d['starid']),
                                commit=False)
        self.wifsip.commit()
Example #2
0
class StackedPhot(object):
    '''
    classdocs
    '''
    def __init__(self):
        '''
        Constructor
        '''
        self.data = []
        from datasource import DataSource
        self.wifsip = DataSource(database=config.dbname,
                                 user=config.dbuser,
                                 host=config.dbhost)

    def fromfile(self, filename=None):
        import pyfits

        hdulist = pyfits.open(filename)
        self.data = hdulist[1].data
        self.keys = []
        for col in hdulist[1].columns:
            self.keys.append(col.name)
        hdulist.close()

    def todatabase(self):
        for d in self.data:
            print d['starid']
            record = {}
            for key in self.keys:
                record[key] = d[key]
            query = """UPDATE ngc2236 SET vmag = %(VMAG)f, bmag = %(BMAG)f, bv = %(BMAG)f-%(VMAG)f, nv = %(V_FLAGS)d, nb = %(B_FLAGS)d, vmag_err = %(VMAG_ERR)f, bmag_err = %(BMAG_ERR)f, member=TRUE WHERE starid = '%(starid)s';""" % record
            self.wifsip.execute(query, commit=False)

        self.wifsip.commit()
Example #3
0
class TwoMass(object):
    '''
    classdocs
    '''
    def __init__(self):
        '''
        Constructor
        '''
        self.data = []
        from datasource import DataSource
        self.wifsip = DataSource(database='wifsip',
                                 user='******',
                                 host='pina.aip.de')

    def fromfile(self, filename=None):
        import pyfits

        if filename is None:
            filename = '/work2/jwe/m48/data/2MASS.fits'
        hdulist = pyfits.open(filename)
        self.data = hdulist[1].data
        hdulist.close()

    def todatabase(self):
        for d in self.data:
            print d['starid'], d['ra_cone'], d['dec_cone']
            self.wifsip.execute("""UPDATE m48stars
                          SET ra = %f, dec = %f
                          WHERE starid = '%s';""" %
                                (d['ra_cone'], d['dec_cone'], d['starid']),
                                commit=False)
        self.wifsip.commit()
Example #4
0
class Yadav(object):
    '''
    classdocs
    '''
    def __init__(self):
        '''
        Constructor
        '''
        self.data = []
        from datasource import DataSource
        self.wifsip = DataSource(database=config.dbname,
                                 user=config.dbuser,
                                 host=config.dbhost)

    def fromfile(self, filename=None):
        hdulist = pyfits.open(filename)
        self.data = hdulist[1].data
        self.keys = []
        for col in hdulist[1].columns:
            self.keys.append(col.name)
        hdulist.close()

    def todatabase(self):
        for d in self.data:
            print(d['seq'])
            record = {}
            for key in self.keys:
                record[key] = d[key]
                if np.isnan(record[key]):
                    record[key] = 'NULL'
            query = """INSERT INTO m67 (seq, ra, dec, vmag, bmag, icmag, pmra, pmdec, pmb, hrv) 
VALUES (%(Seq)d, %(RAJ2000)f, %(DEJ2000)f, %(Vmag)s, %(Bmag)s, %(Icmag)s, %(pmRA)f, %(pmDE)f, %(Pmb)f, %(HRV)s);""" % record
            self.wifsip.execute(query, commit=False)

        self.wifsip.commit()
Example #5
0
 def doimport(self):
     '''import the file and shove it into the database'''
     from datasource import DataSource
     from astronomy import hms2dd, dms2dd
     from StringIO import StringIO
     f = open(self.filename, 'r')
     lines = f.readlines()
     f.close()
     
     # skip header information and compendium
     lines = lines[71:247]
     print lines[0]
     values = ''
     for l in lines:
         id = l[0:9].strip()
         ra = hms2dd(l[9:19].strip())
         dec = dms2dd(l[20:31].strip())
         s = l[32:33]
         try:
             ucac2 = int(l[34:42])
         except ValueError:
             ucac2 = None
         tyc2 = l[43:54]
         v = float(l[56:61])
         remarks = l[62:].rstrip()
         print id,ra,dec,s,ucac2,tyc2,v,remarks
         values += '%s\t%f\t%f\t%s\t%s\t%s\t%f\t%s\n' % (id,ra,dec,s,ucac2,tyc2,v,remarks)
         values = values.replace('None', '\N') 
     f = StringIO(values)
     print values
     wifsip = DataSource(host = 'pina', database = 'wifsip', user = '******')
     wifsip.cursor.copy_from(f, 'ngc2281ref', 
                             columns=('id','ra','dec','s','ucac2','tyc2','vmag', 'remarks'))
     wifsip.commit()
     wifsip.close()
Example #6
0
    def store(self, star, period=None, theta=None):
        from datasource import DataSource
        from numpy import isnan

        wifsip = DataSource(host='pina', database='wifsip', user='******')
        query = "UPDATE ngc2281stars"
        if not period is None:
            query += " SET period=%f" % period
        if not ((theta is None) or isnan(theta)):
            query += ",  theta=%f" % theta
        query += " WHERE id like '%s';" % star
        try:
            wifsip.execute(query)
            wifsip.commit()
        finally:
            wifsip.close()
Example #7
0
    def update_db(self):
        from datasource import DataSource
        import StringIO

        wifsip = DataSource(database='wifsip', user='******', host='pina.aip.de')
        columns = ['star', 'rem', 'vmag', 'bv', 'ub', 'n', 'code', 'ra', 'dec']
        values = ''
        for s in self.stars:
            values += '%s\t%s\t%.2f\t%.2f\t%.2f\t%d\t%d\t%f\t%f\n' % \
            (s['star'], s['rem'], s['V'], s['B-V'], s['U-B'], s['n'], s['code'],
             s['RA'], s['Dec'])
        f = StringIO.StringIO(values)
        print values
        try:
            wifsip.cursor.copy_from(f, 'turner', columns=columns)
        finally:
            wifsip.commit()
        wifsip.close()
Example #8
0
    def todatabase(self):
        """
        puts Gabriels data on the stella database
        """
        import StringIO

        filename = '/work2/jwe/Projects/NGC6633/data/tallrefavgcal_fields.dat'
        with open(filename, 'rt') as infile:
            lines = infile.readlines()

        valarray = []
        field = 0
        lastid = 0
        for l in lines[1:]:
            splitted = l.rstrip('\n').replace('NaN', '000').split()
            curid = int(splitted[0])
            if curid < lastid:
                field += 1
            lastid = curid
            splitted[0] = str(field * 100000 + curid)
            print splitted[0]
            assert (len(splitted) == 9)
            #print '(%s,%s)' % (splitted[1], splitted[2])
            splitted.append('(%s,%s)' % (splitted[1], splitted[2]))
            valline = '\t'.join(splitted)
            valarray.append(valline)

        values = '\n'.join(valarray)

        f = StringIO.StringIO(values)

        from datasource import DataSource

        columns = [
            'id', 'ra_j2000', 'dec_j2000', 'imag', 's_imag', 'vmag', 's_vmag',
            'bmag', 's_bmag', 'coord'
        ]

        stellads = DataSource(database=config.dbname,
                              user=config.dbuser,
                              host=config.dbhost)
        stellads.cursor.copy_from(f, 'ngc6633gab', columns=columns, null='nan')
        stellads.commit()
        stellads.close()
Example #9
0
class PhotDatabase(object):
    '''
    classdocs
    '''
    def __init__(self, filtercol):
        '''
        Constructor
        '''
        self.data = []
        from datasource import DataSource
        self.wifsip = DataSource(database=config.dbname,
                                 user=config.dbuser,
                                 host=config.dbhost,
                                 dictcursor=True)
        if not filtercol in ['u', 'v', 'b', 'y', 'hbn', 'hbw', 'I']:
            raise (ValueError)
        self.__filtercol = filtercol
        print self.__filtercol

    def fromfile(self, filename=None):
        import pyfits

        hdulist = pyfits.open(filename)
        self.data = hdulist[1].data
        self.keys = []
        for col in hdulist[1].columns:
            self.keys.append(col.name)
        hdulist.close()

    def todatabase(self):
        for d in self.data:
            record = {}
            for key in self.keys:
                record[key] = d[key]
            record['filter'] = self.__filtercol
            if self.__filtercol == 'I':
                record['filter'] = 'imag'
            query = """UPDATE ngc2236 
            SET %(filter)s = %(MAG_ISOCOR)f, %(filter)s_err = %(MAGERR_ISO)f 
            WHERE circle(coord,0) <@ circle(point(%(ALPHAWIN_J2000).11f,%(DELTAWIN_J2000).11f), 0.6/3600.0);""" % record
            self.wifsip.execute(query, commit=False)

        self.wifsip.commit()
Example #10
0
    def todatabase(self):
        from datasource import DataSource
        import StringIO

        values = ''

        for t in self.table:
            values = values + 'APASS%(recno)d\t%(RAJ2000).6f\t%(DEJ2000).6f\t%(Bmag).3f\t%(Vmag).3f\t%(rmag).3f\t%(imag).3f\n' % t

        wifsip = DataSource(database='stella',
                            user='******',
                            host='pera.aip.de')
        f = StringIO.StringIO(values)
        columns = ['starid', 'ra', 'dec', '"B"', '"V"', '"R"', '"I"']
        wifsip.cursor.copy_from(f,
                                'referencestars',
                                columns=columns,
                                null='nan')
        wifsip.commit()
        wifsip.close()
Example #11
0
class Ngc2236(object):
    def __init__(self):
        """Constructor"""
        from cluster import Cluster
        from astronomy import mag_distance
        from datasource import DataSource
    
        self.wifsip = DataSource(database='wifsip', user='******', host='pina.aip.de')
        self.stars = []
         
        c = Cluster('NGC 2236')
        self.age = 10**c['logage']/1e6 # in Myr
        self.ebv = c['ebv']
        self.dm = mag_distance(c['d'])- 4.83

    def clearperiods(self):
        """
        reset the periods in the database table
        """
        if not raw_input('press Y to erase the periods in table ')=='Y':
            return
        query="""UPDATE ngc2236 
        SET period=NULL, period_err=NULL, amp=NULL, amp_err=NULL
        WHERE period>0;
        """
        logger.info('resetting periods ...')
        self.wifsip.execute(query)

    def getstars(self):
        """
        build up a list of stars, where we do not have periods yet
        """
        
        query = """SELECT starid 
        FROM ngc2236 
        WHERE NOT corotid IS NULL;"""
        
        logger.info('fetching stars ...')
        result = self.wifsip.query(query)
        logger.info('... %d stars found' % len(result))
        print '... %d stars found' % len(result)
        self.stars = [s[0] for s in result]
    
    def analysis(self, show=False):
        """perform a PDM analysis on each lightcurve"""
        from matplotlib import rcParams
        from functions import sigma_clip, phase
        print 'Analysis'

        fig_width = 18.3/2.54  # width in inches, was 7.48in
        fig_height = 23.3/2.54  # height in inches, was 25.5
        fig_size =  [fig_width,fig_height]
        #set plot attributes
        params = {'backend': 'Agg',
          'axes.labelsize': 12,
          'axes.titlesize': 12,
          'font.size': 12,
          'xtick.labelsize': 12,
          'ytick.labelsize': 12,
          'figure.figsize': fig_size,
          'savefig.dpi' : 300,
          'font.family': 'sans-serif',
          'axes.linewidth' : 0.5,
          'xtick.major.size' : 2,
          'ytick.major.size' : 2,
          }
        rcParams.update(params)

        for starid in self.stars:
            star = NGC2236Star(starid)
            print '%-24s '% starid,
            lc = star.lightcurve()
            meanflux = np.nanmean(lc.flux)
            lc.flux /= meanflux
            lc.flux *= meanflux
            lc.rebin(0.0125)
            lc.interpolate()
            lc.normalize()
            raw_time, raw_flux = (lc.time, lc.flux)
            
            lc.medfilter()
            lc.jmpflt()
            lc.detrend()
            time, flux = (lc.time, lc.flux)
            time -= min(time)
            
            # convert to magnitudes
            mag = -2.512*np.log10(meanflux*flux) + 24
            # remove the mean so we are around 0.0 magnitudes
            mag -= np.mean(mag)
            
            # perform a 3sigma clipping
            time, mag = sigma_clip(time, mag)

            # calculate fourier spectrum with zero padding
            n = len(mag)
            n2 = 8*n
            ft = np.fft.rfft(mag, n2)
            #amp = abs(ft)/n
            power = abs(ft)**2/n
            
            freq = np.fft.fftfreq(n2, d=lc.dt())
            
            
            # apply a bessel filter to eliminate runlength artifacts
            from scipy import signal
            
            b, a = signal.butter(4, 2./max(time), 'high', analog=True)
            _, h = signal.freqs(b, a, worN=freq[1:n])
            filt_pwr = abs(ft[1:n]*h)**2/n

            i = np.argmax(filt_pwr)+1
            maxfreq = freq[i]
            period = 1./maxfreq
            
            from pdm import pdm

            #refine period using pdm
            pdm_periods, pdm_thetas = pdm(time, mag, period/1.5, period*1.5, 0.0125)
            
            i = np.argmin(pdm_thetas)
            period = pdm_periods[i]
            
            print 'P = %5.2f' % period,
            star['period'] = period
            periods = 1./freq[1:n]
            periods = periods[::-1]
            filt_pwr = filt_pwr[::-1]
            
            norm = np.mean(filt_pwr)
            print '%.1f' % (max(filt_pwr)/norm)
            power[1:n] /= norm
            filt_pwr /= norm
            star['amp'] = max(filt_pwr)/norm
            ph_time, ph_mag = phase(time, mag, period)
            num = len(ph_time)/round(max(time)/period)
            rph_mag, _ = signal.resample(ph_mag, num, t = ph_time)
            rph_time =  np.linspace(min(ph_time),max(ph_time),num)
            bv = star['bv']
            if bv is None: bv=-99
            
            plt.subplot(411) ##################################################
            plt.title('%s (%d) B-V=%.2f' % (starid, star['corotid'], bv))
            plt.scatter(raw_time-min(raw_time), raw_flux, edgecolor='none', alpha=0.5, s=3, color='k')
            plt.ylim(min(raw_flux),max(raw_flux))
            plt.xlim(0,max(time))
            
            plt.subplot(412) ##################################################
            plt.plot(time, -mag, 'k')
            plt.ylim(min(-mag),max(-mag))
            plt.xlim(0,max(time))
            
            plt.subplot(413) ##################################################
            plt.plot(1./freq[1:n], power[1:n], 'k')
            plt.plot(1./freq[1:n], filt_pwr, 'g')
            #plt.plot(1./w, 20 * np.log10(abs(h)))
            plt.axvline(period)
            #plt.axhline(np.mean(filt_pwr[1:n]))
            plt.xlim(0.1, max(time))
            
            plt.subplot(414) ##################################################
            plt.plot(rph_time, -rph_mag,color='k')
            plt.plot(rph_time+period, -rph_mag,color='k')
            #plt.plot(phased_time, phased_mag, 'g')
            #plt.plot(phased_time+period, phased_mag, 'g')
            plt.axvline(period, linestyle='--')
            plt.xlabel('P = %.2f' % period)
            plt.xlim(0., period*2)
            
            #plt.grid()

            if show:
                plt.show()
            else:
                plt.savefig(config.plotpath+'%s(%d).pdf' % (starid,star['corotid']))
            plt.close()

    def set_tab_column(self):
        
        self.wifsip.execute('UPDATE ngc2236 set tab=NULL;')
        query = """SELECT starid
            FROM ngc2236 
            WHERE NOT bv IS NULL
            ORDER BY vmag;"""
        result = self.wifsip.query(query)
        starids = [r[0] for r in result]
        
        for starid in starids:
            tab = starids.index(starid)+1
            print '%4d %s' % (tab,starid)
            query = "UPDATE ngc2236 set tab=%d WHERE starid='%s';" % (tab,starid)
            self.wifsip.execute(query)
    
    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()
Example #12
0
class Stetson(object):
    '''
    classdocs
    '''
    def __init__(self):
        '''
        Constructor
        '''
        self.data = []
        from datasource import DataSource
        self.wifsip = DataSource(database='stella',
                                 user='******',
                                 host='pera.aip.de')

    def fromfile(self, filename=None):
        import pyfits

        hdulist = pyfits.open(filename)
        self.data = hdulist[1].data
        self.keys = []
        for col in hdulist[1].columns:
            self.keys.append(col.name)
        hdulist.close()
        print self.keys

    def todatabase(self):
        for d in self.data:
            print d['ID_1']
            query = """INSERT INTO ngc6633ref (starid, ra, dec, dx, dy, x, y, bmag, bsigma, nb, vmag, vsigma, nv, imag, isigma, ni, coord) 
VALUES ('%(ID_1)s', %(RA)f, %(DEC)f, %(dX)f, %(dY)f, %(X)f, %(Y)f, %(B)f,%(sigmaB)f,%(NB)f,%(V)f,%(sigmaV)f,%(NV)f,%(I)f,%(sigmaI)f,%(NI)f, point(%(RA)f,%(DEC)f));""" % d
            self.wifsip.execute(query, commit=False)

        self.wifsip.commit()

    def readpos(self, filename='NGC6633.pos'):
        posfile = open(filename, 'rt')
        data = np.genfromtxt(posfile,
                             dtype=None,
                             names=('RA', 'DEC', 'HH', 'MM', 'SS', 'DD', 'DM',
                                    'DS', 'dX', 'dY', 'X', 'Y', 'ID'))
        for d in data:
            #print ">%(ID)s<" % d
            query = """UPDATE ngc6633ref 
            SET ra=%(RA).11f,dec=%(DEC).11f, coord=point(%(RA).11f,%(DEC).11f)  
            WHERE starid='%(ID)s';""" % d
            print query
            self.wifsip.execute(query)
        posfile.close()

    def setucac4(self, filename='Stetson_UCAC4.fit'):
        import pyfits

        hdulist = pyfits.open(filename)
        ucac = hdulist[1].data
        hdulist.close()
        print ucac.columns
        print ucac['ucacid']

        #clear existing coordinates
        self.wifsip.execute('UPDATE ngc6633ref SET coord=NULL;')

        ra = ucac['raj2000']
        dec = ucac['dej2000']
        x = ucac['X']
        y = ucac['Y']
        A = np.vstack([x, y, np.ones(len(x))]).T
        wcsra = np.linalg.lstsq(A, ra)[0]
        wcsdec = np.linalg.lstsq(A, dec)[0]
        print wcsra
        print wcsdec
        dx1, dy1, cx = wcsra
        dx2, dy2, cy = wcsdec
        param = {
            'dx1': dx1,
            'dy1': dy1,
            'cx': cx,
            'dx2': dx2,
            'dy2': dy2,
            'cy': cy
        }
        query = """UPDATE ngc6633ref SET coord=point(X*%(dx1).11g+Y*%(dy1).11g+%(cx).11f, X*%(dx2).11g+Y*%(dy2).11g+%(cy).11f);""" % param
        print query
        self.wifsip.execute(query)

    def calibrate(self):
        from tools import log
        ffield = {'V': 'vmag', 'B': 'bmag', 'I': 'imag'}
        for filtercol in ['V', 'B', 'I']:
            query = """SELECT objid 
            FROM frames 
            WHERE object LIKE 'NGC 6633 BVI %%' 
            AND filter = '%s';""" % filtercol
            frames = self.wifsip.query(query)
            for frame in frames:
                params = {'objid': frame[0], 'filterfield': ffield[filtercol]}
                query = """SELECT starid, mag_isocor, magerr_isocor, %(filterfield)s, mag_isocor - %(filterfield)s
                FROM phot, ngc6633ref
                WHERE objid = '%(objid)s'
                AND circle(phot.coord,0) <@ circle(ngc6633ref.coord, 1.0/3600.0)
                AND NOT %(filterfield)s IS NULL
                AND flags<4;""" % params
                result = self.wifsip.query(query)
                if len(result) > 0:
                    ocs = np.array([r[4] for r in result])
                    errs = np.array([r[2] for r in result])
                    weights = 1 / errs
                    std = np.std(ocs)
                    i = np.where(abs(ocs - np.mean(ocs)) < std)[0]
                    #for r in result:
                    #    log(config.logfile, '%-11s %.4f %.4f %.3f %6.3f' % r)
                    corr = 0.0
                    if len(i) > 4:
                        corr = np.average(ocs[i], weights=weights[i])
                        std = np.std(ocs[i])
                    log(config.logfile, '%-19s %1s %-7.4f %.3f %3d %3d' % \
                        (frame[0], filtercol, corr, std, len(i), len(ocs)))
                    params['corr'] = corr
                    params['good'] = 'NULL'
                    if std < 0.05:
                        params['good'] = 'TRUE'
                    else:
                        params['good'] = 'FALSE'
                    query = """UPDATE frames SET corr=%(corr)f, good=%(good)s WHERE objid ='%(objid)s';""" % params
                else:
                    query = """UPDATE frames SET corr=NULL, good=FALSE WHERE objid ='%(objid)s';""" % params
                self.wifsip.execute(query)

    def calibratebv(self):
        '''drop view ngc6633match;
        create view ngc6633match as 
            SELECT ngc6633ref.starid, ngc6633.vmag, ngc6633.bmag, ngc6633ref.vmag "vmag_ref", ngc6633ref.bmag "bmag_ref" 
            FROM ngc6633, ngc6633ref 
            WHERE circle(ngc6633.coord,0) <@ circle(ngc6633ref.coord, 1.0/3600.0);'''

        from dbtable import DBTable
        match = DBTable(
            self.wifsip,
            'ngc6633match',
            condition=
            'NOT vmag_ref IS NULL AND NOT bmag_ref IS NULL AND vmag>12')
        vmag = match['vmag']
        bmag = match['bmag']
        bv = bmag - vmag
        vmagref = match['vmag_ref']
        bmagref = match['bmag_ref']
        bvref = bmagref - vmagref
        A = np.vstack([vmag, bv, np.ones(len(vmag))]).T
        vcorr = np.linalg.lstsq(A, vmagref)[0]
        B = np.vstack([bmag, bv, np.ones(len(bmag))]).T
        bcorr = np.linalg.lstsq(B, bmagref)[0]

        print vcorr
        print bcorr
Example #13
0
class Photometry(object):
    '''
    classdocs
    '''
    def __init__(self, filtercol='V'):
        '''
        Constructor
        '''
        from datasource import DataSource

        self.wifsip = DataSource(database='wifsip', host='pina', user='******')
        if filtercol in ('B', 'V'):
            self.filtercol = filtercol
        else:
            raise (ValueError)

        self.frames = []
        print 'filter %s' % self.filtercol

    def createtable(self):
        '''
        create table for the photometry
        '''
        if not raw_input('press Y to erase m48stars') == 'Y':
            return

        query = """
        DROP TABLE IF EXISTS m48stars;
        CREATE TABLE m48stars(
         starid varchar(25),
         bv real,
         vmag real default 0,
         vmag_err real,
         bmag real default 0,
         bmag_err real,
         period real,
         period_err real,
         theta real,
         amp real,
         amp_err real,
         nv integer default 0,
         nb integer default 0,
         ra double precision,
         dec double precision,
         coord point,
         PRIMARY KEY (starid));
        GRANT SELECT ON m48stars TO public;
        CREATE INDEX idx_m48stars_coords ON m48stars USING GIST (circle(coord,1.0/3600.));
        """
        self.wifsip.execute(query)
        print "table 'm48stars' created"

    def cleartable(self):
        if not raw_input('press Y to clear m48stars') == 'Y':
            return
        query = """
        UPDATE m48stars
        SET vmag = 0, vmag_err = NULL, bmag = 0, bmag_err =NULL, nv = 0, nb = 0, bv = NULL;
        """
        self.wifsip.execute(query)
        print "table 'm48stars' cleared"

    def getframes(self):
        for field in ['C', 'NW', 'NE', 'SW', 'SE']:
            query = """SELECT object, objid, abs(corr)
             FROM frames
             WHERE object LIKE 'M 48 BVI %s'
             AND filter LIKE '%s'
             AND NOT corr IS NULL
             ORDER BY abs(corr)
             limit 5;""" % (field, self.filtercol)

            result = self.wifsip.query(query)
            if len(result) == 0:
                print 'no frames found!'
            for r in result:
                print '%s\t%s\t%.3f: ' % r
                objid = r[1]
                self.filltable(objid)
                self.frames.append(objid)
            self.wifsip.dropview('phot1')
        self.update_magnitudes()
        #print '\n'.join(self.frames)

    def filltable(self, objid):
        #get the stars from the phot table ...
        query = """
            SELECT phot.objid ||'#'|| star, mag_auto-corr, alphawin_j2000 , deltawin_j2000
            FROM phot,frames
            WHERE frames.objid='%s'
            AND phot.objid=frames.objid AND flags<8;""" % (objid)
        result = self.wifsip.query(query)
        #... and inject them into the m48stars
        stars = len(result)
        if stars > 400:
            print '%5d stars: ' % stars,
            oldstars = 0
            newstars = 0
            for r in result:
                ostars = self.addstar(r[0], r[1], r[2], r[3])
                if ostars == 0:
                    newstars += 1
                else:
                    oldstars += 1
            print '%5d old , %5d new' % (oldstars, newstars)
            self.wifsip.commit()
        else:
            print 'not enough stars (%d)' % stars
        #value = '%s\t%f\t%f\t%f' % r
        #print value

    def addstar(self, starid, mag, ra, dec):
        # identify by coordinates, if the star is already in table
        query = """SELECT starid
        FROM m48stars
        WHERE circle(point(%f,%f),0)<@circle(coord,1.0/3600.)
        ORDER BY point(%f,%f)<->coord
        LIMIT 1;""" % (ra, dec, ra, dec)
        result = self.wifsip.query(query)
        oldstar = 0
        # if not: append new star
        if len(result) == 0:
            oldstar = 0
            if self.filtercol == 'B':
                query = """INSERT INTO m48stars (starid, bmag, nb, ra, dec, coord)
                VALUES ('%s', %f, 1, %f, %f, point(%f,%f))""" % (
                    starid, mag, ra, dec, ra, dec)
            elif self.filtercol == 'V':
                query = """INSERT INTO m48stars (starid, vmag, nv, ra, dec, coord)
                VALUES ('%s', %f, 1, %f, %f, point(%f,%f))""" % (
                    starid, mag, ra, dec, ra, dec)

        # if star exists: add up magnitudes, increase counter
        else:
            oldstar = 1
            oldid = result[0][0]
            if self.filtercol == 'B':
                query = """UPDATE m48stars
                SET bmag = bmag + %f, nb = nb + 1
                WHERE starid = '%s';
                """ % (mag, oldid)
            elif self.filtercol == 'V':
                query = """UPDATE m48stars
                SET vmag = vmag + %f, nv = nv + 1
                WHERE starid = '%s';
                """ % (mag, oldid)
        self.wifsip.execute(query)
        return oldstar

    def update_magnitudes(self):
        if self.filtercol == 'B':
            query = """UPDATE m48stars
            SET bmag=bmag/nb
            WHERE nb>1;
            UPDATE m48stars
            SET bmag=NULL
            WHERE nb=0;"""
        elif self.filtercol == 'V':
            query = """    
            UPDATE m48stars
            SET vmag=vmag/nv
            WHERE nv>1;
            UPDATE m48stars
            SET vmag=NULL
            WHERE nv=0;"""
        self.wifsip.execute(query)

    def update_sigmas(self):
        import numpy as np

        if self.filtercol == 'V': field = 'vmag'
        elif self.filtercol == 'B': field = 'bmag'
        query = """SELECT starid, coord 
            FROM m48stars 
            WHERE (NOT bv IS NULL) AND (%s_err IS NULL);""" % (field)
        starlist = self.wifsip.query(query)
        for star in starlist:
            print '%5d ' % starlist.index(star),
            print '%-24s: %-25s' % star,
            query = """SELECT phot.objid, mag_auto-corr 
                FROM phot, frames
                WHERE object like 'M 48 BVI %%'
                AND phot.objid=frames.objid
                AND filter='%s'
                AND flags<8
                AND point%s <@ circle(phot.coord,1./3600.)
                ORDER BY abs(corr)
                LIMIT 5;""" % (self.filtercol, star[1])
            result = self.wifsip.query(query)
            mags = np.array([r[1] for r in result])
            try:
                err = np.std(mags)
                print mags,
                print '%.3f %.4f' % (np.mean(mags), err)
                if np.isfinite(err):
                    query = "UPDATE m48stars SET %s_err=%f WHERE starid='%s';" % (
                        field, err, star[0])
                    self.wifsip.execute(query)
            except TypeError:
                print 'no data'

    def update_bv(self):
        query = "UPDATE m48stars SET bv = bmag-vmag;"
        self.wifsip.execute(query)
Example #14
0
class NGC6633cat(object):
    '''
    fill the ngc6633table in the database
    '''
    def __init__(self):
        '''
        Constructor
        '''
        from datasource import DataSource
        self.wifsip = DataSource(database='stella',
                                 user='******',
                                 host='pera.aip.de')

    def load_positions(self, filename=config.datapath + 'NGC6633V.fit'):
        import pyfits

        hdulist = pyfits.open(filename)
        data = hdulist[2].data
        i = np.argsort(data['MAG_ISO'])
        self.stars = data[i]
        hdulist.close()
        print self.stars.columns
        print "%d stars loaded" % len(self.stars)

    def load_fromdb(self):
        query = """SELECT starid, ra, dec from ngc6633 order by vmag;"""
        result = self.wifsip.query(query)
        self.stars = []
        for starid, ra, dec in result:
            record = {
                'starid': starid,
                'ALPHAWIN_J2000': ra,
                'DELTAWIN_J2000': dec
            }
            self.stars.append(record)
        print "%d stars loaded from database" % len(self.stars)

    def build(self, filtercol='V', update=False):
        from psycopg2 import IntegrityError

        if update: print 'updating filter ', filtercol
        for star in self.stars:
            #print star['NUMBER'],star['MAG_ISO'],
            params = {
                'ra': star['ALPHAWIN_J2000'],
                'dec': star['DELTAWIN_J2000'],
                'filtercol': filtercol
            }
            query = """SELECT phot.objid || '#' || phot.star, mag_isocor, corr, magerr_isocor 
                FROM phot, frames
                WHERE object like 'NGC 6633 BVI %%'
                AND phot.objid = frames.objid
                AND filter = '%(filtercol)s'
                AND good = True
                AND circle(phot.coord,0) <@ circle(point(%(ra).11f,%(dec).11f), 0.6/3600.0)
                AND flags<5;""" % params
            result = self.wifsip.query(query)
            #print result
            if len(result) == 0:
                continue
            elif len(result) == 1:
                starid, mags, corr, err = result[0]
                mag = mags - corr
                std = err
                n = 1
            elif len(result) > 1:
                mags = np.array([r[1] for r in result])
                corrs = np.array([r[2] for r in result])
                errs = np.array([r[3] for r in result])
                # take the first (random) identifier, just to have a starid
                starid = result[0][0]
                mags -= corrs
                std0 = np.std(mags)
                # remove outliers
                i = np.where(abs(mags - np.mean(mags)) < std0)
                mags = mags[i]
                errs = errs[i]
                std = np.std(mags)

                try:
                    mag = np.average(mags, weights=1. / errs)
                except TypeError:
                    print mags, corrs, errs
                    continue
                except ZeroDivisionError:
                    print mags, corrs, errs
                    mag = np.mean(mags)
                std = np.std(mags)
                n = len(mags)
            params['mag'] = mag
            params['std'] = std
            params['n'] = n
            filterletter = filtercol.lower()
            params['magfield'] = filterletter + 'mag'
            params['errfield'] = filterletter + 'mag_err'
            params['numfield'] = 'n' + filterletter

            if not update:
                params['starid'] = starid
                query = """INSERT INTO ngc6633 
                (starid,ra,dec,%(magfield)s,%(errfield)s,%(numfield)s)
                VALUES ('%(starid)s', %(ra).11f, %(dec).11f, %(mag)f, %(std)f, %(n)d);""" % params
                query = query.replace('nan', 'NULL')
                try:
                    self.wifsip.execute(query)
                except IntegrityError:  # star already exists
                    continue
            else:
                params['starid'] = star['starid']
                query = """UPDATE ngc6633 
                SET (%(magfield)s,%(errfield)s,%(numfield)s) = (%(mag)f, %(std)f, %(n)d)
                WHERE starid = '%(starid)s';""" % params
                query = query.replace('nan', 'NULL')
                self.wifsip.execute(query)
            print '%(starid)-25s, %(ra).6f, %(dec).6f, %(mag).4f, %(std).4f, %(n)3d' % params
        self.wifsip.commit()
Example #15
0
class WHydra(object):
    '''
    classdocs
    '''

    def __init__(self, field_name = 'M67field'):
        '''
        Constructor
        '''

        self.field_name = field_name

        self.wifsip = DataSource(database=config.dbname, user=config.dbuser, host=config.dbhost)
        
        kpno = ephem.Observer()
        #31.958036,-111.600578
        kpno.lon, kpno.lat = '-111.600578', '31.958036'
        kpno.horizon = '-0:34'
        kpno.elevation = 2096
        tzi = pytz.timezone('MST')
        #fmt = '%Y-%m-%d %H:%M:%S %Z%z'
        
        obsdate = datetime.datetime(2015,2,10,4,0,0, tzinfo=tzi)
        kpno.date = obsdate + datetime.timedelta(7*ephem.hour)
        d = kpno.date.datetime()
        
           
        self.input_epoch = 2000
        self.current_epoch = d.year + d.timetuple().tm_yday/365.
        
        # LST at mid-exposure in decimal hours
        lst = kpno.sidereal_time()
        print('local siderial time: ', lst)
        self.siderial_time = ast.hms2hh(str(lst))
        self.exposure_length = 40.0/60.0 # in hours
        self.wavelength = 5125 # in Angstroem
        self.cable = 'BLUE'
        self.weighting = 'STRONG'
        self.guidewave = 6000
        
        self.center = self._get_center()
        
        c = Cluster('NGC 2682')
        self.ebv = c['ebv']
        self.dm = ast.distance_modulus(c['d'])

        target = {}
        self.table = []
        target['id'] = 6000
        target['name'] = 'M67center'
        target['ra'] = self.center[0]
        target['dec'] = self.center[1]
        target['class'] = 'C'
        self.table.append(target)
        self.targeted = []
        # number of sky fibers
        self.skies = 6
        # number of field orientation probes
        self.fops = 6
        self.tweakcenter = False
        self.tweakangle = False

        print('center', self.center)
        print('E(B-V)', self.ebv)
        print('DM', self.dm)

    def _get_center(self):
        data = self.wifsip.query("""select avg(ra),avg(dec) from m67;""")
        return (data[0][0],data[0][1])

    def priorities(self, verbose = False):
        """updates the priorities in the m67 table"""

        def makeplot(bv, v, p, filename=None):
            """plot the priorities"""
            import matplotlib
            matplotlib.use('Agg')
            from matplotlib import pyplot
            pyplot.scatter(bv, v, c=p, edgecolor='none', alpha=0.75)
            pyplot.xlim(0.4,1.0)
            pyplot.ylim(16.5,12.0)
            pyplot.title('M 67')
            pyplot.xlabel('B - V')
            pyplot.ylabel('V mag')
            
            if filename is None: 
                pyplot.show()    
            else:
                pyplot.savefig(filename, dpi=300)
            pyplot.close()

        def plotradec(ra, dec, p, filename=None):
            """plot the priorities"""
            import matplotlib
            matplotlib.use('Agg')
            from matplotlib import pyplot
            pyplot.scatter(ra, dec, c=p, edgecolor='none', alpha=0.75)
            pyplot.title('M 67')
            pyplot.xlabel('R.A.')
            pyplot.ylabel('Dec')
            
            if filename is None: 
                pyplot.show()    
            else:
                pyplot.savefig(filename, dpi=300)
            pyplot.close()

        print('calculate priorities ...')
        self.wifsip.execute("UPDATE m67 SET priority=NULL, pointing=NULL;")
        self.wifsip.execute("""UPDATE m67 
            SET priority=1.0 
            WHERE bmag-vmag>0.6 and bmag-vmag<1.0
            AND vmag>13.5 and vmag<16 and pmb>0;""")
        
        data = self.wifsip.query("""SELECT seq, vmag, bmag-vmag, pmb 
                               FROM m67
                               WHERE bmag-vmag>0.6 AND bmag-vmag<0.75
                               AND vmag>13.5 and vmag<16
                               ORDER BY pmb DESC;""")
        seq = [d[0] for d in data]
        v = np.array([d[1] for d in data])
        bv = np.array([d[2] for d in data])
        pmb = np.array([d[3] for d in data])
        
        p1 = scaleto(pmb, [0.0, 1.0])

        print(len(seq), 'stars brighter V<16')
        for i in range(len(seq)):
            if verbose:
                print('%4d: %.3f --> %.3f' % (seq[i], v[i], p1[i]))
            self.wifsip.execute("""UPDATE m67
                          SET priority = priority * %f
                          WHERE seq = %d;""" % (p1[i], seq[i]), commit=False)
        self.wifsip.commit()   
        
        data = self.wifsip.query("""SELECT bmag-vmag, vmag, priority
                               FROM m67
                               WHERE priority > 0.0
                               ORDER BY seq;""")
        
        bv = np.array([d[0] for d in data])
        vmag = np.array([d[1] for d in data])
        p = np.array([d[2] for d in data])
        makeplot(bv,vmag, p, filename=config.plotpath+'priorities.pdf')
          
    def setpointing(self, pointing):
        """set pointings according to hydrasim output"""
        #TODO: to be redone! 
        targets = ",".join([str(t) for t in self.targeted])
        print(targets)
        query = """UPDATE m67 
                   SET pointing=%d
                   WHERE seq in (%s)""" % (pointing,targets)
        self.wifsip.execute(query)
    
    def from_database(self, maglimit=16.5):
        """
        load targets from database that have not priority set yet and are
        within the maglimit
        """
        data = self.wifsip.query("""SELECT seq, ra, dec, vmag
                               FROM m67 
                               WHERE priority > 0.0
                               AND vmag<%f
                               AND pointing IS NULL
                               ORDER BY priority DESC;""" % maglimit) 
        for d in data:
            target = {}
            target['id'] = int(d[0])
            target['name'] = 'Star%04dm%.2f' % (int(d[0]),float(d[3]))
            target['ra'] = float(d[1])
            target['dec'] = float(d[2])
            target['class'] = 'O'
            self.table.append(target)
        """
        hydrawiynmanual.pdf p.41:
        The stars selected for use by the FOPs should fall in the magnitude 
        range 10<V<14. If possible, keep the range in magnitude of your FOPs 
        sample in each field to 3 magnitudes or less so that the intensity of 
        each star falls within the dynamic range of the FOPs TV camera. 
        Including the FOPs star magnitudes in the configuration file may
        also be useful later when setting up the field at the telescope.
        """
        query = """SELECT vmag, ra, dec, seq 
        FROM m67
        WHERE vmag>10 and vmag<13 
        ORDER BY vmag;"""
        fops = self.wifsip.query(query)
        print(len(fops), 'FOPs stars')
        for f in fops:
            target = {}
            target['id'] = 6001 + int(f[3])
            target['name'] = 'FOP%dm%.2f' % (int(f[3]),float(f[0]))
            target['ra'] = float(f[1])
            target['dec'] = float(f[2])
            target['class'] = 'F'
            self.table.append(target)

        #extra targets
        query="""select vmag, ra, dec, seq
        FROM m67
        WHERE NOT bmag-vmag IS NULL
        ORDER BY vmag
        LIMIT 999;"""
        
        extra = self.wifsip.query(query)
        print(len(extra), 'extra stars')
        for d in extra:
            target = {}
            target['id'] = 7000 + int(d[3])
            target['name'] = str(d[0])
            target['ra'] = float(d[1])
            target['dec'] = float(d[2])
            target['class'] = 'E'
            self.table.append(target)
    
    def skyfile(self, filename='/work2/jwe/M67/skyfile.txt'):
        """
        load the sky positions
        """
        from astropy import units as u
        from astropy.coordinates import SkyCoord
        ras = [132.686, 132.703, 132.820, 132.965]
        decs = [11.591, 11.745, 11.849, 11.869, 12.091]
        skypos = []
        for ra in ras:
            for de in decs:
                skypos.append((ra,de))
        print(len(skypos), 'sky positions')

        for radec in skypos:
                target = {}
                target['id'] = 9000 + skypos.index(radec)
                target['name'] = 'Sky'+str(target['id'])
                target['ra'] = radec[0]
                target['dec'] = radec[1]
                target['class'] = 'S'
                self.table.append(target)
    
    def tofile(self, filename = None, verbose=False):
        """
        writes the .ast file that finally goes into whydra for processing
        """
        import astronomy as ast
        if filename is None:
            filename = hydrapath+self.field_name+'.ast'
        f = open(filename,'w')
        f.write('FIELD NAME: %s\n' % self.field_name)
        f.write('INPUT EPOCH: %.2f\n' % self.input_epoch)
        f.write('CURRENT EPOCH: %.2f\n' % self.current_epoch)
        f.write('SIDERIAL TIME: %.3f\n' % self.siderial_time)
        f.write('EXPOSURE LENGTH: %.2f\n' % self.exposure_length)
        f.write('WAVELENGTH: %.0f.\n' % self.wavelength)
        f.write('CABLE: %s\n' % self.cable)
        f.write('WEIGHTING: %s\n' % self.weighting)
        f.write('GUIDEWAVE: %.0f.\n' % self.guidewave)
        f.write('#0000000011111111112222222222333333333344444444445555\n')                          
        f.write('#2345678901234567890123456789012345678901234567890123\n')                          
        for t in self.table:
            ra = ast.dd2hms(t['ra'])
            dec = ast.dd2dms(t['dec'])
            s = '%04d %-20s %02d %02d %06.3f %+02.2d %02d %05.2f %1s\n' % \
                (t['id'],t['name'],ra[0],ra[1],ra[2], dec[0], dec[1], dec[2], t['class'])
            if verbose:
                print(s.rstrip('\n'))
            f.write(s)
        f.close()
        #s = 'pointing\tR.A.\tDec\tmag\tcomment'

    def fromfile(self, filename = None):
        if filename is None:
            filename = hydrapath+self.field_name+'.hydra'
        
        f = open(filename,'r')
        lines = f.readlines()
        f.close()
        for l in lines:
            if l[0]=='#':
                l = ''
            #if l.find('FIELD NAME:')>=0:
            # self.field_name = l[l.find(':')+1:].strip()
               
            if l.find('INPUT EPOCH:')>=0:
                self.input_epoch = float(l[l.find(':')+1:])
            if l.find('CURRENT EPOCH:')>=0:
                self.current_epoch = float(l[l.find(':')+1:])
            if l.find('SIDERIAL TIME:')>=0:
                self.siderial_time = float(l[l.find(':')+1:])
            if l.find('EXPOSURE LENGTH:')>=0:
                self.exposure_length = float(l[l.find(':')+1:])
            if l.find('WAVELENGTH:')>=0:
                self.wavelength = float(l[l.find(':')+1:])
            if l.find('CABLE:')>=0:
                self.cable = l[l.find(':')+1:].strip()
            if l.find('WEIGHTING:')>=0:
                self.weighting = l[l.find(':')+1:].strip()
            if l.find('GUIDEWAVE:')>=0:
                self.guidewave = float(l[l.find(':')+1:])
            if len(l.strip())>=53:
                target = {
                'id': int(l[0:4]),
                'name': l[5:25],
                'ra': ast.hms2dd(l[26:38]),
                'dec': ast.dms2dd(l[39:51]),
                'class': l[52]}
                if l.find('STATUS=')>=55:
                    status=l[55:66].strip()
                    if target['class'] in ['O','E']:
                        if (status=='STATUS=OK' or status=='STATUS=EDGE'):
                            self.table.append(target)
                        else:
                            self.targeted.append(target['id'])
                    elif target['class'] in ['C','F','S']:
                        self.table.append(target)
                        
    def make_plot(self, filename):
        plt.figure(figsize=[18/2.54,18/2.54])
        fov = plt.Circle((self.center[0], self.center[1]), 0.5, facecolor='none', edgecolor='g')
        fig = plt.gcf()
        fig.gca().add_artist(fov)
#        plt.scatter(ra,dec, marker=',', c='k', s=0.1)
        
        
        for t in self.table:
            if t['class']=='O':
                plt.scatter(t['ra'], t['dec'], marker='o', c='g', edgecolor='none')
            if t['class']=='E':
                plt.scatter(t['ra'], t['dec'], marker=',', c='gray', edgecolor='none')
            if t['class']=='F':
                plt.scatter(t['ra'], t['dec'], marker='^', c='r', edgecolor='none')
            if t['class']=='S':
                plt.scatter(t['ra'], t['dec'], marker='h', c='b', edgecolor='none')
            if t['class']=='C':
                plt.scatter(t['ra'], t['dec'], marker='+', c='k', edgecolor='none')
                
        plt.xlim([self.center[0]+0.55,self.center[0]-0.55])
        plt.ylim([self.center[1]-0.55,self.center[1]+0.55])
        plt.xlabel('R.A.')
        plt.ylabel('Dec')
        plt.grid()
        plt.savefig(filename, dpi=300)
        plt.close()

    def dohydra(self):
        """
        write the commands file and
        execute the shell script
        """

        f = open('/home/jwe/bin/hydra_simulator/cmds.%s' % self.field_name,'wt')
        f.write('%s.ast\n' % self.field_name)
        f.write('%s\n' % self.field_name)
        f.write('%d\n' % self.fops)
        f.write('%d\n' % self.skies)
        if self.tweakangle:
            f.write('y\n')
        else:
            f.write('n\n')
        if self.tweakcenter:
            f.write('y\n')
        else:
            f.write('n\n')
        f.close()
        
        subprocess.call(['/home/jwe/bin/hydra_simulator/dowhydra.sh',self.field_name])  
        
    def getconcentricities(self):
        """
        fetches the current concentricities file from the WIYN web page
        """
        response = urlopen('http://www.wiyn.org/concentricities')
        html = response.read()
        f = open(hydrapath+'concentricities','wt')
        f.write(html)
        f.close()
Example #16
0
class M48Phot(object):
    '''
    Import photometry from David James
    '''
    def __init__(self):
        '''
        Constructor
        '''
        from datasource import DataSource

        self.data = []
        self.table = DataSource(database='wifsip',
                                user='******',
                                host='pina.aip.de')

    def fromfile(self, filename):
        f = open(filename)
        lines = f.readlines()
        f.close
        p = filename.find('M48-')
        field = filename[p + 4:p + 6]
        for l in lines:
            sl = l.split()
            record = {
                'starid': '%s-%s-%3.3d' % (sl[0], field, int(sl[1])),
                'ccdx': float(sl[2]),
                'ccdy': float(sl[3]),
                'ra': float(sl[10]),
                'dec': float(sl[11]),
                'vmag': float(sl[12]),
                'vmag_err': float(sl[13]),
                'bv': float(sl[14]),
                'bv_err': float(sl[15]),
                'vi': float(sl[16]),
                'vi_err': float(sl[17])
            }
            if record['bv'] > 9: record['bv'] = None
            if record['bv_err'] > 9: record['bv_err'] = None
            if record['vi'] > 9: record['vi'] = None
            if record['vi_err'] > 9: record['vi_err'] = None
            self.data.append(record)
            print record

    def createtable(self):
        '''
        create table for the photometry
        '''
        if not raw_input('press Y to erase m48phot') == 'Y':
            return

        query = """
        DROP TABLE IF EXISTS m48phot;
        CREATE TABLE m48phot(
         starid varchar(10),
         ccdx real,
         ccdy real,
         ra double precision,
         dec double precision,
         vmag real default 0,
         vmag_err real,
         bv real,
         bv_err real,
         vi real,
         vi_err real,
         coord point,
         PRIMARY KEY (starid));
        GRANT SELECT ON m48phot TO public;
        CREATE INDEX idx_m48phot_coords ON m48phot USING GIST (circle(coord,0));
        """
        self.table.execute(query)
        print "table 'm48phot' created"

    def todatabase(self):
        import StringIO

        cur = self.table.cursor

        def nstr(s):
            if s is None: return '\N'
            elif type(s) is str: return str(s)
            else: return str(s)

        values = ''

        for record in self.data:
            valline = '\t'.join([nstr(v) for v in record.values()])
            print valline
            values += valline + '\n'

        columns = record.keys()
        f = StringIO.StringIO(values)
        try:
            cur.copy_from(f, 'm48phot', columns=columns)
        finally:
            self.table.commit()
Example #17
0
class Clusters(object):
    '''
    classdocs
    '''

    def __init__(self):
        '''
        Constructor
        '''
        self.table = DataSource(database=_config.dbname, user=_config.dbuser, host=_config.dbhost)

    def create_table(self):
        query = """DROP TABLE clusters;
        CREATE TABLE clusters (
        name   varchar(18),
        ra     double precision,
        dec    double precision,
        class  varchar(3),
        diam   real,
        d      int,
        ebv    real,
        logage real,
        pmra   real,
        epmra  real,
        pmdec  real,
        epmdec real,
        nc     int,
        ref1   varchar(3),
        rv     real,
        erv    real,
        n      int,
        ref2   varchar(4),
        me     real,
        eme    real,
        nme    real,
        trtyp  varchar(8),   
        coord point,
        PRIMARY KEY (name));"""
        self.table.execute(query)

    def fromfile(self, filename='/work2/jwe/SOCS/clusters.txt'):

        f = open(filename, 'rt')
        lines = f.readlines()
        f.close()

        def nfloat(s):
            from numpy import nan
            try:
                result = float(s)
            except ValueError:
                return nan
            return result

        def nint(s):
            from numpy import nan
            try:
                result = int(s)
            except ValueError:
                return nan
            return result

        cur = self.table.cursor

        values = ''

        for l in lines:
            coords = l[18:37]
            c = SkyCoord(coords, 'icrs', unit=(u.hourangle, u.deg))  # @UndefinedVariable
            record = {'name': l[0:18].rstrip(),
                      'ra': c.ra.degree,
                      'dec': c.dec.degree,
                      'class': l[40:43].rstrip(),
                      'diam': nfloat(l[45:52]),
                      'd': nint(l[55:60]),
                      'ebv': nfloat(l[65:70]),
                      'logage': nfloat(l[73:79]),
                      'pmra': nfloat(l[84:90]),
                      'epmra': nfloat(l[92:96]),
                      'pmdec': nfloat(l[100:106]),
                      'epmdec': nfloat(l[108:112]),
                      'nc': nint(l[113:118]),
                      'ref1': l[119:123].rstrip(),
                      'rv': nfloat(l[127:134]),
                      'erv': nfloat(l[138:143]),
                      'n': nint(l[147:150]),
                      'ref2': nint(l[155:159]),
                      'me': nfloat(l[162:168]),
                      'eme': nfloat(l[171:176]),
                      'nme': nint(l[177:180]),
                      'trtyp': l[183:191].rstrip(),
                      'coord': '(%f,%f)' % (c.ra.degree, c.dec.degree)}

            # print record
            def nstr(s):
                if len(str(s).strip()) == 0:
                    return '\\N'
                elif type(s) is str:
                    return str(s)
                else:
                    return str(s)

            valline = '\t'.join([nstr(v) for v in record.values()])
            valline = valline.replace('nan', '\\N')
            print(valline)
            values += valline + '\n'

        columns = record.keys()
        f = StringIO.StringIO(values)
        try:
            cur.copy_from(f, 'clusters', columns=columns)
        finally:
            self.table.commit()

    def query(self):

        query = """SELECT name, ra, dec, ebv, diam from clusters
        WHERE (name like 'NGC %' or name like 'IC %') 
        AND diam>10 AND diam<60
        AND d<1500
        AND dec>-15.0
        AND ebv<0.3
        AND logage>8.0 AND logage<=9.5
        AND abs(rv)>2.0;"""
        result = self.table.query(query)
        names = [r[0] for r in result]
        ra = np.array([r[1] for r in result])
        dec = np.array([r[2] for r in result])
        ebv = np.array([r[3] for r in result])
        diam = np.array([r[4] for r in result])

        mycmap = plt.cm.get_cmap('Reds')
        # mycmap.set_under('w')fig, ax_f = plt.subplots()

        _, ax1 = plt.subplots(figsize=(10, 7))
        plt.scatter(ra / 15., dec, s=diam * 4, cmap=mycmap, c=ebv)
        for rai, deci, iname in zip(ra, dec, names):
            if iname in ['IC 4756', 'NGC 2682', 'NGC 2319', 'NGC 2374', 'NGC 7209', 'NGC 7243', 'NGC 7082', 'NGC 225']:
                horizontalalignment = 'right'
                withdash = None
                dashlength = None
            elif iname in ['NGC 2413']:
                horizontalalignment = 'right'
                withdash = True
                dashlength = 20.0
            else:
                horizontalalignment = 'left'
                withdash = None
                dashlength = None
            if withdash:
                plt.text(rai / 15., deci, iname, withdash=withdash, dashlength=dashlength,
                         horizontalalignment=horizontalalignment)
            else:
                plt.text(rai / 15., deci, iname, horizontalalignment=horizontalalignment)
        # plt.draw()
        ax2 = ax1.twiny()

        print(np.arange(13, 0, -1))
        ax1.set_xlabel('right ascension')
        ax1.set_ylabel('declination')
        ax2.set_xlabel('culmination month')
        ax1.set_xticks(np.arange(24))
        ax1.set_yticks(np.arange(-15, 65, 5))
        ax2.set_xticks(np.arange(0, 13) + 0.4333)
        ax2.set_xticklabels(['9', '8', '7', '6', '5', '4', '3', '2', '1', '12', '11', '10'])
        ax1.set_xlim(24, 0)
        # ax2.set_xlim(12, 0)
        ax1.set_ylim(-15, 65)
        ax1.grid()
        plt.minorticks_on()
        cbar = plt.colorbar()
        # cbar.ax.set_yticklabels(['0','1','2','>3'])
        cbar.set_label('E(B - V)', rotation=270)
        plt.savefig('/work2/jwe/SOCS/plots/cluster_query.pdf')
        plt.close()
        print(len(names))
        print('\n'.join(names))
Example #18
0
class CDSTable(object):
    '''
    classdocs
    '''
    def __init__(self):
        '''
        Constructor
        '''
        self.datastruct = []

        from datasource import DataSource
        self.wifsip = DataSource(database=config.dbname,
                                 user=config.dbuser,
                                 host=config.dbhost,
                                 dictcursor=True)

    def savetable2(self):
        query = """SELECT tab, vmag, bv, p_fin, e_pfin, amp, amp_err, member, 
            simbad, provisional
            FROM m48stars 
            WHERE good
            ORDER BY vmag ;"""
        self.data = self.wifsip.query(query)
        #Id   Vmag   B-V  P     P_er amp   amp_e M P Simbad
        #         1         2         3         4
        #1234567890123456789012345678901234567890123456789012
        # 287 13.597 0.56  2.88 0.10 0.018 0.004 M p BJG 3157

        f = open(config.resultpath + 'table2.dat', 'wt')

        for d in self.data:
            #i = data.index(d)+1
            simbad = ''
            if type(d['simbad']) is str and d['simbad'].find(
                    'Cl* NGC 2548 ') == 0:
                simbad = d['simbad'][13:]
            if str('simbad') == 'None': simbad = ''
            memstr = '-'
            if d['member']: memstr = 'M'
            elif d['member'] == False: memstr = 'N'
            prostr = '-'
            if d['provisional']: prostr = 'p'
            elif not d['provisional']: prostr = 'c'
            params = d.copy()
            params['simbad'] = simbad
            params['memstr'] = memstr
            params['prostr'] = prostr
            s = '%(tab)4d %(vmag)6.3f %(bv)4.2f %(p_fin)5.2f %(e_pfin).2f %(amp).3f %(amp_err).3f %(memstr)s %(prostr)s %(simbad)s\n' % params
            print s,
            f.write(s)
        f.close()

    def saveappendix(self):
        from numpy import sqrt
        from astropy import units as u
        from astropy.coordinates import SkyCoord
        #TODO: omit simbad column
        query = """SELECT tab, vmag, vmag_err, bv, bmag_err, coord[0] "ra", coord[1] "dec", member
            FROM m48stars 
            WHERE not bv IS NULL
            ORDER BY vmag;"""
        data = self.wifsip.query(query)
        f = open(config.resultpath + 'table_a1.dat', 'wt')

        for d in data:
            memstr = '-'
            if d['member']: memstr = 'M'
            elif d['member'] == False: memstr = 'N'
            try:
                bv_err = sqrt(d['vmag_err']**2 + d['bmag_err']**2)
            except TypeError:
                bv_err = 0.0
            c = SkyCoord(ra=d['ra'] * u.deg,
                         dec=d['dec'] * u.deg,
                         frame='icrs')  # @UndefinedVariable
            ra_str = c.ra.to_string(unit=u.hourangle,
                                    sep=' ',
                                    pad=True,
                                    precision=2)  # @UndefinedVariable
            dec_str = c.dec.to_string(sep=' ', precision=2, pad=True)
            posstr = c.to_string(precision=5)
            #if bmag_err is None: bmag_err=0.0
            params = d.copy()
            params['memstr'] = memstr
            params['bv_err'] = bv_err
            params['ra_str'] = ra_str
            params['dec_str'] = dec_str
            params['posstr'] = posstr
            if d['vmag_err'] is None: params['vmag_err'] = 0.0

            try:
                s = '%(tab)4d %(posstr)s %(vmag)6.3f %(vmag_err)5.3f %(bv)6.3f %(bv_err)5.3f %(ra_str)s %(dec_str)s %(memstr)s\n' % params
                print s,
                f.write(s)
            except TypeError:
                print d, len(s)
        f.close()

    def update_coords(self, filtercol='V'):
        import numpy as np
        from tools import log
        query = """SELECT starid, ra, dec 
        FROM m48stars 
        WHERE NOT bv IS NULL AND coord IS NULL ORDER BY tab;"""
        result = self.wifsip.query(query)
        stars = []
        for starid, ra, dec in result:
            stars.append({'starid': starid, 'ra': ra, 'dec': dec})

        for star in stars:
            #print star['NUMBER'],star['MAG_ISO'],
            params = {
                'ra': star['ra'],
                'dec': star['dec'],
                'filtercol': filtercol
            }
            query = """SELECT alphawin_j2000, deltawin_j2000 
                FROM phot, frames
                WHERE object like 'M 48 %%'
                AND phot.objid = frames.objid
                AND filter = '%(filtercol)s'
                AND circle(phot.coord,0) <@ circle(point(%(ra).11f,%(dec).11f), 0.3/3600.0)
                AND flags<5;""" % params
            result = self.wifsip.query(query)
            #print result
            if len(result) == 0:
                print '%-25s: no data' % star['starid']
                query = """UPDATE m48stars 
                SET coord = NULL
                WHERE starid = '%s';""" % star['starid']
                self.wifsip.execute(query)
                continue
            elif len(result) == 1:
                print '%-25s: not enough data' % star['starid']
                query = """UPDATE m48stars 
                SET coord = NULL
                WHERE starid = '%s';""" % star['starid']
                self.wifsip.execute(query)
                continue
            elif len(result) > 1:
                ra = np.array([r[0] for r in result])
                dec = np.array([r[1] for r in result])

                mean_ra = np.mean(ra)
                mean_dec = np.mean(dec)
                std_ra = np.std(ra)
                std_dec = np.std(dec)
                # remove outliers
                std = np.sqrt(std_ra**2 + std_dec**2)
                dist = np.sqrt((ra - mean_ra)**2 + (dec - mean_dec)**2)
                i = np.where(dist < 2 * std)
                mean_ra = np.mean(ra[i])
                mean_dec = np.mean(dec[i])
                std_ra = np.std(ra[i])
                std_dec = np.std(dec[i])
                n = len(ra[i])
            params['starid'] = star['starid']
            params['ra'] = mean_ra
            params['dec'] = mean_dec
            params['n'] = n

            params['sra'] = std_ra * 3600.0
            params['sdec'] = std_dec * 3600.0
            query = """UPDATE m48stars 
            SET coord = point(%(ra).11f,%(dec).11f)
            WHERE starid = '%(starid)s';""" % params
            #query = query.replace('nan', 'NULL')
            self.wifsip.execute(query)
            log(
                config.projectpath + 'coords1.log',
                '%(starid)-25s, %(ra).6f, %(dec).6f, %(sra).2f, %(sdec).2f, %(n)3d'
                % params)
        self.wifsip.commit()
Example #19
0
class Landolt(object):
    '''
    The famous Landolt class
    '''


    def __init__(self):
        '''
        Constructor
        '''
        from datasource import DataSource
    
        self.table = DataSource(database='stella', user='******', host='pera.aip.de')
        self.data = []

    def fromfile(self, filename='table2.dat'):
        """
        load data from file
        
        filename: string, table2.dat
        """
        from astropy.coordinates import SkyCoord
        from astropy import units as u
        
        def nfloat(s):
            from numpy import nan
            try:
                result = float(s)
            except ValueError:
                return nan
            return result

        def nint(s):
            from numpy import nan
            try:
                result = int(s)
            except ValueError:
                return nan
            return result

        f = open(filename)
        lines = f.readlines()
        f.close
        
        for l in lines:
            coords = l[12:38] 
            c = SkyCoord(coords, 'icrs', unit=(u.hourangle, u.deg))  # @UndefinedVariable
            record = {'name':   l[0:12].rstrip(),
                      'ra':   c.ra.degree,
                      'dec':  c.dec.degree,
                      'vmag': nfloat(l[38:45]),
                      'bv':  nfloat(l[46:51]),
                      'ub':  nfloat(l[52:59]),
                      'vr':  nfloat(l[59:66]),
                      'ri':  nfloat(l[66:73]),
                      'vi':  nfloat(l[73:79]),
                      'nobs': nint(l[80:83]),
                      'nnig': nint(l[84:87]),
                      'e_vmag': nfloat(l[88:94]),
                      'e_bv': nfloat(l[95:101]),
                      'e_ub': nfloat(l[102:108]),
                      'e_vr': nfloat(l[109:115]),
                      'e_ri': nfloat(l[116:122]),
                      'e_vi': nfloat(l[123:129]),
                      'coord': '(%f,%f)' % (c.ra.degree, c.dec.degree)}
            #print record
            self.data.append(record)
            
    def todatabase(self):
        """
        write the imported data to the database
        """
        from StringIO import StringIO
        
        def nstr(s):
            if len(str(s).strip())==0: return '\N'
            elif type(s) is str: return str(s) 
            else: return str(s)
            
        values = ''
        
        for record in self.data:
            valline = '\t'.join([nstr(v) for v in record.values()])
            valline = valline.replace('nan', '\N')
            print valline
            values += valline + '\n'

        columns = record.keys()
        f = StringIO(values)
        cur = self.table.cursor
        try:
            cur.copy_from(f,'landolt', columns=columns)
        finally:
            self.table.commit()
    
    def calibrate(self):
        query = """SELECT frames.datesend, MAX(airmass) "airmass", COUNT(star) "nstar", AVG(mag_auto-vmag) "o-c mag", STDDEV_POP(mag_auto-vmag) "sigma"
            FROM frames, phot, landolt
            WHERE object LIKE 'Landolt%'
            AND filter='V'
            AND frames.objid=phot.objid
            AND circle(phot.coord,3./3600.) @> circle(landolt.coord,0)
            AND frames.objid like '2014%'
            AND phot.flags=0
            GROUP BY frames.datesend
            ORDER BY frames.datesend;"""
        result = self.table.query(query)

        import numpy as np
        for r in result: print '%s %.2f %2d %+6.3f %.3f' % r
        
        import matplotlib.pyplot as plt
        x = np.array([r[0] for r in result])
        n = np.array([r[2] for r in result])
        y = np.array([r[3] for r in result])
        yerr = np.array([r[4] for r in result])
        
        #print len(x),len(y),len(yerr)
        plt.scatter(y,yerr, s=n*5, edgecolor='none')
        plt.show()
        plt.errorbar(x, y, yerr)
        plt.minorticks_on()
        plt.grid()
        plt.show()
Example #20
0
class WHydra(object):
    '''
    classdocs
    '''

    def __init__(self, field_name = 'M48field'):
        '''
        Constructor
        '''
        import ephem
        import datetime
        import pytz
        import astronomy as ast
        from datasource import DataSource

        self.field_name = field_name
        self.wifsip = DataSource(database='wifsip', user='******', host='pina.aip.de') 
        
        kpno = ephem.Observer()
        #31.958036,-111.600578
        kpno.lon, kpno.lat = '-111.600578', '31.958036'
        kpno.horizon = '-0:34'
        kpno.elevation = 2096
        tzi = pytz.timezone('MST')
        #fmt = '%Y-%m-%d %H:%M:%S %Z%z'
        
        obsdate = datetime.datetime(2015,2,9,23,0,0, tzinfo=tzi)
        kpno.date = obsdate + datetime.timedelta(7*ephem.hour)
        d = kpno.date.datetime()
        
           
        self.input_epoch = 2000
        self.current_epoch = d.year + d.timetuple().tm_yday/365.
        
        # LST at mid-exposure in decimal hours
        lst = kpno.sidereal_time()
        print 'local siderial time: ',lst
        self.siderial_time = ast.hms2hh(str(lst))
        self.exposure_length = 40.0/60.0 # in hours
        self.wavelength = 6000 # in Angstroem
        self.cable = 'BLUE'
        self.weighting = 'STRONG'
        self.guidewave = 6000
        
        self.center = self._get_center()
        self.ebv = 0.031 # from Webda
        self.dm = 9.53 # from Webda

        target = {}
        self.table = []
        target['id'] = 6000
        target['name'] = 'M48center'
        target['ra'] = self.center[0]
        target['dec'] = self.center[1]
        target['class'] = 'C'
        self.table.append(target)
        self.targeted = []
        # number of sky fibers
        self.skies = 6
        # number of field orientation probes
        self.fops = 6
        self.tweakcenter = False
        self.tweakangle = False
        
    def _get_center(self):
        data = self.wifsip.query("""select avg(ra),avg(dec) from m48stars;""")
        return (data[0][0],data[0][1])

    def priorities(self, verbose = False):
        """updates the priorieties in the m48stars table"""
        import numpy as np
        from functions import scaleto
        
        print 'calculate priorities ...'
        self.wifsip.execute("UPDATE m48stars SET priority=NULL;")
        self.wifsip.execute("""UPDATE m48stars 
            SET priority=1.0 
            WHERE vmag<16.5 
            AND NOT tab IS NULL;""")
        
        data = self.wifsip.query("""SELECT tab, vmag 
                               FROM m48stars
                               WHERE not bv is null AND vmag<16.5
                               ORDER BY tab;""")
        tab = [d[0] for d in data]
        v = np.array([d[1] for d in data])
        p1 = scaleto(v,[1.0, 0.8])
        print len(tab),'stars brighter V<16.5'
        for i in range(len(tab)):
            if verbose: print '%4d: %.3f --> %.3f' % (tab[i], v[i],p1[i])
            self.wifsip.execute("""UPDATE m48stars
                          SET priority = priority * %f
                          WHERE tab = %d;""" % (p1[i], tab[i]), commit=False)
        self.wifsip.commit()   

        lengood = self.wifsip.query("select count(starid) from m48stars where good;")
        print lengood[0],'stars with periods'
        
        self.wifsip.execute("""UPDATE m48stars
                          SET priority = priority * 0.5
                          WHERE NOT good;""")
        self.wifsip.commit()   

        iso = IsoChrone('/work2/jwe/m48/data/output256520738433.dat')
        x = iso['V'] + self.dm
        y = iso['B-V'] + self.ebv
        data = self.wifsip.query("""SELECT tab, vmag, bv 
                               FROM m48stars 
                               WHERE not bv is null AND vmag<16.5
                               ORDER BY tab;""")
        tab = [d[0] for d in data]
        v= np.array([d[1] for d in data])
        bv = np.array([d[2] for d in data])
        p = np.zeros(len(tab))
        print len(tab),'stars for isochrone priority'
        
        for i in range(len(tab)):
            p[i] = np.min(abs(x-v[i])+abs(y-bv[i]))
            
        p[p > 0.4] = 0.4
        p = scaleto(p, [1.0, 0.0])
        for t in tab:
            i = tab.index(t)
            if verbose: 
                print '%d: V=%.3f B-V=%.3f p=%.3f' % (t, v[i], bv[i], p[i])
            self.wifsip.execute("""UPDATE m48stars
                              SET priority = priority * %f
                              WHERE tab = %d;""" % (p[i], t), commit=False)
        self.wifsip.commit()   

        data = self.wifsip.query("""SELECT tab, ra, dec
                               FROM m48stars
                               WHERE not ra is NULL AND not dec is NULL
                               AND priority > 0.0
                               ORDER BY TAB;""")
        tab = [d[0] for d in data]
        print len(tab),'stars for distance priority'
        ra = np.array([d[1] for d in data])
        dec = np.array([d[2] for d in data])
        dist = np.sqrt((ra-self.center[0])**2+(dec-self.center[1])**2)
        # only rank stars where distance greater than 0.5 degrees
        i = np.where(dist > 0.5)
        p = scaleto(dist, [1.0, 0.0])
        for t in tab:
            i = tab.index(t)
            try:
                if verbose:
                    print '%d: d=%.3f p=%.3f' % (t,dist[i],p[i])
                self.wifsip.execute("""UPDATE m48stars
                              SET priority = priority * %f
                              WHERE tab = %d;""" % (p[i], t), commit=False)
            except TypeError:
                print t
        self.wifsip.commit() 
        
        
          

    def setpointing(self, pointing):
        """set pointings according to hydrasim output"""
        #TODO: to be redone! 
        targets = ",".join([str(t) for t in self.targeted])
        print targets
        query = """UPDATE m48stars 
                   SET pointing=%d
                   WHERE tab in (%s)""" % (pointing,targets)
        self.wifsip.execute(query)
    
    def from_database(self, maglimit=16.5):
        """
        load targets from database that have not priority set yet and are
        within the maglimit
        """
        data = self.wifsip.query("""SELECT tab, starid, ra, dec, vmag
                               FROM m48stars 
                               WHERE priority > 0.0
                               AND vmag<%f
                               AND pointing IS NULL
                               ORDER BY priority DESC;""" % maglimit)
        for d in data:
            target = {}
            target['id'] = int(d[0])
            target['name'] = 'Star%04dm%.2f' % (int(d[0]),float(d[4]))
            target['ra'] = float(d[2])
            target['dec'] = float(d[3])
            target['class'] = 'O'
            self.table.append(target)
        """
        hydrawiynmanual.pdf p.41:
        The stars selected for use by the FOPs should fall in the magnitude 
        range 10<V<14. If possible, keep the range in magnitude of your FOPs 
        sample in each field to 3 magnitudes or less so that the intensity of 
        each star falls within the dynamic range of the FOPs TV camera. 
        Including the FOPs star magnitudes in the configuration file may
        also be useful later when setting up the field at the telescope.
        """
        #TODO: import at most 2000 twomass coordinates
        fops = self.wifsip.query("""SELECT tab, ra, dec,vmag 
                               FROM m48stars 
                               WHERE vmag>10 AND vmag<13
                               AND (priority < 0.3 OR priority IS NULL)
                               AND tab is NOT NULL
                               ORDER BY vmag 
                               LIMIT 2000;""")
        print len(fops),'FOPs stars'
        for f in fops:
            target = {}
            target['id'] = 6001 + int(f[0])
            target['name'] = 'FOP%dm%.2f' % (int(f[0]),float(f[3]))
            target['ra'] = float(f[1])
            target['dec'] = float(f[2])
            target['class'] = 'F'
            self.table.append(target)

        #extra targets
        extra = self.wifsip.query("""SELECT starid, ra, dec
                               FROM m48phot 
                               WHERE vmag<6.5*bv+10.4 and vmag>5*bv+10
                               AND vmag<%f
                               ORDER BY vmag;""" % maglimit)
        print len(extra),'extra stars'
        for d in extra:
            target = {}
            target['id'] = 7000+extra.index(d)
            target['name'] = str(d[0])
            target['ra'] = float(d[1])
            target['dec'] = float(d[2])
            target['class'] = 'E'
            self.table.append(target)
    
    def skyfile(self, filename='/work2/jwe/m48/data/sky_positions.txt'):
        """
        load the sky positions
        """
        from astropy import units as u
        from astropy.coordinates import SkyCoord
        f = open(filename,'r')
        lines = f.readlines()
        f.close()
        print len(lines),'sky positions'
        
        for l in lines:
            try:
                c = SkyCoord(l, 'icrs', unit=(u.hourangle, u.deg))  # @UndefinedVariable
            except ValueError:
                print 'empty line'
            else:
                target = {}
                target['id'] = 9000 + lines.index(l)
                target['name'] = 'Sky'+str(target['id'])
                target['ra'] = c.ra.degree
                target['dec'] = c.dec.degree
                target['class'] = 'S'
                self.table.append(target)
    
    
    def tofile(self, filename = None, verbose=False):
        """
        writes the .ast file that finally goes into whydra for processing
        """
        import astronomy as ast
        if filename is None:
            filename = hydrapath+self.field_name+'.ast'
        f = open(filename,'w')
        f.write('FIELD NAME: %s\n' % self.field_name)
        f.write('INPUT EPOCH: %.2f\n' % self.input_epoch)
        f.write('CURRENT EPOCH: %.2f\n' % self.current_epoch)
        f.write('SIDERIAL TIME: %.3f\n' % self.siderial_time)
        f.write('EXPOSURE LENGTH: %.2f\n' % self.exposure_length)
        f.write('WAVELENGTH: %.0f.\n' % self.wavelength)
        f.write('CABLE: %s\n' % self.cable)
        f.write('WEIGHTING: %s\n' % self.weighting)
        f.write('GUIDEWAVE: %.0f.\n' % self.guidewave)
        f.write('#0000000011111111112222222222333333333344444444445555\n')                          
        f.write('#2345678901234567890123456789012345678901234567890123\n')                          
        for t in self.table:
            ra = ast.dd2hms(t['ra'])
            dec = ast.dd2dms(t['dec'])
            s = '%04d %-20s %02d %02d %06.3f %+02.2d %02d %05.2f %1s\n' % \
                (t['id'],t['name'],ra[0],ra[1],ra[2], dec[0], dec[1], dec[2], t['class'])
            if verbose: print s.rstrip('\n')
            f.write(s)
        f.close()
        #s = 'pointing\tR.A.\tDec\tmag\tcomment'

    def fromfile(self, filename = None):
        import astronomy as ast
        if filename is None:
            filename = hydrapath+self.field_name+'.hydra'
        
        f = open(filename,'r')
        lines = f.readlines()
        f.close()
        for l in lines:
            if l[0]=='#':
                l = ''
            #if l.find('FIELD NAME:')>=0:
            # self.field_name = l[l.find(':')+1:].strip()
               
            if l.find('INPUT EPOCH:')>=0:
                self.input_epoch = float(l[l.find(':')+1:])
            if l.find('CURRENT EPOCH:')>=0:
                self.current_epoch = float(l[l.find(':')+1:])
            if l.find('SIDERIAL TIME:')>=0:
                self.siderial_time = float(l[l.find(':')+1:])
            if l.find('EXPOSURE LENGTH:')>=0:
                self.exposure_length = float(l[l.find(':')+1:])
            if l.find('WAVELENGTH:')>=0:
                self.wavelength = float(l[l.find(':')+1:])
            if l.find('CABLE:')>=0:
                self.cable = l[l.find(':')+1:].strip()
            if l.find('WEIGHTING:')>=0:
                self.weighting = l[l.find(':')+1:].strip()
            if l.find('GUIDEWAVE:')>=0:
                self.guidewave = float(l[l.find(':')+1:])
            if len(l.strip())>=53:
                target = {
                'id': int(l[0:4]),
                'name': l[5:25],
                'ra': ast.hms2dd(l[26:38]),
                'dec': ast.dms2dd(l[39:51]),
                'class': l[52]}
                if l.find('STATUS=')>=55:
                    status=l[55:66].strip()
                    if target['class'] in ['O','E']:
                        if (status=='STATUS=OK' or status=='STATUS=EDGE'):
                            self.table.append(target)
                        else:
                            self.targeted.append(target['id'])
                    elif target['class'] in ['C','F','S']:
                        self.table.append(target)
                        
    def make_plot(self, filename):
        import matplotlib.pyplot as plt
        
        plt.figure(figsize=[18/2.54,18/2.54])
        fov = plt.Circle((self.center[0], self.center[1]), 0.5, facecolor='none', edgecolor='g')
        fig = plt.gcf()
        fig.gca().add_artist(fov)
#        plt.scatter(ra,dec, marker=',', c='k', s=0.1)
        
        
        for t in self.table:
            if t['class']=='O':
                plt.scatter(t['ra'], t['dec'], marker='o', c='g')
            if t['class']=='E':
                plt.scatter(t['ra'], t['dec'], marker=',', c='gray')
            if t['class']=='F':
                plt.scatter(t['ra'], t['dec'], marker='^', c='r')
            if t['class']=='S':
                plt.scatter(t['ra'], t['dec'], marker='h', c='b')
            if t['class']=='C':
                plt.scatter(t['ra'], t['dec'], marker='+', c='k')
                
        plt.xlim([self.center[0]+0.55,self.center[0]-0.55])
        plt.ylim([self.center[1]-0.55,self.center[1]+0.55])
        plt.xlabel('R.A.')
        plt.ylabel('Dec')
        plt.grid()
        #plt.show()
        plt.savefig(filename, dpi=300)
        plt.close()

        #data = self.wifsip.query("""SELECT vmag, bv, priority 
        #                       FROM m48stars 
        #                       WHERE priority>0.1;""")
        #v= np.array([d[0] for d in data])
        #bv = np.array([d[1] for d in data])
        #p = [d[2] for d in data]
        #from matplotlib import pyplot
        #pyplot.scatter(bv, v, c=p)
        #pyplot.xlim(-0.1,1.6)
        #pyplot.ylim(16.5,7.5)
        #pyplot.show()    
        #plt.savefig(filename, dpi=300)
        #plt.close()

    def dohydra(self):
        """
        write the commands file and
        execute the shell script
        """
        import subprocess
        
        f = open('/home/jwe/bin/hydra_simulator/cmds.%s' % self.field_name,'wt')
        f.write('%s.ast\n' % self.field_name)
        f.write('%s\n' % self.field_name)
        f.write('%d\n' % self.fops)
        f.write('%d\n' % self.skies)
        if self.tweakangle:
            f.write('y\n')
        else:
            f.write('n\n')
        if self.tweakcenter:
            f.write('y\n')
        else:
            f.write('n\n')
        f.close()
        
        subprocess.call(['/home/jwe/bin/hydra_simulator/dowhydra.sh',self.field_name])  
        
    def getconcentricities(self):
        """
        fetches the current concentricities file from the WIYN web page
        """
        import urllib2
        response = urllib2.urlopen('http://www.wiyn.org/concentricities')
        html = response.read()
        f = open(hydrapath+'concentricities','wt')
        f.write(html)
        f.close()
Example #21
0
class Photometry(object):
    '''
    Photometry class has the following tasks:
    
    * create the db table
    * optionally clear the values for recalculation
    * build up a list of stars
    * collect B and V magnitudes for each star
    '''
    def __init__(self, objname=None, filtercol='V', dbname=None):
        '''
        Constructor
        '''

        self.wifsip = DataSource(database='wifsip', host='pina', user='******')

        print(objname)

        if objname is None:  # 'M48 BVI'
            raise (ValueError, 'objname not set')
        else:
            self.objname = objname

        if filtercol in ('B', 'V', 'R', 'I', 'u', 'v', 'b', 'y', 'hbn', 'hbw'):
            self.filtercol = filtercol
        else:
            raise (ValueError, 'unknown filter color')

        self.frames = []
        if dbname is None:  # 'M48stars'
            raise (ValueError, 'tablename not set')
        else:
            self.dbname = dbname

        print('filter %s' % self.filtercol)

    def createtable(self):
        '''
        create table for the photometry
        '''
        if not input('press Y to erase ' + self.dbname) == 'Y':
            return

        query = """
        DROP TABLE IF EXISTS %(dbname)s;
        CREATE TABLE %(dbname)s(
         starid varchar(25),
         bv real,
         vmag real default 0,
         vmag_err real,
         bmag real default 0,
         bmag_err real,
         period real,
         period_err real,
         amp real,
         amp_err real,
         nv integer default 0,
         nb integer default 0,
         ra double precision,
         dec double precision,
         coord point,
         PRIMARY KEY (starid));
        GRANT SELECT ON %(dbname)s TO public;
        CREATE INDEX idx_%(dbname)s_coords ON %(dbname)s USING GIST (circle(coord,1.0/3600.));
        """ % {
            'dbname': self.dbname
        }
        self.wifsip.execute(query)
        print("table '%s' created" % self.dbname)

    def cleartable(self):
        """
        clears photometric values from the table for recalculation
        """
        if not input('press Y to clear ' + self.dbname) == 'Y':
            return
        query = """
        UPDATE %s
        SET vmag = 0, 
        vmag_err = NULL, 
        bmag = 0, 
        bmag_err = NULL, 
        nv = 0, 
        nb = 0, 
        bv = NULL;
        """ % self.dbname
        self.wifsip.execute(query)
        print("table '%s' cleared" % self.dbname)

    def getframes(self, fields=['C', 'NW', 'NE', 'SW', 'SE']):
        for field in fields:
            if len(field) > 0:
                objname = self.objname + ' ' + field
            else:
                objname = self.objname
            query = """SELECT object, objid, abs(corr)
             FROM frames
             WHERE object LIKE '%s'
             AND filter LIKE '%s'
             AND NOT corr IS NULL
             ORDER BY abs(corr)
             limit 5;""" % (objname, self.filtercol)

            result = self.wifsip.query(query)
            if len(result) == 0:
                print('no frames found!')
            for r in result:
                print('%s\t%s\t%.3f: ' % r)
                objid = r[1]
                self.filltable(objid)
                self.frames.append(objid)
            self.wifsip.dropview('phot1')

        self.update_magnitudes()
        # print '\n'.join(self.frames)

    def filltable(self, objid):
        # get the stars from the phot table ...
        query = """
            SELECT phot.objid ||'#'|| star, mag_auto-corr, alphawin_j2000 , deltawin_j2000
            FROM phot,frames
            WHERE frames.objid='%s'
            AND phot.objid=frames.objid AND flags<8;""" % (objid)
        result = self.wifsip.query(query)
        # ... and inject them into the m48stars
        stars = len(result)
        if stars > 400:
            print('%5d stars: ' % stars, end=' ')
            oldstars = 0
            newstars = 0
            for r in result:
                ostars = self.addstar(r[0], r[1], r[2], r[3])
                if ostars == 0:
                    newstars += 1
                else:
                    oldstars += 1
            print('%5d old , %5d new' % (oldstars, newstars))
            self.wifsip.commit()
        else:
            print('not enough stars (%d)' % stars)

    def addstar(self, starid, mag, ra, dec):
        # identify by coordinates, if the star is already in table
        query = """SELECT starid
        FROM %(dbname)s
        WHERE circle(point(%(ra)f,%(dec)f),0)<@circle(coord,1.0/3600.)
        ORDER BY point(%(ra)f,%(dec)f)<->coord
        LIMIT 1;""" % {
            'dbname': self.dbname,
            'ra': ra,
            'dec': dec
        }
        result = self.wifsip.query(query)
        oldstar = 0
        # if not: append new star
        if self.filtercol == 'B':
            mname, nname = ('bmag', 'nb')
        elif self.filtercol == 'V':
            mname, nname = ('vmag', 'nv')

        if len(result) == 0:
            oldstar = 0
            query = """INSERT INTO %s (starid, %s, %s, ra, dec, coord)
            VALUES ('%s', %f, 1, %f, %f, point(%f,%f))""" % \
                    (self.dbname, mname, nname, starid, mag, ra, dec, ra, dec)

        # if star exists: add up magnitudes, increase counter
        else:
            oldstar = 1
            oldid = result[0][0]
            query = """UPDATE %(dbname)s
            SET %(mname)s = %(mname)s + %(mag)f, %(nname)s = %(nname)s + 1
            WHERE starid = '%(oldid)s';
            """ % {
                'dbname': self.dbname,
                'mname': mname,
                'nname': nname,
                'mag': mag,
                'oldid': oldid
            }
        self.wifsip.execute(query)
        return oldstar

    def update_magnitudes(self):
        if self.filtercol == 'B':
            magfield, nfield = 'bmag', 'nb'
        elif self.filtercol == 'V':
            magfield, nfield = 'vmag', 'nv'
        query = """UPDATE %(dbname)s
        SET %(magfield)s=%(magfield)s/%(nfield)s
        WHERE %(nfield)s>1;
        UPDATE %(dbname)s
        SET %(magfield)s=NULL
        WHERE %(nfield)s=0;""" % \
                {'dbname': self.dbname, 'magfield': magfield, 'nfield': nfield}

        self.wifsip.execute(query)

    def update_sigmas(self):
        """
        update the photometric erros after the magnitude has been calculated
        """
        import numpy as np

        if self.filtercol == 'V':
            field = 'vmag'
        elif self.filtercol == 'B':
            field = 'bmag'
        query = """SELECT starid, coord 
            FROM %s 
            WHERE (NOT bv IS NULL) AND (%s_err IS NULL);""" % (self.dbname,
                                                               field)
        starlist = self.wifsip.query(query)
        for star in starlist:
            print('%5d ' % starlist.index(star), end=' ')
            print('%-24s: %-25s' % star, end=' ')
            query = """SELECT phot.objid, mag_auto-corr 
                FROM phot, frames
                WHERE object like '%s %%'
                AND phot.objid=frames.objid
                AND filter='%s'
                AND flags<8
                AND point%s <@ circle(phot.coord,1./3600.)
                ORDER BY abs(corr)
                LIMIT 5;""" % (self.objname, self.filtercol, star[1])
            result = self.wifsip.query(query)
            mags = np.array([r[1] for r in result])
            try:
                err = np.std(mags)
                print(mags, end=' ')
                print('%.3f %.4f' % (np.mean(mags), err))
                if np.isfinite(err):
                    query = "UPDATE %s SET %s_err=%f WHERE starid='%s';" % \
                            (self.dbname, field, err, star[0])
                    self.wifsip.execute(query)
            except TypeError:
                print('no data')

    def update_bv(self):
        """
        just calculate the B-V
        """
        query = "UPDATE %s SET bv = bmag-vmag;" % self.dbname
        self.wifsip.execute(query)
Example #22
0
               WHERE corotid = %d;""" % corotid
    wifsip.execute(query)


def set_bad(corotid):
    query = """UPDATE ngc2236
               SET good=False
               WHERE corotid = %d;""" % corotid
    wifsip.execute(query)


if __name__ == '__main__':
    from glob import glob
    from datasource import DataSource

    wifsip = DataSource(host='pina', database='wifsip', user='******')
    pdfs = glob('/work2/jwe/NGC2236/plots/good/*.pdf')
    for p in pdfs:
        cid = corotid(p)
        print p, cid, 'good'
        set_good(cid)

    pdfs = glob('/work2/jwe/NGC2236/plots/*.pdf')
    for p in pdfs:
        cid = corotid(p)
        print p, cid, 'bad'
        set_bad(cid)

    wifsip.commit()
    wifsip.close()
Example #23
0
class ImportRef(object):
    '''
    classdocs
    '''
    def __init__(self):
        '''
        Constructor
        '''
        self.data = []
        from datasource import DataSource
        self.wifsip = DataSource(database=config.dbname,
                                 user=config.dbuser,
                                 host=config.dbhost)

    def loadfromfile(self, filename=''):
        """
           1-  5  I5    ---     Seq       Sequential identification number
           7-  8  I2    h       RAh       Right ascension (J2000)
          10- 11  I2    min     RAm       Right ascension (J2000)
          13- 18  F6.3  s       RAs       Right ascension (J2000)
          20- 21  I2    deg     DEd       Declination (J2000)
          23- 24  I2    arcmin  DEm       Declination (J2000)
          26- 30  F5.2  arcsec  DEs       Declination (J2000)
          31- 39  F9.3  arcmin  xpos      x-frame coordinate in arcmin
          40- 48  F9.3  arcmin  ypos      y-frame coordinate in arcmin
          49- 57  F9.3  mag     b-y       ?=99.99 b-y color index
          58- 65  F8.4  mag   e_b-y       ?=9.99  error in b-y
          66- 74  F9.3  mag     Vmag      ?=99.99 V magnitude
          75- 82  F8.4  mag   e_Vmag      ?=9.99  error in Vmag
          83- 91  F9.3  mag     m1        ?=99.99 Stroemgren m1 color index
          92- 99  F8.4  mag   e_m1        ?=9.99  error in m1
         100-108  F9.3  mag     c1        ?=99.99 Stroemgren c1 color index
         109-116  F8.4  mag   e_c1        ?=9.99  error in c1
         117-125  F9.3  mag     Hb        ?=99.99 Stroemgren Hbeta color index
         126-133  F8.4  mag   e_Hb        ?=9.99  error in Hbeta
         135-136  A2    ---     Mem       [M NM] Membership label
        """
        self.names = [
            'Seq', 'RAh', 'RAm', 'RAs', 'DEd', 'DEm', 'DEs', 'xpos', 'ypos',
            'b-y', 'e_b-y', 'Vmag', 'e_Vmag', 'm1', 'e_m1', 'c1', 'e_c1', 'Hb',
            'e_Hb', 'Mem'
        ]
        print filename
        self.data = np.genfromtxt(filename,
                                  dtype=None,
                                  names=self.names,
                                  missing_values=('99.990', '9.9900'))

    def todatabase(self):
        import StringIO
        from astropy.coordinates import SkyCoord  # High-level coordinates @UnresolvedImport
        import astropy.units as u
        vallines = []
        for d in self.data:
            params = dict(zip(self.names, d))
            radec = '%(RAh)s %(RAm)s %(RAs)s %(DEd)s %(DEm)s %(DEs)s' % params
            c = SkyCoord(radec,
                         unit=(u.hourangle, u.deg))  # @UndefinedVariable
            params['ra'] = c.ra.deg
            params['dec'] = c.dec.deg
            params['starid'] = 'M67_BGJ%04d' % params['Seq']
            params['source'] = '2007A&A...470..585B'
            if params['b-y'] == 99.990: params['b-y'] = np.nan
            if params['m1'] == 99.990: params['m1'] = np.nan
            if params['c1'] == 99.990: params['c1'] = np.nan
            if params['Hb'] == 99.990: params['Hb'] = np.nan
            try:
                vallines.append(
                    '%(starid)s\t%(ra).7f\t%(dec).7f\t%(b-y).3f\t%(m1).3f\t%(c1).3f\t%(Hb).3f\t%(source)s'
                    % params)
            except TypeError:
                print params
        values = '\n'.join(vallines)
        # replace NULL values with \N
        values = values.replace('nan', '\\N')
        print values
        f = StringIO.StringIO(values)
        self.wifsip.cursor.copy_from(f,
                                     'referencestars',
                                     columns=('starid', 'ra', 'dec', '"b-y"',
                                              'm1', 'c1', 'beta', 'source'))
        self.wifsip.commit()
        self.wifsip.close()