Esempio n. 1
0
def saltcombine(images,outimage, method='average', reject=None, mask=True,     \
                weight=True, blank=0, scale=None, statsec=None, lthresh=3,    \
                hthresh=3, clobber=False, logfile='salt.log',verbose=True):

   with logging(logfile,debug) as log:

       # Check the input images 
       infiles = saltio.argunpack ('Input',images)

       #set reject as None
       reject=saltio.checkfornone(reject)
       if reject is not None: reject=reject.lower()

       #set scale 
       scale=saltio.checkfornone(scale)
       if scale is not None: scale=scale.lower()

       #set statsec
       statsec=saltio.checkfornone(statsec)
       statsec=saltio.getSection(statsec, iraf_format=True)

       #Fast combine the images
       outstruct=imcombine(infiles, method=method, reject=reject, mask=mask, \
                           weight=weight, blank=blank, scale=scale,          \
                           statsec=statsec, lthresh=lthresh, hthresh=hthresh)

       # housekeeping keywords
       fname, hist=history(level=1, wrap=False)
       saltkey.housekeeping(outstruct[0],'SCOMBINE', 'File Combined by SALTCOMBINE', hist)

       # write FITS file               
       saltio.writefits(outstruct, outimage)
Esempio n. 2
0
def erroremail(obsdate, server='', username='', password='', sender='', recipient='', bcc='',
              sdbhost='sdb.salt', sdbname='sdb', sdbuser='', sdbpass='', logfile='saltlog.log', verbose=True):
    """Update the PipelineStatistics table with the current information
       about the pipeline

    """

    with logging(logfile,debug) as log:

       #connect the database
       sdb=saltmysql.connectdb(sdbhost, sdbname, sdbuser, sdbpass)

       #get the nightinfo_id
       night_id=saltmysql.getnightinfoid(sdb, obsdate)

       #create the message

       try:
          record=saltmysql.select(sdb, 'PipelineStatus, ErrorMessage', 'PipelineStatistics join PipelineStatus using (PipelineStatus_Id)', 'NightInfo_Id=%i' % night_id)
          status, error_message=record[0]
       except Exception, e:
          raise SaltError('Unable to download information: %s' % e)

       #set up the subject and message
       subject='Pipeline Error on %s' % obsdate
       
       message="Failure is simply the opportunity to begin again, this time more intelligently.--Henry Ford\n\n"  
       message+="%s" %  error_message
       log.message(message, with_stdout=verbose)

       #send the email
       saltio.email(server,username,password,sender,recipient,bcc, subject,message)
Esempio n. 3
0
def hrsprepare(images, outimages, outpref, clobber=True, logfile='salt.log',verbose=True):
    """Convert .fit files to .fits files and place HRS data into 
       standard SALT FITS format

    """
 
    with logging(logfile,debug) as log:

       # Check the input images 
       infiles = saltio.argunpack ('Input',images)

       # create list of output files 
       outfiles=saltio.listparse('Outfile', outimages, outpref,infiles,'')

       #verify that the input and output lists are the same length
       saltio.comparelists(infiles,outfiles,'Input','output')

       #open the file and write out as a fits files in the output directory
       for img,oimg in zip(infiles, outfiles):
          hdu=saltio.openfits(img)
          hdu=prepare(hdu)
          log.message('Preparing HRS %s to %s' % (img, oimg), with_header=False)
          saltio.writefits(hdu, oimg, clobber=clobber)
    
 
    return 
Esempio n. 4
0
def saltobslog(images,
               outfile,
               clobber=False,
               logfile='salt.log',
               verbose=True):
    """Create the observation log from the input files"""

    #start the logging
    with logging(logfile, debug) as log:

        # Check the input images
        infiles = saltio.argunpack('Input', images)

        #create the header dictionary
        headerDict = obslog(infiles, log)

        #clobber the output if it exists
        if (clobber and os.path.isfile(outfile)):
            saltio.delete(outfile)

        #create the fits file
        struct = createobslogfits(headerDict)

        # close table file
        saltio.writefits(struct, outfile)

        #indicate the log was created
        log.message('\nSALTLOG -- created observation log ' + outfile)
Esempio n. 5
0
def saltprepare(images,outimages,outpref,createvar=False, badpixelimage=None, clobber=True,logfile='salt.log',verbose=True):

   with logging(logfile,debug) as log:

       # Check the input images 
       infiles = saltio.argunpack ('Input',images)

       # create list of output files 
       outfiles=saltio.listparse('Outfile', outimages, outpref,infiles,'')

       #verify that the input and output lists are the same length
       saltio.comparelists(infiles,outfiles,'Input','output')

       # open the badpixel image
       if saltio.checkfornone(badpixelimage) is None:
          badpixelstruct=None
       else:
           try:
               badpixelstruct = saltio.openfits(badpixelimage)
           except saltio.SaltIOError,e:
               msg='badpixel image must be specificied\n %s' % e
               raise SaltError(msg)

       # open each raw image file
       for img, oimg, in zip(infiles, outfiles):

           #open the fits file
           struct=saltio.openfits(img)

           #if createvar, throw a warning if it is using slotmode
           if saltkey.fastmode(saltkey.get('DETMODE', struct[0])) and createvar:
               msg='Creating variance frames for slotmode data in %s' % img
               log.warning(msg)
  
           # identify instrument
           instrume,keyprep,keygain,keybias,keyxtalk,keyslot = saltkey.instrumid(struct)

           # has file been prepared already?
           try:
               key = struct[0].header[keyprep]
               message = 'ERROR -- SALTPREPARE: File ' + infile
               message += ' has already been prepared'
               raise SaltError(message)
           except:
               pass


           # prepare file
           struct=prepare(struct,createvar=createvar, badpixelstruct=badpixelstruct)

           # housekeeping keywords
           fname, hist=history(level=1, wrap=False, exclude=['images', 'outimages', 'outpref'])
           saltkey.housekeeping(struct[0],keyprep, 'File prepared for IRAF', hist)

           # write FITS file
           saltio.writefits(struct,oimg, clobber=clobber)
           saltio.closefits(struct)

           message = 'SALTPREPARE -- %s => %s' % (img, oimg)
           log.message(message, with_header=False)
Esempio n. 6
0
def bvitftp(propcode, obsdate, sdbhost,sdbname,sdbuser, password, 
            server,username,sender, bcc, emailfile,
            notify=False, clobber=True,logfile='salt.log',verbose=True):

   # set up

   proposers = []
   propids = []
   pids = []
   
   with logging(logfile,debug) as log:

       # are the arguments defined
       if propcode.strip().upper()=='ALL':
          pids=getbvitproposalcodes(str(obsdate))
       else:
          pids = saltio.argunpack('propcode',propcode)
       
       if len(pids)==0:
           #throw a warning adn exit if not data needs to be filterd
           log.warning('No data to distribute\n', with_stdout=verbose)
           return
   
       #log into the database
       sdb=saltmysql.connectdb(sdbhost,sdbname,sdbuser,password)
 
     
       # check PI directories exist
       ftpdir='/salt/ftparea/'
       for datapath in pids:
           pid=os.path.basename(datapath)
           movebvitdata(pid, datapath, ftpdir, obsdate, sdb, clobber)

           if notify:
               notifybvituser(sdb, pid, obsdate, server,username,password,sender, bcc, emailfile)
Esempio n. 7
0
def hrsstack(images,
             outimages,
             outpref,
             clobber=True,
             logfile='salt.log',
             verbose=True):
    """Convert MEF HRS data into a single image.  If variance frames and BPMs, then 
       convert them to the same format as well.   Returns an MEF image but that is
       combined into a single frame

    """

    with logging(logfile, debug) as log:

        # Check the input images
        infiles = saltio.argunpack('Input', images)

        # create list of output files
        outfiles = saltio.listparse('Outfile', outimages, outpref, infiles, '')

        #verify that the input and output lists are the same length
        saltio.comparelists(infiles, outfiles, 'Input', 'output')

        #open the file and write out as a fits files in the output directory
        for img, oimg in zip(infiles, outfiles):
            hdu = saltio.openfits(img)
            hdu = stack(hdu)
            log.message('Stacking HRS %s to %s' % (img, oimg),
                        with_header=False)
            saltio.writefits(hdu, oimg, clobber=clobber)

    return
Esempio n. 8
0
def masktool(catalog='', image='', logfile='salt.log', verbose=True):

   with logging(logfile,debug) as log:
       app = QtGui.QApplication([])
       myapp = SlitMaskGui(infile=catalog, inimage=image)
       myapp.show()
       app.exec_()
Esempio n. 9
0
def saltprepare(images,outimages,outpref,createvar=False, badpixelimage=None, clobber=True,logfile='salt.log',verbose=True):

   with logging(logfile,debug) as log:

       # Check the input images 
       infiles = saltio.argunpack ('Input',images)

       # create list of output files 
       outfiles=saltio.listparse('Outfile', outimages, outpref,infiles,'')

       #verify that the input and output lists are the same length
       saltio.comparelists(infiles,outfiles,'Input','output')

       # open the badpixel image
       if saltio.checkfornone(badpixelimage) is None:
          badpixelstruct=None
       else:
           try:
               badpixelstruct = saltio.openfits(badpixelimage)
           except saltio.SaltIOError,e:
               msg='badpixel image must be specificied\n %s' % e
               raise SaltError(msg)

       # open each raw image file
       for img, oimg, in zip(infiles, outfiles):

           #open the fits file
           struct=saltio.openfits(img)

           #if createvar, throw a warning if it is using slotmode
           if saltkey.fastmode(saltkey.get('DETMODE', struct[0])) and createvar:
               msg='Creating variance frames for slotmode data in %s' % img
               log.warning(msg)
  
           # identify instrument
           instrume,keyprep,keygain,keybias,keyxtalk,keyslot = saltkey.instrumid(struct)

           # has file been prepared already?
           try:
               key = struct[0].header[keyprep]
               message = 'ERROR -- SALTPREPARE: File ' + infile
               message += ' has already been prepared'
               raise SaltError(message)
           except:
               pass


           # prepare file
           struct=prepare(struct,createvar=createvar, badpixelstruct=badpixelstruct)

           # housekeeping keywords
           fname, hist=history(level=1, wrap=False, exclude=['images', 'outimages', 'outpref'])
           saltkey.housekeeping(struct[0],keyprep, 'File prepared for IRAF', hist)

           # write FITS file
           saltio.writefits(struct,oimg, clobber=clobber)
           saltio.closefits(struct)

           message = 'SALTPREPARE -- %s => %s' % (img, oimg)
           log.message(message, with_header=False)
Esempio n. 10
0
def salt2iraf(images,
              outimages,
              outpref,
              ext=1,
              clobber=True,
              logfile='salt.log',
              verbose=True):

    with logging(logfile, debug) as log:

        # Check the input images
        infiles = saltio.argunpack('Input', images)

        # create list of output files
        outfiles = saltio.listparse('Outfile', outimages, outpref, infiles, '')

        #verify that the input and output lists are the same length
        saltio.comparelists(infiles, outfiles, 'Input', 'output')

        for img, oimg in zip(infiles, outfiles):
            message = "SALT2IRAF--Converting %s to %s" % (img, oimg)
            log.message(message, with_header=False)
            try:
                convertsalt(img, oimg, ext=ext, clobber=clobber)
            except SaltError, e:
                log.message('%s' % e)
