Index: palm/trunk/SOURCE/netcdf_interface_mod.f90
===================================================================
--- palm/trunk/SOURCE/netcdf_interface_mod.f90	(revision 1851)
+++ palm/trunk/SOURCE/netcdf_interface_mod.f90	(revision 1957)
@@ -19,5 +19,5 @@
 ! Current revisions:
 ! ------------------
-! 
+! flight module added
 ! 
 ! Former revisions:
@@ -139,5 +139,5 @@
  MODULE netcdf_interface
 
-    USE control_parameters, ONLY: max_masks
+    USE control_parameters, ONLY: max_masks, fl_max, var_fl_max
     USE kinds
 #if defined( __netcdf )
@@ -238,11 +238,12 @@
     CHARACTER(LEN=40) ::  netcdf_data_format_string
 
-    INTEGER(iwp) ::  id_dim_prtnum, id_dim_time_pr, id_dim_time_prt, &
+    INTEGER(iwp) ::  id_dim_prtnum, id_dim_time_fl, id_dim_time_pr, id_dim_time_prt, &
                      id_dim_time_pts, id_dim_time_sp, id_dim_time_ts, id_dim_x_sp, &
-                     id_dim_y_sp, id_dim_zu_sp, id_dim_zw_sp, id_set_pr, &
-                     id_set_prt, id_set_pts, id_set_sp, id_set_ts, id_var_prtnum, &
-                     id_var_rnop_prt, id_var_time_pr, id_var_time_prt, &
+                     id_dim_y_sp, id_dim_zu_sp, id_dim_zw_sp, id_set_fl, id_set_pr, &
+                     id_set_prt, id_set_pts, id_set_sp, id_set_ts, id_var_time_fl, &
+                     id_var_prtnum, id_var_rnop_prt, id_var_time_pr, id_var_time_prt, &
                      id_var_time_pts, id_var_time_sp, id_var_time_ts, id_var_x_sp, &
                      id_var_y_sp, id_var_zu_sp, id_var_zw_sp, nc_stat
+
 
     INTEGER(iwp), DIMENSION(0:1) ::  id_dim_time_xy, id_dim_time_xz, &
@@ -271,8 +272,20 @@
                                         !< compression
 
+    INTEGER(iwp)                 ::  dofl_time_count
     INTEGER(iwp), DIMENSION(10)  ::  id_var_dospx, id_var_dospy
     INTEGER(iwp), DIMENSION(20)  ::  id_var_prt
     INTEGER(iwp), DIMENSION(11)  ::  nc_precision
     INTEGER(iwp), DIMENSION(dopr_norm_num) ::  id_var_norm_dopr
+    
+    INTEGER(iwp), DIMENSION(fl_max) ::  id_dim_x_fl, id_dim_y_fl, id_dim_z_fl
+    INTEGER(iwp), DIMENSION(fl_max) ::  id_var_x_fl, id_var_y_fl, id_var_z_fl
+    
+    CHARACTER (LEN=20), DIMENSION(fl_max*var_fl_max) :: dofl_label
+    CHARACTER (LEN=20), DIMENSION(fl_max*var_fl_max) :: dofl_unit 
+    CHARACTER (LEN=20), DIMENSION(fl_max) :: dofl_dim_label_x
+    CHARACTER (LEN=20), DIMENSION(fl_max) :: dofl_dim_label_y
+    CHARACTER (LEN=20), DIMENSION(fl_max) :: dofl_dim_label_z
+
+    INTEGER(iwp), DIMENSION(fl_max*var_fl_max) :: id_var_dofl    
 
     INTEGER(iwp), DIMENSION(dopts_num,0:10) ::  id_var_dopts
