Source code for oasislmf.pytools.converters.csvtobin.utils.damagebin
importnumpyasnpfromoasislmf.pytools.converters.csvtobin.utils.commonimportread_csv_as_ndarrayfromoasislmf.pytools.converters.dataimportTOOL_INFOfromoasislmf.pytools.gulmc.commonimportVALID_DAMAGE_TYPEfromoasislmf.utils.exceptionsimportOasisExceptiondef_validate(data):fromoasislmf.pytools.converters.csvtobin.managerimportlogger# Check first and last binfirst_bin_index=data[0]["bin_index"]first_bin_from=data[0]["bin_from"]iffirst_bin_from!=0:logger.warning(f"Warning: Lower limit of first bin is not 0. Are you sure this is what you want?: bin_from={first_bin_from}")iffirst_bin_index!=1:raiseOasisException(f"Error: First bin index must be 1, bin_index found: {first_bin_index}")last_bin_to=data[-1]["bin_to"]iflast_bin_to!=1:logger.warning(f"Warning: Upper limit of last bin is not 1. Are you sure this is what you want?: bin_to={last_bin_to}")# Check contiguous bin_indexindices=data["bin_index"]diffs=np.diff(indices)ifnotnp.all(diffs==1):idx=np.where(diffs!=1)[0][0]v1,v2=indices[idx],indices[idx+1]raiseOasisException(f"Error: Non-contiguous bin_indices found in csv. ({v1}, {v2})")# Check interpolation damage values within rangebin_froms=data["bin_from"]bin_tos=data["bin_to"]interpolations=data["interpolation"]mask_low=interpolations<bin_fromsmask_high=interpolations>bin_tosmask_invalid=mask_low|mask_highifnp.any(mask_invalid):bad_rows=np.where(mask_invalid)[0]error_msg="\n".join(f"\tRow {i}: interpolation={interpolations[i]}, bin_from={bin_froms[i]}, bin_to={bin_tos[i]}"foriinbad_rows)raiseOasisException(f"Error: Interpolation damage value outside of range.\n{error_msg}")# Check valid damage_typedamages=data['damage_type']invalid_mask=~np.isin(damages,list(VALID_DAMAGE_TYPE))invalid_indices=np.where(invalid_mask)[0]ifinvalid_indices.size>0:invalid_values=damages[invalid_mask]warning_msg="\n".join(f"Row {i}: damage_type={val}, damage_type must be in {VALID_DAMAGE_TYPE}"fori,valinzip(invalid_indices,invalid_values))logger.warning(f"Error: Invalid damage_type values found:\n{warning_msg}")