Esempio n. 11
0
def specidentify(images, linelist, outfile, guesstype, guessfile, function,
                 order, rstep, interact, clobber, logfile, verbose, status):

    with logging(logfile, debug) as log:

        # set up the variables
        infiles = []
        outfiles = []

        # Check the input images
        infiles = saltsafeio.argunpack('Input', images)
        print(infiles)

        # create list of output files
        outfiles = saltsafeio.argunpack('Input', outfile)

        # if outfiles is a single image, turn it into a list
        # of the same length as infiles
        if len(outfiles) != len(infiles):
            if len(outfiles) == 1:
                outfiles = outfiles * len(infiles)
            elif len(outfiles) == 0:
                outfiles = [None] * len(infiles)
            else:
                msg = 'Please enter an appropriate number of outfiles'
                raise SALTSpecError(msg)

        # open the line lists
        slines, sfluxes = st.readlinelist(linelist)

        # Identify the lines in each file
        for img, oimg in zip(infiles, outfiles):
            log.message('Proccessing image %s' % img)
            identify(img, oimg, slines, sfluxes, guesstype, guessfile,
                     function, order, rstep, interact, clobber, log, verbose)
Esempio n. 12
0
def saltsurface(images,outimages,outpref, mask=True,  order=3, minlevel=0,
             clobber=False, logfile='salt.log',verbose=True):


   with logging(logfile,debug) as log:

       # Check the input images 
       infiles = saltio.argunpack ('Input',images)

       # create list of output files 
       outfiles=saltio.listparse('Outfile', outimages, outpref,infiles,'')

       #verify that the input and output lists are the same length
       saltio.comparelists(infiles,outfiles,'Input','output')

       # open each raw image file
       for infile, outfile in zip(infiles,outfiles):
           struct = saltio.openfits(infile)

           
           struct = surface_fit(struct, order=order, mask=mask, minlevel=minlevel)

           #add any header keywords like history 
           fname, hist=history(level=1, wrap=False)
           saltkey.housekeeping(struct[0],'SURFIT', 'File fit by a surface', hist)

           #write it out and close it
           saltio.writefits(struct,outfile,clobber=clobber)
           saltio.closefits(struct)

           #output the information
           log.message('Surface fitted image %s ' % (infile), with_header=False, with_stdout=verbose)
Esempio n. 13
0
def specsky(images,outimages,outpref, method='normal', section=None, 
            function='polynomial', order=2, 
            clobber=True,logfile='salt.log',verbose=True):

   with logging(logfile,debug) as log:

       # Check the input images 
       infiles = saltio.argunpack ('Input',images)

       # create list of output files 
       outfiles=saltio.listparse('Outfile', outimages, outpref,infiles,'')

       if method not in ['normal', 'fit']: 
           msg='%s mode is not supported yet' % method
           raise SALTSpecError(msg)

       if section is None:
           section=saltio.getSection(section)
           msg='This mode is not supported yet'
           raise SALTSpecError(msg)
       else:
           section=saltio.getSection(section)

       # Identify the lines in each file
       for img, ofile in zip(infiles, outfiles):
          log.message('Subtracting sky spectrum in image %s into %s' % (img, ofile))
          #open the images
          hdu=saltio.openfits(img)

          #sky subtract the array
          hdu=skysubtract(hdu, method=method, section=section, funct=function, order=order) 

          #write out the image
          if clobber and os.path.isfile(ofile): saltio.delete(ofile)
          hdu.writeto(ofile)
Esempio n. 14
0
def hrsstack(images, outimages, outpref, clobber=True, logfile='salt.log',verbose=True):
    """Convert MEF HRS data into a single image.  If variance frames and BPMs, then 
       convert them to the same format as well.   Returns an MEF image but that is
       combined into a single frame

    """
 
    with logging(logfile,debug) as log:

       # Check the input images 
       infiles = saltio.argunpack ('Input',images)

       # create list of output files 
       outfiles=saltio.listparse('Outfile', outimages, outpref,infiles,'')

       #verify that the input and output lists are the same length
       saltio.comparelists(infiles,outfiles,'Input','output')

       #open the file and write out as a fits files in the output directory
       for img,oimg in zip(infiles, outfiles):
          hdu=saltio.openfits(img)
          hdu=stack(hdu)
          log.message('Stacking HRS %s to %s' % (img, oimg), with_header=False)
          saltio.writefits(hdu, oimg, clobber=clobber)
    
 
    return 
Esempio n. 15
0
def saltillum(images, outimages, outpref, mbox=11, clobber=False, logfile="salt.log", verbose=True):

    with logging(logfile, debug) as log:

        # Check the input images
        infiles = saltio.argunpack("Input", images)

        # create list of output files
        outfiles = saltio.listparse("Outfile", outimages, outpref, infiles, "")

        # verify that the input and output lists are the same length
        saltio.comparelists(infiles, outfiles, "Input", "output")

        # open each raw image file
        for infile, outfile in zip(infiles, outfiles):
            struct = saltio.openfits(infile)

            struct = illum_cor(struct, mbox)

            # add any header keywords like history
            fname, hist = history(level=1, wrap=False)
            saltkey.housekeeping(struct[0], "SILLUM", "File Illumination corrected", hist)

            # write it out and close it
            saltio.writefits(struct, outfile, clobber=clobber)
            saltio.closefits(struct)

            # output the information
            log.message("Illumination corrected image %s " % (infile), with_header=False, with_stdout=verbose)
Esempio n. 16
0
def saltfirst(obsdate, imdir, prodir, server='smtp.saao.ac.za', readme='readme.fast.template', sdbhost='sdb.salt', sdbname='sdb', sdbuser='', password='',imreduce=True, sexfile='/home/ccd/tools/qred.sex', update=True, clobber=False,logfile='salt.log',verbose=True):

   #Move into the working directory
   if os.path.isdir(prodir):
       if clobber:
           shutil.rmtree(prodir)
           os.mkdir(prodir)
   else:
       os.mkdir(prodir)
   os.chdir(prodir)


   with logging(logfile,debug) as log:

       #create GUI
       App = QtGui.QApplication([])

       #Add information to gui
       aw=FirstWindow(obsdate, imdir, prodir, server=server, readme=readme, sdbhost=sdbhost, sdbname=sdbname, sdbuser=sdbuser, password=password, imreduce=imreduce, sexfile=sexfile, update=update, clobber=clobber, log=log, verbose=verbose)
       aw.setMinimumHeight(800)
       aw.setMinimumWidth(500)
       aw.show()

       # Start application event loop
       exit=App.exec_()

       # Check if GUI was executed succesfully
       if exit!=0:
           raise SaltError('SALTFIRST GUI has unexpected exit status '+str(exit))
Esempio n. 17
0
def saltobslog(images,outfile,clobber=False,logfile='salt.log',verbose=True):
  """Create the observation log from the input files"""

  #start the logging
  with logging(logfile,debug) as log:

       # Check the input images 
       infiles = saltio.argunpack ('Input',images)


       #create the header dictionary
       headerDict=obslog(infiles, log)

       #clobber the output if it exists
       if (clobber and os.path.isfile(outfile)):
           saltio.delete(outfile)

       #create the fits file
       struct=createobslogfits(headerDict)

       # close table file
       saltio.writefits(struct, outfile)

       #indicate the log was created
       log.message('\nSALTLOG -- created observation log ' + outfile)
Esempio n. 18
0
def specselfid(images, outimages, outpref, refimage=None, ystart='middlerow',
               rstep=3, clobber=False, logfile='salt.log', verbose=True):

    with logging(logfile, debug) as log:

        # set up the variables
        infiles = []
        outfiles = []

        # Check the input images
        infiles = saltio.argunpack('Input', images)

        # create list of output files
        outfiles = saltio.listparse(
            'Outimages',
            outimages,
            outpref,
            infiles,
            '')

        # set up defaults
        if saltio.checkfornone(refimage) is not None:
            rhdu = saltio.openfits(refimage)
        else:
            refimage = None

        # read in rectify each image
        for img, oimg, in zip(infiles, outfiles):
            hdu = saltio.openfits(img)
            log.message(
                'Performing self-identification and rectification on %s' %
                img)
            for i in range(1, len(hdu)):
                if hdu[i].name == 'SCI':
                    if refimage is None:
                        sdata = hdu[i].data
                    else:
                        sdata = rhdu[i].data
                    hdu[i].data = selfid(
                        hdu[i].data,
                        sdata,
                        ystart=ystart,
                        rstep=rstep)
                    if saltkey.found('VAREXT', hdu[i]):
                        varext = saltkey.get('VAREXT', hdu[i])
                        hdu[varext].data = selfid(
                            hdu[varext].data,
                            sdata,
                            ystart=ystart,
                            rstep=rstep)
                    if saltkey.found('BPMEXT', hdu[i]):
                        bpmext = saltkey.get('BPMEXT', hdu[i])
                        hdu[bpmext].data = selfid(
                            hdu[bpmext].data,
                            sdata,
                            ystart=ystart,
                            rstep=rstep)

            # write out the oimg
            saltio.writefits(hdu, oimg, clobber=clobber)
Esempio n. 19
0
def saltarchive(obsdate,archpath, clobber,logfile,verbose):
   """Archive the SALT data.  Move the data to /salt/data after it has been run by the pipeline"""

   with logging(logfile,debug) as log:
       #check the entries
       saltio.argdefined('obsdate',obsdate)
       saltio.argdefined('archpath',archpath)

       # create the directory if it does not already exist
       if (not os.path.exists(archpath+obsdate[:4])):
           saltio.createdir(archpath+obsdate[:4])

       #cleen up the directory if it already exists
       archdir=archpath+obsdate[:4]+'/'+obsdate[4:]
       if (os.path.exists(archdir) and clobber):
           for root, dirs, files in os.walk(archpath+obsdate[:4]+'/'+obsdate[4:],topdown=False):
               for file in files:
                   os.remove(os.path.join(root,file))
               for dir in dirs:
                   os.rmdir(os.path.join(root,dir))
           os.rmdir(archpath+obsdate[:4]+'/'+obsdate[4:])
       elif (os.path.exists(archdir) and not clobber):
           message  = 'Cannot overwrite '+archpath+obsdate[:4]+'/'+obsdate[4:]
           raise SaltError(message)
       elif (not os.path.exists(archdir)):
            pass

       saltio.move(obsdate,archdir)
Esempio n. 20
0
def saltcombine(images,outimage, method='average', reject=None, mask=True,     \
                weight=True, blank=0, scale=None, statsec=None, lthresh=3,    \
                hthresh=3, clobber=False, logfile='salt.log',verbose=True):

    with logging(logfile, debug) as log:

        # Check the input images
        infiles = saltio.argunpack('Input', images)

        #set reject as None
        reject = saltio.checkfornone(reject)
        if reject is not None: reject = reject.lower()

        #set scale
        scale = saltio.checkfornone(scale)
        if scale is not None: scale = scale.lower()

        #set statsec
        statsec = saltio.checkfornone(statsec)
        statsec = saltio.getSection(statsec, iraf_format=True)

        #Fast combine the images
        outstruct=imcombine(infiles, method=method, reject=reject, mask=mask, \
                            weight=weight, blank=blank, scale=scale,          \
                            statsec=statsec, lthresh=lthresh, hthresh=hthresh)

        # housekeeping keywords
        fname, hist = history(level=1, wrap=False)
        saltkey.housekeeping(outstruct[0], 'SCOMBINE',
                             'File Combined by SALTCOMBINE', hist)

        # write FITS file
        saltio.writefits(outstruct, outimage)
