Source code for oasislmf.pytools.cdftocsv

#!/usr/bin/env python

import sys
import argparse
from contextlib import ExitStack

from oasislmf.pytools.gul.manager import read_getmodel_stream

[docs] parser = argparse.ArgumentParser( usage='use "%(prog)s --help" for more information', formatter_class=argparse.RawTextHelpFormatter # for multi-line help text )
parser.add_argument('-s', help='skip header (default: False).', default=False, action='store_true', dest='skip_header') parser.add_argument('--run-dir', help='path to the run directory (default: ".")', default='.')
[docs] def run(run_dir, skip_header, file_in=None): """Run cdftocsv command: convert the cdf output from getmodel into csv format. The binary data is read from an input stream, and the csv file is streamed to stdout. Args: run_dir ([str]): Path to the run directory. skip_header ([bool]): If True, does not print the csv header. Raises: ValueError: If the stream type is not 1. """ with ExitStack() as stack: if file_in is None: streams_in = sys.stdin.buffer else: streams_in = stack.enter_context(open(file_in, 'rb')) stream_out = sys.stdout if not skip_header: stream_out.write("event_id,areaperil_id,vulnerability_id,bin_index,prob_to,bin_mean\n") # TODO: try using np.savetxt for better performance for event_id, damagecdf, Nbins, rec in read_getmodel_stream(run_dir, streams_in): lines = print_cdftocsv(event_id, damagecdf, Nbins, rec) stream_out.writelines(lines)
[docs] def main(): kwargs = vars(parser.parse_args()) run(**kwargs)
if __name__ == '__main__': main()