Example #1
0
def saltbin2fit(inpath,outpath,cleanup,fitsconfig,logfile,verbose,status=0):

    status = 0
    output = {}
    headlist18 = []
    headlist19 = []

# test the logfile

    logfile = saltio.logname(logfile)

# log the call

    saltprint.line(logfile,verbose)
    message = 'SALTBIN2FIT -- '
    message += 'inpath='+inpath+' '
    message += 'outpath='+outpath+' '
    yn = 'n'
    if (cleanup): yn = 'y'
    message += 'cleanup='+yn+' '
    message += 'fitsconfig='+fitsconfig+' '
    message += 'logfile='+logfile+' '
    yn = 'n'
    if (verbose): yn = 'y'
    message += 'verbose='+yn+'\n'
    saltprint.log(logfile,message,verbose)

# start time

    saltprint.time('SALTBIN2FIT started at  ',logfile,verbose)

# check directory inpath exists

    if (status == 0): inpath, status = saltio.pathexists(inpath,logfile)

# check directory outpath exists or create it

    outpath = outpath.strip()
    if (outpath[-1] != '/'): outpath += '/'
    if (status == 0 and not os.path.exists(outpath)):
        status = saltio.createdir(outpath,'no',logfile)

# are there binary files in directory inpath?

    if (status == 0):
        binlist = glob.glob(inpath+'*.bin')
        if (len(binlist) == 0):
            message = 'SALTBIN2FIT: ERROR -- no binary files (*.bin) found in '+inpath
            status = saltprint.err(logfile,message)

# determine date of the observations

    if (status == 0):
        filemax = 4
        binpath = binlist[0].split('/')
        binfile = binpath[len(binpath)-1]
        if (len(binfile) < 19):
            date = binfile[-16:-8]
            instrument = binfile[-17]
        else:
            date = binfile[-17:-9]
            instrument = binfile[-18]
            filemax = 5

# check all files are consistent with one date and have understandable names

    if (status == 0):
        for file in binlist:
            binpath = file.split('/')
            file = binpath[len(binpath)-1]
            if (len(file) == 17 and file[-16:-8] != date):
                status = 10
            elif (len(file) == 18 and file[-17:-9] != date):
                status = 10
            if (status == 10):
                message = 'ERROR: SALTBIN2FIT -- Either binary files from multiple dates exist, '
                message += 'or binary files exist with non-standard names.'
                status = saltprint.err(logfile,message)
            if (file[0:1] != instrument):
                message = 'ERROR: SALTBIN2FIT -- there may be binary files for more than one instrument '
                message += 'because file names do not all start with the same character - '+file
                status = saltprint.err(logfile,message)

# create list of header definition files

    if (status == 0):
        headlist = glob.glob(inpath+'*.head')
        headlist.sort()
        if (len(headlist) == 0):
            message = 'SALTBIN2FIT: ERROR -- no header definition files (*.head) found in '+inpath
            status = saltprint.err(logfile,message)

# create list of bin files

    if (status == 0):
        binlist = glob.glob(inpath+'*.bin')
        binlist.sort()
        #set the maximum head value that can be used
        maxnexthead=findimagenumber(binlist[-1])+10
        if (len(binlist) == 0):
            message = 'SALTBIN2FIT: ERROR -- no bin files (*.bin) found in '+inpath
            status = saltprint.err(logfile,message)

# run vid2fits to convert the data


    print maxnexthead
    if (status == 0):

        #set yo tge counting for the image headers
        i=0
        inhead=headlist[i]
        i+=1
        #if only one image header exists
        nexthead=maxnexthead
        if i<len(headlist):
        #if more than one header list
            nexthead=findimagenumber(headlist[i])

        #loop through the binned images to convert each one
        for binimg in binlist:
            #name the output file
            fitsimg=string.replace(binimg,'.bin','.fits')

            #find the right inhead for each frame
            if findimagenumber(binimg) >= nexthead:
                inhead=headlist[i]
                i += 1
                if i < len(headlist):
                    nexthead=findimagenumber(headlist[i])
                else:
                    nexthead=maxnexthead

            #convert the images
            vid2fits.vid2fits(inhead,binimg,fitsimg,fitsconfig)
            try:
                 message = 'SALTBIN2FIT: Created '+fitsimg+' from '+binimg
                 message += ' using header file  '+inhead
                 saltprint.log(logfile,message,verbose)
            except:
                message = 'SALTBIN2FIT ERROR: Unable to create '+fitsimg+' from '+binimg
                message += ' using header file  '+inhead
                saltprint.log(logfile,message,verbose)



# end time

    if (status == 0):
        saltprint.log(logfile,' ',verbose)
        saltprint.time('SALTBIN2FIT completed at',logfile,verbose)
    else:
        saltprint.time('SALTBIN2FIT aborted at  ',logfile,verbose)
Example #2
0
def salthtml(propcode,scamobslog,rssobslog, hrsobslog, htmlpath,nightlog,readme,clobber,logfile,verbose):

