def run_ell(file_in, file_tab, file_dat, mag, **ellipse_par):    

    # prochaine etape : mettre en argument sous une certaine forme les parametres associes epar des taches 1) strict minimum sous forme de dico fixe 2) tout pouvoir changer sous forme de dictionnaire et tester les cles ...

    # import iraf packages
 
    iraf.stsdas()
    iraf.analysis()
    iraf.isophote()

    p = Popen("ds9", shell = True)
    time.sleep(5)

    iraf.display(file_in, 1)  

    set_iraf_param(ellipse_par)

    # iraf.lpar(iraf.geompar)

    iraf.ellipse(file_in, file_tab, mag0 = str(mag), verbose = "no")
    
    # iraf.lpar(iraf.isoimap)
    
    iraf.isoimap(file_in, file_tab)

    iraf.tproject(file_tab, "temp.fits", "SMA,INTENS,INT_ERR,ELLIP,ELLIP_ERR, PA, PA_ERR, X0, Y0, TFLUX_C, TMAG_E,NPIX_E")
    
    iraf.tprint("temp.fits", pwidth = 200, Stdout = file_dat)
    
    iraf.imdel("temp.fits")

    p.kill()
Esempio n. 2
0
def displayImage(fitsfile):

    # Now open ds9 (this assumes no ds9 instance is yet running)
    d = ds9.ds9()

    iraf.display.setParam("image", fitsfile)
    iraf.display(mode="h")

    if saveImage:
        # Zoom to fit
        d.set('zoom to fit')

        # Change the colormap and scaling
        #d.set('cmap bb')
        #d.set('scale log')

        # Add a label
        #d.set('regions command {text 30 20 #text="Fun with pyds9" font="times 18 bold"}')

        # Now you can play in ds9 to your heart's content.
        # Check back to see what the current color scale is.
        #print d.get('scale')

        # Finally, save your completed image (including regions or labels)
        d.set('saveimage png %s2.png' % fitsfile)
        d.pid.terminate()
        time.sleep(2)
Esempio n. 3
0
    def viewCubeMov(self, inCube, step=10, **kwargs):

        print "\nSTEPPING THROUGH " + inCube

        # obtain number of wavelength samples in cube
        hdul = fits.open(inCube)
        nWave = hdul["SCI"].data.shape[0]

        # step through cube, summing images within each chunk
        cubeChunk = "chunk.fits"
        for i in range(0, nWave, step):
            iraf.imdelete(cubeChunk)

            if i + 9 > nWave - 1:
                lastFrame = str(nWave - 1)
            else:
                lastFrame = str(i + 9)

            iraf.imcombine(inCube + "[SCI][*,*," + str(i) + ":" + lastFrame + \
             "]", cubeChunk, project="yes", combine="sum", logfile="")
            print "Displaying chunk [" + str(i) + ":" + lastFrame + "]"
            iraf.display(cubeChunk, frame="1", contrast=0.)
            time.sleep(2)

        # delete the last wavelength chunk and close the cube
        iraf.imdelete(cubeChunk)
        hdul.close()

        return
Esempio n. 4
0
def get_cal_spec_line(filename1, filename2):
    print 'Run get_cal_spec_line, apall...'
    outname = 'a' + filename1
    if os.path.isfile(outname):
        print outname, 'is already exist'
    else:
        print 'display %s 1' % filename1
        iraf.display(image = filename1, frame = 1)
        iraf.apall(input = filename1
                , output = outname, apertures = '', format = 'multispec'
                , references = filename2, profiles = '', interactive = True
                , find = False, recenter = False, resize = False
                , edit = False, trace = False, fittrace = False
                , extract = True, extras = False, review = True
                , line = 'INDEF', nsum = 10, lower = -15.0
                , upper = 15.0, apidtable = '', b_function = 'chebyshev'
                , b_order = 1, b_sample = '-30:-20,20:30', b_naverage = -3
                , b_niterate = 0, b_low_reject = 3.0, b_high_reject = 3.0
                , b_grow = 0.0, width = 5.0, radius = 10.0
                , threshold = 0.0, nfind = 1, minsep = 5.0
                , maxsep = 1000.0, order = 'increasing', aprecenter = ''
                , npeaks = 'INDEF', shift = True, llimit ='INDEF'
                , ulimit = 'INDEF', ylevel = 0.1, peak = True
                , bkg = True, r_grow = 0.0, avglimits = False
                , t_nsum = 10, t_step = 10, t_nlost = 3, t_function = 'spline3'
                , t_order = 3, t_sample = '*', t_naverage = 1
                , t_niterate = 0, t_low_reject = 3.0, t_high_reject = 3.0
                , t_grow = 0.0, background = 'none', skybox = 1
                , weights = 'none', pfit = 'fit1d', clean = False
                , saturation = 'INDEF', readnoise = 9.4, gain = 0.35
                , lsigma = 4.0, usigma = 4.0, nsubaps = 1)
    print 'splot %s' % outname
    iraf.splot(images = outname)
    return outname
Esempio n. 5
0
 def photautocal(self,reference,reflimit=30,selflimit=30):
     """Find the rough WCS for this image, based on reference star_phootmetry,
     by cross-identifying stars by ID in DS9"""
     iraf.display(self.image,1)
     temp = iraf.pdump(self.photfile,"xcen,ycen,id","MAG<%f"%selflimit,Stdout=1)
     iraf.tvmark(1,"STDIN",Stdin=temp)
     iraf.display(reference.image,2)
     temp = iraf.pdump(reference.photfile,"xcen,ycen,id","MAG<%f"%reflimit,Stdout=1)
     iraf.tvmark(2,"STDIN",Stdin=temp)
     fred = open('tempautocal','w')
     input1=None
     input2=None
     print "Cross identify at least five stars, <q> to continue"
     while (input1!='q')and(input2!='q'):
         print
         input1 = raw_input("Star ID on Frame 1: ")
         input2 = raw_input("Matches star ID on Frame 2: ")
         try:
             star1 = self[int(input1)]
             star2 = reference[int(input2)]
         except:
             print "Known star integers required"
             continue
         fred.write("%f %f %f %f\n"%(star1[1],star1[2],star2[7],star2[8]))
     fred.close()
     iraf.ccmap(input='tempautocal',images=self.image,lngunits='degrees',
                database='database',fit='rxyscale',update='yes',inter='yes')
     print "Recalculating data"
     self.calcradec()
     print "If happy, now run ast_by_photometry"
Esempio n. 6
0
def cos_clear3(filenames):
    outname = 'c3' + filenames[0]
    if os.path.isfile(outname):
        print outname, 'is already exist'
    else:
        inname = ''
        for i in filenames:
            inname = inname + ',' + i
        inname = inname[1:]
        print 'runing cos_clear3, imcombine...'
        print 'make file', outname
        iraf.imcombine(input = inname
                , output = outname, headers = '', bpmasks = ''
                , rejmasks = '', nrejmasks = '', expmasks = ''
                , sigmas = '', imcmb = '$I', logfile = 'STDOUT'
                , combine = 'average', reject = 'avsigclip', project = False
                , outtype = 'real', outlimits = 'none', offsets = 'none'
                , masktype = '', maskvalue = 0.0, blank = 0.0
                , scale = 'none', zero = 'none', weight = 'exposure'
                , statsec = '', expname = 'EXPTIME', lthreshold = 'INDEF'
                , hthreshold = 'INDEF', nlow = 0, nhigh = 1
                , nkeep = 1, mclip = True, lsigma = 3.0
                , hsigma = 8.0, rdnoise = 'RDNOISE', gain = 'GAIN'
                , snoise = 0.0, sigscale = 0.1, pclip = -0.5, grow = 0.0)
    print 'display %s 1' % outname
    iraf.display(image = outname, frame = 1)
    valget = raw_input('Are you need to run crmedian?(y or n): ')
    if valget == 'Y' or valget == 'y':
        return cos_clear1([outname])
    else:
        return outname
Esempio n. 7
0
def cos_clear2(filenames):
    outname = 'fake_' + filenames[0]
    if os.path.isfile(outname):
        print outname, 'is already exist'
    else:
        inname = filenames[0] + ',' + filenames[1]
        print 'runing cos_clear2, imcombine...'
        print 'make file', outname
        iraf.imcombine(input = inname
                , output = outname, headers = '', bpmasks = ''
                , rejmasks = '', nrejmasks = '', expmasks = ''
                , sigmas = '', imcmb = '$I', logfile = 'STDOUT'
                , combine = 'average', reject = 'minmax', project = False
                , outtype = 'real', outlimits = 'none', offsets = 'none'
                , masktype = 'none', maskvalue = 0, blank = 0.0
                , scale = 'exposure', zero = 'none', weight = 'none'
                , statsec = '', expname = 'EXPTIME', lthreshold = 'INDEF'
                , hthreshold = 'INDEF', nlow = 0, nhigh = 1
                , nkeep = 1, mclip = True, lsigma = 3.0
                , hsigma = 10.0, rdnoise = 'RDNOISE', gain = 'GAIN'
                , snoise = 0.0, sigscale = 0.1, pclip = -0.5, grow = 0.0)
        iraf.hedit(images = outname
                , fields = 'ncombine', value = '0', add = True
                , addonly = False, delete = False, verify = False
                , show = True, update = True)
        iraf.hedit(images = outname
                , fields = 'EXPTIME', value = '0', add = True
                , addonly = False, delete = False, verify = False
                , show = True, update = True)
    print 'display %s 1' % outname
    iraf.display(image = outname, frame = 1)
    filenames.append(outname)
    return cos_clear3(filenames)
Esempio n. 8
0
def single_display(image_id, frame=1):
    """
    Display the flat field reduced image for a given image ID (as defined in the GDB).
    """
    _prep_ds9()
    produce_flat(image_id, True)
    print "single", image_id
    filename, cam = get_cam_flat_filename(image_id)
    iraf.display(filename, frame)
Esempio n. 9
0
 def displayFile(self, filename, frame=None, **kargs ):
     assert type(frame) == int
     
     self.frame( frame )
     
     if self.pyds9_sup:
         self.set( 'file ' + str(filename) )
     else:
         iraf.display( filename, frame )
Esempio n. 10
0
def single_display(image_id, frame=1):
    """
    Display the flat field reduced image for a given image ID (as defined in the GDB).
    """
    _prep_ds9()
    produce_flat(image_id, True)
    print "single", image_id
    filename, cam = get_cam_flat_filename(image_id)
    iraf.display(filename, frame)
Esempio n. 11
0
def display_image(img,frame,_z1,_z2,scale,_xcen=0.5,_ycen=0.5,_xsize=1,_ysize=1,_erase='yes'):
    goon='True'
    import glob, subprocess, os, time
    ds9 = subprocess.Popen("ps -U {:d} u | grep -v grep | grep ds9".format(os.getuid()),shell=True,stdout=subprocess.PIPE).stdout.readlines()
    if len(ds9)== 0 :   
       subproc = subprocess.Popen('ds9',shell=True)
       time.sleep(3)

    if glob.glob(img):
       from pyraf import iraf
       iraf.images(_doprint=0)
       iraf.tv(_doprint=0)
       import string,os
       if _z2: 
          try:
              sss=iraf.display(img + '[0]', frame, xcen=_xcen, ycen=_ycen, xsize=_xsize, ysize=_ysize, erase=_erase,\
                                   fill='yes', zscale='no', zrange='no', z1=_z1, z2=_z2,Stdout=1)
          except:
              print ''
              print '### ERROR: PROBLEM OPENING DS9'
              print ''
              goon='False'                 
       else:
        try:  
            sss=iraf.display(img + '[0]', frame, xcen=_xcen, ycen=_ycen, xsize=_xsize, ysize=_ysize, erase=_erase, fill='yes', Stdout=1)
        except:
            print ''
            print '### ERROR: PROBLEM OPENING DS9'
            print ''
            goon=False
 
       if scale and goon:
          answ0 = raw_input('>>> Cuts OK ? [y/n] ? [y] ')
          if not answ0: answ0='y'
          elif answ0=='no' or answ0=='NO': answ0='n' 

          while answ0=='n':
              _z11=float(string.split(string.split(sss[0])[0],'=')[1])
              _z22=float(string.split(string.split(sss[0])[1],'=')[1])
              z11 = raw_input('>>> z1 = ? ['+str(_z11)+'] ? ')
              z22 = raw_input('>>> z2 = ? ['+str(_z22)+'] ? ')
              if not z11: z11=_z11
              else: z11=float(z11)
              if not z22: z22=_z22
              else: z22=float(z22)
              print z11,z22
              sss=iraf.display(img + '[0]',frame,fill='yes', xcen=_xcen, ycen=_ycen, xsize=_xsize, ysize=_ysize, erase=_erase,\
                                   zrange='no', zscale='no', z1=z11, z2=z22, Stdout=1)
              answ0 = raw_input('>>> Cuts OK ? [y/n] ? [y] ')
              if not answ0: answ0='y'
              elif answ0=='no' or answ0=='NO': answ0='n'
       if goon:
          _z1,_z2=string.split(string.split(sss[0])[0],'=')[1],string.split(string.split(sss[0])[1],'=')[1]
    else:
        print 'Warning: image '+str(img)+' not found in the directory '
    return _z1,_z2,goon
Esempio n. 12
0
def MatchNSubtract(TargetImg,Template,OutputImage,fitgeometry="general"):
    """ Creates OutputImage =  TargetImg - Template after scaling and matching Template to TargetImg.
    fitgeometry can be set to 'rotate' when the Template is also TIRSPEC data
    Otherwise if Template is 2MASS or other instrument set it as 'general'  """
    
    AlignedImg = os.path.splitext(TargetImg)[0]+"_"+os.path.basename(Template)
    AlignedImg = os.path.splitext(AlignedImg)[0][:115]+'.fits' # Reduce filename length for iraf geopmap
    TransformDBfile = AlignImage(TargetImg,Template,AlignedImg,fitgeometry=fitgeometry)
    
    # Now get the Good sky region coordinates
    SkyCoordsFile = os.path.splitext(TargetImg)[0]+'_BlankSky.coo'
    if not os.path.isfile(SkyCoordsFile) :
        iraf.display(TargetImg,1)
        print ('For taking coordinates of good sky. Press _x_ over blank sky areas.')
        imx=iraf.imexam(Stdout=1)
        with open(SkyCoordsFile,'w') as foo :    #Creating blank sky coords files
            for line in imx :               
                foo.write(line.split()[0] +'  '+line.split()[1]+'\n')

    # Now get the regions in the image whose brightness has to be cancelled by scaling
    FluxCoordsFile = os.path.splitext(TargetImg)[0]+'_FluxRegions.coo'
    if not os.path.isfile(FluxCoordsFile) :
        iraf.display(TargetImg,1)
        print ('Press _x_ over areas you want to minimise the residual flux after subtraction')
        imx=iraf.imexam(Stdout=1)
        with open(FluxCoordsFile,'w') as foo :    #Creating Flux ares which we have to remove in subtraction
            for line in imx :               
                foo.write(line.split()[0] +'  '+line.split()[1]+'\n')

    #Now we first has to remove background from both the images.
    TargetSkySubtractedFile = os.path.splitext(TargetImg)[0]+'_SkyS.fits'
    if not os.path.isfile(TargetSkySubtractedFile):
        skyvalue = SkySubtractImage(TargetImg,TargetSkySubtractedFile,SkyCoordsFile)
    else:
        print('Warning: Using old {0} file'.format(TargetSkySubtractedFile))

    AlignedSkySubtractedFile = os.path.splitext(AlignedImg)[0]+'_SkyS.fits'
    if not os.path.isfile(AlignedSkySubtractedFile):
        skyvalue = SkySubtractImage(AlignedImg,AlignedSkySubtractedFile,SkyCoordsFile)
    else:
        print('Warning: Using old {0} file'.format(AlignedSkySubtractedFile))

    #We shall now extract the totel Flux in each tiles from both the images
    TargetFluxinTiles = ExtractTiles(TargetSkySubtractedFile,FluxCoordsFile,Summeryfunction=np.sum,hsize=7*1.5)
    TemplateFluxinTiles = ExtractTiles(AlignedSkySubtractedFile,FluxCoordsFile,Summeryfunction=np.sum,hsize=7*1.5)
    
    def DiffSquareSum(x):
        return np.sum([(targetF - x*templateF)**2 for targetF,templateF in zip(TargetFluxinTiles,TemplateFluxinTiles)])
    
    res = scipy.optimize.minimize_scalar(DiffSquareSum)
    Scale = res.x
    print('Scaling to match the fluxes is {0}'.format(Scale))
    iraf.imarith(operand1=AlignedSkySubtractedFile,op="*",operand2=Scale,result=os.path.splitext(AlignedSkySubtractedFile)[0]+'M.fits')

    iraf.imarith(operand1=TargetSkySubtractedFile,op="-",operand2=os.path.splitext(AlignedSkySubtractedFile)[0]+'M.fits',result=OutputImage)
Esempio n. 13
0
    def rectifySpec(self, inIm, **kwargs):

        # remove previous rectified spectra
        iraf.imdelete("t" + inIm)

        # transform the spectra and view the result
        iraf.gftransform(inIm, **kwargs)
        print "\nDisplaying 2D spectra"
        iraf.display("t" + inIm + "[SCI]")

        return
Esempio n. 14
0
def FlattenData():
    filters = ['J', '1113', '1184']
    for filt in filters:
        s = 'm*' + filt + '*.fits'
        files = glob.glob(s)
        flat = 'flat' + filt
        for file in files:
            out = 'f' + file
            print file, " -> ", out
            iraf.imarith(file, '/', flat, out)
            iraf.display(out, 1)
Esempio n. 15
0
	  def OnOpen(self,e):
				""" Open a file"""
				dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*", wx.OPEN)
				if dlg.ShowModal() == wx.ID_OK:
						self.filename = dlg.GetFilename()
						self.dirname = dlg.GetDirectory()
						imagename = os.path.join(self.dirname, self.filename)
						d = ds9.ds9()
						iraf.display.setParam("image", imagename)
						iraf.display(mode="h")
				
				dlg.Destroy()
Esempio n. 16
0
    def OnOpen(self, e):
        """ Open a file"""
        dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*",
                            wx.OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            self.filename = dlg.GetFilename()
            self.dirname = dlg.GetDirectory()
            imagename = os.path.join(self.dirname, self.filename)
            d = ds9.ds9()
            iraf.display.setParam("image", imagename)
            iraf.display(mode="h")

        dlg.Destroy()
Esempio n. 17
0
def label_objects(images, xfield, yfield):
    '''Given a list of images, allow the user to pick an object in the
   display and have the x and y coordinates saved as header keywords
   xfield and yfield.'''
    files = get_files(images)
    print "Please use the 'a' key to select objects"
    for file in files:
        iraf.display(file, 1)
        res = iraf.imexamine(file, Stdout=1)
        #res = iraf.rimcursor(file, Stdout=1)
        x, y = map(float, (string.split(res[2])[0:2]))

        iraf.hedit(images=file, fields=xfield, value=x, add=1, verify=0)
        iraf.hedit(images=file, fields=yfield, value=y, add=1, verify=0)
Esempio n. 18
0
def runsextractor(image, auto):
    s = "sex " + str(image)
    flag = 0
    while ~flag:
        print s
        os.system(s)
        os.system('getxycat.pl')
        iraf.display(image, 1, fill='yes')
        iraf.display(image, 2, fill='yes')
        iraf.display('check.fits', 3, fill='yes')
        iraf.display('background.fits', 4)
        iraf.tvmark(2, 'testxy.cat', color=204, radii=2)
        #iraf.tvmark(4,'testxy.cat',color=204,radii=2)
        if auto > 0.:
            break
        try:
            flag = raw_input(
                "how does it look?  1=keep, 0=adjust default.sex \n")
            flag = int(flag)
        except ValueError:
            print "Sorry, didn't get that.  Let's try one more time."
            flag = raw_input(
                "how does it look?  1=keep, 0=adjust default.sex \n")
            flag = int(flag)
        if flag > .1:
            break
        junk = raw_input("Edit default.sex.  Hit any key when ready \n")
