Example #1
0
def main():
    from legacypipe.runbrick import run_brick, get_parser, get_runbrick_kwargs
    parser = get_parser()
    #parser.add_argument('--subset', type=int, help='COSMOS subset number [0 to 4, 10 to 12]', default=0)
    opt = parser.parse_args()
    if opt.brick is None and opt.radec is None:
        parser.print_help()
        return -1

    optdict = vars(opt)
    verbose = optdict.pop('verbose')

    survey, kwargs = get_runbrick_kwargs(**optdict)
    if kwargs in [-1,0]:
        return kwargs

    #kwargs.update(prereqs_update={'detect': 'mask_junk',
    #                              'galdetect': 'mask_junk'})

    kwargs.update(prereqs_update={'detect': 'tims'})
    
    stagefunc = CallGlobalTime('stage_%s', globals())
    kwargs.update(stagefunc=stagefunc)

    #kwargs.update(stages=['image_coadds', 'detect'])
    #kwargs.update(stages=['galdetect'])
    #kwargs.update(stages=['detect',]) # 'srcs']) # with early_coadds, srcs:image_coadds
    #kwargs.update(stages=['srcs'])

    run_brick(opt.brick, survey, **kwargs)
    return 0
Example #2
0
def main():

    brickname = '2428p117'
    objtype = 'STAR'
    lobjtype = objtype.lower()

    decals_sim_dir = '/Users/ioannis/decals_sim_dir/star/2428p117'

    zoom = [1800,2400,1800,2400]
    #zoom = [1800,2000,1800,2000]
    #zoom = None

    decals = Decals()
    brickinfo = decals.get_brick_by_name(brickname)
    brickwcs = wcs_for_brick(brickinfo)
    W, H, pixscale = brickwcs.get_width(), brickwcs.get_height(), brickwcs.pixel_scale()

    if zoom is not None:
        # See also runbrick.stage_tims()
        #(x0,x1,y0,y1) = args.zoom
        (x0,x1,y0,y1) = zoom
        W = x1-x0
        H = y1-y0
        targetwcs = brickwcs.get_subimage(x0, y0, W, H)

    bounds = brickwcs.radec_bounds()
    ra_range = bounds[1]-bounds[0]
    dec_range = bounds[3]-bounds[2]
    radec_center = brickwcs.radec_center()
    print(radec_center, ra_range, dec_range)
        
    corners = np.array([brickwcs.pixelxy2radec(x,y) for x,y in
                        [(1,1),(W,1),(W,H),(1,H),(1,1)]])
    

    # Identify the CCDs in the region of interest.
    ccdinfo = decals.ccds_touching_wcs(brickwcs)
    ccdinfo.about()
    log.info('Got {} CCDs'.format(len(ccdinfo)))

    # Generate the catalog of simulated sources here!
    simcat = fits_table('simcat-{}-{}-00.fits'.format(brickname,lobjtype))
    #simcat = fits.getdata('simcat-{}-{}-00.fits'.format(brickname,lobjtype))
    simcat.about()
    blobxy = zip(simcat.x,simcat.y)

    simdecals = SimDecals(sim_sources=simcat)

    # For testing!
    #sim = SimImage(simdecals,ccdinfo[0])
    #tim = sim.get_tractor_image(const2psf=True)

    run_brick(brickname, decals=simdecals, outdir=decals_sim_dir,
              threads=8, zoom=zoom, wise=False, sdssInit=False,
              forceAll=True, writePickles=False, blobxy=blobxy,
              pixPsf=False, stages=['tims','srcs','fitblobs',
                                    'fitblobs_finish','coadds',
                                    'writecat'])
Example #3
0
def main():

    from astrometry.util.fits import fits_table, merge_tables
    from legacypipe.survey import exposure_metadata
    # Fake up a survey-ccds.fits table from MzLS_CP
    from glob import glob
    #fns = glob('/project/projectdirs/cosmo/staging/mosaicz/MZLS_CP/CP20160202/k4m_160203_*oki*')
    fns = glob('/project/projectdirs/cosmo/staging/mosaicz/MZLS_CP/CP20160202/k4m_160203_08*oki*')

    print('Filenames:', fns)
    T = exposure_metadata(fns)

    # HACK
    T.fwhm = T.seeing / 0.262

    # FAKE
    T.ccdnmatch = np.zeros(len(T), np.int32) + 50
    T.zpt = np.zeros(len(T), np.float32) + 26.518
    T.ccdzpt = T.zpt.copy()
    T.ccdraoff = np.zeros(len(T), np.float32)
    T.ccddecoff = np.zeros(len(T), np.float32)
    
    fmap = {'zd':'z'}
    T.filter = np.array([fmap[f] for f in T.filter])
    
    T.writeto('mzls-ccds.fits')

    os.system('cp mzls-ccds.fits ~/legacypipe-dir/survey-ccds.fits')
    os.system('gzip -f ~/legacypipe-dir/survey-ccds.fits')
    
    import sys
    sys.exit(0)
    
    import logging
    import sys
    from legacypipe.runbrick import run_brick, get_runbrick_kwargs, get_parser

    parser = get_parser()
    opt = parser.parse_args()
    if opt.brick is None and opt.radec is None:
        parser.print_help()
        return -1
    survey, kwargs = get_runbrick_kwargs(opt)
    if kwargs in [-1, 0]:
        return kwargs

    if opt.verbose == 0:
        lvl = logging.INFO
    else:
        lvl = logging.DEBUG
    logging.basicConfig(level=lvl, format='%(message)s', stream=sys.stdout)

    kwargs.update(splinesky=True, pixPsf=True)

    run_brick(opt.brick, survey, **kwargs)
Example #4
0
def _custom_brick(galaxycat,
                  objid,
                  survey=None,
                  radius=100,
                  ncpu=1,
                  pixscale=0.262):
    """Run legacypipe on a custom "brick" centered on the central.

    """
    from legacypipe.runbrick import run_brick

    run_brick(None,
              survey,
              radec=(galaxycat['ra'], galaxycat['dec']),
              pixscale=pixscale,
              width=2 * radius,
              height=2 * radius,
              threads=ncpu,
              normalizePsf=True,
              do_calibs=False,
              wise=False,
              depth_cut=False,
              splinesky=True,
              early_coadds=False,
              pixPsf=True,
              hybridPsf=True,
              ceres=False,
              rex=True,
              forceall=True,
              write_pickles=False,
              write_metrics=False,
              gaia_stars=True,
              stages=['writecat'])

    # Move (rename) files into the desired output directory and clean up.
    brickname = custom_brickname(galaxycat, prefix='custom-')
    oldfile = os.path.join(survey.output_dir, 'tractor', 'cus',
                           'tractor-{}.fits'.format(brickname))
    newfile = os.path.join(survey.output_dir, '{}-tractor.fits'.format(objid))
    shutil.copy(oldfile, newfile)

    oldfile = os.path.join(survey.output_dir, 'coadd', 'cus', brickname,
                           'legacysurvey-{}-ccds.fits'.format(brickname))
    newfile = os.path.join(survey.output_dir, '{}-ccds.fits'.format(objid))
    shutil.copy(oldfile, newfile)

    if True:
        shutil.rmtree(os.path.join(survey.output_dir, 'coadd'))
        shutil.rmtree(os.path.join(survey.output_dir, 'tractor'))
        shutil.rmtree(os.path.join(survey.output_dir, 'tractor-i'))