Esempio n. 21
0
def saltgain(images,
             outimages,
             outpref,
             gaindb=None,
             usedb=False,
             mult=True,
             clobber=True,
             logfile='salt.log',
             verbose=True):

    #start logging
    with logging(logfile, debug) as log:

        # Check the input images
        infiles = saltio.argunpack('Input', images)

        # create list of output files
        outfiles = saltio.listparse('Outfile', outimages, outpref, infiles, '')

        #verify that the input and output lists are the same length
        saltio.comparelists(infiles, outfiles, 'Input', 'output')

        # read in the database file if usedb is true
        if usedb:
            gaindb = gaindb.strip()
            dblist = saltio.readgaindb(gaindb)
        else:
            dblist = []

        for img, oimg in zip(infiles, outfiles):
            #open the fits file
            struct = saltio.openfits(img)

            # identify instrument
            instrume, keyprep, keygain, keybias, keyxtalk, keyslot = saltkey.instrumid(
                struct)

            # has file been prepared already?
            if saltkey.found(keygain, struct[0]):
                message = 'SALTGAIN: %s has already been gain-corrected' % img
                raise SaltError(message)

            # gain correct the data
            struct = gain(struct,
                          mult=mult,
                          usedb=usedb,
                          dblist=dblist,
                          log=log,
                          verbose=verbose)

            # housekeeping keywords
            fname, hist = history(level=1,
                                  wrap=False,
                                  exclude=['images', 'outimages', 'outpref'])
            saltkey.housekeeping(struct[0], keygain,
                                 'Images have been gain corrected', hist)

            # write FITS file
            saltio.writefits(struct, oimg, clobber=clobber)
            saltio.closefits(struct)
Esempio n. 22
0
def masktool(catalog='', image='', logfile='salt.log', verbose=True):

    with logging(logfile, debug) as log:
        app = QtGui.QApplication([])
        myapp = SlitMaskGui(infile=catalog, inimage=image)
        myapp.show()
        app.exec_()
Esempio n. 23
0
def specidentify(images,linelist, outfile, guesstype, guessfile, function, \
                 order, rstep, interact, clobber,logfile,verbose,status):

   with logging(logfile,debug) as log:

       #set up the variables
       infiles = []
       outfiles = []
       status = 0

       # Check the input images 
       infiles = saltsafeio.argunpack ('Input',images)

       # create list of output files 
       outfiles = saltsafeio.argunpack ('Input',outfile)

       #if outfiles is a single image, turn it into a list 
       #of the same length as infiles
       if len(outfiles)!=len(infiles):
          if len(outfiles)==1:
               outfiles=outfiles*len(infiles)
          elif len(outfiles)==0:
               outfiles=[None]*len(infiles)
          else:
               msg='Please enter an appropriate number of outfiles'
               raise SALTSpecError(msg)

       # open the line lists
       slines, sfluxes = readlinelist(linelist)

       # Identify the lines in each file
       for img, oimg in zip(infiles, outfiles):
           identify(img, oimg, slines, sfluxes, guesstype, guessfile, function, 
                    order, rstep, interact, clobber, verbose)
Esempio n. 24
0
def saltfirst(obsdate, imdir, prodir, server='smtp.saao.ac.za', readme='readme.fast.template', sdbhost='sdb.salt', sdbname='sdb', sdbuser='', password='',imreduce=True, sexfile='/home/ccd/tools/qred.sex', update=True, clobber=False,logfile='salt.log',verbose=True):

   #Move into the working directory
   if os.path.isdir(prodir):
       if clobber:
           shutil.rmtree(prodir)
           os.mkdir(prodir)
   else:
       os.mkdir(prodir)
   os.chdir(prodir)


   with logging(logfile,debug) as log:

       #create GUI
       App = QtGui.QApplication([])

       #Add information to gui
       aw=FirstWindow(obsdate, imdir, prodir, server=server, readme=readme, sdbhost=sdbhost, sdbname=sdbname, sdbuser=sdbuser, password=password, imreduce=imreduce, sexfile=sexfile, update=update, clobber=clobber, log=log, verbose=verbose)
       aw.setMinimumHeight(800)
       aw.setMinimumWidth(500)
       aw.show()

       # Start application event loop
       exit=App.exec_()

       # Check if GUI was executed succesfully
       if exit!=0:
           raise SaltError('SALTFIRST GUI has unexpected exit status '+str(exit))
Esempio n. 25
0
def saltillum(images,outimages,outpref, mbox=11, clobber=False,     \
             logfile='salt.log',verbose=True):

    with logging(logfile, debug) as log:

        # Check the input images
        infiles = saltio.argunpack('Input', images)

        # create list of output files
        outfiles = saltio.listparse('Outfile', outimages, outpref, infiles, '')

        #verify that the input and output lists are the same length
        saltio.comparelists(infiles, outfiles, 'Input', 'output')

        # open each raw image file
        for infile, outfile in zip(infiles, outfiles):
            struct = saltio.openfits(infile)

            struct = illum_cor(struct, mbox)

            #add any header keywords like history
            fname, hist = history(level=1, wrap=False)
            saltkey.housekeeping(struct[0], 'SILLUM',
                                 'File Illumination corrected', hist)

            #write it out and close it
            saltio.writefits(struct, outfile, clobber=clobber)
            saltio.closefits(struct)

            #output the information
            log.message('Illumination corrected image %s ' % (infile),
                        with_header=False,
                        with_stdout=verbose)
Esempio n. 26
0
def saltelserror(obsdate, elshost, elsname, elsuser, elspass, 
            sdbhost,sdbname,sdbuser, password, clobber,logfile,verbose):

   # set up

   proposers = []
   propids = []
   pids = []
   
   with logging(logfile,debug) as log:

       #open the database
       els=saltmysql.connectdb(elshost, elsname, elsuser, elspass)
       sdb=saltmysql.connectdb(sdbhost,sdbname,sdbuser, password)  

       #create the values for the entire night
       nid=saltmysql.getnightinfoid(sdb, obsdate)
       stime, etime=saltmysql.select(sdb, 'EveningTwilightEnd,MorningTwilightStart', 'NightInfo', 'NightInfo_Id=%i' % nid)[0]
       print stime, etime

       errors=gettcserrors(els, stime, etime)
       if not errors: return None

       for e in errors:
           i=e.index('T')
           print i, error_list[i], e[-1]
       
       print len(errors)
Esempio n. 27
0
def specselfid(images,
               outimages,
               outpref,
               refimage=None,
               ystart='middlerow',
               rstep=3,
               clobber=False,
               logfile='salt.log',
               verbose=True):

    with logging(logfile, debug) as log:

        # set up the variables
        infiles = []
        outfiles = []

        # Check the input images
        infiles = saltio.argunpack('Input', images)

        # create list of output files
        outfiles = saltio.listparse('Outimages', outimages, outpref, infiles,
                                    '')

        # set up defaults
        if saltio.checkfornone(refimage) is not None:
            rhdu = saltio.openfits(refimage)
        else:
            refimage = None

        # read in rectify each image
        for img, oimg, in zip(infiles, outfiles):
            hdu = saltio.openfits(img)
            log.message(
                'Performing self-identification and rectification on %s' % img)
            for i in range(1, len(hdu)):
                if hdu[i].name == 'SCI':
                    if refimage is None:
                        sdata = hdu[i].data
                    else:
                        sdata = rhdu[i].data
                    hdu[i].data = selfid(hdu[i].data,
                                         sdata,
                                         ystart=ystart,
                                         rstep=rstep)
                    if saltkey.found('VAREXT', hdu[i]):
                        varext = saltkey.get('VAREXT', hdu[i])
                        hdu[varext].data = selfid(hdu[varext].data,
                                                  sdata,
                                                  ystart=ystart,
                                                  rstep=rstep)
                    if saltkey.found('BPMEXT', hdu[i]):
                        bpmext = saltkey.get('BPMEXT', hdu[i])
                        hdu[bpmext].data = selfid(hdu[bpmext].data,
                                                  sdata,
                                                  ystart=ystart,
                                                  rstep=rstep)

            # write out the oimg
            saltio.writefits(hdu, oimg, clobber=clobber)
Esempio n. 28
0
def saltfpevelocity(infile, existfit, xc, yc, rmax, logfile, verbose):
    """XXX"""

    # getting paths for filenames

    pathin = os.path.dirname(infile)
    basein = os.path.basename(infile)
    pathlog = os.path.dirname(logfile)
    baselog = os.path.basename(logfile)

    print pathin, basein

    # start log now that all parameters are set up
    with logging(logfile, debug) as log:

        # Some basic checks, some tests are done in the FORTRAN code itself
        # is the input file specified?
        saltsafeio.filedefined('Input', infile)

        # if the input file is a file, does it exist?
        if basein[0] != '@':
            saltsafeio.fileexists(infile)

# if a list is input throw an error
        if basein[0] == '@':
            raise SaltIOError(basein + ' list input instead of a file')

# The file contains a list of images, check they exist
# format is 4x dummy lines, then a line/lines containing: x,y,wave0,sn,norm,dnorm,filename

        testfile = open(infile)
        print testfile
        for i in range(1, 5):
            line = testfile.readline()

        line = 'dummy'
        infiles = []
        while (len(line.strip()) != 0):
            line = testfile.readline()
            if (len(line.strip()) > 0):
                linearray = line.split()
                infiles.append(linearray[6])

# check input files exist
        saltsafeio.filesexist(infiles, '', 'r')

        # convert existfile to a one letter string
        if existfit == 'yes':
            existfit == 'y'
        else:
            existfit == 'n'

# If all looks OK, run the FORTRAN code

        print infile, 'input filename'
        print existfit, xc, yc, rmax

        evelocity_wrapper2.evelocity2(infile, existfit, xc, yc, rmax)
Esempio n. 29
0
def saltfpskyring(
    images,
    outimages,
    outpref,
    axc,
    ayc,
    arad,
    rxc,
    ryc,
    pmin,
    pmax,
    swindow=5,
    clobber=False,
    logfile="salt.log",
    verbose=True,
):
    """Sky subtracts Fabry-Perot images"""

    # start log now that all parameter are set up

    with logging(logfile, debug) as log:

        # Check the input images
        infiles = saltio.argunpack("Input", images)

        # create list of output files
        outfiles = saltio.listparse("Outfile", outimages, outpref, infiles, "")

        # verify that the input and output lists are the same length
        saltio.comparelists(infiles, outfiles, "Input", "output")

        for img, oimg in zip(infiles, outfiles):

            # open up the file
            hdu = saltio.openfits(img)

            # determine the azimuthally averaged profile
            rpro, radial_profile = median_radial_profile(
                hdu[0].data, xc=axc, yc=ayc, rmax=arad, nbins=100, pmin=pmin, pmax=pmax
            )

            if swindow > 1:
                radial_profile = np.convolve(radial_profile, np.ones(swindow), mode="same") / swindow

            # calculate the indices from the image
            y, x = np.indices(hdu[0].data.shape)
            radius = np.hypot(x - axc, y - ayc)

            # subtract off the sky data
            mdata = hdu[0].data - np.interp(radius, rpro, radial_profile)
            hdu[0].data = mdata

            # write FITS file
            saltio.writefits(hdu, oimg, clobber=clobber)
            saltio.closefits(hdu)

            message = "SALTFPSKYRING -- Subtracted sky from  %s" % (img)
            log.message(message, with_header=False)