# set up
   
    filenames = []
    proposers = []
    propids = []
    instrumes = []
    objects = []
    ras = []
    decs = []
    detmodes = []
    ccdtypes = []
    ccdsums = []
    gainsets = []
    rospeeds = []
    filters = []
    gratings = []
    gr_angles = []
    ar_angles = []
    time_obss = []
    date_obss = []
    exptimes = []
    hours = []
    filename = {}
    proposer = {}
    propid = {}
    instrume = {}
    object = {}
    ra = {}
    dec = {}
    detmode = {}
    ccdsum = {}
    ccdtype = {}
    gainset = {}
    rospeed = {}
    filter = {}
    grating = {}
    gr_angle = {}
    ar_angle = {}
    time_obs = {}
    exptime = {}
    status=0

# test the logfile

    logfile = saltio.logname(logfile)

# log the call

    saltprint.line(logfile,verbose)
    history = 'SALTHTML -- '
    history += 'scamobslog='+scamobslog+' '
    history += 'rssobslog='+rssobslog+' '
    history += 'htmlpath='+htmlpath+' '
    history += 'nightlog='+nightlog+' '
    history += 'readme='+readme+' '
    yn = 'n'
    if (clobber): yn = 'y'
    history += 'clobber='+yn+' '
    history += 'logfile='+logfile+' '
    yn = 'n'
    if (verbose): yn = 'y'
    history += 'verbose='+yn
    saltprint.log(logfile,history,verbose)

# start time

    saltprint.time('\nSALTHTML -- started at',logfile,verbose)
    saltprint.log(logfile,' ',verbose)

# are the arguments defined

    if (status == 0): pids,status = saltio.argunpack('propcode',propcode ,logfile)
    if (status == 0): status = saltio.argdefined('scamobslog',scamobslog,logfile)
    if (status == 0): status = saltio.argdefined('rssobslog',rssobslog,logfile)
    if (status == 0): status = saltio.argdefined('htmlpath',htmlpath,logfile)



# check htmlpath exists, ends with a "/" and convert to absolute path

    if (status == 0): htmlpath, status = saltio.abspath(htmlpath,logfile)

# check observation log files exist

    if (status == 0 and scamobslog.upper() != 'NONE'): status = saltio.fileexists(scamobslog,logfile)
    if (status == 0 and rssobslog.upper() != 'NONE'): status = saltio.fileexists(rssobslog,logfile)

# read observation logs

    for obslog in [scamobslog, rssobslog, hrsobslog]:
        if (status == 0 and obslog.upper() != 'NONE'): struct,status = saltio.openfits(obslog,logfile)
        if (status == 0 and obslog.upper() != 'NONE'): obstab,status = saltio.readtab(struct[1],obslog,logfile)
        if (status == 0 and obslog.upper() != 'NONE'): status = saltio.closefits(struct,logfile)
        if (status == 0 and obslog.upper() != 'NONE'):
            filenames.extend(obstab.field('filename'))
            objects.extend(obstab.field('object'))
            ras.extend(obstab.field('ra'))
            decs.extend(obstab.field('dec'))
            instrumes.extend(obstab.field('instrume'))
            proposers.extend(obstab.field('proposer'))
            propids.extend(obstab.field('propid'))
            ccdtypes.extend(obstab.field('ccdtype'))
            ccdsums.extend(obstab.field('ccdsum'))
            gainsets.extend(obstab.field('gainset'))
            rospeeds.extend(obstab.field('rospeed'))
            detmodes.extend(obstab.field('detmode'))
            filters.extend(obstab.field('filter'))
            time_obss.extend(obstab.field('time-obs'))
            date_obss.extend(obstab.field('date-obs'))
            exptimes.extend(obstab.field('exptime'))
            if (obslog == rssobslog):
                gratings.extend(obstab.field('grating'))
                gr_angles.extend(obstab.field('gr-angle'))
                ar_angles.extend(obstab.field('ar-angle'))
            else:
                for i in range(len(filenames)):
                    gratings.append(' ')
                    gr_angles.append(0.)
                    ar_angles.append(0.)


# Create the list of proposals

    if (status == 0): pids,status=saltio.cleanpropcode(pids, propids, logfile)


# date of observations
    
    date, caldate = salttime.date_obs2yyyymmdd(date_obss[0])

