From 60828628f178e0acb6cf9cb2582e8ce8d7021121 Mon Sep 17 00:00:00 2001 From: madsmpedersen <m@madsp.dk> Date: Thu, 5 Nov 2015 13:26:12 +0100 Subject: [PATCH] shearfile.py + test --- README.md | 1 + wetb/hawc2/shearfile.py | 32 +++++++++++++++++++++ wetb/hawc2/tests/test_shearfile.py | 46 ++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 wetb/hawc2/shearfile.py create mode 100644 wetb/hawc2/tests/test_shearfile.py diff --git a/README.md b/README.md index 5798d55..82f8160 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ - [Hawc2io](wetb/hawc2/Hawc2io.py): Read binary, ascii and flex result files - [sel_file](wetb/hawc2/sel_file.py): Read/write *.sel (sensor list) files - [htcfile](wetb/hawc2/htcfile.py): Read/write/manipulate htc files +- [shearfile](wetb/hawc2/shearfile.py): Create user defined shear file - [at_time_file](wetb/hawc2/at_time_file.py): read at output_at_time files diff --git a/wetb/hawc2/shearfile.py b/wetb/hawc2/shearfile.py new file mode 100644 index 0000000..16c33f1 --- /dev/null +++ b/wetb/hawc2/shearfile.py @@ -0,0 +1,32 @@ +''' +Created on 24/04/2014 + +@author: MMPE +''' + +import numpy as np + +def save(filename, y_coordinates, z_coordinates, u=None, v=None, w=None): + shape = (len(z_coordinates), len(y_coordinates)) + vuw = [v, u, w] + for i in range(3): + if vuw[i] is None: + vuw[i] = np.zeros((shape)) + else: + assert vuw[i].shape == shape, (i, vuw[i].shape, shape) + + with open(filename, 'w') as fid: + fid.write(" # autogenerated shear file\n") + fid.write(" %d %d\n" % (len(y_coordinates), len(z_coordinates))) + for i, l in enumerate(['v', 'u', 'w']): + fid.write(" # shear %s component\n " % l) + fid.write("\n ".join([" ".join(["%.10f" % v for v in r ]) for r in vuw[i]])) + fid.write("\n") + for yz, coor in (['y', y_coordinates], ['z', z_coordinates]): + fid.write(" # %s coordinates\n " % yz) + fid.write("\n ".join("%.10f" % v for v in coor)) + fid.write("\n") + + +if __name__ == "__main__": + save("test.dat", [-55, 55], [30, 100, 160] , u=np.array([[0.7, 1, 1.3], [0.7, 1, 1.3]]).T) diff --git a/wetb/hawc2/tests/test_shearfile.py b/wetb/hawc2/tests/test_shearfile.py new file mode 100644 index 0000000..6670e6f --- /dev/null +++ b/wetb/hawc2/tests/test_shearfile.py @@ -0,0 +1,46 @@ +''' +Created on 05/11/2015 + +@author: MMPE +''' +import unittest +from wetb.hawc2 import shearfile +import numpy as np +import os +testfilepath = 'test_files/' +class Test(unittest.TestCase): + + + def test_shearfile(self): + f = testfilepath + "tmp_shearfile.dat" + shearfile.save(f, [-55, 55], [30, 100, 160] , u=np.array([[0.7, 1, 1.3], [0.7, 1, 1.3]]).T) + with open(f) as fid: + self.assertEqual(fid.read(), +""" # autogenerated shear file + 2 3 + # shear v component + 0.0000000000 0.0000000000 + 0.0000000000 0.0000000000 + 0.0000000000 0.0000000000 + # shear u component + 0.7000000000 0.7000000000 + 1.0000000000 1.0000000000 + 1.3000000000 1.3000000000 + # shear w component + 0.0000000000 0.0000000000 + 0.0000000000 0.0000000000 + 0.0000000000 0.0000000000 + # y coordinates + -55.0000000000 + 55.0000000000 + # z coordinates + 30.0000000000 + 100.0000000000 + 160.0000000000 +""") + os.remove(f) + + +if __name__ == "__main__": + #import sys;sys.argv = ['', 'Test.test_shearfile'] + unittest.main() -- GitLab