diff options
author | Aaron LI <aaronly.me@outlook.com> | 2016-11-26 17:45:05 +0800 |
---|---|---|
committer | Aaron LI <aaronly.me@outlook.com> | 2016-11-26 17:45:05 +0800 |
commit | d59bd2b64a75de5a398958d7e147b43edb5bc898 (patch) | |
tree | e565df0e919924debb97ba85e6f681d328d754d3 /bin/fg21sim-download-data | |
parent | 3b2eaa5e704b84c1e85b6cfed14c9b70ff6839e0 (diff) | |
download | fg21sim-d59bd2b64a75de5a398958d7e147b43edb5bc898.tar.bz2 |
Implement CLI tool: bin/fg21sim-download-data
Diffstat (limited to 'bin/fg21sim-download-data')
-rwxr-xr-x | bin/fg21sim-download-data | 50 |
1 files changed, 49 insertions, 1 deletions
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() |