From 183e42fcd9365384ddf388c6f4e8c3623c9e01a1 Mon Sep 17 00:00:00 2001
From: madsmpedersen <m@madsp.dk>
Date: Fri, 19 Feb 2016 10:09:25 +0100
Subject: [PATCH] cp1252 instead of utf8 for htc files

---
 wetb/hawc2/htc_contents.py        | 11 ++++++++---
 wetb/hawc2/htc_file.py            | 23 ++++++++++++-----------
 wetb/hawc2/simulation.py          | 23 +++++++++++++----------
 wetb/hawc2/tests/test_htc_file.py |  4 ++++
 4 files changed, 37 insertions(+), 24 deletions(-)

diff --git a/wetb/hawc2/htc_contents.py b/wetb/hawc2/htc_contents.py
index 6b76a14a..821748aa 100644
--- a/wetb/hawc2/htc_contents.py
+++ b/wetb/hawc2/htc_contents.py
@@ -106,7 +106,9 @@ class HTCContents(object):
         return section
 
     def add_line(self, name, values, comments):
-        self._add_contents(HTCLine(name, values, comments))
+        line = HTCLine(name, values, comments)
+        self._add_contents(line)
+        return line
 
 
 
@@ -333,9 +335,12 @@ class HTCDefaults(object):
                             no_grid_points=(4096, 32, 32), box_dimension=(6000, 100, 100),
                             std_scaling=(1, .8, .5)):
         wind = self.add_section('wind')
-        wind.turb_format = (1, "0=none, 1=mann,2=flex")
+        wind.turb_format = 1
         mann = wind.add_section('mann')
-        mann.add_line('create_turb_parameters', [L, ae23, Gamma, seed, int(high_frq_compensation)], "L, alfaeps, gamma, seed, highfrq compensation")
+        if 'create_turb_parameters' in mann:
+            mann.create_turb_parameters.values = [L, ae23, Gamma, seed, int(high_frq_compensation)]
+        else:
+            mann.add_line('create_turb_parameters', [L, ae23, Gamma, seed, int(high_frq_compensation)], "L, alfaeps, gamma, seed, highfrq compensation")
         if filenames is None:
             filenames = ["./turb/turb_wsp%d_s%04d%s.bin" % (self.wind.wsp[0], seed, c) for c in ['u', 'v', 'w']]
         if isinstance(filenames, str):
diff --git a/wetb/hawc2/htc_file.py b/wetb/hawc2/htc_file.py
index 67e629df..5efd49c3 100644
--- a/wetb/hawc2/htc_file.py
+++ b/wetb/hawc2/htc_file.py
@@ -60,7 +60,7 @@ class HTCFile(HTCContents, HTCDefaults):
     def readlines(self, filename):
         self.htc_inputfiles.append(filename)
         htc_lines = []
-        with open(filename, encoding='utf-8') as fid:
+        with open(filename, encoding='cp1252') as fid:
             lines = fid.readlines()
         for l in lines:
             if l.lower().lstrip().startswith('continue_in_file'):
@@ -85,7 +85,7 @@ class HTCFile(HTCContents, HTCDefaults):
             self.filename = filename
         # exist_ok does not exist in Python27
         if not os.path.exists(os.path.dirname(filename)):
-            os.makedirs(os.path.dirname(filename))#, exist_ok=True)
+            os.makedirs(os.path.dirname(filename))  #, exist_ok=True)
         with open(filename, 'w', encoding='utf-8') as fid:
             fid.write(str(self))
 
@@ -96,10 +96,11 @@ class HTCFile(HTCContents, HTCDefaults):
 
     def input_files(self):
         files = self.htc_inputfiles
-        for mb in [self.new_htc_structure[mb] for mb in self.new_htc_structure.keys() if mb.startswith('main_body')]:
-            if "timoschenko_input" in mb:
-                files.append(mb.timoschenko_input.filename[0])
-            files.append(mb.get('external_bladedata_dll', [None, None, None])[2])
+        if 'new_htc_structure' in self:
+            for mb in [self.new_htc_structure[mb] for mb in self.new_htc_structure.keys() if mb.startswith('main_body')]:
+                if "timoschenko_input" in mb:
+                    files.append(mb.timoschenko_input.filename[0])
+                files.append(mb.get('external_bladedata_dll', [None, None, None])[2])
         if 'aero' in self:
             files.append(self.aero.ae_filename[0])
             files.append(self.aero.pc_filename[0])
@@ -146,11 +147,11 @@ class HTCFile(HTCContents, HTCDefaults):
             line = self.get(k)
             if line:
                 files.append(line[index])
-
-        if 'system_eigenanalysis' in self.new_htc_structure:
-            f = self.new_htc_structure.system_eigenanalysis[0]
-            files.append(f)
-            files.append(os.path.join(os.path.dirname(f), 'mode*.dat').replace("\\", "/"))
+        if 'new_htc_structure' in self:
+            if 'system_eigenanalysis' in self.new_htc_structure:
+                f = self.new_htc_structure.system_eigenanalysis[0]
+                files.append(f)
+                files.append(os.path.join(os.path.dirname(f), 'mode*.dat').replace("\\", "/"))
         files.extend(self.res_file_lst())
 
         for key in [k for k in self.contents.keys() if k.startswith("output_at_time")]:
diff --git a/wetb/hawc2/simulation.py b/wetb/hawc2/simulation.py
index 1fbe49b7..2f924301 100644
--- a/wetb/hawc2/simulation.py
+++ b/wetb/hawc2/simulation.py
@@ -122,7 +122,7 @@ class Simulation(object):
 
     def add_additional_input_file(self, file):
         additional_files = self.additional_files()
-        additional_files['input'] = additional_files.get('input', []) + [file]
+        additional_files['input'] = list(set(additional_files.get('input', []) + [file]))
         additional_files_file = os.path.join(self.modelpath, 'additional_files.txt')
         with open(additional_files_file, 'w', encoding='utf-8') as fid:
                 json.dump(additional_files, fid)
@@ -140,7 +140,7 @@ class Simulation(object):
                 dst = os.path.join(self.tmp_modelpath, os.path.relpath(src_file, self.modelpath))
                 # exist_ok does not exist in Python27
                 if not os.path.exists(os.path.dirname(dst)):
-                    os.makedirs(os.path.dirname(dst))#, exist_ok=True)
+                    os.makedirs(os.path.dirname(dst))  #, exist_ok=True)
                 shutil.copy(src_file, dst)
                 if not os.path.isfile(dst) or os.stat(dst).st_size != os.stat(src_file).st_size:
                     print ("error copy ", dst)
@@ -173,7 +173,7 @@ class Simulation(object):
                 dst_file = os.path.join(self.modelpath, os.path.relpath(src_file, self.tmp_modelpath))
                 # exist_ok does not exist in Python27
                 if not os.path.exists(os.path.dirname(dst_file)):
-                    os.makedirs(os.path.dirname(dst_file))#, exist_ok=True)
+                    os.makedirs(os.path.dirname(dst_file))  #, exist_ok=True)
                 if not os.path.isfile(dst_file) or os.path.getmtime(dst_file) != os.path.getmtime(src_file):
                     shutil.copy(src_file, dst_file)
 
@@ -199,10 +199,11 @@ class Simulation(object):
         self.simulationThread.join()
         self.returncode, self.stdout = self.simulationThread.res
         if self.returncode or 'error' in self.stdout.lower():
-            self.errors = (list(set([l for l in self.stdout.split("\n") if 'error' in l.lower()])))
+            self.errors = list(set([l for l in self.stdout.split("\n") if 'error' in l.lower()]))
             self.status = ERROR
         self.is_simulating = False
         self.logFile.update_status()
+        self.errors.extend(list(set(self.logFile.errors)))
         if self.returncode:
             raise Exception("Simulation error:\n" + "\n".join(self.errors))
         elif self.logFile.status != log_file.DONE  or self.errors or self.logFile.errors:
@@ -223,13 +224,15 @@ class Simulation(object):
                 filename = os.path.join(folder, file).replace(os.path.sep, "/")
                 if self.get_confirmation("File missing", "'%s' seems to be missing in the temporary working directory. \n\nDo you want to add it to additional_files.txt" % filename):
                     self.add_additional_input_file(filename)
-                    self.show_message("'%s' is not added to additional_files.txt.\n\nPlease restart the simulation" % filename)
+                    self.show_message("'%s' is now added to additional_files.txt.\n\nPlease restart the simulation" % filename)
         for error in self.errors:
-            m = re.compile(r".*\*\*\* ERROR \*\*\* File '(.*)' does not exist in the (.*) folder").match(error.strip())
-            if m is not None:
-                file, folder = m.groups()
-                confirm_add_additional_file(folder, file)
-                continue
+            for regex in [r".*\*\*\* ERROR \*\*\* File '(.*)' does not exist in the (.*) folder",
+                          r".*\*\*\* ERROR \*\*\* DLL (.*)()"]:
+                m = re.compile(regex).match(error.strip())
+                if m is not None:
+                    file, folder = m.groups()
+                    confirm_add_additional_file(folder, file)
+                    continue
             m = re.compile(r".*\*\*\* ERROR \*\*\* File '(.*)' does not exist in the working directory").match(error.strip())
             if m is not None:
                 file = m.groups()[0]
diff --git a/wetb/hawc2/tests/test_htc_file.py b/wetb/hawc2/tests/test_htc_file.py
index bed55c39..6429b14c 100644
--- a/wetb/hawc2/tests/test_htc_file.py
+++ b/wetb/hawc2/tests/test_htc_file.py
@@ -127,6 +127,8 @@ class TestHtcFile(unittest.TestCase):
     std_scaling\t1.000000 0.800000 0.500000;"""
         for a, b in zip(s.split("\n"), str(htcfile.wind.mann).split("\n")):
             self.assertEqual(a.strip(), b.strip())
+        self.assertEqual(htcfile.wind.turb_format[0], 1)
+        self.assertEqual(htcfile.wind.turb_format.comments, "")
 
 
     def test_sensors(self):
@@ -208,6 +210,8 @@ class TestHtcFile(unittest.TestCase):
         htcfile = HTCFile(self.testfilepath + "./tjul.htc", ".")
         htcfile.save("./temp.htc")
 
+    def test_ansi(self):
+        htcfile = HTCFile(self.testfilepath + "./ansi.htc")
 
 
 
-- 
GitLab