# sort into chronological order

    for i in range(len(filenames)):
        hours.append(salttime.time_obs2hr(time_obss[i]))
        if (hours[i] < 12.): hours[i] += 24
        filename[str(hours[i])] = filenames[i]
        object[str(hours[i])] = objects[i]
        ra[str(hours[i])] = ras[i]
        dec[str(hours[i])] = decs[i]
        instrume[str(hours[i])] = instrumes[i]
        proposer[str(hours[i])] = proposers[i]
        propid[str(hours[i])] = propids[i]
        ccdsum[str(hours[i])] = ccdsums[i].replace(' ','x')
        ccdtype[str(hours[i])] = ccdtypes[i]
        gainset[str(hours[i])] = gainsets[i]
        rospeed[str(hours[i])] = rospeeds[i]
        detmode[str(hours[i])] = detmodes[i]
        filter[str(hours[i])] = filters[i]
        time_obs[str(hours[i])] = time_obss[i]
        grating[str(hours[i])] = gratings[i]
        gr_angle[str(hours[i])] = gr_angles[i]
        ar_angle[str(hours[i])] = ar_angles[i]
        exptime[str(hours[i])] = exptimes[i]
        if (instrume[str(hours[i])] == 'SALTICAM'): instrume[str(hours[i])] = 'SCM'
        if ('Video Mode' in detmode[str(hours[i])]): detmode[str(hours[i])] = 'VI'
        if ('Slot Mode' in detmode[str(hours[i])]): detmode[str(hours[i])] = 'SL'
        if ('Frame Transfer' in detmode[str(hours[i])]): detmode[str(hours[i])] = 'FT'
        if ('Normal' in detmode[str(hours[i])]): detmode[str(hours[i])] = 'IM'
        if ('Bright' in gainset[str(hours[i])]): gainset[str(hours[i])] = 'BR'
        if ('Faint' in gainset[str(hours[i])]): gainset[str(hours[i])] = 'FA'
        if ('Fast' in rospeed[str(hours[i])]): rospeed[str(hours[i])] = 'FA'
        if ('Slow' in rospeed[str(hours[i])]): rospeed[str(hours[i])] = 'SL'
        if ('OBJECT' not in ccdtype[str(hours[i])].upper() and
            'UNKNOWN' in proposer[str(hours[i])].upper()):
            proposer[str(hours[i])] = ''
    hours.sort()

# create HTML directory in datapath and define html files

    docpath = htmlpath + 'doc/'
    if (status == 0 and not os.path.exists(docpath)): status = saltio.createdir(docpath,False,logfile)

    htmlfile = docpath + 'ObservationSequence' + date +'.html'
    notefile = docpath + 'CapeTownNotes' + date + '.html'
    nlogfile = docpath + 'AstronomersLog' + date + '.html'
    plogfile = docpath + 'PipelineLog' + date + '.html'
    elogfile = docpath + 'EnvironmentLog' + date + '.html'
    dlogfile = docpath + 'InstrumentDiagnostics' + date + '.html'

# Copy css and banner images to the doc directory
    if (status == 0):
        status=saltio.copy(iraf.osfn('pipetools$html/style.css'),docpath,False,logfile)
        status=saltio.copy(iraf.osfn('pipetools$html/style_home.css'),docpath,False,logfile)
        status=saltio.copy(iraf.osfn('pipetools$html/header_salt.jpg'),docpath,False,logfile)


# write observation log html file

    if (status == 0):
        status = writeobslog(htmlfile,filename,object,ra,dec,instrume,detmode,filter,ccdsum,
                             gainset,rospeed,grating,gr_angle,ar_angle,exptime,time_obs,proposer,
                             propid,hours,date,caldate,clobber,logfile,verbose,status)

# write readme html file

    if (status == 0):
        status = writecapetownnotes(notefile,readme,date,caldate,clobber,logfile,verbose,status)

# write nightlog html file

    if (status == 0):
        status = writenightlog(nlogfile,nightlog,date,caldate,clobber,logfile,verbose,status)

# write pipeline log html file

    if (status == 0):
        status = writepipelog(plogfile,date,caldate,clobber,logfile,verbose,status)

# write environment log html file

    if (status == 0):
        status = writeenvlog(elogfile,date,caldate,clobber,logfile,verbose,status)

# write instrument diagnostics html file

    if (status == 0):
        status = writediaglog(dlogfile,date,caldate,clobber,logfile,verbose,status)

# copy html files to PI directories

    if (status == 0):
        saltprint.log(logfile,' ',verbose)
        for pids in set(propids):
            for pid in pids.split(','):
                pid = pid.strip().upper()
                pidpath = htmlpath + pid
                if (os.path.exists(pidpath)):
                    if (os.path.exists(pidpath+'/doc')):
                        for file in glob.glob(pidpath+'/doc/*'):
                            status = saltio.delete(file,False,logfile)
                        status = saltio.deletedir(pidpath+'/doc',logfile)
                    status = saltio.copydir(docpath,pidpath+'/doc',verbose,logfile)
                    if (status == 0):
                        infile, status = saltio.openascii(pidpath+'/doc/CapeTownNotes'+date+'.html','r',logfile)
                    if (status == 0): saltio.delete(pidpath+'/doc/CapeTownNotes'+date+'.html',False,logfile)
                    if (status == 0):
                        outfile, status = saltio.openascii(pidpath+'/doc/CapeTownNotes'+date+'.html','w',logfile)
                        if (status == 0):
                            for line in infile:
                                #line = line.replace('SALT user','Dr ' + string.capwords(pi.lower()))
                                #line = line.replace('yourname',pi)
                                line = line.replace('yyyymmdd',date)
                                outfile.write(line)
                            status = saltio.closeascii(outfile,logfile)

# end time

    if (status == 0):
        saltprint.time('\nSALTHTML -- completed at',logfile,verbose)
    else:
        saltprint.time('\nSALTHTML -- aborted at',logfile,verbose)