Esempio n. 19
0
def marksn2(img,fitstab,frame=1,fitstab2='',verbose=False):
   from pyraf import iraf
   from numpy import array   #,log10
   import lsc
   iraf.noao(_doprint=0)
   iraf.digiphot(_doprint=0)
   iraf.daophot(_doprint=0)
   iraf.images(_doprint=0)
   iraf.imcoords(_doprint=0)
   iraf.proto(_doprint=0)
   iraf.set(stdimage='imt1024')
   hdr=lsc.util.readhdr(fitstab)
   _filter=lsc.util.readkey3(hdr,'filter')
   column=lsc.lscabsphotdef.makecatalogue([fitstab])[_filter][fitstab]

   rasex=array(column['ra0'],float)
   decsex=array(column['dec0'],float)


   if fitstab2:
      hdr=lsc.util.readhdr(fitstab2)
      _filter=lsc.util.readkey3(hdr,'filter')
      _exptime=lsc.util.readkey3(hdr,'exptime')
      column=lsc.lscabsphotdef.makecatalogue([fitstab2])[_filter][fitstab2]
      rasex2=array(column['ra0'],float)
      decsex2=array(column['dec0'],float)

   iraf.set(stdimage='imt1024')
   iraf.display(img + '[0]',frame,fill=True,Stdout=1)
   vector=[]
   for i in range(0,len(rasex)):
      vector.append(str(rasex[i])+' '+str(decsex[i]))

   xy = iraf.wcsctran('STDIN',output="STDOUT",Stdin=vector,Stdout=1,image=img+'[0]',inwcs='world',units='degrees degrees',outwcs='logical',\
                         formats='%10.1f %10.1f',verbose='yes')[3:]
   iraf.tvmark(frame,'STDIN',Stdin=list(xy),mark="circle",number='yes',label='no',radii=10,nxoffse=5,nyoffse=5,color=207,txsize=2)

   if verbose:
 #     print 2.5*log10(_exptime)
      for i in range(0,len(column['ra0'])):
         print xy[i],column['ra0'][i],column['dec0'][i],column['magp3'][i],column['magp4'][i],column['smagf'][i],column['magp2'][i]

   if fitstab2:
      vector2=[]
      for i in range(0,len(rasex2)):
         vector2.append(str(rasex2[i])+' '+str(decsex2[i]))
      xy1 = iraf.wcsctran('STDIN',output="STDOUT",Stdin=vector2,Stdout=1,image=img+'[0]',inwcs='world',units='degrees degrees',outwcs='logical',\
                            formats='%10.1f %10.1f',verbose='yes')[3:]
      iraf.tvmark(frame,'STDIN',Stdin=list(xy1),mark="cross",number='yes',label='no',radii=10,nxoffse=5,nyoffse=5,color=205,txsize=2)
Esempio n. 20
0
def aperphot(
    imgs,
    posname,
    aper1,
    aper2,
    aper3,
):
    postfix = '.obs'
    for i in xrange(len(imgs)):

        basename, exten = os.path.splitext(imgs[i])
        photname = basename + postfix

        meanpsf = 2.0
        std = 70.0

        iraf.display(imgs[i], frame=1)
        iraf.tvmark(frame=1,
                    coords=posname,
                    autolog='No',
                    mark='circle',
                    radii=30,
                    lengths=3,
                    font='raster',
                    color=204,
                    label='No',
                    number='Yes',
                    txsize=3)

        iraf.datapars.ccdread = 'CCDRON'
        iraf.datapars.gain = 'CCDGAIN'
        iraf.datapars.scale = 1.0
        iraf.datapars.fwhmpsf = meanpsf
        iraf.datapars.sigma = std
        iraf.datapars.filter = 'INSFLNAM'
        iraf.datapars.datamax = 1000000.0
        iraf.centerpars.cbox = 8.0
        iraf.centerpars.calgorithm = 'centroid'
        iraf.fitskypars.salgorithm = 'median'
        iraf.fitskypars.annulus = aper2  # ------ important ------
        iraf.fitskypars.dannulus = aper3  # ------ important ------
        #iraf.photpars.apertures = meanpsf  # ------ important ------
        iraf.photpars.apertures = aper1  # ------ important ------

        iraf.phot(image=imgs[i],
                  coords=posname,
                  output=photname,
                  interactive=False,
                  verify=False)
Esempio n. 21
0
    def subSky(self, inIm, **kwargs):

        print "\nSUBTRACTING SKY FROM " + inIm

        # delete previous sky-subtracted spectra
        iraf.imdelete("s" + inIm)

        # subtract the sky and view the result (as image and datacube)
        iraf.gfskysub(inIm, **kwargs)
        print "\nDisplaying 2D spectra"
        iraf.display("s" + inIm + "[SCI]")
        print "\nDisplaying data cube"
        self.viewCube("s" + inIm, extname="SCI", version="1")

        return
Esempio n. 22
0
    def viewWhiteIm(self, inIm, **kwargs):

        print "\nDISPLAYING WHITE-LIGHT IMAGE"

        idx = inIm.find(".")
        #whiteIm = inIm[:idx] + "_2d.fits"
        whiteIm = "whiteIm_" + inIm

        iraf.imdelete(whiteIm)
        iraf.imcombine(inIm + "[SCI]", whiteIm, project="yes", combine="sum", \
         logfile="")

        iraf.display(whiteIm)

        return
Esempio n. 23
0
 def autoast(self,infile='asu.fit',RAfield='RAJ2000',decfield='DEJ2000',magfield='Vmag',magoff=None,
             markmax=30,crosses=False):
     """Calibrate image from file of data (2mass etc) by making initial astrometry
     by selecting a handful of stars, them using the other cal routines
     RAfield: column with RA (decimal!).
     decfield: column with dec (decimal!).
     magfield: column to calibrate magnitude by"""
     global xpoint,ypoint
     if self.image is None:
         print "No image to calibrate data by"
         return
     RA,dec,mag = load_data(infile,RAfield,decfield,magfield)
     fig = pylab.figure(figsize=(8,8))
     pylab.clf()
     try:
         magoff = magoff or mag.max()
         pylab.scatter(RA[mag>0],dec[mag>0],s=10**(0.4*(magoff-mag[mag>0])),hold=0,alpha=0.5)
     except:
         crosses=True
     if crosses: pylab.plot(RA,dec,'k+')
     pylab.axis([RA.max(),RA.min(),dec.min(),dec.max()])
     pylab.draw()
     cid = fig.canvas.mpl_connect('button_press_event',self._on_press)
     iraf.display(self.image,1)
     iraf.tvmark(1,"STDIN",Stdin=iraf.pdump(self.photfile,"xcen,ycen,id","MAG<%f"%markmax,Stdout=1))
     print "Pick five stars with middle button, then enter their IDs in order,\n<q> to continue"
     ids = []; self.xpoint=[]; self.ypoint=[]
     select = None
     while not(select=='q'):        
         select=raw_input('Star ID: ')
         try: ids.append(int(select))
         except: pass
     fig.canvas.mpl_disconnect('button_press_event')
     fred = open('autocalcoords','w')
     for i in range(min(len(ids),len(self.xpoint))):
         try:
             x,y = self[ids[i]][[1,2]]
             locmin = ((self.xpoint[i]-RA)**2+(self.ypoint[i]-dec)**2).argmin()
             fred.write('%f %f %15.12e %15.12e\n'%(x,y,RA[locmin],dec[locmin]))
         except:
             print "Coord pair %i failed"%i
     fred.close()
     iraf.ccmap(input='autocalcoords',images=self.image,lngunits='degrees',
                database='database',fit='rxyscale',update='yes',inter='yes')
     print "Recalculating..."
     self.calcradec()
     print "If happy, now run ast_by_file"        
Esempio n. 24
0
def runsextractor(image):
    s="sex "+str(image)
    for i in range(1000):
	os.system(s)
	os.system('getxycat.pl')
	iraf.display(image,2)
	iraf.tvmark(2,'testxy.cat',color=207,radii=7)
	try:
	    flag= raw_input("how does it look?  1=keep, 0=adjust default.sex \n")
	    flag=float(flag)
	except ValueError:
	    print "Sorry, didn't get that.  Let's try one more time."
	    flag= raw_input("how does it look?  1=keep, 0=adjust default.sex \n")
	    flag=float(flag)
	if flag > .1:
	    break
	flag= raw_input("Edit default.sex.  Hit any key when ready \n")	
Esempio n. 25
0
 def displayresults(self):
     iraf.display(self.sex_image, 1, fill='yes', contrast=0.01)
     iraf.display(self.sex_image, 2, fill='yes', contrast=0.01)
     iraf.display('check.fits', 3, fill='yes', contrast=0.01)
     iraf.display('background.fits', 4, contrast=0.01)
     iraf.tvmark(2,
                 self.prefix + '_sex_xy.txt',
                 color=204,
                 radii=2,
                 mark='circle')
Esempio n. 26
0
def shift_align(filenames):
    if len(filenames) == 0:
        return ''
    fno = 1
    for i in filenames:
        iraf.display(image = i, frame = fno)
        fno += 1
    print 'The fits number is', len(filenames)
    if len(filenames) > 1:
        print 'Please check the fits pic and determine whether you need to shift the pic'
        print 'imexamine...'
        iraf.imexamine()
        valget = raw_input('Are you need to shift the fits picture?(y)')
        if valget == 'y' or valget == 'Y':
            nfilenames = [filenames[0]]
            for i2 in filenames:
                print 'implot ' + i2
                iraf.implot(image = i2)
            fno = 1
            iraf.immatch()
            while fno < len(filenames):
                valget = raw_input('Please input ' + filenames[fno] + ' xshift, yshift: ')
                valget = valget.split()
                fxshift = float(valget[0])
                fyshift = float(valget[1])
                ims_key = 1
                if os.path.isfile('shift_' + filenames[fno]):
                    valget = raw_input('shift_' + filenames[fno] + ' is already exist, are you want to delete the old one?(y):')
                    if valget == 'y' or valget == 'Y' or valget == 'yes' or valget == 'Yes':
                        os.remove('shift_' + filenames[fno])
                    else:
                        ims_key = 0
                if ims_key == 1:
                    print 'what happend????????????????'
                    iraf.imshift(input = filenames[fno]
                            , output = 'shift_' + filenames[fno], xshift = fxshift
                            , yshift = fyshift, shifts_file = '', interp_type = 'linear'
                            , boundary_type = 'nearest', constant = 0.0)
                nfilenames.append('shift_' + filenames[fno])
                fno += 1
            for i2 in xrange(len(nfilenames)):
                iraf.display(image = nfilenames[i2], frame = i2 + 1)
            iraf.imexamine()
            return cos_clear(nfilenames)
    return cos_clear(filenames)
Esempio n. 27
0
def source_list(filename):
    print('*****************************')
    print('now selecting the sources')
    iraf.display(filename, 1)
    print(
        'Please do the following:\n1. Press enter here and then click on any source in the DS9 window. \n2. Press (comma) in the middle of source(s) to get FWHM.\n3. Press q to quit. \n '
    )
    imx = iraf.imexam(Stdout=1)
    xval = np.zeros(len(imx))
    yval = np.zeros(len(imx))
    for i in range(1, len(imx)):
        xval[i] = eval(imx[i].split()[0])
        yval[i] = eval(imx[i].split()[1])
    with open('co_ordinates_list.txt', 'w') as f:
        for i in range(1, len(imx)):
            print(xval[i], '\t', yval[i], file=f)

    return (xval, yval)
Esempio n. 28
0
def CreateMatchingXYCoords(TargetImg,Template,MatchingXYcoordsFile):
    """ Creates Xref Yref X Y, a 4 coulumn matched coordinates file MatchingXYcoordsFile."""
    iraf.display(TargetImg,1)
    print('Choose _atleast_ 9 matching stars')
    print('First Select maximum number of stars from this Target image by pressing _a_ on good stars. Press q to finish.')
    imxTarget = iraf.imexam(Stdout=1)
    iraf.display(Template,1)
    print('Press q to read from '+os.path.splitext(Template)[0]+'_TemplXY.coo'+'. OR Select exactly same stars in same order from this Template image by pressing _a_ and then press q to finish.')
    imxTemplate = iraf.imexam(Stdout=1)
    if len(imxTemplate) == 0: # Default to the predefinted coo file
        with open(os.path.splitext(Template)[0]+'_TemplXY.coo') as defaultcoo:
            imxTemplate = [line.rstrip() for line in defaultcoo]

    with open(MatchingXYcoordsFile,'w') as foo :    #Creating XYXY file
        i=2
        while i < len(imxTemplate) :               
            foo.write(imxTarget[i].split()[0] +' '+imxTarget[i].split()[1]+' '+imxTemplate[i].split()[0]+' '+imxTemplate[i].split()[1]+'\n')
            i=i+2
    print("Xref Yref X Y Table created in "+MatchingXYcoordsFile)
Esempio n. 29
0
def cos_clear1(filenames):
    outname = 'c1' + filenames[0]
    if os.path.isfile(outname):
        print outname, 'is already exist'
    else:
        print 'runing cos_clear1, crmedian...'
        print 'make file', outname
        iraf.crmedian(input = filenames[0]
                , output = outname, crmask = '', median = ''
                , sigma = '', residual = '', var0 = 0.0
                , var1 = 0.0, var2 = 0.0, lsigma = 10.0
                , hsigma = 3.0, ncmed = 5, nlmed = 5
                , ncsig = 25, nlsig = 25)
    print 'display %s 1' % outname
    iraf.display(image = outname, frame = 1)
    valget = raw_input('is the fits ok(y)?')
    if valget == 'n' or valget == 'N':
        print 'I can\'t to do more, may be you can change the parameter of crmedian and run again.'
        print 'Now, let\'s get to pyraf and try yourself, the filename is: ' + outname
    return outname
Esempio n. 30
0
def iraf_fwhm():
    print(' We need to get the FWHM using IRAF tasks.')
    print(' Opening DS9 for image display.')
    os.system("ds9 &")
    filename = input('Enter the filename:')
    iraf.display(filename, 1)
    print(
        'Please do the following:\n1. Press enter here and then click on any source in the DS9 window. \n2. Press (comma) in the middle of source(s) to get FWHM.\n3. Press q to quit. \n '
    )
    imx = iraf.imexam(Stdout=1)
    sum1 = 0
    if (imx[1].split()[10] == 'INDEF'):
        fwhm = 8.0
        print('Fwhm:', 'INDEF', 'but taken as 8.0')
    else:
        for i in range(1, len(imx)):
            token = i
            sum1 += eval(imx[i].split()[10])
        fwhm = sum1 / token
        print("Average FWHM in pixels:", fwhm, ' in arc seconds:', 0.53 * fwhm)
    return (fwhm, filename)
Esempio n. 31
0
def display_triplet(root):
    # Display the original, template and difference images in 3 frames
    SN = root + "SN.fits"
    diff = root + "diff.fits"
    temp = root + "temp.fits"
    iraf.display(SN, 1)
    iraf.display(temp, 2)
    iraf.display(diff, 3)
Esempio n. 32
0
def photometry(filenames):
    """
        Do photometry of the files
        
    """
    for item in range(len(filenames)):
        print('Photometry on file', filenames[item][0])
        iraf.imstat(filenames[item][0])
        iraf.display(filenames[item][0], 1)
        print(
            'Press , (comma) on your target to get the coord and then q, but DONT press r \n'
        )
        imx = iraf.imexam(Stdout=1)
        # If there is an error like "# ^SyntaxError: unexpected EOF while parsing" then make imx[2] instead imx[3] in all places. It is mainly a file reading problem
        Xref = eval(
            imx[1].split()
            [0])  #Using the first star as ref star for cose shift calculation
        Yref = eval(imx[1].split()[1])
        if (imx[1].split()[10] == 'INDEF'):
            fwhm = 4.0
            print('fwhm:', 'INDEF', 'but taken as 4.0')
        else:
            fwhm = eval(imx[1].split()[10])
            print('fwhm:', fwhm)
        print('Reference cord:', Xref, Yref)
        file1 = open('coord_list', 'w')
        file1.write('{0:0.3f}\t{1:0.3f}\n'.format(Xref, Yref))
        file1.close()
        Xref1, Yref1 = np.loadtxt('coord_list')
        print('Xref1, Yref1', Xref1, Yref1)
        do_phot(filenames[item][0],
                'coord_list',
                fwhm=fwhm,
                sky_annulus=10.0,
                width_sky=10.,
                zeropoint=25.)

    print('All photometry is done. Enjoy! Check .phot file')
Esempio n. 33
0
def findsolution(image,alsfile,datafile,source="GSC"):
    stars = CMD.star_photometry(alsfile,image,False)
    datfile = pyfits.open(datafile)
    mydata=datfile[-1].data
    datfile.close()
    iraf.display(image,1)
    temp=iraf.pdump(alsfile,'xcen,ycen,id','yes',Stdout=1)
    iraf.tvmark(1,"STDIN",Stdin=temp)
    rafield,decfield = fields[source]
    ra = mydata.field(rafield)
    dec= mydata.field(decfield)
    raint = int(min(ra))
    decint= int(min(dec))
    print "ra offset by %i, dec by %i\n"%(raint,decint)
    clf()
    plot(ra-raint,dec-decint,'k+')
    print "Require 4 stars matched to make simple solution"
    print "Enter co-ordinates as acurately as you can:-"
    fred = open('ccmapfile','w')
    for i in range(4):
        idin=input("Star %i ID:  "%i)
        mystar=stars[idin]
        rain=input("Star %i RA:  " %i) + raint
        decin=input("Star %i DEC: "%i) + decint
        radiff = ra-rain
        decdiff = dec-decin
        radecdiff = radiff**2+decdiff**2
        locmin = radecdiff.argmin()
        raout = ra[locmin]
        decout = dec[locmin]
        fred.write("%7.2f %7.2f %10.6f %10.6f\n"%(mystar[1],mystar[2],raout,decout))
    fred.close()
    iraf.ccmap('ccmapfile','database',images=image,xcolumn=1,ycolumn=2,lngcolumn=3,
               latcolumn=4,lngunits='degrees',latunits='degrees',insystem='j2000',
               fitgeometry='rxyscale')
    findbettersolution(image,alsfile,datafile,source=source)
Esempio n. 34
0
def kait_disp():

    inlis = iraffiles('*_001.sub.fit')
    for image in inlis:
        root = image[:len(image) - 12]
        iraf.display("%s_001.fit" % root, 1)
        if os.path.exists('%s.z.can' % root):
            iraf.tvmark(1, '%s.z.can' % root, radii=10)
        if os.path.exists('%s.mp.cor' % root):
            iraf.tvmark(1, '%s.mp.cor' % root, radii=10)
        iraf.display('%s.fit' % root, 2)
        iraf.display('%s_001.sub.fit' % root, 3)
        if os.path.exists('%s.z.cans' % root):
            iraf.tvmark(3, '%s.z.cans' % root, radii=10)
        iraf.imexam()
Esempio n. 35
0
def kait_disp():

    inlis=iraffiles('*_001.sub.fit')
    for image in inlis:
        root=image[:len(image)-12]
        iraf.display("%s_001.fit" % root, 1)
        if os.path.exists('%s.z.can' % root):
            iraf.tvmark(1,'%s.z.can' % root, radii=10)
        if os.path.exists('%s.mp.cor' % root):
            iraf.tvmark(1,'%s.mp.cor' % root, radii=10)
        iraf.display('%s.fit' % root,2)
        iraf.display('%s_001.sub.fit' % root, 3)
        if os.path.exists('%s.z.cans' % root):
            iraf.tvmark(3, '%s.z.cans' % root, radii=10)
        iraf.imexam()
Esempio n. 36
0
def CheckFiles():

	os.chdir('/Volumes/DATA/NITES/Results/M71/CheckCalibs/')
	os.system('cp ~/login.cl .')
	
	from pyraf import iraf
	
	templist=commands.getoutput('ls').split('\n')

	flat,dark,bias,night=[],[],[],[]

	for i in range(0,len(templist)):
		os.chdir(templist[i])
		
		print "\nCurrent Directory: " + str(templist[i])
		 
		iraf.display(image='Flat.fits', frame=1)
		#iraf.imexam(frame=1,image='Flat.fits',input='Flat.fits')
		x=raw_input("(Good? y/n): ")
		flat.append(x)
		
		iraf.display(image='Dark.fits', frame=2)
		#iraf.imexam(frame=2,image='Dark.fits',input='Dark.fits')
		y=raw_input("(Good? y/n): ")
		dark.append(y)
		
		iraf.display(image='Zero.fits', frame=3)
		#iraf.imexam(frame=3,image='Zero.fits',input='Zero.fits')
		z=raw_input("(Good? y/n): ")
		bias.append(z)
		
		night.append(templist[i])
		
		os.chdir('../')
	
	return 0
