generate_bngl_function.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. """
  2. This is free and unencumbered software released into the public domain.
  3. Anyone is free to copy, modify, publish, use, compile, sell, or
  4. distribute this software, either in source code form or as a compiled
  5. binary, for any purpose, commercial or non-commercial, and by any
  6. means.
  7. In jurisdictions that recognize copyright laws, the author or authors
  8. of this software dedicate any and all copyright interest in the
  9. software to the public domain. We make this dedication for the benefit
  10. of the public at large and to the detriment of our heirs and
  11. successors. We intend this dedication to be an overt act of
  12. relinquishment in perpetuity of all present and future rights to this
  13. software under copyright law.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  17. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. For more information, please refer to [http://unlicense.org]
  22. """
  23. import sys
  24. import pandas as pd
  25. import numpy as np
  26. # set this to change the precision of the values printed, it must be a string
  27. DIGITS = '6'
  28. def write_bngl_function(input_fname, param):
  29. # load the input file, make sure that the values are floats
  30. df = pd.read_csv(
  31. input_fname, delim_whitespace=True, comment='#',
  32. names=['time', 'value'], dtype={'time':np.float64, 'value':np.float64})
  33. #print(df['time'])
  34. #print(df['value'])
  35. if df['time'][0] != 0:
  36. print("First time must be 0")
  37. sys.exit(1)
  38. ind = " "
  39. for i in range(1, len(df)):
  40. print(
  41. ind +
  42. "(if((" + param + "<" + str.format('{0:.' + DIGITS + '}', df['time'][i]) + "), " +
  43. "(" + str.format('{0:.' + DIGITS + '}', df['value'][i - 1]) + "), \\")
  44. # last value
  45. print(ind + " (" + str.format('{0:.' + DIGITS + '}', df['value'].iloc[-1]) + ") \\")
  46. print(ind, end='')
  47. for i in range(1, len(df)):
  48. print("))", end = '')
  49. print()
  50. def main():
  51. if len(sys.argv) != 3:
  52. print("Expecting input file as 1st argument and input variable name (usually time) as 2nd argument")
  53. sys.exit(1)
  54. write_bngl_function(sys.argv[1], sys.argv[2])
  55. if __name__ == "__main__":
  56. main()