[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]defprint_cdftocsv(event_id,damagecdf,Nbins,rec):"""Print the cdf produced by getmodel to csv file. Note that the input arrays are lists of cdf entries, namely the shape on axis=0 is the number of entries. Args: event_id (int): event_id damagecdf (array-like, damagecdf): damage cdf record Nbins (array-like, int): number of damage bins rec (array-like, rec): cdf record Returns: list[str]: list of csv lines """# TODO: accelerate this with numba when it will support string formatting# number of cdf entries in the input dataNentries=damagecdf.shape[0]# build the csv linescsv_lines=[]foriinrange(Nentries):csv_line_fixed=f"{event_id},{damagecdf[i]['areaperil_id']},{damagecdf[i]['vulnerability_id']},"forjinrange(Nbins[i]):# note that bin index starts from 1 in the csvcsv_lines.append(csv_line_fixed+f"{j+1},{rec[i,j]['prob_to']:8.6f},{rec[i,j]['bin_mean']:8.6f}\n")returncsv_lines
[docs]defrun(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. """withExitStack()asstack:iffile_inisNone:streams_in=sys.stdin.bufferelse:streams_in=stack.enter_context(open(file_in,'rb'))stream_out=sys.stdoutifnotskip_header:stream_out.write("event_id,areaperil_id,vulnerability_id,bin_index,prob_to,bin_mean\n")# TODO: try using np.savetxt for better performanceforevent_id,damagecdf,Nbins,recinread_getmodel_stream(run_dir,streams_in):lines=print_cdftocsv(event_id,damagecdf,Nbins,rec)stream_out.writelines(lines)