From e9bc2a7758de611f75eb53781af809e91af01189 Mon Sep 17 00:00:00 2001 From: Aaron LI Date: Sat, 27 May 2017 17:11:55 +0800 Subject: Add bin/get-healpix-patch: extract patch from HEALPix map --- bin/get-healpix-patch | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++ setup.py | 7 ++-- 2 files changed, 94 insertions(+), 3 deletions(-) create mode 100755 bin/get-healpix-patch diff --git a/bin/get-healpix-patch b/bin/get-healpix-patch new file mode 100755 index 0000000..8295d0b --- /dev/null +++ b/bin/get-healpix-patch @@ -0,0 +1,90 @@ +#!/usr/bin/env python3 +# +# Copyright (c) 2017 Weitian LI +# MIT license +# + +""" +Extract a patch of sky from the all-sky HEALPix map. +""" + + +import os +import sys +import argparse +import logging + +from reproject import reproject_from_healpix + +from fg21sim.configs import configs +from fg21sim.sky import SkyPatch +from fg21sim.utils import setup_logging +from fg21sim.utils.fits import read_fits_healpix + + +def main(): + parser = argparse.ArgumentParser( + description="Extract a patch from the all-sky HEALPix map") + parser.add_argument("-C", "--clobber", action="store_true", + help="overwrite the existing output files") + parser.add_argument("-c", "--config", dest="config", required=False, + help="fg21sim configuration from which to " + + "obtain the sky patch properties") + parser.add_argument("--center", dest="center", + help="center coordinate of the sky patch; " + + "format: ra,dec; unit: deg") + parser.add_argument("--size", dest="size", + help="size of the sky patch; " + + "format: xsize,ysize; unit: pixel") + parser.add_argument("--pixelsize", dest="pixelsize", type=float, + help="pixel size of the sky patch; unit: arcmin") + parser.add_argument("infile", help="input all-sky HEALPix map") + parser.add_argument("outfile", help="output extracted sky patch") + args = parser.parse_args() + + setup_logging(dict_config=configs.logging) + tool = os.path.basename(sys.argv[0]) + logger = logging.getLogger(tool) + logger.info("COMMAND: {0}".format(" ".join(sys.argv))) + + if args.config: + configs.read_userconfig(args.config) + center = (configs.getn("sky/patch/xcenter"), + configs.getn("sky/patch/ycenter")) # [ deg ] + size = (configs.getn("sky/patch/xsize"), + configs.getn("sky/patch/ysize")) + pixelsize = configs.getn("sky/patch/pixelsize") # [ arcmin ] + elif not all([args.center, args.size, args.pixelsize]): + raise ValueError("--center, --size, and --pixelsize are " + + "required when --config is missing!") + + if args.center: + center = args.center.split(",") + center = (float(center[0]), float(center[1])) + if args.size: + size = args.size.split(",") + size = (int(size[0]), int(size[1])) + if args.pixelsize: + pixelsize = args.pixelsize + + logger.info("patch center: (%.3f, %.3f) [deg]" % center) + logger.info("patch size: (%d, %d) pixels" % size) + logger.info("patch pixel size: %.1f [arcmin]" % pixelsize) + + sky = SkyPatch(size=size, pixelsize=pixelsize, center=center) + logger.info("Read HEALPix map from file: %s" % args.infile) + hpdata, hpheader = read_fits_healpix(args.infile) + logger.info("Reprojecting HEALPix map to sky patch ...") + image, __ = reproject_from_healpix( + input_data=(hpdata, hpheader["COORDSYS"]), + output_projection=sky.wcs, shape_out=size) + sky.header = hpheader.copy(strip=True) + sky.header["OBJECT"] = "Sky Patch" + sky.header["EXTNAME"] = "IMAGE" + sky.data = image + sky.write(args.outfile, clobber=args.clobber) + logger.info("Write sky patch to file: %s" % args.outfile) + + +if __name__ == "__main__": + main() diff --git a/setup.py b/setup.py index 626dbb0..6fffb2c 100755 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 # -# Copyright (c) 2016 Weitian LI +# Copyright (c) 2016-2017 Weitian LI # MIT license # # References: @@ -77,12 +77,13 @@ setup( zip_safe=False, scripts=[ "bin/fg21sim", + "bin/fg21sim-download-cluster-data", + "bin/fg21sim-download-snr-data", "bin/fg21sim-webui", + "bin/get-healpix-patch", "bin/healpix2hpx", "bin/hpx2healpix", "bin/zea2healpix", - "bin/fg21sim-download-snr-data", - "bin/fg21sim-download-cluster-data", ], install_requires=[ "numpy", -- cgit v1.2.2