def getUVFlux(configOpts, logger): logger.write('Measuring FUV fluxes ...', newLine=True) uvheader, uvimage = open_image(configOpts['fuvImage']) data_uv = configOpts['data_uv'] coords_uv = os.path.splitext(configOpts['fuvMaskFileRej'])[0] + '_Peaks.dat' if os.path.exists(data_uv): try: columns = np.dtype([('PDRID', 'int',), ('RA', '|S14'), ('DEC', '|S14'), ('aperture', 'float'), ('mean_at_r', 'float'), ('bgflux', 'float'), ('cumulflux', 'float'), ('netflux', 'float'), ('sflux', 'float')]) fluxtable = read_array(data_uv, columns) logger.write('... fluxes read from file {0} instead.'.format(data_uv)) logger.write('... {0} records read'.format(np.size(fluxtable.PDRID))) return uvheader, fluxtable except: pass #file does not exist, so we calculate. Force recalculation by removing the file. #Variables in: uvheader, uvimage, uvcoords #Variable out: fluxtable (includes uvcoords afterwards) # fluxtable labels: PDRID, RA, DEC, aperture, mean_at_r, bgflux, cumulflux, netflux, sflux logger.write('No previous flux measurements.') uvcoords = at.read(coords_uv, delimiter=',') logger.write('Flux will be measured at {0} positions on image {1} ...'.format(len(uvcoords), configOpts['fuvImage'])) logger.write('Fluxes will be scaled by a factor {0}'.format(configOpts['FUVscale'])) #This scaling is useful since clumpfind doesn't seem to work properly with ~1e-15 values. fluxtable = fuv_flux(configOpts['fuvImage'], uvheader, uvcoords, configOpts, verbose=True) #logger.write(fluxtable.RA, fluxtable.RA[0] #here fluxtable.RA[0] is simply a string #Write: UV fluxes fluxfile = open(data_uv, 'w') #logger.write('#PDRID, aperture (arcsec), mean at r (units/arcsec^2), bg flux, cumul. flux, net flux' fluxfile.write('#PDRID, RA, DEC, aperture (arcsec), mean at r (units/pixel), bg flux, cumul. flux, net flux, sigma net flux\n') for n in range(len(fluxtable)): #fedora bug: changed PDRID:>3 to PDRID:3g #logger.write('{0.PDRID:3g}, {1.aperture:5.2f}, {1.mean_at_r:7.5e}, {1.bgflux:7.5e}, {1.cumulflux:7.5e}, ' + # '{1.netflux:7.5e}'.format(uvcoords[n], fluxtable[n]) fluxfile.write('{0.PDRID:3g}, {0.RA}, {0.DEC}, {1.aperture:7.5e}, {1.mean_at_r:7.5e}, {1.bgflux:7.5e}, ' + '{1.cumulflux:7.5e}, {1.netflux:7.5e}, {1.sflux:7.5e}\n'.format(uvcoords[n], fluxtable[n])) fluxfile.close() return uvheader, fluxtable
def getHI(configOpts, fluxtable): print "Identifying HI patches ..." data_hi = configOpts['data_hi'] hiImage = configOpts['hiImage'] hi_baselist = "{0}_Regions/{0}_Reg".format(os.path.splitext(hiImage)[0]) hi_logsbase = "{0}_Regions/{1}".format(os.path.splitext(hiImage)[0], configOpts['hi_logsbase']) try: dummy = file(data_hi, 'r') print "... HI patch data read from file {0}".format(data_hi) #Read: HI data (if already there); note that if the read fails, patches will be re-measured also columns = np.dtype([('PDRID', 'int',), ('RA', '|S14'), ('DEC', '|S14'), ('NHI', 'float'), ('sNHI', 'float')]) hidata = pdr.read_array(data_hi, columns) print " {0} records read".format(np.size(hidata)) #Very simple and incomplete check for enough data: if (np.size(np.unique(hidata.PDRID)) != np.size(np.unique(fluxtable.PDRID))): print "Fatal error: number of UV and HI PDRIDs do not match. Try regenerating the HI files." exit(0) except: #File does not exist, so process HI postage stamp regions. #Must have regions with names matching the PDRIDs scale = configOpts['HIscale'] # 2.896e19 for NGC 628 (m?)Jy/beam to cm-2 hi_bg = configOpts['hiBackground'] print "Using map scaling of {0} cm-2".format(scale) hiresults = [[] for dummy in xrange(5)] #PDRID, ra, dec, NHI, sNHI #Warning: up to 999 files for i in range(np.size(fluxtable.PDRID)): fitsfile = hi_baselist + "{0:03d}.fits".format(int(fluxtable.PDRID[i])) print "Calling SExtractor with file {0}".format(fitsfile) catfile = pdr.call_SEx(fitsfile) if catfile==1: print "Fatal error: SExtractor call failed." exit(0) else: logfile = hi_logsbase + "{0:03}.txt".format(int(fluxtable.PDRID[i])) patches = pdr.read_secat(catfile, fitsfile, logfile, wcs=True) #Patches are not background-subtracted, that will happen now: for n in range(len(patches[0])): if patches[2][n] > (hi_bg*2.): #subtract the background and ignore patches that are fainter than hi_bg hiresults[0].append(fluxtable.PDRID[i]) hiresults[1].append(patches[0][n]) hiresults[2].append(patches[1][n]) hiresults[3].append((patches[2][n]-hi_bg)*scale) hiresults[4].append(hi_bg * scale * 0.5) #fix sN_HI to half the background value else: print "Patch too faint: ", patches[2][n] if not np.size(np.where(hiresults[0] == fluxtable.PDRID[i])): print "No suitable patches were found for PDRID ", fluxtable.PDRID[i], "; creating empty entry." #With SExtractor, this is unlikely since it does not pick up the faintest patches hiresults[0].append(fluxtable.PDRID[i]) hiresults[1].append(0) hiresults[2].append(0) hiresults[3].append(0) hiresults[4].append(0) #End for #Finally, convert the results into a rec array and save the results labels = 'PDRID, RA, DEC, NHI, sNHI' hidata = pdr.records(hiresults, labels) #Write: HI patches hifile = fancyPrint(writeLog=True, logFile=data_hi, verbose=True) print "HI patches:" #print "#PDRID, RA, DEC, NHI, sNHI" hifile.write("#PDRID, RA, DEC, NHI, sNHI\n") for n in range(len(hidata)): #print type(hidata[n].NHI) #Apparently python 2.6 needs explicit conversion from numpy.float32, so workaround #fedora bug: changed PDRID:>3 to PDRID:3g #print "{0.PDRID:3g}, {0.RA:14s}, {0.DEC:14s}, {1:7.5g}, {2:7.5g}".format(hidata[n], float(hidata[n].NHI), float(hidata[n].sNHI)) hifile.write("{0.PDRID:3g}, {0.RA:14s}, {0.DEC:14s}, {1:7.5g}, {2:7.5g}\n".format(hidata[n], float(hidata[n].NHI), float(hidata[n].sNHI))) #hifile.close() #End of try/except print return hidata