@@ -300,16 +313,19 @@
 
 
-    PUBLIC  domask_unit, dopr_unit, dopts_num, dots_label, dots_max, dots_num, &
-            dots_rad, dots_soil, dots_unit, do2d_unit, do3d_unit, id_set_mask, &
-            id_set_pr, id_set_prt, id_set_pts, id_set_sp, id_set_ts,           &
-            id_set_xy, id_set_xz, id_set_yz, id_set_3d, id_var_domask,         &
-            id_var_dopr, id_var_dopts, id_var_dospx, id_var_dospy,             &
-            id_var_dots, id_var_do2d, id_var_do3d, id_var_norm_dopr,           &
-            id_var_time_mask, id_var_time_pr, id_var_time_pts, id_var_time_sp, &
-            id_var_time_ts, id_var_time_xy, id_var_time_xz, id_var_time_yz,    &
-            id_var_time_3d, nc_stat, netcdf_data_format,                       &
-            netcdf_data_format_string, netcdf_deflate, netcdf_precision,       &
-            output_for_t0
-
+    PUBLIC  dofl_dim_label_x, dofl_dim_label_y, dofl_dim_label_z, dofl_label,  &
+            dofl_time_count, dofl_unit, domask_unit, dopr_unit, dopts_num,     &
+            dots_label, dots_max, dots_num, dots_rad, dots_soil, dots_unit,    &
+            do2d_unit, do3d_unit, id_set_fl, id_set_mask, id_set_pr,           &
+            id_set_prt, id_set_pts, id_set_sp, id_set_ts, id_set_xy, id_set_xz,&
+            id_set_yz, id_set_3d, id_var_domask, id_var_dofl, id_var_dopr,     &
+            id_var_dopts, id_var_dospx, id_var_dospy, id_var_dots, id_var_do2d,&
+            id_var_do3d, id_var_norm_dopr, id_var_time_fl, id_var_time_mask,   &
+            id_var_time_pr, id_var_time_pts, id_var_time_sp, id_var_time_ts,   &
+            id_var_time_xy, id_var_time_xz, id_var_time_yz, id_var_time_3d,    &
+            id_var_x_fl, id_var_y_fl, id_var_z_fl,  nc_stat,                   &
+            netcdf_data_format, netcdf_data_format_string, netcdf_deflate,     &
+            netcdf_precision, output_for_t0
+            
+            
     SAVE
 
@@ -366,5 +382,5 @@
                simulated_time_at_begin, skip_time_data_output_av,              &
                skip_time_do2d_xy, skip_time_do2d_xz, skip_time_do2d_yz,        &
-               skip_time_do3d, topography
+               skip_time_do3d, topography, num_leg, num_var_fl
 
     USE grid_variables,                                                        &
@@ -433,4 +449,5 @@
     INTEGER(iwp) ::  ntime_count                             !< number of time levels found in file
     INTEGER(iwp) ::  nz_old                                  !<
+    INTEGER(iwp) ::  l                                       !<
 
     INTEGER(iwp), SAVE ::  oldmode                           !<
@@ -510,4 +527,6 @@
              CASE ( 'masks' )
                 nc_precision(11) = j
+             CASE ( 'fl' )
+                nc_precision(9) = j
              CASE ( 'all' )
                 nc_precision    = j
@@ -4887,5 +4906,184 @@
           CALL message( 'netcdf_define_header', 'PA0269', 0, 0, 0, 6, 0 )
 
