예제 #1
0
파일: comm14.py 프로젝트: amerand/PRIMA
def reduce030(demo=False, max_GD_um=1.0, max_err_um=0.35, max_length_s=1.0,
              sigma_clipping=4.0, onlyScans=False, min_scan_snr=3.0):
    global astro_files030, data_directory030, scan_files030
    if not onlyScans:
        for f in astro_files030:        
            print '### reducing ', os.path.join(data_directory030,f) , '######' 
            # typical reduction
            a = prima.drs(os.path.join(data_directory030,f))
            a.astrometryFtk(writeOut=(not demo), overwrite=True,
                            max_length_s=max_length_s,plot=demo,
                            max_GD_um=max_GD_um, max_err_um=max_err_um,
                            sigma_clipping=sigma_clipping)
            if demo:
                break
            a.raw.close()

    for f in scan_files030:        
        print '### reducing ', os.path.join(data_directory030,f) , '########' 
        # typical reduction
        a = prima.drs(os.path.join(data_directory030,f))
        a.astrometryScan(writeOut=(not demo), overwrite=True,
                         plot=demo, min_snr=min_scan_snr,
                         sigma_clipping=sigma_clipping)
        if demo:
            break
        a.raw.close()
    print "#########################################################"
    print " remember to copy reduced files to internal hard drive !"
    print "#########################################################"
    return
예제 #2
0
파일: comm16.py 프로젝트: amerand/PRIMA
def analyse241_HD10268(reduceData=False, bootstrap=False):
    """
    astrometric fit
    """
    files = files_241_HD10268
    if reduceData:
        for f in files:
            print '#'*5, f, '#'*12
            a = prima.drs(data_directory+f)
            a.astrometryFtk(max_err_um=5, max_GD_um=3.0, sigma_clipping=4.0,
                            writeOut=True, overwrite=True, max_length_s=1.0)
            del a
    selection = range(len(files))
    selection=[0,1,2,4,5,6]
    if bootstrap:
        files = [files[k] for k in selection]
        res = astrom.bootstrapListOfFiles(files, data_directory, N=500,
                                          fit_param  =[1,    1,    1],
                                          plot=True,
                                          first_guess=[6.0, 200.0,-3.92104],
                                          maxResidualsMas=30, multi=True)
    else:
        astrom.interpListOfFiles(files, data_directory,
                                 plot=True,
                                 fit_param  =[1,    1,    1],
                                 first_guess=[6.0, 200.0,1.0],
                                 fit_only_files=selection,maxResiduals=20)
        res = []
    return res
예제 #3
0
파일: comm16.py 프로젝트: amerand/PRIMA
def analyse238_HD10360(reduceData=False, bootstrap=False):
    """
    astrometric fit
    """
    files = files_238_10360
    if reduceData:
        for f in files:
            a = prima.drs(data_directory+f)
            print '#'*5, f, '#'*12
            a.astrometryFtk(max_err_um=2.0, max_GD_um=3.0, sigma_clipping=4.0,
                            writeOut=True, overwrite=True, max_length_s=1.0)
            del a
            a=[]

    selection = range(len(files))
    #selection = [0,1,2,3,5]
    #selection = [1,3,5]
    if bootstrap:
        files = [files[k] for k in selection]
        astrom.bootstrapListOfFiles(files, data_directory, N=500,
                                    fit_param  =[1,    1,    1], plot=True,
                                    first_guess=[6.0, 200.0,-3.92104])
    else:
        res = astrom.interpListOfFiles(files, data_directory,
                                       plot=True,quiet=False,
                                       fit_param  =[1,    1,    1],
                                       first_guess=[6.0, 200.0,-3.92104],
                                       fit_only_files=selection,
                                       maxResiduals=5)
    return
예제 #4
0
파일: comm16.py 프로젝트: amerand/PRIMA
def analyse236_HD202730(reduceData=False, bootstrap=False):
    """
    astrometric fit
    """
    files = files_236
    if reduceData:
        for f in files:
            print '#'*5, f, '#'*12
            a = prima.drs(data_directory+f)
            a.astrometryFtk(max_err_um=5, max_GD_um=3.0, sigma_clipping=3.0,
                            writeOut=True, overwrite=True, max_length_s=1.0)
            del a
            if f == '2011-08-23/PACMAN_OBJ_ASTRO_236_0007.fits':
                tmp=pyfits.open(data_directory+\
                               '/2011-08-23/PACMAN_OBJ_ASTRO_236_0007_RED.fits',
                               mode='update')
                tmp[0].header.update('HIERARCH ESO INS MODE', 'SWAPPED')
                tmp.flush()
                tmp.close()

    selection = range(len(files))
    #selection = [0,1,4,5,8,9]
    if bootstrap:
        files = [files[k] for k in selection]
        astrom.bootstrapListOfFiles(files, data_directory, N=500,
                                    fit_param  =[1,    1,    1], plot=True,
                                    first_guess=[6.0, 200.0,-3.92104],
                                    maxResidualsMas=20, multi=True)
    else:
        astrom.interpListOfFiles(files, data_directory,
                                 plot=True,
                                 fit_param  =[1,    1,    1],
                                 first_guess=[6.0, 200.0,1.0],
                                 fit_only_files=selection, maxResiduals=20 )
    return
예제 #5
0
파일: comm15.py 프로젝트: amerand/PRIMA
def analyse200_HD156274(reduction=False):
    """
    astrometric fit
    """

    files = files_200_HD156274
    print len(files)

    if reduction:
        for f in files:
            a = prima.drs(os.path.join(data_directory,f))
            a.astrometryFtk(writeOut=True, max_err_um=4.0, max_GD_um=3.0, 
                            correctA_B=True, overwrite=True, sigma_clipping=6.0)
            del a

    selection = [0,1,4,5,6,7,8,9,10,11,17,18,19] # normal
    
    selection = range(len(files)) # ALL files
    #selection = filter(lambda x: x!=6, selection) # merged
    #selection = filter(lambda x: x!=9, selection) # merged
    #selection = filter(lambda x: x!=20, selection) # merged

    print len(selection)
    astrom.interpListOfFiles(files, data_directory,
                             plot=True,
                             fit_param  =[1, 1, 1,      0,0,0],
                             first_guess=[8,256,5.15523,0,0,0],
                             fit_only_files=selection,
                             maxResiduals=10)
    return
