def writeenvlog(elogfile,date,caldate,clobber,logfile,verbose,status): # overwrite old environemnt log html file if it exists line = ' ' if (status == 0): status = saltio.overwrite(elogfile,clobber,logfile) # open environment log html file if (status == 0): saltprint.log(logfile,'SALTHTML -- creating EnvironmentLog' + date + '.html',verbose) outfile, status = saltio.openascii(elogfile,'w',logfile) # write html header if (status == 0): status = htmlheader(outfile,date,caldate,'EnvironmentLog',0,logfile) # content pending outfile.write('Content pending\n') # write html footer if (status == 0): status = htmlfooter(outfile,logfile) # close htmlfile if (status == 0): status = saltio.closeascii(outfile,logfile) return status
def symlink(infile,linkfile,clobber,verbose,logfile): # delete file if one of the same name already exists status = 0 message = 'SALTIO.SYMLINK -- created symbolic link from ' + infile + ' to ' + linkfile if (os.path.exists(linkfile) and not clobber): message = 'ERROR: SALTIO.SYMLINK -- file ' + linkfile + ' exists, use clobber=y' status = saltprint.err(logfile,message) if (status == 0 and clobber): try: os.remove(linkfile) except: status = 0 # create symbolic link if (status == 0): try: os.symlink(infile,linkfile) except: message = 'ERROR: SALTIO.SYMLINK -- could not create symbolic link from ' message += infile + ' to ' + linkfile status = saltprint.err(logfile,message) if (status == 0): saltprint.log(logfile,message,verbose) return status
def writediaglog(dlogfile,date,caldate,clobber,logfile,verbose,status): # overwrite old diagnostic html file if it exists line = ' ' if (status == 0): status = saltio.overwrite(dlogfile,clobber,logfile) # open diagnostic log html file if (status == 0): saltprint.log(logfile,'SALTHTML -- creating InstrumentDiagnostics' + date + '.html',verbose) outfile, status = saltio.openascii(dlogfile,'w',logfile) # write html header if (status == 0): status = htmlheader(outfile,date,caldate,'InstrumentDiagnostics',0,logfile) # content pending outfile.write('Content pending\n') # write html footer if (status == 0): status = htmlfooter(outfile,logfile) # close htmlfile if (status == 0): status = saltio.closeascii(outfile,logfile) return status
def delete(file,verbose,logfile): status = 0 message = 'SALTIO.DELETE -- deleted file ' + file try: os.remove(file) saltprint.log(logfile,message,verbose) except Exception, e: message = 'ERROR -- SALTIO.DELETE: Could not delete %s because %s' % (file ,e) status = saltprint.err(logfile,message)
def move(file1,file2,verbose,logfile): status = 0 message = 'SALTIO.MOVE -- moved ' + file1 + ' to ' + file2 try: shutil.move(file1,file2) saltprint.log(logfile,message,verbose) except: message = 'ERROR -- SALTIO.MOVE: Could not move ' + file1 + ' to ' + file2 status = saltprint.err(logfile,message) return status
def copy(file1,file2,verbose,logfile): status = 0 message = 'SALTIO.COPY -- copied ' + file1 + ' to ' + file2 try: shutil.copy2(file1,file2) saltprint.log(logfile,message,verbose) except: message = 'ERROR -- SALTIO.COPY: could not copy ' + file1 + ' to ' + file2 status = saltprint.err(logfile,message) return status
def tmpfile(path,verbose,logfile): status = 0 message = 'SALTIO.TMPFILE -- Created temporary file name ' try: tempfile.tempdir = path file = tempfile.mktemp() saltprint.log(logfile,message + file,verbose) except: file = '' message = 'ERROR -- SALTPRINT.TMPFILE: Cannot create temporary file name' status = saltprint.err(logfile,message) return file, status
def changedir(path,verbose,logfile): status = 0 path = path.strip() message = 'SALTIO.CHANGEDIR -- Moved to directory ' + path try: os.chdir(path) saltprint.log(logfile,message,verbose) except: message = 'ERROR -- SALTIO.CREATEDIR: Could not move to directory ' + path status = saltprint.err(logfile,message) return status
def writecapetownnotes(notefile,readme,date,caldate,clobber,logfile,verbose,status): # overwrite old observation log html file if it exists line = ' ' if (status == 0): status = saltio.overwrite(notefile,clobber,logfile) # open observation log html file if (status == 0): saltprint.log(logfile,'SALTHTML -- creating CapeTownNotes' + date + '.html',verbose) outfile, status = saltio.openascii(notefile,'w',logfile) # write html header if (status == 0): status = htmlheader(outfile,date,caldate,'CapeTownNotes',0,logfile) # readme file exists? if (status == 0): if (not os.path.isfile(readme)): status=1 message = 'ERRROR: SALTHTML -- readme file does not exist' else: # open nightlog file if (status == 0): infile, status = saltio.openascii(readme,'r',logfile) # append night log to html file if (status == 0): while line: line = infile.readline() outfile.write(line) # append readme to html file if (status == 0): while line: line = infile.readline() outfile.write(line) # write html footer if (status == 0): status = htmlfooter(outfile,logfile) return status
def createdir(path,verbose,logfile): status = 0 path = path.strip() message = 'SALTIO.CREATEDIR -- Created directory ' + path if (path[-1] != '/'): path += '/' if (not os.path.exists(path)): try: os.mkdir(path) saltprint.log(logfile,message,verbose) except: message = 'ERROR -- SALTIO.CREATEDIR: Could not create directory ' + path status = saltprint.err(logfile,message) else: message = 'SALTIO.CREATEDIR -- ' + path + ' directory exists' saltprint.log(logfile,message,verbose) return status
def writepipelog(plogfile,date,caldate,clobber,logfile,verbose,status): # overwrite old pipeline log html file if it exists line = ' ' if (status == 0): status = saltio.overwrite(plogfile,clobber,logfile) # open pipeline log html file if (status == 0): saltprint.log(logfile,'SALTHTML -- creating PipelineLog' + date + '.html',verbose) outfile, status = saltio.openascii(plogfile,'w',logfile) # write html header if (status == 0): status = htmlheader(outfile,date,caldate,'PipelineLog',0,logfile) # nightlog file exists? if (status == 0): if (not os.path.isfile(logfile)): message = 'WARNING: SALTHTML -- pipeline log does not exist' else: # open nightlog file if (status == 0): infile, status = saltio.openascii(logfile,'r',logfile) # append night log to html file if (status == 0): while line: line = infile.readline() outfile.write(line) # write html footer if (status == 0): status = htmlfooter(outfile,logfile) # close htmlfile if (status == 0): status = saltio.closeascii(outfile,logfile) return status
def mkheader(file,keyword,value,comment,verbose,logfile): """create keyword with mkheader IRAF tool i.e. without opening the whole file""" status = 0 message = 'SALTKEY.MKHEADER: Created keyword ' + keyword + ' in ' + file try: tmpfile, status = saltio.tmpfile('.',False,logfile) tmp, status = saltio.openascii(tmpfile,'w',logfile) tmp.write('%-8s= \'%-18s\' / %-s\n' % (keyword,value,comment)) status = saltio.closeascii(tmp,logfile) iraf.noao.artdata.mkheader(file,tmpfile,append='y',verbose='n') saltio.delete(tmpfile,False,logfile) saltprint.log(logfile,message,verbose) except: message = 'ERROR -- SALTKEY.MKHEADER: Cannot edit keyword ' + keyword message += ' in ' + file status = saltprint.err(logfile,message) return status
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)
def deleteframe(self, e): self.newphot=1 self.goodframes[self.id] = 0 message='SALTPHOT: Extension %i was deleted' % self.pid[self.id] saltprint.log(self.logfile, message, self.verbose)
def 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): # overwrite observation log html file if (status == 0 and os.path.isfile(htmlfile) and clobber): status = saltio.delete(htmlfile,False,logfile) elif (status == 0 and os.path.isfile(htmlfile) and not clobber): message = 'ERROR: SALTHTML -- file ' + htmlfile + ' exists. Use clobber=y' status = saltprint.err(logfile,message) # open observation log html file if (status == 0): line = ' ' saltprint.log(logfile,'SALTHTML -- creating ObservationSequence' + date + '.html',verbose) outfile, status = saltio.openascii(htmlfile,'w',logfile) # write html header if (status == 0): status = htmlheader(outfile,date,caldate,'ObservationLog',-1,logfile) # write table headings if (status == 0): outfile.write("%14s %12s %10s %9s %3s %2s %7s %3s %2s %2s %6s %5s %6s %6s %8s %15s %16s \n" % ('file'.ljust(14), 'object'.ljust(12), 'ra2000'.rjust(10), 'dec2000'.rjust(9), 'ins'.ljust(3), 'md'.ljust(2), 'filter'.rjust(7), 'bin'.rjust(3), 'gn'.rjust(2), 'sp'.rjust(2), 'grat'.rjust(6), 'gr-ang'.ljust(6), 'ar-ang'.ljust(6), 'exp'.rjust(6), 'UT'.rjust(8), 'Code'.ljust(15), 'PI'.ljust(16))) for i in range(140): outfile.write('-') outfile.write('\n') # write table for i in hours: outfile.write("%15s %12s %10s %9s %3s %2s %7s %3s %2s %2s %6s %5.2f %6.2f %6.1f %8s %15s %16s\n" % (filename[str(i)].replace('.fits','')[:13].ljust(13), object[str(i)][:12].ljust(12), ra[str(i)][:10].ljust(10), dec[str(i)][:9].ljust(9), instrume[str(i)][:3].ljust(3), detmode[str(i)][:2].ljust(2), filter[str(i)][:7].rjust(7), ccdsum[str(i)][:3].rjust(3), gainset[str(i)][:2].rjust(2), rospeed[str(i)][:2].rjust(2), grating[str(i)][:6].ljust(6), gr_angle[str(i)], ar_angle[str(i)], exptime[str(i)], time_obs[str(i)][:8].ljust(8), propid[str(i)][:15].ljust(15), proposer[str(i)][:16].ljust(16))) # write html foolter if (status == 0): status = htmlfooter(outfile,logfile) return status
def slot(struct, infile, dbspeed, dbrate, dbgain, dbnoise, dbbias, dbamp, xcoeff, gaindb, xtalkfile, logfile, verbose): import saltprint, saltkey, saltio, saltstat, time # identify instrument instrume, keyprep, keygain, keybias, keyxtalk, keyslot, status = saltkey.instrumid( struct, infile, logfile) # number of image HDU nextend = 0 while (status == 0): try: struct[nextend + 1].header['XTENSION'] nextend += 1 except: break nccds, status = saltkey.get('NCCDS', struct[0], infile, logfile) amplifiers = nccds * 2 if (nextend % (amplifiers) != 0): message = '\nERROR -- SALTSLOT: Number of image extensions and' message += 'number of amplifiers are not consistent' status = saltprint.err(saltlog, message) status = saltkey.new('NSCIEXT', nextend, 'Number of science extensions', struct[0], infile, logfile) status = saltkey.new('NEXTEND', nextend, 'Number of data extensions', struct[0], infile, logfile) # check image file and gain database are compatible if (status == 0): ngains = len(dbgain) if (int(max(dbamp)) != amplifiers): message = '\nERROR -- SALTGSLOT: ' + infile + ' contains ' + str( amplifiers) + ' amplifiers' message += ', the gaindb file ' + gaindb + ' contains ' + str( max(dbamp)) + ' amplifiers' status = saltprint.err(logfile, message) # check image file and cross talk database are compatible if (status == 0): if (len(xcoeff) - 1 != amplifiers): message = '\nERROR -- SALTSLOT: ' + infile + ' contains ' + str( amplifiers) + ' amplifiers' message += ', the cross talk file ' + xtalkfile + ' contains ' + str( len(xcoeff) - 1) + ' amplifiers' status = saltprint.err(logfile, message) # housekeeping keywords if (status == 0): status = saltkey.put('SAL-TLM', time.asctime(time.localtime()), struct[0], infile, logfile) status = saltkey.new(keyslot, time.asctime(time.localtime()), 'Data have been cleaned by SALTSLOT', struct[0], infile, logfile) # keywords for image extensions for i in range(nextend): hdu = i + 1 status = saltkey.new('EXTNAME', 'SCI', 'Extension name', struct[hdu], infile, logfile) status = saltkey.new('EXTVER', hdu, 'Extension number', struct[hdu], infile, logfile) # log coefficent table if (status == 0): message = '%30s %5s %4s %8s' % ('HDU', 'Gain', 'Bias', 'Xtalk') saltprint.log(logfile, '\n ---------------------------------------------', verbose) saltprint.log(logfile, message, verbose) saltprint.log(logfile, ' ---------------------------------------------', verbose) # loop over image extensions if (status == 0): for i in range(nextend / 2): hdu = i * 2 + 1 amplifier = hdu % amplifiers if (amplifier == 0): amplifier = amplifiers if (status == 0): value, status = saltkey.get('NAXIS1', struct[hdu], infile, logfile) naxis1 = int(value) if (status == 0): value, status = saltkey.get('NAXIS1', struct[hdu + 1], infile, logfile) naxis2 = int(value) if (status == 0 and hdu == 1): biassec, status = saltkey.get('BIASSEC', struct[hdu], infile, logfile) if (status == 0 and hdu == 1): ranges = biassec.lstrip('[').rstrip(']').split(',') x1_1 = int(ranges[0].split(':')[0]) - 1 x2_1 = int(ranges[0].split(':')[1]) - 1 y1_1 = int(ranges[1].split(':')[0]) - 1 y2_1 = int(ranges[1].split(':')[1]) - 1 if (status == 0 and hdu == 1): biassec, status = saltkey.get('BIASSEC', struct[hdu + 1], infile, logfile) if (status == 0 and hdu == 1): ranges = biassec.lstrip('[').rstrip(']').split(',') x1_2 = int(ranges[0].split(':')[0]) - 1 x2_2 = int(ranges[0].split(':')[1]) - 1 y1_2 = int(ranges[1].split(':')[0]) - 1 y2_2 = int(ranges[1].split(':')[1]) - 1 if (status == 0 and hdu == 1): datasec, status = saltkey.get('DATASEC', struct[hdu], infile, logfile) if (status == 0 and hdu == 1): ranges = datasec.lstrip('[').rstrip(']').split(',') dx1_1 = int(ranges[0].split(':')[0]) - 1 dx2_1 = int(ranges[0].split(':')[1]) dy1_1 = int(ranges[1].split(':')[0]) - 1 dy2_1 = int(ranges[1].split(':')[1]) if (status == 0 and hdu == 1): datasec, status = saltkey.get('DATASEC', struct[hdu + 1], infile, logfile) if (status == 0 and hdu == 1): ranges = datasec.lstrip('[').rstrip(']').split(',') dx1_2 = int(ranges[0].split(':')[0]) - 1 dx2_2 = int(ranges[0].split(':')[1]) dy1_2 = int(ranges[1].split(':')[0]) - 1 dy2_2 = int(ranges[1].split(':')[1]) if (status == 0 and dx2_1 - dx1_1 != dx2_2 - dx1_2): message = 'ERROR -- SALTSLOT: HDUs ' + infile message += '[' + str(hdu) + '] and ' + infile + '[' + str( hdu + 1) + ']' message += ' have different dimensions' status = saltprint.err(logfile, message) # read speed and gain of each exposure if (status == 0 and hdu == 1): gainset, status = saltkey.get('GAINSET', struct[0], infile, logfile) rospeed, status = saltkey.get('ROSPEED', struct[0], infile, logfile) if (rospeed == 'NONE'): saltprint.log(logfile, " ", verbose) message = "ERROR -- SALTSLOT: Readout speed is 'NONE' in " message += "primary keywords of " + infile status = saltprint.err(logfile, message) # read raw images if (status == 0): imagedata1, status = saltio.readimage(struct, hdu, logfile) imagedata2, status = saltio.readimage(struct, hdu + 1, logfile) # gain correction if (status == 0): for j in range(len(dbgain)): if (gainset == dbrate[j] and rospeed == dbspeed[j] and amplifier == int(dbamp[j])): try: gain1 = float(dbgain[j]) imagedata1 *= gain1 except: mesage = 'ERROR -- SALTSLOT: Cannot perform gain correction on image ' message += infile + '[' + str(hdu) + ']' status = saltprint.err(logfile, message) elif (gainset == dbrate[j] and rospeed == dbspeed[j] and amplifier + 1 == int(dbamp[j])): try: gain2 = float(dbgain[j]) imagedata2 *= gain2 except: mesage = 'ERROR -- SALTSLOT: Cannot perform gain correction on image ' message += infile + '[' + str(hdu + 1) + ']' status = saltprint.err(logfile, message) # crosstalk correction if (status == 0): revimage1 = imagedata1 * float(xcoeff[amplifier]) revimage2 = imagedata2 * float(xcoeff[amplifier + 1]) for j in range(dx2_1 - dx1_1 + 1): imagedata1[:, j] -= revimage2[:, dx2_2 - j - 1] imagedata2[:, j] -= revimage1[:, dx2_1 - j - 1] # bias subtraction if (status == 0): overx_val_1 = [] overx_val_2 = [] for x in range(x1_1, x2_1 + 1): list_1 = imagedata1[y1_1:y2_1, x] * 1.0 overx_val_1.append(saltstat.median(list_1, logfile)) overlevel_1 = saltstat.median(overx_val_1, logfile) for x in range(x1_2, x2_2 + 1): list_2 = imagedata2[y1_2:y2_2, x] * 1.0 overx_val_2.append(saltstat.median(list_2, logfile)) overlevel_2 = saltstat.median(overx_val_2, logfile) imagedata1 -= overlevel_1 imagedata2 -= overlevel_2 # trim overscan if (status == 0): imagedata1 = imagedata1[dy1_1:dy2_1, dx1_1:dx2_1] imagedata2 = imagedata2[dy1_2:dy2_2, dx1_2:dx2_2] datasec = '[1:' + str(dx2_1 - dx1_1) + ',1:' + str(dy2_1 - dy1_1) + ']' status = saltkey.put('DATASEC', datasec, struct[hdu], infile, logfile) status = saltkey.rem('BIASSEC', struct[hdu], infile, logfile) datasec = '[1:' + str(dx2_2 - dx1_2) + ',1:' + str(dy2_2 - dy1_2) + ']' status = saltkey.put('DATASEC', datasec, struct[hdu + 1], infile, logfile) status = saltkey.rem('BIASSEC', struct[hdu + 1], infile, logfile) # log coefficient table if (status == 0): infilename = infile.split('/') infilename = infilename[len(infilename) - 1] message = '%25s[%3d] %5.2f %4d %8.6f' % \ (infilename, hdu, gain1, overlevel_1, float(xcoeff[amplifier+1])) saltprint.log(logfile, message, verbose) message = '%25s[%3d] %5.2f %4d %8.6f' % \ (infilename, hdu+1, gain2, overlevel_2,float(xcoeff[amplifier])) saltprint.log(logfile, message, verbose) # update image in HDU structure if (status == 0): struct, status = saltio.writeimage(struct, hdu, imagedata1, logfile) struct, status = saltio.writeimage(struct, hdu + 1, imagedata2, logfile) return struct, status
def slot(struct,infile,dbspeed,dbrate,dbgain,dbnoise,dbbias,dbamp,xcoeff,gaindb,xtalkfile, logfile,verbose): import saltprint, saltkey, saltio, saltstat, time # identify instrument instrume,keyprep,keygain,keybias,keyxtalk,keyslot,status = saltkey.instrumid(struct,infile,logfile) # number of image HDU nextend = 0 while (status == 0): try: struct[nextend+1].header['XTENSION'] nextend += 1 except: break nccds,status = saltkey.get('NCCDS',struct[0],infile,logfile) amplifiers = nccds * 2 if (nextend%(amplifiers) != 0): message = '\nERROR -- SALTSLOT: Number of image extensions and' message += 'number of amplifiers are not consistent' status = saltprint.err(saltlog,message) status = saltkey.new('NSCIEXT',nextend,'Number of science extensions',struct[0],infile,logfile) status = saltkey.new('NEXTEND',nextend,'Number of data extensions',struct[0],infile,logfile) # check image file and gain database are compatible if (status == 0): ngains = len(dbgain) if (int(max(dbamp)) != amplifiers): message = '\nERROR -- SALTGSLOT: ' + infile + ' contains ' + str(amplifiers) + ' amplifiers' message += ', the gaindb file ' + gaindb + ' contains ' + str(max(dbamp)) + ' amplifiers' status = saltprint.err(logfile,message) # check image file and cross talk database are compatible if (status == 0): if (len(xcoeff)-1 != amplifiers): message = '\nERROR -- SALTSLOT: ' + infile + ' contains ' + str(amplifiers) + ' amplifiers' message += ', the cross talk file ' + xtalkfile + ' contains ' + str(len(xcoeff)-1) + ' amplifiers' status = saltprint.err(logfile,message) # housekeeping keywords if (status == 0): status = saltkey.put('SAL-TLM',time.asctime(time.localtime()),struct[0],infile,logfile) status = saltkey.new(keyslot,time.asctime(time.localtime()), 'Data have been cleaned by SALTSLOT',struct[0],infile,logfile) # keywords for image extensions for i in range(nextend): hdu = i + 1 status = saltkey.new('EXTNAME','SCI','Extension name',struct[hdu],infile,logfile) status = saltkey.new('EXTVER',hdu,'Extension number',struct[hdu],infile,logfile) # log coefficent table if (status == 0): message = '%30s %5s %4s %8s' % ('HDU','Gain','Bias','Xtalk') saltprint.log(logfile,'\n ---------------------------------------------',verbose) saltprint.log(logfile,message,verbose) saltprint.log(logfile,' ---------------------------------------------',verbose) # loop over image extensions if (status == 0): for i in range(nextend/2): hdu = i * 2 + 1 amplifier = hdu%amplifiers if (amplifier == 0): amplifier = amplifiers if (status == 0): value,status = saltkey.get('NAXIS1',struct[hdu],infile,logfile) naxis1 = int(value) if (status == 0): value,status = saltkey.get('NAXIS1',struct[hdu+1],infile,logfile) naxis2 = int(value) if (status == 0 and hdu == 1): biassec, status = saltkey.get('BIASSEC',struct[hdu],infile,logfile) if (status == 0 and hdu == 1): ranges = biassec.lstrip('[').rstrip(']').split(',') x1_1 = int(ranges[0].split(':')[0]) - 1 x2_1 = int(ranges[0].split(':')[1]) - 1 y1_1 = int(ranges[1].split(':')[0]) - 1 y2_1 = int(ranges[1].split(':')[1]) - 1 if (status == 0 and hdu == 1): biassec, status = saltkey.get('BIASSEC',struct[hdu+1],infile,logfile) if (status == 0 and hdu == 1): ranges = biassec.lstrip('[').rstrip(']').split(',') x1_2 = int(ranges[0].split(':')[0]) - 1 x2_2 = int(ranges[0].split(':')[1]) - 1 y1_2 = int(ranges[1].split(':')[0]) - 1 y2_2 = int(ranges[1].split(':')[1]) - 1 if (status == 0 and hdu == 1): datasec,status = saltkey.get('DATASEC',struct[hdu],infile,logfile) if (status == 0 and hdu == 1): ranges = datasec.lstrip('[').rstrip(']').split(',') dx1_1 = int(ranges[0].split(':')[0]) - 1 dx2_1 = int(ranges[0].split(':')[1]) dy1_1 = int(ranges[1].split(':')[0]) - 1 dy2_1 = int(ranges[1].split(':')[1]) if (status == 0 and hdu == 1): datasec,status = saltkey.get('DATASEC',struct[hdu+1],infile,logfile) if (status == 0 and hdu == 1): ranges = datasec.lstrip('[').rstrip(']').split(',') dx1_2 = int(ranges[0].split(':')[0]) - 1 dx2_2 = int(ranges[0].split(':')[1]) dy1_2 = int(ranges[1].split(':')[0]) - 1 dy2_2 = int(ranges[1].split(':')[1]) if (status == 0 and dx2_1 - dx1_1 != dx2_2 - dx1_2): message = 'ERROR -- SALTSLOT: HDUs '+infile message += '['+str(hdu)+'] and '+infile+'['+str(hdu+1)+']' message += ' have different dimensions' status = saltprint.err(logfile,message) # read speed and gain of each exposure if (status == 0 and hdu == 1): gainset,status = saltkey.get('GAINSET',struct[0],infile,logfile) rospeed,status = saltkey.get('ROSPEED',struct[0],infile,logfile) if (rospeed == 'NONE'): saltprint.log(logfile," ",verbose) message = "ERROR -- SALTSLOT: Readout speed is 'NONE' in " message += "primary keywords of " + infile status = saltprint.err(logfile,message) # read raw images if (status == 0): imagedata1,status = saltio.readimage(struct,hdu,logfile) imagedata2,status = saltio.readimage(struct,hdu+1,logfile) # gain correction if (status == 0): for j in range(len(dbgain)): if (gainset == dbrate[j] and rospeed == dbspeed[j] and amplifier == int(dbamp[j])): try: gain1 = float(dbgain[j]) imagedata1 *= gain1 except: mesage = 'ERROR -- SALTSLOT: Cannot perform gain correction on image ' message += infile+'['+str(hdu)+']' status = saltprint.err(logfile,message) elif (gainset == dbrate[j] and rospeed == dbspeed[j] and amplifier + 1 == int(dbamp[j])): try: gain2 = float(dbgain[j]) imagedata2 *= gain2 except: mesage = 'ERROR -- SALTSLOT: Cannot perform gain correction on image ' message += infile+'['+str(hdu+1)+']' status = saltprint.err(logfile,message) # crosstalk correction if (status == 0): revimage1 = imagedata1 * float(xcoeff[amplifier]) revimage2 = imagedata2 * float(xcoeff[amplifier+1]) for j in range(dx2_1-dx1_1+1): imagedata1[:,j] -= revimage2[:,dx2_2-j-1] imagedata2[:,j] -= revimage1[:,dx2_1-j-1] # bias subtraction if (status == 0): overx_val_1 = [] overx_val_2 = [] for x in range(x1_1,x2_1+1): list_1 = imagedata1[y1_1:y2_1,x] * 1.0 overx_val_1.append(saltstat.median(list_1,logfile)) overlevel_1 = saltstat.median(overx_val_1,logfile) for x in range(x1_2,x2_2+1): list_2 = imagedata2[y1_2:y2_2,x] * 1.0 overx_val_2.append(saltstat.median(list_2,logfile)) overlevel_2 = saltstat.median(overx_val_2,logfile) imagedata1 -= overlevel_1 imagedata2 -= overlevel_2 # trim overscan if (status == 0): imagedata1 = imagedata1[dy1_1:dy2_1,dx1_1:dx2_1] imagedata2 = imagedata2[dy1_2:dy2_2,dx1_2:dx2_2] datasec = '[1:'+str(dx2_1-dx1_1)+',1:'+str(dy2_1-dy1_1)+']' status = saltkey.put('DATASEC',datasec,struct[hdu],infile,logfile) status = saltkey.rem('BIASSEC',struct[hdu],infile,logfile) datasec = '[1:'+str(dx2_2-dx1_2)+',1:'+str(dy2_2-dy1_2)+']' status = saltkey.put('DATASEC',datasec,struct[hdu+1],infile,logfile) status = saltkey.rem('BIASSEC',struct[hdu+1],infile,logfile) # log coefficient table if (status == 0): infilename = infile.split('/') infilename = infilename[len(infilename)-1] message = '%25s[%3d] %5.2f %4d %8.6f' % \ (infilename, hdu, gain1, overlevel_1, float(xcoeff[amplifier+1])) saltprint.log(logfile,message,verbose) message = '%25s[%3d] %5.2f %4d %8.6f' % \ (infilename, hdu+1, gain2, overlevel_2,float(xcoeff[amplifier])) saltprint.log(logfile,message,verbose) # update image in HDU structure if (status == 0): struct,status = saltio.writeimage(struct,hdu,imagedata1,logfile) struct,status = saltio.writeimage(struct,hdu+1,imagedata2,logfile) return struct, status
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)