1. markolopa
  2. limma

Commits

Marko Loparic  committed 8350722

[scripts] postprocessing of PlantRisk output

  • Participants
  • Parent commits 2e37d72
  • Branches default

Comments (0)

Files changed (2)

File scripts/add_case.py

View file
+import os
+import sys
+
+def add_case(plantrisk_output_dir, case_name):
+    for fname in os.listdir(plantrisk_output_dir):
+        print fname
+        if not fname.endswith('.csv'):
+            continue
+
+        fname_out = fname + '.out'
+
+        i_line = 0
+        path_in = os.path.join(plantrisk_output_dir, fname)
+        path_out = os.path.join(plantrisk_output_dir, fname_out)
+        with open(path_in) as f_in:
+            with open(path_out, 'w') as f_out:
+                while True:
+                
+                    # get first line and guess the separator
+                    line = f_in.readline()
+                    i_line += 1
+                    if len(line) == 0:
+                        break
+
+                    # - first line: get the list separator
+                    # - other lines: replace the scenario
+                    if i_line == 1:
+                        count_comma = line.count(',')
+                        count_dot = line.count('.')
+                        if count_comma > 0 and count_comma > count_dot:
+                            sep = ','
+                        elif count_dot > 0 and count_dot > count_comma:
+                            sep = '.'
+                        line_out = line
+                    else:
+                        if line.strip() == '':
+                            # tolerate empty line
+                            line_out = line
+                        else:
+                            pos = line.find(sep)
+                            if pos == -1:
+                                raise ValueError("List separator '%s' not found"
+                                                 "line number %d of file %s"
+                                                 "(%s)"
+                                                 % (sep, i_line, fname, line))
+                            line_out = case_name + line[pos:]
+
+                    # write line
+                    f_out.write(line_out)
+
+if __name__ == '__main__':
+    add_case(*sys.argv[1:])

File scripts/set_case_as_scenario.py

View file
+import os
+import sys
+
+def add_case(plantrisk_output_dir, case_name):
+    for fname in os.listdir(plantrisk_output_dir):
+        print fname
+        if not fname.endswith('.csv'):
+            continue
+
+        fname_out = fname + '.out'
+
+        i_line = 0
+        path_in = os.path.join(plantrisk_output_dir, fname)
+        path_out = os.path.join(plantrisk_output_dir, fname_out)
+        with open(path_in) as f_in:
+            with open(path_out, 'w') as f_out:
+                while True:
+                
+                    # get first line and guess the separator
+                    line = f_in.readline()
+                    i_line += 1
+                    if len(line) == 0:
+                        break
+
+                    # - first line: get the list separator
+                    # - other lines: replace the scenario
+                    if i_line == 1:
+                        count_comma = line.count(',')
+                        count_dot = line.count('.')
+                        if count_comma > 0 and count_comma > count_dot:
+                            sep = ','
+                        elif count_dot > 0 and count_dot > count_comma:
+                            sep = '.'
+                        line_out = line
+                    else:
+                        if line.strip() == '':
+                            # tolerate empty line
+                            line_out = line
+                        else:
+                            pos = line.find(sep)
+                            if pos == -1:
+                                raise ValueError("List separator '%s' not found"
+                                                 "line number %d of file %s"
+                                                 "(%s)"
+                                                 % (sep, i_line, fname, line))
+                            line_out = case_name + line[pos:]
+
+                    # write line
+                    f_out.write(line_out)
+
+if __name__ == '__main__':
+    add_case(*sys.argv[1:])