예제 #6
0
파일: comm15.py 프로젝트: amerand/PRIMA
def analyse201_HD156274(reReduce=False):
    """
    astrometric fit
    STS-AT4 rotation test
    """

    files = files_201_HD156274
    print len(files)

    if reReduce:
        for f in files:
            a = prima.drs(os.path.join(data_directory,f))
            a.astrometryFtk(writeOut=True, max_GD_um = 2.0,
                            overwrite=True, max_err_um=10, sigma_clipping=5.0)
            del a
    
    selection = range(len(files)) # ALL files
    selection = filter(lambda x: x!=17, selection) # 17 has A-B overflow
    selection = filter(lambda x: x!=1, selection) # 17 has A-B overflow
    selection = filter(lambda x: x!=21, selection) # 17 has A-B overflow
    #selection = [0,1,2,3,4,5,6,7,8,9,19,20,21] # normal
    #selection = [0,1,5,9,19,20,21] # normal, no rotation
    #selection = [10,11,12,13,14,15,16,18, 22, 23] # swapped, 17 has A-B overflow
    #selection = [10,18,22,23] # swapped, no rotation
    #selection = [0,1,5,9,19,20,21,10,18,22,23]
    astrom.interpListOfFiles(files, data_directory,
                             plot=True,
                             fit_param  =[1, 1, 1,      0,0,0],
                             first_guess=[10.08,258.02,5.15523,0,0,0],
                             fit_only_files=selection,
                             maxResiduals=35)
    return
    at4 = prima.pssRecorder('/Volumes/DATA/PRIMA/COMM15/pssRecorder/pssguiRecorder_lat4fsm_2011-07-19_23-49-45.dat')
    at3 = prima.pssRecorder('/Volumes/DATA/PRIMA/COMM15/pssRecorder/pssguiRecorder_lat3fsm_2011-07-19_23-49-45.dat')
    pyplot.figure(1)
    pyplot.clf()
    pyplot.plot(at4.mjd[::10], at4.data['Dermec.[deg]'][::10],
                color='orange', linewidth=5, label='AT4 derot', alpha=0.9)
    pyplot.plot(at3.mjd[::10], at3.data['Dermec.[deg]'][::10],
                linestyle='dashed',
                color='green', linewidth=2, label='AT3 derot')
    pyplot.legend()
    pyplot.xlim(5.5762e4+0.01, 5.5762e4+0.17)
    pyplot.annotate('modulation\ncommand', xy=(5.5762e4+0.025, 108),
                    xycoords='data', size=12,
                    xytext=(5.5762e4+0.04, 120), textcoords='data',
                    arrowprops=dict(arrowstyle="->"))
    pyplot.annotate('tracking\nwrapping', xy=(5.5762e4+0.1, 160),
                    xycoords='data', size=12,
                    xytext=(5.5762e4+0.06, 150), textcoords='data',
                    arrowprops=dict(arrowstyle="->"))
    pyplot.annotate('swapping', xy=(5.5762e4+0.067, 60),
                    xycoords='data', size=12,
                    xytext=(5.5762e4+0.03, 50), textcoords='data',
                    arrowprops=dict(arrowstyle="->"))
    pyplot.xlabel('MJD')
    pyplot.ylabel('mechanical position (degrees)')
                  
    return
예제 #7
0
파일: opdmodel.py 프로젝트: amerand/PRIMA
def exctractOpdFromFiles(files):
    """
    run the .expectedOPD methon from the PRIMA drs and pickled it in a
    '.opd' file.
    """
    for f in files:
        a = prima.drs(f)
        opd = a.expectedOPD(plot=False)
        g = open(f+'.opd', 'wb')
        print 'saving:', f+'.opd' 
        cPickle.dump(opd, g, 2)
        g.close
        a.raw.close()
예제 #8
0
파일: astrom.py 프로젝트: amerand/PRIMA
def doAll(files, directory='', reduceData=False, bootstrap=False,
          selection=None, maxResiduals=None, pssRec=None,
          firstGuess=[10.,90., 0.0], fittedParam=[1,1,1],
          res2fits=False, max_GD_um=5.0, sigma_clipping=3.5,
          max_err_um=2, max_length_s=1.0):
    """
    astrometric data reduction and fit of a list of files. bootstrap=True also
    provides a statistical estimate of the error bar in the separation vector.
    """
    # -- data reduction
    if reduceData:
        for f in files:
            print '#'*3, 'reducing:', f, '#'*5
            a = prima.drs(directory+f, pssRec=pssRec)
            if 'OBJ_ASTRO' in f: # reduce dual fringe tracking data
                a.astrometryFtk(max_err_um=max_err_um, max_GD_um=max_GD_um,
                                sigma_clipping=sigma_clipping,
                                max_length_s=max_length_s, writeOut=True,
                                overwrite=True)
            else: # reduce scanning data:
                a.asrtrometryScan(max_err_um=max_err_um, max_GD_um=max_GD_um,
                                sigma_clipping=sigma_clipping,
                                     writeOut=True,
                                overwrite=True)
            a.raw.close()
            a = ""

    # -- data fitting (astrometry)
    if selection is None:
        selection = range(len(files))
    if bootstrap: # boostrapped error bar
        files = [files[k] for k in selection]
        t0 = time.time()
        res = bootstrapListOfFiles(files, directory, N=500,
                                          fit_param  =fittedParam, plot=True,
                                          first_guess=firstGuess,
                                          maxResidualsMas=maxResiduals,
                                          multi=True)
        print 'bootstraping performed in', round(time.time()-t0, 1), 's'
        return res
    else: # simple leastsquare fit of thge separation vector
        interpListOfFiles(files, directory,
                                 plot=True, quiet=False,
                                 fit_param  = fittedParam,
                                 first_guess= firstGuess,
                                 fit_only_files=selection,
                                 maxResiduals=maxResiduals, res2fits=res2fits)
        res = []
    return
예제 #9
0
파일: comm14.py 프로젝트: amerand/PRIMA
def reduce034(demo=False, max_GD_um=1.0, max_err_um=0.3, max_length_s=1.0,
              sigma_clipping=5.0):
    global astro_files034, data_directory034
    for f in astro_files034:        
        if f.split('_')[2] == 'ASTRO':            
            print '### reducing ', f , '###################################' 
            # typical reduction
            a = prima.drs(os.path.join(data_directory034,f))
            a.astrometry(writeOut=(not demo), overwrite=True,
                         max_length_s=max_length_s,plot=demo,
                         max_GD_um=max_GD_um, max_err_um=max_err_um,
                         sigma_clipping=sigma_clipping)
            if demo:
                break
            a.raw.close()
    return