Esempio n. 37
0
def ecpsf(img, ofwhm, threshold, interactive, ds9, fixaperture=False,_catalog=''):
    try:
        import agnkey
        import string

        hdr = agnkey.util.readhdr(img + '.fits')
        instrument = agnkey.util.readkey3(hdr, 'instrume')
        print 'INSTRUMENT:', instrument

        if 'PIXSCALE' in hdr:
            pixelscale = agnkey.util.readkey3(hdr, 'PIXSCALE')
        elif 'CCDSCALE' in hdr:
            if 'CCDXBIN' in hdr:
                pixelscale = agnkey.util.readkey3(hdr, 'CCDSCALE') * agnkey.util.readkey3(hdr, 'CCDXBIN')
            elif 'CCDSUM' in hdr:
                pixelscale = agnkey.util.readkey3(hdr, 'CCDSCALE') * int(
                    string.split(agnkey.util.readkey3(hdr, 'CCDSUM'))[0])

        if instrument in ['kb05', 'kb70', 'kb71', 'kb73', 'kb74', 'kb75', 'kb76', 'kb77', 'kb78', 'kb79']:
            scale = pixelscale
            _datamax = 45000
        elif instrument in ['fl02', 'fl03', 'fl04']:
            scale = pixelscale
            _datamax = 120000
        elif instrument in ['fs01', 'em03']:
            scale = pixelscale
            _datamax = 65000
        elif instrument in ['fs02', 'fs03']:
            scale = pixelscale
            _datamax = 65000
        elif instrument in ['em01']:
            scale = pixelscale
            _datamax = 65000
        try:
            _wcserr = agnkey.util.readkey3(hdr, 'wcserr')
            if float(_wcserr) == 0:
                if instrument in ['kb05', 'kb70', 'kb71', 'kb73', 'kb74', 'kb75', 'kb76', 'kb77', 'kb78', 'kb79']:
                    seeing = float(agnkey.util.readkey3(hdr, 'L1FWHM')) * .75
                elif instrument in ['fl02', 'fl03', 'fl04']:
                    seeing = float(agnkey.util.readkey3(hdr, 'L1FWHM')) * .75
                elif instrument in ['fs01', 'fs02', 'fs03', 'em03', 'em01']:
                    if 'L1FWHM' in hdr:
                        seeing = float(agnkey.util.readkey3(hdr, 'L1FWHM')) * .75
                    elif 'L1SEEING' in hdr:
                        seeing = float(agnkey.util.readkey3(hdr, 'L1SEEING')) * scale
                    else:
                        seeing = 3
                else:
                    seeing = 3
            else:
                seeing = float(agnkey.util.readkey3(hdr, 'PSF_FWHM'))
                sys.exit('astrometry not good')
        except:
            sys.exit('astrometry not good')

        fwhm = seeing / scale
        print 'FWHM[header]  ', fwhm, '   in pixel'
        if ofwhm:
            fwhm = float(ofwhm)
        print '    FWHM[input]  ', fwhm, ' in pixel'

        if interactive:
            iraf.display(img, 1, fill=True)
            iraf.delete('tmp.lo?', verify=False)
            print '_' * 80
            print '>>> Mark reference stars with "a". Then "q"'
            print '-' * 80
            iraf.imexamine(img, 1, wcs='logical', logfile='tmp.log', keeplog=True)
            xyrefer = iraf.fields('tmp.log', '1,2,6,15', Stdout=1)
            xns, yns, _fws = [], [], []
            ff = open('_ap.coo', 'w')
            for i in range(len(xyrefer)):
                xns.append(float(xyrefer[i].split()[0]))
                yns.append(float(xyrefer[i].split()[1]))
                _fws.append(float(xyrefer[i].split()[3]))
                ff.write('%10.3f %10.3f %7.2f \n' % (xns[i], yns[i], float(_fws[i])))
            ff.close()

        elif _catalog:
#            cat1=agnkey.agnastrodef.readtxt(_catalog)
#            cat1['ra'],cat1['dec']
            ddd=iraf.wcsctran(input=_catalog,output='STDOUT',Stdout=1,image=img,inwcs='world',outwcs='logical',
                              units='degrees degrees',columns='1 2',formats='%10.1f %10.1f',verbose='no')
            ddd=[i for i in ddd if i[0]!='#']
            ddd=['  '.join(i.split()[0:3]) for i in ddd]
            ff = open('_ap.coo', 'w')
            for i in ddd:
                a,b,c = string.split(i)
                #print a,b,c
                ff.write('%10s %10s %10s \n' % (a, b, c))
            ff.close()
            print 'use catalog'
        else:
            xs, ys, ran, decn, magbest, classstar, fluxrad, bkg = runsex(img, fwhm, threshold, scale)
            ff = open('_ap.coo', 'w')
            for i in range(len(xs)):
                    ff.write('%10.3f %10.3f %7.2f \n' % (xs[i], ys[i], float(fluxrad[i])))
            ff.close()  ## End automatic selection

        print 80 * "#"
        photmag = apfit(img, fwhm, hdr, interactive, _datamax, fixaperture)

        radec = iraf.wcsctran(input='STDIN', output='STDOUT', Stdin=photmag, \
                              Stdout=1, image=img, inwcs='logical', outwcs='world', columns="1 2", \
                              format='%13.3H %12.2h', min_sig=9, mode='h')[3:]

        exptime = agnkey.util.readkey3(hdr, 'exptime')
        object = agnkey.util.readkey3(hdr, 'object').replace(' ', '')
        filtro = agnkey.util.readkey3(hdr, 'filter')

        #######################################
        rap, decp, magp2, magp3, magp4, smagf = [], [], [], [], [], []
        merrp2, merrp3, merrp4, smagerrf = [], [], [], []
        rap0, decp0 = [], []
        for i in range(len(radec)):
            aa = radec[i].split()
            rap.append(aa[0])
            decp.append(aa[1])
            rap0.append(agnkey.agnabsphotdef.deg2HMS(ra=aa[0]))
            decp0.append(agnkey.agnabsphotdef.deg2HMS(dec=aa[1]))
            idp = aa[2]
            magp2.append(aa[3])
            magp3.append(aa[4])
            magp4.append(aa[5])
            merrp2.append(aa[6])
            merrp3.append(aa[7])
            merrp4.append(aa[8])

        tbhdu = pyfits.new_table(pyfits.ColDefs([
            pyfits.Column(name='ra', format='20A', array=np.array(rap)),
            pyfits.Column(name='dec', format='20A', array=np.array(decp)),
            pyfits.Column(name='ra0', format='E', array=np.array(rap0)),
            pyfits.Column(name='dec0', format='E', array=np.array(decp0)),
            pyfits.Column(name='magp2', format='E', array=np.array(np.where((np.array(magp2) != 'INDEF'),
                                                                            np.array(magp2), 9999), float)),
            pyfits.Column(name='magp3', format='E', array=np.array(np.where((np.array(magp3) != 'INDEF'),
                                                                            np.array(magp3), 9999), float)),
            pyfits.Column(name='magp4', format='E', array=np.array(np.where((np.array(magp4) != 'INDEF'),
                                                                            np.array(magp4), 9999), float)),
            pyfits.Column(name='merrp2', format='E', array=np.array(np.where((np.array(merrp2) != 'INDEF'),
                                                                             np.array(merrp2), 9999), float)),
            pyfits.Column(name='merrp3', format='E', array=np.array(np.where((np.array(merrp3) != 'INDEF'),
                                                                             np.array(merrp3), 9999), float)),
            pyfits.Column(name='merrp4', format='E', array=np.array(np.where((np.array(merrp4) != 'INDEF'),
                                                                             np.array(merrp4), 9999), float)),
            pyfits.Column(name='smagf', format='E', array=np.array(np.where((np.array(magp2) != 'INDEF'),
                                                                            np.array(magp2), 9999), float)),
            pyfits.Column(name='smagerrf', format='E', array=np.array(np.where((np.array(merrp2) != 'INDEF'),
                                                                               np.array(merrp2), 9999), float)),

        ]))
        hdu = pyfits.PrimaryHDU(header=hdr)
        thdulist = pyfits.HDUList([hdu, tbhdu])
        agnkey.util.delete(img + '.sn2.fits')
        thdulist.writeto(img + '.sn2.fits')
        agnkey.util.updateheader(img + '.sn2.fits', 0,
                                 {'XDIM': [agnkey.util.readkey3(hdr, 'naxis1'), 'x number of pixels']})
        agnkey.util.updateheader(img + '.sn2.fits', 0,
                                 {'YDIM': [agnkey.util.readkey3(hdr, 'naxis2'), 'y number of pixels']})
        agnkey.util.updateheader(img + '.sn2.fits', 0,
                                 {'PSF_FWHM': [fwhm * scale, 'FWHM (arcsec) - computed with daophot']})
        os.chmod(img + '.sn2.fits', 0664)
        os.chmod(img + '.psf.fits', 0664)
        result = 1

    except:
        result = 0
        fwhm = 0.0
        traceback.print_exc()
    return result, fwhm * scale
Esempio n. 38
0
def display_image(img, frame, _z1, _z2, scale, _xcen=0.5, _ycen=0.5, _xsize=1, _ysize=1, _erase='yes'):
    # print "LOGX:: Entering `display_image` method/function in %(__file__)s"
    # % globals()
    goon = 'True'
    import glob
    import subprocess
    import time
    import os

    ds9 = subprocess.Popen("ps -U" + str(os.getuid()) + "|grep -v grep | grep ds9", shell=True,
                           stdout=subprocess.PIPE).stdout.readlines()
    if len(ds9) == 0:
        subproc = subprocess.Popen('ds9', shell=True)
        time.sleep(3)

    if glob.glob(img):
        from pyraf import iraf

        iraf.images(_doprint=0)
        iraf.tv(_doprint=0)
        import string
        import os

        if _z2:
            try:
                sss = iraf.display(img, frame, xcen=_xcen, ycen=_ycen, xsize=_xsize, ysize=_ysize, erase=_erase,
                                   fill='yes', zscale='no', zrange='no', z1=_z1, z2=_z2, Stdout=1)
            except:
                print ''
                print '### ERROR: PROBLEM OPENING DS9'
                print ''
                goon = 'False'
        else:
            try:
                sss = iraf.display(img, frame, xcen=_xcen, ycen=_ycen, xsize=_xsize, ysize=_ysize, erase=_erase,
                                   fill='yes', Stdout=1)
            except:
                print ''
                print '### ERROR: PROBLEM OPENING DS9'
                print ''
                goon = False

        if scale and goon:
            answ0 = raw_input('>>> Cuts OK ? [y/n] ? [y] ')
            if not answ0:
                answ0 = 'y'
            elif answ0 == 'no' or answ0 == 'NO':
                answ0 = 'n'

            while answ0 == 'n':
                _z11 = float(string.split(string.split(sss[0])[0], '=')[1])
                _z22 = float(string.split(string.split(sss[0])[1], '=')[1])
                z11 = raw_input('>>> z1 = ? [' + str(_z11) + '] ? ')
                z22 = raw_input('>>> z2 = ? [' + str(_z22) + '] ? ')
                if not z11:
                    z11 = _z11
                else:
                    z11 = float(z11)
                if not z22:
                    z22 = _z22
                else:
                    z22 = float(z22)
                print z11, z22
                sss = iraf.display(img, frame, fill='yes', xcen=_xcen, ycen=_ycen, xsize=_xsize, ysize=_ysize,
                                   erase=_erase, zrange='no', zscale='no', z1=z11, z2=z22, Stdout=1)
                answ0 = raw_input('>>> Cuts OK ? [y/n] ? [y] ')
                if not answ0:
                    answ0 = 'y'
                elif answ0 == 'no' or answ0 == 'NO':
                    answ0 = 'n'
        if goon:
            _z1, _z2 = string.split(string.split(sss[0])[0], '=')[
                1], string.split(string.split(sss[0])[1], '=')[1]
    else:
        print 'Warning: image ' + str(img) + ' not found in the directory '
    return _z1, _z2, goon