Example #5
0
def do_one_chunk(d=None):
    """Runs the legacypipe/Tractor pipeline on images with simulated sources

    Args:
        d: {'args': obiwan.kenobi.py cmd line argparse.Namespace object
            'brickname': chunk of sky
            'metacat': fits_table configuration params for the simulated sources
            'simcat': fits_table simulated source catalog for a given brick (not CCD).

    Note:
        runb_brick() is 'main' for the legacypipe/Tractor pipeline

    Returns:
        Nothing, but this func end ups writing out all the obiwan results
    """
    log = logging.getLogger('decals_sim')
    assert(d is not None)
    kw= dict(dataset=d['args'].dataset,\
             survey_dir=d['survey_dir'], \
             metacat=d['metacat'], simcat=d['simcat'], \
             output_dir=d['simcat_dir'], \
             add_sim_noise=d['args'].add_sim_noise, seed=d['seed'],\
             image_eq_model=d['args'].image_eq_model)
    if d['args'].dataset == 'cosmos':
        kw.update(subset=d['args'].subset)
        simdecals= SimDecalsCosmos(**kw)
    else:
        simdecals = SimDecals(**kw)
    # Use Tractor to just process the blobs containing the simulated sources.
    if d['args'].all_blobs:
        blobxy = None
    else:
        blobxy = zip(d['simcat'].get('x'), d['simcat'].get('y'))
    # Default runbrick call sequence
    obiwan_kwargs= vars(d['args'])
    runbrick_kwargs= get_runbrick_setup(**obiwan_kwargs)
    # Obiwan modifications
    runbrick_kwargs.update(blobxy=blobxy)
    #plotbase='obiwan')
    log.info('Calling run_brick with: ')
    log.info('brickname= %s' % d['brickname'])
#    log.info('simdecals= ',simdecals)
    log.info('runbrick_kwards= ',runbrick_kwargs)
    # Run it: run_brick(brick, survey obj, **kwargs)
    np.random.seed(d['seed'])
    log.info(runbrick_kwargs)
    run_brick(d['brickname'], simdecals, **runbrick_kwargs)
    log.info('HUI-TEST::: checkpoint3h')
Example #6
0
def do_one_chunk(d=None):
    '''Run tractor
    Can be run as 1) a loop over nchunks or 2) for one chunk
    d -- dict returned by get_metadata_others() AND added to by get_ith_simcat()'''
    assert(d is not None)
    simdecals = SimDecals(metacat=d['metacat'], simcat=d['simcat'], output_dir=d['simcat_dir'], \
                          add_sim_noise=d['args'].add_sim_noise, folding_threshold=d['args'].folding_threshold,\
                          image_eq_model=d['args'].image_eq_model)
    # Use Tractor to just process the blobs containing the simulated sources.
    if d['args'].all_blobs:
        blobxy = None
    else:
        blobxy = zip(d['simcat']['X'], d['simcat']['Y'])

    run_brick(d['brickname'], simdecals, threads=d['args'].threads, zoom=d['args'].zoom,
              wise=False, forceAll=True, writePickles=False, do_calibs=False,
              write_metrics=False, pixPsf=True, blobxy=blobxy, early_coadds=d['args'].early_coadds,
              splinesky=True, ceres=False, stages=[ d['args'].stage ], plots=False,
              plotbase='sim')
Example #7
0
def main():
    import logging
    import sys
    from legacypipe.runbrick import run_brick, get_runbrick_kwargs, get_parser

    parser = get_parser()
    opt = parser.parse_args()
    if opt.brick is None and opt.radec is None:
        parser.print_help()
        return -1
    kwargs = get_runbrick_kwargs(opt)
    if kwargs in [-1, 0]:
        return kwargs

    if opt.verbose == 0:
        lvl = logging.INFO
    else:
        lvl = logging.DEBUG
    logging.basicConfig(level=lvl, format='%(message)s', stream=sys.stdout)

    kwargs.update(splinesky=True, pixPsf=True)

    run_brick(opt.brick, **kwargs)
Example #8
0
def main():
    import logging
    import sys
    from legacypipe.runbrick import run_brick, get_runbrick_kwargs, get_parser

    parser = get_parser()
    opt = parser.parse_args()
    if opt.brick is None and opt.radec is None:
        parser.print_help()
        return -1
    kwargs = get_runbrick_kwargs(opt)
    if kwargs in [-1, 0]:
        return kwargs

    if opt.verbose == 0:
        lvl = logging.INFO
    else:
        lvl = logging.DEBUG
    logging.basicConfig(level=lvl, format='%(message)s', stream=sys.stdout)

    kwargs.update(splinesky=True, pixPsf=True)

    run_brick(opt.brick, **kwargs)
