Index: palm/trunk/UTIL/inifor/src/io.f90
===================================================================
--- palm/trunk/UTIL/inifor/src/io.f90	(revision 3262)
+++ palm/trunk/UTIL/inifor/src/io.f90	(revision 3395)
@@ -26,5 +26,13 @@
 ! -----------------
 ! $Id$
-! Removed unnecessary check for output file
+! Added command-line options for configuring the computation of geostrophic
+!     winds (--averagin-mode, --averaging-angle)
+! Added command-line option --input-prefix for setting input file prefixes all
+!     at once
+! Added --debug option for more verbose terminal output
+! Added option-specific *_is_set LOGICALs to indicate invocation from the
+!     command-line 
+! Improved error messages in case of empty file-name strings
+! Improved routine naming
 ! 
 ! 3183 2018-07-27 14:25:55Z suehring
@@ -148,4 +156,13 @@
        INTEGER                            ::  arg_count, i
 
+       cfg % p0_is_set = .FALSE.
+       cfg % ug_is_set = .FALSE.
+       cfg % vg_is_set = .FALSE.
+       cfg % flow_prefix_is_set = .FALSE.
+       cfg % input_prefix_is_set = .FALSE.
+       cfg % radiation_prefix_is_set = .FALSE.
+       cfg % soil_prefix_is_set = .FALSE.
+       cfg % soilmoisture_prefix_is_set = .FALSE.
+
        arg_count = COMMAND_ARGUMENT_COUNT()
        IF (arg_count .GT. 0)  THEN
@@ -170,7 +187,14 @@
              SELECT CASE( TRIM(option) )
 
+             CASE( '--averaging-mode' )
+                CALL get_option_argument( i, arg )
+                cfg % averaging_mode = TRIM(arg)
+
              CASE( '-date', '-d', '--date' )
                 CALL get_option_argument( i, arg )
                 cfg % start_date = TRIM(arg)
+
+             CASE( '--debug' )
+                cfg % debug = .TRUE.
 
              ! Elevation of the PALM-4U domain above sea level
@@ -181,4 +205,5 @@
              ! surface pressure, at z0
              CASE( '-p0', '-r', '--surface-pressure' )
+                cfg % p0_is_set = .TRUE.
                 CALL get_option_argument( i, arg )
                 READ(arg, *) cfg % p0
@@ -186,4 +211,5 @@
              ! geostrophic wind in x direction
              CASE( '-ug', '-u', '--geostrophic-u' )
+                cfg % ug_is_set = .TRUE.
                 CALL get_option_argument( i, arg )
                 READ(arg, *) cfg % ug
@@ -191,4 +217,5 @@
              ! geostrophic wind in y direction
              CASE( '-vg', '-v', '--geostrophic-v' )
+                cfg % vg_is_set = .TRUE.
                 CALL get_option_argument( i, arg )
                 READ(arg, *) cfg % vg
@@ -208,29 +235,42 @@
              CASE( '-hhl', '-l', '--hhl-file' )
                 CALL get_option_argument( i, arg )
-                 cfg % hhl_file = TRIM(arg)
+                cfg % hhl_file = TRIM(arg)
+
+             CASE( '--input-prefix')
+                CALL get_option_argument( i, arg )
+                cfg % input_prefix = TRIM(arg)
+                cfg % input_prefix_is_set = .TRUE.
+   
+             CASE( '-a', '--averaging-angle' )
+                CALL get_option_argument( i, arg )
+                READ(arg, *) cfg % averaging_angle
 
              CASE( '-static', '-t', '--static-driver' )
                 CALL get_option_argument( i, arg )
-                 cfg % static_driver_file = TRIM(arg)
+                cfg % static_driver_file = TRIM(arg)
 
              CASE( '-soil', '-s', '--soil-file')
                 CALL get_option_argument( i, arg )
-                 cfg % soiltyp_file = TRIM(arg)
+                cfg % soiltyp_file = TRIM(arg)
 
              CASE( '--flow-prefix')
                 CALL get_option_argument( i, arg )