Esempio n. 39
0
def completeness():#measure completeness on final image
	
    #combinepath=bcdpath+'pbcd/Combine/output_apex_step2'
    combinepath=bcdpath+'pbcd/Combine/apex_1frame_step2'
    os.chdir(combinepath)

    file='mosaic_extract_final.tbl'
    input=open(file,'r')
    xgal=[]#positions of previous detections with snr > 3
    ygal=[]
    fap4gal=[]
    for line in input:
	if line.find('#') > -1: #skip lines with '#' in them
	    continue
	if line.find('\\') > -1: #skip lines with '#' in them
	    continue
	if line.find('|') > -1: #skip lines with '#' in them
	    continue
	t=line.split()
	xgal.append(float(t[8]))
	ygal.append(float(t[10]))
	fap4gal.append(float(t[28]))
    input.close()
    xgal=N.array(xgal,'f')
    ygal=N.array(ygal,'f')


    fsimall=[]
    matchflagsimall=[]
    f2all=[]
    f3all=[]
    f4all=[]
    deblendsimall=[]
    snrsimall=[]

    myminmag=24.75
    mymaxmag=27.4
    myfmin=10.**((25.-mymaxmag)/2.5)#ZP=25
    myfmax=10.**((25.-myminmag)/2.5)#ZP=25


    #below is loop to create image w/artificial sources, extract, and compare

    for k in range(100):
	    createflag=1.#create image w/artificial sources?
	    detectflag=1.#detect sources in image?
	    if createflag > 0.1:
		    xnoise=[]
		    ynoise=[]
		    infile=open('noisecoords.dat','r')
		    for line in infile:
			    t=line.split()
			    xnoise.append(float(t[0]))
			    ynoise.append(float(t[1]))
	    infile.close()
	
	
	    nstar=10
    
	    xsim=N.zeros(nstar,'d')
	    ysim=N.zeros(nstar,'d')
	    msim=N.zeros(nstar,'d')
	    outfile=open('stars.coords.dat','w')
	    for i in range(nstar):
	    #j=int(round(1.*len(xnoise)*random.uniform(0,1)))

	    #xsim[i]=xnoise[j]
	    #ysim[i]=ynoise[j]
		    j=0
		    for j in range(10000):
			    xt=int(round(random.uniform(5.,125.)))
			    yt=int(round(random.uniform(5.,140.)))
			    d=pylab.sqrt((xt-xgal)**2+(yt-ygal)**2)#make sure sim galaxies are not near real galaxies
			    if min(d) > -1.:
				    d2=pylab.sqrt((xt-xsim)**2+(yt-ysim)**2)#make sure sim points are not on top of each other
				    if min(d2) > 5.:
					    print i,'got a good point after ',j,' tries',xt,yt
					    break
				    j=j+1
		    xsim[i]=xt
		    ysim[i]=yt
		    k=random.uniform(myfmin,myfmax)
		    msim[i]=25.-2.5*pylab.log10(k)
	    #print k,msim[i] 
		    s='%8.2f %8.2f %8.2f \n' % (xsim[i],ysim[i],msim[i])
		    outfile.write(s)
	    outfile.close()
	      
	
	#os.system('rm stars.coords.dat')
	#iraf.starlist('stars.coords.dat',nstars=100,spatial='uniform',xmax=130,ymax=145,luminosity='uniform',minmag=22.,maxmag=30.0,mzero=22.0,sseed='INDEF',power=0.6,alpha=0.74,lseed='INDEF')
	
    
	    os.system('rm mosaic-completeness.fits')
        #iraf.mkobjects(input='mosaic_minus_median_extract.fits',output='mosaic-completeness.fits',objects='stars.coords.dat',radius=1.13,magzero=25.,background=0.,gain=5.,rdnoise=0.,poisson='no')#don't convolve w/PRF
	    #os.system('cp ../cal/MIPS24_PRF_HD_center.fits .')#convolve star w/SSC PRF
	    os.system('cp ../cal/mips24_prf_mosaic_2.45_4x.fits .')#convolve star w/SSC PRF
	    iraf.mkobjects(input='mosaic_minus_median_extract.fits',output='mosaic-completeness.fits',objects='stars.coords.dat',radius=14,star='mips24_prf_mosaic_2.45_4x.fits',magzero=25.,background=0.,gain=5.,rdnoise=0.,poisson='no')
        #os.system('cp ../cal/PRF_estimate.fits .')#convolve gaussian w/measured PRF
        #iraf.mkobjects(input='mosaic_minus_median_extract.fits',output='mosaic-completeness.fits',objects='stars.coords.dat',radius=15,star='PRF_estimate.fits',magzero=25.,background=0.,gain=5.,rdnoise=0.,poisson='no')
	    os.system('ls *.fits')
	    os.system('pwd')
	    iraf.display('mosaic_minus_median_extract.fits',1,contrast=0.01)
	    iraf.display('mosaic-completeness.fits',2,contrast=0.01)
	    iraf.tvmark(1,'stars.coords.dat')
	    iraf.tvmark(2,'stars.coords.dat')
	    fsim=10.**((25.-msim)/2.5)#ZP=25

	    if createflag < .1:#read in positions and magnitudes of artdata sources
		    xsim=[]
		    ysim=[]
		    msim=[]
		    infile=open('stars.coords.dat','r')
		    for line in infile:
			    if line.find('#') > -1:
				    continue
			    t=line.split()
			    xsim.append(float(t[0]))
			    ysim.append(float(t[1]))
			    msim.append(float(t[2]))
		    infile.close()
		    xsim=N.array(xsim,'f')
		    ysim=N.array(ysim,'f')
		    msim=N.array(msim,'f')
		    
		    fsim=10.**((25.-msim)/2.5)#ZP=25

	    if detectflag > 0.1:#now run detection on mosaic-completeness.fits
		    combinepath=bcdpath+'pbcd/Combine/'
		    os.chdir(combinepath)
		    print combinepath
		    #os.system('apex_1frame.pl -n apex_1frame_MIPS24_step2.nl -i output_apex_step2/mosaic-completeness.fits')
	
		    #combinepath=bcdpath+'pbcd/Combine/output_apex_step2'
		    os.system('apex_1frame.pl -n apex_1frame_step2all.nl -i apex_1frame_step2/mosaic-completeness.fits')
	
		    combinepath=bcdpath+'pbcd/Combine/apex_1frame_step2'
		    os.chdir(combinepath)
		    print combinepath
		    file='mosaic-completeness_extract_raw.tbl'
		    input=open(file,'r')
		    ngal=0
		    for line in input:
			    if line.find('Conversion') > -1:
				    t=line.split('=')
				    convfactor=float(t[1])#conversion from ADU to uJy
  	    #aperr=aveaperr*convfactor #convert noise in ADU to uJy using conv factor from apex
				    print "Conversion Factor = ",convfactor
	    #print "aveaperr = ",aveaperr
	    #print "aperr = ",aperr
				    continue
			    if line.find('#') > -1: #skip lines with '#' in them
				    continue
			    if line.find('\\') > -1: #skip lines with '#' in them
				    continue
			    if line.find('|') > -1: #skip lines with '#' in them
				    continue
			    ngal=ngal+1
		    input.close()
    
	

	    id24 = N.zeros(ngal,'f')
	    imagex24 = N.zeros(ngal,'f')
	    imagey24  = N.zeros(ngal,'f')
	    ra24 = N.zeros(ngal,'f')
	    dec24 = N.zeros(ngal,'f')
	    f24 = N.zeros(ngal,'d')#flux
	    errf24 = N.zeros(ngal,'d')
	    fap1 = N.zeros(ngal,'d')#flux in aperture 1 (1,1.5,2,2.6,3,3.5,4,4.5,5.,5.5) pixels
	    fap2 = N.zeros(ngal,'d')#flux
	    fap3 = N.zeros(ngal,'d')#flux
	    fap4 = N.zeros(ngal,'d')#flux in ap 4 - this is one w/ap cor of 1.67 (Calzetti et al 2007)
	    fap5 = N.zeros(ngal,'d')#flux
	    fap6 = N.zeros(ngal,'d')#flux
	    fap7 = N.zeros(ngal,'d')#flux
	    fap8 = N.zeros(ngal,'d')#flux
	    fap9 = N.zeros(ngal,'d')#flux
	    fap10 = N.zeros(ngal,'d')#flux
	    snr24 = N.zeros(ngal,'d')#SNR calculated by mopex
	    deblend = N.zeros(ngal,'f')#SNR calculated by mopex
	    

	    input=open(file,'r')
	    i=0
	    output=open('xy24raw.dat','w')
	    for line in input:
		    if line.find('#') > -1: #skip lines with '#' in them
			    continue
		    if line.find('\\') > -1: #skip lines with '#' in them
			    continue
		    if line.find('|') > -1: #skip lines with '#' in them
			    continue
	 
	
		    t=line.split()
	#print "length of t = ",len(t)
	#print (t[8]),(t[10]),(t[13]),(t[14]),(t[18]),(t[2]),(t[23]),(t[24]),(t[25]),(t[26]),(t[27]),(t[28]),(t[29]),(t[30]),(t[31]),(t[32])

		    (imagex24[i],imagey24[i],f24[i],errf24[i],snr24[i],deblend[i],fap1[i],fap2[i],fap3[i],fap4[i],fap5[i],fap6[i],fap7[i],fap8[i],fap9[i],fap10[i])=(float(t[8]),float(t[10]),float(t[13]),float(t[14]),float(t[18]),float(t[2]),float(t[25]),float(t[26]),float(t[27]),float(t[28]),float(t[29]),float(t[30]),float(t[31]),float(t[32]),float(t[33]),float(t[34]))
		    s='%6.2f %6.2f \n'%(imagex24[i],imagey24[i])
		    output.write(s)

		    i=i+1
	    input.close()#44 -> 43
	    output.close()
	    iraf.tvmark(1,'xy24raw.dat',color=204,radi=2)
	    iraf.tvmark(2,'xy24raw.dat',color=204,radi=2)
    
	    delta=1.#max number of pixels for a match

	    #get rid of objects that were detected in original image.  Otherwise, matching will think any object near a sim galaxy is the sim galaxy.  A faint galaxy placed on type of a pre-existing bright galaxy will be detected.

            newgalflag=N.ones(len(imagex24),'i')
	    for i in range(len(imagex24)):
		    (imatch, matchflag,nmatch)=findnearest(imagex24[i],imagey24[i],xgal,ygal,delta)
		    if matchflag > 0.:
			    dflux=abs(fap4gal[imatch] - fap4[i])/fap4[i]
			    if dflux < .1:#position of real galaxy, flux difference less than 10% -> not a new galaxy
				    newgalflag[i] = 0
	    #keep only galaxies that are new
	    imagex24 = N.compress(newgalflag,imagex24)
	    imagey24  = N.compress(newgalflag,imagey24)
	    fap1 = N.compress(newgalflag,fap1)
	    fap2 = N.compress(newgalflag,fap2)
	    fap3 = N.compress(newgalflag,fap3)
	    fap4 = N.compress(newgalflag,fap4)
	    fap5 = N.compress(newgalflag,fap5)
	    fap6 = N.compress(newgalflag,fap6)
	    fap7 = N.compress(newgalflag,fap7)
	    fap8 = N.compress(newgalflag,fap8)
	    fap9 = N.compress(newgalflag,fap9)
	    fap10 =N.compress(newgalflag,fap10)
	    snr24 =N.compress(newgalflag,snr24)
	    deblend = N.compress(newgalflag,deblend)

	    delta=2.#max number of pixels for a match
	    matchflagsim=N.zeros(len(xsim),'i')
	    fmeas1=N.zeros(len(xsim),'f')
	    fmeas2=N.zeros(len(xsim),'f')
	    fmeas3=N.zeros(len(xsim),'f')
	    fmeas4=N.zeros(len(xsim),'f')
	    fmeas5=N.zeros(len(xsim),'f')
	    fmeas6=N.zeros(len(xsim),'f')
	    fmeas7=N.zeros(len(xsim),'f')
	    fmeas8=N.zeros(len(xsim),'f')
	    fmeas9=N.zeros(len(xsim),'f')
	    fmeas10=N.zeros(len(xsim),'f')
	    fmeas24=N.zeros(len(xsim),'f')
	    deblendsim=N.zeros(len(xsim),'f')
	    snrsim=N.zeros(len(xsim),'f')
	    for i in range(len(xsim)):
		    (imatch, matchflag,nmatch)=findnearest(xsim[i],ysim[i],imagex24,imagey24,delta)
		    matchflagsim[i]=matchflag
		    if matchflag > .1:
			    fmeas1[i]=fap1[int(imatch)]
			    fmeas2[i]=fap2[int(imatch)]
			    fmeas3[i]=fap3[int(imatch)]
			    fmeas4[i]=fap4[int(imatch)]
			    fmeas5[i]=fap5[int(imatch)]
			    fmeas6[i]=fap6[int(imatch)]
			    fmeas7[i]=fap7[int(imatch)]
			    fmeas8[i]=fap8[int(imatch)]
			    fmeas9[i]=fap9[int(imatch)]
			    fmeas10[i]=fap10[int(imatch)]
			    fmeas24[i]=f24[int(imatch)]
			    deblendsim[i]=deblend[int(imatch)]
			    snrsim[i]=snr24[int(imatch)]
			    



	    fsimall=fsimall+list(fsim)
	    matchflagsimall=matchflagsimall+list(matchflagsim)
	    f2all=f2all+list(fmeas2)
	    f3all=f3all+list(fmeas3)
	    f4all=f4all+list(fmeas4)
	    deblendsimall=deblendsimall+list(deblendsim)
	    snrsimall=snrsimall+list(snrsim)


    fsim=N.array(fsimall,'f')
    matchflagsim=N.array(matchflagsimall,'f')
    fmeas2=N.array(f2all,'f')
    fmeas3=N.array(f3all,'f')
    fmeas4=N.array(f4all,'f')
    deblendsim=N.array(deblendsimall,'f')
    snrsim=N.array(snrsimall,'f')


    #make plots using all realizations 
    pylab.cla()
    pylab.clf()
    fsim=fsim*convfactor
    fs=pylab.compress((matchflagsim > 0.1) & (deblendsim < 1.5),fsim)
    #f1=pylab.compress((matchflagsim > 0.1) & (deblendsim < 1.5),fmeas1)
    f2=pylab.compress((matchflagsim > 0.1) & (deblendsim < 1.5),fmeas2)
    f3=pylab.compress((matchflagsim > 0.1) & (deblendsim < 1.5),fmeas3)
    f4=pylab.compress((matchflagsim > 0.1) & (deblendsim < 1.5),fmeas4)
    #f242=pylab.compress((matchflagsim > 0.1) & (deblendsim < 1.5),fmeas24)

    r4=pylab.median(fs/f4)
    r3=pylab.median(fs/f3)
    r2=pylab.median(fs/f2)
    print "average ratios ap 4",pylab.average(fs/f4),r4,pylab.std((fs/f4)/pylab.average(fs/f2))
    print "average ratios ap 3",pylab.average(fs/f3),pylab.median(fs/f3),pylab.std((fs/f3)/pylab.average(fs/f3))
    print "average ratios ap 2",pylab.average(fs/f2),pylab.median(fs/f2),pylab.std((fs/f2)/pylab.average(fs/f2))

    s='f4 w/apcor = %3.2f(%4.2f)'%(r4,pylab.average(abs(fs-f4*r4)/fs))
    pylab.plot(fs,f4*r4,'b.',label=s)
    pylab.plot(fs,f4,'bo',label='f4')
    s='f3 w/apcor = %3.2f(%4.2f)'%(r3,pylab.average(abs(fs-f3*r3)/fs))
    pylab.plot(fs,f3*r3,'g.',label=s)
    pylab.plot(fs,f3,'go',label='f3')
    s='f2 w/apcor = %3.2f(%4.2f)'%(r2,pylab.average(abs(fs-f2*r2)/fs))
    pylab.plot(fs,f2*r2,'r.',label=s)
    pylab.plot(fs,f2,'ro',label='f2')
    #pylab.plot(fs,f1,'co',label='f1')
    #pylab.plot(fs,f242,'k.',label='f24')
    pylab.legend(loc='best')
    x=N.arange(0.,max(fs),10.)
    y=x
    pylab.plot(x,y,'k-')
    #y=2.*x
    #pylab.plot(x,y,'k--')
    #y=3.*x
    #pylab.plot(x,y,'k--')
    #y=4.*x
    #pylab.plot(x,y,'k--')
    #y=5.*x
    #pylab.plot(x,y,'k--')
    pylab.xlabel('F(24) Input')
    pylab.ylabel('F(24) measured')
    #pylab.axis([0.,50.,0.,50.])
    s=str(prefix)+'fluxcomp.eps'
    pylab.savefig(s)

    pylab.cla()
    pylab.clf()


    nbins=20
    fmin=10.#min(fsim)
    fmax=max(fsim)
    df=5.#(fmax-fmin)/(1.*nbins)
    bins=N.arange(fmin,(fmax+df),df)



    (xbin,ybin,ybinerr)=mystuff.completeness(bins,fsim,matchflagsim)
    s=str(prefix)+'FracComplvsFlux.dat'
    outdat=open(s,'w')
    print "Completeness vs Input Flux"
    for i in range(len(xbin)):
	    print i, xbin[i],ybin[i],ybinerr[i]
	    t='%8.2f %8.4f %8.4f\n'%(xbin[i],ybin[i],ybinerr[i])
	    outdat.write(t)
    outdat.close()
    #for i in range(len(fsim)):
	#if snrsim[i] > 3.:
	#    print i, fsim[i],matchflagsim[i],deblendsim[i],abs(fsim[i]-fmeas4[i]*1.67)/fsim[i],snrsim[i]
    #(xbin,ybin2,ybin2err)=mystuff.scipyhist2(bins,fmeas4)
    #pylab.plot(xbin,ybin,'bo')
    #pylab.plot(xbin,ybin2,'ro')
    #s=str(prefix)+'NDetectvsFlux.eps'
    #pylab.savefig(s)

    pylab.cla()
    pylab.clf()
    pylab.plot(xbin,ybin,'ko')
    pylab.errorbar(xbin,ybin,yerr=ybinerr,fmt=None,ecolor='k')
    s=str(prefix)+'FracComplvsFlux.eps'
    pylab.axhline(y=1.0,ls='-')
    pylab.axhline(y=.8,ls='--')
    pylab.axvline(x=80.0,ls=':',color='b')
    pylab.xlabel('Input Flux (uJy)')
    pylab.ylabel('Completeness')
    pylab.axis([0.,max(xbin)+df,-.05,1.05])

    pylab.savefig(s)
    
    os.system('cp *.eps /Users/rfinn/clusters/spitzer/completeness/.')
    os.system('cp *vsFlux.dat /Users/rfinn/clusters/spitzer/completeness/.')
Esempio n. 40
0
File: VS.py Progetto: eddienko/SamPy
def doit(fn,dp,mt):
  """ JHT: this is the module that analyses an ALFOSC image of
           a Vertical Slit, and recommends an alignment offset to the
           current APERTURE WHEEL stepper motor units."""
          

  ## First check if data file exists
  if not os.access(dp+fn+".fits",os.F_OK): 
     messageOut(mt,"File not found:  "+dp+fn+".fits\n")
     return "File not found:  "+dp+fn+".fits"

  messageOut(mt,"\nVertical-Slit analysis of file:  "+dp+fn+".fits\n")

  from pyraf import iraf
  from pyraf import gwm


  ## Read current grism wheel position from image FITS header
  iraf.images.imutil.imgets(dp+fn,"GRISM")
  grismid=iraf.images.imutil.imgets.value
  if grismid.find("pen")==-1: 
     messageOut(mt,"Vertical-Slit mode:  grism wheel should be Open\n")
     return "File  %s:  Vertical-Slit mode:  grism wheel should be Open" % fn




  ## Read current aperture wheel position from image FITS header
  iraf.images.imutil.imgets(dp+fn,"APERTUR")
  slitid=iraf.images.imutil.imgets.value
  if slitid.find("Vert")==-1: 
     messageOut(mt,"Vertical-Slit mode:  no vertical slit in aperture wheel\n")
     return "File  %s:  Vertical-Slit mode:  no vertical slit in aperture wheel" % fn

  slitwidth=float(slitid.split('_',9)[1].strip('\"'))
  messageOut(mt,"Slit width "+str(slitwidth)+"   "+slitid+"\n")

  iraf.noao(_doprint=0)
  iraf.noao.imred(_doprint=0)
  iraf.noao.imred.specred(_doprint=0)

  if not os.access("/tmp/tiasgat/",os.F_OK):
    os.mkdir("/tmp/tiasgat/")
    os.chmod("/tmp/tiasgat/",0777)
  if os.access("/tmp/tiasgat/plot",os.F_OK):     os.remove("/tmp/tiasgat/plot")
  if os.access("/tmp/tiasgat/plot2",os.F_OK):    os.remove("/tmp/tiasgat/plot2")
  if os.access("/tmp/tiasgat/aplast",os.F_OK):   os.remove("/tmp/tiasgat/aplast")
  if os.access("/tmp/tiasgat/tvmarks",os.F_OK):  os.remove("/tmp/tiasgat/tvmarks")
  if os.access("/tmp/tiasgat/logfile",os.F_OK):  os.remove("/tmp/tiasgat/logfile")

  ## Note that this will *not* update any uparm files !! (see pyraf documentation)
  iraf.noao.imred.specred.dispaxis=2
  iraf.noao.imred.specred.database="/tmp/tiasgat/"
  iraf.noao.imred.specred.plotfile="/tmp/tiasgat/plot"
  iraf.noao.imred.specred.logfile="/tmp/tiasgat/logfile"
  width=6*slitwidth    # x6 or else the widest slits go wrong
  if width<4: width=4  # smaller than 4 and apfind will not find it
  messageOut(mt,"Using width of "+str(width)+" pixels \n")
  iraf.noao.imred.specred.apedit.width=width



  ## Display image on ds9
  iraf.set(stdimage="imt512")
  iraf.display(dp+fn,1,fill="no",Stdout="/dev/null")

# Suppress IRAF query for number of apertures to find
# This is only necesary for the widest slits: then the call to
# apfind results in an empty database file, as it cannot find an aperture.
# But aptrace works fine anyway (except for the annoying query) !? 
  iraf.noao.imred.specred.apfind.setParam('nfind.p_value', 1)
  iraf.noao.imred.specred.apfind.setParam('nfind.p_mode','h')

  ## 'find' and trace spectrum; this will dump a plot to /tmp/tiasgat/plot
  lines = iraf.noao.imred.specred.apfind(dp+fn,nfind=1,interactive="no", edit="no", nsum=20, Stdout=1)
  for i in range (0,len(lines)):     messageOut(mt,lines[i]+"\n")


  lines = iraf.noao.imred.specred.aptrace(dp+fn,interactive="no",step=5,low_reject=2.5,
                   high_reject=2.5,function="leg",order=2,niterate=5,naverage=1, Stdout=1)
  for i in range (0,len(lines)):     messageOut(mt,lines[i]+"\n")


  ## Start graphics window; select the correct plot; show plot
  gwm.window("Tiasgat!  graphics")
  iraf.plot.gkiextract("/tmp/tiasgat/plot",2,Stdout="/tmp/tiasgat/plot2")
  gwm.getActiveGraphicsWindow().load("/tmp/tiasgat/plot2")


### how to read the aperture file, as output by aptrace ####
### 
### center    line 6        gives zero point
### max,min   lines 24-25   n = (2 * x - (max + min)) / (max - min)
### c1,c2     lines 26-27   
### 
### The polynomial can be expressed as the sum
###     
###             poly = sum from i=1 to order {c_i * z_i} 
###     
### where  the  the  c_i  are  the  coefficients and the z_i are defined
### interatively as:
###     
###             z_1 = 1
###             z_2 = n
###             z_i = ((2*i-3) * n * z_{i-1} - (i-2) * z_{i-2}) / (i - 1)
###     
### So for order=2 and for vertical slit/grism:   X=center+c1+c2*n
###             X=center + c1 + c2*(2 * Y - (max + min)) / (max - min)
    
### translated to X=a + bY
###    a=center + c1 - c2*(max+min)/(max-min)
###    b=2*C2/(max-min)


  ## Read the aperture definition file
  apfile=open("/tmp/tiasgat/ap"+dp.replace('/','_')+fn,'r')
  lines=apfile.readlines()
  apfile.close()
  #print lines[5], lines[23:]
  c0    = float(lines[5].split(None,9)[1].strip())
  lower = float(lines[23].strip())
  upper = float(lines[24].strip())
  c1    = float(lines[25].strip())
  c2    = float(lines[26].strip())
  a = c0 + c1 - c2*(upper+lower)/(upper-lower)
  b = 2*c2/(upper-lower)
  #print "zeropoint ", a, "    slope ",b

  ## Remove aperture definition file
  if os.access("/tmp/tiasgat/ap"+dp.replace('/','_')+fn,os.F_OK): 
    os.remove("/tmp/tiasgat/ap"+dp.replace('/','_')+fn)

  ## Mark the fit on the image display
  if os.access("/tmp/tiasgat/tvmarks",os.F_OK):   os.remove("/tmp/tiasgat/tvmarks")
  tvmarkfile=open("/tmp/tiasgat/tvmarks",'w')
  for i in range(int(lower),int(upper)+1,3):
     tvmarkfile.write(str(a+b*i)+"  "+str(i)+" 100 s \n")
  tvmarkfile.close()
  iraf.tv.tvmark(1,"",commands="/tmp/tiasgat/tvmarks",interactive="no")

  ## Read current grism wheel position from image FITS header
  iraf.images.imutil.imgets(dp+fn,"ALAPRSTP")
  oldwheelunits=float(iraf.images.imutil.imgets.value)
  #print "APERSTEP", oldwheelunits

  ## Read binning FITS headers
  iraf.images.imutil.imgets(dp+fn,"CDELT1")
  xbin=float(iraf.images.imutil.imgets.value)
  iraf.images.imutil.imgets(dp+fn,"CDELT2")
  ybin=float(iraf.images.imutil.imgets.value)
  iraf.images.imutil.imgets(dp+fn,"CRVAL1")
  xstart=float(iraf.images.imutil.imgets.value)
  messageOut(mt,"\nBinning factors "+str(int(xbin))+" x "+str(int(ybin))+"\n")
  #messageOut(mt,"Xstart "+str(int(xstart))+"\n")
  #messageOut(mt,"half "+str(int((lower+upper)/2))+"\n")
  if xbin==1:
    messageOut(mt,"\nSlitpos:   X-position at CCD center "+str((xstart-1+a+b*(lower+upper)/2))+" according to fit\n")
  else:
    messageOut(mt,"\nSlitpos:   X-binning is not 1:   set wrongly in vsalign.run ?!\n\n")

  ## Correct the angle for the binning factors.
  ## A full wheel turn corresponds to 320000 units
  offsetangle=320000 * math.atan(b*xbin/ybin) / (2*math.pi)
  messageOut(mt,"Alignment: Offset to motor units "+str(offsetangle)+"\n")

  newwheelunits=offsetangle + oldwheelunits
  if newwheelunits < 0:  newwheelunits+=320000

  return "Result for %s :  current APERTURE wheel units  %d,  suggested new value  %d" % \
         (fn, (0.5+oldwheelunits), (0.5+newwheelunits))
