def __init__(self,obs,delFilters=_delFiltsDef,excludefilt=None,noContext=None): """ constructor gets an Observation object (obs) from the pipeline. """ self.modName = string.split(string.split(str(self))[0],'.')[0][1:] self.root = obs.newobspath # root path of the observation dir self.obsName = obs.newobs self.obsPars = obs.newpar self.obsFits = obs.newfits # path to fits files self.messagedir = obs.messagedir # where the module message will go. self.configdir = obs.configdir self.logfile = obs.logfile self.defaultInParFile = os.path.join(obs.pardir,self.modName + '.inpar') self.defaultParamFile = os.path.join(obs.pardir,self.modName + '.param') self.inParFileName = os.path.join(self.obsPars, 'noisy.inpar') self.inpars = pUtil.readInParFile(self.defaultInParFile) # readInParFile returns a dict self.detImName = 'detectionImage.fits' self.detWgtName = 'detectionWeight.fits' self.edgeMaskName = obs.edgeMaskName self.errorList = [] self.inputList = [] self.outputList = {} self.logfile.write(self.modName+": Instantiated detectionImage object.") # excludefilt must be a list of ACS filter names # i.e. --excludefilt=['F502N','F475W'] # in a pipeline context, this form will be passed to the constructor. self.excludeFilters = excludefilt if noContext: checkLists = [obs.sciImageList,obs.weightImageList,obs.flagImageList,obs.rmsImageList] else: checkLists = [obs.sciImageList,obs.contextImageList,obs.weightImageList,obs.flagImageList,obs.rmsImageList] # remove drizzled grism images and other oddities from various lists and further processing if delFilters: self.logfile.write("Checking for input images having filters: "+str(delFilters)) for _obsList in checkLists: for ii in range(len(_obsList)-1,-1,-1): im = _obsList[ii] imfilt1 = fUtil.getFilter1(self.obsFits+'/'+im) imfilt2 = fUtil.getFilter2(self.obsFits+'/'+im) if imfilt1 in delFilters or imfilt2 in delFilters: self.logfile.write(im+" ("+imfilt+") removed from further obs processing.") del _obsList[ii] # now it belongs to the ages for _obsList in checkLists: if len(_obsList) != len(obs.sciImageList): errtxt = "Error: inconsistency in Image Lists after removing unwanted filter images." self.logfile.write(errtxt) raise Exception,errtxt self.sciImageList = obs.sciImageList for i in self.sciImageList: self.inputList.append(i) if len(self.sciImageList) == 0: raise detectionImageError("Image list has no images to make detection image.")
def getStats(self,Band): """Runs SExtractor with phoney pars to get noise and background stats. Now implement this using the Popen4 class, which sends both stdout and stderr to the fromchild file object. We suppress stdout from SExtractor by shunting stuff to /dev/null via the par file, so we will only get stderr anyway, at least, in theory. The Popen3 class or rather, the factory function is uses, popen3, has proved to be buggy at times, so this is an attempt to move away from it. """ self.logfile.write('Generating background and noise statistics for fits images') self.statsList = [] # pdb.set_trace() for fitsfile in self.sciImageList: # Use user-defined detector for the detection image det = fUtil.getDetector(os.path.join(self.obsFits,fitsfile)) if det == self.det: if (Band =='red'): if string.find(fitsfile,'160') > -1 or string.find(fitsfile,'140') > -1: yes = 1 else: self.logfile.write('Not including '+fitsfile+' in detection image.') yes = 0 else: yes = 1 else: self.logfile.write('Not including '+fitsfile+' in detection image.') yes = 0 if self.excludeFilters: # pass on user excluded filters for the detection image filt = fUtil.getFilter2(os.path.join(self.obsFits,fitsfile)) if filt in self.excludeFilters: self.logfile.write('User requested exclude filter found in data.') self.logfile.write('Not including'+filt+' in detection image.') yes = 0 if (yes == 1): self.logfile.write('running SExtractor on ' + fitsfile) cmd = 'sex ' + os.path.join(self.obsFits, fitsfile) + ' -c ' + self.inParFileName subproc = popen2.Popen4(cmd) lines = subproc.fromchild.readlines() self.logfile.write('logging background and noise stats.') for line in lines: if string.find(line,'(M+D) Background:') != -1: # pdb.set_trace() linef = string.split(line) self.logfile.write(fitsfile+': '+linef[1]+linef[2]+' '+linef[3]+linef[4]) self.statsList.append((fitsfile,linef[2],linef[4])) # linef[2] is background, linef[4] is noise. ############ attention here by XX elif string.find(line,'*ERROR*') != -1: self.errorList.append((self.modName,line)) self.logfile.write('Error in running SExtractor: '+ line) return
def xmlStartCat(outfile, name, imgfile=None): """Initialise the xml header of a catalog file. imgfile is a passed fits file. """ xmlcatalog = open(outfile, 'w+') xmlcatalog.write( "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n") xmlcatalog.write('<!DOCTYPE catalog SYSTEM ' +\ '\n\t"http://acs.pha.jhu.edu/science/pipeline/DTD/Catalog.dtd">\n') if imgfile == None: xmlcatalog.write("<catalog type=\"xml/text\" dataset=\"" + name + "\" date=\"" + ptime() + "\">\n") else: img = os.path.basename(imgfile) tel = fUtil.getTel(imgfile) inst = fUtil.getInstr(imgfile) det = fUtil.getDetector(imgfile) filter1 = fUtil.getFilter1(imgfile) filter2 = fUtil.getFilter2(imgfile) acs = filters.ACSFilterInfo() if filter1 and filter2: if filter1 not in acs.clear and filter2 not in acs.clear: xmlcatalog.write("<catalog type=\"xml/text\" imgsrc=\"" +img+ \ "\" dataset=\""+name+"\" telescope=\"" +tel+ \ "\" instrument=\""+inst+"\" detector=\"" +det+\ "\" filtername=\""+filter1+"/"+filter2+"\" date=\""+ptime()+"\">\n") elif filter1 in acs.clear: xmlcatalog.write("<catalog type=\"xml/text\" imgsrc=\"" +img+ \ "\" dataset=\""+name+"\" telescope=\"" +tel+ \ "\" instrument=\""+inst+"\" detector=\"" +det+\ "\" filtername=\""+filter2+"\" date=\""+ptime()+"\">\n") elif filter2 in acs.clear: xmlcatalog.write("<catalog type=\"xml/text\" imgsrc=\"" +img+ \ "\" dataset=\""+name+"\" telescope=\"" +tel+ \ "\" instrument=\""+inst+"\" detector=\"" +det+\ "\" filtername=\""+filter1+"\" date=\""+ptime()+"\">\n") else: print "No filters found in image:", img, "for markup." xmlcatalog.write("<catalog type=\"xml/text\" dataset=\""+name+"\" imgsrc=\"" \ +img+ "\" date=\""+ptime()+"\">\n") xmlcatalog.close() return