Index: python/prepare_flexpart.py
===================================================================
--- python/prepare_flexpart.py	(revision ff99eae65dfaeec1221e06572d770ebd48feb3f4)
+++ python/prepare_flexpart.py	(revision 54a8a01d975ad8c2dea60c93439d9bc0ef426132)
@@ -1,9 +1,4 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
-#************************************************************************
-# ToDo AP
-# - wieso start=startm1 wenn basetime = 0 ?  wenn die fluxes nicht mehr
-#   relevant sind? verstehe ich nicht
-#************************************************************************
 #*******************************************************************************
 # @Author: Anne Fouilloux (University of Oslo)
@@ -62,8 +57,9 @@
 import sys
 import socket
+import _config
 
 # software specific classes and modules from flex_extract
 from UioFiles import UioFiles
-from tools import interpret_args_and_control, clean_up
+from tools import clean_up, get_cmdline_arguments, read_ecenv
 from EcFlexpart import EcFlexpart
 
@@ -81,4 +77,5 @@
     sys.path.append(LOCAL_PYTHON_PATH)
 
+
 # ------------------------------------------------------------------------------
 # FUNCTION
@@ -97,10 +94,28 @@
         <nothing>
     '''
-    args, c = interpret_args_and_control()
-    prepare_flexpart(args, c)
+
+    args = get_cmdline_arguments()
+
+    try:
+        c = ControlFile(args.controlfile)
+    except IOError:
+        try:
+            c = ControlFile(LOCAL_PYTHON_PATH + args.controlfile)
+        except IOError:
+            print 'Could not read CONTROL file "' + args.controlfile + '"'
+            print 'Either it does not exist or its syntax is wrong.'
+            print 'Try "' + sys.argv[0].split('/')[-1] + \
+                  ' -h" to print usage information'
+            sys.exit(1)
+
+    env_parameter = read_ecenv(c.ecmwfdatadir + 'python/ECMWF_ENV')
+    c.assign_args_to_control(args, env_parameter)
+    c.assign_envs_to_control(env_parameter)
+    c.check_conditions()
+    prepare_flexpart(args.ppid, c)
 
     return
 
-def prepare_flexpart(args, c):
+def prepare_flexpart(ppid, c):
     '''
     @Description:
@@ -112,6 +127,7 @@
 
     @Input:
-        args: instance of ArgumentParser
-            Contains the commandline arguments from script/program call.
+        ppid: int
+            Contains the ppid number of the current ECMWF job. If it is called
+            from this script, it is "None".
 
         c: instance of class ControlFile
@@ -132,8 +148,8 @@
     '''
 
-    if not args.ppid:
+    if not ppid:
         c.ppid = str(os.getppid())
     else:
-        c.ppid = args.ppid
+        c.ppid = ppid
 
     c.ecapi = ecapi
@@ -148,17 +164,19 @@
                         day=int(c.end_date[6:]))
 
-    # to deaccumulate the fluxes correctly
-    # one day ahead of the start date and
-    # one day after the end date is needed
-    startm1 = start - datetime.timedelta(days=1)
-#    endp1 = end + datetime.timedelta(days=1)
-
-    # get all files with flux data to be deaccumulated
-    inputfiles = UioFiles('*OG_acc_SL*.' + c.ppid + '.*')
-    inputfiles.list_files(c.inputdir)
+    # assign starting date minus 1 day
+    # since for basetime 00 we need the 12 hours upfront
+    # (the day before from 12 UTC to current day 00 UTC)
+    if c.basetime == '00':
+        start = start - datetime.timedelta(days=1)
+
+    print 'Prepare ' + start.strftime("%Y%m%d") + \
+           "/to/" + end.strftime("%Y%m%d")
 
     # create output dir if necessary
     if not os.path.exists(c.outputdir):
         os.makedirs(c.outputdir)
+
+    # get all files with flux data to be deaccumulated
+    inputfiles = UioFiles(c.inputdir, '*OG_acc_SL*.' + c.ppid + '.*')
 
     # deaccumulate the flux data
@@ -167,17 +185,9 @@
     flexpart.deacc_fluxes(inputfiles, c)
 
-    print 'Prepare ' + start.strftime("%Y%m%d") + \
-          "/to/" + end.strftime("%Y%m%d")
-
     # get a list of all files from the root inputdir
-    inputfiles = UioFiles('????__??.*' + c.ppid + '.*')
-    inputfiles.list_files(c.inputdir)
-
-    # produce FLEXPART-ready GRIB files and
-    # process GRIB files -
+    inputfiles = UioFiles(c.inputdir, '????__??.*' + c.ppid + '.*')
+
+    # produce FLEXPART-ready GRIB files and process them -
     # copy/transfer/interpolate them or make them GRIB2
-    if c.basetime == '00':
-        start = startm1
-
     flexpart = EcFlexpart(c, fluxes=False)
     flexpart.create(inputfiles, c)
@@ -187,5 +197,5 @@
     # otherwise delete temporary files
     if int(c.debug) != 0:
-        print 'Temporary files left intact'
+        print '\nTemporary files left intact'
     else:
         clean_up(c)