def absphot(img,_field,_catalogue,_fix,_color,rejection,_interactive,_type='fit',redo=False,show=False,cutmag=-1,database='dataredulco',_calib='sloan'):
    from astropy.io import fits
    import lsc
    import math
    import sys,re,string,os
    from lsc.util import readkey3, readhdr
    from numpy import array, compress, zeros, median, std, asarray, isfinite,mean
    from pyraf import iraf
    if show:
          from pylab import ion,plot,draw,clf
          import time
    iraf.noao(_doprint=0)
    iraf.digiphot(_doprint=0)
    iraf.daophot(_doprint=0)
    iraf.images(_doprint=0)
    iraf.imcoords(_doprint=0)
    iraf.proto(_doprint=0)
    t = fits.open(img)
    tbdata = t[1].data
    hdr2=t[1].header
    hdr=lsc.util.readhdr(img)
    _cat=readkey3(hdr,'catalog')
    _telescope=lsc.util.readkey3(hdr,'telescop')
    _instrume=lsc.util.readkey3(hdr,'instrume')
    _filter=lsc.util.readkey3(hdr,'filter')
    _airmass=lsc.util.readkey3(hdr,'airmass')
    _exptime=lsc.util.readkey3(hdr,'exptime')
    _date=lsc.util.readkey3(hdr,'date-obs')
    _object=lsc.util.readkey3(hdr,'object')
    _ra=lsc.util.readkey3(hdr,'RA')
    _dec=lsc.util.readkey3(hdr,'DEC')
    print _filter
    if _telescope in ['lsc','1m0-04','1m0-05','1m0-09']:     kk=lsc.sites.extintion('ctio')
    elif _telescope in ['elp','1m0-08']:                     kk=lsc.sites.extintion('mcdonald')
    elif _telescope in ['cpt','1m0-12','1m0-10','1m0-13']:   kk=lsc.sites.extintion('southafrica')
    elif _telescope in ['ftn','Faulkes Telescope North']:    kk=lsc.sites.extintion('mauna')
    elif _telescope in ['1m0-03','1m0-11','coj','fts','Faulkes Telescope South']:    kk=lsc.sites.extintion('siding')

    if _calib not in ['sloan','sloanprime','natural','apass','']:   colorefisso=lsc.sites.colfix(_instrume)
    else:                                                           colorefisso=lsc.sites.colfix(_instrume,_calib)

    print redo
    print _cat
    if _cat and not redo:
        print 'already calibrated'
    else:
     try:
           lsc.mysqldef.updatevalue(database,'zcat','X',string.split(re.sub('.sn2.fits','.fits',img),'/')[-1])
           if os.path.isfile(string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1]):
                 lsc.mysqldef.updatevalue(database,'zcat','X',string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1])
     except: print 'module mysqldef not found'

     column=makecatalogue([img])[_filter][img]

     rasex=array(column['ra0'],float)
     decsex=array(column['dec0'],float)
     if _type=='fit':
        magsex=array(column['smagf'],float)
        magerrsex=array(column['smagerrf'],float)
     elif _type=='ph':
        magsex=array(column['magp3'],float)
        magerrsex=array(column['merrp3'],float)
     else: sys.exit(_type+' not valid (ph or fit)')
     

     print len(rasex)
     if not cutmag: cutmag=99
     #else: cutmag= cutmag-2.5*math.log10(float(_exptime))
     if len(compress( array(magsex) < float(cutmag) , magsex)) < 5 : cutmag=99  # not cut if only few object
     rasex     = compress(array(magsex,float)<=cutmag,rasex)
     decsex    = compress(array(magsex,float)<=cutmag,decsex)
     magerrsex = compress(array(magsex,float)<=cutmag,magerrsex)
     magsex    = compress(array(magsex,float)<=cutmag,array(magsex))

     print len(rasex)
     if _interactive:
        iraf.set(stdimage='imt1024')
        iraf.display(re.sub('.sn2','',img),1,fill=True,Stdout=1)
        vector=[]
        for i in range(0,len(rasex)):
            vector.append(str(rasex[i])+' '+str(decsex[i]))
        xy = iraf.wcsctran('STDIN',output="STDOUT",Stdin=vector,Stdout=1,image=img,inwcs='world',units='degrees degrees',outwcs='logical',\
                               formats='%10.1f %10.1f',verbose='yes')[3:]
        iraf.tvmark(1,'STDIN',Stdin=list(xy),mark="circle",number='yes',label='no',radii=10,nxoffse=5,nyoffse=5,color=207,txsize=2)
#        raw_input('here')
     if _catalogue:
        ######## use external catalogue
        if _catalogue[0]=='/':   stdcooC=lsc.lscastrodef.readtxt(_catalogue)
        else:                   stdcooC=lsc.lscastrodef.readtxt(lsc.__path__[0]+'/standard/cat/'+_catalogue)
        rastdC,decstdL=array(stdcooC['ra'],float),array(stdcooC['dec'],float)
        lsc.util.delete('tmp.stdL.pix')
        colonne=str(stdcooC['rapos'])+'   '+str(stdcooC['decpos'])
        if _catalogue[0]=='/': 
            iraf.wcsctran(_catalogue,'tmp.stdL.pix',img,inwcs='world',units='degrees degrees',outwcs='logical',\
                          columns=colonne,formats='%10.1f %10.1f',verbose='no')
        else:
            iraf.wcsctran(lsc.__path__[0]+'/standard/cat/'+_catalogue,'tmp.stdL.pix',img,inwcs='world',units='degrees degrees',outwcs='logical',\
                          columns=colonne,formats='%10.1f %10.1f',verbose='no')
        standardpixC=lsc.lscastrodef.readtxt('tmp.stdL.pix')
        if _interactive:
            iraf.tvmark(1,'tmp.stdL.pix',mark="circle",number='yes',label='no',radii=8,nxoffse=5,nyoffse=5,color=204,txsize=2)
        xstdC=standardpixC['ra']
        ystdC=standardpixC['dec']
        idstdC=standardpixC['id']
        xstdC=compress((array(xstdC,float)<readkey3(hdr,'XDIM'))&(array(xstdC,float)>0)&(array(ystdC,float)>0)&(array(ystdC,float)<readkey3(hdr,'YDIM')),xstdC)
        xstdL=xstdLL=xstdS=xstdC
        standardpixL=standardpixLL=standardpixS=standardpixC
        stdcooL=stdcooLL=stdcooS=stdcooC
     else:
        ######## check if it is landolt field
        stdcooL=lsc.lscastrodef.readtxt(lsc.__path__[0]+'/standard/cat/landolt.cat')
        rastdL,decstdL=array(stdcooL['ra'],float),array(stdcooL['dec'],float)
        lsc.util.delete('tmp.stdL.pix')
        iraf.wcsctran(lsc.__path__[0]+'/standard/cat/landolt.cat','tmp.stdL.pix',img,inwcs='world',units='degrees degrees',outwcs='logical',\
                          columns='1 2',formats='%10.1f %10.1f',verbose='no')
        standardpixL=lsc.lscastrodef.readtxt('tmp.stdL.pix')
        if _interactive:
            iraf.tvmark(1,'tmp.stdL.pix',mark="circle",number='yes',label='no',radii=8,nxoffse=5,nyoffse=5,color=204,txsize=2)
            print 'yelow circles sextractor'

        xstdL=standardpixL['ra']
        ystdL=standardpixL['dec']
        idstdL=standardpixL['id']
        xstdL=compress((array(xstdL,float)<readkey3(hdr,'XDIM'))&(array(xstdL,float)>0)&(array(ystdL,float)>0)&(array(ystdL,float)<readkey3(hdr,'YDIM')),xstdL)

        ######## check if it is Stetson field
        stdcooLL=lsc.lscastrodef.readtxt(lsc.__path__[0]+'/standard/cat/StetsonCat.dat')
        ww=asarray([i for i in range(len(stdcooLL['ra'])) if ( abs(float(stdcooLL['ra'][i])-float(_ra))<.2 and  abs(float(stdcooLL['dec'][i])-_dec)<.2    )])
        if len(ww)>0:
            for hh in stdcooLL.keys(): 
                if type(stdcooLL[hh])!=int:
                    if hh not in ['id','ra','dec']:
                        stdcooLL[hh]=array(array(stdcooLL[hh])[ww],float)
                    else:
                        stdcooLL[hh]=array(stdcooLL[hh])[ww]
        lll=[]
        for i in range(0,len(stdcooLL['ra'])): lll.append(stdcooLL['ra'][i]+' '+stdcooLL['dec'][i])

        rastdLL,decstdLL=array(stdcooLL['ra'],float),array(stdcooLL['dec'],float)
        lsc.util.delete('tmp.stdLL.pix')
        iraf.wcsctran('STDIN','tmp.stdLL.pix',img,inwcs='world',Stdin=lll,units='degrees degrees',outwcs='logical',\
                          columns='1 2',formats='%10.1f %10.1f',verbose='no')
        if _interactive:
            iraf.tvmark(1,'tmp.stdLL.pix',mark="cross",number='yes',label='no',radii=8,nxoffse=5,nyoffse=5,color=204,txsize=2)
            print 'red crosses Stetson'

        standardpixLL={}
        for ii in stdcooLL.keys(): standardpixLL[ii]=stdcooLL[ii]
        standardpixLL['ra']=array(iraf.proto.fields('tmp.stdLL.pix',fields='1',Stdout=1),float) #standardpixLL['ra']
        standardpixLL['dec']=array(iraf.proto.fields('tmp.stdLL.pix',fields='2',Stdout=1),float) #standardpixLL['dec']
        xstdLL=array(iraf.proto.fields('tmp.stdLL.pix',fields='1',Stdout=1),float) #standardpixLL['ra']
        ystdLL=array(iraf.proto.fields('tmp.stdLL.pix',fields='2',Stdout=1),float) #standardpixLL['dec']
        idstdLL=standardpixLL['id']

        xstdLL=compress((array(xstdLL,float)<readkey3(hdr,'XDIM'))&(array(xstdLL,float)>0)&(array(ystdLL,float)>0)&(array(ystdLL,float)<readkey3(hdr,'YDIM')),xstdLL)
        ######## check if it is sloan field
        magsel0,magsel1=12,18
        _ids=lsc.lscastrodef.sloan2file(_ra,_dec,20,float(magsel0),float(magsel1),'_tmpsloan.cat')
        ascifile='_tmpsloan.cat'
        stdcooS=lsc.lscastrodef.readtxt(ascifile)
        rastdS,decstdS=array(stdcooS['ra'],float),array(stdcooS['dec'],float)
        lsc.util.delete('tmp.stdS.pix')
        iraf.wcsctran(ascifile,'tmp.stdS.pix',img,inwcs='world',units='degrees degrees',outwcs='logical',columns='1 2',formats='%10.1f %10.1f',verbose='no')
        standardpixS=lsc.lscastrodef.readtxt('tmp.stdS.pix')
        if _interactive:
            iraf.tvmark(1,'tmp.stdS.pix',mark="cross",number='yes',label='no',radii=8,nxoffse=5,nyoffse=5,color=205,txsize=2)
            print 'green cross sloan'
        xstdS=standardpixS['ra']
        ystdS=standardpixS['dec']
        idstdS=standardpixS['id']
        xstdS=compress((array(xstdS,float)<readkey3(hdr,'XDIM'))&(array(xstdS,float)>0)&(array(ystdS,float)>0)&(array(ystdS,float)<readkey3(hdr,'YDIM')),xstdS)
        ##############################################3
     if not _catalogue and len(xstdLL)>0:
             xstdL=xstdLL
             standardpixL=standardpixLL
             stdcooL=stdcooLL

     if _filter in ['U', 'B', 'V', 'R','I','Bessell-B','Bessell-V','Bessell-R','Bessell-I']:
        filters={'U':'U', 'B':'B', 'V':'V', 'R':'R', 'I':'I','Bessell-B':'B','Bessell-V':'V','Bessell-R':'R','Bessell-I':'I'}
        if _color:
            colors=lsc.myloopdef.chosecolor(_color,False)
            if not colors:             colors={'U':['UB'],'B':['UB','BV'],'V':['BV','VR'],'R':['VR','RI'],'I':['RI']}
        else:
            colors={'U':['UB'],'B':['UB','BV'],'V':['BV','VR'],'R':['VR','RI'],'I':['RI']}
        if _field=='sloan':
                standardpix,stdcoo={'ra':[9999],'dec':[9999],'id':[1]},{'ra':[9999],'dec':[9999]}
                print 'filters and field selected do not match'
        else:
            _field='landolt'
            if len(xstdL)>=1:
                    standardpix=standardpixL
                    stdcoo=stdcooL
                    if not _catalogue:
                          if len(xstdLL)>0: _catalogue='StetsonCat.dat'
                          else:             _catalogue='landolt.dat'
            elif len(xstdS)>=1:
                if not _catalogue:  _catalogue='sdss8'  
                standardpix=standardpixS
                stdcoo=stdcooS
                stdcoo=lsc.lscastrodef.transformsloanlandolt(stdcoo)
                if not _catalogue:  _catalogue='sdss8' 
                print '\n### transform sloan in landolt'
            else:
                print 'landolt, but catalogue not found'
                standardpix,stdcoo={'ra':[9999],'dec':[9999],'id':[1]},{'ra':[9999],'dec':[9999]}
     elif _filter in  ['up','gp','rp','ip','zs','SDSS-G','SDSS-R','SDSS-I','Pan-Starrs-Z']: 
        filters={'up':'u','ip':'i','gp':'g','rp':'r','zs':'z','SDSS-G':'g','SDSS-R':'r','SDSS-I':'i','Pan-Starrs-Z':'z'}
        if _color:
            colors=lsc.myloopdef.chosecolor(_color,False)
            if not colors:             colors={'i':['ri','iz'],'r':['gr','ri'],'g':['ug','gr'],'z':['iz'],'u':['ug']}
        else:
            colors={'i':['ri','iz'],'r':['gr','ri'],'g':['ug','gr'],'z':['iz'],'u':['ug']}
        if _field=='landolt':   
                standardpix,stdcoo={'ra':[9999],'dec':[9999],'id':[1]},{'ra':[9999],'dec':[9999]}
                print 'filters and field selected do not match'
        else:
            _field='sloan'
            if len(xstdS)>=1:
                if not _catalogue:  _catalogue='sdss8' 
                standardpix=standardpixS
                stdcoo=stdcooS
            elif len(xstdL)>=1:
                standardpix=standardpixL
                stdcoo=stdcooL
                stdcoo=lsc.lscastrodef.transformlandoltsloan(stdcoo)
                if not _catalogue:  _catalogue='landolt.dat' 
                print '\n### transform landolt to sloan'
            else:
                print 'sloan, but not in the sdss footprint'
                standardpix,stdcoo={'ra':[9999],'dec':[9999],'id':[1]},{'ra':[9999],'dec':[9999]}        

     xstd=standardpix['ra']
     ystd=standardpix['dec']
     idstd=standardpix['id']
     rastd,decstd=array(stdcoo['ra'],float),array(stdcoo['dec'],float)
     xstd0=compress((array(xstd,float)<readkey3(hdr,'XDIM'))&(array(xstd,float)>0)&(array(ystd,float)>0)&(array(ystd,float)<readkey3(hdr,'YDIM')),xstd)

     if len(xstd0)>1:  ########   go only if standard stars are in the field  ##########
        magstd0={}
        airmass0={}
        result={}
        fileph={}
        print '\n###  standard field: '+str(_field)
        ystd0=compress((array(xstd,float)<readkey3(hdr,'XDIM'))&(array(xstd,float)>0)&(array(ystd,float)>0)\
                               &(array(ystd,float)<readkey3(hdr,'YDIM')),ystd)
        rastd0=compress((array(xstd,float)<readkey3(hdr,'XDIM'))&(array(xstd,float)>0)&(array(ystd,float)>0)\
                                &(array(ystd,float)<readkey3(hdr,'YDIM')),rastd)
        decstd0=compress((array(xstd,float)<readkey3(hdr,'XDIM'))&(array(xstd,float)>0)&(array(ystd,float)>0)\
                                 &(array(ystd,float)<readkey3(hdr,'YDIM')),decstd)
        idstd0=compress((array(xstd,float)<readkey3(hdr,'XDIM'))&(array(xstd,float)>0)&(array(ystd,float)>0)\
                                 &(array(ystd,float)<readkey3(hdr,'YDIM')),idstd)
        stdcoo0={}
        for key in stdcoo.keys():
              if key in 'ugrizUBVRI':
                    stdcoo0[key]=compress((array(xstd,float)<readkey3(hdr,'XDIM'))&(array(xstd,float)>0)&(array(ystd,float)>0)\
                                                &(array(ystd,float)<readkey3(hdr,'YDIM')),stdcoo[key])
        ###############################################################
        #               pos0 = standard                          pos1 = sextractor
        distvec,pos0,pos1=lsc.lscastrodef.crossmatch(array(rastd0),array(decstd0),array(rasex),array(decsex),10)
        for key in stdcoo0.keys():            stdcoo0[key]=stdcoo0[key][pos0]
        rastd0=rastd0[pos0]
        decstd0=decstd0[pos0]
        idstd0=idstd0[pos0]
        rasex=rasex[pos1]
        decsex=decsex[pos1]
        # after change in may 2013 mag in sn2.fits file are already at 1s
        magsex=magsex[pos1]-kk[filters[_filter]]*float(_airmass)  #   - K x airmass
#        magsex=magsex[pos1]+2.5*math.log10(float(_exptime))-kk[filters[_filter]]*float(_airmass)  #  mag    exptime      - K x airmass
#################################################################################
        if _field=='landolt':
            print '\n###  landolt system'
            for _filtlandolt in 'UBVRI':
                if _filtlandolt==filters[_filter]:  airmass0[_filtlandolt]=  0 #_airmass
                else: airmass0[_filtlandolt]= 0
                magstd0[_filtlandolt]=stdcoo0[_filtlandolt]
            fileph['mU']=zeros(len(rastd0))+999
            fileph['mB']=zeros(len(rastd0))+999
            fileph['mV']=zeros(len(rastd0))+999
            fileph['mR']=zeros(len(rastd0))+999
            fileph['mI']=zeros(len(rastd0))+999
            fileph['V']=magstd0['V']
            fileph['BV']=array(array(magstd0['B'],float)-array(magstd0['V'],float),str)
            fileph['UB']=array(array(magstd0['U'],float)-array(magstd0['B'],float),str)
            fileph['VR']=array(array(magstd0['V'],float)-array(magstd0['R'],float),str)
            fileph['RI']=array(array(magstd0['R'],float)-array(magstd0['I'],float),str)
        elif _field=='sloan':
            for _filtsloan in 'ugriz':
                if _filtsloan==filters[_filter]:  airmass0[_filtsloan]= 0   # _airmass
                else: airmass0[_filtsloan]=0
                magstd0[_filtsloan]=stdcoo0[_filtsloan]
            fileph['mu']=zeros(len(rastd0))+999
            fileph['mg']=zeros(len(rastd0))+999
            fileph['mr']=zeros(len(rastd0))+999
            fileph['mi']=zeros(len(rastd0))+999
            fileph['mz']=zeros(len(rastd0))+999
            fileph['r']=magstd0['r']
            fileph['gr']=array(array(magstd0['g'],float)-array(magstd0['r'],float),str)
            fileph['ri']=array(array(magstd0['r'],float)-array(magstd0['i'],float),str)
            fileph['ug']=array(array(magstd0['u'],float)-array(magstd0['g'],float),str)
            fileph['iz']=array(array(magstd0['i'],float)-array(magstd0['z'],float),str)