예제 #10
0
def main():
    if not os.path.exists(sys.argv[1]):
        print 'ERROR: file '+sys.argv[1]+' does not exist'
    if len(sys.argv)>2:
        if not sys.argv[2].split('=')[0]=='pssguiRecorderDir':
            print 'ERROR: second argument should be pssguiRecorderDir=/xxx/xxx/xxx'
        if not os.path.exists(sys.argv[2].split('=')[1]):
            print 'ERROR: directory '+sys.argv[2].split('=')[1]+' does not exist'
        else:
            pssDir = sys.argv[2].split('=')[1]
    else:
        pssDir=None
    a = prima.drs(sys.argv[1], pssRec=pssDir)
    a.astrometryFtk(writeOut=1, overwrite=1, max_GD_um=2.5, max_err_um = 2.0,
                    sigma_clipping=3.5)
    a=[]
예제 #11
0
파일: comm16.py 프로젝트: amerand/PRIMA
def doAll(files, directory=data_directory, reduceData=False, bootstrap=False,
          selection=None, maxResiduals=None, firstGuess=[10.,90., 0.0],
          fittedParam=[1,1,1], res2fits=False):
    """
    astrometric fit
    """
    # -- data reduction
    if reduceData:
        for f in files:
            print '#'*5, f, '#'*12
            a = prima.drs(data_directory+f)
            a.astrometryFtk(max_err_um=3, max_GD_um=5.0, sigma_clipping=4.0,
                            writeOut=True, overwrite=True, max_length_s=1.0)
            a.raw.close()
            del a
            # correct a bug in this particular file
            if f == '2011-08-23/PACMAN_OBJ_ASTRO_236_0007.fits':
                tmp=pyfits.open(data_directory+\
                               '/2011-08-23/PACMAN_OBJ_ASTRO_236_0007_RED.fits',
                                mode='update')
                tmp[0].header.update('HIERARCH ESO INS MODE', 'SWAPPED')
                tmp.flush()
                tmp.close()

    # -- data fitting
    if selection is None:
        selection = range(len(files))
    if bootstrap:
        files = [files[k] for k in selection]
        t0 = time.time()
        res = astrom.bootstrapListOfFiles(files, data_directory, N=500,
                                          fit_param  =fittedParam, plot=True,
                                          first_guess=firstGuess,
                                          maxResidualsMas=maxResiduals,
                                          multi=True)
        print 'bootstraping performed in', round(time.time()-t0, 1), 's'
    else:
        astrom.interpListOfFiles(files, data_directory,
                                 plot=True, quiet=False,
                                 fit_param  = fittedParam,
                                 first_guess= firstGuess,
                                 fit_only_files=selection,
                                 maxResiduals=maxResiduals, res2fits=res2fits)
        res = []
    return
예제 #12
0
파일: comm14.py 프로젝트: amerand/PRIMA
def analyseDark(sub_dir):
    """
    sub routine for 'checkIfDarksAreContaminated'    
    """
    global data_directory
    files = os.listdir(data_directory+sub_dir)
    
    darksA = []
    darksB = []
    flatsA  = []
    flatsB  = []
    mjd_obs = []
    ditA = []
    ditB = []
    for f in files:
        if f[:16]=='PACMAN_OBJ_ASTRO':
            print f
            a = prima.drs(data_directory+sub_dir+f)
            try:
                darksA.append(a.fsu_calib[('FSUA', 'DARK')])
                darksB.append(a.fsu_calib[('FSUB', 'DARK')])
                flatsA.append(a.fsu_calib[('FSUA', 'FLAT')])
                flatsB.append(a.fsu_calib[('FSUB', 'FLAT')])
                mjd_obs.append(a.getKeyword('DATE-OBS'))
                ditA.append(a.getKeyword('ISS PRI FSU1 DIT'))
                ditB.append(a.getKeyword('ISS PRI FSU2 DIT'))
            except:
                print 'no calibs?'
            a.__del__()
    #--
    darksA = numpy.array(darksA)
    darksB = numpy.array(darksB)
    flatsA = numpy.array(flatsA)
    flatsB = numpy.array(flatsB)
    ditA = numpy.array(ditA)
    ditB = numpy.array(ditB)
    # end analyseDark
    return mjd_obs, darksA, darksB, flatsA, flatsB, ditA, ditB
