def get_cl2d(self, axis, prefix='cl2d_1', clfind_args=None): density = self.get_fits_array('density', axis) name_root = '%s/%s' % (self.fits_dir, prefix) outname = "%s_Mask.fits" % name_root if clfind_args is None: clfind_args = {'levels': range(1, 12), 'log': True, 'nPixMin': 3} if len(glob.glob(outname)) == 0: clfind2d.clfind2d(self.fits_name, name_root, **clfind_args) return pyfits.open(outname)[0].data
def calcBackground(image, logger, verbose=True): import tempfile print print 'Automatic estimation of the background level' imageData = pf.open(image) tmpBackgroundLevel = calcBack(imageData[0].data) print 'Step 1) Background level = %.4E' % tmpBackgroundLevel sys.stdout.write('Step 2) Running CLFind to obtain mask ... ') sys.stdout.flush() if verbose is True: print tmpMask = tempfile.NamedTemporaryFile(dir='./', delete=False) tmpLevels = np.linspace(10.0 * tmpBackgroundLevel, np.max(imageData[0].data), 20) clfind2d(image, tmpMask.name, tmpLevels, log=False, nPixMin=20, verbose=verbose) if verbose is False: print 'done' print 'Step 3) Calculating residuals' dataMask = pf.getdata(tmpMask.name) mask = np.where(dataMask > 0) imageData[0].data[mask] = np.nan backLevel = calcBack(imageData[0].data) print 'Step 4) Background level of the residuals = %.4E' % backLevel if os.path.exists(tmpMask.name): os.remove(tmpMask.name) del imageData, dataMask return backLevel
def getRegions(configOpts, logger): ##################################################################### ########################## FUV processing ########################## fuvImage = configOpts['fuvImage'] logger.write('FUV Image: %s' % fuvImage, newLine=True) if not os.path.exists(fuvImage): raiseError('FUV image does not exist.', logger) if (configOpts['fuvMaskFile'].lower() == 'default') or (configOpts['fuvMaskFile'] is None): configOpts['fuvMaskFile'] = configOpts['root'] + '_Mask.fits' doCLFindFUV = True if os.path.exists(configOpts['fuvMaskFile']): if configOpts['overwrite'] is True: doCLFindFUV = True else: doCLFindFUV = getYN('\nFound {0}. Rerun CLFind? [y/N] '.format(configOpts['fuvMaskFile']), returnOpt=False) if doCLFindFUV is False: logger.write('FUV mask not created. Using previously generated mask %s.' % configOpts['fuvMaskFile']) else: if configOpts['fuvInteractive'] is False: logger.write('FUV parameters loaded') else: logger.write('FUV: Running in interactive mode ... ', newLine=True) data = pf.getdata(fuvImage) logger.write('Image statistics', newLine=True, log=True) logger.write('Max: %9.3e' % np.nanmax(data), log=True) logger.write('Min: %9.3e' % np.nanmin(data), log=True) logger.write('Mean: %9.3e' % np.mean(data), log=True) logger.write('Median: %9.3e' % np.median(data), log=True) configOpts['fuvBackground'] = calcBackground(fuvImage, logger, verbose=configOpts['verbose']) logger.write('Background: %.5e' % configOpts['fuvBackground'], newLine=True) configOpts['fuvSigma'] = getFloat('Sigma [3]: ', 3) configOpts['fuvhLevel'] = getFloat('Highest level [-1 for automatic]: ', -1) configOpts['fuvnLevels'] = getFloat('Number of levels [-1 for automatic]: ', -1) configOpts['fuvMinPixels'] = getFloat('Minimum number of pixels [20]: ', 20) calcParameters('fuv', configOpts, logger) printCLFindOptions('fuv', configOpts, logger) resultCheck = checkCLFindOptions('fuv', configOpts) if resultCheck is False: raiseError('Some of the parameters are incorrect. Please, review the configuration file.', logger) clfind2d(fuvImage, configOpts['fuvMaskFile'], configOpts['fuvLevels'], log=True, nPixMin=configOpts['fuvMinPixels'], verbose=configOpts['verbose']) logger.write('CLFind log can be found in %s.log' % configOpts['fuvMaskFile'], newLine=True, doPrint=False) ##################################################################### ########################## FUV rejection ########################### if configOpts['checkFUV']: if (configOpts['fuvMaskFileRej'] is None) or (configOpts['fuvMaskFileRej'].lower() == 'default'): configOpts['fuvMaskFileRej'] = os.path.splitext(configOpts['fuvImage'])[0] + '_RejMsk.fits' doRejection = True if os.path.exists(configOpts['fuvMaskFileRej']) and doCLFindFUV is False: if configOpts['overwrite'] is False: doRejection = getYN('\nFound %s. Redo FUV rejection? [y/N] ' % configOpts['fuvMaskFileRej'], returnOpt=False) if doRejection is True: logger.write('FUV rejection', newLine=True) checkFUV(configOpts, logger) logger.write('New mask saved as %s' % configOpts['fuvMaskFileRej'], newLine=True) else: logger.write('Not doing FUV rejection. Using image %s' % configOpts['fuvMaskFileRej']) ##################################################################### ###################### FUV regions catalogue ####################### logger.write('Creating FUV catalogs ... ', newLine=True) rejMask = configOpts['fuvMaskFileRej'] votRegsFile = os.path.splitext(rejMask)[0] + '.vot' ds9RegsFile = os.path.splitext(rejMask)[0] + '.reg' peaksFile = os.path.splitext(rejMask)[0] + '_Peaks.dat' if False not in map(os.path.exists, [votRegsFile, ds9RegsFile, peaksFile]) and \ configOpts['overwrite'] is False: logger.write('FUV catalogues already exist.', newLine=True) if os.path.exists(votRegsFile): logger.write('VOTable catalogue: %s' % votRegsFile) logger.write('DS9 catalogue: %s' % ds9RegsFile) logger.write('Peaks file: %s' % peaksFile) else: createCatalog(fuvImage, rejMask, votRegsFile, ds9RegsFile, logger, peaksFile=peaksFile, ellipse=False, plot=configOpts['plotDS9']) return ##################################################################### ########################## HI processing ########################### # if configOpts['hiInteractive'] == False: # areCLParamsOK = checkCLFindOptions('hi') # if areCLParamsOK == False: # fancyPrint('Some HI parameters in config file are wrong\n', # newLine=True, log=True) # sys.exit() # fancyPrint('HI parameters loaded', newLine=True, log=True) # else: # fancyPrint('HI: Running in interactive mode ... ', newLine=True, log=True) # data = pf.getdata(configOpts['hiImage']) # fancyPrint('Image statistics', newLine=True, log=True) # fancyPrint('Max: %9.3e' % np.nanmax(data), log=True) # fancyPrint('Min: %9.3e' % np.nanmin(data), log=True) # fancyPrint('Mean: %9.3e' % np.mean(data), log=True) # fancyPrint('Median: %9.3e' % np.median(data), log=True) # configOpts['hiBackground'] = \ # calcBackground(configOpts['hiImage'], fancyPrint) # fancyPrint('Background: %.5e' % configOpts['hiBackground'], # newLine=True, log=False) # fancyPrint('') # configOpts['hiSigma'] = getFloat('Sigma [3]: ', 3) # configOpts['hihLevel'] = getFloat('Highest level [-1 for automatic]: ', -1) # configOpts['hinLevels'] = getFloat('Number of levels [-1 for automatic]: ', -1) # configOpts['hiMinPixels'] = getFloat('Minimum number of pixels [20]: ', 20) # fancyPrint('') # hiLevels = calcParameters('hi') # if (configOpts['hiMaskFile'].lower() == 'default') or (configOpts['hiMaskFile'] == None): # configOpts['hiMaskFile'] = os.path.splitext(configOpts['hiImage'])[0] + '_Msk.fits' # doIDL = True # if os.path.exists(configOpts['hiMaskFile']): # doIDL = getYN('\nFound {0}. Rerun CLFind? [y/N] '.format(configOpts['hiMaskFile']), # returnOpt=False) # if doIDL: # status, maskFile, idlLogFile = clfind2dIDL(configOpts['hiImage'], hiLevels, # log=configOpts['log'], nPixMin=configOpts['hiMinPixels'], # verbose=configOpts['verbose']) # if not status: # fancyPrint('Problem found running IDL', newLine=True, log=True) # fancyPrint('Kill IDL, check {0} and try again\n'.format(idlLogFile), log=True) # sys.exit() # sh.move(maskFile, configOpts['hiMaskFile']) # logData = open(idlLogFile, 'r').read().splitlines() # sh.move(idlLogFile, 'clfind2dHI.log') # else: # logData = open('clfind2dHI.log', 'r').read().splitlines() # for line in logData: # fancyPrint(line, log=True, noPrint=True) # if 'clumps found (' in line: # m = re.match(r'(\d+)(.+)(\(+)(\d+)', line.replace(' ','')) # nClumpsHI = int(m.groups(0)[0]) # nClumpsRejHI = nClumps = int(m.groups(0)[-1]) # fancyPrint('HI mask file: {0}'.format(configOpts['hiMaskFile']), newLine=True, log=True) # fancyPrint('Number of clumps: {0}'.format(nClumpsHI), log=True) # fancyPrint('Number of clumps rejected: {0}'.format(nClumpsRejHI), log=True) # # ##################################################################### # # ########################## HI rejection ########################### # # # # if configOpts['checkHI']: # # if (configOpts['fuvMaskFileRej'] == None) or (configOpts['fuvMaskFileRej'].lower() == 'default'): # # configOpts['fuvMaskFileRej'] = os.path.splitext(configOpts['fuvImage'])[0] + '_RejMsk.fits' # # # # doRejection = True # # if os.path.exists(configOpts['fuvMaskFileRej']) and doIDL == False: # # doRejection = getYN('\nFound {0}. Redo FUV rejection? [y/N] '.format(configOpts['fuvMaskFileRej']), # # returnOpt=False) # # # # if doRejection == True: # # try: # # checkFUV(configOpts, fancyPrint) # # except Exception as detail: # # raise detail # # else: # # fancyPrint('Not doing FUV rejection. Using image {0}'.format(configOpts['fuvMaskFileRej']), # # log=True, noPrint=True, newLine=True) # # # # # # ##################################################################### # fancyPrint('') return configOpts
if 0: field = 'Density' fits_name = 'p14b_atest.fits' hdu = pyfits.PrimaryHDU(frb[field]) hdulist = pyfits.HDUList([hdu]) hdulist.writeto(fits_name) clump_fname = 'p14_atest1_Mask.fits' if 0: import clfind2d import pyfits if os.path.exists(clump_fname): os.remove(clump_fname) clfind_args = {'levels': [3], 'log': True, 'nPixMin': 3} clfind2d.clfind2d(fits_name, 'p14_atest1', **clfind_args) if 0: #twod_clump = np.transpose(pyfits.open(clump_fname)[0].data) twod_clump = pyfits.open(clump_fname)[0].data flat_clump = twod_clump.flatten() clump_axis = 0 plt.clf() plt.imshow(twod_clump) plt.savefig('p14_atest_clump.png') if 0: clump_axis = 0 twod_clump = np.transpose(pyfits.open(clump_fname)[0].data) flat_clump = twod_clump.flatten() clump_stuff_tuple = (flat_clump, 'xyz'[clump_axis], twod_clump)