Exemple #1
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()
Exemple #2
0
def saltquery(selection, logic, startdate, enddate, outfile=None, sdbhost='sdb.saao', sdbname='sdb', \
              sdbuser='', password='', clobber=False, logfile='saltlog.log', verbose=True):
    """Query the salt database for FITS files

    """

    with logging(logfile,debug) as log:

       #check the outfiles
       if not saltio.checkfornone(outfile):
          outfile=None
          
       #check that the output file can be deleted
       if outfile:
           saltio.overwrite(outfile, clobber)

       #open outfile
       if outfile:
          fout=saltio.openascii(outfile, 'w')


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

       #Create a list of the selection and then unpack it
       selection_list = saltio.argunpack('Selection', selection)
       selection=','.join(selection_list)

       #write out the header for the outfile
       outstr='#'+' '.join(['%s' % x for x in selection_list]) 
       if outfile:
          fout.write(outstr+'\n')
       else:
          print outstr


       #set up the table
       rsstable=''' FileData 
  left join FitsHeaderImage using (FileData_Id) 
  inner join FitsHeaderRss using (FileData_Id) 
'''
       #set up the table
       scamtable=''' FileData 
  left join FitsHeaderImage using (FileData_Id) 
  inner join FitsHeaderSalticam using (FileData_Id) 
'''

       #set up the logic
       logic=makelogic(logic, startdate, enddate)

       for tab in [rsstable, scamtable]:

          msg='''
Mysql querying data is:

  SELECT %s
  FROM   %s
  WHERE  %s
'''  % (selection, tab, logic)
          log.message(msg, with_stdout=verbose)


          record=saltmysql.select(sdb, selection, tab, logic)

          print record
  
          for r in record:
             outstr=' '.join(['%s' % x for x in r]) 
             if outfile:
                 fout.write(outstr+'\n')
             else:
                 print outstr

       #close outfile
       if outfile: fout.close()
Exemple #3
0
def saltcharge(
    obsdate,
    outfile,
    sdbhost="sdb.saao",
    sdbname="sdb",
    sdbuser="",
    password="",
    clobber=False,
    logfile="saltlog.log",
    verbose=True,
):
    """Calculate the charged time for proposals observed during a night

    """

    with logging(logfile, debug) as log:

        # check the outfiles
        if not saltio.checkfornone(outfile):
            outfile = None

        # check that the output file can be deleted
        if outfile:
            saltio.overwrite(outfile, clobber)

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

        # get all the proposals observed during the night
        select_state = "distinct Proposal_Code"
        table_state = "FileData Join ProposalCode using (ProposalCode_Id)"
        logic_state = "FileName like '%" + obsdate + "%'"

        records = saltmysql.select(sdb, select_state, table_state, logic_state)
        pids = []
        pid_time = {}
        for r in records:
            pids.append(r[0])
            pid_time[r[0]] = 0
        if len(pids) == 0:
            message = "There are no proposal to charge time for %s" % obsdate
            log.message(message)
            return

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

        # get a list of all the images taken during the night
        select_state = "FileName, Proposal_Code, Target_Name, ExposureTime, UTSTART, INSTRUME, OBSMODE, DETMODE, CCDTYPE, NExposures"
        table_state = "FileData Join ProposalCode using (ProposalCode_Id) join FitsHeaderImage using (FileData_Id)"
        logic_state = "FileName like '%" + obsdate + "%'"
        img_list = saltmysql.select(sdb, select_state, table_state, logic_state)

        # get all the blocks visited
        select_state = "Block_Id, Accepted, Proposal_Code"
        table_state = "Block join BlockVisit using (Block_Id) join Proposal using (Proposal_Id) join ProposalCode using (ProposalCode_Id)"
        logic_state = "NightInfo_Id=%i" % night_id
        block_list = saltmysql.select(sdb, select_state, table_state, logic_state)
        print block_list

        # get the start and end of twilight
        nightstart = saltmysql.select(sdb, "EveningTwilightEnd", "NightInfo", "NightInfo_Id=%i" % night_id)[0][0]
        nightend = saltmysql.select(sdb, "MorningTwilightStart", "NightInfo", "NightInfo_Id=%i" % night_id)[0][0]
        print nightstart, nightend, (nightend - nightstart).seconds
        print

        # download the SOMMI log from the night
        try:
            sommi_log = saltmysql.select(sdb, "SONightLog", "NightLogs", "NightInfo_Id=%i" % night_id)[0][0]
        except Exception, e:
            msg = "Unable to read in SOMMI log for %s" % obsdate
            raise SaltError(msg)

        # add to account for error in SOMMI log
        if int(obsdate) < 20111027:
            sommi_log = sommi_log.replace("Object name", "\nObject name")

        # parse the sommi log
        point_list = parseforpointings(sommi_log)

        # now find all blocks observed during a night
        for point in point_list:
            # proposal for that block
            pid = point[0].strip()

            # start time for that block
            starttime = point[1]

            # find the end time for that block
            endtime = findnexttime(point[1], point_list, nightend)

            # find the start time of the first object to be observed
            # for this block
            startimage = findfirstimage(pid, starttime, img_list)
            lastimage = findlastimage(pid, endtime, img_list)

            # check to see if the end time for the last file observed for
            # this block
            if startimage is not None:
                if startimage > endtime:
                    startimage = None

            # determine the acquisition time for the block
            if startimage is not None:
                acqdelta = (startimage - starttime).seconds
            else:
                acqdelta = -1

            # find the shutter open time
            shuttertime = calculate_shutteropen(img_list, starttime, endtime)

            # if the shutter and time of the last image is substantially different from the
            # end of the block, then take the last image as the end of the block
            # *TODO* Change to use expected block time
            st = starttime + datetime.timedelta(0, shuttertime + acqdelta)
            if (endtime - st).seconds > 900:
                if lastimage is None:
                    endtime = st
                elif (lastimage - st).seconds > 3600:
                    endtime = st
                else:
                    endtime = lastimage
            # account for the time for that block
            tblock = (endtime - starttime).seconds

            if acqdelta > -1:
                # find the associated block
                block_id, blockvisit_id, accepted = getblockid(sdb, night_id, pid, img_list, starttime, endtime)

                if accepted and pid.count("-3-"):
                    # charged time for that block
                    try:
                        pid_time[pid] += tblock
                    except KeyError:
                        pid_time[pid] = tblock

                    print block_id, blockvisit_id, pid, starttime, tblock, acqdelta, shuttertime, shuttertime / tblock, block_id, accepted
                # update the charge time
                slewdelta = 0
                scidelta = tblock - acqdelta
                update_cmd = "TotalSlewTime=%i, TotalAcquisitionTime=%i, TotalScienceTime=%i" % (
                    slewdelta,
                    acqdelta,
                    scidelta,
                )
                table_cmd = "BlockVisit"
                logic_cmd = "BlockVisit_Id=%i" % blockvisit_id
                saltmysql.update(sdb, update_cmd, table_cmd, logic_cmd)

        return

        # update the charged time information
        # TODO: Check to see if the block was approved
        ptime_tot = 0
        stime_tot = 0
        for k in pid_time:
            ptime = pid_time[k]
            if ptime > 0:
                obsratio = stime / ptime
            else:
                obsratio = 0
            print "%25s %5i %5i %3.2f" % (k, ptime, stime, obsratio)
            if k.count("-3-"):
                ptime_tot += ptime
                stime_tot += stime

        # print out the total night statistics
        tdelta = nightend - nightstart
        print tdelta.seconds, ptime_tot, stime_tot
