From d59bd2b64a75de5a398958d7e147b43edb5bc898 Mon Sep 17 00:00:00 2001 From: Aaron LI Date: Sat, 26 Nov 2016 17:45:05 +0800 Subject: Implement CLI tool: bin/fg21sim-download-data --- bin/fg21sim-download-data | 50 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) (limited to 'bin/fg21sim-download-data') diff --git a/bin/fg21sim-download-data b/bin/fg21sim-download-data index db432f0..d4d03b4 100755 --- a/bin/fg21sim-download-data +++ b/bin/fg21sim-download-data @@ -7,4 +7,52 @@ Download the required data (e.g., template maps) for simulations. """ -raise NotImplementedError("TODO") +import os +import sys +import argparse +import logging +import json + +from fg21sim.configs import configs +from fg21sim.utils import setup_logging +from fg21sim.utils.download import download_file + + +def main(): + parser = argparse.ArgumentParser( + description="Retrieve required input templates and catalogs") + parser.add_argument("manifest", help="data manifest file") + parser.add_argument("outdir", nargs="?", default=".", + help="output directory to save the downloaded data " + + "(default: .)") + parser.add_argument("-C", "--clobber", action="store_true", + help="overwrite the existing output file") + args = parser.parse_args() + + tool = os.path.basename(sys.argv[0]) + setup_logging(dict_config=configs.logging) + logger = logging.getLogger(tool) + logger.info("COMMAND: {0}".format(" ".join(sys.argv))) + + if not os.path.exists(args.outdir): + os.mkdir(args.outdir) + logger.info("Created output directory: {0}".format(args.outdir)) + + manifest = json.load(open(args.manifest)) + logger.info("Loaded data manifest from file: {0}".format(args.manifest)) + nfile = len(manifest) + for i, fdata in enumerate(manifest): + filename = fdata["filename"] + outfile = os.path.join(args.outdir, filename) + size = fdata["size"] + md5 = fdata["md5"] + url = fdata["url"] + logger.info("[#{0}/{1}] Downloading file: {2} ...".format( + (i+1), nfile, filename)) + download_file(url, outfile=outfile, size=size, md5=md5, + clobber=args.clobber) + logger.info("Finished downloading data!") + + +if __name__ == "__main__": + main() -- cgit v1.2.2