-                 cfg % flow_prefix = TRIM(arg)
-
+                cfg % flow_prefix = TRIM(arg)
+                cfg % flow_prefix_is_set = .TRUE.
+   
              CASE( '--radiation-prefix')
                 CALL get_option_argument( i, arg )
-                 cfg % radiation_prefix = TRIM(arg)
-
+                cfg % radiation_prefix = TRIM(arg)
+                cfg % radiation_prefix_is_set = .TRUE.
+   
              CASE( '--soil-prefix')
                 CALL get_option_argument( i, arg )
-                 cfg % soil_prefix = TRIM(arg)
-
+                cfg % soil_prefix = TRIM(arg)
+                cfg % soil_prefix_is_set = .TRUE.
+   
              CASE( '--soilmoisture-prefix')
                 CALL get_option_argument( i, arg )
-                 cfg % soilmoisture_prefix = TRIM(arg)
+                cfg % soilmoisture_prefix = TRIM(arg)
+                cfg % soilmoisture_prefix_is_set = .TRUE.
 
              CASE( '-o', '--output' )
@@ -297,11 +337,11 @@
 
       all_files_present = .TRUE.
-      all_files_present = all_files_present .AND. file_present(cfg % hhl_file)
-      all_files_present = all_files_present .AND. file_present(cfg % namelist_file)
-      all_files_present = all_files_present .AND. file_present(cfg % soiltyp_file)
+      all_files_present = all_files_present .AND. file_present(cfg % hhl_file, 'HHL')
+      all_files_present = all_files_present .AND. file_present(cfg % namelist_file, 'NAMELIST')
+      all_files_present = all_files_present .AND. file_present(cfg % soiltyp_file, 'SOILTYP')
 
       ! Only check optional static driver file name, if it has been given.
       IF (TRIM(cfg % static_driver_file) .NE. '')  THEN
-         all_files_present = all_files_present .AND. file_present(cfg % static_driver_file)
+         all_files_present = all_files_present .AND. file_present(cfg % static_driver_file, 'static driver')
       END IF
 
@@ -335,16 +375,44 @@
       END SELECT
 
+      SELECT CASE( TRIM(cfg % averaging_mode) )
+      CASE( 'level', 'height')
+      CASE DEFAULT
+         message = "Averaging mode '" // TRIM(cfg % averaging_mode) //&
+                   "' is not supported. " //&
+                   "Please select either 'height' or 'level', " //&
+                   "or omit the --averaging-mode option entirely, which corresponds "//&
+                   "to the latter."
+         CALL abort( 'validate_config', message ) 
+      END SELECT
+
+      IF ( cfg % ug_is_set .NEQV. cfg % vg_is_set )  THEN
+         message = "You specified only one component of the geostrophic " // &
+                   "wind. Please specify either both or none."
+         CALL abort( 'validate_config', message )
+      END IF
 
    END SUBROUTINE validate_config
 
 
-   LOGICAL FUNCTION file_present(filename)
+   LOGICAL FUNCTION file_present(filename, kind)
       CHARACTER(LEN=PATH), INTENT(IN) ::  filename
-
-      INQUIRE(FILE=filename, EXIST=file_present)
-
-      IF (.NOT. file_present)  THEN
-         message = "The given file '" // "' does not exist."
+      CHARACTER(LEN=*), INTENT(IN)    ::  kind
+
+      IF (LEN(TRIM(filename))==0)  THEN
+
+         file_present = .FALSE.
+         message = "No name was given for the " // TRIM(kind) // " file."
          CALL report('file_present', message)
+
+      ELSE
+
+         INQUIRE(FILE=filename, EXIST=file_present)
+
+         IF (.NOT. file_present)  THEN
+            message = "The given " // TRIM(kind) // " file '" //               &
+                      TRIM(filename) // "' does not exist."
+            CALL report('file_present', message)
+         END IF
+
       END IF
 
@@ -388,7 +456,17 @@
 #endif
 
