Exemplo n.º 1
0
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()
Exemplo n.º 2
0
def saltfast(obsdate, readme, emailserver, username,password, bcc, sdbhost,
             sdbname,sdbuser, clobber,logfile,verbose):

   # set up
   nightlog = ''

 
   with logging(logfile,debug) as log:
       # determine current directory
       workdir = os.getcwd()
       logfile = workdir + '/' + os.path.basename(logfile)


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

       #query the data base for all of the data taken last night and the proposal codes
       select='FileName, Target_Name, Proposal_Code'
       table='FileData join ProposalCode using (ProposalCode_Id)'
       logic="FileName like '%"+obsdate+"%'"
       records=saltmysql.select(sdb, select, table, logic)

       if len(records)==0:
          message='No data taken on %s\n' % obsdate
          log.message(message)
          return 

       #determine the list of files, targets, and propcodes
       file_list=[]
       target_list=[]
       propcode_list=[]
       propcode_dict={}
       for name,targ,pid in records: 
           file_list.append(name)
           target_list.append(targ)
           propcode_list.append(pid)
           try:
               propcode_dict[pid].append(targ)
           except KeyError:
               propcode_dict[pid]=[targ]


       # check to see if any of the PI directories requires fast response
       for pid in propcode_dict.keys():
            target=set(propcode_dict[pid])
            if checkforfast(pid, target, sdb):
                #log the move
                message='Copying  data from %s to the ftp site' % pid
                log.message(message, with_stdout=verbose)

                #get the username
                piptuser=saltmysql.getpiptusername(sdb, pid) 
                #create the fast directory
                fastdir='/salt/ftparea/%s/FAST/' % piptuser

                if os.path.isdir(fastdir):   saltio.deletedir(fastdir)
                os.mkdir(fastdir)
 
                for i in range(len(file_list)):
                    if propcode_list[i]==pid:
                       #create the data filename
                       if file_list[i].startswith('S'):
                          instr='scam'
                       elif file_list[i].startswith('P'):
                          instr='rss'
                       filepath='/salt/%s/data/%s/%s/raw/%s' % (instr, obsdate[0:4], obsdate[4:8], file_list[i])
                       saltio.copy(filepath, fastdir)
                           
                #make the temporary readme
                mailmessage = maketempreadmefast(pid, sdb, readme)

                #send the email
                subject='SALT Raw Data available for %s' % pid
                sender='*****@*****.**'
                recipient=saltmysql.getpiptemail(sdb, piptuser)
                bcc='*****@*****.**'#[email protected]'