diff --git a/wetb/hawc2/htc_file.py b/wetb/hawc2/htc_file.py index c53b964174875d68847ef65edf4837ccc620e5c0..9d22ce8d98936858248543d95411dbae3cbce024 100644 --- a/wetb/hawc2/htc_file.py +++ b/wetb/hawc2/htc_file.py @@ -35,6 +35,37 @@ class HTCFile(HTCContents, HTCDefaults): >>> htcfile = HTCFile('htc/test.htc') >>> htcfile.wind.wsp = 10 >>> htcfile.save() + + #--------------------------------------------- + >>> htc = HTCFile(filename=None, modelpath=None) # create minimal htcfile + + #Add section + >>> htc.add_section('hydro') + + #Add subsection + >>> htc.hydro.add_section("hydro_element") + + #Set values + >>> htc.hydro.hydro_element.wave_breaking = [2, 6.28, 1] # or + >>> htc.hydro.hydro_element.wave_breaking = 2, 6.28, 1 + + #Set comments + >>> htc.hydro.hydro_element.wave_breaking.comments = "This is a comment" + + #Access section + >>> hydro_element = htc.hydro.hydro_element #or + >>> hydro_element = htc['hydro.hydro_element'] # or + >>> hydro_element = htc['hydro/hydro_element'] # or + >>> print (hydro_element.wave_breaking) #string represenation + wave_breaking 2 6.28 1; This is a comment + >>> print (hydro_element.wave_breaking.name_) # command + wave_breaking + >>> print (hydro_element.wave_breaking.values) # values + [2, 6.28, 1 + >>> print (hydro_element.wave_breaking.comments) # comments + This is a comment + >>> print (hydro_element.wave_breaking[0]) # first value + 2 """ filename = None @@ -96,9 +127,9 @@ class HTCFile(HTCContents, HTCDefaults): self._add_contents(HTCSection.from_lines(lines)) else: line = HTCLine.from_lines(lines) - self._add_contents(line) if line.name_ == "exit": break + self._add_contents(line) def reset(self): @@ -146,7 +177,7 @@ class HTCFile(HTCContents, HTCDefaults): def __str__(self): self.contents #load - return "".join(self.initial_comments + [c.__str__(1) for c in self]) + return "".join(self.initial_comments + [c.__str__(1) for c in self]+ ["exit;"]) def save(self, filename=None): self.contents #load if not loaded diff --git a/wetb/hawc2/tests/test_htc_file.py b/wetb/hawc2/tests/test_htc_file.py index 9d8e4031e4fb308258f3caeaeac6982ba7067723..5270c1b911f7f31e077be862dc1d73e56420df7e 100644 --- a/wetb/hawc2/tests/test_htc_file.py +++ b/wetb/hawc2/tests/test_htc_file.py @@ -34,7 +34,7 @@ class TestHtcFile(unittest.TestCase): def check_htc_file(self, f): with open(f) as fid: - orglines = fid.readlines() + orglines = fid.read().strip().split("\n") htcfile = HTCFile(f,"../") newlines = str(htcfile).split("\n") @@ -131,6 +131,17 @@ class TestHtcFile(unittest.TestCase): self.assertEqual(htcfile.wind.mann.create_turb_parameters[0], 29.4) self.assertEqual(htcfile.wind.mann.create_turb_parameters[3], 1004) self.assertEqual(htcfile.wind.mann.create_turb_parameters.comments, "L, alfaeps, gamma, seed, highfrq compensation") + + def test_add_section2(self): + htcfile = HTCFile() + htcfile.add_section('hydro') + #self.assertEqual(str(htcfile).strip()[-5:], "exit;") + + htcfile = HTCFile(self.testfilepath + "test.htc") + htcfile.add_section('hydro') + self.assertEqual(str(htcfile).strip()[-5:], "exit;") + + def test_add_mann(self): htcfile = HTCFile()