예제 #13
0
파일: comm16.py 프로젝트: amerand/PRIMA
def FSM_full_test(model=False, AT='AT4'):
    """
    """
    directory = '/Volumes/DATA500/PRIMA/COMM16/'
    ### AT4
    if AT=='AT4' or AT==4:
        AT = 'AT4'; DT = .6/(24*3600)
        #a = prima.drs(directory+'2011-08-29/PACMAN_OBJ_ASTRO_242_0014.fits')
        #p = prima.pssRecorder(directory+'PSSRECORDER/pssguiRecorder_lat4vcm_2011-08-30_09-09-54.dat')
        #f = open(directory+'PIEZOSCAN/pscsosfPiezoScan2_lat4fsm_2011-08-30T09_10_46.txt')
        a = prima.drs(directory+'2011-08-29/PACMAN_OBJ_ASTRO_242_0018.fits')
        p = prima.pssRecorder(directory+'PSSRECORDER/pssguiRecorder_lat4vcm_2011-08-30_09-49-23.dat')
        f = open(directory+'PIEZOSCAN/pscsosfPiezoScan2_lat4fsm_2011-08-30T09_50_11.txt')
        #a = prima.drs(directory+'2011-08-29/PACMAN_OBJ_ASTRO_242_0021.fits')
        #p = prima.pssRecorder(directory+'PSSRECORDER/pssguiRecorder_lat4vcm_2011-08-30_10-24-43.dat')
        #f = open(directory+'PIEZOSCAN/pscsosfPiezoScan2_lat4fsm_2011-08-30T10_25_14.txt')
        #a = prima.drs(directory+'2011-08-29/PACMAN_OBJ_ASTRO_242_0023.fits')
        #p = prima.pssRecorder(directory+'PSSRECORDER/pssguiRecorder_lat4vcm_2011-08-30_10-35-52.dat')
        #f = open(directory+'PIEZOSCAN/pscsosfPiezoScan2_lat4fsm_2011-08-30T10_36_31.txt')
    else:
        ### AT3
        AT = 'AT3'; DT=0.75/(24.*3600)
        #a = prima.drs(directory+'2011-08-29/PACMAN_OBJ_ASTRO_242_0016.fits')
        #p = prima.pssRecorder(directory+'PSSRECORDER/pssguiRecorder_lat3vcm_2011-08-30_09-27-37.dat')
        #f = open(directory+'PIEZOSCAN/pscsosfPiezoScan2_lat3fsm_2011-08-30T09_28_22.txt')
        #a = prima.drs(directory+'2011-08-29/PACMAN_OBJ_ASTRO_242_0020.fits')
        #p = prima.pssRecorder(directory+'PSSRECORDER/pssguiRecorder_lat3vcm_2011-08-30_10-09-40.dat')
        #f = open(directory+'PIEZOSCAN/pscsosfPiezoScan2_lat3fsm_2011-08-30T10_10_09.txt')
        #a = prima.drs(directory+'2011-08-29/PACMAN_OBJ_ASTRO_242_0024.fits')
        #p = prima.pssRecorder(directory+'PSSRECORDER/pssguiRecorder_lat3vcm_2011-08-30_10-41-53.dat')
        #f = open(directory+'PIEZOSCAN/pscsosfPiezoScan2_lat3fsm_2011-08-30T10_43_04.txt')
        #a = prima.drs(directory+'2011-08-29/PACMAN_OBJ_ASTRO_242_0026.fits')
        #p = prima.pssRecorder(directory+'PSSRECORDER/pssguiRecorder_lat3vcm_2011-08-30_10-53-32.dat')
        #f = open(directory+'PIEZOSCAN/pscsosfPiezoScan2_lat3fsm_2011-08-30T10_54_20.txt')
        a = prima.drs(directory+'2011-08-29/PACMAN_OBJ_ASTRO_242_0027.fits')
        p = prima.pssRecorder(directory+'PSSRECORDER/pssguiRecorder_lat3vcm_2011-08-30_11-00-29.dat')
        f = open(directory+'PIEZOSCAN/pscsosfPiezoScan2_lat3fsm_2011-08-30T11_00_58.txt')

    lines = f.readlines()
    f.close()
    lines = filter(lambda x: not '#' in x and len(x)>10, lines)
    mjd =  [astro.tag2mjd('2011-08-30T'+x.split()[1])+DT for x in lines]
    xmjd = np.linspace(min(mjd), max(mjd), 200)

    FSM1X = [float(x.split()[2]) for x in lines]
    FSM1Y = [float(x.split()[3]) for x in lines]
    FSM2X = [float(x.split()[4]) for x in lines]
    FSM2Y = [float(x.split()[5]) for x in lines]

    print 'PCR START:', a.raw[0].header['ESO PCR ACQ START']
    print 'PCR END  :', a.raw[0].header['ESO PCR ACQ END']

    min_mjd = min(mjd)
    max_mjd = max(mjd)
    if model:
        param = np.array([0,0,.1,.1,.1,.1])
        mjd_primet =  1e-6*a.raw['METROLOGY_DATA'].data.field('TIME')/\
                   (24*3600)+astro.tag2mjd(a.raw[0].header['ESO PCR ACQ START'])
        primet =  (a.raw['METROLOGY_DATA'].data.field('DELTAL')-
                   a.raw['METROLOGY_DATA'].data.field('DELTAL').mean())*1e6
        print 'w_fit:'
        w_fit = np.where((mjd_primet>min_mjd)*(mjd_primet<max_mjd))
        #w_fit = np.where((mjd_primet>min_mjd))
        w_fit = (w_fit[0][::200],)
        print 'x_fit:'
        X_fit = [mjd_primet[w_fit]-mjd_primet[w_fit].mean(),
                 interpByStep(mjd_primet[w_fit], mjd, np.array(FSM1X)-FSM1X[0]),
                 interpByStep(mjd_primet[w_fit], mjd, np.array(FSM1Y)-FSM1Y[0]),
                 interpByStep(mjd_primet[w_fit], mjd, np.array(FSM2X)-FSM2X[0]),
                 interpByStep(mjd_primet[w_fit], mjd, np.array(FSM2Y)-FSM2Y[0])]
        Y_fit = primet[w_fit]
        print 'fit:'
        fit = myfit.fit(primetFsmBias, X_fit, param, Y_fit)
        fit.leastsqfit()
        print 'dOPD_um/(FSM1X_um - %5.3f) = %6.4f' %\
              (FSM1X[0], fit.leastsq_best_param[2])
        print 'dOPD_um/(FSM1Y_um - %5.3f) = %6.4f' %\
              (FSM1Y[0], fit.leastsq_best_param[3])
        print 'dOPD_um/(FSM2X_um - %5.3f) = %6.4f' %\
              (FSM2X[0], fit.leastsq_best_param[4])
        print 'dOPD_um/(FSM2Y_um - %5.3f) = %6.4f' %\
              (FSM2Y[0], fit.leastsq_best_param[5])

        pylab.figure(4, figsize=(17,3))
        pylab.clf()
        pylab.subplots_adjust(left=0.06, bottom=0.15, right=0.96, top=0.85,
                              wspace=0.15, hspace=0.01)
        pylab.title(AT+' FSM test:'+a.filename)
        pylab.plot(mjd_primet,  primet, 'b-', label='PRIMET A-B')
        pylab.plot(mjd_primet[w_fit[0]],
                   primetFsmBias(X_fit,fit.leastsq_best_param),
                   'r-', alpha=0.5, linewidth=3, label='linear model FSM')
        pylab.plot(mjd_primet[w_fit[0]],
                   Y_fit-primetFsmBias(X_fit,fit.leastsq_best_param),
                   '-', color='g', alpha=0.5, linewidth=3, label='residuals')
        pylab.hlines([0], min(mjd), max(mjd), color='k', linestyle='dashed',
                     linewidth=2)
        pylab.legend(ncol=3, loc=('upper left' if AT=='AT4' else 'upper right'))
        pylab.xlim(min_mjd, max_mjd)
        pylab.ylabel('PRIMET A-B ($\mu$m)')

    pylab.figure(3, figsize=(17,9))
    pylab.subplots_adjust(left=0.06, bottom=0.07, right=0.96, top=0.96,
                          wspace=0.15, hspace=0.01)
    pylab.clf()

    ax1 = pylab.subplot(5,2,1)
    pylab.title(AT+' FSM test:'+a.filename)
    pylab.plot(1e-6*a.raw['METROLOGY_DATA'].data.field('TIME')/(24*3600)+
               astro.tag2mjd(a.raw[0].header['ESO PCR ACQ START']),
               (a.raw['METROLOGY_DATA'].data.field('DELTAL')-
                a.raw['METROLOGY_DATA'].data.field('DELTAL').mean())*1e6,
               'b-')
    pylab.ylabel('PRIMET A-B ($\mu$m)')

    pylab.subplot(5,2,3, sharex=ax1)
    #pylab.plot(mjd, FSM1X, '-k', markersize=8, linestyle='steps')
    pylab.plot(xmjd, interpByStep(xmjd, mjd, FSM1X), 'k-')
    pylab.ylabel('FSM1 X ($\mu$m)')

    pylab.subplot(5,2,5, sharex=ax1)
    #pylab.plot(mjd, FSM1Y, '-k', markersize=8, linestyle='steps')
    pylab.plot(xmjd, interpByStep(xmjd, mjd, FSM1Y), 'k-')
    pylab.ylabel('FSM1 Y ($\mu$m)')

    pylab.subplot(5,2,7, sharex=ax1)
    #pylab.plot(mjd, FSM2X, '-k', markersize=8, linestyle='steps')
    pylab.plot(xmjd, interpByStep(xmjd, mjd, FSM2X), 'k-')
    pylab.ylabel('FSM2 X ($\mu$m)')

    pylab.subplot(5,2,9, sharex=ax1)
    #pylab.plot(mjd, FSM2Y, '-k', markersize=8, linestyle='steps')
    pylab.plot(xmjd, interpByStep(xmjd, mjd, FSM2Y), 'k-')
    pylab.ylabel('FSM2 Y ($\mu$m)')
    pylab.xlabel('MJD')

    pylab.subplot(5,2,2, sharex = ax1, sharey= ax1)
    pylab.title(AT+' FSM test:'+a.filename)
    pylab.plot(1e-6*a.raw['METROLOGY_DATA'].data.field('TIME')/(24*3600)+
               astro.tag2mjd(a.raw[0].header['ESO PCR ACQ START']),
               (a.raw['METROLOGY_DATA'].data.field('DELTAL')-
                a.raw['METROLOGY_DATA'].data.field('DELTAL').mean())*1e6,
               'b-')

    pylab.subplot(5,2,4, sharex=ax1)
    pylab.plot(p.mjd, p.data['VCM1X[um]'], '-k')
    pylab.ylabel('VCM1 X ($\mu$m)')

    pylab.subplot(5,2,6, sharex=ax1)
    pylab.plot(p.mjd, p.data['VCM1Y[um]'], '-k')
    pylab.ylabel('VCM1 Y ($\mu$m)')

    pylab.subplot(5,2,8, sharex=ax1)
    pylab.plot(p.mjd, p.data['VCM2X[um]'], '-k')
    pylab.ylabel('VCM2 X ($\mu$m)')

    pylab.subplot(5,2,10, sharex=ax1)
    pylab.plot(p.mjd, p.data['VCM2Y[um]'], '-k')
    pylab.ylabel('VCM2 Y ($\mu$m)')
    pylab.xlim(min_mjd, max_mjd)
    del a

    return