Esempio n. 30
0
def saltfpeprofile(plottype, infile, logfile, verbose):
    """Line profile fitting and display"""

    plottype = str(plottype)
    plottype = plottype.strip()
    if plottype == 'xwindow':
        plottype = '/xw'
    else:
        plottype = '/ps'

# getting paths for filenames

    pathin = os.path.dirname(infile)
    basein = os.path.basename(infile)
    pathlog = os.path.dirname(logfile)
    baselog = os.path.basename(logfile)

    print pathin, basein

    # start log now that all parameters are set up
    with logging(logfile, debug) as log:

        # Some basic checks, some tests are done in the FORTRAN code itself
        # is the input file specified?
        saltsafeio.filedefined('Input', infile)

        # if the input file is a file, does it exist?
        if basein[0] != '@':
            saltsafeio.fileexists(infile)

# if a list is input throw an error
        if basein[0] == '@':
            raise SaltIOError(basein + ' list input instead of a file')

# The file contains a list of images, check they exist
# format is 4x dummy lines, then a line/lines containing: x,y,wave0,sn,norm,dnorm,filename

        testfile = open(infile)
        print testfile
        for i in range(1, 5):
            line = testfile.readline()

        line = 'dummy'
        infiles = []
        while (len(line.strip()) != 0):
            line = testfile.readline()
            if (len(line.strip()) > 0):
                linearray = line.split()
                infiles.append(linearray[6])

# check input files exist
        saltsafeio.filesexist(infiles, '', 'r')

        # If all looks OK, run the FORTRAN code

        print 'input filename = ', infile

        eprofile_wrapper.eprofile(plottype, infile)
Esempio n. 31
0
def specextract(images,
                outfile,
                method='normal',
                section=None,
                thresh=3.0,
                minsize=3.0,
                outformat='ascii',
                ext=1,
                convert=True,
                clobber=True,
                logfile='salt.log',
                verbose=True):

    with logging(logfile, debug) as log:

        # Check the input images
        infiles = saltio.argunpack('Input', images)

        # create list of output files
        outfiles = saltio.argunpack('outfile', outfile)

        if method is 'weighted':
            msg = 'This mode is not supported yet'
            raise SALTSpecError(msg)

        section = saltio.checkfornone(section)
        if section is not None:
            sections = saltio.getSection(section, iraf_format=False)
            section = []
            for i in range(0, len(sections), 2):
                section.append((sections[i], sections[i + 1]))

        # Identify the lines in each file
        for img, ofile in zip(infiles, outfiles):
            log.message('\nExtracting spectrum in image %s to %s' %
                        (img, ofile),
                        with_header=False,
                        with_stdout=verbose)

            # open the images
            hdu = saltio.openfits(img)
            ap_list = extract(hdu,
                              ext=ext,
                              method=method,
                              section=section,
                              minsize=minsize,
                              thresh=thresh,
                              convert=convert)

            # write the spectra out
            if ap_list:
                write_extract(ofile.strip(),
                              ap_list,
                              outformat=outformat,
                              clobber=clobber)

        log.message('', with_header=False, with_stdout=verbose)
Esempio n. 32
0
def saltxtalk(images,outimages,outpref,xtalkfile=None, usedb=False,
              clobber=True, logfile='salt.log',verbose=True):

   #start logging
   with logging(logfile,debug) as log:

       # Check the input images 
       infiles = saltio.argunpack ('Input',images)

       # create list of output files 
       outfiles=saltio.listparse('Outfile', outimages, outpref,infiles,'')

       # are input and output lists the same length?
       saltio.comparelists(infiles,outfiles,'Input','output')

       # does crosstalk coefficient data exist
       if usedb:
           xtalkfile = xtalkfile.strip()
           xdict = saltio.readxtalkcoeff(xtalkfile)
       else:
           xdict=None

       for img, oimg in zip(infiles, outfiles):

           #open the fits file
           struct=saltio.openfits(img)

           #find the best xcoeff for the image if using the db
           if usedb:
              obsdate=saltkey.get('DATE-OBS', struct[0])
              obsdate=int('%s%s%s' % (obsdate[0:4],obsdate[5:7], obsdate[8:]))
              xkey=np.array(xdict.keys())
              date=xkey[abs(xkey-obsdate).argmin()]
              xcoeff=xdict[date]
           else:
              xcoeff=[]

           # identify instrument
           instrume,keyprep,keygain,keybias,keyxtalk,keyslot = saltkey.instrumid(struct)

           # has file been prepared already?
           if saltkey.found(keyxtalk, struct[0]):
               message='%s has already been xtalk corrected' % img
               raise SaltError(message)


           #apply the cross-talk correction
           struct = xtalk(struct, xcoeff, log=log, verbose=verbose)

           # housekeeping keywords
           fname, hist=history(level=1, wrap=False, exclude=['images', 'outimages', 'outpref'])
           saltkey.housekeeping(struct[0], 'SXTALK', 'Images have been xtalk corrected', hist)

           # write FITS file
           saltio.writefits(struct,oimg, clobber=clobber)
           saltio.closefits(struct)
Esempio n. 33
0
def saltfpcalibrate(plottype, infile, outfile, calibratelogfile, logfile,
                    verbose):
    """Determines the wavelength calibration of the Fabry-Perot    
   interactively, using the ring measurements from the CALRING program."""

    plottype = str(plottype)
    plottype = plottype.strip()
    if plottype == 'xwindow':
        plottype = '/xw'
    else:
        plottype = '/ps'

# start log now that all parameter are set up
    with logging(logfile, debug) as log:

        # Some basic checks, many tests are done in the FORTRAN code itself
        # is the input file specified?
        saltsafeio.filedefined('Input', infile)

        # The input file in this case is a file, does it exist?
        if infile[0] != '@':
            saltsafeio.fileexists(infile)

# is the proposed output file specified?

        saltsafeio.filedefined('Output', outfile)

        # check whether the proposed output file exists

        path = os.path.dirname(infile)

        if len(path) > 0:
            dir = path + '/'
        else:
            dir = './'

        print dir, ' directory with data'
        outfile = outfile.strip()
        if os.path.isfile(outfile):
            print 'output file exists, appending'
#            saltsafeio.delete(outfile)

# check whether the calibrate logfile is defined

        saltsafeio.filedefined('Log', calibratelogfile)

        # Get current working directory as the Fortran code changes dir
        startdir = os.getcwd()

        # If all looks OK, run the FORTRAN code

        calibrate_wrapper.calibrate(plottype, infile, outfile,
                                    calibratelogfile)

        # go back to starting directory
        os.chdir(startdir)
Esempio n. 34
0
def saltcrclean(images,outimages,outpref,crtype='fast',thresh=5,mbox=3,         \
                bthresh=3, flux_ratio=0.2, bbox=11, gain=1, rdnoise=5, fthresh=5,\
                bfactor=2, gbox=3, maxiter=5, multithread=False, update=True,
                clobber=True,  logfile='salt.log', verbose=True):


   with logging(logfile,debug) as log:


       # Check the input images 
       infiles = saltio.argunpack ('Input',images)

       # create list of output files 
       outfiles=saltio.listparse('Outfile', outimages, outpref,infiles,'')

       #verify that the input and output lists are the same length
       saltio.comparelists(infiles,outfiles,'Input','output')

       #check to see if multithreading is available
       if mp:
          pass
       else:
          multithread=False
          log.warning('multiprocessing module is not available.  Setting multiththread=False')


       # Begin processes each file
       for infile,outfile in zip(infiles,outfiles):

           #open the infile
           struct=saltio.openfits(infile)

           #clean the cosmic rays
           if multithread and len(struct)>1:
               struct=multicrclean(struct, crtype, thresh, mbox, bbox, bthresh, flux_ratio, \
                          gain, rdnoise, bfactor, fthresh, gbox, maxiter, log, verbose=verbose)
           else:
               struct=crclean(struct, crtype, thresh, mbox, bbox, bthresh, flux_ratio, \
                          gain, rdnoise, bfactor, fthresh, gbox, maxiter, update, log, verbose=verbose)
          
           #log the call
           #log.message('Cleaned %i cosmic rays from %s using %s method' % (totcr, infile, crtype), with_header=False)
           log.message('', with_header=False, with_stdout=verbose)

           #add house keeping keywords
           saltkey.put('SAL-TLM',time.asctime(time.localtime()), struct[0])
   
           #add the history keyword
           fname, hist=history(level=1, wrap=False)
           saltkey.history(struct[0],hist)

           #write out the file
           saltio.writefits(struct, outfile, clobber=clobber)

           #close the image
           saltio.closefits(struct)
Esempio n. 35
0
def saltfpcalibrate(plottype,infile,outfile,calibratelogfile,logfile,verbose):  

    """Determines the wavelength calibration of the Fabry-Perot    
   interactively, using the ring measurements from the CALRING program."""

    plottype = str(plottype)
    plottype = plottype.strip()
    if plottype == 'xwindow':
        plottype = '/xw'
    else:
        plottype = '/ps'
            
# start log now that all parameter are set up          
    with logging(logfile, debug) as log:

# Some basic checks, many tests are done in the FORTRAN code itself
# is the input file specified?
        saltsafeio.filedefined('Input',infile)

# The input file in this case is a file, does it exist?
        if infile[0] != '@':
            saltsafeio.fileexists(infile)

# is the proposed output file specified?

        saltsafeio.filedefined('Output',outfile)

# check whether the proposed output file exists

        path = os.path.dirname(infile)

        if len(path) > 0:
            dir = path + '/'
        else:
            dir = './'

        print dir, ' directory with data'
        outfile = outfile.strip()
        if os.path.isfile(outfile):
            print 'output file exists, appending'
#            saltsafeio.delete(outfile)

# check whether the calibrate logfile is defined

        saltsafeio.filedefined('Log',calibratelogfile)

        # Get current working directory as the Fortran code changes dir
        startdir = os.getcwd()
    
# If all looks OK, run the FORTRAN code
            
        calibrate_wrapper.calibrate(plottype,infile, outfile, calibratelogfile)

        # go back to starting directory
        os.chdir(startdir)   
