示例#1
0
def get_model(model_file=None, with_default=False):
    if model_file is not None:
        try:
            logger.info('Loading model from %s.' % model_file)
            model = BDModel.load(model_file)
            model.wl = 5635.0
            return model
        except:
            raise Exception('Could not read model file %s.' % model_file)
    elif not with_default:
        logger.info('Using default model.')
        return default_model()
    else:
        raise Exception('No model_file and with_default=False, what do you want?')
示例#2
0
    '''
    return (x - x0) * a + y0
################################################################################


################################################################################
##########
########## Setup
##########
################################################################################

logger.setLevel(-1)
args = parse_args()
pdf = plot_setup(args.plotFile)

logger.info('Loading data from: %s' % args.db)
db = openFile(args.db)

try:
    logger.info('Galaxy: %s' % args.galaxyName)
    g = db.getNode('/%s' % args.galaxyName)
except:
    logger.error('Unknown galaxy: %s' % args.galaxyName)
    sys.exit()

masked = (g.flag_ifs[...] > 0) | (g.full_ifs[...] < 0.0)
masked2d = masked.sum(axis=0) > masked.shape[0] / 2
bulge_image = g.bulge_image[...]
disk_image = g.disk_image[...]
full_image = np.ma.array(bulge_image + disk_image, mask=masked2d)
示例#3
0
def decomp(cube, sampleId, args):
    galaxyId = califa_id_from_cube(cube)
    c = DecompContainer()
    if not args.overwrite:
        logger.info('Checking if the decomposition is already done for %s ...' % galaxyId)
        try:
            c.loadHDF5(args.db, sampleId, galaxyId)
            logger.warn('Previous data found, skipping decomposition.')
            return c
        except Exception as e:
            print e
            logger.info('No previous data found, continuing decomposition.')
            
    logger.info('Starting fit for %s...' % galaxyId)
    dec = CALIFADecomposer(cube, grating=args.grating, nproc=args.nproc)
    npix = dec.K.qMask.sum()
    dec.minNPix = npix / 2
    logger.info('Minimum number of pixels for fitting: %d' % dec.minNPix)
    dec.useEstimatedVariance = args.estVar
    dec.setSynthPSF(FWHM=args.psfFWHM, beta=args.psfBeta, size=args.psfSize)
    
    logger.warn('Computing initial model using DE algorithm (takes a LOT of time).')
    t1 = time.time()
    if not path.exists(args.maskFile):
        logger.error('Mask file %s not found.' % args.maskFile)
        exit(1)
    logger.info('Using mask file %s.' % args.maskFile)
    masked_wl = load_line_mask(args.maskFile, dec.wl)
    
    l1 = find_nearest_index(dec.wl, 4500.0)
    l2 = dec.Nl_obs
    cache_file = cube + '.initmodel'
    if not path.exists(cache_file):
        logger.info('Creating gray image for initial model.')
        gray_image, gray_noise, _ = dec.getSpectraSlice(l1, l2, masked_wl)
    else:
        gray_image = None
        gray_noise = None
    initial_model = bd_initial_model(gray_image, gray_noise, dec.PSF, quiet=False, nproc=args.nproc,
                                            cache_model_file=cache_file)
    logger.debug('Refined initial model:\n%s\n' % initial_model)
    logger.warn('Initial model time: %.2f\n' % (time.time() - t1))
    
    t1 = time.time()
    c.zones = np.ma.array(dec.K.qZones, mask=dec.K.qZones < 0)
    c.initialParams = initial_model.getParams()
    c.attrs = dict(PSF_FWHM=args.psfFWHM,
                   PSF_beta=args.psfBeta,
                   PSF_size=args.psfSize,
                   box_step=args.boxStep,
                   box_radius=args.boxRadius,
                   orig_file=cube,
                   mask_file=args.maskFile, 
                   object_name=dec.K.galaxyName,
                   flux_unit=dec.flux_unit,
                   distance_Mpc=dec.K.distance_Mpc,
                   x0=dec.K.x0,
                   y0=dec.K.y0,
                   target_vd=dec.targetVd,
                   wl_FWHM=dec.wlFWHM)
    
    models = dec.fitSpectra(step=50*args.boxStep, box_radius=25*args.boxStep,
                            initial_model=initial_model, mode=args.fitAlgorithm, masked_wl=masked_wl)
    c.firstPassParams = np.array([m.getParams() for m in models], dtype=models[0].dtype)
    logger.info('Done first pass modeling, time: %.2f' % (time.time() - t1))
    
    t1 = time.time()
    logger.info('Smoothing parameters.')
    models = smooth_models(models, dec.wl, degree=1)
    
    logger.info('Starting second pass modeling...')
    models = dec.fitSpectra(step=args.boxStep, box_radius=args.boxRadius,
                            initial_model=models, mode=args.fitAlgorithm, insist=False, masked_wl=masked_wl)
    logger.info('Done second pass modeling, time: %.2f' % (time.time() - t1))
    
    t1 = time.time()
    logger.info('Computing model spectra...')
    c.total.f_obs = dec.flux[::args.boxStep]
    c.total.f_err = dec.error[::args.boxStep]
    c.total.f_flag = dec.flags[::args.boxStep]
    c.total.mask = dec.K.qMask
    c.total.wl = dec.wl[::args.boxStep]
    
    c.bulge.f_obs, c.disk.f_obs = dec.getModelSpectra(models, args.nproc)
    c.bulge.mask = dec.K.qMask
    c.bulge.wl = dec.wl[::args.boxStep]
    c.disk.mask = dec.K.qMask
    c.disk.wl = dec.wl[::args.boxStep]

    # TODO: better array and dtype handling.
    c.fitParams = np.array([m.getParams() for m in models], dtype=models[0].dtype)
    
    flag_bad_fit = c.fitParams['flag'][:, np.newaxis, np.newaxis] > 0.0
    c.updateErrorsFlags(flag_bad_fit)
    c.updateIntegratedSpec()
    
    logger.info('Saving qbick planes...')
    fname = path.join(args.zoneFileDir, '%s_%s-planes.fits' % (galaxyId, sampleId))
    save_qbick_images(c.total, dec, fname, overwrite=args.overwrite)
    fname = path.join(args.zoneFileDir, '%s_%s-bulge-planes.fits' % (galaxyId, sampleId))
    save_qbick_images(c.bulge, dec, fname, overwrite=args.overwrite)
    fname = path.join(args.zoneFileDir, '%s_%s-disk-planes.fits' % (galaxyId, sampleId))
    save_qbick_images(c.disk, dec, fname, overwrite=args.overwrite)
    
    logger.info('Saving to storage...')
    c.writeHDF5(args.db, sampleId, galaxyId, args.overwrite)
    logger.info('Storage complete, time: %.2f' % (time.time() - t1))
    
    return c
示例#4
0
    return [BDModel.fromParamVector(k) for k in p]
################################################################################

        
################################################################################
##########
########## Population model setup
##########
################################################################################

logger.setLevel(-1)
args = parse_args()
pdf = plot_setup(args.plotFile)

logger.info('Loading base %s', path.basename(args.baseFile))
t1 = time.clock()
base = StarlightBase(args.baseFile, args.baseDir)
l_ssp = np.arange(base.l_ssp.min(), base.l_ssp.max(), 2.0)
logger.info('Took %.2f seconds to read the base (%d files)' % (time.clock() - t1, base.sspfile.size))
wl_norm_window = (base.l_ssp < 5680.0) & (base.l_ssp > 5590.0)

logger.info('Computing synthetic SFH and their spectra.')
bulge_sfh = SyntheticSFH(base.ageBase)
bulge_sfh.addExp(14e9, 2.0e9, 1.0)
bulge_flux = (base.f_ssp * bulge_sfh.massVector()[:, np.newaxis]).sum(axis=1).sum(axis=0)
bulge_flux /= np.median(bulge_flux[wl_norm_window])
bulge_flux = spec_resample(base.l_ssp, l_ssp, bulge_flux)

disk_sfh = SyntheticSFH(base.ageBase)
#disk_sfh.addExp(10e9, 2.0e9, 1.0)
示例#5
0
    return distance(shape, model.x0.value, model.x0.value, model.bulge.PA.value, model.bulge.ell.value)


################################################################################


################################################################################
##########
########## Population model setup
##########
################################################################################

logger.setLevel(-1)
args = parse_args()

logger.info("Loading base %s", path.basename(args.baseFile))
t1 = time.clock()
base = StarlightBase(args.baseFile, args.baseDir)
l_ssp = np.arange(3650.0, 6850.0, 2.0)
f_ssp = base.f_sspResam(l_ssp)
logger.info("Took %.2f seconds to read the base (%d files)" % (time.clock() - t1, base.sspfile.size))
wl_norm_window = (l_ssp < 5680.0) & (l_ssp > 5590.0)

################################################################################
##########
########## Morphology model setup
##########
################################################################################

logger.info("Creating original B-D model.")
norm_model = get_model(args.trueModel, with_default=True)
    frame = InitialModelFrame(None, wx.ID_ANY, image, noise, psf, model_file,
                              'Initial Model Finder', plot_title)
    frame.Show()
    app.MainLoop()
################################################################################


args = parse_args()
if args.verbose:
    logger.setLevel(-1)
    logger.debug('Verbose output enabled.')

cube = args.cube[0]
model_file = args.cube[0] + '.initmodel'
title = path.basename(cube)

logger.info('Loading cube: %s' % cube)
K = fitsQ3DataCube(args.cube[0])

logger.info('Creating PSF (FWHM=%f, beta=%f, size=%d)' % (args.psfFWHM, args.psfBeta, args.psfSize))
psf = moffat_psf(args.psfFWHM, args.psfBeta, size=args.psfSize)

flags = ~K.qMask | (K.qSignal <= 0.0) | (K.qNoise <= 0.0)
flux = np.ma.array(K.qSignal, mask=flags)
noise = np.ma.array(K.qNoise, mask=flags)

logger.info('Running GUI...')
run_app(flux, noise, psf, model_file, title)
logger.info('Exiting GUI.')

示例#7
0

################################################################################


################################################################################
##########
########## Population model setup
##########
################################################################################

logger.setLevel(-1)
args = parse_args()
pdf = plot_setup(args.plotFile)

logger.info("Loading base %s", path.basename(args.baseFile))
t1 = time.clock()
base = StarlightBase(args.baseFile, args.baseDir)
l_ssp = np.arange(base.l_ssp.min(), base.l_ssp.max(), 2.0)
f_ssp = base.f_sspResam(l_ssp)
logger.info("Took %.2f seconds to read the base (%d files)" % (time.clock() - t1, base.sspfile.size))
wl_norm_window = (l_ssp < 5680.0) & (l_ssp > 5590.0)

logger.info("Computing synthetic SFH and their spectra.")
# bulge_sfh = SyntheticSFH(base.ageBase)
# bulge_sfh.addExp(14e9, 2.0e9, 1.0)
# bulge_flux = (f_ssp * bulge_sfh.massVector()[:, np.newaxis]).sum(axis=1).sum(axis=0)
# bulge_flux /= np.median(bulge_flux[wl_norm_window])

# disk_sfh = SyntheticSFH(base.ageBase)
# disk_sfh.addSquare(0.0, 14e9, 1.0)