########################################################################################
        zero=[]
        magcor=[]
        fil = open(re.sub('.fits','.ph',img),'w')
        fil.write(str(_instrume)+' '+str(_date)+'\n')
        fil.write('*** '+_object+' '+str(len(magsex))+'\n')
        if _field=='landolt':
            fil.write('%6.6s\t%6.6s\t%6.6s\t%6.6s\t%6.6s\n' % (str(1),str(1),str(1),str(1),str(1)))  # exptime
            fil.write('%6.6s\t%6.6s\t%6.6s\t%6.6s\t%6.6s\n' % (str(airmass0['U']),str(airmass0['B']),str(airmass0['V']),str(airmass0['R']),str(airmass0['I'])))
        elif _field=='sloan':
            fil.write('%6.6s\t%6.6s\t%6.6s\t%6.6s\t%6.6s\n' % (str(1),str(1),str(1),str(1),str(1)))  # exptime
            fil.write('%6.6s\t%6.6s\t%6.6s\t%6.6s\t%6.6s\n' % (str(airmass0['u']),str(airmass0['g']),str(airmass0['r']),str(airmass0['i']),str(airmass0['z'])))
        for i in range(0,len(magsex)): 
            fileph['m'+filters[_filter]][i]=magsex[i]    #  instrumental mangitude of std in pos0[i]
            if _field=='landolt':
                stringastandard='%12.12s\t%7.7s\t%7.7s\t%7.7s\t%7.7s\t%7.7s' % (idstd0[i],fileph['V'][i],fileph['BV'][i],fileph['UB'][i],\
                                                                                fileph['VR'][i],fileph['RI'][i])
                fil.write('%7.7s\t%7.7s\t%7.7s\t%7.7s\t%7.7s\t%60.60s\n' \
                              % (str(fileph['mU'][i]),str(fileph['mB'][i]),str(fileph['mV'][i]),str(fileph['mR'][i]),str(fileph['mI'][i]),str(stringastandard)))
            elif _field=='sloan':
                stringastandard='%12.12s\t%7.7s\t%7.7s\t%7.7s\t%7.7s\t%7.7s' % (idstd0[i],fileph['r'][i],fileph['gr'][i],fileph['ug'][i],\
                                                                                fileph['ri'][i],fileph['iz'][i])
                fil.write('%7.7s\t%7.7s\t%7.7s\t%7.7s\t%7.7s\t%60.60s\n' \
                              % (str(fileph['mu'][i]),str(fileph['mg'][i]),str(fileph['mr'][i]),str(fileph['mi'][i]),str(fileph['mz'][i]),str(stringastandard)))
            zero.append(float(float(magstd0[filters[_filter]][i]))-float(magsex[i]))
            magcor.append(magsex[i])
        fil.close()

        if show:
              import time
              from pylab import ion,plot,draw
              ion()
              aa=mean(compress(abs(array(zero))<99,zero))
              xxx=compress((abs(array(magcor))<99)&(abs(array(zero))<99),magcor)
              yyy=compress((abs(array(zero))<99)&(abs(array(magcor))<99),zero)
              plot(xxx,yyy,'or')
              plot([min(compress(abs(array(magcor))<99,magcor)),max(compress(abs(array(magcor))<99,magcor))],[aa,aa],'-b')
              draw()
              print std(compress(abs(array(zero))<99,zero))
              time.sleep(5)

        colorvec=colors[filters[_filter]]
        for col in colorvec:
            col0=magstd0[col[0]] 
            col1=magstd0[col[1]]
            colstd0=array(col0,float)-array(col1,float)
            ################## sex  ######################
            colore=[]
            for i in range(0,len(pos1)):   colore.append(colstd0[i])

            colore1=compress(abs(array(zero))<50,array(colore))
            zero1=compress(abs(array(zero))<50,array(zero))
            zero2=compress(abs(array(colore1))<2,array(zero1))
            colore2=compress(abs(array(colore1))<2,array(colore1))
            if _fix: fisso=colorefisso[filters[_filter]+col]
            else: fisso=''

            if len(colore2)==0:
                print 'no calibration, '+_filter+' '+_field
                b,a,sa,sb=9999,9999,0,0
            else:
                if _interactive:    a,sa,b,sb=fitcol(colore2,zero2,_filter,col,fisso)
                else:               a,sa,b,sb=fitcol2(colore2,zero2,_filter,col,fisso,show,rejection)

            print a,sa,b,sb
            result[filters[_filter]+col]=[a,sa,b,sb]
        if result:
            print '\n### zeropoint ..... done at airmass 0'
            if _catalogue:
                lsc.util.updateheader(img,0,{'CATALOG':[str(string.split(_catalogue,'/')[-1]),'catalogue source']})
            stringa=''
            for ll in result:
                for kk in range(0,len(result[ll])):
                                    if not isfinite(result[ll][kk]): result[ll][kk]=0.0 
                valore='%3.3s %6.6s %6.6s  %6.6s  %6.6s' %  (str(ll),str(result[ll][0]),str(result[ll][2]),str(result[ll][1]),str(result[ll][3]))
                print '### ',valore
                lsc.util.updateheader(img,0,{'zp'+ll:[str(valore),'a b sa sb in y=a+bx']})
                if ll[0]==ll[2]: num=2
                elif ll[0]==ll[1]: num=1
                else: sys.exit('somthing wrong with color '+ll)
                print ll,num
                try:
                    print 'zcol'+str(num),ll[1:],string.split(re.sub('.sn2.fits','.fits',img),'/')[-1]
                    lsc.mysqldef.updatevalue(database,'zcol'+str(num),ll[1:],string.split(re.sub('.sn2.fits','.fits',img),'/')[-1])
                    lsc.mysqldef.updatevalue(database,'z'+str(num),result[ll][0],string.split(re.sub('.sn2.fits','.fits',img),'/')[-1])
                    lsc.mysqldef.updatevalue(database,'c'+str(num),result[ll][2],string.split(re.sub('.sn2.fits','.fits',img),'/')[-1])
                    lsc.mysqldef.updatevalue(database,'dz'+str(num),result[ll][1],string.split(re.sub('.sn2.fits','.fits',img),'/')[-1])
                    lsc.mysqldef.updatevalue(database,'dc'+str(num),result[ll][3],string.split(re.sub('.sn2.fits','.fits',img),'/')[-1])
                    if os.path.isfile(string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1]):
                          lsc.mysqldef.updatevalue(database,'zcol'+str(num),ll[1:],string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1])
                          lsc.mysqldef.updatevalue(database,'z'+str(num),result[ll][0],string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1])
                          lsc.mysqldef.updatevalue(database,'c'+str(num),result[ll][2],string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1])
                          lsc.mysqldef.updatevalue(database,'dz'+str(num),result[ll][1],string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1])
                          lsc.mysqldef.updatevalue(database,'dc'+str(num),result[ll][3],string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1])
                    if result[ll][0]!=9999:
                          print _catalogue
                          lsc.mysqldef.updatevalue(database,'zcat',string.split(_catalogue,'/')[-1],string.split(re.sub('.sn2.fits','.fits',img),'/')[-1])
                          if os.path.isfile(string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1]):
                                lsc.mysqldef.updatevalue(database,'zcat',string.split(_catalogue,'/')[-1],string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1])
                    else:
                        lsc.mysqldef.updatevalue(database,'zcat','X',string.split(re.sub('.sn2.fits','.fits',img),'/')[-1])
                        if os.path.isfile(string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1]):
                              lsc.mysqldef.updatevalue(database,'zcat','X',string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1])
                except: print 'module mysqldef not found'
Esempio n. 42
0
                            iraf.immatch.gregister(_dirtemp + tempmask0, tempmask, "tmp$db"+temp_file0, temp_file0+"_tmpcoo", geometr="geometric",
                                               interpo=_interpolation, boundar='constant', constan=0, flux='yes', verbose='no')
                        except:
                            # this is strange, sometime the registration of the msk fail the first time, but not the second time
                            iraf.immatch.gregister(_dirtemp + tempmask0, tempmask, "tmp$db"+temp_file0, temp_file0+"_tmpcoo", geometr="geometric",
                                               interpo=_interpolation, boundar='constant', constan=0, flux='yes', verbose='no')

                        if os.path.isfile(_dirtemp +re.sub('.fits','.var.fits',imgtemp0)):
                            if _verbose:  print 'variance image already there, do not create noise image'
                            iraf.immatch.gregister(_dirtemp + re.sub('.fits','.var.fits',imgtemp0), 
                                                   temp_file0+'_tempnoise3.fits', "tmp$db"+temp_file0, temp_file0+"_tmpcoo", geometr="geometric", 
                                                   interpo=_interpolation, boundar='constant', constan=0, flux='yes', verbose='no')


                        if _show:
                            iraf.display(imgtemp, frame=4, fill='yes')
                            iraf.display(_dirtemp + imgtemp0, frame=3, fill='yes')
                            iraf.display(imgtarg, frame=2, fill='yes')
                            iraf.display(_dir + imgtarg0, frame=1, fill='yes')

                        ###########################################################

                        data_targ, head_targ = pyfits.getdata(imgtarg, header=True)
                        exp_targ  = agnkey.util.readkey3(head_targ, 'exptime')
                        sat_targ = agnkey.util.readkey3(head_targ, 'saturate')
                        gain_targ = agnkey.util.readkey3(head_targ, 'gain')
                        rn_targ = agnkey.util.readkey3(head_targ,'ron')

                        data_temp, head_temp = pyfits.getdata(imgtemp, header=True)
                        exp_temp = agnkey.util.readkey3(head_temp, 'exptime')
                        sat_temp = agnkey.util.readkey3(head_temp, 'saturate')
Esempio n. 43
0
def maskImages(filename):
  #Read in the full width half maximum from the input file
  imageHeader = fits.open(filename+".fits")[0]
  fwhm = imageHeader.header['SEEING']

  ######CONFIG STUFF######
  z1_default=-12 #Change this value to alter the low end of the image display
  z2_default=1000 #Change this value to alter the high end of the image display
  ztrans_default='log'  #Change this to 'log' to use a logarithmic intensity plot in the display of the R image
  iraf.imedit.radius=3.0*fwhm  #Change this value to change imedit's default radius.
  ########################


  #Determine if a masked R image exists; create one if it does not.
  maskedExists=False
  try:
    with open(filename+'Masked.fits'):maskedExists=True
  except IOError:
    print 'The masked file does not exist.  Creating masked file...'
    mask = fits.getdata(filename+'MaskReversed.fits')
    base = fits.getdata(filename+'.fits')
    result = np.multiply(base,mask)
    
    hdu=fits.PrimaryHDU(result)
    hdulist = fits.HDUList([hdu])
    hdulist.writeto(filename+'Masked.fits')
    print 'Masked file written as testDataMasked.fits'
    
  
  #Display the image's Ha counterpart
  iraf.display(filename+'_Ha.fits',2)
  
  #Set the display parameters for the R image
  iraf.display.z1=z1_default
  iraf.display.z2=z2_defulat
  iraf.display.ztrans=ztrans_default
  iraf.imedit.command="display $image 1 erase=$erase fill=no order=0 >& dev$null"
  
  #Determine if a handmasked image exists
  handmaskedExists=False
  try:
    with open(filename+'_Handmasked.fits'):handmaskedExists=True
    print 'Handmasked image detected, loading it for further editing...'
  except IOError:
    print 'Creating new handmask image...'
  
  #Print some useful commands
  print 'Here are some pertinent commands used in imedit\ne  Fill circles   with constant value\n+/-  increase/decrease radius\nk  Fill circles with input data (spot healing, essentially)\nu  Undo the last change\nq  Finish editing the image and write the output\nQ Finish editing without writing output\ni  Reset editor to initial loaded image'
  
  #if a handmasked image does not exist, load the R image for editing otherwise load the handmask to edit it.
  iraf.display.zscale='no'
  iraf.display.zrange='no'
  if (not handmaskedExists):
    iraf.imedit(filename+'Masked.fits',filename+'_Handmasked.fits')
  else:
    iraf.imedit(filename+'_Handmasked.fits','')
  
  #Repeat the process for Ha.
  print 'Mask Ha.  Same commands apply'
  haHandmaskedExists=False
  iraf.display.zscale='yes'
  iraf.display.zrange='yes'
Esempio n. 44
0
  else:
    iraf.imedit(filename+'_Handmasked.fits','')
  
  #Repeat the process for Ha.
  print 'Mask Ha.  Same commands apply'
  haHandmaskedExists=False
  iraf.display.zscale='yes'
  iraf.display.zrange='yes'
 try:
    with open(filename+'_HaHandmasked.fits'):haHandmaskedExists=True
    print 'Ha handmasked image detected, loading it for further editing...'
  except IOError:
    print 'Creating new Ha handmask image...'
    
  
  iraf.display(filename+'Masked.fits',2) 
  
  if (haHandmaskedExists):
    iraf.imedit(filename+'_HaHandmasked.fits','')
  else:
    iraf.imedit(filename+'_Ha.fits',filename+'_HaHandmasked.fits')

#This reads through a handmasked set of R and Ha images and extracts the #masks from it, writing it as (filename)handmask.fits and (filename
#)_HaHandmask.fits
def getMasks(filename):
  #Delete the old masks, if any exist.
  iraf.imdelete(filename+'_Handmask.fits')
  iraf.imdelete(filename+'_HaHandmask.fits')
  
  #Extract the mask from the masked R image, using a handmasked image if one exists otherwise using the masked image
  maskValue = 0.0
Esempio n. 45
0
def runellipse(files,xcenter,ycenter,minr,ipa,initialr,maxr,iellip,nframe=1,myradius=15):
    initialradius=myradius
    #print 'got here'
    #print files
    for i in range(len(files)):
        myradius=initialradius
    
        mfile=files[i]

        #mask image
        
        outfile1='m'+mfile
        if os.path.isfile(outfile1):
            print "found masked file ",outfile1
            print "skipping imedit and running ellipse interactively"
            continueWithProgram=1
            continueWithObject=1

        else:
            print "can't find masked file ",outfile1
            print "running imedit"
            continueWithProgram,continueWithObject=runimedit(mfile,outfile1,nframe)

        if not continueWithProgram:
            print "quitting program"
            return
        if not continueWithObject:
            print "going on to next image"
            continue

        #run ellipse
        t=mfile.split('.')
        efile=t[0]+'.tab'
        imprefix=t[0]
        try:
            os.remove(efile)
        except OSError:
            print 'everything is ok'
            #continue

        print "First pass through ellipse to find center"
        iraf.ellipse(input=outfile1,output=efile,x0=xcenter,y0=ycenter,hcenter='no',sma0=initialr,minsma=minr,maxsma=maxr,pa=ipa,hpa='no',ellip=iellip,hellip='no',interactive='no')
        #print 'Displaying isophotes from first pass.  Hit q in DS9 window to quit'
        #iraf.isoexam(table=efile)

        os.system('rm junk.txt')
        iraf.tprint(table=efile,pwidth='INDEF',showhdr='no', Stdout='junk.txt')
        os.system("awk '{print $2, $7, $9, $11, $13}' < junk.txt > junk2.txt")
        #run ellipse a second time, keeping PA and ellip fixed
        #allow user to adjust the radius where PA and ellip are measured
        infile=open('junk2.txt','r')
        for line in infile:
            t=line.split()
            if float(t[0]) > myradius:
                newxcenter=float(t[3])
                newycenter=float(t[4])
                break
        s='rm '+efile
        os.system(s)

        #draw ellipse with ds9
        iraf.display(outfile1,1)
        (myellip,mypa)=findellipse(outfile1,newxcenter,newycenter)
        flag2=str(raw_input('Do you want to skip this one?  y=skip, any other key to continue '))
        if flag2.find('y') > -1:
            s='mv *'+imprefix+'* ../PeculiarGalaxies/'
            print s
            os.system(s)
            continue
        #run ellipse interactively
        #allow user to repeat until satisfied with script
        repeatflag=1
        while repeatflag:
            s='rm '+efile
            os.system(s)
            iraf.ellipse(input=outfile1,output=efile,x0=newxcenter,y0=newycenter,hcenter='yes',sma0=initialr,minsma=minr,maxsma=maxr,pa0=mypa,hpa='yes',ellip0=myellip,hellip='yes',interactive='no')

            print 'Displaying isophotes from second pass using r = ',myradius
            print 'Hit q in the DS9 window to quit'
            iraf.isoexam(table=efile)
                
            flag=str(raw_input('Are you happy with the fit?  y=yes n=no x=quit '))
            flag=str(flag)
            print 'this is what I think you typed ',flag
            if flag.find('n') > -1:
                s='rm '+efile
                os.system(s)
                repeatflag=1
            elif flag.find('x') > -1:
                repeatflag=0
                print 'i think repeatflag = 0', repeatflag
                return
            else:
                s='mv *'+imprefix+'* ../Finished/'
                os.system(s)
                repeatflag=0
                print 'i think repeatflag = 0 ', repeatflag

            print 'repeatflag = ',repeatflag
Esempio n. 46
0
    def displayDS9(self, image, buffer):
        '''
		Displays the given image on DS9. The function requires that
		the DS9 is already running. In the future this should be changed.
		'''
        I.display(image, buffer)
Esempio n. 47
0
                      imgdir +
                      '" --filename=imgs.txt --width 600 --height 250 &')

            print(
                'What do you want to do? Enter a = Align ; c = Combine ; s = Weighted average of Exptime ; i = Inspect ; e = Exit and go to Next filter directory'
            )
            choice = raw_input('Enter your Choice (a,c,s,i or e) :')
            if (choice == "e"):
                break  # breaking to move to next filter image directory
            elif (choice == "i"):
                while 1:
                    imgEX = input(
                        'Enter the Sl.No# of Image you want to Examine [Enter a large number not in image list to exit] : '
                    )
                    if imgEX >= len(images): break
                    iraf.display(images[imgEX], 1)
                    iraf.imexam()
            elif (choice == "c"):  # Simply combining
                imgINP = map(
                    int,
                    raw_input(
                        'Enter the Sl.No#s of all Images to combine [ Eg: 1,2,3 ] : '
                    ).split(','))
                i = len(imgINP)
                if i < 2: break  # minimum 2 images to combine are not there..
                for j in imgINP:  #Sanity check
                    if j >= len(images):
                        print('Wrong serial number ' + str(j) +
                              ' given.. exiting the combine task..')
                        break
                inpVAR = ','.join([images[j] for j in imgINP])
Esempio n. 48
0
def runellipse(files, xcenter, ycenter, minr, initialr, maxr, nframe=1):
    for i in range(len(files)):
        #for i in range(5):
        mfile = files[i]
        repeatflag = 1
        while (repeatflag > 0.1):
            iraf.display(mfile, frame=nframe, fill='yes')
            outfile1 = 'm' + mfile
            print mfile
            print 'Running imedit to mask out other sources in the field:'
            print 'Enter b to mask out a circular region'
            print 'Enter a to mark the corners of a rectangle'
            print 'Enter q when finished'
            try:
                os.remove(outfile1)
            except OSError:
                print 'everything is ok'
            print 'running imedit ', mfile, outfile1
            iraf.imedit(mfile, outfile1)

            t = mfile.split('.')
            efile = t[0] + '.tab'
            print 'Running ellipse to fit isophotes to galaxy:'
            print 'Enter h to continue fitting'
            try:
                os.remove(efile)
            except OSError:
                print 'everything is ok'
            iraf.ellipse(input=outfile1,
                         output=efile,
                         x0=xcenter,
                         y0=ycenter,
                         hcenter='yes',
                         sma0=initialr,
                         minsma=minr,
                         maxsma=maxr)
            print 'Displaying isophotes.  Hit q to quit'
            iraf.isoexam(table=efile)
            #            try:
            #                iraf.ellipse(input=outfile1,output=efile,x0=xcenter,y0=ycenter,hcenter='yes',sma0=initialr,minsma=minr,maxsma=maxr)
            #            except:
            #                print efile," already exists so I am deleting it. Hit Cntrl-c if you don't want to delete"
            #                s='rm '+efile
            #                os.system(s)
            #                iraf.ellipse(input=outfile1,output=efile,x0=xcenter,y0=ycenter,hcenter='yes',sma0=initialr,minsma=minr,maxsma=maxr)

            flag = str(
                raw_input('Are you happy with the fit?  y=yes n=no x=quit '))
            flag = str(flag)
            print 'this is what I think you typed ', flag
            if flag.find('y') > -1:
                s = 'mv *' + t[0] + '* Finished/'
                os.system(s)
                repeatflag = 0
                print 'i think repeatflag = 0', repeatflag
            if flag.find('n') > -1:
                s = 'rm *' + t[0] + '*.tab'
                os.system(s)
                s = 'rm m' + t[0] + '*.fits'
                os.system(s)
                repeatflag = 1
                print 'i think repeatflag = 1', repeatflag
            if flag.find('x') > -1:
                repeatflag = 0
                print 'i think repeatflag = 0', repeatflag
                return
            print 'repeatflag = ', repeatflag
Esempio n. 49
0
#
####################
if createcal=='yes':
    iraf.niri.niflat("@fflats.lis", flatfile="flat.fits", lampsoff="@fflatdarks.lis", \
                     darks="@fshortdarks.lis", bpmfile="f2_bpm.pl", fl_inter='no')
else:
    print "using flats.fits"
#####
#
#Displaying flat and bad pixel mask
#
#####

beep()
beep()
iraf.display("flat.fits[sci]",1)
sleep(5)                                 # 5 sec 

beep()
beep()
iraf.display("f2_bpm.pl",2)
sleep(5) 



########
#
# F2prepare  science
#
########
Esempio n. 50
0
            hdulist.close()
            imgs_txt.write(str(i)+' '+img+' '+str(Obj)+' '+str(Exptime)+' '+str(Commentx)+' '+str(Comment)+' '+str(Filter)+'\n')
            i=i+1
    #   Now list is ready, continuing with what to do
        imgs_txt.close()

        junk = os.system(DISPLAY_MENU_CMD.format(**{"imgdir":imgdir,"imglist":IMGListFile}))

        print ('What do you want to do? Enter a = Align ; c = Combine ; s = Weighted average of Exptime ; i = Inspect ; e = Exit and go to Next filter directory')
        choice=raw_input('Enter your Choice (a,c,s,i or e) :')
        if ( choice == "e" ) : break   # breaking to move to next filter image directory
        elif (choice == "i") :
            while 1 :
                imgEX=input('Enter the Sl.No# of Image you want to Examine [Enter a large number not in image list to exit] : ')
                if imgEX >= len(images) : break
                iraf.display(images[imgEX],1)
                iraf.imexam()
        elif (choice == "c" ) :  # Simply combining 
            imgINP=map(int, raw_input('Enter the Sl.No#s of all Images to combine [ Eg: 1,2,3 ] : ').split(','))
            i=len(imgINP)
            if i < 2 : break    # minimum 2 images to combine are not there..
            for j in imgINP : #Sanity check
                if j >= len(images) : 
                    print('Wrong serial number '+str(j)+' given.. exiting the combine task..')
                    break
            inpVAR=','.join([images[j] for j in imgINP])
            name=raw_input('Enter the name for combined image without .fits extension : ')
            iraf.imcombine(input=inpVAR, output=name+'.fits',combine="average")

        elif (choice == "s" ) :  # Summing and finally dividing by total EXPTIME 
            imgINP=map(int, raw_input('Enter the Sl.No#s of all Images to average by weighted sum of EXPTIME [ Eg: 1,2,3 ] : ').split(','))