Esempio n. 36
0
def specwavemap(
    images,
    outimages,
    outpref,
    solfile=None,
    caltype="line",
    function="polynomial",
    order=3,
    blank=0,
    nearest=False,
    clobber=True,
    logfile="salt.log",
    verbose=True,
):

    with logging(logfile, debug) as log:

        # set up the variables
        infiles = []
        outfiles = []

        # Check the input images
        infiles = saltsafeio.argunpack("Input", images)

        # create list of output files
        outfiles = saltsafeio.listparse("Outimages", outimages, outpref, infiles, "")

        # read in the wavelength solutions and enter them into
        # there own format
        if caltype == "line":
            soldict = sr.entersolution(solfile)
        else:
            soldict = None

        # read in rectify each image
        for img, oimg in zip(infiles, outfiles):
            if caltype == "line":
                msg = "Creating wave map image %s from image %s using files %s" % (oimg, img, solfile)
            else:
                msg = "Creating wave map image %s from image %s using RSS Model" % (oimg, img)
            log.message(msg)
            hdu = saltsafeio.openfits(img)
            hdu = wavemap(
                hdu,
                soldict,
                caltype=caltype,
                function=function,
                order=order,
                blank=blank,
                nearest=nearest,
                clobber=clobber,
                log=log,
                verbose=verbose,
            )
            saltsafeio.writefits(hdu, oimg, clobber=clobber)
Esempio n. 37
0
def saltcrclean(images,outimages,outpref,crtype='fast',thresh=5,mbox=3,         \
                bthresh=3, flux_ratio=0.2, bbox=11, gain=1, rdnoise=5, fthresh=5,\
                bfactor=2, gbox=3, maxiter=5, multithread=False, update=True,
                clobber=True,  logfile='salt.log', verbose=True):

    with logging(logfile, debug) as log:

        # Check the input images
        infiles = saltio.argunpack('Input', images)

        # create list of output files
        outfiles = saltio.listparse('Outfile', outimages, outpref, infiles, '')

        #verify that the input and output lists are the same length
        saltio.comparelists(infiles, outfiles, 'Input', 'output')

        #check to see if multithreading is available
        if mp:
            pass
        else:
            multithread = False
            log.warning(
                'multiprocessing module is not available.  Setting multiththread=False'
            )

        # Begin processes each file
        for infile, outfile in zip(infiles, outfiles):

            #open the infile
            struct = saltio.openfits(infile)

            #clean the cosmic rays
            if multithread and len(struct) > 1:
                struct=multicrclean(struct, crtype, thresh, mbox, bbox, bthresh, flux_ratio, \
                           gain, rdnoise, bfactor, fthresh, gbox, maxiter, log, verbose=verbose)
            else:
                struct=crclean(struct, crtype, thresh, mbox, bbox, bthresh, flux_ratio, \
                           gain, rdnoise, bfactor, fthresh, gbox, maxiter, update, log, verbose=verbose)

            #log the call
            #log.message('Cleaned %i cosmic rays from %s using %s method' % (totcr, infile, crtype), with_header=False)
            log.message('', with_header=False, with_stdout=verbose)

            #add house keeping keywords
            saltkey.put('SAL-TLM', time.asctime(time.localtime()), struct[0])

            #add the history keyword
            fname, hist = history(level=1, wrap=False)
            saltkey.history(struct[0], hist)

            #write out the file
            saltio.writefits(struct, outfile, clobber=clobber)

            #close the image
            saltio.closefits(struct)
Esempio n. 38
0
def specsens(specfile, outfile, stdfile, extfile, airmass=None, exptime=None,
                stdzp=3.68e-20, function='polynomial', order=3, thresh=3, niter=5, 
                clobber=True, logfile='salt.log',verbose=True):

   with logging(logfile,debug) as log:

       #read in the specfile and create a spectrum object
       obs_spectra=st.readspectrum(specfile, error=True, ftype='ascii')

       #read in the std file and convert from magnitudes to fnu
       #then convert it to fwave (ergs/s/cm2/A)
       std_spectra=st.readspectrum(stdfile, error=False, ftype='ascii')
       std_spectra.flux=Spectrum.magtoflux(std_spectra.flux, stdzp)
       std_spectra.flux=Spectrum.fnutofwave(std_spectra.wavelength, std_spectra.flux)


       #read in the extinction file (leave in magnitudes)
       ext_spectra=st.readspectrum(extfile, error=False, ftype='ascii')

       #determine the airmass if not specified
       if saltio.checkfornone(airmass) is None:
           message='Airmass was not supplied'
           raise SALTSpecError(message)

       #determine the exptime if not specified
       if saltio.checkfornone(airmass) is None:
           message='Exposure Time was not supplied'
           raise SALTSpecError(message)
          
       #calculate the calibrated spectra
       log.message('Calculating the calibration curve for %s' % specfile)
       cal_spectra=sensfunc(obs_spectra, std_spectra, ext_spectra, airmass, exptime)

       #fit the spectra--first take a first cut of the spectra
       #using the median absolute deviation to throw away bad points
       cmed=np.median(cal_spectra.flux)
       cmad=saltstat.mad(cal_spectra.flux)
       mask=(abs(cal_spectra.flux-cmed)<thresh*cmad)
       mask=(cal_spectra.flux>0)

       #now fit the data
       fit=interfit(cal_spectra.wavelength[mask], cal_spectra.flux[mask], function=function, order=order, thresh=thresh, niter=niter)
       fit.interfit()

       #print 'plotting...'
       #figure()
       #plot(cal_spectra.wavelength, cal_spectra.flux)
       #plot(obs_spectra.wavelength, obs_spectra.flux*cal_spectra.flux.mean()/obs_spectra.flux.mean())
       #plot(std_spectra.wavelength, std_spectra.flux*cal_spectra.flux.mean()/std_spectra.flux.mean())
       #plot(cal_spectra.wavelength, fit(cal_spectra.wavelength))
       #show()

       #write the spectra out
       cal_spectra.flux=fit(cal_spectra.wavelength)
       st.writespectrum(cal_spectra, outfile, ftype='ascii')
Esempio n. 39
0
def saltslot(images,
             outimages,
             outpref,
             gaindb='',
             xtalkfile='',
             usedb=False,
             clobber=False,
             logfile='salt.log',
             verbose=True):

    #start logging
    with logging(logfile, debug) as log:

        # Check the input images
        infiles = saltio.argunpack('Input', images)

        # create list of output files
        outfiles = saltio.listparse('Outfile', outimages, outpref, infiles, '')

        # are input and output lists the same length?
        saltio.comparelists(infiles, outfiles, 'Input', 'output')

        # does crosstalk coefficient data exist
        if usedb:
            dblist = saltio.readgaindb(gaindb)
            xtalkfile = xtalkfile.strip()
            xdict = saltio.readxtalkcoeff(xtalkfile)
        else:
            dblist = []
            xdict = None

        for img, oimg in zip(infiles, outfiles):
            #open the fits file
            struct = saltio.openfits(img)

            # identify instrument
            instrume, keyprep, keygain, keybias, keyxtalk, keyslot = saltkey.instrumid(
                struct)

            # has file been prepared already?
            if saltkey.found(keygain, struct[0]):
                message = '%s has already been reduced' % img
                raise SaltError(message)

            # housekeeping keywords
            fname, hist = history(level=1,
                                  wrap=False,
                                  exclude=['images', 'outimages', 'outpref'])
            saltkey.housekeeping(struct[0], keyslot,
                                 'Images have been slotmode reduced', hist)

            # write FITS file
            saltio.writefits(struct, oimg, clobber=clobber)
            saltio.closefits(struct)
Esempio n. 40
0
def specwavemap(images,
                outimages,
                outpref,
                solfile=None,
                caltype='line',
                function='polynomial',
                order=3,
                blank=0,
                nearest=False,
                clobber=True,
                logfile='salt.log',
                verbose=True):

    with logging(logfile, debug) as log:

        # set up the variables
        infiles = []
        outfiles = []

        # Check the input images
        infiles = saltsafeio.argunpack('Input', images)

        # create list of output files
        outfiles = saltsafeio.listparse('Outimages', outimages, outpref,
                                        infiles, '')

        # read in the wavelength solutions and enter them into
        # there own format
        if caltype == 'line':
            soldict = sr.entersolution(solfile)
        else:
            soldict = None

        # read in rectify each image
        for img, oimg, in zip(infiles, outfiles):
            if caltype == 'line':
                msg = 'Creating wave map image %s from image %s using files %s' % (
                    oimg, img, solfile)
            else:
                msg = 'Creating wave map image %s from image %s using RSS Model' % (
                    oimg, img)
            log.message(msg)
            hdu = saltsafeio.openfits(img)
            hdu = wavemap(hdu,
                          soldict,
                          caltype=caltype,
                          function=function,
                          order=order,
                          blank=blank,
                          nearest=nearest,
                          clobber=clobber,
                          log=log,
                          verbose=verbose)
            saltsafeio.writefits(hdu, oimg, clobber=clobber)
Esempio n. 41
0
def saltarith(operand1, op, operand2, result, outpref, divzero=0, clobber=False,     \
             logfile='salt.log',verbose=True):

    with logging(logfile, debug) as log:

        # Check the input images
        infiles = saltio.argunpack('Input', operand1)

        # create list of output files
        outfiles = saltio.listparse('Outfile', result, outpref, infiles, '')

        #verify that the input and output lists are the same length
        saltio.comparelists(infiles, outfiles, 'Input', 'output')

        #let's keep track of whether operand2 is an image or not
        is_image = False

        #load in operand2, or, if it's not an image, assume it's a number
        try:
            operand2struct = float(operand2)
        except ValueError:
            operand2struct = saltio.openfits(operand2)
            is_image = True

        #open the input image files
        for infile, outfile in zip(infiles, outfiles):
            struct = saltio.openfits(infile)

            #do some math!
            outstruct = arith(struct, op, operand2struct, is_image, divzero)
            try:
                pass
            except Exception, e:
                msg = 'Unable to do math %s because %s' % (infile, e)
                raise SaltError(msg)

            #update header stuff
            fname, hist = history(level=1, wrap=False)
            saltkey.housekeeping(struct[0], 'SARITH',
                                 'Some arithmatic was performed', hist)

            #write it. close it.
            saltio.writefits(outstruct, outfile, clobber=clobber)
            saltio.closefits(struct)

            #output the information
            log.message('imarith: %s %s %s %s' %
                        (infile, op, operand2, outfile),
                        with_header=False,
                        with_stdout=verbose)

        #close the operand2 image
        if is_image: saltio.closefits(operand2struct)
Esempio n. 42
0
def saltobsstats(obsdate, statfile, sdbhost='sdb.saao', sdbname='sdb', \
              sdbuser='', password='', clobber=False, logfile='salt.log', verbose=True):
    """Measure statistics about SALT observations

       obsdate: Night for measuring statistics
       statfile: Output file for statistics

    """

    with logging(logfile,debug) as log:
 
       #connect to the sdb
       sdb=saltmysql.connect(sdbhost, sdbname, sdbuser, password)
Esempio n. 43
0
def specextract(
    images,
    outfile,
    method="normal",
    section=None,
    thresh=3.0,
    minsize=3.0,
    outformat="ascii",
    ext=1,
    convert=True,
    clobber=True,
    logfile="salt.log",
    verbose=True,
):

    with logging(logfile, debug) as log:

        # Check the input images
        infiles = saltio.argunpack("Input", images)

        # create list of output files
        outfiles = saltio.argunpack("outfile", outfile)

        if method is "weighted":
            msg = "This mode is not supported yet"
            raise SALTSpecError(msg)

        section = saltio.checkfornone(section)
        if section is not None:
            sections = saltio.getSection(section, iraf_format=False)
            section = []
            for i in range(0, len(sections), 2):
                section.append((sections[i], sections[i + 1]))

        # Identify the lines in each file
        for img, ofile in zip(infiles, outfiles):
            log.message(
                "\nExtracting spectrum in image %s to %s" % (img, ofile), with_header=False, with_stdout=verbose
            )

            # open the images
            hdu = saltio.openfits(img)
            ap_list = extract(
                hdu, ext=ext, method=method, section=section, minsize=minsize, thresh=thresh, convert=convert
            )

            # write the spectra out
            if ap_list:
                write_extract(ofile.strip(), ap_list, outformat=outformat, clobber=clobber)

        log.message("", with_header=False, with_stdout=verbose)