def saltcalibrations(
    propcode,
    outfile=None,
    sdbhost="sdb.saao",
    sdbname="sdb",
    sdbuser="",
    password="",
    clobber=False,
    logfile="saltlog.log",
    verbose=True,
):
    """Seach the salt database for FITS files

    """

    with logging(logfile, debug) as log:

        # check the outfiles
        if not saltio.checkfornone(outfile):
            outfile = None

        # check that the output file can be deleted
        if outfile:
            saltio.overwrite(outfile, clobber)
            fout = open(oufile, "w")

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

        # determine the associated
        username = saltmysql.getpiptusername(sdb, propcode)
        userdir = "/salt/ftparea/%s/spst" % username
        if not os.path.exists(userdir):
            saltio.createdir(userdir)

        log.message("Will copy data to %s" % userdir)

        # Find all the data assocated with a proposal
        cmd_select = "d.FileName,d.FileData_Id,  CCDTYPE, d.DETMODE, d.OBSMODE, CCDSUM, GAINSET, ROSPEED, FILTER, GRATING, GRTILT, CAMANG, MASKID"
        cmd_table = """ FileData as d 
  left join FitsHeaderImage using (FileData_Id) 
  left join FitsHeaderRss using (FileData_Id) 
  left join ProposalCode using (ProposalCode_Id)
"""
        cmd_logic = 'Proposal_Code="%s" and CCDTYPE="OBJECT" and d.OBSMODE="SPECTROSCOPY"' % (propcode)

        record = saltmysql.select(sdb, cmd_select, cmd_table, cmd_logic)

        # loop through all the results and return only the Set of identical results
        caldict = create_caldict(record)

        # prepare for writing out the results
        outstr = ""
        if outfile:
            fout.write(outstr + "\n")
        else:
            print outstr

        # now find all the cal_spst that have the same settings
        cmd_select = "d.FileName,d.FileData_Id,  CCDTYPE, d.DETMODE, d.OBSMODE, CCDSUM, GAINSET, ROSPEED, FILTER, GRATING, GRTILT, CAMANG, MASKID"
        cmd_table = """ FileData as d
  left join FitsHeaderImage using (FileData_Id) 
  left join FitsHeaderRss using (FileData_Id) 
  left join ProposalCode using (ProposalCode_Id)
"""
        for r in caldict:
            cmd_logic = "CCDSUM='%s' and GRATING='%s' and GRTILT='%s' and CAMANG='%s' and Proposal_Code='CAL_SPST'" % (
                caldict[r][3],
                caldict[r][7],
                caldict[r][8],
                caldict[r][9],
            )
            # cmd_logic="CCDSUM='%s' and GRATING='%s' and AR_STA='%s' " % (caldict[r][3], caldict[r][7], caldict[r][9])
            log.message(cmd_logic, with_header=False)
            record = saltmysql.select(sdb, cmd_select, cmd_table, cmd_logic)
            # print record

            # write out hte results
            for r in record:
                outstr = " ".join(["%s" % x for x in r])
                if outfile:
                    fout.write(outstr + "\n")
                else:
                    log.message(outstr, with_header=False)

                # copy to the user directory
                cfile = makefilename(r[0], state="product")
                shutil.copy(cfile, userdir)
                cfile = makefilename(r[0], state="raw")
                shutil.copy(cfile, userdir)

        # close outfile
        if outfile:
            fout.close()