Index: palm/trunk/SOURCE/netcdf_interface_mod.f90
===================================================================
--- palm/trunk/SOURCE/netcdf_interface_mod.f90	(revision 3049)
+++ palm/trunk/SOURCE/netcdf_interface_mod.f90	(revision 3159)
@@ -25,4 +25,7 @@
 ! -----------------
 ! $Id$
+! Added multi agent system
+! 
+! 3049 2018-05-29 13:52:36Z Giersch
 ! Error messages revised
 ! 
@@ -282,4 +285,18 @@
     PRIVATE
 
+    CHARACTER (LEN=16), DIMENSION(13) ::  agt_var_names =                      &
+          (/ 'ag_x            ', 'ag_y            ', 'ag_wind         ',       &
+             'ag_temp         ', 'ag_group        ', 'PM10            ',       &
+             'PM25            ', 'ag_therm_comf   ', 'ag_uv           ',       &
+             'not_used        ', 'not_used        ', 'not_used        ',       &
+             'not_used        ' /)
+
+    CHARACTER (LEN=16), DIMENSION(13) ::  agt_var_units = &
+          (/ 'meters          ', 'meters          ', 'm/s             ',       &
+             'K               ', 'dim_less        ', 'tbd             ',       &
+             'tbd             ', 'tbd             ', 'tbd             ',       &
+             'not_used        ', 'not_used        ', 'not_used        ',       &
+             'not_used        ' /)
+
     INTEGER(iwp), PARAMETER ::  dopr_norm_num = 7, dopts_num = 29, dots_max = 100
 
@@ -377,11 +394,15 @@
     CHARACTER(LEN=40) ::  netcdf_data_format_string
 
-    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_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) ::  id_dim_agtnum, id_dim_prtnum, id_dim_time_agt,            &
+                     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_agt, id_set_fl, id_set_pr, id_set_prt, id_set_pts, &
+                     id_set_sp, id_set_ts, id_var_agtnum, id_var_time_agt,     &
+                     id_var_time_fl, id_var_prtnum, id_var_rnoa_agt,           &
+                     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
 
 
@@ -413,4 +434,5 @@
     INTEGER(iwp)                 ::  dofl_time_count
     INTEGER(iwp), DIMENSION(10)  ::  id_var_dospx, id_var_dospy
+    INTEGER(iwp), DIMENSION(20)  ::  id_var_agt
     INTEGER(iwp), DIMENSION(20)  ::  id_var_prt
     INTEGER(iwp), DIMENSION(11)  ::  nc_precision
@@ -457,11 +479,12 @@
             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, fill_value,                                  & 
-            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,   &
+            do2d_unit, do3d_unit, fill_value, id_set_agt, 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_agt, &
+            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_agt, id_var_time_fl,                 &
+            id_var_time_mask, id_var_time_pr, id_var_rnoa_agt, 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,                   &
@@ -515,10 +538,11 @@
 
     USE control_parameters,                                                    &
-        ONLY:  air_chemistry, averaging_interval, averaging_interval_pr,       &
-               data_output_pr, domask, dopr_n,                                 &
+        ONLY:  agent_time_unlimited, air_chemistry, averaging_interval,        &
+               averaging_interval_pr, data_output_pr, domask, dopr_n,          &
                dopr_time_count, dopts_time_count, dots_time_count,             &
                do2d, do2d_at_begin, do2d_xz_time_count, do3d, do3d_at_begin,   &
                do2d_yz_time_count, dt_data_output_av, dt_do2d_xy, dt_do2d_xz,  &
-               dt_do2d_yz, dt_do3d, mask_size, do2d_xy_time_count,             &
+               dt_do2d_yz, dt_do3d, dt_write_agent_data, mask_size,            &
+               do2d_xy_time_count,                                             &
                do3d_time_count, domask_time_count, end_time, land_surface,     &
                mask_size_l, mask_i, mask_i_global, mask_j, mask_j_global,      &
@@ -1922,4 +1946,118 @@
                            '&This file will be extended.'
           CALL message( 'define_netcdf_header', 'PA0248', 0, 0, 0, 6, 0 )