Esempio n. 44
0
def slotreadtimefix(images,
                    outimages,
                    outpref,
                    clobber=False,
                    logfile='slot.log',
                    verbose=True):

    with logging(logfile, debug) as log:

        # Check the input images
        infiles = saltio.argunpack('Input', images)

        # create list of output files
        outfiles = saltio.listparse('Outfile', outimages, outpref, infiles, '')

        #verify that the input and output lists are the same length
        saltio.comparelists(infiles, outfiles, 'Input', 'output')

        for img, oimg in zip(infiles, outfiles):
            #check to see if the out image already exists
            if not clobber and os.path.isfile(oimg):
                raise SaltIOError('%s alraedy exists' % oimg)

            #open the file
            struct = saltio.openfits(img)

            #log the message
            log.message('Updateing times in %s' % img,
                        with_header=False,
                        with_stdout=verbose)

            #now for each science frame, corrent the readtime
            #Assumes SALT format and that the first extension
            #is empty
            for i in range(1, len(struct)):
                try:
                    struct[i] = readtimefix(struct[i])
                except SaltIOError, e:
                    raise SaltError('%s %s' % (img, e))

            #Add history keywords
            # housekeeping keywords
            fname, hist = history(level=1,
                                  wrap=False,
                                  exclude=['images', 'outimages', 'outpref'])
            saltkey.housekeeping(struct[0], "SLOTREAD", 'READTIME added', hist)

            #write to the output
            saltio.writefits(struct, oimg, clobber)

        return
Esempio n. 45
0
def slotpreview(images,outfile,ampperccd=2,ignorexp=6,recenter_radius=5,
                tgt_col='b',cmp_col='g', tgt_lw=2,cmp_lw=2,cmap='gray',
                scale='zscale',contrast=0.1,clobber=True,logfile='salt.log',
                verbose=True):

    with logging(logfile,debug) as log:
        
        # is the input file specified?
        saltsafeio.filedefined('Input',images)

        # if the input file is a list, does it exist?
        if images[0] == '@':
            saltsafeio.listexists('Input',images)

        # parse list of input files
        imlist=saltsafeio.listparse('Raw image',images,'','','')

        # check input files exist
        saltsafeio.filesexist(imlist,'','r')

        # is the output file specified?
        saltsafeio.filedefined('Output',outfile)

        # check output file does not exist, optionally remove it if it does exist
        if os.path.exists(outfile) and clobber:
            os.remove(outfile)
        elif os.path.exists(outfile) and not clobber:
            raise SaltIOError('File '+outfile+' already exists, use clobber=y')

        # Get the number of ccds to calculate the number of frames to skip
        try:
            nccds=pyfits.getheader(imlist[0])['NCCDS']
        except:
            raise SaltIOError('Could not read NCCDS parameter from header of first fits file.')

        # Create GUI
        App = QtGui.QApplication(sys.argv)
        aw = ApplicationWindow(imlist=imlist,
                number=ignorexp*ampperccd*nccds+1,
                config=outfile, target_line_color=tgt_col,
                comparison_line_color=cmp_col, target_line_width=tgt_lw,
                comparison_line_width=cmp_lw, distance=recenter_radius,
                cmap=cmap, scale=scale, contrast=contrast)
        aw.show()

        # Start application event loop
        exit=App.exec_()

        # Check if GUI was executed succesfully
        if exit!=0:
            log.warning('Slotpreview GUI has unexpected exit status'+str(exit))
Esempio n. 46
0
def saltarith(operand1, op, operand2, result, outpref, divzero=0, clobber=False,     \
             logfile='salt.log',verbose=True):


   with logging(logfile,debug) as log:

       # Check the input images 
       infiles = saltio.argunpack ('Input',operand1)

       # create list of output files 
       outfiles=saltio.listparse('Outfile', result, outpref, infiles,'')

       #verify that the input and output lists are the same length
       saltio.comparelists(infiles,outfiles,'Input','output')

       #let's keep track of whether operand2 is an image or not
       is_image = False

       #load in operand2, or, if it's not an image, assume it's a number
       try: operand2struct = float(operand2)
       except ValueError:
          operand2struct = saltio.openfits(operand2)
          is_image = True

       #open the input image files
       for infile, outfile in zip(infiles,outfiles):
           struct = saltio.openfits(infile)

           #do some math!
           outstruct = arith(struct, op, operand2struct, is_image, divzero)
           try:
               pass 
           except Exception as e:
               msg='Unable to do math %s because %s' % (infile, e)
               raise SaltError(msg)

           #update header stuff
           fname, hist = history(level=1, wrap=False)
           saltkey.housekeeping(struct[0],'SARITH', 'Some arithmatic was performed',hist)

           #write it. close it.
           saltio.writefits(outstruct,outfile,clobber=clobber)
           saltio.closefits(struct)

           #output the information
           log.message('imarith: %s %s %s %s' % (infile, op, operand2, outfile), with_header=False, with_stdout=verbose)

       #close the operand2 image
       if is_image: saltio.closefits(operand2struct)
Esempio n. 47
0
def specsky(images,
            outimages,
            outpref,
            method='normal',
            section=None,
            function='polynomial',
            order=2,
            clobber=True,
            logfile='salt.log',
            verbose=True):

    with logging(logfile, debug) as log:

        # Check the input images
        infiles = saltio.argunpack('Input', images)

        # create list of output files
        outfiles = saltio.listparse('Outfile', outimages, outpref, infiles, '')

        if method not in ['normal', 'fit']:
            msg = '%s mode is not supported yet' % method
            raise SALTSpecError(msg)

        if section is None:
            section = saltio.getSection(section)
            msg = 'This mode is not supported yet'
            raise SALTSpecError(msg)
        else:
            section = saltio.getSection(section)

        # Identify the lines in each file
        for img, ofile in zip(infiles, outfiles):
            log.message('Subtracting sky spectrum in image %s into %s' %
                        (img, ofile))
            # open the images
            hdu = saltio.openfits(img)

            # sky subtract the array
            hdu = skysubtract(hdu,
                              method=method,
                              section=section,
                              funct=function,
                              order=order)

            # write out the image
            if clobber and os.path.isfile(ofile):
                saltio.delete(ofile)
            hdu.writeto(ofile)
Esempio n. 48
0
def saltsdbloadfits(images, sdbhost, sdbname, sdbuser, password, logfile, verbose):
    """Upload data about images (which is either a fits file, or specificied by @list)
       to the science data base, sdb

    """

    with logging(logfile,debug) as log:

       #open up the infiles
       infiles = saltio.argunpack('Input',images)

       sdb=saltmysql.connectdb(sdbhost,sdbname,sdbuser,password)


       for infile in infiles:
            sdbloadfits(infile, sdb, log, verbose)
Esempio n. 49
0
def saltfpcalring(images,outfile, waves, method=None, thresh=5, minsize=10, niter=3, conv=0.05,  
                  axc=None, ayc=None, 
                  clobber=False,logfile='salt.log',verbose=True):  
   """Fits rings in Fabry-Perot ring images"""
   with logging(logfile,debug) as log:

       # Check the input images 
       infiles = saltio.argunpack ('Input',images)

       # if the outfile exists and not clobber, fail
       saltio.overwrite(outfile,clobber) 

       #open the output file
       fout = saltio.openascii(outfile, 'w')

       #make sure that the list of waves is convertable to a numpy array
       #convert to floats in case the wave is given as a string
       if isinstance(waves, str):  
          waves=[float(x) for x in waves.split(',')]

       try: 
           waves=np.array(waves)
       except:
           raise SaltError('%s is not convertable to a numpy array' % waves)

       #check the value of method
       method=saltio.checkfornone(method)

       #setup the output file
       fout.write('#Comment\n')
       fout.write('# radius   err     xc      yc      z      ut     wave   dn  file\n')

       # open each image and detect the ring
       for img,w  in zip(infiles, waves):

          #open the image
          hdu=saltio.openfits(img)

          #measure the ring in each file
          xc, yc, radius, err, z, ut=make_calring(hdu, method=method, thresh=thresh, niter=niter, conv=conv, minsize=minsize, axc=axc, ayc=ayc)

          #output the results
          outstr=' %7.2f %6.2f %7.2f %7.2f %6.2f %7.4f %8.3f  0 %s\n' % (radius, err, xc, yc, z, ut, w, img)
          fout.write(outstr)
          log.message(outstr.strip(), with_stdout=verbose, with_header=False)

       fout.close()
Esempio n. 50
0
def saltmosaic(images,outimages,outpref,geomfile,interp='linear',geotran=True, cleanup=True,clobber=False,logfile=None,verbose=True):

   #Start the logging
   with logging(logfile,debug) as log:

       # Check the input images 
       infiles = saltio.argunpack ('Input',images)

       # create list of output files 
       outfiles=saltio.listparse('Outfile', outimages, outpref,infiles,'')

       #verify that the input and output lists are the same length
       saltio.comparelists(infiles,outfiles,'Input','output')



       # does CCD geometry definition file exist
       geomfilefile = geomfile.strip()
       saltio.fileexists(geomfile)

       gap = 0
       xshift = [0, 0]
       yshift = [0, 0]
       rotation = [0, 0]
       gap, xshift, yshift, rotation=saltio.readccdgeom(geomfile)

       # open each raw image file and apply the transformation to it
       for img, oimg in zip(infiles, outfiles):

           #open the structure
           struct = saltio.openfits(img)

           #create the mosaic
           ostruct=make_mosaic(struct, gap, xshift, yshift, rotation, interp_type=interp, geotran=geotran, cleanup=cleanup, log=log, verbose=verbose)

           #update the header information
           # housekeeping keywords
           fname, hist=history(level=1, wrap=False, exclude=['images', 'outimages', 'outpref'])
           saltkey.housekeeping(ostruct[0], 'SMOSAIC', 'Images have been mosaicked ', hist)

           #write the image out
           saltio.writefits(ostruct,oimg, clobber=clobber)
 
           #close the files
           saltio.closefits(struct)
           saltio.closefits(ostruct)