Example #9
0
def main():

    parser = argparse.ArgumentParser()
    parser.add_argument('--build-sample',
                        action='store_true',
                        help='Build the sample.')
    parser.add_argument('--jpg-cutouts',
                        action='store_true',
                        help='Get jpg cutouts from the viewer.')
    parser.add_argument('--ccd-cutouts',
                        action='store_true',
                        help='Get CCD cutouts of each galaxy.')
    parser.add_argument('--runbrick',
                        action='store_true',
                        help='Run the pipeline.')
    parser.add_argument('--build-webpage',
                        action='store_true',
                        help='(Re)build the web content.')
    args = parser.parse_args()

    # Top-level directory
    key = 'LEGACY_SURVEY_LARGE_GALAXIES'
    if key not in os.environ:
        print('Required ${} environment variable not set'.format(key))
        return 0
    largedir = os.getenv(key)
    samplefile = os.path.join(largedir, 'large-galaxies-sample.fits')

    # --------------------------------------------------
    # Build the sample of large galaxies based on the available imaging.
    if args.build_sample:

        # Read the parent catalog.
        cat = read_rc3()

        # Create a simple WCS object for each object and find all the CCDs
        # touching that WCS footprint.
        survey = LegacySurveyData(version='dr2')  # hack!
        allccds = survey.get_ccds()
        keep = np.concatenate((survey.apply_blacklist(allccds),
                               survey.photometric_ccds(allccds)))
        allccds.cut(keep)

        ccdlist = []
        outcat = []
        for gal in cat:
            galwcs = _simplewcs(gal)

            ccds1 = allccds[ccds_touching_wcs(galwcs, allccds)]
            ccds1 = ccds1[_uniqccds(ccds1)]

            if len(
                    ccds1
            ) > 0 and 'g' in ccds1.filter and 'r' in ccds1.filter and 'z' in ccds1.filter:
                print('Found {} CCDs for {}, D(25)={:.4f}'.format(
                    len(ccds1), gal['GALAXY'], gal['RADIUS']))

                ccdsfile = os.path.join(
                    largedir, 'ccds',
                    '{}-ccds.fits'.format(gal['GALAXY'].strip().lower()))
                print('  Writing {}'.format(ccdsfile))
                if os.path.isfile(ccdsfile):
                    os.remove(ccdsfile)
                ccds1.writeto(ccdsfile)

                ccdlist.append(ccds1)
                if len(outcat) == 0:
                    outcat = gal
                else:
                    outcat = vstack((outcat, gal))
                #if gal['GALAXY'] == 'MCG5-19-36':
                #    pdb.set_trace()

        # Write out the final catalog.
        samplefile = os.path.join(largedir, 'large-galaxies-sample.fits')
        if os.path.isfile(samplefile):
            os.remove(samplefile)
        print('Writing {}'.format(samplefile))
        outcat.write(samplefile)
        print(outcat)

        # Do we need to transfer any of the data to nyx?
        _getfiles(merge_tables(ccdlist))

    # --------------------------------------------------
    # Get data, model, and residual cutouts from the legacysurvey viewer.  Also
    # get thumbnails that are lower resolution.
    if args.jpg_cutouts:
        thumbsize = 100
        sample = fits.getdata(samplefile, 1)
        for gal in sample:
            size = np.ceil(10 * gal['RADIUS'] / PIXSCALE)
            thumbpixscale = PIXSCALE * size / thumbsize

            #imageurl = 'http://legacysurvey.org/viewer/jpeg-cutout-decals-dr2?ra={:.6f}&dec={:.6f}'.format(gal['RA'], gal['DEC'])+\
            #  '&pixscale={:.3f}&size={:g}'.format(PIXSCALE, size)
            #imagejpg = os.path.join(largedir, 'cutouts', gal['GALAXY'].strip().lower()+'-image.jpg')
            #if os.path.isfile(imagejpg):
            #    os.remove(imagejpg)
            #os.system('wget --continue -O {:s} "{:s}"' .format(imagejpg, imageurl))

            thumburl = 'http://legacysurvey.org/viewer/jpeg-cutout-decals-dr2?ra={:.6f}&dec={:.6f}'.format(gal['RA'], gal['DEC'])+\
              '&pixscale={:.3f}&size={:g}'.format(thumbpixscale, thumbsize)
            thumbjpg = os.path.join(
                largedir, 'cutouts',
                gal['GALAXY'].strip().lower() + '-image-thumb.jpg')
            if os.path.isfile(thumbjpg):
                os.remove(thumbjpg)
            os.system('wget --continue -O {:s} "{:s}"'.format(
                thumbjpg, thumburl))

    # --------------------------------------------------
    # (Re)build the webpage.
    if args.build_webpage:

        # index.html
        html = open(os.path.join(largedir, 'index.html'), 'w')
        html.write('<html><body>\n')
        html.write('<h1>Sample of Large Galaxies</h1>\n')
        html.write('<table border="2" width="30%">\n')
        html.write('<tbody>\n')
        sample = fits.getdata(samplefile, 1)
        for gal in sample:
            # Add coordinates and sizes here.
            galaxy = gal['GALAXY'].strip().lower()
            html.write('<tr>\n')
            html.write('<td><a href="html/{}.html">{}</a></td>\n'.format(
                galaxy, galaxy.upper()))
            html.write(
                '<td><a href="http://legacysurvey.org/viewer/?ra={:.6f}&dec={:.6f}" target="_blank"><img src=cutouts/{}-image-thumb.jpg alt={} /></a></td>\n'
                .format(gal['RA'], gal['DEC'], galaxy, galaxy.upper()))
            #           html.write('<td><a href="html/{}.html"><img src=cutouts/{}-image-thumb.jpg alt={} /></a></td>\n'.format(galaxy, galaxy, galaxy.upper()))
            html.write('</tr>\n')
        html.write('</tbody>\n')
        html.write('</table>\n')
        html.write('</body></html>\n')
        html.close()

        sys.exit(1)

        # individual galaxy pages
        for gal in sample[:3]:
            galaxy = gal['GALAXY'].strip().lower()
            html = open(os.path.join(largedir, 'html/{}.html'.format(galaxy)),
                        'w')
            html.write('<html><body>\n')
            html.write(
                '<a href=../cutouts/{}.jpg><img src=../cutouts/{}-image.jpg alt={} /></a>\n'
                .format(galaxy, galaxy, galaxy, galaxy.upper()))
            html.write('</body></html>\n')
            html.close()

    # --------------------------------------------------
    # Get cutouts of all the CCDs for each galaxy.
    if args.ccd_cutouts:
        sample = fits.getdata(samplefile, 1)

        for gal in sample[1:2]:
            galaxy = gal['GALAXY'].strip().lower()
            ccdsfile = os.path.join(largedir, 'ccds',
                                    '{}-ccds.fits'.format(galaxy))
            ccds = fits.getdata(ccdsfile)

            pdb.set_trace()

    # --------------------------------------------------
    # Run the pipeline.
    if args.runbrick:
        sample = fits.getdata(samplefile, 1)

        for gal in sample[1:2]:
            galaxy = gal['GALAXY'].strip().lower()
            diam = 10 * np.ceil(gal['RADIUS'] / PIXSCALE).astype(
                'int16')  # [pixels]

            # Note: zoom is relative to the center of an imaginary brick with
            # dimensions (0, 3600, 0, 3600).
            survey = LegacySurveyData(version='dr2', output_dir=largedir)
            run_brick(None,
                      survey,
                      radec=(gal['RA'], gal['DEC']),
                      blobxy=zip([diam / 2], [diam / 2]),
                      threads=1,
                      zoom=(1800 - diam / 2, 1800 + diam / 2, 1800 - diam / 2,
                            1800 + diam / 2),
                      wise=False,
                      forceAll=True,
                      writePickles=False,
                      do_calibs=False,
                      write_metrics=False,
                      pixPsf=True,
                      splinesky=True,
                      early_coadds=True,
                      stages=['writecat'],
                      ceres=False)

            pdb.set_trace()