예제 #14
0
파일: astromNew.py 프로젝트: amerand/PRIMA
def fitListOfFiles(
    files=[],
    directory="",
    fitOnlyFiles=None,
    firstGuess={"M0": 0.0, "SEP": 10.0, "PA": 90.0},
    fitOnly=None,
    doNotFit=None,
    maxResiduals=None,
    verbose=1,
    reduceData=False,
    pssRecDir=None,
    max_err_um=1.0,
    max_GD_um=2.5,
    sigma_clipping=3.5,
    plot=True,
    randomize=False,
    exportFits=None,
    exportAscii=None,
    rectified=True,
    allX=False,
):

    """ files, list of files

    directory='', where the data are

    fitOnlyFiles=None, list of index of files to fit

    firstGuess={'M0':0.0, 'SEP':10.0, 'PA':90.0}, see dOPDfunc



    fitOnly=None, doNotFit=None, list of names of variable to fit or not fit

    verbose=1, 2 for max verbosity (list of files)

    reduceData=False, : to forece reducing the data: relevant parameters:
       pssRecDir=None,
       max_err_um=2.0,
       max_GD_um=4.0,
       sigma_clipping=5.0,

    exportFits=None, exportAscii=None : give a filename to activate
 
    plot=True,
       rectified=True, plot 'rectified' residuals,
       maxResiduals=None, for the plot, for the MAX/min to this value (in um)
       
    randomize=False : to be used for bootstrapping
    """
    global dOPDfuncNiter, obs, obsFit

    if len(files) == 0:  # no files given means all of them!
        files = os.listdir(directory)
        files = filter(lambda x: ".fits" in x and not "RED" in x, files)

    if reduceData:
        for f in files:
            print "#" * 3, "reducing:", f, "#" * 5
            a = prima.drs(directory + f, pssRec=pssRecDir)
            if "OBJ_ASTRO" in f:  # reduce dual fringe tracking data
                a.astrometryFtk(
                    max_err_um=max_err_um,
                    max_GD_um=max_GD_um,
                    sigma_clipping=sigma_clipping,
                    max_length_s=1.0,
                    writeOut=True,
                    overwrite=True,
                )
            else:  # reduce scanning data:
                a.astrometryScan(
                    max_err_um=max_err_um,
                    max_GD_um=max_GD_um,
                    sigma_clipping=sigma_clipping,
                    writeOut=True,
                    overwrite=True,
                )
            a = []
    red = []  # tables of atom
    # load all reduced files
    if fitOnlyFiles is None:
        fitOnlyFiles = range(len(files))

    for k, f in enumerate(files):
        # reduced file:
        rf = f.split(".")[0] + "_RED.fits"
        #  load reduced file
        a = atom(os.path.join(directory, rf))
        # print the table of loaded files:
        if not fitOnlyFiles == None:
            if k in fitOnlyFiles:
                fit = "X"
            else:
                fit = "_"
        else:
            fit = "X"
        if verbose > 1:  # pretty printing of files, in a table:
            if k == 0:
                N = len(rf)
                print "|idx |fit|INS.MODE | TARGET     | file" + " " * 20 + "| UT " + " " * 16 + " | MJD-OBS"
            print "| %2d | %1s | %7s | %10s | %s | %s | %10.6f" % (
                k,
                fit,
                a.insmode,
                a.PS_ID[:10],
                rf.split("OBJ_")[1],
                a.date_obs[:19],
                a.mjd_obs,
            )

        red.append(a)  # list of reduced files
    if verbose:
        print ""

    # prepare observation vector for the FIT
    obs = []
    for k in range(len(red)):
        for i in range(len(red[k].d_al)):
            obs.append(
                {
                    "TARGET": red[k].data[0].header["ESO OCS PS ID"],
                    "FILENAME": red[k].filename.split("/")[-1],
                    "DOPD": red[k].d_al[i],
                    "eDOPD": red[k].d_al_err[i],
                    "LST": red[k].lst[i],
                    "MJD": red[k].mjd[i].min(),
                    "RA": red[k].radec[0],
                    "DEC": red[k].radec[1],
                    "SWAPPED": int(red[k].swapped[i]),
                    "FIT": k in fitOnlyFiles and red[k].d_al_err[i] > 0,
                    "B_XYZA": [
                        red[k].data[0].header["ESO ISS CONF T1X"] - red[k].data[0].header["ESO ISS CONF T2X"],
                        red[k].data[0].header["ESO ISS CONF T1Y"] - red[k].data[0].header["ESO ISS CONF T2Y"],
                        red[k].data[0].header["ESO ISS CONF T1Z"] - red[k].data[0].header["ESO ISS CONF T2Z"],
                        red[k].data[0].header["ESO ISS CONF A1L"] - red[k].data[0].header["ESO ISS CONF A2L"],
                    ],
                    "ROT3": red[k].rot3[i],
                    "ROT4": red[k].rot4[i],
                    "AZ3": red[k].az3[i],
                    "AZ4": red[k].az4[i],
                    "ALT3": red[k].alt3[i],
                    "ALT4": red[k].alt4[i],
                }
            )
    obsFit = filter(lambda x: x["FIT"], obs)
    obsNoFit = filter(lambda x: not x["FIT"], obs)
    t0 = time.time()

    if not doNotFit is None:
        # fitOnly = filter(lambda k: not k in doNotFit, firstGuess.keys())
        fitOnly = filter(lambda k: not any([x in k for x in doNotFit]), firstGuess.keys())
    dOPDfuncNiter = 0

    if randomize > 0:  # to be used by bootstrapping
        if randomize != True:
            np.random.seed(randomize)
        w = np.random.randint(len(obsFit), size=len(obsFit))
        tmp = []
        for k in w:
            tmp.append(obsFit[k])
        obsFit = tmp

    if "T:" in [x[:2] for x in firstGuess.keys()]:
        ### multiple targets case:
        fit = dpfit.leastsqFit(
            dOPDfuncMultiTargets,
            obsFit,
            firstGuess,
            [o["DOPD"] for o in obsFit],
            err=[o["eDOPD"] for o in obsFit],
            fitOnly=fitOnly,
            verbose=0,
            epsfcn=1e-5,
        )
        best = fit["best"]
        uncer = fit["uncer"]
        chi2 = fit["chi2"]
        model = fit["model"]

        obs = dOPDfuncMultiTargets(obs, best, addIntermediate=True)
        obsFit = dOPDfuncMultiTargets(obsFit, best, addIntermediate=True)
        obsNoFit = dOPDfuncMultiTargets(obsNoFit, best, addIntermediate=True)
        ### compute a chi2 for each targets:
        targets = list(set([x["TARGET"] for x in obsFit]))
        chi2T = {}
        rmsT = {}
        for t in targets:
            obsT = filter(lambda x: x["TARGET"] == t, obsFit)
            tmp = dOPDfuncMultiTargets(obsT, best) - np.array([x["DOPD"] for x in obsT])
            rmsT[t] = round(tmp.std() * 1e6, 2)
            chi2T[t] = round((tmp ** 2 / np.array([x["eDOPD"] for x in obsT]) ** 2).mean(), 2)
        if plot:
            print "RMS (um):", rmsT
            print "CHI2    :", chi2T
    else:
        fit = dpfit.leastsqFit(
            dOPDfunc,
            obsFit,
            firstGuess,
            [o["DOPD"] for o in obsFit],
            err=[o["eDOPD"] for o in obsFit],
            fitOnly=fitOnly,
            verbose=0,
            epsfcn=1e-5,
        )
        best = fit["best"]
        uncer = fit["uncer"]
        chi2 = fit["chi2"]
        model = fit["model"]

        obs = dOPDfunc(obs, best, addIntermediate=True)
        obsFit = dOPDfunc(obsFit, best, addIntermediate=True)
        obsNoFit = dOPDfunc(obsNoFit, best, addIntermediate=True)

    RMSum = (np.array([o["DOPD"] for o in obsFit]) - model).std() * 1e6
    if plot:
        print "residuals= %4.2f (um)" % (RMSum)
        print "CHI2= %4.2f" % (chi2)

    # print result
    tmp = best.keys()
    tmp.sort()
    if best.has_key("AT scale") and best["AT scale"] == 0:
        tmp = filter(lambda x: not "AT4" in x and "AT3" not in x, tmp)
    units = {
        "M0": "mm",
        "SEP": "arcsec",
        "PA": "deg",
        "AT3 phi": "deg",
        "AT4 phi": "deg",
        "LINRATE": "uas/day",
        "LINDIR": "deg",
    }
    if plot:
        for k in tmp:
            if uncer[k] > 0:
                nd = int(-np.log10(uncer[k]) + 2)
                form = "%" + str(max(nd, 0) + 2) + "." + str(max(nd, 0)) + "f"
                form = k + " = " + form + " +/- " + form
                print form % (round(best[k], nd), round(uncer[k], nd)),
            else:
                print k + " = " + " %f" % (best[k]),
            u = ""
            for z in units.keys():
                if z in k:
                    u = "(" + units[z] + ")"
            print u

    # export to Ascii:
    if not exportAscii is None:
        f = open(exportAscii, "w")
        tmp = best.keys()
        tmp.sort()
        for k in tmp:
            f.write("# " + k + " =" + str(best[k]) + "\n")
            if uncer[k] != 0:
                f.write("# ERR " + k + " =" + str(uncer[k]) + "\n")
        tmp = obs[0].keys()
        tmp.sort()
        f.write("# " + "; ".join([t.replace(" ", "_") for t in tmp]) + "\n")
        for o in obs:
            f.write("" + "; ".join([str(o[k]) for k in tmp]) + "\n")
        f.close()
    # export to FITS:
    if not exportFits is None:
        # create fits object
        hdu0 = pyfits.PrimaryHDU(None)
        tmp = best.keys()
        tmp.sort()
        for k in tmp:
            hdu0.header.update("HIERARCH " + k, best[k])
            if uncer[k] != 0:
                hdu0.header.update("HIERARCH ERR " + k, uncer[k])
        tmp = obs[0].keys()
        tmp.sort()
        cols = []
        for k in tmp:
            if k == "MJD":
                form = "F12.5"
            elif (
                type(obs[0][k]) == float
                or type(obs[0][k]) == np.float
                or type(obs[0][k]) == np.float32
                or type(obs[0][k]) == np.float64
            ):
                form = "F12.9"
            elif type(obs[0][k]) == str:
                form = "A" + str(max([len(o[k]) for o in obs]))
            elif (
                type(obs[0][k]) == bool
                or type(obs[0][k]) == np.bool
                or type(obs[0][k]) == np.bool_
                or type(obs[0][k]) == int
            ):
                form = "I"
            else:
                print "DEBUG:", k, obs[0][k], type(obs[0][k])
                form = ""

            if k != "B_XYZA":
                cols.append(pyfits.Column(name=k.replace(" ", "_"), format=form, array=[o[k] for o in obs]))
            else:
                cols.append(pyfits.Column(name="B_X", format="F12.8", array=[o[k][0] for o in obs]))
                cols.append(pyfits.Column(name="B_Y", format="F12.8", array=[o[k][1] for o in obs]))
                cols.append(pyfits.Column(name="B_Z", format="F12.8", array=[o[k][2] for o in obs]))
                cols.append(pyfits.Column(name="B_A", format="F12.8", array=[o[k][3] for o in obs]))

        hducols = pyfits.ColDefs(cols)
        hdub = pyfits.new_table(hducols)
        hdub.header.update("EXTNAME", "ASTROMETRY REDUCED", "")
        thdulist = pyfits.HDUList([hdu0, hdub])
        outfile = exportFits
        if os.path.exists(outfile):
            os.remove(outfile)
        print "writting ->", outfile
        thdulist.writeto(outfile)
        return

    # return fitting result
    res = {
        "BEST": best,
        "UNCER": uncer,
        "CHI2": chi2,
        "RMS": RMSum,
        "MJD_MIN": min([o["MJD"] for o in obsFit]),
        "MJD_MAX": max([o["MJD"] for o in obsFit]),
        "MJD_MEAN": np.array([o["MJD"] for o in obsFit]).mean(),
    }
    res["RA"] = {}
    res["DEC"] = {}
    for o in obs:
        if not res["RA"].has_key(o["TARGET"]):
            res["RA"][o["TARGET"]] = o["RA"]
            res["DEC"][o["TARGET"]] = o["DEC"]

    if not plot:
        return res

    # =============== PLOT =============================
    if "T:" in [x[:2] for x in best.keys()]:
        modelNoFit = dOPDfuncMultiTargets(obsNoFit, best)
    else:
        modelNoFit = dOPDfunc(obsNoFit, best)

    pyplot.figure(0, figsize=(18, 6))
    pyplot.clf()
    pyplot.subplots_adjust(left=0.05, bottom=0.07, right=0.99, top=0.95, wspace=0.01)

    if allX:
        # display all sorts of X axis
        Xs = [
            lambda o: (o["LST"] - o["RA"] + 12) % 24 - 12,
            lambda o: o["ROT4"],
            lambda o: (o["AZ4"] - o["ALT4"] - 2 * o["ROT4"]) % 360,
            lambda o: o["AZ4"],
            lambda o: o["MJD"],
        ]

        Xlabels = ["hour angle (h)", "ROT4 (degrees)", "AZ4 - ALT4 - 2*ROT4 (degrees)", "AZ4 (degrees)", "MJD"]
    else:
        # display only Hour Angle
        Xs = [lambda o: (o["LST"] - o["RA"] + 12) % 24 - 12]
        Xlabels = ["hour angle (h)"]

    for i in range(len(Xs)):
        X = Xs[i]
        Xlabel = Xlabels[i]
        if i == 0:
            ax0 = pyplot.subplot(1, len(Xs), i + 1)
        else:
            pyplot.subplot(1, len(Xs), i + 1, sharey=ax0)

        if not rectified:
            ### plot data used for the fit
            pyplot.plot(
                [X(o) if not o["SWAPPED"] else None for o in obsFit],
                [(obsFit[k]["DOPD"] - model[k]) * 1e6 for k in range(len(obsFit))],
                "ob",
                alpha=0.3,
                label="NORMAL",
            )
            pyplot.plot(
                [X(o) if o["SWAPPED"] else None for o in obsFit],
                [(obsFit[k]["DOPD"] - model[k]) * 1e6 for k in range(len(obsFit))],
                "or",
                alpha=0.3,
                label="SWAPPED",
            )
            if len(obsNoFit) > 1:
                ### plot data NOT used for the fit
                pyplot.plot(
                    [X(o) if not o["SWAPPED"] else None for o in obsNoFit],
                    [(obsNoFit[k]["DOPD"] - modelNoFit[k]) * 1e6 for k in range(len(obsNoFit))],
                    "+c",
                    alpha=0.2,
                    label="NORMAL, not fitted",
                )
                pyplot.plot(
                    [X(o) if o["SWAPPED"] else None for o in obsNoFit],
                    [(obsNoFit[k]["DOPD"] - modelNoFit[k]) * 1e6 for k in range(len(obsNoFit))],
                    "+y",
                    alpha=0.2,
                    label="SWAPPED, not fitted",
                )
            pyplot.ylabel("$O-C$ ($\mu$m)")
        else:
            ### plot data used for the fit
            pyplot.plot(
                [X(o) if not o["SWAPPED"] else None for o in obsFit],
                [(-1) ** obsFit[k]["SWAPPED"] * (obsFit[k]["DOPD"] - model[k]) * 1e6 for k in range(len(obsFit))],
                "ob",
                alpha=0.3,
                label="NORMAL",
            )
            pyplot.plot(
                [X(o) if o["SWAPPED"] else None for o in obsFit],
                [(-1) ** obsFit[k]["SWAPPED"] * (obsFit[k]["DOPD"] - model[k]) * 1e6 for k in range(len(obsFit))],
                "or",
                alpha=0.3,
                label="SWAPPED",
            )
            if len(obsNoFit) > 1:
                ### plot data NOT used for the fit
                pyplot.plot(
                    [X(o) if not o["SWAPPED"] else None for o in obsNoFit],
                    [
                        (-1) ** obsNoFit[k]["SWAPPED"] * (obsNoFit[k]["DOPD"] - modelNoFit[k]) * 1e6
                        for k in range(len(obsNoFit))
                    ],
                    "+c",
                    alpha=0.2,
                    label="NORMAL, not fitted",
                )
                pyplot.plot(
                    [X(o) if o["SWAPPED"] else None for o in obsNoFit],
                    [
                        (-1) ** obsNoFit[k]["SWAPPED"] * (obsNoFit[k]["DOPD"] - modelNoFit[k]) * 1e6
                        for k in range(len(obsNoFit))
                    ],
                    "+y",
                    alpha=0.2,
                    label="SWAPPED, not fitted",
                )

            # -- plot pupil bias correction
            # pyplot.plot([X(o) for o in obs],
            #            [(-1)**o['SWAPPED']*o['fit PUPbias']*1e6 for o in obs],
            #            '.k', label='pup runout correction')

        if i == 0:
            pyplot.ylabel("$(-1)^\mathrm{swapped}(O-C)$ ($\mu$m)")
            pyplot.text(
                min([X(o) for o in obs]),
                0,
                "RMS=" + str(round(RMSum, 2)) + "$\mu$m",
                va="center",
                ha="left",
                bbox=dict(boxstyle="round", ec=(0.4, 0.2, 0.0), fc=(1.0, 0.8, 0.0), alpha=0.2),
            )
            pyplot.legend(ncol=4, loc="upper left")
        pyplot.xlabel(Xlabel)
        if not maxResiduals is None:
            pyplot.ylim(-maxResiduals, maxResiduals)

    return res