Esempio n. 51
0
def saltgain(images,outimages, outpref, gaindb=None,usedb=False, mult=True,
             clobber=True, logfile='salt.log',verbose=True):

   #start logging
   with logging(logfile,debug) as log:

       # Check the input images 
       infiles = saltio.argunpack ('Input',images)

       # create list of output files 
       outfiles=saltio.listparse('Outfile', outimages, outpref,infiles,'')

       #verify that the input and output lists are the same length
       saltio.comparelists(infiles,outfiles,'Input','output')

       # read in the database file if usedb is true
       if usedb:
           gaindb = gaindb.strip()
           dblist= saltio.readgaindb(gaindb)
       else:
           dblist=[]


       for img, oimg in zip(infiles, outfiles):
           #open the fits file
           struct=saltio.openfits(img)

           # identify instrument
           instrume,keyprep,keygain,keybias,keyxtalk,keyslot = saltkey.instrumid(struct)

           # has file been prepared already?
           if saltkey.found(keygain, struct[0]):
               message='SALTGAIN: %s has already been gain-corrected' % img
               raise SaltError(message)


           # gain correct the data
           struct = gain(struct,mult=mult, usedb=usedb, dblist=dblist, log=log, verbose=verbose)

           # housekeeping keywords
           fname, hist=history(level=1, wrap=False, exclude=['images', 'outimages', 'outpref'])
           saltkey.housekeeping(struct[0],keygain, 'Images have been gain corrected', hist)

           # write FITS file
           saltio.writefits(struct,oimg, clobber=clobber)
           saltio.closefits(struct)
Esempio n. 52
0
def saltfpmask(images, outimages, outpref, axc,ayc, arad, maskmethod='c', maskvalue=0, \
               radi=None,rado=None,clobber=False, logfile='saltfp.log', verbose=True):
    """Aperture masks Fabry-Perot images"""

    with logging(logfile, debug) as log:
        # Check the input images
        infiles = saltio.argunpack('Input', images)

        # create list of output files
        outfiles = saltio.listparse('Outfile', outimages, outpref, infiles, '')

        #verify that the input and output lists are the same length
        saltio.comparelists(infiles, outfiles, 'Input', 'output')

        for img, oimg in zip(infiles, outfiles):
            #open the image
            hdu = saltio.openfits(img)

            #test that the data are in the first
            try:
                data = hdu[0].data
            except Exception, e:
                message = 'SALTFPMASK--ERROR:  Could not access data in Primary exention of %s because %s' % (
                    img, e)
                log.message(message)

            #determine the mask value if it is a region
            if maskmethod == 'region':
                maskvalue = calc_maskvalue(data, axc, ayc, radi, rado)

            #mask the image
            message = "SALTFPMASK--Masking image %s with a value of %s" % (
                img, maskvalue)
            log.message(message, with_header=False)
            hdu[0].data = maskimage(data, axc, ayc, arad, maskvalue)
            try:
                pass
            except Exception, e:
                message = 'SALTFPMASK--ERROR:  Could not create mask for %s because %s' % (
                    img, e)
                log.message(message)

            #write out the image
            saltio.writefits(hdu, oimg, clobber=clobber)
Esempio n. 53
0
def speccal(specfile, outfile, calfile, extfile, airmass=None, exptime=None,
            clobber=True, logfile='salt.log', verbose=True):

    with logging(logfile, debug) as log:

        # read in the specfile and create a spectrum object
        obs_spectra = st.readspectrum(specfile, error=True, ftype='ascii')

        # read in the std file and convert from magnitudes to fnu
        # then convert it to fwave (ergs/s/cm2/A)
        cal_spectra = st.readspectrum(calfile, error=False, ftype='ascii')

        # read in the extinction file (leave in magnitudes)
        ext_spectra = st.readspectrum(extfile, error=False, ftype='ascii')

        # determine the airmass if not specified
        if saltio.checkfornone(airmass) is None:
            message = 'Airmass was not supplied'
            raise SALTSpecError(message)

        # determine the exptime if not specified
        if saltio.checkfornone(airmass) is None:
            message = 'Exposure Time was not supplied'
            raise SALTSpecError(message)

        # calculate the calibrated spectra
        log.message('Calculating the calibration curve for %s' % specfile)
        error = False
        try:
            if obs_spectra.var is not None:
                error = True
        except:
            error = False
        flux_spectra = calfunc(
            obs_spectra,
            cal_spectra,
            ext_spectra,
            airmass,
            exptime,
            error)

        # write the spectra out
        st.writespectrum(flux_spectra, outfile, ftype='ascii', error=error)
Esempio n. 54
0
def saltsurface(images,
                outimages,
                outpref,
                mask=True,
                order=3,
                minlevel=0,
                clobber=False,
                logfile='salt.log',
                verbose=True):

    with logging(logfile, debug) as log:

        # Check the input images
        infiles = saltio.argunpack('Input', images)

        # create list of output files
        outfiles = saltio.listparse('Outfile', outimages, outpref, infiles, '')

        #verify that the input and output lists are the same length
        saltio.comparelists(infiles, outfiles, 'Input', 'output')

        # open each raw image file
        for infile, outfile in zip(infiles, outfiles):
            struct = saltio.openfits(infile)

            struct = surface_fit(struct,
                                 order=order,
                                 mask=mask,
                                 minlevel=minlevel)

            #add any header keywords like history
            fname, hist = history(level=1, wrap=False)
            saltkey.housekeeping(struct[0], 'SURFIT', 'File fit by a surface',
                                 hist)

            #write it out and close it
            saltio.writefits(struct, outfile, clobber=clobber)
            saltio.closefits(struct)

            #output the information
            log.message('Surface fitted image %s ' % (infile),
                        with_header=False,
                        with_stdout=verbose)
Esempio n. 55
0
def saltfpzeropoint(images,outimages, outpref, calfile, fpa, fpb, fpc, fpd, fpe, fpf, clobber=False,logfile='salt.log',verbose=True):  
   """Adds zeropoint information to each image"""

   with logging(logfile,debug) as log:

       # Check the input images 
       infiles = saltio.argunpack ('Input',images)

       # create the output files
       outfiles=saltio.listparse('Outfile', outimages, outpref,infiles,'')

       #verify that the input and output lists are the same length
       saltio.comparelists(infiles,outfiles,'Input','output')


       #calculate the zeropoint coefficients
       fpcoef=np.array([fpa,fpb,fpc,fpd,fpe,fpf])
       zpcoef,tstart=calc_zpcoef(calfile, fpcoef)

       # open each image and detect the ring
       for img, oimg  in zip(infiles, outfiles):
      
          hdu=saltio.openfits(img)

          #get the image time
          t=(get_datetime(hdu)-tstart).seconds

          #add the header values to the image
          saltkey.new('FPA',fpa+zpcoef[1]+zpcoef[0]*t,'FPA Coef',hdu[0])
          saltkey.new('FPB',fpb,'FPB Coef',hdu[0])
          saltkey.new('FPC',fpc,'FPC Coef',hdu[0])
          saltkey.new('FPD',fpd,'FPD Coef',hdu[0])
          saltkey.new('FPE',  0,'FPE Coef',hdu[0])
          saltkey.new('FPF',fpf,'FPF Coef',hdu[0])

          #write the file out
          saltio.writefits(hdu, oimg, clobber)

          #log the action
          msg='Updating the FP information in %s' % (img)
          log.message(msg, with_stdout=verbose, with_header=False)
Esempio n. 56
0
def saltfpprep(images, outimages, outpref, 
               clobber=True, logfile='saltfp.log', verbose=True):  
    """Prepares data for Fabry-Perot reductions"""

    with logging(logfile, debug) as log:
       # Check the input images 
       infiles = saltio.argunpack ('Input',images)

       # create list of output files 
       outfiles=saltio.listparse('Outfile', outimages, outpref,infiles,'')

       #verify that the input and output lists are the same length
       saltio.comparelists(infiles,outfiles,'Input','output')

       for img, oimg in zip(infiles, outfiles):
           message="SALTFPPREP--Preparing image %s" % (img)
           log.message(message, with_header=False)
           try:
               convertsalt(img, oimg, clobber=clobber)
           except SaltError, e:
               log.message('%s' %e)
Esempio n. 57
0
def specprepare(images, outimages, outpref, badpixelimage='',
                clobber=True, logfile='salt.log', verbose=True):

    with logging(logfile, debug) as log:

        # Check the input images
        infiles = saltio.argunpack('Input', images)

        # create list of output files
        outfiles = saltio.listparse('Outfile', outimages, outpref, infiles, '')

        # verify that the input and output lists are the same length
        saltio.comparelists(infiles, outfiles, 'Input', 'output')

        # open the badpixel image
        if saltio.checkfornone(badpixelimage) is None:
            badpixelstruct = None
        else:
            try:
                badpixelstruct = saltio.openfits(badpixelimage)
            except saltio.SaltIOError as e:
                msg = 'badpixel image must be specificied\n %s' % e
                raise SALTSpecError(msg)

        # open each raw image file

        for img, oimg, in zip(infiles, outfiles):

            # open the fits file
            struct = saltio.openfits(img)

            # prepare file
            struct = prepare(struct, badpixelstruct)

            # write FITS file
            saltio.writefits(struct, oimg, clobber=clobber)
            saltio.closefits(struct)

            message = 'SPECPREPARE -- %s => %s' % (img, oimg)
            log.message(message)
Esempio n. 58
0
def saltembed(images,outimages,outpref, clobber=True,logfile='salt.log',verbose=True): 

   with logging(logfile,debug) as log:

       # Check the input images 
       infiles = saltio.argunpack ('Input',images)

       # create list of output files 
       outfiles=saltio.listparse('Outfile', outimages, outpref,infiles,'')

       #verify that the input and output lists are the same length
       saltio.comparelists(infiles,outfiles,'Input','output')

       # open each raw image file
       for img, oimg, in zip(infiles, outfiles):

           #open the fits file
           struct=saltio.openfits(img)

           #get the number of CCDs
           nccd=saltkey.get('NCCDS', struct[0])

           #set the number of amps per CCD--*TODO* Read from header
           namps=2

           #get the number of windows--try to read from header
           try:
              nwindows=saltkey.get('NWINDOWS', struct[0])
           except:
              nwindows=len(struct)/(nccd*namps)
 
           outstruct=embedimage(struct, nccd, namps, nwindows)

           saltio.writefits(outstruct, oimg, clobber=clobber)

           message = 'SALTEMBED -- %s => %s' % (img, oimg)
           log.message(message, with_header=False)