Example #10
0
def main():
    """Main routine which parses the optional inputs."""

    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
        description='DECaLS simulations.')
    parser.add_argument('-n',
                        '--nobj',
                        type=long,
                        default=None,
                        metavar='',
                        help='number of objects to simulate (required input)')
    parser.add_argument('-c',
                        '--chunksize',
                        type=long,
                        default=500,
                        metavar='',
                        help='divide NOBJ into CHUNKSIZE chunks')
    parser.add_argument('-b',
                        '--brick',
                        type=str,
                        default='2428p117',
                        metavar='',
                        help='simulate objects in this brick')
    parser.add_argument('-o',
                        '--objtype',
                        type=str,
                        default='STAR',
                        metavar='',
                        help='object type (STAR, ELG, LRG, QSO, LSB)')
    parser.add_argument(
        '-t',
        '--threads',
        type=int,
        default=8,
        metavar='',
        help='number of threads to use when calling The Tractor')
    parser.add_argument('-s',
                        '--seed',
                        type=long,
                        default=None,
                        metavar='',
                        help='random number seed')
    parser.add_argument('-z',
                        '--zoom',
                        nargs=4,
                        type=int,
                        metavar='',
                        help='see runbrick.py; (default is 0 3600 0 3600)')
    parser.add_argument('--rmag-range',
                        nargs=2,
                        type=float,
                        default=(18, 26),
                        metavar='',
                        help='r-band magnitude range')
    parser.add_argument('-v',
                        '--verbose',
                        action='store_true',
                        help='toggle on verbose output')

    args = parser.parse_args()
    if args.nobj is None:
        parser.print_help()
        sys.exit(1)

    # Set the debugging level
    if args.verbose:
        lvl = logging.DEBUG
    else:
        lvl = logging.INFO
    logging.basicConfig(format='%(message)s', level=lvl, stream=sys.stdout)
    log = logging.getLogger('__name__')

    brickname = args.brick
    objtype = args.objtype.upper()
    lobjtype = objtype.lower()

    if objtype == 'LRG':
        log.warning('{} objtype not yet supported!'.format(objtype))
        sys.exit(1)
    elif objtype == 'LSB':
        log.warning('{} objtype not yet supported!'.format(objtype))
        sys.exit(1)
    elif objtype == 'QSO':
        log.warning('{} objtype not yet supported!'.format(objtype))
        sys.exit(1)

    # Determine how many chunks we need
    nobj = args.nobj
    chunksize = args.chunksize
    nchunk = long(np.ceil(nobj / chunksize))

    log.info('Object type = {}'.format(objtype))
    log.info('Number of objects = {}'.format(nobj))
    log.info('Chunksize = {}'.format(chunksize))
    log.info('Number of chunks = {}'.format(nchunk))

    # Optionally zoom into a portion of the brick
    decals = Decals()
    brickinfo = decals.get_brick_by_name(brickname)
    brickwcs = wcs_for_brick(brickinfo)
    W, H, pixscale = brickwcs.get_width(), brickwcs.get_height(
    ), brickwcs.pixel_scale()
    print(W, H, pixscale)

    log.info('Brick = {}'.format(brickname))
    if args.zoom is not None:  # See also runbrick.stage_tims()
        (x0, x1, y0, y1) = args.zoom
        W = x1 - x0
        H = y1 - y0
        brickwcs = brickwcs.get_subimage(x0, y0, W, H)
        log.info('Zoom (pixel boundaries) = {}'.format(args.zoom))
    targetrd = np.array([
        brickwcs.pixelxy2radec(x, y)
        for x, y in [(1, 1), (W, 1), (W, H), (1, H), (1, 1)]
    ])

    radec_center = brickwcs.radec_center()
    log.info('RA, Dec center = {}'.format(radec_center))
    log.info('Brick = {}'.format(brickname))

    # Pack the input parameters into a meta-data table.
    metacat = Table()
    metacat['brickname'] = Column([brickname], dtype='S10')
    metacat['objtype'] = Column([objtype], dtype='S10')
    metacat['nobj'] = Column([args.nobj], dtype='i4')
    metacat['chunksize'] = Column([args.chunksize], dtype='i2')
    metacat['nchunk'] = Column([nchunk], dtype='i2')
    #metacat['RA'] = Column([ra_range],dtype='f8')
    #metacat['DEC'] = Column([dec_range],dtype='f8')
    if args.zoom is None:
        metacat['zoom'] = Column([[0, 3600, 0, 3600]], dtype='i4')
    else:
        metacat['zoom'] = Column([args.zoom], dtype='i4')
    metacat['rmag_range'] = Column([args.rmag_range], dtype='f4')

    # Deal with the random seed
    if args.seed is not None:
        log.info('Random seed = {}'.format(args.seed))
        metacat['seed'] = Column([args.seed], dtype='i4')
    rand = np.random.RandomState(args.seed)
    seeds = rand.random_integers(0, 2**18, nchunk)

    if 'DECALS_SIM_DIR' in os.environ:
        decals_sim_dir = os.getenv('DECALS_SIM_DIR')
    else:
        decals_sim_dir = '.'
    if not os.path.exists(os.path.join(decals_sim_dir, brickname)):
        os.makedirs(os.path.join(decals_sim_dir, brickname))

    metafile = os.path.join(decals_sim_dir, brickname,
                            'metacat-' + brickname + '-' + lobjtype + '.fits')
    log.info('Writing {}'.format(metafile))
    if os.path.isfile(metafile):
        os.remove(metafile)
    metacat.write(metafile)

    # Work in chunks.
    for ichunk in range(nchunk):
        log.info('Working on chunk {:02d}/{:02d}'.format(ichunk + 1, nchunk))
        chunksuffix = '{:02d}'.format(ichunk)

        # There's probably a smarter way to do this
        nobjchunk = np.min((nobj, chunksize))
        if (nchunk > 1) and (ichunk == (nchunk - 1)):
            nobjchunk = np.max((nobjchunk, nobj % ((nchunk - 1) * chunksize)))

        # Build and write out the simulated object catalog.
        simcat = build_simcat(nobjchunk, brickname, brickwcs, metacat,
                              seeds[ichunk])
        simcatfile = os.path.join(
            decals_sim_dir, brickname, 'simcat-' + brickname + '-' + lobjtype +
            '-' + chunksuffix + '.fits')
        log.info('Writing {}'.format(simcatfile))
        if os.path.isfile(simcatfile):
            os.remove(simcatfile)
        simcat.write(simcatfile)

        #       # Use Tractor to just process the blobs containing the simulated sources.
        simdecals = SimDecals(metacat=metacat, simcat=simcat)
        blobxy = zip(simcat['x'], simcat['y'])
        run_brick(brickname,
                  decals=simdecals,
                  outdir=os.path.join(decals_sim_dir, brickname),
                  threads=args.threads,
                  zoom=args.zoom,
                  wise=False,
                  sdssInit=False,
                  forceAll=True,
                  writePickles=False,
                  do_calibs=True,
                  write_metrics=False,
                  pixPsf=True,
                  blobxy=blobxy,
                  early_coadds=False,
                  stages=['writecat'],
                  splinesky=True)

        log.info('Cleaning up...')
        shutil.move(
            os.path.join(decals_sim_dir, brickname, 'tractor', brickname[:3],
                         'tractor-' + brickname + '.fits'),
            os.path.join(
                decals_sim_dir, brickname, 'tractor-' + brickname + '-' +
                lobjtype + '-' + chunksuffix + '.fits'))
        shutil.move(
            os.path.join(decals_sim_dir, brickname, 'coadd', brickname[:3],
                         brickname, 'decals-' + brickname + '-image.jpg'),
            os.path.join(
                decals_sim_dir, brickname, 'qa-' + brickname + '-' + lobjtype +
                '-image-' + chunksuffix + '.jpg'))
        shutil.move(
            os.path.join(decals_sim_dir, brickname, 'coadd', brickname[:3],
                         brickname, 'decals-' + brickname + '-resid.jpg'),
            os.path.join(
                decals_sim_dir, brickname, 'qa-' + brickname + '-' + lobjtype +
                '-resid-' + chunksuffix + '.jpg'))

        shutil.rmtree(os.path.join(decals_sim_dir, brickname, 'coadd'))
        shutil.rmtree(os.path.join(decals_sim_dir, brickname, 'tractor'))
        #shutil.rmtree(os.path.join(decals_sim_dir,brickname,'images'))
        #shutil.rmtree(os.path.join(decals_sim_dir,brickname,'metrics'))

        # Write a log file

    log.info('All done!')