예제 #15
0
파일: comm17.py 프로젝트: amerand/PRIMA
def OPDmodel(loadData=False, bin=10, do_fit=False):
    """
    """
    global opd_model_data
    try:
        print len(opd_model_data)
    except:
        loadData=True
    if loadData:
        files = ['2011-11-21/PACMAN_OBJ_ASTRO_326_00%s.fits'%(f) for f in
                 ['04', '05', '06', '07', '08', '09', '11', '12',
                  '13', '14', '15', '16', '17', '18', '19', '20']]
        opd_model_data = []
        for f in files:
            a = prima.drs(data_directory+f)
            opd_model_data.append(a.expectedOPD(bin=bin))
            a = []

    B = opd_model_data[0]['XYZ']
    fit = np.array([1,1,1,1])
    p_fit = np.array(B)[np.where(np.array(fit))]
    p_fixed = np.array(B)[np.where(1-np.array(fit))]

    obsN = filter(lambda x: x['INSMODE']=='NORMAL',  opd_model_data)
    obsS = filter(lambda x: x['INSMODE']=='SWAPPED', opd_model_data)
    
    plsq = leastsq(leastsqOPDModel, p_fit, args=(fit, p_fixed, obsN), epsfcn=1e-3)
    Bf = np.array(B)
    Bf[np.where(fit)] = plsq[0]

    print np.array(B)-np.array(Bf)

    for x in opd_model_data:
        x['ORIGINAL']=prima.projBaseline(x['XYZA'], [x['RA'], x['DEC']],
                                         x['LST'])['opd']/x['AIRN']-x['DL2-DL1']
        x['RESIDUALS']=prima.projBaseline(Bf, [x['RA'], x['DEC']],
                                          x['LST'])['opd']/x['AIRN']-x['DL2-DL1']
    obsN = filter(lambda x: x['INSMODE']=='NORMAL', opd_model_data)
    obsS = filter(lambda x: x['INSMODE']=='SWAPPED', opd_model_data)
    obs = opd_model_data
    
    pyplot.figure(0)
    pyplot.clf()
    pyplot.subplot(221)
    pyplot.plot([x['AZ'] for x in obsN], [x['RESIDUALS'].mean() for x in obsN], 'ob',
                label='NORMAL')
    pyplot.plot([x['AZ'] for x in obsS], [x['RESIDUALS'].mean() for x in obsS], 'or',
                label='SWAPPED')
    pyplot.plot([x['AZ'] for x in obsN], [x['ORIGINAL'].mean() for x in obsN], '+b',
                label='NORMAL')
    pyplot.plot([x['AZ'] for x in obsS], [x['ORIGINAL'].mean() for x in obsS], '+r',
                label='SWAPPED')
    #pyplot.legend() 
    pyplot.xlabel('AZ')
    pyplot.ylabel('residuals')
    pyplot.subplot(222)
    pyplot.plot([x['ALT'] for x in obsN], [x['RESIDUALS'].mean() for x in obsN], 'ob',
                label='NORMAL')
    pyplot.plot([x['ALT'] for x in obsS], [x['RESIDUALS'].mean() for x in obsS], 'or',
                label='SWAPPED')
    pyplot.plot([x['ALT'] for x in obsN], [x['ORIGINAL'].mean() for x in obsN], '+b',
                label='NORMAL')
    pyplot.plot([x['ALT'] for x in obsS], [x['ORIGINAL'].mean() for x in obsS], '+r',
                label='SWAPPED')
    #pyplot.legend() 
    pyplot.xlabel('ALT')
    pyplot.ylabel('residuals')
    pyplot.subplot(223)
    pyplot.plot(range(len(obs)), [x['RESIDUALS'].mean() for x in obs], 'ok',
                label='NORMAL')
    pyplot.plot(range(len(obs)), [x['ORIGINAL'].mean() for x in obs], '+k',
                label='NORMAL')
    #pyplot.legend() 
    pyplot.xlabel('N')
    pyplot.ylabel('residuals')
    return opd_model_data