+
+
+       CASE ( 'ag_new' )
+
+!
+!--       Define some global attributes of the dataset
+          nc_stat = NF90_PUT_ATT( id_set_agt, NF90_GLOBAL, 'title', &
+                                  TRIM( run_description_header ) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 330 )
+!
+!--       Switch for unlimited time dimension
+          IF ( agent_time_unlimited ) THEN
+             CALL netcdf_create_dim( id_set_agt, 'time', NF90_UNLIMITED,       &
+                                     id_dim_time_agt, 331 )
+          ELSE
+             CALL netcdf_create_dim( id_set_agt, 'time',                       &
+                                     INT(end_time/dt_write_agent_data*1.2),    &
+                                     id_dim_time_agt, 331 )
+          ENDIF
+
+          CALL netcdf_create_var( id_set_agt, (/ id_dim_time_agt /), 'time',   &
+                                  NF90_DOUBLE, id_var_time_agt, 'seconds', '', &
+                                  332, 333, 000 )
+!
+!--       netCDF4 allows more than one unlimited dimension
+          CALL netcdf_create_dim( id_set_agt, 'agent_number',            &
+                                  NF90_UNLIMITED, id_dim_agtnum, 334 )
+
+          CALL netcdf_create_var( id_set_agt, (/ id_dim_agtnum /),             &
+                                  'agent_number', NF90_DOUBLE,              &
+                                  id_var_agtnum, 'agent number', '', 335,   &
+                                  336, 000 )
+!
+!--       Define variable which contains the real number of agents in use
+          CALL netcdf_create_var( id_set_agt, (/ id_dim_time_agt /),           &
+                                  'real_num_of_agt', NF90_DOUBLE,              &
+                                  id_var_rnoa_agt, 'agent number', '', 337, &
+                                  338, 000 )
+!
+!--       Define the variables
+          DO  i = 1, 5
+             CALL netcdf_create_var( id_set_agt, (/ id_dim_agtnum,             &
+                                     id_dim_time_agt /), agt_var_names(i),     &
+                                     nc_precision(8), id_var_agt(i),           &
+                                     TRIM( agt_var_units(i) ),                 &
+                                     TRIM( agt_var_names(i) ), 339, 340, 341 )
+
+          ENDDO
+!
+!--       Leave netCDF define mode
+          nc_stat = NF90_ENDDEF( id_set_agt )
+          CALL netcdf_handle_error( 'netcdf_define_header', 342 )
+
+
+!        CASE ( 'ag_ext' )
+! !+?agent extend output for restart runs has to be adapted
+! 
+! !
+! !--       Get the id of the time coordinate (unlimited coordinate) and its
+! !--       last index on the file. The next time level is prt..count+1.
+! !--       The current time must be larger than the last output time
+! !--       on the file.
+!           nc_stat = NF90_INQ_VARID( id_set_agt, 'time', id_var_time_agt )
+!           CALL netcdf_handle_error( 'netcdf_define_header', 343 )
+! 
+!           nc_stat = NF90_INQUIRE_VARIABLE( id_set_agt, id_var_time_agt, &
+!                                            dimids = id_dim_time_old )
+!           CALL netcdf_handle_error( 'netcdf_define_header', 344 )
+!           id_dim_time_agt = id_dim_time_old(1)
+! 
+!           nc_stat = NF90_INQUIRE_DIMENSION( id_set_agt, id_dim_time_agt, &
+!                                             len = agt_time_count )
+!           CALL netcdf_handle_error( 'netcdf_define_header', 345 )
+! 
+!           nc_stat = NF90_GET_VAR( id_set_agt, id_var_time_agt,  &
+!                                   last_time_coordinate,         &
+!                                   start = (/ agt_time_count /), &
+!                                   count = (/ 1 /) )
+!           CALL netcdf_handle_error( 'netcdf_define_header', 346 )
+! 
+!           IF ( last_time_coordinate(1) >= simulated_time )  THEN
+!              message_string = 'netCDF file for agents ' //                  &
+!                               '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', 'PA0265', 0, 1, 0, 6, 0 )
+!              agt_time_count = 0
+!              extend = .FALSE.
+!              RETURN
+!           ENDIF
+! 
+! !
+! !--       Dataset seems to be extendable.
+! !--       Now get the variable ids.
+!           nc_stat = NF90_INQ_VARID( id_set_agt, 'real_num_of_agt', &
+!                                     id_var_rnoa_agt )
+!           CALL netcdf_handle_error( 'netcdf_define_header', 347 )
+! 
+!           DO  i = 1, 17
+! 
+!              nc_stat = NF90_INQ_VARID( id_set_agt, agt_var_names(i), &
+!                                        id_var_prt(i) )
+!              CALL netcdf_handle_error( 'netcdf_define_header', 348 )
+! 
+!           ENDDO
+! 
+!           message_string = 'netCDF file for particles ' // &
+!                            'from previous run found.' //   &
+!                            '&This file will be extended.'
+!           CALL message( 'define_netcdf_header', 'PA0266', 0, 0, 0, 6, 0 )
+          
 
        CASE ( 'xy_new' )