Example #11
0
def main(args=None):
    import os
    import datetime
    import logging
    import numpy as np
    from legacypipe.survey import get_git_version
    from legacypipe.runbrick import (get_parser, get_runbrick_kwargs,
                                     run_brick, NothingToDoError,
                                     RunbrickError)

    print()
    print('mpi-runbrick.py starting at', datetime.datetime.now().isoformat())
    print('legacypipe git version:', get_git_version())
    if args is None:
        print('Command-line args:', sys.argv)
        cmd = 'python'
        for vv in sys.argv:
            cmd += ' {}'.format(vv)
        print(cmd)
    else:
        print('Args:', args)
    print()

    parser = get_parser()
    opt = parser.parse_args(args=args)

    if opt.brick is None and opt.radec is None:
        parser.print_help()
        return -1

    optdict = vars(opt)
    verbose = optdict.pop('verbose')

    survey, kwargs = get_runbrick_kwargs(**optdict)
    if kwargs in [-1, 0]:
        return kwargs
    kwargs.update(command_line=' '.join(sys.argv))

    if verbose == 0:
        lvl = logging.INFO
    else:
        lvl = logging.DEBUG
    global_init(lvl)

    # matplotlib config directory, when running in shifter...
    for tag in ['CACHE', 'CONFIG']:
        if not 'XDG_%s_HOME' % tag in os.environ:
            src = os.path.expanduser('~/.%s' % (tag.lower()))
            # Read-only?
            if os.path.isdir(src) and not (os.access(src, os.W_OK)):
                import shutil
                import tempfile
                tempdir = tempfile.mkdtemp(prefix='%s-' % tag.lower())
                os.rmdir(tempdir)
                shutil.copytree(src, tempdir,
                                symlinks=True)  #dirs_exist_ok=True)
                # astropy config file: if XDG_CONFIG_HOME is set,
                # expect to find $XDG_CONFIG_HOME/astropy, not
                # ~/.astropy.
                if tag == 'CONFIG':
                    shutil.copytree(os.path.expanduser('~/.astropy'),
                                    os.path.join(tempdir, 'astropy'),
                                    symlinks=True)  #dirs_exist_ok=True)
                os.environ['XDG_%s_HOME' % tag] = tempdir

    if opt.plots:
        import matplotlib
        matplotlib.use('Agg')
        import pylab as plt
        plt.figure(figsize=(12, 9))
        plt.subplots_adjust(left=0.07,
                            right=0.99,
                            bottom=0.07,
                            top=0.93,
                            hspace=0.2,
                            wspace=0.05)

    # The "initializer" arg is only available in mpi4py master
    pool = MyMPIPool(initializer=global_init, initargs=(lvl, ))

    u = int(os.environ.get('OMPI_UNIVERSE_SIZE', '0'))
    if u == 0:
        u = int(os.environ.get('MPICH_UNIVERSE_SIZE', '0'))
    if u == 0:
        from mpi4py import MPI
        u = MPI.COMM_WORLD.Get_size()

    print('Booting up MPI pool with', u, 'workers...')
    pool.bootup()
    print('Booted up MPI pool.')
    pool._processes = u
    kwargs.update(pool=pool)

    #pool.map(hello, np.arange(128))

    rtn = -1
    try:
        run_brick(opt.brick, survey, **kwargs)
        rtn = 0
    except NothingToDoError as e:
        print()
        if hasattr(e, 'message'):
            print(e.message)
        else:
            print(e)
        print()
        rtn = 0
    except RunbrickError as e:
        print()
        if hasattr(e, 'message'):
            print(e.message)
        else:
            print(e)
        print()
        rtn = -1

    print('Shutting down MPI pool...')
    pool.shutdown()
    print('Shut down MPI pool')

    return rtn
Example #12
0
def main():
    """Main routine which parses the optional inputs."""

    parser = argparse.ArgumentParser(formatter_class=argparse.
                                     ArgumentDefaultsHelpFormatter,
                                     description='DECaLS simulations.')
    parser.add_argument('-n', '--nobj', type=long, default=None, metavar='', 
                        help='number of objects to simulate (required input)')
    parser.add_argument('-c', '--chunksize', type=long, default=500, metavar='', 
                        help='divide NOBJ into CHUNKSIZE chunks')
    parser.add_argument('-b', '--brick', type=str, default='2428p117', metavar='', 
                        help='simulate objects in this brick')
    parser.add_argument('-o', '--objtype', type=str, default='STAR', metavar='', 
                        help='object type (STAR, ELG, LRG, QSO, LSB)') 
    parser.add_argument('-t', '--threads', type=int, default=8, metavar='', 
                        help='number of threads to use when calling The Tractor')
    parser.add_argument('-s', '--seed', type=long, default=None, metavar='', 
                        help='random number seed')
    parser.add_argument('-z', '--zoom', nargs=4, type=int, metavar='', 
                        help='see runbrick.py; (default is 0 3600 0 3600)')
    parser.add_argument('--rmag-range', nargs=2, type=float, default=(18,25), metavar='', 
                        help='r-band magnitude range')
    parser.add_argument('--test', action='store_true',
                        help='run the test code and write out test images')
    parser.add_argument('-v', '--verbose', action='store_true', 
                        help='toggle on verbose output')

    args = parser.parse_args()
    if args.nobj is None:
        parser.print_help()
        sys.exit(1)

    # Set the debugging level
    if args.verbose:
        lvl = logging.DEBUG
    else:
        lvl = logging.INFO
    logging.basicConfig(format='%(message)s',level=lvl,stream=sys.stdout)
    log = logging.getLogger('__name__')

    brickname = args.brick
    objtype = args.objtype.upper()
    lobjtype = objtype.lower()

    if objtype=='LRG':
        log.warning('{} objtype not yet supported!'.format(objtype))
        sys.exit(1)
    elif objtype=='LSB':
        log.warning('{} objtype not yet supported!'.format(objtype))
        sys.exit(1)
    elif objtype=='QSO':
        log.warning('{} objtype not yet supported!'.format(objtype))
        sys.exit(1)

    # Determine how many chunks we need
    nobj = args.nobj
    chunksize = args.chunksize
    nchunk = long(np.ceil(nobj/chunksize))

    log.info('Object type = {}'.format(objtype))
    log.info('Number of objects = {}'.format(nobj))
    log.info('Chunksize = {}'.format(chunksize))
    log.info('Number of chunks = {}'.format(nchunk))

    # Optionally zoom into a portion of the brick
    decals = Decals()
    brickinfo = decals.get_brick_by_name(brickname)
    brickwcs = wcs_for_brick(brickinfo)
    W, H, pixscale = brickwcs.get_width(), brickwcs.get_height(), brickwcs.pixel_scale()
    print(W, H, pixscale)

    log.info('Brick = {}'.format(brickname))
    if args.zoom is not None: # See also runbrick.stage_tims()
        (x0,x1,y0,y1) = args.zoom
        W = x1-x0
        H = y1-y0
        brickwcs = brickwcs.get_subimage(x0, y0, W, H)
        log.info('Zoom (pixel boundaries) = {}'.format(args.zoom))
    targetrd = np.array([brickwcs.pixelxy2radec(x,y) for x,y in
                         [(1,1),(W,1),(W,H),(1,H),(1,1)]])
 
    radec_center = brickwcs.radec_center()
    log.info('RA, Dec center = {}'.format(radec_center))
    log.info('Brick = {}'.format(brickname))

    if args.seed is not None:
        log.info('Random seed = {}'.format(args.seed))

    # Pack the input parameters into a meta-data table.
    metacat = Table()
    metacat['brickname'] = Column([brickname],dtype='S10')
    metacat['objtype'] = Column([objtype],dtype='S10')
    if args.seed is not None:
        metacat['seed'] = Column([args.seed],dtype='i4')
    metacat['nobj'] = Column([args.nobj],dtype='i4')
    metacat['chunksize'] = Column([args.chunksize],dtype='i2')
    metacat['nchunk'] = Column([nchunk],dtype='i2')
    #metacat['RA'] = Column([ra_range],dtype='f8')
    #metacat['DEC'] = Column([dec_range],dtype='f8')
    if args.zoom is None:
        metacat['zoom'] = Column([[0,3600,0,3600]],dtype='i4')
    else:
        metacat['zoom'] = Column([args.zoom],dtype='i4')
    metacat['rmag_range'] = Column([args.rmag_range],dtype='f4')

    print('Hack!')
    decals_sim_dir = './'
    metafile = os.path.join(decals_sim_dir,'metacat-'+brickname+'-'+lobjtype+'.fits')
    log.info('Writing {}'.format(metafile))
    if os.path.isfile(metafile):
        os.remove(metafile)
    metacat.write(metafile)

    # Work in chunks.
    for ichunk in range(nchunk):
        log.info('Working on chunk {:02d}/{:02d}'.format(ichunk+1,nchunk))
        chunksuffix = '{:02d}'.format(ichunk)

        # There's probably a smarter way to do this
        nobjchunk = np.min((nobj,chunksize))
        if (nchunk>1) and (ichunk==(nchunk-1)):
            nobjchunk = np.max((nobjchunk,nobj%((nchunk-1)*chunksize)))

        # Build and write out the simulated object catalog.
        simcat = build_simcat(nobjchunk,brickname,brickwcs,metacat)
        simcatfile = os.path.join(decals_sim_dir,'simcat-'+brickname+'-'+
                                  lobjtype+'-'+chunksuffix+'.fits')
        log.info('Writing {}'.format(simcatfile))
        if os.path.isfile(simcatfile):
            os.remove(simcatfile)
        simcat.write(simcatfile)