Esempio n. 59
0
def saltclean(images,
              outpath,
              obslogfile=None,
              gaindb=None,
              xtalkfile=None,
              geomfile=None,
              subover=True,
              trim=True,
              masbias=None,
              subbias=False,
              median=False,
              function='polynomial',
              order=5,
              rej_lo=3,
              rej_hi=3,
              niter=5,
              interp='linear',
              clobber=False,
              logfile='salt.log',
              verbose=True):
    """SALTCLEAN will provide basic CCD reductions for a set of data.  It will 
      sort the data, and first process the biases, flats, and then the science 
      frames.  It will record basic quality control information about each of 
      the steps.
   """
    plotover = False

    #start logging
    with logging(logfile, debug) as log:

        # Check the input images
        infiles = saltio.argunpack('Input', images)

        # create list of output files
        outpath = saltio.abspath(outpath)

        #does the gain database file exist
        if gaindb:
            dblist = saltio.readgaindb(gaindb)
        else:
            dblist = []

        # does crosstalk coefficient data exist
        if xtalkfile:
            xtalkfile = xtalkfile.strip()
            xdict = saltio.readxtalkcoeff(xtalkfile)
        else:
            xdict = None
        #does the mosaic file exist--raise error if no
        saltio.fileexists(geomfile)

        # Delete the obslog file if it already exists
        if os.path.isfile(obslogfile) and clobber: saltio.delete(obslogfile)

        #read in the obsveration log or create it
        if os.path.isfile(obslogfile):
            msg = 'The observing log already exists.  Please either delete it or run saltclean with clobber=yes'
            raise SaltError(msg)
        else:
            headerDict = obslog(infiles, log)
            obsstruct = createobslogfits(headerDict)
            saltio.writefits(obsstruct, obslogfile)

        #create the list of bias frames and process them
        filename = obsstruct.data.field('FILENAME')
        detmode = obsstruct.data.field('DETMODE')
        ccdtype = obsstruct.data.field('CCDTYPE')

        #set the bias list of objects
        biaslist = filename[ccdtype == 'ZERO']
        masterbias_dict = {}
        for img in infiles:
            if os.path.basename(img) in biaslist:
                #open the image
                struct = fits.open(img)
                bimg = outpath + 'bxgp' + os.path.basename(img)

                #print the message
                if log:
                    message = 'Processing Zero frame %s' % img
                    log.message(message, with_stdout=verbose)

                #process the image
                struct = clean(struct,
                               createvar=False,
                               badpixelstruct=None,
                               mult=True,
                               dblist=dblist,
                               xdict=xdict,
                               subover=subover,
                               trim=trim,
                               subbias=False,
                               bstruct=None,
                               median=median,
                               function=function,
                               order=order,
                               rej_lo=rej_lo,
                               rej_hi=rej_hi,
                               niter=niter,
                               plotover=plotover,
                               log=log,
                               verbose=verbose)

                #write the file out
                # housekeeping keywords
                fname, hist = history(
                    level=1,
                    wrap=False,
                    exclude=['images', 'outimages', 'outpref'])
                saltkey.housekeeping(struct[0], 'SPREPARE',
                                     'Images have been prepared', hist)
                saltkey.new('SGAIN', time.asctime(time.localtime()),
                            'Images have been gain corrected', struct[0])
                saltkey.new('SXTALK', time.asctime(time.localtime()),
                            'Images have been xtalk corrected', struct[0])
                saltkey.new('SBIAS', time.asctime(time.localtime()),
                            'Images have been de-biased', struct[0])

                # write FITS file
                saltio.writefits(struct, bimg, clobber=clobber)
                saltio.closefits(struct)

                #add files to the master bias list
                masterbias_dict = compareimages(struct,
                                                bimg,
                                                masterbias_dict,
                                                keylist=biasheader_list)

        #create the master bias frame
        for i in masterbias_dict.keys():
            bkeys = masterbias_dict[i][0]
            blist = masterbias_dict[i][1:]
            mbiasname = outpath + createmasterbiasname(blist, bkeys)
            bfiles = ','.join(blist)
            saltcombine(bfiles, mbiasname, method='median', reject='sigclip', mask=False,
                        weight=False, blank=0, scale=None, statsec=None, lthresh=3,    \
                        hthresh=3, clobber=False, logfile=logfile,verbose=verbose)

        #create the list of flatfields and process them
        flatlist = filename[ccdtype == 'FLAT']
        masterflat_dict = {}
        for img in infiles:
            if os.path.basename(img) in flatlist:
                #open the image
                struct = fits.open(img)
                fimg = outpath + 'bxgp' + os.path.basename(img)

                #print the message
                if log:
                    message = 'Processing Flat frame %s' % img
                    log.message(message, with_stdout=verbose)

                #process the image
                struct = clean(struct,
                               createvar=False,
                               badpixelstruct=None,
                               mult=True,
                               dblist=dblist,
                               xdict=xdict,
                               subover=subover,
                               trim=trim,
                               subbias=False,
                               bstruct=None,
                               median=median,
                               function=function,
                               order=order,
                               rej_lo=rej_lo,
                               rej_hi=rej_hi,
                               niter=niter,
                               plotover=plotover,
                               log=log,
                               verbose=verbose)

                #write the file out
                # housekeeping keywords
                fname, hist = history(
                    level=1,
                    wrap=False,
                    exclude=['images', 'outimages', 'outpref'])
                saltkey.housekeeping(struct[0], 'SPREPARE',
                                     'Images have been prepared', hist)
                saltkey.new('SGAIN', time.asctime(time.localtime()),
                            'Images have been gain corrected', struct[0])
                saltkey.new('SXTALK', time.asctime(time.localtime()),
                            'Images have been xtalk corrected', struct[0])
                saltkey.new('SBIAS', time.asctime(time.localtime()),
                            'Images have been de-biased', struct[0])

                # write FITS file
                saltio.writefits(struct, fimg, clobber=clobber)
                saltio.closefits(struct)

                #add files to the master bias list
                masterflat_dict = compareimages(struct,
                                                fimg,
                                                masterflat_dict,
                                                keylist=flatheader_list)

        #create the master flat frame
        for i in masterflat_dict.keys():
            fkeys = masterflat_dict[i][0]
            flist = masterflat_dict[i][1:]
            mflatname = outpath + createmasterflatname(flist, fkeys)
            ffiles = ','.join(flist)
            saltcombine(ffiles, mflatname, method='median', reject='sigclip', mask=False,
                        weight=False, blank=0, scale=None, statsec=None, lthresh=3,    \
                        hthresh=3, clobber=False, logfile=logfile,verbose=verbose)

        #process the science data
        for img in infiles:
            nimg = os.path.basename(img)
            #print nimg, nimg in flatlist, nimg in biaslist
            if not (nimg in biaslist):
                #open the image
                struct = fits.open(img)
                simg = outpath + 'bxgp' + os.path.basename(img)

                #print the message
                if log:
                    message = 'Processing science frame %s' % img
                    log.message(message, with_stdout=verbose)

                #process the image
                struct = clean(struct,
                               createvar=False,
                               badpixelstruct=None,
                               mult=True,
                               dblist=dblist,
                               xdict=xdict,
                               subover=subover,
                               trim=trim,
                               subbias=False,
                               bstruct=None,
                               median=median,
                               function=function,
                               order=order,
                               rej_lo=rej_lo,
                               rej_hi=rej_hi,
                               niter=niter,
                               plotover=plotover,
                               log=log,
                               verbose=verbose)

                #write the file out
                # housekeeping keywords
                fname, hist = history(
                    level=1,
                    wrap=False,
                    exclude=['images', 'outimages', 'outpref'])
                saltkey.housekeeping(struct[0], 'SPREPARE',
                                     'Images have been prepared', hist)
                saltkey.new('SGAIN', time.asctime(time.localtime()),
                            'Images have been gain corrected', struct[0])
                saltkey.new('SXTALK', time.asctime(time.localtime()),
                            'Images have been xtalk corrected', struct[0])
                saltkey.new('SBIAS', time.asctime(time.localtime()),
                            'Images have been de-biased', struct[0])

                # write FITS file
                saltio.writefits(struct, simg, clobber=clobber)
                saltio.closefits(struct)

                #mosaic the files--currently not in the proper format--will update when it is
                if not saltkey.fastmode(saltkey.get('DETMODE', struct[0])):
                    mimg = outpath + 'mbxgp' + os.path.basename(img)
                    saltmosaic(images=simg,
                               outimages=mimg,
                               outpref='',
                               geomfile=geomfile,
                               interp=interp,
                               cleanup=True,
                               clobber=clobber,
                               logfile=logfile,
                               verbose=verbose)

                    #remove the intermediate steps
                    saltio.delete(simg)
Esempio n. 60
0
def specarcstraighten(images, outfile, function='poly', order=3, rstep=1,
                      rstart='middlerow', nrows=1, 
                      y1=None, y2=None, sigma=5, sections=3, niter=5,
                      startext=0, clobber=False, logfile='salt.log', verbose=True):

    with logging(logfile, debug) as log:

        # Check the input images
        infiles = saltio.argunpack('Input', images)

        # create list of output files
        outfiles = saltio.argunpack('Output', outfile)

        # Identify the lines in each file
        for img, ofile in zip(infiles, outfiles):

            # open the image
            hdu = saltio.openfits(img)

            # get the basic information about the spectrograph
            dateobs = saltkey.get('DATE-OBS', hdu[0])
            try:
                utctime = saltkey.get('UTC-OBS', hdu[0])
            except SaltError:
                utctime = saltkey.get('TIME-OBS', hdu[0])

            instrume = saltkey.get('INSTRUME', hdu[0]).strip()
            grating = saltkey.get('GRATING', hdu[0]).strip()
            grang = saltkey.get('GR-ANGLE', hdu[0])
            grasteps = saltkey.get('GRTILT', hdu[0])
            arang = saltkey.get('AR-ANGLE', hdu[0])
            arsteps = saltkey.get('CAMANG', hdu[0])
            rssfilter = saltkey.get('FILTER', hdu[0])
            specmode = saltkey.get('OBSMODE', hdu[0])
            masktype = saltkey.get('MASKTYP', hdu[0]).strip().upper()
            slitname = saltkey.get('MASKID', hdu[0])
            xbin, ybin = saltkey.ccdbin(hdu[0], img)

            for i in range(startext, len(hdu)):
                if hdu[i].name == 'SCI':
                    log.message('Proccessing extension %i in  %s' % (i, img))
                    # things that will change for each slit

                    if masktype == 'LONGSLIT':
                        slit = st.getslitsize(slitname)
                        objid = None
                    #elif masktype == 'MOS':
                        #slit = 1.5
                        # slit=saltkey.get('SLIT', hdu[i])

                        # set up the x and y positions
                        #miny = hdu[i].header['MINY']
                        #maxy = hdu[i].header['MAXY']
                        #ras = hdu[i].header['SLIT_RA']
                        #des = hdu[i].header['SLIT_DEC']
                        #objid = hdu[i].header['SLITNAME']

                        # Check the perfomance of masks at different PA
                        #rac = hdu[0].header['MASK_RA']
                        #dec = hdu[0].header['MASK_DEC']
                        #pac = hdu[0].header['PA']

                    else:
                        msg = '%s is not a currently supported masktype' % masktype
                        raise SALTSpecError(msg)

                    if instrume not in ['PFIS', 'RSS']:
                        msg = '%s is not a currently supported instrument' % instrume
                        raise SALTSpecError(msg)

                    # set up the data for the source
                    try:
                        data = hdu[i].data
                    except Exception as e:
                        message = 'Unable to read in data array in %s because %s' % (
                            img, e)
                        raise SALTSpecError(message)

                    # set up the center row
                    if rstart == 'middlerow':
                        ystart = int(0.5 * len(data))
                    else:
                        ystart = rstart


                    # set up the xarr array based on the image
                    xarr = np.arange(len(data[ystart]), dtype='int64')

                    # calculate the transformation
                    ImageSolution = arcstraight(data, xarr, ystart, function=function, order=order, 
                                                rstep=rstep, y1=y1, y2=y2, sigma=sigma, sections=sections,
                                                niter=niter, log=log, verbose=verbose)

                    if outfile and len(ImageSolution):
                        writeIS(ImageSolution, outfile, dateobs=dateobs, utctime=utctime, instrume=instrume,
                                grating=grating, grang=grang, grasteps=grasteps, arsteps=arsteps,
                                arang=arang, rfilter=rssfilter, slit=slit, xbin=xbin,
                                ybin=ybin, objid=objid,
                                filename=img, log=log, verbose=verbose)