-
+!
+!--    Flight data
+       CASE ( 'fl_new' )
+!
+!--       Define some global attributes of the dataset
+          nc_stat = NF90_PUT_ATT( id_set_fl, NF90_GLOBAL, 'title',             &
+                                  TRIM( run_description_header ) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 249 )
+
+!
+!--       Define time and location coordinates for flight space-time series
+!--       (unlimited dimension)
+!--       Error number must still be set appropriately.
+          CALL netcdf_create_dim( id_set_fl, 'time', NF90_UNLIMITED,           &
+                                  id_dim_time_fl, 250 )
+          CALL netcdf_create_var( id_set_fl, (/ id_dim_time_fl /), 'time',     &
+                                  NF90_DOUBLE, id_var_time_fl, 'seconds', '',  &
+                                  251, 252, 000 )
+
+          DO l = 1, num_leg
+             CALL netcdf_create_dim( id_set_fl, dofl_dim_label_x(l),           &
+                                     NF90_UNLIMITED, id_dim_x_fl(l), 250 )
+             CALL netcdf_create_dim( id_set_fl, dofl_dim_label_y(l),           &
+                                     NF90_UNLIMITED, id_dim_y_fl(l), 250 )
+             CALL netcdf_create_dim( id_set_fl, dofl_dim_label_z(l),           &
+                                     NF90_UNLIMITED, id_dim_z_fl(l), 250 )
+
+             CALL netcdf_create_var( id_set_fl, (/ id_dim_x_fl(l) /),          &
+                                     dofl_dim_label_x(l), NF90_DOUBLE,         &
+                                     id_var_x_fl(l), 'm', '', 251, 252, 000 )
+             CALL netcdf_create_var( id_set_fl, (/ id_dim_y_fl(l) /),          &
+                                     dofl_dim_label_y(l), NF90_DOUBLE,         &
+                                     id_var_y_fl(l), 'm', '', 251, 252, 000 )
+             CALL netcdf_create_var( id_set_fl, (/ id_dim_z_fl(l) /),          &
+                                     dofl_dim_label_z(l), NF90_DOUBLE,         &
+                                     id_var_z_fl(l), 'm', '', 251, 252, 000 )
+          ENDDO
+!
+!--       Define the variables
+          var_list = ';'
+          k = 1
+          DO  l = 1, num_leg
+             DO i = 1, num_var_fl
+
+                CALL netcdf_create_var( id_set_fl, (/ id_dim_time_fl /),       &
+                                        dofl_label(k), nc_precision(9),        &
+                                        id_var_dofl(k),                        &
+                                        TRIM( dofl_unit(k) ),                  &
+                                        TRIM( dofl_label(k) ), 253, 254, 255 )
+
+                k = k + 1
+                
+             ENDDO
+
+          ENDDO
+
+!
+!--       Write the list of variables as global attribute (this is used by
+!--       restart runs)
+          nc_stat = NF90_PUT_ATT( id_set_fl, NF90_GLOBAL, 'VAR_LIST', var_list )
+          CALL netcdf_handle_error( 'netcdf_define_header', 258 )
+
+!
+!--       Leave netCDF define mode
+          nc_stat = NF90_ENDDEF( id_set_fl )
+          CALL netcdf_handle_error( 'netcdf_define_header', 259 )
+
+
+       CASE ( 'fl_ext' )
+
+!
+!--       Get the list of variables and compare with the actual run.
+!--       First var_list_old has to be reset, since GET_ATT does not assign
+!--       trailing blanks.
+          var_list_old = ' '
+          nc_stat = NF90_GET_ATT( id_set_fl, NF90_GLOBAL, 'VAR_LIST',          &
+                                  var_list_old )
+          CALL netcdf_handle_error( 'netcdf_define_header', 260 )
+
+          var_list = ';'
+          i = 1
+          DO  i = 1, num_leg * num_var_fl
+
+             var_list = TRIM( var_list ) // TRIM( dofl_label(i) ) // ';'
+
+          ENDDO
+
+          IF ( TRIM( var_list ) /= TRIM( var_list_old ) )  THEN
+             message_string = 'netCDF file for flight time series ' //         &
+                              'from previous run found,' //                    &
+                              '& but this file cannot be extended due to' //   &
+                              ' variable mismatch.' //                         &
+                              '&New file is created instead.'
+             CALL message( 'define_netcdf_header', 'PA0257', 0, 1, 0, 6, 0 )
+             extend = .FALSE.
+             RETURN
+          ENDIF
+
+!
+!--       Get the id of the time coordinate (unlimited coordinate) and its
+!--       last index on the file. The next time level is dofl_time_count+1.
+!--       The current time must be larger than the last output time
+!--       on the file.
+          nc_stat = NF90_INQ_VARID( id_set_fl, 'time', id_var_time_fl )
+          CALL netcdf_handle_error( 'netcdf_define_header', 261 )
+
+          nc_stat = NF90_INQUIRE_VARIABLE( id_set_fl, id_var_time_fl, &
+                                           dimids = id_dim_time_old )
+          CALL netcdf_handle_error( 'netcdf_define_header', 262 )
+          id_dim_time_fl = id_dim_time_old(1)
+
+          nc_stat = NF90_INQUIRE_DIMENSION( id_set_fl, id_dim_time_fl, &
+                                            len = dofl_time_count )
+          CALL netcdf_handle_error( 'netcdf_define_header', 263 )
+
+          nc_stat = NF90_GET_VAR( id_set_fl, id_var_time_fl,        &
+                                  last_time_coordinate,             &
+                                  start = (/ dofl_time_count /), &
+                                  count = (/ 1 /) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 264 )
+
+          IF ( last_time_coordinate(1) >= simulated_time )  THEN
+             message_string = 'netCDF file for flight-time series ' //      &
+                              'from previous run found,' //                 &
+                              '&but this file cannot be extended becaus' // &
+                              'e the current output time' //                &
+                              '&is less or equal than the last output t' // &
+                              'ime on this file.' //                        &
+                              '&New file is created instead.'
+             CALL message( 'define_netcdf_header', 'PA0258', 0, 1, 0, 6, 0 )
+             dofl_time_count = 0
+             extend = .FALSE.
+             RETURN
+          ENDIF
+
+!
+!--       Dataset seems to be extendable.
+!--       Now get the remaining dimension and variable ids
+          DO l = 1, num_leg
+             nc_stat = NF90_INQ_VARID( id_set_fl, dofl_dim_label_x(l),         &
+                                       id_var_x_fl(l) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 265 )
+             nc_stat = NF90_INQ_VARID( id_set_fl, dofl_dim_label_y(l),         &
+                                       id_var_y_fl(l) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 265 )
+             nc_stat = NF90_INQ_VARID( id_set_fl, dofl_dim_label_z(l),         &
+                                       id_var_z_fl(l) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 265 )
+
+          ENDDO
+
+
+          DO  i = 1, num_leg * num_var_fl
+ 
+            nc_stat = NF90_INQ_VARID( id_set_fl, dofl_label(i), &
+                                       id_var_dofl(i) )
+            CALL netcdf_handle_error( 'netcdf_define_header', 265 )
+
+          ENDDO
+
+!
+!--       Update the title attribute on file
+!--       In order to avoid 'data mode' errors if updated attributes are larger 
+!--       than their original size, NF90_PUT_ATT is called in 'define mode' 
+!--       enclosed by NF90_REDEF and NF90_ENDDEF calls. This implies a possible 
+!--       performance loss due to data copying; an alternative strategy would be 
+!--       to ensure equal attribute size in a job chain. Maybe revise later.
+          nc_stat = NF90_REDEF( id_set_fl )
+          CALL netcdf_handle_error( 'netcdf_define_header', 439 )
+          nc_stat = NF90_PUT_ATT( id_set_fl, NF90_GLOBAL, 'title',             &
+                                  TRIM( run_description_header ) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 267 )
+          nc_stat = NF90_ENDDEF( id_set_fl )
+          CALL netcdf_handle_error( 'netcdf_define_header', 440 )
+          message_string = 'netCDF file for flight-time series ' //            &
+                           'from previous run found.' //                       &
+                           '&This file will be extended.'
+          CALL message( 'define_netcdf_header', 'PA0259', 0, 0, 0, 6, 0 )
+
+          
        CASE DEFAULT
 