#       # Use Tractor to just process the blobs containing the simulated sources. 
        if args.test:
            # Test code
            simdecals = SimDecals(metacat=metacat, simcat=simcat, test=True)
            ccdinfo = decals.ccds_touching_wcs(brickwcs)
            sim = SimImage(simdecals, ccdinfo[19])
            tim = sim.get_tractor_image(const2psf=True, radecpoly=targetrd)
            #for ii, ccd in enumerate(ccdinfo):
            #    log.info('Working on CCD {}'.format(ii))
            #    sim = SimImage(simdecals,ccd)
            #    tim = sim.get_tractor_image(const2psf=True, radecpoly=targetrd, test=args.test)
        else:
            simdecals = SimDecals(metacat=metacat,simcat=simcat)
            blobxy = zip(simcat['x'],simcat['y'])
            run_brick(brickname, decals=simdecals, outdir=decals_sim_dir,
                      threads=8, zoom=args.zoom, wise=False, sdssInit=False,
                      forceAll=True, writePickles=False, do_calibs=False,
                      write_metrics=False, pixPsf=False, blobxy=blobxy, 
                      early_coadds=False, stages=['writecat'])
Example #13
0
def main():

    parser = argparse.ArgumentParser()
    parser.add_argument('--build-sample', action='store_true', help='Build the sample.')
    parser.add_argument('--jpg-cutouts', action='store_true', help='Get jpg cutouts from the viewer.')
    parser.add_argument('--ccd-cutouts', action='store_true', help='Get CCD cutouts of each galaxy.')
    parser.add_argument('--runbrick', action='store_true', help='Run the pipeline.')
    parser.add_argument('--build-webpage', action='store_true', help='(Re)build the web content.')
    args = parser.parse_args()

    # Top-level directory
    key = 'LEGACY_SURVEY_LARGE_GALAXIES'
    if key not in os.environ:
        print('Required ${} environment variable not set'.format(key))
        return 0
    largedir = os.getenv(key)
    samplefile = os.path.join(largedir, 'large-galaxies-sample.fits')

    # --------------------------------------------------
    # Build the sample of large galaxies based on the available imaging.
    if args.build_sample:

        # Read the parent catalog.
        cat = read_rc3()
        
        # Create a simple WCS object for each object and find all the CCDs
        # touching that WCS footprint.
        survey = LegacySurveyData(version='dr2') # hack!
        allccds = survey.get_ccds()
        keep = np.concatenate((survey.apply_blacklist(allccds),
                               survey.photometric_ccds(allccds)))
        allccds.cut(keep)

        ccdlist = []
        outcat = []
        for gal in cat:
            galwcs = _simplewcs(gal)

            ccds1 = allccds[ccds_touching_wcs(galwcs, allccds)]
            ccds1 = ccds1[_uniqccds(ccds1)]
            
            if len(ccds1) > 0 and 'g' in ccds1.filter and 'r' in ccds1.filter and 'z' in ccds1.filter:
                print('Found {} CCDs for {}, D(25)={:.4f}'.format(
                    len(ccds1), gal['GALAXY'], gal['RADIUS']))
                
                ccdsfile = os.path.join(largedir, 'ccds', '{}-ccds.fits'.format(gal['GALAXY'].strip().lower()))
                print('  Writing {}'.format(ccdsfile))
                if os.path.isfile(ccdsfile):
                    os.remove(ccdsfile)
                ccds1.writeto(ccdsfile)
                
                ccdlist.append(ccds1)
                if len(outcat) == 0:
                    outcat = gal
                else:
                    outcat = vstack((outcat, gal))
                #if gal['GALAXY'] == 'MCG5-19-36':
                #    pdb.set_trace()

        # Write out the final catalog.
        samplefile = os.path.join(largedir, 'large-galaxies-sample.fits')
        if os.path.isfile(samplefile):
            os.remove(samplefile)
        print('Writing {}'.format(samplefile))
        outcat.write(samplefile)
        print(outcat)

        # Do we need to transfer any of the data to nyx?
        _getfiles(merge_tables(ccdlist))

    # --------------------------------------------------
    # Get data, model, and residual cutouts from the legacysurvey viewer.  Also
    # get thumbnails that are lower resolution.
    if args.jpg_cutouts:
        thumbsize = 100
        sample = fits.getdata(samplefile, 1)
        for gal in sample:
            size = np.ceil(10*gal['RADIUS']/PIXSCALE)
            thumbpixscale = PIXSCALE*size/thumbsize

            #imageurl = 'http://legacysurvey.org/viewer/jpeg-cutout-decals-dr2?ra={:.6f}&dec={:.6f}'.format(gal['RA'], gal['DEC'])+\
            #  '&pixscale={:.3f}&size={:g}'.format(PIXSCALE, size)
            #imagejpg = os.path.join(largedir, 'cutouts', gal['GALAXY'].strip().lower()+'-image.jpg')
            #if os.path.isfile(imagejpg):
            #    os.remove(imagejpg)
            #os.system('wget --continue -O {:s} "{:s}"' .format(imagejpg, imageurl))

            thumburl = 'http://legacysurvey.org/viewer/jpeg-cutout-decals-dr2?ra={:.6f}&dec={:.6f}'.format(gal['RA'], gal['DEC'])+\
              '&pixscale={:.3f}&size={:g}'.format(thumbpixscale, thumbsize)
            thumbjpg = os.path.join(largedir, 'cutouts', gal['GALAXY'].strip().lower()+'-image-thumb.jpg')
            if os.path.isfile(thumbjpg):
                os.remove(thumbjpg)
            os.system('wget --continue -O {:s} "{:s}"' .format(thumbjpg, thumburl))

    # --------------------------------------------------
    # (Re)build the webpage.
    if args.build_webpage:

        # index.html
        html = open(os.path.join(largedir, 'index.html'), 'w')
        html.write('<html><body>\n')
        html.write('<h1>Sample of Large Galaxies</h1>\n')
        html.write('<table border="2" width="30%">\n')
        html.write('<tbody>\n')
        sample = fits.getdata(samplefile, 1)
        for gal in sample:
            # Add coordinates and sizes here.
            galaxy = gal['GALAXY'].strip().lower()
            html.write('<tr>\n')
            html.write('<td><a href="html/{}.html">{}</a></td>\n'.format(galaxy, galaxy.upper()))
            html.write('<td><a href="http://legacysurvey.org/viewer/?ra={:.6f}&dec={:.6f}" target="_blank"><img src=cutouts/{}-image-thumb.jpg alt={} /></a></td>\n'.format(gal['RA'], gal['DEC'], galaxy, galaxy.upper()))