Esempio n. 51
0
def ecpsf(img, ofwhm, threshold, psfstars, distance, interactive, psffun='gauss', fixaperture=False, _catalog='', _datamax=None, show=False):
    try:
        import lsc, string

        hdr = lsc.util.readhdr(img + '.fits')
        instrument = lsc.util.readkey3(hdr, 'instrume')
        print 'INSTRUMENT:', instrument

        if 'PIXSCALE' in hdr:
            scale = lsc.util.readkey3(hdr, 'PIXSCALE')
        elif 'CCDSCALE' in hdr:
            if 'CCDXBIN' in hdr:
                scale = lsc.util.readkey3(hdr, 'CCDSCALE') * lsc.util.readkey3(hdr, 'CCDXBIN')
            elif 'CCDSUM' in hdr:
                scale = lsc.util.readkey3(hdr, 'CCDSCALE') * int(string.split(lsc.util.readkey3(hdr, 'CCDSUM'))[0])

        if _datamax is None:
            _datamax = lsc.util.readkey3(hdr, 'datamax')
        _wcserr = lsc.util.readkey3(hdr, 'wcserr')
        print _wcserr
        if float(_wcserr) == 0:
            if 'L1FWHM' in hdr:
                seeing = float(lsc.util.readkey3(hdr, 'L1FWHM'))
            elif 'L1SEEING' in hdr:
                seeing = float(lsc.util.readkey3(hdr, 'L1SEEING')) * scale
            else:
                seeing = 3
        else:
            seeing = 3
            if 'PSF_FWHM' in hdr:
                seeing = float(lsc.util.readkey3(hdr, 'PSF_FWHM'))
            else:
                sys.exit('astrometry not good')
        #except:  sys.exit('astrometry not good')

        fwhm = seeing / scale
        print 'FWHM[header]  ', fwhm, '   in pixel'
        if ofwhm: fwhm = float(ofwhm)
        print '    FWHM[input]  ', fwhm, ' in pixel'

        xdim, ydim = iraf.hselect(img+'[0]', 'i_naxis1,i_naxis2', 'yes', Stdout=1)[0].split()
        print img, fwhm, threshold, scale,xdim

        #################################################################################
        ###################        write file to compute psf     _psf.coo    ############
        #################################################################################
        if _catalog:
            print '\n#### use catalog to measure the psf'
            ddd=iraf.wcsctran(input=_catalog,output='STDOUT',Stdout=1,image=img + '[0]',inwcs='world',outwcs='logical',
                              units='degrees degrees',columns='1 2',formats='%10.1f %10.1f',verbose='no')
            ddd=[i for i in ddd if i[0]!='#']
            ddd=['  '.join(i.split()[0:3]) for i in ddd]
            line=''
            for i in ddd:
                a,b,c = string.split(i)
                if float(a) < float(xdim) and  float(b) < float(ydim) and float(b) > 0:
                    line = line + '%10s %10s %10s \n' % (a, b, c)
            if line:
                ff = open('_psf.coo', 'w')
                ff.write(line)
                ff.close()
            else:
                sys.exit('error: no catalog objects are in the field')
        elif interactive:
            iraf.display(img + '[0]', 1, fill=True)
            iraf.delete('tmp.lo?', verify=False)
            print '_' * 80
            print '>>> Mark reference stars with "a". Then "q"'
            print '-' * 80
            iraf.imexamine(img, 1, wcs='logical', logfile='tmp.log', keeplog=True)
            xyrefer = iraf.fields('tmp.log', '1,2,6,15', Stdout=1)
            xns, yns, _fws = [], [], []
            #############      write    file for PSF                           #########################
            ff = open('_psf.coo', 'w')
            for i in range(len(xyrefer)):
                xns.append(float(xyrefer[i].split()[0]))
                yns.append(float(xyrefer[i].split()[1]))
                _fws.append(float(xyrefer[i].split()[3]))
                ff.write('%10.3f %10.3f %7.2f \n' % (xns[i], yns[i], float(_fws[i])))
            ff.close()
            fwhm = np.median(_fws)
        else:
            ############              run  sextractor                #####################################
            xs, ys, ran, decn, magbest, classstar, fluxrad, bkg = runsex(img, fwhm, threshold, scale)
            tot = np.compress(abs(np.array(fluxrad) * 1.6 - fwhm) / fwhm < .5, fluxrad)
            if len(tot) < 5:
                print 'warning: fwhm from sexractor different from fwhm computed during pre-reduction'
                print 'try using option --fwhm xxx'

            ff = open('tmp.cursor', 'w')
            image_hdu = fits.open(img + '.fits')
            for i in range(len(xs)):
                _xs = np.delete(xs, i)
                _ys = np.delete(ys, i)
                dist2 = np.sqrt((_xs - xs[i]) ** 2 + (_ys - ys[i]) ** 2)
                ###########           cut  star, not near other object    ##########################
                if abs(fluxrad[i] * 1.6 - fwhm) / fwhm < .5 and min(dist2) > distance * fwhm:
                    x1, x2 = int(xs[i] - fwhm * 3), int(xs[i] + fwhm * 3)
                    y1, y2 = int(ys[i] - fwhm * 3), int(ys[i] + fwhm * 3)
                    if x1 < 1: x1 = 1
                    if y1 < 1: y1 = 1
                    if x2 > int(xdim):
                        x2 = int(xdim)
                    if y2 > int(ydim):
                        y2 = int(ydim)
                    fmax = np.max(image_hdu[0].data[y1-1:y2, x1-1:x2])
                ##########       cut saturated object               ########################
                    if float(fmax) < _datamax:  # not saturated
                        ff.write('%10.3f %10.3f 1 m \n' % (xs[i], ys[i]))
            ff.close()
            image_hdu.close()

            iraf.delete('tmp.lo?,tmp.sta?,tmp.gk?', verify=False)
            iraf.psfmeasure(img+'[0]', imagecur='tmp.cursor', logfile='tmp.log', radius=int(fwhm), iter=3,
                            display=False, StdoutG='tmp.gki')

            ff = open('tmp.log')
            righe = ff.readlines()
            xn = [float(righe[3].split()[1])]
            yn = [float(righe[3].split()[2])]
            _fw = [float(righe[3].split()[4])]
            for r in righe[4:-2]:
                if len(r) > 0:
                    xn.append(float(r.split()[0]))
                    yn.append(float(r.split()[1]))
                    _fw.append(float(r.split()[3]))
            print 'FWHM: ', righe[-1].split()[-1]
            print 80 * "#"
            ######
            ##############            eliminate double object identification         ###########################
            xns, yns, _fws = [xn[0]], [yn[0]], [_fw[0]]
            for i in range(1, len(xn)):
                if abs(xn[i] - xn[i - 1]) > .2 and abs(yn[i] - yn[i - 1]) > .2:
                    xns.append(xn[i])
                    yns.append(yn[i])
                    _fws.append(_fw[i])
            #########      write clean   file for PSF                           #########################
            fw = []
            ff = open('_psf.coo', 'w')
            for i in range(len(xns)):
                if abs(_fws[i] - fwhm) / fwhm < .3:
                    ff.write('%10.3f %10.3f %7.2f \n' % (xns[i], yns[i], float(_fws[i])))
                    fw.append(_fws[i])
            ff.close()  ## End automatic selection
        ######################################################################################
        ###################        write file of object to store in  fits table  #############
        ######################################################################################
        if _catalog:
            ddd=iraf.wcsctran(input=_catalog,output='STDOUT',Stdout=1,image=img + '[0]',inwcs='world',outwcs='logical',
                              units='degrees degrees',columns='1 2',formats='%10.1f %10.1f',verbose='no')
            ddd=[i for i in ddd if i[0]!='#']
            ddd=['  '.join(i.split()[0:3]) for i in ddd]
            ff = open('_psf2.coo', 'w')
            for i in ddd:
                a,b,c = string.split(i)
                ff.write('%10s %10s %10s \n' % (a, b, c))
            ff.close()
        elif interactive:
            xs, ys, ran, decn, magbest, classstar, fluxrad, bkg = runsex(img, fwhm, threshold, scale)
            ff = open('_psf2.coo', 'w')
            for i in range(len(xs)):
                ff.write('%10s %10s %10s \n' % (xs[i], ys[i], fluxrad[i]))
            ff.close()
        else:
            os.system('cp _psf.coo _psf2.coo')
#            dflux = fluxrad - np.median(fluxrad)
#            fstar = np.compress(dflux < np.std(fluxrad), fluxrad)
#################################################################################################################

        print 80 * "#"
        photmag, pst, fitmag = psffit(img, fwhm, psfstars, hdr, interactive, _datamax, psffun, fixaperture)

        photmag2, fitmag2 = psffit2(img, fwhm, psfstars, hdr, _datamax, psffun, fixaperture)

        radec = iraf.wcsctran(input='STDIN', output='STDOUT', Stdin=photmag,
                              Stdout=1, image=img + '[0]', inwcs='logical', outwcs='world', columns="1 2",
                              format='%13.3H %12.2h', min_sig=9, mode='h')[3:]

        radec2 = iraf.wcsctran(input='STDIN', output='STDOUT', Stdin=photmag2,
                               Stdout=1, image=img + '[0]', inwcs='logical', outwcs='world', columns="1 2",
                               format='%13.3H %12.2h', min_sig=9, mode='h')[3:]

        if interactive or show:
            iraf.set(stdimage='imt1024')
            iraf.display(img + '[0]', 1, fill=True, Stdout=1)
            iraf.tvmark(1, coords='STDIN', mark='circle', radii=15, label=True, Stdin=photmag, nxoffset=5, nyoffset=5, txsize=2)
            iraf.tvmark(1, coords='STDIN', mark='circle', radii=35, label=False, Stdin=pst, color=208)
#            iraf.tvmark(1, coords='STDIN', mark='cross', length=35, label=False, Stdin=fitmag2, color=204)

        idpsf = []
        for i in range(len(pst)):
            idpsf.append(pst[i].split()[2])
        dmag = []
        for i in range(len(radec)):
            ra, dec, idph, magp2, magp3, magp4, merrp2, merrp3, merrp4 = radec[i].split()
            dmag.append(9.99)
            for j in range(len(fitmag)):
                raf, decf, idf, magf, magerrf = fitmag[j].split()
                if idph == idf and idph in idpsf and \
                                magp3 != 'INDEF' and magf != 'INDEF':
                    dmag[i] = float(magp3) - float(magf)
                    break

        _dmag = np.compress(np.array(dmag) < 9.99, np.array(dmag))

        print '>>> Aperture correction (phot)   %6.3f +/- %5.3f %3d ' % \
              (np.mean(_dmag), np.std(_dmag), len(_dmag))
        if len(_dmag) > 3:
            _dmag = np.compress(np.abs(_dmag - np.median(_dmag)) < 2 * np.std(_dmag), _dmag)
            print '>>>         2 sigma rejection)   %6.3f +/- %5.3f %3d  [default]' \
                  % (np.mean(_dmag), np.std(_dmag), len(_dmag))
            print '>>>     fwhm   %s  ' % (str(fwhm))
        for i in range(len(dmag)):
            if dmag[i] == 9.99:
                dmag[i] = ''
            else:
                dmag[i] = '%6.3f' % (dmag[i])

        exptime = lsc.util.readkey3(hdr, 'exptime')
        object = lsc.util.readkey3(hdr, 'object').replace(' ', '')
        filtro = lsc.util.readkey3(hdr, 'filter')

        #######################################
        rap, decp, magp2, magp3, magp4, smagf, merrp3, smagerrf = [], [], [], [], [], [], [], []
        rap0, decp0 = [], []
        for i in range(len(radec2)):
            aa = radec2[i].split()
            rap.append(aa[0])
            decp.append(aa[1])
            rap0.append(lsc.lscabsphotdef.deg2HMS(ra=aa[0]))
            decp0.append(lsc.lscabsphotdef.deg2HMS(dec=aa[1]))
            idp = aa[2]
            magp2.append(aa[3])
            magp3.append(aa[4])
            magp4.append(aa[5])
            merrp3.append(aa[7])
            _smagf, _smagerrf = 9999, 9999
            for j in range(len(fitmag2)):
                raf, decf, idf, magf, magerrf = fitmag2[j].split()
                if idf == idp:
                    _smagf = magf
                    _smagerrf = magerrf
                    break
            smagf.append(_smagf)
            smagerrf.append(_smagerrf)
        tbhdu = fits.BinTableHDU.from_columns(fits.ColDefs([fits.Column(name='ra', format='20A', array=np.array(rap)),
                                               fits.Column(name='dec', format='20A', array=np.array(decp)),
                                               fits.Column(name='ra0', format='E', array=np.array(rap0)),
                                               fits.Column(name='dec0', format='E', array=np.array(decp0)),
                                               fits.Column(name='magp2', format='E',
                                                           array=np.array(np.where((np.array(magp2) != 'INDEF'),
                                                                                   np.array(magp2), 9999), float)),
                                               fits.Column(name='magp3', format='E',
                                                               array=np.array(np.where((np.array(magp3) != 'INDEF'),
                                                                                       np.array(magp3), 9999), float)),
                                               fits.Column(name='merrp3', format='E',
                                                               array=np.array(np.where((np.array(merrp3) != 'INDEF'),
                                                                                       np.array(merrp3), 9999), float)),
                                               fits.Column(name='magp4', format='E',
                                                               array=np.array(np.where((np.array(magp4) != 'INDEF'),
                                                                                       np.array(magp4), 9999), float)),
                                               fits.Column(name='smagf', format='E',
                                                               array=np.array(np.where((np.array(smagf) != 'INDEF'),
                                                                                       np.array(smagf), 9999), float)),
                                               fits.Column(name='smagerrf', format='E',
                                                               array=np.array(np.where((np.array(smagerrf) != 'INDEF'),
                                                                                       np.array(smagerrf), 9999),
                                                                              float)),
        ]))

        hdu = fits.PrimaryHDU(header=hdr)
        thdulist = fits.HDUList([hdu, tbhdu])
        lsc.util.delete(img + '.sn2.fits')
        thdulist.writeto(img + '.sn2.fits')
        lsc.util.updateheader(img + '.sn2.fits', 0, {'APCO': [np.mean(_dmag), 'Aperture correction']})
        lsc.util.updateheader(img + '.sn2.fits', 0, {'APCOERR': [np.std(_dmag), 'Aperture correction error']})
        lsc.util.updateheader(img + '.sn2.fits', 0, {'XDIM': [lsc.util.readkey3(hdr, 'naxis1'), 'x number of pixels']})
        lsc.util.updateheader(img + '.sn2.fits', 0, {'YDIM': [lsc.util.readkey3(hdr, 'naxis2'), 'y number of pixels']})
        lsc.util.updateheader(img + '.sn2.fits', 0,
                              {'PSF_FWHM': [fwhm * scale, 'FWHM (arcsec) - computed with daophot']})
        os.chmod(img + '.sn2.fits', 0664)
        os.chmod(img + '.psf.fits', 0664)
        result = 1

    except:
        result = 0
        fwhm = 0.0
        traceback.print_exc()
    return result, fwhm * scale
Esempio n. 52
0
def ecpsf(img, ofwhm, threshold, psfstars, distance, interactive, ds9, psffun='gauss', fixaperture=False, _catalog='', _datamax=None, show=False):
    try:
        import lsc, string

        hdr = lsc.util.readhdr(img + '.fits')
        instrument = lsc.util.readkey3(hdr, 'instrume')
        print 'INSTRUMENT:', instrument

        if 'PIXSCALE' in hdr:
            scale = lsc.util.readkey3(hdr, 'PIXSCALE')
        elif 'CCDSCALE' in hdr:
            if 'CCDXBIN' in hdr:
                scale = lsc.util.readkey3(hdr, 'CCDSCALE') * lsc.util.readkey3(hdr, 'CCDXBIN')
            elif 'CCDSUM' in hdr:
                scale = lsc.util.readkey3(hdr, 'CCDSCALE') * int(string.split(lsc.util.readkey3(hdr, 'CCDSUM'))[0])

        if _datamax is None and 'kb' in instrument:
            _datamax = 45000
        elif _datamax is None:
            _datamax = 65000
        _wcserr = lsc.util.readkey3(hdr, 'wcserr')
        print _wcserr
        if float(_wcserr) == 0:
            if 'L1FWHM' in hdr:
                seeing = float(lsc.util.readkey3(hdr, 'L1FWHM'))
            elif 'L1SEEING' in hdr:
                seeing = float(lsc.util.readkey3(hdr, 'L1SEEING')) * scale
            else:
                seeing = 3
        else:
            seeing = 3
            if 'PSF_FWHM' in hdr:
                seeing = float(lsc.util.readkey3(hdr, 'PSF_FWHM'))
            else:
                sys.exit('astrometry not good')
        #except:  sys.exit('astrometry not good')

        fwhm = seeing / scale
        print 'FWHM[header]  ', fwhm, '   in pixel'
        if ofwhm: fwhm = float(ofwhm)
        print '    FWHM[input]  ', fwhm, ' in pixel'

        xdim, ydim = iraf.hselect(img+'[0]', 'i_naxis1,i_naxis2', 'yes', Stdout=1)[0].split()
        print img, fwhm, threshold, scale,xdim

        #################################################################################
        ###################        write file to compute psf     _psf.coo    ############
        #################################################################################
        if interactive:
            iraf.display(img, 1, fill=True)
            iraf.delete('tmp.lo?', verify=False)
            print '_' * 80
            print '>>> Mark reference stars with "a". Then "q"'
            print '-' * 80
            iraf.imexamine(img, 1, wcs='logical', logfile='tmp.log', keeplog=True)
            xyrefer = iraf.fields('tmp.log', '1,2,6,15', Stdout=1)
            xns, yns, _fws = [], [], []
            #############      write    file for PSF                           #########################
            ff = open('_psf.coo', 'w')
            for i in range(len(xyrefer)):
                xns.append(float(xyrefer[i].split()[0]))
                yns.append(float(xyrefer[i].split()[1]))
                _fws.append(float(xyrefer[i].split()[3]))
                ff.write('%10.3f %10.3f %7.2f \n' % (xns[i], yns[i], float(_fws[i])))
            ff.close()
            fwhm = np.median(_fws)
        elif _catalog:
            print '\n#### use catalog to measure the psf'
            ddd=iraf.wcsctran(input=_catalog,output='STDOUT',Stdout=1,image=img + '[0]',inwcs='world',outwcs='logical',
                              units='degrees degrees',columns='1 2',formats='%10.1f %10.1f',verbose='no')
            ddd=[i for i in ddd if i[0]!='#']
            ddd=['  '.join(i.split()[0:3]) for i in ddd]
            line=''
            for i in ddd:
                a,b,c = string.split(i)
                if float(a) < float(xdim) and  float(b) < float(ydim) and float(b) > 0:
                    line = line + '%10s %10s %10s \n' % (a, b, c)
            if line:
                ff = open('_psf.coo', 'w')
                ff.write(line)
                ff.close()
            else:
                sys.exit('error: no catalog objects are in the field')
        else:
            ############              run  sextractor                #####################################
            xs, ys, ran, decn, magbest, classstar, fluxrad, bkg = runsex(img, fwhm, threshold, scale)
            tot = np.compress(abs(np.array(fluxrad) * 1.6 - fwhm) / fwhm < .5, fluxrad)
            if len(tot) < 5:
                print 'warning: fwhm from sexractor different from fwhm computed during pre-reduction'
                print 'try using option --fwhm xxx'

            ff = open('tmp.cursor', 'w')
            image_hdu = fits.open(img + '.fits')
            for i in range(len(xs)):
                _xs = np.delete(xs, i)
                _ys = np.delete(ys, i)
                dist2 = np.sqrt((_xs - xs[i]) ** 2 + (_ys - ys[i]) ** 2)
                ###########           cut  star, not near other object    ##########################
                if abs(fluxrad[i] * 1.6 - fwhm) / fwhm < .5 and min(dist2) > distance * fwhm:
                    x1, x2 = int(xs[i] - fwhm * 3), int(xs[i] + fwhm * 3)
                    y1, y2 = int(ys[i] - fwhm * 3), int(ys[i] + fwhm * 3)
                    if x1 < 1: x1 = 1
                    if y1 < 1: y1 = 1
                    if x2 > int(xdim):
                        x2 = int(xdim)
                    if y2 > int(ydim):
                        y2 = int(ydim)
                    fmax = np.max(image_hdu[0].data[y1-1:y2, x1-1:x2])
                ##########       cut saturated object               ########################
                    if float(fmax) < _datamax:  # not saturated
                        ff.write('%10.3f %10.3f 1 m \n' % (xs[i], ys[i]))
            ff.close()
            image_hdu.close()

            iraf.delete('tmp.lo?,tmp.sta?,tmp.gk?', verify=False)
            iraf.psfmeasure(img+'[0]', imagecur='tmp.cursor', logfile='tmp.log', radius=int(fwhm), iter=3,
                            display=False, StdoutG='tmp.gki')

            ff = open('tmp.log')
            righe = ff.readlines()
            xn = [float(righe[3].split()[1])]
            yn = [float(righe[3].split()[2])]
            _fw = [float(righe[3].split()[4])]
            for r in righe[4:-2]:
                if len(r) > 0:
                    xn.append(float(r.split()[0]))
                    yn.append(float(r.split()[1]))
                    _fw.append(float(r.split()[3]))
            print 'FWHM: ', righe[-1].split()[-1]
            print 80 * "#"
            ######
            ##############            eliminate double object identification         ###########################
            xns, yns, _fws = [xn[0]], [yn[0]], [_fw[0]]
            for i in range(1, len(xn)):
                if abs(xn[i] - xn[i - 1]) > .2 and abs(yn[i] - yn[i - 1]) > .2:
                    xns.append(xn[i])
                    yns.append(yn[i])
                    _fws.append(_fw[i])
            #########      write clean   file for PSF                           #########################
            fw = []
            ff = open('_psf.coo', 'w')
            for i in range(len(xns)):
                if abs(_fws[i] - fwhm) / fwhm < .3:
                    ff.write('%10.3f %10.3f %7.2f \n' % (xns[i], yns[i], float(_fws[i])))
                    fw.append(_fws[i])
            ff.close()  ## End automatic selection
        ######################################################################################
        ###################        write file of object to store in  fits table  #############
        ######################################################################################
        if interactive:
            xs, ys, ran, decn, magbest, classstar, fluxrad, bkg = runsex(img, fwhm, threshold, scale)
            ff = open('_psf2.coo', 'w')
            for i in range(len(xs)):
                ff.write('%10s %10s %10s \n' % (xs[i], ys[i], fluxrad[i]))
            ff.close()
        elif _catalog:
            ddd=iraf.wcsctran(input=_catalog,output='STDOUT',Stdout=1,image=img + '[0]',inwcs='world',outwcs='logical',
                              units='degrees degrees',columns='1 2',formats='%10.1f %10.1f',verbose='no')
            ddd=[i for i in ddd if i[0]!='#']
            ddd=['  '.join(i.split()[0:3]) for i in ddd]
            ff = open('_psf2.coo', 'w')
            for i in ddd:
                a,b,c = string.split(i)
                ff.write('%10s %10s %10s \n' % (a, b, c))
            ff.close()
        else:
            os.system('cp _psf.coo _psf2.coo')
