From 2d56c04d12d1df747e1122329edf58c18fc28eb5 Mon Sep 17 00:00:00 2001
From: Anne Philipp <anne.philipp@univie.ac.at>
Date: Wed, 28 Nov 2018 00:34:34 +0100
Subject: [PATCH] automatic detection of grid and area component formats
 (1/1000 or normal degree format)

---
 source/python/classes/ControlFile.py | 53 ++++++++++++++++++----------
 source/python/classes/EcFlexpart.py  | 16 +++------
 source/python/mods/tools.py          |  2 --
 3 files changed, 40 insertions(+), 31 deletions(-)

diff --git a/source/python/classes/ControlFile.py b/source/python/classes/ControlFile.py
index 6a54c6d..447c299 100644
--- a/source/python/classes/ControlFile.py
+++ b/source/python/classes/ControlFile.py
@@ -326,8 +326,8 @@ class ControlFile(object):
         # check for having at least a starting date
         # otherwise program is not allowed to run
         if not self.start_date:
-            print('start_date specified neither in command line nor \
-                   in CONTROL file ' +  self.controlfile)
+            print('start_date specified neither in command line nor '
+                  'in CONTROL file ' +  self.controlfile)
             print('Try "' + sys.argv[0].split('/')[-1] +
                   ' -h" to print usage information')
             sys.exit(1)
@@ -365,16 +365,6 @@ class ControlFile(object):
             print('Check parameter "LEVEL" or the max level of "LEVELIST"!')
             sys.exit(1)
 
-        # if area was provided (only from commandline)
-        # decompose area into its 4 components
-        if self.area:
-            components = self.area.split('/')
-            # convert float to integer coordinates
-            if '.' in self.area:
-                components = [str(int(float(item) * 1000))
-                              for i, item in enumerate(components)]
-            self.upper, self.left, self.lower, self.right = components
-
         # prepare step list if "/" signs are found
         if '/' in self.step:
             steps = self.step.split('/')
@@ -385,8 +375,8 @@ class ControlFile(object):
                 self.step = ['{:0>3}'.format(i) for i in ilist]
             elif 'to' in self.step.lower() and 'by' not in self.step.lower():
                 my_error(self.mailfail, self.step + ':\n' +
-                         'if "to" is used in steps parameter, \
-                         please use "by" as well')
+                         'if "to" is used in steps parameter, '
+                         'please use "by" as well')
             else:
                 self.step = steps
 
@@ -426,10 +416,10 @@ class ControlFile(object):
         if queue in _config.QUEUES_LIST and \
            not self.gateway or not self.destination or \
            not self.ecuid or not self.ecgid:
-            print('\nEnvironment variables GATEWAY, DESTINATION, ECUID and \
-                   ECGID were not set properly!')
-            print('Please check for existence of file "ECMWF_ENV" in the \
-                   python directory!')
+            print('\nEnvironment variables GATEWAY, DESTINATION, ECUID and '
+                  'ECGID were not set properly!')
+            print('Please check for existence of file "ECMWF_ENV" in the '
+                  'python directory!')
             sys.exit(1)
 
         if self.request != 0:
@@ -484,6 +474,33 @@ class ControlFile(object):
             print('Use default value "12" for flux forecast!')
             self.accmaxstep='12'
 
+        # if area was provided (only from commandline)
+        # decompose area into its 4 components
+        if self.area:
+            components = self.area.split('/')
+            self.upper, self.left, self.lower, self.right = components
+
+        # convert grid and area components to correct format and input
+        if 'N' in self.grid:  # Gaussian output grid
+            self.area = 'G'
+        else:
+            # check on grid format
+            if float(self.grid) / 100. >= 0.5:
+                # grid is defined in 1/1000 degrees; old method
+                self.grid = '{}/{}'.format(float(self.grid) / 1000.,
+                                           float(self.grid) / 1000.)
+                self.area = '{}/{}/{}/{}'.format(float(self.upper) / 1000.,
+                                                 float(self.left) / 1000.,
+                                                 float(self.lower) / 1000.,
+                                                 float(self.right) / 1000.)
+            elif float(self.grid) / 100. < 0.5:
+                # grid is defined in normal degree; new method
+                self.grid = '{}/{}'.format(float(self.grid), float(self.grid))
+                self.area = '{}/{}/{}/{}'.format(float(self.upper),
+                                                 float(self.left),
+                                                 float(self.lower),
+                                                 float(self.right))
+
         return
 
     def check_install_conditions(self):
diff --git a/source/python/classes/EcFlexpart.py b/source/python/classes/EcFlexpart.py
index 0144637..5da789a 100644
--- a/source/python/classes/EcFlexpart.py
+++ b/source/python/classes/EcFlexpart.py
@@ -193,17 +193,8 @@ class EcFlexpart(object):
         # for gaussian grid retrieval
         self.glevelist = '1/to/' + c.level
         self.gaussian = c.gaussian
-
-        if 'N' in c.grid:  # Gaussian output grid
-            self.grid = c.grid
-            self.area = 'G'
-        else:
-            self.grid = '{}/{}'.format(int(c.grid) / 1000., int(c.grid) / 1000.)
-            self.area = '{}/{}/{}/{}'.format(int(c.upper) / 1000.,
-                                             int(c.left) / 1000.,
-                                             int(c.lower) / 1000.,
-                                             int(c.right) / 1000.)
-
+        self.grid = c.grid
+        self.area = c.area
         self.outputfilelist = []
 
 
@@ -1106,6 +1097,9 @@ class EcFlexpart(object):
                 my_error(c.mailfail, 'fort.21 is empty while parameter eta \
                          is set to 1 in CONTROL file')
 
+            # write out all output to log file before starting fortran programm
+            sys.stdout.flush()
+
             # Fortran program creates file fort.15 (with u,v,etadot,t,sp,q)
             p = subprocess.check_call([os.path.join(
                 c.exedir, _config.FORTRAN_EXECUTABLE)], shell=True)
diff --git a/source/python/mods/tools.py b/source/python/mods/tools.py
index 070763b..18b60aa 100644
--- a/source/python/mods/tools.py
+++ b/source/python/mods/tools.py
@@ -106,8 +106,6 @@ def check_filepattern(filename):
         return True
     return False
 
-
-
 def get_cmdline_arguments():
     '''Decomposes the command line arguments and assigns them to variables.
     Apply default values for non mentioned arguments.
-- 
GitLab