#           html.write('<td><a href="html/{}.html"><img src=cutouts/{}-image-thumb.jpg alt={} /></a></td>\n'.format(galaxy, galaxy, galaxy.upper()))
            html.write('</tr>\n')
        html.write('</tbody>\n')
        html.write('</table>\n')
        html.write('</body></html>\n')
        html.close()

        sys.exit(1)
    
        # individual galaxy pages
        for gal in sample[:3]:
            galaxy = gal['GALAXY'].strip().lower()
            html = open(os.path.join(largedir, 'html/{}.html'.format(galaxy)), 'w')
            html.write('<html><body>\n')
            html.write('<a href=../cutouts/{}.jpg><img src=../cutouts/{}-image.jpg alt={} /></a>\n'.format(galaxy, galaxy, galaxy, galaxy.upper()))
            html.write('</body></html>\n')
            html.close()

    # --------------------------------------------------
    # Get cutouts of all the CCDs for each galaxy.
    if args.ccd_cutouts:
        sample = fits.getdata(samplefile, 1)

        for gal in sample[1:2]:
            galaxy = gal['GALAXY'].strip().lower()
            ccdsfile = os.path.join(largedir, 'ccds', '{}-ccds.fits'.format(galaxy))
            ccds = fits.getdata(ccdsfile)

            pdb.set_trace()

    # --------------------------------------------------
    # Run the pipeline.
    if args.runbrick:
        sample = fits.getdata(samplefile, 1)

        for gal in sample[1:2]:
            galaxy = gal['GALAXY'].strip().lower()
            diam = 10*np.ceil(gal['RADIUS']/PIXSCALE).astype('int16') # [pixels]

            # Note: zoom is relative to the center of an imaginary brick with
            # dimensions (0, 3600, 0, 3600).
            survey = LegacySurveyData(version='dr2', output_dir=largedir)
            run_brick(None, survey, radec=(gal['RA'], gal['DEC']), blobxy=zip([diam/2], [diam/2]), 
                      threads=1, zoom=(1800-diam/2, 1800+diam/2, 1800-diam/2, 1800+diam/2),
                      wise=False, forceAll=True, writePickles=False, do_calibs=False,
                      write_metrics=False, pixPsf=True, splinesky=True, 
                      early_coadds=True, stages=['writecat'], ceres=False)

            pdb.set_trace()
Example #14
0
def main():
    from astrometry.util.ttime import Time

    t0 = Time()

    parser = get_parser()
    parser.set_defaults(wise=False)

    parser.add_argument('expnum', type=int, help='Exposure number')
    parser.add_argument('ccdname', help='CCD name (eg: "N4")')

    opt = parser.parse_args()
    optdict = vars(opt)
    verbose = optdict.pop('verbose')

    import logging
    import sys
    if verbose == 0:
        lvl = logging.INFO
    else:
        lvl = logging.DEBUG
    logging.basicConfig(level=lvl, format='%(message)s', stream=sys.stdout)
    # tractor logging is *soooo* chatty
    logging.getLogger('tractor.engine').setLevel(lvl + 10)

    expnum = optdict.pop('expnum')
    ccdname = optdict.pop('ccdname')
    survey = FakeLegacySurveyData(survey_dir=opt.survey_dir,
                                  output_dir=opt.output_dir,
                                  cache_dir=opt.cache_dir)
    survey.no_kd = False

    ccds = survey.find_ccds(expnum=expnum, ccdname=ccdname)
    if len(ccds) == 0:
        print('Did not find EXPNUM', expnum, 'CCDNAME', ccdname)
        return -1

    # Force the CCDs
    survey.ccds = ccds
    survey.no_kd = True
    ccd = ccds[0]
    print('Found CCD', ccd)

    awcs = survey.get_approx_wcs(ccd)
    ra,dec = awcs.radec_center()
    h,w = awcs.shape
    rr,dd = awcs.pixelxy2radec([1,1,w,w], [1,h,h,1])
    # Rotate RAs to be around RA=180 to avoid wrap-around
    rotra = np.fmod((rr - ra + 180) + 360, 360.)

    # assume default pixscale
    pixscale = 0.262 / 3600

    W = int(np.ceil((rotra.max() - rotra.min()) * np.cos(np.deg2rad(dec))
                    / pixscale))
    H = int(np.ceil((dd.max() - dd.min()) / pixscale))
    print('W, H', W, H)

    optdict.update(survey=survey)
    survey, kwargs = get_runbrick_kwargs(**optdict)

    # Only set W,H if they were not specified (to other than default values) on the command-line
    if opt.width == 3600 and opt.height == 3600:
        kwargs.update(width=W, height=H)
    if opt.radec is None and opt.brick is None:
        kwargs.update(radec=(ra,dec))
    kwargs.update(bands=[ccd.filter])
    print('kwargs:', kwargs)
    run_brick(None, survey, **kwargs)
    print('Finished:', Time()-t0)