#            dflux = fluxrad - np.median(fluxrad)
#            fstar = np.compress(dflux < np.std(fluxrad), fluxrad)
#################################################################################################################

        print 80 * "#"
        photmag, pst, fitmag = psffit(img, fwhm, psfstars, hdr, interactive, _datamax, psffun, fixaperture)

        photmag2, fitmag2 = psffit2(img, fwhm, psfstars, hdr, _datamax, psffun, fixaperture)

        radec = iraf.wcsctran(input='STDIN', output='STDOUT', Stdin=photmag,
                              Stdout=1, image=img + '[0]', inwcs='logical', outwcs='world', columns="1 2",
                              format='%13.3H %12.2h', min_sig=9, mode='h')[3:]

        radec2 = iraf.wcsctran(input='STDIN', output='STDOUT', Stdin=photmag2,
                               Stdout=1, image=img + '[0]', inwcs='logical', outwcs='world', columns="1 2",
                               format='%13.3H %12.2h', min_sig=9, mode='h')[3:]

        if ds9 == 0 and (interactive or show):
            iraf.set(stdimage='imt1024')
            iraf.display(img, 1, fill=True, Stdout=1)
            iraf.tvmark(1, coords='STDIN', mark='circle', radii=15, label=True, Stdin=photmag, nxoffset=5, nyoffset=5, txsize=2)
            iraf.tvmark(1, coords='STDIN', mark='circle', radii=35, label=False, Stdin=pst, color=208)
#            iraf.tvmark(1, coords='STDIN', mark='cross', length=35, label=False, Stdin=fitmag2, color=204)

        idpsf = []
        for i in range(len(pst)):
            idpsf.append(pst[i].split()[2])
        dmag = []
        for i in range(len(radec)):
            ra, dec, idph, magp2, magp3, magp4, merrp2, merrp3, merrp4 = radec[i].split()
            dmag.append(9.99)
            for j in range(len(fitmag)):
                raf, decf, idf, magf, magerrf = fitmag[j].split()
                if idph == idf and idph in idpsf and \
                                magp3 != 'INDEF' and magf != 'INDEF':
                    dmag[i] = float(magp3) - float(magf)
                    break

        _dmag = np.compress(np.array(dmag) < 9.99, np.array(dmag))

        print '>>> Aperture correction (phot)   %6.3f +/- %5.3f %3d ' % \
              (np.mean(_dmag), np.std(_dmag), len(_dmag))
        if len(_dmag) > 3:
            _dmag = np.compress(np.abs(_dmag - np.median(_dmag)) < 2 * np.std(_dmag), _dmag)
            print '>>>         2 sigma rejection)   %6.3f +/- %5.3f %3d  [default]' \
                  % (np.mean(_dmag), np.std(_dmag), len(_dmag))
            print '>>>     fwhm   %s  ' % (str(fwhm))
        for i in range(len(dmag)):
            if dmag[i] == 9.99:
                dmag[i] = ''
            else:
                dmag[i] = '%6.3f' % (dmag[i])

        exptime = lsc.util.readkey3(hdr, 'exptime')
        object = lsc.util.readkey3(hdr, 'object').replace(' ', '')
        filtro = lsc.util.readkey3(hdr, 'filter')

        #######################################
        rap, decp, magp2, magp3, magp4, smagf, merrp3, smagerrf = [], [], [], [], [], [], [], []
        rap0, decp0 = [], []
        for i in range(len(radec2)):
            aa = radec2[i].split()
            rap.append(aa[0])
            decp.append(aa[1])
            rap0.append(lsc.lscabsphotdef.deg2HMS(ra=aa[0]))
            decp0.append(lsc.lscabsphotdef.deg2HMS(dec=aa[1]))
            idp = aa[2]
            magp2.append(aa[3])
            magp3.append(aa[4])
            magp4.append(aa[5])
            merrp3.append(aa[7])
            _smagf, _smagerrf = 9999, 9999
            for j in range(len(fitmag2)):
                raf, decf, idf, magf, magerrf = fitmag2[j].split()
                if idf == idp:
                    _smagf = magf
                    _smagerrf = magerrf
                    break
            smagf.append(_smagf)
            smagerrf.append(_smagerrf)
        tbhdu = fits.BinTableHDU.from_columns(fits.ColDefs([fits.Column(name='ra', format='20A', array=np.array(rap)),
                                               fits.Column(name='dec', format='20A', array=np.array(decp)),
                                               fits.Column(name='ra0', format='E', array=np.array(rap0)),
                                               fits.Column(name='dec0', format='E', array=np.array(decp0)),
                                               fits.Column(name='magp2', format='E',
                                                           array=np.array(np.where((np.array(magp2) != 'INDEF'),
                                                                                   np.array(magp2), 9999), float)),
                                               fits.Column(name='magp3', format='E',
                                                               array=np.array(np.where((np.array(magp3) != 'INDEF'),
                                                                                       np.array(magp3), 9999), float)),
                                               fits.Column(name='merrp3', format='E',
                                                               array=np.array(np.where((np.array(merrp3) != 'INDEF'),
                                                                                       np.array(merrp3), 9999), float)),
                                               fits.Column(name='magp4', format='E',
                                                               array=np.array(np.where((np.array(magp4) != 'INDEF'),
                                                                                       np.array(magp4), 9999), float)),
                                               fits.Column(name='smagf', format='E',
                                                               array=np.array(np.where((np.array(smagf) != 'INDEF'),
                                                                                       np.array(smagf), 9999), float)),
                                               fits.Column(name='smagerrf', format='E',
                                                               array=np.array(np.where((np.array(smagerrf) != 'INDEF'),
                                                                                       np.array(smagerrf), 9999),
                                                                              float)),
        ]))

        hdu = fits.PrimaryHDU(header=hdr)
        thdulist = fits.HDUList([hdu, tbhdu])
        lsc.util.delete(img + '.sn2.fits')
        thdulist.writeto(img + '.sn2.fits')
        lsc.util.updateheader(img + '.sn2.fits', 0, {'APCO': [np.mean(_dmag), 'Aperture correction']})
        lsc.util.updateheader(img + '.sn2.fits', 0, {'APCOERR': [np.std(_dmag), 'Aperture correction error']})
        lsc.util.updateheader(img + '.sn2.fits', 0, {'XDIM': [lsc.util.readkey3(hdr, 'naxis1'), 'x number of pixels']})
        lsc.util.updateheader(img + '.sn2.fits', 0, {'YDIM': [lsc.util.readkey3(hdr, 'naxis2'), 'y number of pixels']})
        lsc.util.updateheader(img + '.sn2.fits', 0,
                              {'PSF_FWHM': [fwhm * scale, 'FWHM (arcsec) - computed with daophot']})
        os.chmod(img + '.sn2.fits', 0664)
        os.chmod(img + '.psf.fits', 0664)
        result = 1

    except:
        result = 0
        fwhm = 0.0
        traceback.print_exc()
    return result, fwhm * scale
Esempio n. 53
0
def display_image(img,
                  frame,
                  _z1,
                  _z2,
                  scale,
                  _xcen=0.5,
                  _ycen=0.5,
                  _xsize=1,
                  _ysize=1,
                  _erase='yes'):
    goon = 'True'
    import glob, subprocess, os, time
    # removing u : this option does not work on mac
    ds9 = subprocess.Popen("ps -U {:d} | grep -v grep | grep ds9".format(
        os.getuid()),
                           shell=True,
                           stdout=subprocess.PIPE).stdout.readlines()
    if len(ds9) == 0:
        subproc = subprocess.Popen('ds9', shell=True)
        time.sleep(3)

    if glob.glob(img):
        from pyraf import iraf
        iraf.images(_doprint=0)
        iraf.tv(_doprint=0)
        import string, os
        if _z2:
            try:
                sss=iraf.display(img + '[0]', frame, xcen=_xcen, ycen=_ycen, xsize=_xsize, ysize=_ysize, erase=_erase,\
                                     fill='yes', zscale='no', zrange='no', z1=_z1, z2=_z2,Stdout=1)
            except:
                print ''
                print '### ERROR: PROBLEM OPENING DS9'
                print ''
                goon = 'False'
        else:
            try:
                sss = iraf.display(img + '[0]',
                                   frame,
                                   xcen=_xcen,
                                   ycen=_ycen,
                                   xsize=_xsize,
                                   ysize=_ysize,
                                   erase=_erase,
                                   fill='yes',
                                   Stdout=1)
            except:
                print ''
                print '### ERROR: PROBLEM OPENING DS9'
                print ''
                goon = False

        if scale and goon:
            answ0 = raw_input('>>> Cuts OK ? [y/n] ? [y] ')
            if not answ0: answ0 = 'y'
            elif answ0 == 'no' or answ0 == 'NO': answ0 = 'n'

            while answ0 == 'n':
                _z11 = float(string.split(string.split(sss[0])[0], '=')[1])
                _z22 = float(string.split(string.split(sss[0])[1], '=')[1])
                z11 = raw_input('>>> z1 = ? [' + str(_z11) + '] ? ')
                z22 = raw_input('>>> z2 = ? [' + str(_z22) + '] ? ')
                if not z11: z11 = _z11
                else: z11 = float(z11)
                if not z22: z22 = _z22
                else: z22 = float(z22)
                print z11, z22
                sss=iraf.display(img + '[0]',frame,fill='yes', xcen=_xcen, ycen=_ycen, xsize=_xsize, ysize=_ysize, erase=_erase,\
                                     zrange='no', zscale='no', z1=z11, z2=z22, Stdout=1)
                answ0 = raw_input('>>> Cuts OK ? [y/n] ? [y] ')
                if not answ0: answ0 = 'y'
                elif answ0 == 'no' or answ0 == 'NO': answ0 = 'n'
        if goon:
            _z1, _z2 = string.split(string.split(sss[0])[0],
                                    '=')[1], string.split(
                                        string.split(sss[0])[1], '=')[1]
    else:
        print 'Warning: image ' + str(img) + ' not found in the directory '
    return _z1, _z2, goon
Esempio n. 54
0
def marksn2(img, fitstab, frame=1, fitstab2='', verbose=False):
    from pyraf import iraf
    from numpy import array  #,log10
    import lsc
    iraf.noao(_doprint=0)
    iraf.digiphot(_doprint=0)
    iraf.daophot(_doprint=0)
    iraf.images(_doprint=0)
    iraf.imcoords(_doprint=0)
    iraf.proto(_doprint=0)
    iraf.set(stdimage='imt1024')
    hdr = lsc.util.readhdr(fitstab)
    _filter = lsc.util.readkey3(hdr, 'filter')
    column = lsc.lscabsphotdef.makecatalogue([fitstab])[_filter][fitstab]

    rasex = array(column['ra0'], float)
    decsex = array(column['dec0'], float)

    if fitstab2:
        hdr = lsc.util.readhdr(fitstab2)
        _filter = lsc.util.readkey3(hdr, 'filter')
        _exptime = lsc.util.readkey3(hdr, 'exptime')
        column = lsc.lscabsphotdef.makecatalogue([fitstab2])[_filter][fitstab2]
        rasex2 = array(column['ra0'], float)
        decsex2 = array(column['dec0'], float)

    iraf.set(stdimage='imt1024')
    iraf.display(img + '[0]', frame, fill=True, Stdout=1)
    vector = []
    for i in range(0, len(rasex)):
        vector.append(str(rasex[i]) + ' ' + str(decsex[i]))

    xy = iraf.wcsctran('STDIN',output="STDOUT",Stdin=vector,Stdout=1,image=img+'[0]',inwcs='world',units='degrees degrees',outwcs='logical',\
                          formats='%10.1f %10.1f',verbose='yes')[3:]
    iraf.tvmark(frame,
                'STDIN',
                Stdin=list(xy),
                mark="circle",
                number='yes',
                label='no',
                radii=10,
                nxoffse=5,
                nyoffse=5,
                color=207,
                txsize=2)

    if verbose:
        #     print 2.5*log10(_exptime)
        for i in range(0, len(column['ra0'])):
            print xy[i], column['ra0'][i], column['dec0'][i], column['magp3'][
                i], column['magp4'][i], column['smagf'][i], column['magp2'][i]

    if fitstab2:
        vector2 = []
        for i in range(0, len(rasex2)):
            vector2.append(str(rasex2[i]) + ' ' + str(decsex2[i]))
        xy1 = iraf.wcsctran('STDIN',output="STDOUT",Stdin=vector2,Stdout=1,image=img+'[0]',inwcs='world',units='degrees degrees',outwcs='logical',\
                              formats='%10.1f %10.1f',verbose='yes')[3:]
        iraf.tvmark(frame,
                    'STDIN',
                    Stdin=list(xy1),
                    mark="cross",
                    number='yes',
                    label='no',
                    radii=10,
                    nxoffse=5,
                    nyoffse=5,
                    color=205,
                    txsize=2)
Esempio n. 55
0
iraf.imdelete('tmpdq@' + ic.lst_sci)

for sci in iraf.type(ic.lst_sci, Stdout=1):
    sci = sci.strip()
    iraf.copy('eqxbrg' + sci + '.fits', 'tmp' + sci + '.fits')
    for j in np.arange(ic.nslit):
        iraf.proto.fixpix('tmp' + sci + '.fits[sci,{0:1d}]'.format(j + 1),
                          'mskbadcol_' + sci + '_{0:1d}.pl'.format(j + 1),
                          linterp='1,2,3,4')
    iraf.copy('tmp' + sci + '.fits', 'xeqxbrg' + sci + '.fits')
    for j in np.arange(ic.nslit):
        iraf.imarith('mskbadcol_' + sci + '_{0:1d}.pl'.format(j + 1), '+',
                     'xeqxbrg' + sci + '.fits[dq,{0:1d}]'.format(j + 1),
                     'tmpdq' + sci + '.fits[dq,{0:1d}]'.format(j + 1))
        iraf.imcopy('tmpdq' + sci + '.fits[dq,{0:1d}][*,*]'.format(j + 1),
                    'xeqxbrg' + sci + '.fits[dq,{0:1d}][*,*]'.format(j + 1))
        iraf.proto.fixpix('tmp' + sci + '.fits[sci,{0:1d}]'.format(j + 1),
                          'mskbadcol_' + sci + '_{0:1d}.pl'.format(j + 1),
                          linterp='1,2,3,4')

for sci in iraf.type(ic.lst_sci, Stdout=1):
    for j in np.arange(ic.nslit):
        os.system('ds9 &')
        iraf.sleep(5.0)
        iraf.display(image='eqxbrg' + sci + '.fits[sci,{0:1d}]'.format(j + 1),
                     frame=1)
        iraf.display(image='xeqxbrg' + sci + '.fits[sci,{0:1d}]'.format(j + 1),
                     frame=2)

# Printing the running time
print('--- %s seconds ---' % (time.time() - start_time))
Esempio n. 56
0
for i in range(0,len(slist)):
		
	if os.path.isfile(slist[i]) == True:
		print str(slist[i]) + " exists, skipping..."
	elif os.path.isfile(slist[i]) == False: 
		command="scp -r [email protected]:/data/jmcc/diapl/"+str(slist[i]) + " ."
		os.system(command)
		
	
yn=[]
print "\nSelect images to keep or exclude!\n"

# display the images
for i in range(0,len(slist)):
	print "Image: " + str(slist[i]) + " [" + str(i+1) + "/" + str(len(slist)) + "]"  
	iraf.display(image=slist[i],frame='1') 
	q=raw_input("(e.g. y): ")
	yn.append(q)


# enter y to keep a frame, anything else to reject it
new_tpl_list=[]
for i in range(0,len(yn)):
	if str(yn[i]) == 'y':
		new_tpl_list.append(slist[i])


# rename the template list
print "Make new template.list?"
tpl_yn=raw_input("(e.g. y): ")
if str(tpl_yn) == 'y':		
Esempio n. 57
0
def runimedit(mfile,outfile1,nframe):
    continueWithProgram=1
    continueWithObject=1
    repeatflag=1
    while (repeatflag > 0.1):

        iraf.display(mfile,frame=nframe, fill='yes')

        print mfile
        print 'Running imedit to mask out other sources in the field:'
        print 'Enter b to mask out a circular region'
        print 'Enter a to mark the corners of a rectangle'
        print 'Enter q when finished'
        try:
            os.remove(outfile1)
        except OSError:
            print 'everything is ok'
        print 'running imedit ',mfile, outfile1
        iraf.imedit(mfile,outfile1)

        flag=str(raw_input('Are you happy with the editing?  n=no x=quit y (or any other key) = yes '))
        flag=str(flag)
        print 'this is what I think you typed ',flag
        if flag.find('n') > -1:
            flag2=str(raw_input('What is wrong?  r=redo masking, o=nearby object, p=partial image, x=quit '))
            if flag2.find('r') > -1:
                s='rm '+outfile1
                os.system(s)
                repeatflag=1
                print 'i think repeatflag = 1 ', repeatflag
            elif flag2.find('o') > -1:
                s='rm '+outfile1
                os.system(s)

                s='mv '+mfile+' NearbyObjects/'
                os.system(s)
                continueWithObject=0
                return continueWithProgram,continueWithObject
            elif flag2.find('p') > -1:
                s='rm '+outfile1
                os.system(s)

                s='mv '+mfile+' PartialImages/'
                os.system(s)
                continueWithObject=0
                return continueWithProgram,continueWithObject
            elif flag2.find('x') > -1:
                continueWithProgram=0
                repeatflag=0
                print 'i think repeatflag = 0', repeatflag
                return continueWithProgram,continueWithObject
            else: 
                repeatflag=0

        elif flag.find('x') > -1:
            print 'i think you want to exit'
            continueWithProgram=0
            repeatflag=0
            return continueWithProgram,continueWithObject
        else:
            repeatflag=0

    return continueWithProgram,continueWithObject