-!
-!------------------------------------------------------------------------------
-!- Section 1: Write global NetCDF attributes
+!------------------------------------------------------------------------------
+!- Section 1: Define NetCDF dimensions and coordinates
+!------------------------------------------------------------------------------
+       nt = SIZE(output_file % time)
+       nx = palm_grid % nx
+       ny = palm_grid % ny
+       nz = palm_grid % nz
+       z0 = palm_grid % z0
+
+
+!
+!------------------------------------------------------------------------------
+!- Section 2: Write global NetCDF attributes
 !------------------------------------------------------------------------------
        CALL date_and_time(DATE=date_string, TIME=time_string, ZONE=zone_string)
@@ -406,17 +484,12 @@
        CALL check(nf90_put_att(ncid, NF90_GLOBAL, 'origin_lat',     TRIM(real_to_str(origin_lat*TO_DEGREES, '(F18.13)'))))
        CALL check(nf90_put_att(ncid, NF90_GLOBAL, 'origin_lon',     TRIM(real_to_str(origin_lon*TO_DEGREES, '(F18.13)'))))
+       ! FIXME: This is the elevation relative to COSMO-DE/D2 sea level and does
+       ! FIXME: not necessarily comply with DHHN2016 (c.f. PALM Input Data
+       ! FIXME: Standard v1.9., origin_z)
+       CALL check(nf90_put_att(ncid, NF90_GLOBAL, 'origin_z',       TRIM(real_to_str(z0, '(F18.13)'))))
        CALL check(nf90_put_att(ncid, NF90_GLOBAL, 'inifor_version', TRIM(VERSION)))
        CALL check(nf90_put_att(ncid, NF90_GLOBAL, 'palm_version',   '--'))
 
 !
-!------------------------------------------------------------------------------
-!- Section 2: Define NetCDF dimensions and coordinates
-!------------------------------------------------------------------------------
-       nt = SIZE(output_file % time)
-       nx = palm_grid % nx
-       ny = palm_grid % ny
-       nz = palm_grid % nz
-       z0 = palm_grid % z0
-
 !
 !------------------------------------------------------------------------------
@@ -618,9 +691,16 @@
        ELSE
 
-          nbuffers = SIZE( group % in_var_list )
+          ! Allocate one input buffer per input_variable. If more quantities
+          ! have to be computed than input variables exist in this group,
+          ! allocate more buffers. For instance, in the thermodynamics group,
+          ! there are three input variabels (PP, T, Qv) and four quantities
+          ! necessart (P, Theta, Rho, qv) for the corresponding output fields
+          ! (p0, Theta, qv, ug, and vg)
+          nbuffers = MAX( group % n_inputs, group % n_output_quantities )
           ALLOCATE( buffer(nbuffers) )
  CALL run_control('time', 'alloc')
           
-          DO ivar = 1, nbuffers
+          ! Read in all input variables, leave extra buffers-if any-untouched.
+          DO ivar = 1, group % n_inputs
 
              input_var => group % in_var_list(ivar)
@@ -628,5 +708,5 @@
              ! Check wheather P or PP is present in input file
              IF (input_var % name == 'P')  THEN
-                input_var % name = TRIM( get_pressure_var(input_file) )
+                input_var % name = TRIM( get_pressure_varname(input_file) )
  CALL run_control('time', 'read')
              END IF
@@ -668,5 +748,5 @@
 !> perturbation, 'PP', and returns the appropriate string.
 !------------------------------------------------------------------------------!
-    CHARACTER(LEN=2) FUNCTION get_pressure_var(input_file) RESULT(var)
+    CHARACTER(LEN=2) FUNCTION get_pressure_varname(input_file) RESULT(var)
        CHARACTER(LEN=*) ::  input_file
        INTEGER          ::  ncid, varid
@@ -692,5 +772,5 @@
        CALL check(nf90_close(ncid))
 
-    END FUNCTION get_pressure_var
+    END FUNCTION get_pressure_varname
 
 
@@ -805,5 +885,5 @@
                                    start=start(1:ndim+1) ) )
 
-       CASE ( 'constant scalar profile' )
+       CASE ( 'constant scalar profile', 'geostrophic', 'internal profile' )
 
           CALL check(nf90_put_var( ncid, var%varid, array(1,1,:),              &