Example #15
0
def main():
    # I read a DESI DR8 target catalog, cut to ELGs, then took a narrow
    # r-mag slice around the peak of that distribution;
    # desi/target/catalogs/dr8/0.31.1/targets/main/resolve/targets-dr8-hp-1,5,11,50,55,60,83,84,86,89,91,98,119,155,158,174,186,187,190.fits')
    # Then took the median g and z mags
    # And opened the coadd invvar mags for a random brick in that footprint
    # (0701p000) to find the median per-pixel invvars.

    r = 23.0
    g = 23.4
    z = 22.2

    # Selecting EXPs, the peak of the shapeexp_r was ~ 0.75".
    r_e = 0.75

    # Image properties:

    giv = 77000.
    riv = 27000.
    ziv = 8000.

    # PSF sizes were roughly equal, 1.16, 1.17, 1.19"
    # -> sigma = 1.9 DECam pixels
    psf_sigma = 1.9

    H, W = 1000, 1000

    seed = 42
    np.random.seed(seed)

    ra, dec = 70., 1.
    brick = BrickDuck(ra, dec, 'custom-0700p010')
    wcs = wcs_for_brick(brick, W=W, H=H)
    bands = 'grz'

    tims = []
    for band, iv in zip(bands, [giv, riv, ziv]):
        img = np.zeros((H, W), np.float32)
        tiv = np.zeros_like(img) + iv
        s = psf_sigma**2
        psf = GaussianMixturePSF(1., 0., 0., s, s, 0.)
        twcs = ConstantFitsWcs(wcs)
        sky = ConstantSky(0.)
        photocal = LinearPhotoCal(1., band=band)

        tai = TAITime(None, mjd=55700.)

        tim = tractor.Image(data=img,
                            invvar=tiv,
                            psf=psf,
                            wcs=twcs,
                            sky=sky,
                            photocal=photocal,
                            name='fake %s' % band,
                            time=tai)
        tim.skyver = ('1', '1')
        tim.psfver = ('1', '1')
        tim.plver = '1'
        tim.x0 = tim.y0 = 0
        tim.subwcs = wcs
        tim.psfnorm = 1. / (2. * np.sqrt(np.pi) * psf_sigma)
        tim.galnorm = tim.psfnorm
        tim.propid = '2020A-000'
        tim.band = band
        tim.dq = None
        tim.sig1 = 1. / np.sqrt(iv)
        tim.psf_sigma = psf_sigma
        tim.primhdr = fitsio.FITSHDR()

        tims.append(tim)

    # Simulated catalog
    gflux = NanoMaggies.magToNanomaggies(g)
    rflux = NanoMaggies.magToNanomaggies(r)
    zflux = NanoMaggies.magToNanomaggies(z)
    box = 50
    CX, CY = np.meshgrid(np.arange(box // 2, W, box),
                         np.arange(box // 2, H, box))
    ny, nx = CX.shape
    BA, PHI = np.meshgrid(np.linspace(0.1, 1.0, nx), np.linspace(0., 180., ny))
    cat = []
    for cx, cy, ba, phi in zip(CX.ravel(), CY.ravel(), BA.ravel(),
                               PHI.ravel()):
        #print('x,y %.1f,%.1f, ba %.2f, phi %.2f' % (cx, cy, ba, phi))
        r, d = wcs.pixelxy2radec(cx + 1, cy + 1)
        src = ExpGalaxy(RaDecPos(r, d),
                        NanoMaggies(order=bands, g=gflux, r=rflux, z=zflux),
                        EllipseE.fromRAbPhi(r_e, ba, phi))
        cat.append(src)

    from legacypipe.catalog import prepare_fits_catalog
    TT = fits_table()
    TT.bx = CX.ravel()
    TT.by = CY.ravel()
    TT.ba = BA.ravel()
    TT.phi = PHI.ravel()

    tcat = tractor.Catalog(*cat)
    T2 = prepare_fits_catalog(tcat, None, TT, bands, save_invvars=False)
    T2.writeto('sim-cat.fits')

    tr = Tractor(tims, cat)

    for band, tim in zip(bands, tims):
        mod = tr.getModelImage(tim)
        mod += np.random.standard_normal(
            size=tim.shape) * 1. / tim.getInvError()
        fitsio.write('sim-%s.fits' % band, mod, clobber=True)
        tim.data = mod

    ccds = fits_table()
    ccds.filter = np.array([f for f in bands])
    ccds.ccd_cuts = np.zeros(len(ccds), np.int16)
    ccds.imgfn = np.array([tim.name for tim in tims])
    ccds.propid = np.array(['2020A-000'] * len(ccds))
    ccds.fwhm = np.zeros(len(ccds), np.float32) + psf_sigma * 2.35
    ccds.mjd_obs = np.zeros(len(ccds))
    ccds.camera = np.array(['fake'] * len(ccds))

    survey = FakeLegacySurvey(ccds, tims)

    import logging
    verbose = False
    if verbose == 0:
        lvl = logging.INFO
    else:
        lvl = logging.DEBUG
    logging.basicConfig(level=lvl, format='%(message)s', stream=sys.stdout)
    # tractor logging is *soooo* chatty
    logging.getLogger('tractor.engine').setLevel(lvl + 10)

    run_brick(None,
              survey,
              radec=(ra, dec),
              width=W,
              height=H,
              do_calibs=False,
              gaia_stars=False,
              large_galaxies=False,
              tycho_stars=False,
              forceall=True,
              outliers=False)  #, stages=['image_coadds'])
Example #16
0
def main():
    from astrometry.util.ttime import Time

    t0 = Time()

    parser = get_parser()
    parser.set_defaults(wise=False)

    #hybridPsf=True, normalizePsf=True, rex=True, splinesky=True,
    #gaia_stars=True, wise=False, ceres=False,

    parser.add_argument('expnum', type=int, help='Exposure number')
    parser.add_argument('ccdname', help='CCD name (eg: "N4")')

    opt = parser.parse_args()
    optdict = vars(opt)
    verbose = optdict.pop('verbose')

    expnum = optdict.pop('expnum')
    ccdname = optdict.pop('ccdname')

    #print('optdict:', optdict)

    survey = FakeLegacySurveyData(survey_dir=opt.survey_dir,
                                  output_dir=opt.output_dir,
                                  cache_dir=opt.cache_dir)
    survey.no_kd = False

    ccds = survey.find_ccds(expnum=expnum, ccdname=ccdname)
    if len(ccds) == 0:
        print('Did not find EXPNUM', expnum, 'CCDNAME', ccdname)
        return -1

    # Force the CCDs
    survey.ccds = ccds
    survey.no_kd = True

    ccd = ccds[0]
    print('Found CCD', ccd)

    awcs = survey.get_approx_wcs(ccd)
    ra, dec = awcs.radec_center()
    h, w = awcs.shape
    rr, dd = awcs.pixelxy2radec([1, 1, w, w], [1, h, h, 1])
    # Rotate RAs to be around RA=180 to avoid wrap-around
    rotra = np.fmod((rr - ra + 180) + 360, 360.)

    # assume default pixscale
    pixscale = 0.262 / 3600

    W = int(
        np.ceil(
            (rotra.max() - rotra.min()) * np.cos(np.deg2rad(dec)) / pixscale))
    H = int(np.ceil((dd.max() - dd.min()) / pixscale))
    print('W, H', W, H)

    optdict.update(survey=survey)

    survey, kwargs = get_runbrick_kwargs(**optdict)

    kwargs.update(radec=(ra, dec), width=W, height=H, bands=[ccd.filter])

    #if opt.brick is None and opt.radec is None:

    run_brick(None, survey, **kwargs)

    #hybridPsf=True, normalizePsf=True, rex=True, splinesky=True,
    #gaia_stars=True, wise=False, ceres=False,

    print('Finished:', Time() - t0)
Example #17
0
    print('Read', len(stars), 'PS1 stars')

    brickname = ('custom-%06i%s%05i' %
                 (int(1000*rc), 'm' if dc < 0 else 'p', int(1000*np.abs(dc))))

    outfn = survey.find_file('tractor', brick=brickname, output=True)
    print('Output catalog:', outfn)

    if not os.path.exists(outfn):
        run_brick(None, survey, radec=(rc,dc),
                  width=ccd.height, height=ccd.width,    # CCDs are rotated
                  bands=im.band,
                  wise=False,
                  blobradec=zip(stars.ra, stars.dec),
                  do_calibs=False,
                  write_metrics=True,
                  pixPsf=True,
                  constant_invvar=True,
                  ceres=False,
                  splinesky=True,
                  coadd_bw=True,
                  forceAll=True,
                  writePickles=False)

    print('Reading', outfn)
    cat = fits_table(outfn)
    primhdr = fitsio.read_header(outfn)
    
    iband = survey.index_of_band(im.band)
    cat.flux = cat.decam_flux[:, iband]
    cat.apflux = cat.decam_apflux[:, iband, :]
    cat.mag = -2.5 * (np.log10(cat.flux) - 9.)
Example #18
0
from legacypipe.runbrick import run_brick
from legacypipe.survey import LegacySurveyData
'''
run_brick('1273p255', LegacySurveyData, \
        #outdir = '.',\
        threads=1, \
        zoom="0 500 0 500", \
        wise=False, \
        #force-all=True, \
        hybridPsf=True, writePickles=False, do_calibs=True, \
        write_metrics=False, pixPsf=True, blobxy=None, early_coadds=False, \
        splinesky=True, ceres=False,  \
        plotbase='sim',\
        allbands='ugrizY', stages = ['writecat'], plots = False)
'''
from legacypipe.runbrick import main
#def main(args):
#    run_brick(args)

oytdir = './'
main(args=['--brick', '1102p240', '--zoom', '500', '600', '650', '750',\
                   '--force-all', '--no-write', '--no-wise',\
                               #'--rex', #'--plots',\
                                              '--survey-dir', surveydir,\
                                                             '--outdir', outdir])