Index: palm/trunk/SOURCE/Makefile
===================================================================
--- palm/trunk/SOURCE/Makefile	(revision 1782)
+++ palm/trunk/SOURCE/Makefile	(revision 1783)
@@ -20,5 +20,5 @@
 # Current revisions:
 # ------------------
-# 
+# name change of netcdf routines and respective dependencies
 # 
 # Former revisions:
@@ -256,6 +256,6 @@
 	lpm_write_exchange_statistics.f90 lpm_write_restart_file.f90 \
 	ls_forcing.f90 message.f90 microphysics.f90 modules.f90 mod_kinds.f90 \
-	mod_particle_attributes.f90 netcdf.f90 nudging.f90 package_parin.f90 \
-	palm.f90 parin.f90 plant_canopy_model.f90 pmc_interface.f90 \
+	mod_particle_attributes.f90 netcdf_interface.f90 nudging.f90 \
+	package_parin.f90 palm.f90 parin.f90 plant_canopy_model.f90 pmc_interface.f90 \
 	pmc_client.f90 pmc_general.f90 pmc_handle_communicator.f90 pmc_mpi_wrapper.f90 \
 	pmc_server.f90 \
@@ -335,8 +335,9 @@
 calc_spectra.o: modules.o cpulog.o fft_xy.o mod_kinds.o
 check_for_restart.o: modules.o mod_kinds.o
-check_open.o: modules.o mod_kinds.o mod_particle_attributes.o
-check_parameters.o: modules.o mod_kinds.o land_surface_model.o\
-	plant_canopy_model.o pmc_interface.o radiation_model.o subsidence.o
-close_file.o: modules.o mod_kinds.o
+check_open.o: modules.o mod_kinds.o mod_particle_attributes.o netcdf_interface.o
+check_parameters.o: modules.o mod_kinds.o land_surface_model.o \
+	netcdf_interface.o plant_canopy_model.o pmc_interface.o radiation_model.o \
+	subsidence.o
+close_file.o: modules.o mod_kinds.o netcdf_interface.o
 compute_vpt.o: modules.o mod_kinds.o
 coriolis.o: modules.o mod_kinds.o
@@ -346,13 +347,15 @@
 data_log.o: modules.o mod_kinds.o
 data_output_dvrp.o: modules.o cpulog.o mod_kinds.o
-data_output_mask.o: modules.o cpulog.o mod_kinds.o mod_particle_attributes.o
-data_output_profiles.o: modules.o cpulog.o mod_kinds.o
-data_output_ptseries.o: modules.o cpulog.o mod_kinds.o mod_particle_attributes.o
-data_output_spectra.o: modules.o cpulog.o mod_kinds.o
-data_output_tseries.o: modules.o cpulog.o mod_kinds.o
+data_output_mask.o: modules.o cpulog.o mod_kinds.o mod_particle_attributes.o \
+   netcdf_interface.o
+data_output_profiles.o: modules.o cpulog.o mod_kinds.o netcdf_interface.o
+data_output_ptseries.o: modules.o cpulog.o mod_kinds.o \
+   netcdf_interface.o mod_particle_attributes.o
+data_output_spectra.o: modules.o cpulog.o mod_kinds.o netcdf_interface.o
+data_output_tseries.o: modules.o cpulog.o mod_kinds.o netcdf_interface.o
 data_output_2d.o: modules.o cpulog.o mod_kinds.o mod_particle_attributes.o\
-                  land_surface_model.o radiation_model.o
+   netcdf_interface.o land_surface_model.o radiation_model.o
 data_output_3d.o: modules.o cpulog.o mod_kinds.o mod_particle_attributes.o\
-                  land_surface_model.o
+   netcdf_interface.o land_surface_model.o
 diffusion_e.o: modules.o mod_kinds.o
 diffusion_s.o: modules.o mod_kinds.o
@@ -369,14 +372,15 @@
 fft_xy.o: cuda_fft_interfaces.o modules.o mod_kinds.o singleton.o temperton_fft.o
 flow_statistics.o: modules.o cpulog.o mod_kinds.o land_surface_model.o\
-                   radiation_model.o
+   netcdf_interface.o radiation_model.o
 global_min_max.o: modules.o mod_kinds.o
-header.o: modules.o cpulog.o mod_kinds.o land_surface_model.o\
+header.o: modules.o cpulog.o mod_kinds.o netcdf_interface.o land_surface_model.o\
           plant_canopy_model.o pmc_interface.o radiation_model.o subsidence.o 
 impact_of_latent_heat.o: modules.o mod_kinds.o
 inflow_turbulence.o: modules.o cpulog.o mod_kinds.o
 init_1d_model.o: modules.o mod_kinds.o
-init_3d_model.o: modules.o cpulog.o mod_kinds.o random_function.o advec_ws.o\
-	land_surface_model.o ls_forcing.o lpm_init.o plant_canopy_model.o\
-	radiation_model.o random_generator_parallel.o surface_layer_fluxes.o
+init_3d_model.o: modules.o mod_kinds.o advec_ws.o cpulog.o land_surface_model.o \
+   lpm_init.o ls_forcing.o netcdf_interface.o plant_canopy_model.o \
+   radiation_model.o random_function.o random_generator_parallel.o \
+	surface_layer_fluxes.o
 init_advec.o: modules.o mod_kinds.o
 init_cloud_physics.o: modules.o mod_kinds.o
@@ -384,5 +388,5 @@
 init_dvrp.o: modules.o mod_kinds.o
 init_grid.o: modules.o mod_kinds.o
-init_masks.o: modules.o mod_kinds.o
+init_masks.o: modules.o mod_kinds.o netcdf_interface.o
 init_ocean.o: modules.o eqn_state_seawater.o mod_kinds.o
 init_pegrid.o: modules.o mod_kinds.o
@@ -406,5 +410,5 @@
 	mod_particle_attributes.o
 lpm_data_output_particles.o: modules.o cpulog.o mod_kinds.o \
-	mod_particle_attributes.o
+	mod_particle_attributes.o netcdf_interface.o
 lpm_droplet_collision.o: modules.o cpulog.o lpm_collision_kernels.o \
 	mod_kinds.o mod_particle_attributes.o
@@ -412,10 +416,10 @@
 	mod_kinds.o mod_particle_attributes.o
 lpm_exchange_horiz.o: modules.o cpulog.o lpm_pack_arrays.o mod_kinds.o \
-	mod_particle_attributes.o
+	mod_particle_attributes.o netcdf_interface.o
 lpm_extend_tails.o: modules.o mod_kinds.o mod_particle_attributes.o
 lpm_extend_tail_array.o: modules.o mod_kinds.o mod_particle_attributes.o
 lpm_init.o: modules.o lpm_collision_kernels.o mod_kinds.o \
-	random_function.o mod_particle_attributes.o lpm_exchange_horiz.o \
-	lpm_pack_arrays.o random_generator_parallel.o
+	netcdf_interface.o random_function.o mod_particle_attributes.o \
+	lpm_exchange_horiz.o lpm_pack_arrays.o random_generator_parallel.o
 lpm_init_sgs_tke.o: modules.o mod_kinds.o mod_particle_attributes.o
 lpm_pack_arrays.o: modules.o cpulog.o mod_kinds.o mod_particle_attributes.o
@@ -433,5 +437,5 @@
 mod_kinds.o: mod_kinds.f90
 mod_particle_attributes.o: mod_particle_attributes.f90 mod_kinds.o
-netcdf.o: modules.o mod_kinds.o land_surface_model.o
+netcdf_interface.o: netcdf_interface.f90 modules.o mod_kinds.o land_surface_model.o
 nudging.o: modules.o cpulog.o mod_kinds.o
 package_parin.o: modules.o mod_kinds.o land_surface_model.o\
@@ -439,5 +443,6 @@
 palm.o: modules.o cpulog.o ls_forcing.o mod_kinds.o nudging.o\
 	pmc_interface.o surface_layer_fluxes.o
-parin.o: modules.o cpulog.o mod_kinds.o pmc_interface.o progress_bar.o
+parin.o: modules.o cpulog.o mod_kinds.o netcdf_interface.o \
+   pmc_interface.o progress_bar.o
 plant_canopy_model.o: modules.o mod_kinds.o
 pmc_interface.o: modules.o mod_kinds.o pmc_client.o pmc_general.o\
@@ -470,5 +475,5 @@
                   radiation_model.o random_function.o\
                   random_generator_parallel.o
-read_var_list.o: modules.o mod_kinds.o plant_canopy_model.o
+read_var_list.o: modules.o mod_kinds.o netcdf_interface.o plant_canopy_model.o
 run_control.o: modules.o cpulog.o mod_kinds.o
 set_slicer_attributes_dvrp.o: modules.o mod_kinds.o
@@ -497,5 +502,6 @@
 user_additional_routines.o: modules.o mod_kinds.o user_module.o
 user_check_data_output.o: modules.o mod_kinds.o user_module.o
-user_check_data_output_pr.o: modules.o mod_kinds.o user_module.o
+user_check_data_output_pr.o: modules.o mod_kinds.o user_module.o \
+   netcdf_interface.o
 user_check_parameters.o: modules.o mod_kinds.o user_module.o
 user_data_output_2d.o: modules.o mod_kinds.o user_module.o
@@ -506,5 +512,5 @@
 user_dvrp_coltab.o: modules.o mod_kinds.o user_module.o
 user_header.o: modules.o mod_kinds.o user_module.o
-user_init.o: modules.o mod_kinds.o user_module.o
+user_init.o: modules.o mod_kinds.o netcdf_interface.o user_module.o
 user_init_3d_model.o: modules.o mod_kinds.o user_module.o
 user_init_grid.o: modules.o mod_kinds.o user_module.o
@@ -520,8 +526,8 @@
 user_read_restart_data.o: modules.o mod_kinds.o user_module.o
 user_spectra.o: modules.o mod_kinds.o user_module.o
-user_statistics.o: modules.o mod_kinds.o user_module.o
+user_statistics.o: modules.o mod_kinds.o netcdf_interface.o user_module.o
 wall_fluxes.o: modules.o mod_kinds.o
 write_3d_binary.o: modules.o cpulog.o mod_kinds.o land_surface_model.o\
                    radiation_model.o random_function.o\
                    random_generator_parallel.o
-write_var_list.o: modules.o mod_kinds.o plant_canopy_model.o 
+write_var_list.o: modules.o mod_kinds.o netcdf_interface.o plant_canopy_model.o 
Index: palm/trunk/SOURCE/check_open.f90
===================================================================
--- palm/trunk/SOURCE/check_open.f90	(revision 1782)
+++ palm/trunk/SOURCE/check_open.f90	(revision 1783)
@@ -19,5 +19,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! name change of netcdf routines and module + related changes
 !
 ! Former revisions:
@@ -106,5 +106,5 @@
     USE control_parameters,                                                    &
         ONLY:  avs_data_file, coupling_char, data_output_2d_on_each_pe, host,  &
-               message_string, mid, netcdf_data_format, nz_do3d, openfile,     &
+               max_masks, message_string, mid, nz_do3d, openfile,              &
                return_addres, return_username, run_description_header, runnr
 
@@ -117,5 +117,13 @@
     USE kinds
 
-    USE netcdf_control
+#if defined( __netcdf )
+    USE NETCDF
+#endif
+
+    USE netcdf_interface,                                                      &
+        ONLY:  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, nc_stat, &
+               netcdf_create_file, netcdf_data_format, netcdf_define_header,   &
+               netcdf_handle_error, netcdf_open_write_file
 
     USE particle_attributes,                                                   &
@@ -649,10 +657,10 @@
 !
 !--          Open an existing netCDF file for output
-             CALL open_write_netcdf_file( filename, id_set_xy(av), .TRUE., 20 )
+             CALL netcdf_open_write_file( filename, id_set_xy(av), .TRUE., 20 )
 !
 !--          Read header information and set all ids. If there is a mismatch
 !--          between the previuos and the actual run, netcdf_extend is returned
 !--          as .FALSE.
-             CALL define_netcdf_header( 'xy', netcdf_extend, av )
+             CALL netcdf_define_header( 'xy', netcdf_extend, av )
 
 !
@@ -660,5 +668,5 @@
              IF ( .NOT. netcdf_extend )  THEN
                 nc_stat = NF90_CLOSE( id_set_xy(av) )
-                CALL handle_netcdf_error( 'check_open', 21 )
+                CALL netcdf_handle_error( 'check_open', 21 )
                 IF ( myid == 0 )  CALL local_system( 'rm ' // TRIM( filename ) )
 #if defined( __parallel ) && ! defined ( __check )
@@ -675,9 +683,9 @@
 !
 !--          Create a new netCDF output file with requested netCDF format
-             CALL create_netcdf_file( filename, id_set_xy(av), .TRUE., 22 )
+             CALL netcdf_create_file( filename, id_set_xy(av), .TRUE., 22 )
 
 !
 !--          Define the header
-             CALL define_netcdf_header( 'xy', netcdf_extend, av )
+             CALL netcdf_define_header( 'xy', netcdf_extend, av )
 
 !
@@ -711,10 +719,10 @@
 !
 !--          Open an existing netCDF file for output
-             CALL open_write_netcdf_file( filename, id_set_xz(av), .TRUE., 23 )
+             CALL netcdf_open_write_file( filename, id_set_xz(av), .TRUE., 23 )
 !
 !--          Read header information and set all ids. If there is a mismatch
 !--          between the previuos and the actual run, netcdf_extend is returned
 !--          as .FALSE.
-             CALL define_netcdf_header( 'xz', netcdf_extend, av )
+             CALL netcdf_define_header( 'xz', netcdf_extend, av )
 
 !
@@ -722,5 +730,5 @@
              IF ( .NOT. netcdf_extend )  THEN
                 nc_stat = NF90_CLOSE( id_set_xz(av) )
-                CALL handle_netcdf_error( 'check_open', 24 )
+                CALL netcdf_handle_error( 'check_open', 24 )
                 IF ( myid == 0 )  CALL local_system( 'rm ' // TRIM( filename ) )
 #if defined( __parallel ) && ! defined ( __check )
@@ -737,9 +745,9 @@
 !
 !--          Create a new netCDF output file with requested netCDF format
-             CALL create_netcdf_file( filename, id_set_xz(av), .TRUE., 25 )
+             CALL netcdf_create_file( filename, id_set_xz(av), .TRUE., 25 )
 
 !
 !--          Define the header
-             CALL define_netcdf_header( 'xz', netcdf_extend, av )
+             CALL netcdf_define_header( 'xz', netcdf_extend, av )
 
 !
@@ -773,10 +781,10 @@
 !
 !--          Open an existing netCDF file for output
-             CALL open_write_netcdf_file( filename, id_set_yz(av), .TRUE., 26 )
+             CALL netcdf_open_write_file( filename, id_set_yz(av), .TRUE., 26 )
 !
 !--          Read header information and set all ids. If there is a mismatch
 !--          between the previuos and the actual run, netcdf_extend is returned
 !--          as .FALSE.
-             CALL define_netcdf_header( 'yz', netcdf_extend, av )
+             CALL netcdf_define_header( 'yz', netcdf_extend, av )
 
 !
@@ -784,5 +792,5 @@
              IF ( .NOT. netcdf_extend )  THEN
                 nc_stat = NF90_CLOSE( id_set_yz(av) )
-                CALL handle_netcdf_error( 'check_open', 27 )
+                CALL netcdf_handle_error( 'check_open', 27 )
                 IF ( myid == 0 )  CALL local_system( 'rm ' // TRIM( filename ) )
 #if defined( __parallel ) && ! defined ( __check )
@@ -799,9 +807,9 @@
 !
 !--          Create a new netCDF output file with requested netCDF format
-             CALL create_netcdf_file( filename, id_set_yz(av), .TRUE., 28 )
+             CALL netcdf_create_file( filename, id_set_yz(av), .TRUE., 28 )
 
 !
 !--          Define the header
-             CALL define_netcdf_header( 'yz', netcdf_extend, av )
+             CALL netcdf_define_header( 'yz', netcdf_extend, av )
 
 !
@@ -829,10 +837,10 @@
 !
 !--          Open an existing netCDF file for output
-             CALL open_write_netcdf_file( filename, id_set_pr, .FALSE., 29 )
+             CALL netcdf_open_write_file( filename, id_set_pr, .FALSE., 29 )
 !
 !--          Read header information and set all ids. If there is a mismatch
 !--          between the previuos and the actual run, netcdf_extend is returned
 !--          as .FALSE.
-             CALL define_netcdf_header( 'pr', netcdf_extend, 0 )
+             CALL netcdf_define_header( 'pr', netcdf_extend, 0 )
 
 !
@@ -840,5 +848,5 @@
              IF ( .NOT. netcdf_extend )  THEN
                 nc_stat = NF90_CLOSE( id_set_pr )
-                CALL handle_netcdf_error( 'check_open', 30 )
+                CALL netcdf_handle_error( 'check_open', 30 )
                 CALL local_system( 'rm ' // TRIM( filename ) )
              ENDIF
@@ -849,8 +857,8 @@
 !
 !--          Create a new netCDF output file with requested netCDF format
-             CALL create_netcdf_file( filename, id_set_pr, .FALSE., 31 )
+             CALL netcdf_create_file( filename, id_set_pr, .FALSE., 31 )
 !
 !--          Define the header
-             CALL define_netcdf_header( 'pr', netcdf_extend, 0 )
+             CALL netcdf_define_header( 'pr', netcdf_extend, 0 )
 
           ENDIF
@@ -869,10 +877,10 @@
 !
 !--          Open an existing netCDF file for output
-             CALL open_write_netcdf_file( filename, id_set_ts, .FALSE., 32 )
+             CALL netcdf_open_write_file( filename, id_set_ts, .FALSE., 32 )
 !
 !--          Read header information and set all ids. If there is a mismatch
 !--          between the previuos and the actual run, netcdf_extend is returned
 !--          as .FALSE.
-             CALL define_netcdf_header( 'ts', netcdf_extend, 0 )
+             CALL netcdf_define_header( 'ts', netcdf_extend, 0 )
 
 !
@@ -880,5 +888,5 @@
              IF ( .NOT. netcdf_extend )  THEN
                 nc_stat = NF90_CLOSE( id_set_ts )
-                CALL handle_netcdf_error( 'check_open', 33 )
+                CALL netcdf_handle_error( 'check_open', 33 )
                 CALL local_system( 'rm ' // TRIM( filename ) )
              ENDIF
@@ -889,8 +897,8 @@
 !
 !--          Create a new netCDF output file with requested netCDF format
-             CALL create_netcdf_file( filename, id_set_ts, .FALSE., 34 )
+             CALL netcdf_create_file( filename, id_set_ts, .FALSE., 34 )
 !
 !--          Define the header
-             CALL define_netcdf_header( 'ts', netcdf_extend, 0 )
+             CALL netcdf_define_header( 'ts', netcdf_extend, 0 )
 
           ENDIF
@@ -916,10 +924,10 @@
 !
 !--          Open an existing netCDF file for output
-             CALL open_write_netcdf_file( filename, id_set_3d(av), .TRUE., 35 )
+             CALL netcdf_open_write_file( filename, id_set_3d(av), .TRUE., 35 )
 !
 !--          Read header information and set all ids. If there is a mismatch
 !--          between the previuos and the actual run, netcdf_extend is returned
 !--          as .FALSE.
-             CALL define_netcdf_header( '3d', netcdf_extend, av )
+             CALL netcdf_define_header( '3d', netcdf_extend, av )
 
 !
@@ -927,5 +935,5 @@
              IF ( .NOT. netcdf_extend )  THEN
                 nc_stat = NF90_CLOSE( id_set_3d(av) )
-                CALL handle_netcdf_error( 'check_open', 36 )
+                CALL netcdf_handle_error( 'check_open', 36 )
                 IF ( myid == 0 )  CALL local_system( 'rm ' // TRIM( filename ) )
 #if defined( __parallel ) && ! defined ( __check )
@@ -943,9 +951,9 @@
 !
 !--          Create a new netCDF output file with requested netCDF format
-             CALL create_netcdf_file( filename, id_set_3d(av), .TRUE., 37 )
+             CALL netcdf_create_file( filename, id_set_3d(av), .TRUE., 37 )
 
 !
 !--          Define the header
-             CALL define_netcdf_header( '3d', netcdf_extend, av )
+             CALL netcdf_define_header( '3d', netcdf_extend, av )
 
 !
@@ -974,5 +982,5 @@
 !
 !--          Open an existing netCDF file for output
-             CALL open_write_netcdf_file( filename, id_set_sp, .FALSE., 38 )
+             CALL netcdf_open_write_file( filename, id_set_sp, .FALSE., 38 )
 
 !
@@ -980,5 +988,5 @@
 !--          between the previuos and the actual run, netcdf_extend is returned
 !--          as .FALSE.
-             CALL define_netcdf_header( 'sp', netcdf_extend, 0 )
+             CALL netcdf_define_header( 'sp', netcdf_extend, 0 )
 
 !
@@ -986,5 +994,5 @@
              IF ( .NOT. netcdf_extend )  THEN
                 nc_stat = NF90_CLOSE( id_set_sp )
-                CALL handle_netcdf_error( 'check_open', 39 )
+                CALL netcdf_handle_error( 'check_open', 39 )
                 CALL local_system( 'rm ' // TRIM( filename ) )
              ENDIF
@@ -995,8 +1003,8 @@
 !
 !--          Create a new netCDF output file with requested netCDF format
-             CALL create_netcdf_file( filename, id_set_sp, .FALSE., 40 )
+             CALL netcdf_create_file( filename, id_set_sp, .FALSE., 40 )
 !
 !--          Define the header
-             CALL define_netcdf_header( 'sp', netcdf_extend, 0 )
+             CALL netcdf_define_header( 'sp', netcdf_extend, 0 )
 
           ENDIF
@@ -1019,10 +1027,10 @@
 !
 !--          Open an existing netCDF file for output
-             CALL open_write_netcdf_file( filename, id_set_prt, .FALSE., 41 )
+             CALL netcdf_open_write_file( filename, id_set_prt, .FALSE., 41 )
 !
 !--          Read header information and set all ids. If there is a mismatch
 !--          between the previuos and the actual run, netcdf_extend is returned
 !--          as .FALSE.
-             CALL define_netcdf_header( 'pt', netcdf_extend, 0 )
+             CALL netcdf_define_header( 'pt', netcdf_extend, 0 )
 
 !
@@ -1030,5 +1038,5 @@
              IF ( .NOT. netcdf_extend )  THEN
                 nc_stat = NF90_CLOSE( id_set_prt )
-                CALL handle_netcdf_error( 'check_open', 42 )
+                CALL netcdf_handle_error( 'check_open', 42 )
                 CALL local_system( 'rm ' // TRIM( filename ) )
              ENDIF
@@ -1056,9 +1064,9 @@
 !
 !--          Create a new netCDF output file with requested netCDF format
-             CALL create_netcdf_file( filename, id_set_prt, .FALSE., 43 )
+             CALL netcdf_create_file( filename, id_set_prt, .FALSE., 43 )
 
 !
 !--          Define the header
-             CALL define_netcdf_header( 'pt', netcdf_extend, 0 )
+             CALL netcdf_define_header( 'pt', netcdf_extend, 0 )
 
           ENDIF
@@ -1077,10 +1085,10 @@
 !
 !--          Open an existing netCDF file for output
-             CALL open_write_netcdf_file( filename, id_set_pts, .FALSE., 393 )
+             CALL netcdf_open_write_file( filename, id_set_pts, .FALSE., 393 )
 !
 !--          Read header information and set all ids. If there is a mismatch
 !--          between the previuos and the actual run, netcdf_extend is returned
 !--          as .FALSE.
-             CALL define_netcdf_header( 'ps', netcdf_extend, 0 )
+             CALL netcdf_define_header( 'ps', netcdf_extend, 0 )
 
 !
@@ -1088,5 +1096,5 @@
              IF ( .NOT. netcdf_extend )  THEN
                 nc_stat = NF90_CLOSE( id_set_pts )
-                CALL handle_netcdf_error( 'check_open', 394 )
+                CALL netcdf_handle_error( 'check_open', 394 )
                 CALL local_system( 'rm ' // TRIM( filename ) )
              ENDIF
@@ -1097,8 +1105,8 @@
 !
 !--          Create a new netCDF output file with requested netCDF format
-             CALL create_netcdf_file( filename, id_set_pts, .FALSE., 395 )
+             CALL netcdf_create_file( filename, id_set_pts, .FALSE., 395 )
 !
 !--          Define the header
-             CALL define_netcdf_header( 'ps', netcdf_extend, 0 )
+             CALL netcdf_define_header( 'ps', netcdf_extend, 0 )
 
           ENDIF
@@ -1138,5 +1146,5 @@
 !
 !--          Open an existing netCDF file for output
-             CALL open_write_netcdf_file( filename, id_set_mask(mid,av),       &
+             CALL netcdf_open_write_file( filename, id_set_mask(mid,av),       &
                                           .TRUE., 456 )
 !
@@ -1144,5 +1152,5 @@
 !--          between the previuos and the actual run, netcdf_extend is returned
 !--          as .FALSE.
-             CALL define_netcdf_header( 'ma', netcdf_extend, file_id )
+             CALL netcdf_define_header( 'ma', netcdf_extend, file_id )
 
 !
@@ -1150,5 +1158,5 @@
              IF ( .NOT. netcdf_extend )  THEN
                 nc_stat = NF90_CLOSE( id_set_mask(mid,av) )
-                CALL handle_netcdf_error( 'check_open', 457 )
+                CALL netcdf_handle_error( 'check_open', 457 )
                 CALL local_system('rm ' // TRIM( filename ) )
              ENDIF
@@ -1159,8 +1167,8 @@
 !
 !--          Create a new netCDF output file with requested netCDF format
-             CALL create_netcdf_file( filename, id_set_mask(mid,av), .TRUE., 458 )
+             CALL netcdf_create_file( filename, id_set_mask(mid,av), .TRUE., 458 )
 !
 !--          Define the header
-             CALL define_netcdf_header( 'ma', netcdf_extend, file_id )
+             CALL netcdf_define_header( 'ma', netcdf_extend, file_id )
 
           ENDIF
Index: palm/trunk/SOURCE/check_parameters.f90
===================================================================
--- palm/trunk/SOURCE/check_parameters.f90	(revision 1782)
+++ palm/trunk/SOURCE/check_parameters.f90	(revision 1783)
@@ -19,5 +19,6 @@
 ! Current revisions:
 ! -----------------
-! 
+! netcdf variables and module name changed,
+! check of netcdf precision removed (is done in the netcdf module)
 !
 ! Former revisions:
@@ -312,5 +313,7 @@
     USE kinds
     USE model_1d
-    USE netcdf_control
+    USE netcdf_interface,                                                      &
+        ONLY:  dopr_unit, do2d_unit, do3d_unit, netcdf_data_format,            &
+               netcdf_data_format_string
     USE particle_attributes
     USE pegrid
@@ -350,5 +353,4 @@
     
     LOGICAL     ::  found                            !< 
-    LOGICAL     ::  ldum                             !< 
     
     REAL(wp)    ::  gradient                         !< 
@@ -3733,15 +3735,15 @@
     SELECT CASE ( netcdf_data_format )
        CASE ( 1 )
-          output_format_netcdf = 'netCDF classic'
+          netcdf_data_format_string = 'netCDF classic'
        CASE ( 2 )
-          output_format_netcdf = 'netCDF 64bit offset'
+          netcdf_data_format_string = 'netCDF 64bit offset'
        CASE ( 3 )
-          output_format_netcdf = 'netCDF4/HDF5'
+          netcdf_data_format_string = 'netCDF4/HDF5'
        CASE ( 4 )
-          output_format_netcdf = 'netCDF4/HDF5 classic'
+          netcdf_data_format_string = 'netCDF4/HDF5 classic'
        CASE ( 5 )
-          output_format_netcdf = 'parallel netCDF4/HDF5'
+          netcdf_data_format_string = 'parallel netCDF4/HDF5'
        CASE ( 6 )
-          output_format_netcdf = 'parallel netCDF4/HDF5 classic'
+          netcdf_data_format_string = 'parallel netCDF4/HDF5 classic'
 
     END SELECT
@@ -3864,10 +3866,4 @@
     ENDIF
 
-#if ! defined( __check )
-!
-!-- Check netcdf precison
-    ldum = .FALSE.
-    CALL define_netcdf_header( 'ch', ldum, 0 )
-#endif
 !
 !-- Check, whether a constant diffusion coefficient shall be used
Index: palm/trunk/SOURCE/close_file.f90
===================================================================
--- palm/trunk/SOURCE/close_file.f90	(revision 1782)
+++ palm/trunk/SOURCE/close_file.f90	(revision 1783)
@@ -19,5 +19,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! name change of netcdf routines and module + related changes
 ! 
 ! Former revisions:
@@ -65,8 +65,6 @@
 
     USE control_parameters,                                                    &
-        ONLY:  do2d_xz_n, do2d_xy_n, do2d_yz_n, do3d_avs_n,                    &
-               host, max_masks, mid, netcdf_data_format,                       &
-               nz_do3d, openfile, run_description_header,       &
-               z_max_do2d
+        ONLY:  do2d_xz_n, do2d_xy_n, do2d_yz_n, do3d_avs_n, host, max_masks,   &
+               mid, nz_do3d, openfile, run_description_header, z_max_do2d
                 
     USE grid_variables,                                                        &
@@ -78,5 +76,12 @@
     USE kinds
     
-    USE netcdf_control
+#if defined( __netcdf )
+    USE NETCDF
+#endif
+
+    USE netcdf_interface,                                                      &
+        ONLY:  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, nc_stat, &
+               netcdf_data_format, netcdf_handle_error
                 
     USE pegrid                                           
@@ -132,5 +137,5 @@
                 IF ( myid == 0  .OR.  netcdf_data_format > 4 )  THEN
                    nc_stat = NF90_CLOSE( id_set_xy(0) )
-                   CALL handle_netcdf_error( 'close_file', 44 )
+                   CALL netcdf_handle_error( 'close_file', 44 )
                 ENDIF
 
@@ -139,5 +144,5 @@
                 IF ( myid == 0  .OR.  netcdf_data_format > 4 )  THEN
                    nc_stat = NF90_CLOSE( id_set_xz(0) )
-                   CALL handle_netcdf_error( 'close_file', 45 )
+                   CALL netcdf_handle_error( 'close_file', 45 )
                 ENDIF
 
@@ -146,5 +151,5 @@
                 IF ( myid == 0  .OR.  netcdf_data_format > 4 )  THEN
                    nc_stat = NF90_CLOSE( id_set_yz(0) )
-                   CALL handle_netcdf_error( 'close_file', 46 )
+                   CALL netcdf_handle_error( 'close_file', 46 )
                 ENDIF
 
@@ -153,5 +158,5 @@
                 IF ( myid == 0 )  THEN
                    nc_stat = NF90_CLOSE( id_set_pr )
-                   CALL handle_netcdf_error( 'close_file', 47 )
+                   CALL netcdf_handle_error( 'close_file', 47 )
                 ENDIF
 
@@ -160,5 +165,5 @@
                 IF ( myid == 0 )  THEN
                    nc_stat = NF90_CLOSE( id_set_ts )
-                   CALL handle_netcdf_error( 'close_file', 48 )
+                   CALL netcdf_handle_error( 'close_file', 48 )
                 ENDIF
 
@@ -167,5 +172,5 @@
                 IF ( myid == 0  .OR.  netcdf_data_format > 4 )  THEN
                    nc_stat = NF90_CLOSE( id_set_3d(0) )
-                   CALL handle_netcdf_error( 'close_file', 49 )
+                   CALL netcdf_handle_error( 'close_file', 49 )
                 ENDIF
 
@@ -174,5 +179,5 @@
                 IF ( myid == 0 )  THEN
                    nc_stat = NF90_CLOSE( id_set_sp )
-                   CALL handle_netcdf_error( 'close_file', 50 )
+                   CALL netcdf_handle_error( 'close_file', 50 )
                 ENDIF
 
@@ -180,10 +185,10 @@
 
                 nc_stat = NF90_CLOSE( id_set_prt )
-                CALL handle_netcdf_error( 'close_file', 51 )
+                CALL netcdf_handle_error( 'close_file', 51 )
 
              CASE ( 109 ) 
 
                 nc_stat = NF90_CLOSE( id_set_pts )
-                CALL handle_netcdf_error( 'close_file', 412 )
+                CALL netcdf_handle_error( 'close_file', 412 )
 
              CASE ( 111 )
@@ -191,5 +196,5 @@
                 IF ( myid == 0  .OR.  netcdf_data_format > 4 )  THEN
                    nc_stat = NF90_CLOSE( id_set_xy(1) )
-                   CALL handle_netcdf_error( 'close_file', 52 )
+                   CALL netcdf_handle_error( 'close_file', 52 )
                 ENDIF
 
@@ -198,5 +203,5 @@
                 IF ( myid == 0  .OR.  netcdf_data_format > 4 )  THEN
                    nc_stat = NF90_CLOSE( id_set_xz(1) )
-                   CALL handle_netcdf_error( 'close_file', 352 )
+                   CALL netcdf_handle_error( 'close_file', 352 )
                 ENDIF
 
@@ -205,5 +210,5 @@
                 IF ( myid == 0  .OR.  netcdf_data_format > 4 )  THEN
                    nc_stat = NF90_CLOSE( id_set_yz(1) )
-                   CALL handle_netcdf_error( 'close_file', 353 )
+                   CALL netcdf_handle_error( 'close_file', 353 )
                 ENDIF
 
@@ -212,5 +217,5 @@
                 IF ( myid == 0  .OR.  netcdf_data_format > 4 )  THEN
                    nc_stat = NF90_CLOSE( id_set_3d(1) )
-                   CALL handle_netcdf_error( 'close_file', 353 )
+                   CALL netcdf_handle_error( 'close_file', 353 )
                 ENDIF
 
@@ -228,5 +233,5 @@
                    ENDIF
                    nc_stat = NF90_CLOSE( id_set_mask(mid,av) )
-                   CALL handle_netcdf_error( 'close_file', 459 )
+                   CALL netcdf_handle_error( 'close_file', 459 )
 		
                 ENDIF
Index: palm/trunk/SOURCE/data_output_2d.f90
===================================================================
--- palm/trunk/SOURCE/data_output_2d.f90	(revision 1782)
+++ palm/trunk/SOURCE/data_output_2d.f90	(revision 1783)
@@ -19,5 +19,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! name change of netcdf routines and module + related changes
 ! 
 ! Former revisions:
@@ -141,5 +141,5 @@
                do2d_yz_last_time, do2d_yz_n, do2d_yz_time_count,               &
                ibc_uv_b, icloud_scheme, io_blocks, io_group,                   &
-               message_string, netcdf_data_format,                             &
+               message_string,                             &
                ntdim_2d_xy, ntdim_2d_xz, ntdim_2d_yz, psolver, section,        &
                simulated_time,  simulated_time_chr, time_since_reference_point
@@ -165,5 +165,12 @@
                shf_eb_av, t_soil, t_soil_av, zs
     
-    USE netcdf_control
+#if defined( __netcdf )
+    USE NETCDF
+#endif
+
+    USE netcdf_interface,                                                      &
+        ONLY:  id_set_xy, id_set_xz, id_set_yz, id_var_do2d, id_var_time_xy,   &
+               id_var_time_xz, id_var_time_yz, nc_stat, netcdf_data_format,    &
+               netcdf_handle_error
 
     USE particle_attributes,                                                   &
@@ -1322,5 +1329,5 @@
                                          start = (/ do2d_xy_time_count(av) /), &
                                                     count = (/ 1 /) )
-                            CALL handle_netcdf_error( 'data_output_2d', 53 )
+                            CALL netcdf_handle_error( 'data_output_2d', 53 )
 #endif
                          ENDIF
@@ -1443,5 +1450,5 @@
                                              count = (/ nx+2, ny+2, 1, 1 /) )
                             ENDIF
-                            CALL handle_netcdf_error( 'data_output_2d', 54 )
+                            CALL netcdf_handle_error( 'data_output_2d', 54 )
 #endif
 
@@ -1481,5 +1488,5 @@
                                            count = (/ nx+2, ny+2, 1, 1 /) )
                    ENDIF
-                   CALL handle_netcdf_error( 'data_output_2d', 447 )
+                   CALL netcdf_handle_error( 'data_output_2d', 447 )
 #endif
 #endif
@@ -1511,5 +1518,5 @@
                                          start = (/ do2d_xz_time_count(av) /), &
                                                     count = (/ 1 /) )
-                            CALL handle_netcdf_error( 'data_output_2d', 56 )
+                            CALL netcdf_handle_error( 'data_output_2d', 56 )
 #endif
                          ENDIF
@@ -1666,5 +1673,5 @@
                             start = (/ 1, is, 1, do2d_xz_time_count(av) /), &
                                              count = (/ nx+2, 1, nzt_do-nzb_do+1, 1 /) )
-                            CALL handle_netcdf_error( 'data_output_2d', 58 )
+                            CALL netcdf_handle_error( 'data_output_2d', 58 )
 #endif
 
@@ -1707,5 +1714,5 @@
                             start = (/ 1, is, 1, do2d_xz_time_count(av) /), &
                                            count = (/ nx+2, 1, nzt_do-nzb_do+1, 1 /) )
-                   CALL handle_netcdf_error( 'data_output_2d', 451 )
+                   CALL netcdf_handle_error( 'data_output_2d', 451 )
 #endif
 #endif
@@ -1730,5 +1737,5 @@
                                          start = (/ do2d_yz_time_count(av) /), &
                                                     count = (/ 1 /) )
-                            CALL handle_netcdf_error( 'data_output_2d', 59 )
+                            CALL netcdf_handle_error( 'data_output_2d', 59 )
 #endif
                          ENDIF
@@ -1885,5 +1892,5 @@
                             start = (/ is, 1, 1, do2d_yz_time_count(av) /), &
                                              count = (/ 1, ny+2, nzt_do-nzb_do+1, 1 /) )
-                            CALL handle_netcdf_error( 'data_output_2d', 61 )
+                            CALL netcdf_handle_error( 'data_output_2d', 61 )
 #endif
 
@@ -1926,5 +1933,5 @@
                             start = (/ is, 1, 1, do2d_xz_time_count(av) /), &
                                            count = (/ 1, ny+2, nzt_do-nzb_do+1, 1 /) )
-                   CALL handle_netcdf_error( 'data_output_2d', 452 )
+                   CALL netcdf_handle_error( 'data_output_2d', 452 )
 #endif
 #endif
@@ -1997,5 +2004,5 @@
                       ENDIF    
 
-                      CALL handle_netcdf_error( 'data_output_2d', 55 ) 
+                      CALL netcdf_handle_error( 'data_output_2d', 55 )
 
                    CASE ( 'xz' )
@@ -2047,5 +2054,5 @@
                       ENDIF
 
-                      CALL handle_netcdf_error( 'data_output_2d', 57 )
+                      CALL netcdf_handle_error( 'data_output_2d', 57 )
 
                    CASE ( 'yz' )
@@ -2097,5 +2104,5 @@
                       ENDIF
 
-                      CALL handle_netcdf_error( 'data_output_2d', 60 )
+                      CALL netcdf_handle_error( 'data_output_2d', 60 )
 
                    CASE DEFAULT
Index: palm/trunk/SOURCE/data_output_3d.f90
===================================================================
--- palm/trunk/SOURCE/data_output_3d.f90	(revision 1782)
+++ palm/trunk/SOURCE/data_output_3d.f90	(revision 1783)
@@ -19,5 +19,5 @@
 ! Current revisions:
 ! ------------------
-! 
+! name change of netcdf routines and module + related changes
 ! 
 ! Former revisions:
@@ -118,5 +118,5 @@
         ONLY:  avs_data_file, cloud_physics, do3d, do3d_avs_n,                 &
                do3d_no, do3d_time_count, io_blocks, io_group,                  &
-               message_string, netcdf_data_format, ntdim_3d,                   &
+               message_string, ntdim_3d,                                       &
                nz_do3d, plot_3d_precision, psolver, simulated_time,            &
                simulated_time_chr, skip_do_avs, time_since_reference_point
@@ -134,5 +134,11 @@
         ONLY: m_soil, m_soil_av, nzb_soil, nzt_soil, t_soil, t_soil_av
 
-    USE netcdf_control
+#if defined( __netcdf )
+    USE NETCDF
+#endif
+
+    USE netcdf_interface,                                                      &
+        ONLY:  id_set_3d, id_var_do3d, id_var_time_3d, nc_stat,                &
+               netcdf_data_format, netcdf_handle_error
         
     USE particle_attributes,                                                   &
@@ -228,5 +234,5 @@
                                start = (/ do3d_time_count(av) /),           &
                                count = (/ 1 /) )
-       CALL handle_netcdf_error( 'data_output_3d', 376 )
+       CALL netcdf_handle_error( 'data_output_3d', 376 )
     ENDIF
 #endif
@@ -688,5 +694,5 @@
                 count = (/ nxr-nxl+1, nyn-nys+1, nzt_do-nzb_do+1, 1 /) )
           ENDIF
-          CALL handle_netcdf_error( 'data_output_3d', 386 )
+          CALL netcdf_handle_error( 'data_output_3d', 386 )
 #endif
        ENDIF
@@ -697,5 +703,5 @@
                          start = (/ 1, 1, 1, do3d_time_count(av) /),     &
                          count = (/ nx+2, ny+2, nzt_do-nzb_do+1, 1 /) )
-       CALL handle_netcdf_error( 'data_output_3d', 446 )
+       CALL netcdf_handle_error( 'data_output_3d', 446 )
 #endif
 #endif
Index: palm/trunk/SOURCE/data_output_mask.f90
===================================================================
--- palm/trunk/SOURCE/data_output_mask.f90	(revision 1782)
+++ palm/trunk/SOURCE/data_output_mask.f90	(revision 1783)
@@ -19,5 +19,6 @@
 ! Current revisions:
 ! -----------------
-! 
+! name change of netcdf routines and module + related changes,
+! switch back of netcdf data format moved from time integration routine to here
 ! 
 ! Former revisions:
@@ -96,13 +97,10 @@
     
     USE control_parameters,                                                    &
-        ONLY:  cloud_physics, domask, domask_no, domask_time_count,            &
-               mask_i, mask_j, mask_k, mask_size, mask_size_l,                 &
-               mask_start_l, max_masks, message_string, mid,                   &
-               netcdf_data_format, nz_do3d, simulated_time
+        ONLY:  cloud_physics, domask, domask_no, domask_time_count, mask_i,    &
+               mask_j, mask_k, mask_size, mask_size_l, mask_start_l,           &
+               max_masks, message_string, mid, nz_do3d, simulated_time
     USE cpulog,                                                                &
         ONLY:  cpu_log, log_point
 
-
-    
     USE indices,                                                               &
         ONLY:  nbgp, nxl, nxr, nyn, nys, nzb, nzt
@@ -110,7 +108,9 @@
     USE kinds
     
-    USE netcdf
-    
-    USE netcdf_control
+    USE NETCDF
+    
+    USE netcdf_interface,                                                      &
+        ONLY:  id_set_mask, id_var_domask, id_var_time_mask, nc_stat,          &
+               netcdf_data_format, netcdf_handle_error
     
     USE particle_attributes,                                                   &
@@ -136,4 +136,5 @@
     INTEGER(iwp) ::  k        !< 
     INTEGER(iwp) ::  n        !< 
+    INTEGER(iwp) ::  netcdf_data_format_save !<
     INTEGER(iwp) ::  psi      !< 
     INTEGER(iwp) ::  sender   !< 
@@ -158,4 +159,11 @@
 
     CALL cpu_log (log_point(49),'data_output_mask','start')
+
+!
+!-- Parallel netcdf output is not tested so far for masked data, hence
+!-- netcdf_data_format is switched back to non-paralell output.
+    netcdf_data_format_save = netcdf_data_format
+    IF ( netcdf_data_format == 5 ) netcdf_data_format = 3
+    IF ( netcdf_data_format == 6 ) netcdf_data_format = 4
 
 !
@@ -183,5 +191,5 @@
                                start = (/ domask_time_count(mid,av) /),       &
                                count = (/ 1 /) )
-       CALL handle_netcdf_error( 'data_output_mask', 460 )
+       CALL netcdf_handle_error( 'data_output_mask', 460 )
     ENDIF
 
@@ -564,5 +572,5 @@
                count = (/ mask_size_l(mid,1), mask_size_l(mid,2),  &
                           mask_size_l(mid,3), 1 /) )
-          CALL handle_netcdf_error( 'data_output_mask', 461 )
+          CALL netcdf_handle_error( 'data_output_mask', 461 )
        ELSE
 #endif
@@ -611,5 +619,5 @@
                   count = (/ mask_size(mid,1), mask_size(mid,2), &
                              mask_size(mid,3), 1 /) )
-             CALL handle_netcdf_error( 'data_output_mask', 462 )
+             CALL netcdf_handle_error( 'data_output_mask', 462 )
 
           ELSE
@@ -656,5 +664,5 @@
             count = (/ mask_size_l(mid,1), mask_size_l(mid,2), &
                        mask_size_l(mid,3), 1 /) )
-       CALL handle_netcdf_error( 'data_output_mask', 463 )
+       CALL netcdf_handle_error( 'data_output_mask', 463 )
 #endif
 
@@ -672,4 +680,7 @@
 #endif
 
+!
+!-- Switch back to original format given by user (see beginning of this routine)
+    netcdf_data_format = netcdf_data_format_save
 
     CALL cpu_log( log_point(49), 'data_output_mask', 'stop' )
Index: palm/trunk/SOURCE/data_output_profiles.f90
===================================================================
--- palm/trunk/SOURCE/data_output_profiles.f90	(revision 1782)
+++ palm/trunk/SOURCE/data_output_profiles.f90	(revision 1783)
@@ -19,5 +19,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! name change of netcdf routines and module + related changes
 ! 
 ! Former revisions:
@@ -86,5 +86,11 @@
     USE kinds
 
-    USE netcdf_control
+#if defined( __netcdf )
+    USE NETCDF
+#endif
+
+    USE netcdf_interface,                                                      &
+        ONLY:  id_set_pr, id_var_dopr, id_var_norm_dopr, id_var_time_pr,       &
+               nc_stat, netcdf_handle_error, output_for_t0
 
     USE pegrid
@@ -155,5 +161,5 @@
                                         (/ -coupling_start_time /), &
                                         start = (/ 1 /), count = (/ 1 /) )
-                   CALL handle_netcdf_error( 'data_output_profiles', 329 )
+                   CALL netcdf_handle_error( 'data_output_profiles', 329 )
                    output_for_t0 = .TRUE.
                    EXIT
@@ -166,18 +172,18 @@
                                   (/ hom_sum(nzb,18,normalizing_region) /), &
                                      start = (/ 1 /), count = (/ 1 /) )
-             CALL handle_netcdf_error( 'data_output_profiles', 330 )
+             CALL netcdf_handle_error( 'data_output_profiles', 330 )
 
              nc_stat = NF90_PUT_VAR( id_set_pr, id_var_norm_dopr(2), & ! ws2
                         (/ hom_sum(nzb+8,pr_palm,normalizing_region)**2 /), &
                                      start = (/ 1 /), count = (/ 1 /) )
-             CALL handle_netcdf_error( 'data_output_profiles', 331 )
+             CALL netcdf_handle_error( 'data_output_profiles', 331 )
              nc_stat = NF90_PUT_VAR( id_set_pr, id_var_norm_dopr(3), & ! tsw2
                         (/ hom_sum(nzb+3,pr_palm,normalizing_region)**2 /), &
                                   start = (/ 1 /), count = (/ 1 /) )
-             CALL handle_netcdf_error( 'data_output_profiles', 332 )
+             CALL netcdf_handle_error( 'data_output_profiles', 332 )
              nc_stat = NF90_PUT_VAR( id_set_pr, id_var_norm_dopr(4), & ! ws3
                         (/ hom_sum(nzb+8,pr_palm,normalizing_region)**3 /), &
                                      start = (/ 1 /), count = (/ 1 /) )
-             CALL handle_netcdf_error( 'data_output_profiles', 333 )
+             CALL netcdf_handle_error( 'data_output_profiles', 333 )
 
              nc_stat = NF90_PUT_VAR( id_set_pr, id_var_norm_dopr(5), &!ws2tsw
@@ -185,5 +191,5 @@
                            hom_sum(nzb+3,pr_palm,normalizing_region)    /), &
                                      start = (/ 1 /), count = (/ 1 /) )
-             CALL handle_netcdf_error( 'data_output_profiles', 334 )
+             CALL netcdf_handle_error( 'data_output_profiles', 334 )
 
              nc_stat = NF90_PUT_VAR( id_set_pr, id_var_norm_dopr(6), &!wstsw2
@@ -191,10 +197,10 @@
                            hom_sum(nzb+3,pr_palm,normalizing_region)**2 /), &
                                      start = (/ 1 /), count = (/ 1 /) )
-             CALL handle_netcdf_error( 'data_output_profiles', 335 )
+             CALL netcdf_handle_error( 'data_output_profiles', 335 )
 
              nc_stat = NF90_PUT_VAR( id_set_pr, id_var_norm_dopr(7), & ! z_i
                            (/ hom_sum(nzb+6,pr_palm,normalizing_region) /), &
                                      start = (/ 1 /), count = (/ 1 /) )
-             CALL handle_netcdf_error( 'data_output_profiles', 336 )
+             CALL netcdf_handle_error( 'data_output_profiles', 336 )
              
 #endif
@@ -216,5 +222,5 @@
                                               start = (/ 1, 1 /),              &
                                               count = (/ nzt-nzb+2, 1 /) )
-                      CALL handle_netcdf_error( 'data_output_profiles', 337 )
+                      CALL netcdf_handle_error( 'data_output_profiles', 337 )
 #endif
 
@@ -240,5 +246,5 @@
                                start = (/ dopr_time_count /),    &
                                count = (/ 1 /) )
-       CALL handle_netcdf_error( 'data_output_profiles', 338 )
+       CALL netcdf_handle_error( 'data_output_profiles', 338 )
 
 !
@@ -248,5 +254,5 @@
                                start = (/ dopr_time_count /),               &
                                count = (/ 1 /) )
-       CALL handle_netcdf_error( 'data_output_profiles', 339 )
+       CALL netcdf_handle_error( 'data_output_profiles', 339 )
 
        nc_stat = NF90_PUT_VAR( id_set_pr, id_var_norm_dopr(2), &  ! ws2
@@ -254,5 +260,5 @@
                                start = (/ dopr_time_count /),               &
                                count = (/ 1 /) )
-       CALL handle_netcdf_error( 'data_output_profiles', 340 )
+       CALL netcdf_handle_error( 'data_output_profiles', 340 )
 
        nc_stat = NF90_PUT_VAR( id_set_pr, id_var_norm_dopr(3), &  ! tsw2
@@ -260,5 +266,5 @@
                                start = (/ dopr_time_count /),               &
                                count = (/ 1 /) )
-       CALL handle_netcdf_error( 'data_output_profiles', 341 )
+       CALL netcdf_handle_error( 'data_output_profiles', 341 )
 
        nc_stat = NF90_PUT_VAR( id_set_pr, id_var_norm_dopr(4), &  ! ws3
@@ -266,5 +272,5 @@
                                start = (/ dopr_time_count /),               &
                                count = (/ 1 /) )
-       CALL handle_netcdf_error( 'data_output_profiles', 342 )
+       CALL netcdf_handle_error( 'data_output_profiles', 342 )
 
        nc_stat = NF90_PUT_VAR( id_set_pr, id_var_norm_dopr(5), &  ! ws2tsw
@@ -273,5 +279,5 @@
                                start = (/ dopr_time_count /),               &
                                count = (/ 1 /) )
-       CALL handle_netcdf_error( 'data_output_profiles', 343 )
+       CALL netcdf_handle_error( 'data_output_profiles', 343 )
 
        nc_stat = NF90_PUT_VAR( id_set_pr, id_var_norm_dopr(6), &  ! wstsw2
@@ -280,5 +286,5 @@
                                start = (/ dopr_time_count /),               &
                                count = (/ 1 /) )
-       CALL handle_netcdf_error( 'data_output_profiles', 344 )
+       CALL netcdf_handle_error( 'data_output_profiles', 344 )
 
        nc_stat = NF90_PUT_VAR( id_set_pr, id_var_norm_dopr(7), &  ! z_i
@@ -286,5 +292,5 @@
                                start = (/ dopr_time_count /),               &
                                count = (/ 1 /) )
-       CALL handle_netcdf_error( 'data_output_profiles', 345 )
+       CALL netcdf_handle_error( 'data_output_profiles', 345 )
 #endif
 
@@ -304,5 +310,5 @@
                                      start = (/ 1, dopr_time_count /),      &
                                      count = (/ nzt-nzb+2, 1 /) )
-             CALL handle_netcdf_error( 'data_output_profiles', 346 )
+             CALL netcdf_handle_error( 'data_output_profiles', 346 )
 #endif
 
Index: palm/trunk/SOURCE/data_output_ptseries.f90
===================================================================
--- palm/trunk/SOURCE/data_output_ptseries.f90	(revision 1782)
+++ palm/trunk/SOURCE/data_output_ptseries.f90	(revision 1783)
@@ -19,5 +19,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! netcdf module name changed + related changes
 ! 
 ! Former revisions:
@@ -81,5 +81,11 @@
     USE kinds
 
-    USE netcdf_control
+#if defined( __netcdf )
+    USE NETCDF
+#endif
+
+    USE netcdf_interface,                                                      &
+        ONLY:  dopts_num, id_set_pts, id_var_dopts, id_var_time_pts, nc_stat,  &
+               netcdf_handle_error
 
     USE particle_attributes,                                                   &
@@ -116,5 +122,5 @@
                                (/ time_since_reference_point /), &
                                start = (/ dopts_time_count /), count = (/ 1 /) )
-       CALL handle_netcdf_error( 'data_output_ptseries', 391 )
+       CALL netcdf_handle_error( 'data_output_ptseries', 391 )
 #endif
 
@@ -375,5 +381,5 @@
                                      start = (/ dopts_time_count /), &
                                      count = (/ 1 /) )
-             CALL handle_netcdf_error( 'data_output_ptseries', 392 )
+             CALL netcdf_handle_error( 'data_output_ptseries', 392 )
           ENDDO
        ENDDO
Index: palm/trunk/SOURCE/data_output_spectra.f90
===================================================================
--- palm/trunk/SOURCE/data_output_spectra.f90	(revision 1782)
+++ palm/trunk/SOURCE/data_output_spectra.f90	(revision 1783)
@@ -19,5 +19,5 @@
 ! Current revisions:
 ! ------------------
-! 
+! name change of netcdf routines and module + related changes
 ! 
 ! Former revisions:
@@ -69,4 +69,5 @@
  SUBROUTINE data_output_spectra
  
+#if defined( __netcdf )
 #if defined( __spectra )
 
@@ -81,5 +82,8 @@
     USE kinds
 
-    USE netcdf_control
+    USE NETCDF
+
+    USE netcdf_interface,                                                      &
+        ONLY:  id_set_sp, id_var_time_sp, nc_stat, netcdf_handle_error
 
     USE pegrid
@@ -116,5 +120,4 @@
        dosp_time_count = dosp_time_count + 1
 
-#if defined( __netcdf )
 !
 !--    Update the spectra time axis
@@ -122,6 +125,5 @@
                                (/ time_since_reference_point /), &
                                start = (/ dosp_time_count /), count = (/ 1 /) )
-       CALL handle_netcdf_error( 'data_output_spectra', 47 )
-#endif
+       CALL netcdf_handle_error( 'data_output_spectra', 47 )
 
 !
@@ -201,4 +203,5 @@
 
 #endif
+#endif
  END SUBROUTINE data_output_spectra
 
@@ -226,5 +229,9 @@
     USE kinds
 
-    USE netcdf_control
+    USE NETCDF
+
+    USE netcdf_interface,                                                      &
+        ONLY:  id_set_sp, id_var_dospx, id_var_dospy, nc_stat,                 &
+               netcdf_handle_error
 
     USE spectrum,                                                              &
@@ -261,5 +268,5 @@
                                   start = (/ 1, k, dosp_time_count /), &
                                   count = (/ nx/2, 1, 1 /) )
-          CALL handle_netcdf_error( 'data_output_spectra', 348 )
+          CALL netcdf_handle_error( 'data_output_spectra', 348 )
 
        ENDDO
@@ -279,5 +286,5 @@
                                   start = (/ 1, k, dosp_time_count /), &
                                   count = (/ ny/2, 1, 1 /) )
-          CALL handle_netcdf_error( 'data_output_spectra', 349 )
+          CALL netcdf_handle_error( 'data_output_spectra', 349 )
 
        ENDDO
Index: palm/trunk/SOURCE/data_output_tseries.f90
===================================================================
--- palm/trunk/SOURCE/data_output_tseries.f90	(revision 1782)
+++ palm/trunk/SOURCE/data_output_tseries.f90	(revision 1783)
@@ -19,5 +19,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! name change of netcdf routines and module + related changes
 ! 
 ! Former revisions:
@@ -72,5 +72,10 @@
     USE kinds
 
-    USE netcdf_control
+#if defined( __netcdf )
+    USE NETCDF
+#endif
+    USE netcdf_interface,                                                      &
+        ONLY:  dots_num, id_set_ts, id_var_dots, id_var_time_ts, nc_stat,      &
+               netcdf_handle_error
 
     USE pegrid
@@ -115,5 +120,5 @@
                                start = (/ dots_time_count /),    &
                                count = (/ 1 /) )
-       CALL handle_netcdf_error( 'data_output_tseries', 350 )
+       CALL netcdf_handle_error( 'data_output_tseries', 350 )
 #endif
 
@@ -129,5 +134,5 @@
                                      start = (/ dots_time_count /), &
                                      count = (/ 1 /) )
-             CALL handle_netcdf_error( 'data_output_tseries', 351 )
+             CALL netcdf_handle_error( 'data_output_tseries', 351 )
           ENDDO
 #endif
Index: palm/trunk/SOURCE/flow_statistics.f90
===================================================================
--- palm/trunk/SOURCE/flow_statistics.f90	(revision 1782)
+++ palm/trunk/SOURCE/flow_statistics.f90	(revision 1783)
@@ -19,5 +19,5 @@
 ! Current revisions:
 ! -----------------
-!
+! +module netcdf_interface
 !
 ! Former revisions:
@@ -207,12 +207,15 @@
     
     USE land_surface_model_mod,                                                &
-        ONLY:   dots_soil, ghf_eb, land_surface, m_soil, nzb_soil, nzt_soil,   &
+        ONLY:   ghf_eb, land_surface, m_soil, nzb_soil, nzt_soil,              &
                 qsws_eb, qsws_liq_eb, qsws_soil_eb, qsws_veg_eb, r_a, r_s,     &
                 shf_eb, t_soil
 
+    USE netcdf_interface,                                                      &
+        ONLY:  dots_rad, dots_soil
+
     USE pegrid
 
     USE radiation_model_mod,                                                   &
-        ONLY:  dots_rad, radiation, radiation_scheme, rad_net,                 &
+        ONLY:  radiation, radiation_scheme, rad_net,                           &
                rad_lw_in, rad_lw_out, rad_lw_cs_hr, rad_lw_hr,                 &
                rad_sw_in, rad_sw_out, rad_sw_cs_hr, rad_sw_hr
Index: palm/trunk/SOURCE/header.f90
===================================================================
--- palm/trunk/SOURCE/header.f90	(revision 1782)
+++ palm/trunk/SOURCE/header.f90	(revision 1783)
@@ -19,5 +19,5 @@
 ! Current revisions:
 ! -----------------
-!
+! netcdf module and variable names changed, output of netcdf_deflate
 !
 ! Former revisions:
@@ -249,4 +249,7 @@
         ONLY:  damp_level_ind_1d, dt_pr_1d, dt_run_control_1d, end_time_1d
         
+    USE netcdf_interface,                                                      &
+        ONLY:  netcdf_data_format, netcdf_data_format_string, netcdf_deflate
+
     USE particle_attributes,                                                   &
         ONLY:  bc_par_b, bc_par_lr, bc_par_ns, bc_par_t, collision_kernel,     &
@@ -1315,6 +1318,10 @@
 
        output_format = ''
-       output_format = output_format_netcdf
-       WRITE ( io, 344 )  output_format
+       output_format = netcdf_data_format_string
+       IF ( netcdf_deflate == 0 )  THEN
+          WRITE ( io, 344 )  output_format
+       ELSE
+          WRITE ( io, 354 )  TRIM( output_format ), netcdf_deflate
+       ENDIF
 
        DO  i = 1, dopr_n
@@ -1379,6 +1386,10 @@
 
           output_format = ''
-          output_format = output_format_netcdf
-          WRITE ( io, 344 )  output_format
+          output_format = netcdf_data_format_string
+          IF ( netcdf_deflate == 0 )  THEN
+             WRITE ( io, 344 )  output_format
+          ELSE
+             WRITE ( io, 354 )  TRIM( output_format ), netcdf_deflate
+          ENDIF
 
           IF ( do2d_xy /= ''  .AND.  section(1,1) /= -9999 )  THEN
@@ -1536,6 +1547,10 @@
           ENDIF
 
-          output_format = output_format_netcdf
-          WRITE ( io, 344 )  output_format
+          output_format = netcdf_data_format_string
+          IF ( netcdf_deflate == 0 )  THEN
+             WRITE ( io, 344 )  output_format
+          ELSE
+             WRITE ( io, 354 )  TRIM( output_format ), netcdf_deflate
+          ENDIF
 
           IF ( do3d_at_begin )  THEN
@@ -1595,10 +1610,14 @@
              ENDIF
 
-             output_format = output_format_netcdf
+             output_format = netcdf_data_format_string
 !--          Parallel output not implemented for mask data, hence
 !--          output_format must be adjusted.
              IF ( netcdf_data_format == 5 ) output_format = 'netCDF4/HDF5'
              IF ( netcdf_data_format == 6 ) output_format = 'netCDF4/HDF5 classic'
-             WRITE ( io, 344 )  output_format
+             IF ( netcdf_deflate == 0 )  THEN
+                WRITE ( io, 344 )  output_format
+             ELSE
+                WRITE ( io, 354 )  TRIM( output_format ), netcdf_deflate
+             ENDIF
 
              IF ( av == 0 )  THEN
@@ -1650,6 +1669,10 @@
        WRITE ( io, 340 )
 
-       output_format = output_format_netcdf
-       WRITE ( io, 344 )  output_format
+       output_format = netcdf_data_format_string
+       IF ( netcdf_deflate == 0 )  THEN
+          WRITE ( io, 344 )  output_format
+       ELSE
+          WRITE ( io, 354 )  TRIM( output_format ), netcdf_deflate
+       ENDIF
        WRITE ( io, 341 )  dt_dots
     ENDIF
@@ -1713,6 +1736,10 @@
        WRITE ( io, 370 )
 
-       output_format = output_format_netcdf
-       WRITE ( io, 344 )  output_format
+       output_format = netcdf_data_format_string
+       IF ( netcdf_deflate == 0 )  THEN
+          WRITE ( io, 344 )  output_format
+       ELSE
+          WRITE ( io, 354 )  TRIM( output_format ), netcdf_deflate
+       ENDIF
        WRITE ( io, 371 )  dt_dosp
        IF ( skip_time_dosp /= 0.0_wp )  WRITE ( io, 339 )  skip_time_dosp
@@ -1864,5 +1891,9 @@
              output_format = 'netcdf and binary'
           ENDIF
-          WRITE ( io, 344 )  output_format
+          IF ( netcdf_deflate == 0 )  THEN
+             WRITE ( io, 344 )  output_format
+          ELSE
+             WRITE ( io, 354 )  TRIM( output_format ), netcdf_deflate
+          ENDIF
        ENDIF
        IF ( dt_dopts /= 9999999.9_wp )  WRITE ( io, 494 )  dt_dopts
@@ -2176,4 +2207,5 @@
 352 FORMAT  (/'       Number of output time levels allowed: ',I3 /)
 353 FORMAT  (/'       Number of output time levels allowed: unlimited' /)
+354 FORMAT ('       Output format: ',A, '   compressed with level: ',I1/)
 #if defined( __dvrp_graphics )
 360 FORMAT ('    Plot-Sequence with dvrp-software:'/ &
Index: palm/trunk/SOURCE/init_3d_model.f90
===================================================================
--- palm/trunk/SOURCE/init_3d_model.f90	(revision 1782)
+++ palm/trunk/SOURCE/init_3d_model.f90	(revision 1783)
@@ -19,5 +19,5 @@
 ! Current revisions:
 ! ------------------
-!
+! netcdf module name changed + related changes
 !
 ! Former revisions:
@@ -262,5 +262,6 @@
         ONLY:  e1d, kh1d, km1d, l1d, rif1d, u1d, us1d, usws1d, v1d, vsws1d 
     
-    USE netcdf_control
+    USE netcdf_interface,                                                      &
+        ONLY:  dots_max, dots_num
     
     USE particle_attributes,                                                   &
Index: palm/trunk/SOURCE/init_masks.f90
===================================================================
--- palm/trunk/SOURCE/init_masks.f90	(revision 1782)
+++ palm/trunk/SOURCE/init_masks.f90	(revision 1783)
@@ -19,5 +19,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! netcdf module name changed + related changes
 ! 
 ! Former revisions:
@@ -96,5 +96,5 @@
                mask_x_loop, mask_xyz_dimension, mask_y, mask_y_loop, mask_z,   &
                mask_z_loop, max_masks,  message_string, mid,                   &
-               netcdf_data_format, passive_scalar, precipitation, ocean
+               passive_scalar, precipitation, ocean
 
     USE grid_variables,                                                        &
@@ -106,5 +106,6 @@
     USE kinds
 
-    USE netcdf_control
+    USE netcdf_interface,                                                      &
+        ONLY:  domask_unit, netcdf_data_format
 
     USE particle_attributes,                                                   &
Index: palm/trunk/SOURCE/land_surface_model.f90
===================================================================
--- palm/trunk/SOURCE/land_surface_model.f90	(revision 1782)
+++ palm/trunk/SOURCE/land_surface_model.f90	(revision 1783)
@@ -19,5 +19,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! netcdf variables moved to netcdf module
 ! 
 ! Former revisions:
@@ -132,7 +132,4 @@
     USE kinds
 
-    USE netcdf_control,                                                        &
-        ONLY:  dots_label, dots_num, dots_unit
-
     USE pegrid
 
@@ -157,12 +154,4 @@
                                nzt_soil = 3, & !< top of the soil model (to be switched)
                                nzs = 4         !< number of soil layers (fixed for now)
-
-    INTEGER(iwp) :: dots_soil = 0  !< starting index for timeseries output 
-
-    INTEGER(iwp), DIMENSION(0:1) :: id_dim_zs_xy, id_dim_zs_xz, id_dim_zs_yz,  &
-                                    id_dim_zs_3d, id_var_zs_xy,                & 
-                                    id_var_zs_xz, id_var_zs_yz, id_var_zs_3d
-                                    
-    INTEGER(iwp), DIMENSION(1:max_masks,0:1) :: id_dim_zs_mask, id_var_zs_mask
 
     REAL(wp), PARAMETER ::                     &
@@ -519,9 +508,6 @@
 
 !
-!-- Public grid and NetCDF variables
-    PUBLIC dots_soil, id_dim_zs_xy, id_dim_zs_xz, id_dim_zs_yz,                &
-           id_dim_zs_3d, id_dim_zs_mask, id_var_zs_xy, id_var_zs_xz,           &
-           id_var_zs_yz, id_var_zs_3d, id_var_zs_mask, nzb_soil, nzs, nzt_soil,&
-           zs
+!-- Public grid variables
+    PUBLIC nzb_soil, nzs, nzt_soil, zs
 
 !
@@ -928,22 +914,4 @@
                                                 statistic_regions+1 )
 
-!
-!--    Add timeseries for land surface model
-       dots_soil = dots_num + 1
-       dots_num  = dots_num + 8
-
-       dots_label(dots_soil) = "ghf_eb"
-       dots_label(dots_soil+1) = "shf_eb"
-       dots_label(dots_soil+2) = "qsws_eb"
-       dots_label(dots_soil+3) = "qsws_liq_eb"
-       dots_label(dots_soil+4) = "qsws_soil_eb"
-       dots_label(dots_soil+5) = "qsws_veg_eb"
-       dots_label(dots_soil+6) = "r_a"
-       dots_label(dots_soil+7) = "r_s"
-
-       dots_unit(dots_soil:dots_soil+5) = "W/m2"
-       dots_unit(dots_soil+6:dots_soil+7) = "s/m"
-
-
     END SUBROUTINE init_lsm
 
Index: palm/trunk/SOURCE/lpm_data_output_particles.f90
===================================================================
--- palm/trunk/SOURCE/lpm_data_output_particles.f90	(revision 1782)
+++ palm/trunk/SOURCE/lpm_data_output_particles.f90	(revision 1783)
@@ -19,5 +19,5 @@
 ! Current revisions:
 ! ------------------
-! 
+! name change of netcdf routines and module + related changes
 ! 
 ! Former revisions:
@@ -65,5 +65,6 @@
     USE kinds
 
-    USE netcdf_control
+    USE netcdf_interface,                                                      &
+        ONLY:  nc_stat
 
     USE particle_attributes,                                                   &
@@ -122,5 +123,5 @@
 !                             (/ simulated_time /),        &
 !                             start = (/ prt_time_count /), count = (/ 1 /) )
-!     CALL handle_netcdf_error( 'lpm_data_output_particles', 1 )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 1 )
 ! 
 ! !
@@ -129,5 +130,5 @@
 !                             (/ number_of_particles /),   &
 !                             start = (/ prt_time_count /), count = (/ 1 /) )
-!     CALL handle_netcdf_error( 'lpm_data_output_particles', 2 )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 2 )
 ! 
 ! !
@@ -136,45 +137,45 @@
 !                             start = (/ 1, prt_time_count /),               &
 !                             count = (/ maximum_number_of_particles /) )
-!     CALL handle_netcdf_error( 'lpm_data_output_particles', 3 )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 3 )
 ! 
 !     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(2), particles%dvrp_psize,&
 !                             start = (/ 1, prt_time_count /),                &
 !                             count = (/ maximum_number_of_particles /) )
-!     CALL handle_netcdf_error( 'lpm_data_output_particles', 4 )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 4 )
 ! 
 !     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(3), particles%origin_x, &
 !                             start = (/ 1, prt_time_count /),               &
 !                             count = (/ maximum_number_of_particles /) )
-!     CALL handle_netcdf_error( 'lpm_data_output_particles', 5 )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 5 )
 ! 
 !     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(4), particles%origin_y, &
 !                             start = (/ 1, prt_time_count /),               &
 !                             count = (/ maximum_number_of_particles /) )
-!     CALL handle_netcdf_error( 'lpm_data_output_particles', 6 )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 6 )
 ! 
 !     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(5), particles%origin_z, &
 !                             start = (/ 1, prt_time_count /),               &
 !                             count = (/ maximum_number_of_particles /) )
-!     CALL handle_netcdf_error( 'lpm_data_output_particles', 7 )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 7 )
 ! 
 !     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(6), particles%radius,   &
 !                             start = (/ 1, prt_time_count /),               &
 !                             count = (/ maximum_number_of_particles /) )
-!     CALL handle_netcdf_error( 'lpm_data_output_particles', 8 )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 8 )
 ! 
 !     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(7), particles%speed_x,  &
 !                             start = (/ 1, prt_time_count /),               &
 !                             count = (/ maximum_number_of_particles /) )
-!     CALL handle_netcdf_error( 'lpm_data_output_particles', 9 )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 9 )
 ! 
 !     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(8), particles%speed_y,  &
 !                             start = (/ 1, prt_time_count /),               &
 !                             count = (/ maximum_number_of_particles /) )
-!     CALL handle_netcdf_error( 'lpm_data_output_particles', 10 )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 10 )
 ! 
 !     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(9), particles%speed_z,  &
 !                             start = (/ 1, prt_time_count /),               &
 !                             count = (/ maximum_number_of_particles /) )
-!     CALL handle_netcdf_error( 'lpm_data_output_particles', 11 )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 11 )
 ! 
 !     nc_stat = NF90_PUT_VAR( id_set_prt,id_var_prt(10),                     &
@@ -182,30 +183,30 @@
 !                             start = (/ 1, prt_time_count /),               &
 !                             count = (/ maximum_number_of_particles /) )
-!     CALL handle_netcdf_error( 'lpm_data_output_particles', 12 )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 12 )
 ! 
 !     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(11), particles%x,       &
 !                             start = (/ 1, prt_time_count /),               &
 !                             count = (/ maximum_number_of_particles /) )
-!     CALL handle_netcdf_error( 'lpm_data_output_particles', 13 )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 13 )
 ! 
 !     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(12), particles%y,       &
 !                             start = (/ 1, prt_time_count /),               &
 !                             count = (/ maximum_number_of_particles /) )
-!     CALL handle_netcdf_error( 'lpm_data_output_particles', 14 )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 14 )
 ! 
 !     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(13), particles%z,       &
 !                             start = (/ 1, prt_time_count /),               &
 !                             count = (/ maximum_number_of_particles /) )
-!     CALL handle_netcdf_error( 'lpm_data_output_particles', 15 )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 15 )
 ! 
 !     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(14), particles%class,   &
 !                             start = (/ 1, prt_time_count /),               &
 !                             count = (/ maximum_number_of_particles /) )
-!     CALL handle_netcdf_error( 'lpm_data_output_particles', 16 )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 16 )
 ! 
 !     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(15), particles%group,   &
 !                             start = (/ 1, prt_time_count /),               &
 !                             count = (/ maximum_number_of_particles /) )
-!     CALL handle_netcdf_error( 'lpm_data_output_particles', 17 )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 17 )
 ! 
 !     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(16),                    &
@@ -213,10 +214,10 @@
 !                             start = (/ 1, prt_time_count /),               &
 !                             count = (/ maximum_number_of_particles /) )
-!     CALL handle_netcdf_error( 'lpm_data_output_particles', 18 )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 18 )
 ! 
 !     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(17), particles%tail_id, &
 !                             start = (/ 1, prt_time_count /),               &
 !                             count = (/ maximum_number_of_particles /) )
-!     CALL handle_netcdf_error( 'lpm_data_output_particles', 19 )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 19 )
 ! 
 #endif
Index: palm/trunk/SOURCE/lpm_exchange_horiz.f90
===================================================================
--- palm/trunk/SOURCE/lpm_exchange_horiz.f90	(revision 1782)
+++ palm/trunk/SOURCE/lpm_exchange_horiz.f90	(revision 1783)
@@ -19,5 +19,5 @@
 ! Current revisions:
 ! ------------------
-! 
+! new netcdf-module included
 ! 
 ! Former revisions:
@@ -70,5 +70,5 @@
 
     USE control_parameters,                                                    &
-        ONLY:  dz, message_string, netcdf_data_format, simulated_time
+        ONLY:  dz, message_string, simulated_time
 
     USE cpulog,                                                                &
@@ -85,4 +85,7 @@
     USE lpm_pack_arrays_mod,                                                   &
         ONLY:  lpm_pack_arrays
+
+    USE netcdf_interface,                                                      &
+        ONLY:  netcdf_data_format
 
     USE particle_attributes,                                                   &
Index: palm/trunk/SOURCE/lpm_init.f90
===================================================================
--- palm/trunk/SOURCE/lpm_init.f90	(revision 1782)
+++ palm/trunk/SOURCE/lpm_init.f90	(revision 1783)
@@ -19,5 +19,5 @@
 ! Current revisions:
 ! -----------------
-!
+! netcdf module added
 ! 
 ! Former revisions:
@@ -108,6 +108,5 @@
     USE control_parameters,                                                    &
         ONLY:  cloud_droplets, constant_flux_layer, current_timestep_number,   &
-               dz, initializing_actions, message_string, netcdf_data_format,   &
-               ocean, simulated_time
+               dz, initializing_actions, message_string, ocean, simulated_time
 
     USE dvrp_variables,                                                        &
@@ -125,4 +124,7 @@
     USE lpm_collision_kernels_mod,                                             &
         ONLY:  init_kernels
+
+    USE netcdf_interface,                                                      &
+        ONLY:  netcdf_data_format
 
     USE particle_attributes,                                                   &
Index: palm/trunk/SOURCE/lpm_release_set.f90
===================================================================
--- palm/trunk/SOURCE/lpm_release_set.f90	(revision 1782)
+++ palm/trunk/SOURCE/lpm_release_set.f90	(revision 1783)
@@ -19,5 +19,5 @@
 ! Current revisions:
 ! ------------------
-! 
+! module control_parameters removed
 ! 
 ! Former revisions:
@@ -58,7 +58,4 @@
  SUBROUTINE lpm_release_set
  
-
-    USE control_parameters,                                                    &
-        ONLY:  iran, message_string, netcdf_data_format
 
     USE lpm_init_mod,                                                          &
Index: palm/trunk/SOURCE/modules.f90
===================================================================
--- palm/trunk/SOURCE/modules.f90	(revision 1782)
+++ palm/trunk/SOURCE/modules.f90	(revision 1783)
@@ -19,5 +19,5 @@
 ! Current revisions:
 ! ------------------
-!
+! netcdf variables moved to the netcdf-interface module
 !
 ! Former revisions:
@@ -614,6 +614,5 @@
                              return_addres, return_username, &
                              timestep_scheme = 'runge-kutta-3'                             
-    CHARACTER (LEN=40)   ::  avs_data_file, output_format_netcdf, &
-                             topography = 'flat'
+    CHARACTER (LEN=40)   ::  avs_data_file, topography = 'flat'
     CHARACTER (LEN=64)   ::  host = ' '
     CHARACTER (LEN=80)   ::  log_message, run_identifier
@@ -629,6 +628,4 @@
     CHARACTER (LEN=20), DIMENSION(300) ::  data_output_pr = ' '
     CHARACTER (LEN=20), DIMENSION(200) ::  data_output_pr_user = ' '
-    CHARACTER (LEN=20), DIMENSION(11)  ::  netcdf_precision = ' '
-
     CHARACTER (LEN=20), DIMENSION(max_masks,0:1,100) ::  domask = ' '
     CHARACTER (LEN=20), DIMENSION(0:1,100) ::  do2d = ' ', do3d = ' '
@@ -653,5 +650,5 @@
                      maximum_parallel_io_streams = -1, max_pr_user = 0, &
                      mgcycles = 0, mg_cycles = -1, mg_switch_to_pe0_level = 0, mid, &
-                     nlsf = 1000, ntnudge = 1000, netcdf_data_format = 2, ngsrb = 2, &
+                     nlsf = 1000, ntnudge = 1000, ngsrb = 2, &
                      nr_timesteps_this_run = 0, &
                      nsor = 20, nsor_ini = 100, n_sor, normalizing_region = 0, &
@@ -1153,148 +1150,4 @@
 
 
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Definition of parameters and variables for netcdf control.
-!------------------------------------------------------------------------------!
- MODULE netcdf_control
-
-    USE control_parameters, ONLY: max_masks
-    USE kinds
-#if defined( __netcdf )
-    USE netcdf
-#endif
-
-    INTEGER(iwp), PARAMETER ::  dopr_norm_num = 7, dopts_num = 29, dots_max = 100
-
-    INTEGER(iwp) ::  dots_num = 23
-
-    CHARACTER (LEN=6), DIMENSION(dopr_norm_num) ::  dopr_norm_names =          &
-         (/ 'wpt0  ', 'ws2   ', 'tsw2  ', 'ws3   ', 'ws2tsw', 'wstsw2',        &
-            'z_i   ' /)
-
-    CHARACTER (LEN=6), DIMENSION(dopr_norm_num) ::  dopr_norm_longnames =      &
-         (/ 'wpt0  ', 'w*2   ', 't*w2  ', 'w*3   ', 'w*2t*w', 'w*t*w2',        &
-            'z_i   ' /)
-
-    CHARACTER (LEN=7), DIMENSION(dopts_num) :: dopts_label =                   &
-          (/ 'tnpt   ', 'x_     ', 'y_     ', 'z_     ', 'z_abs  ', 'u      ', &
-             'v      ', 'w      ', 'u"     ', 'v"     ', 'w"     ', 'npt_up ', &
-             'w_up   ', 'w_down ', 'radius ', 'r_min  ', 'r_max  ', 'npt_max', &
-             'npt_min', 'x*2    ', 'y*2    ', 'z*2    ', 'u*2    ', 'v*2    ', &
-             'w*2    ', 'u"2    ', 'v"2    ', 'w"2    ', 'npt*2  ' /)
-
-    CHARACTER (LEN=7), DIMENSION(dopts_num) :: dopts_unit =                    &
-          (/ 'number ', 'm      ', 'm      ', 'm      ', 'm      ', 'm/s    ', &
-             'm/s    ', 'm/s    ', 'm/s    ', 'm/s    ', 'm/s    ', 'number ', &
-             'm/s    ', 'm/s    ', 'm      ', 'm      ', 'm      ', 'number ', &
-             'number ', 'm2     ', 'm2     ', 'm2     ', 'm2/s2  ', 'm2/s2  ', &
-             'm2/s2  ', 'm2/s2  ', 'm2/s2  ', 'm2/s2  ', 'number2' /)
-
-    CHARACTER (LEN=13), DIMENSION(dots_max) :: dots_label =                    &
-          (/ 'E            ', 'E*           ', 'dt           ',                &
-             'u*           ', 'th*          ', 'umax         ',                &
-             'vmax         ', 'wmax         ', 'div_new      ',                &
-             'div_old      ', 'z_i_wpt      ', 'z_i_pt       ',                &
-             'w*           ', 'w"pt"0       ', 'w"pt"        ',                &
-             'wpt          ', 'pt(0)        ', 'pt(z_mo)     ',                &
-             'w"u"0        ', 'w"v"0        ', 'w"q"0        ',                &
-             'ol           ', 'q*           ',                                 &
-             ( 'unknown      ', i9 = 1, dots_max-23 ) /)
-
-    CHARACTER (LEN=13), DIMENSION(dots_max) :: dots_unit =                     &
-          (/ 'm2/s2        ', 'm2/s2        ', 's            ',                &
-             'm/s          ', 'K            ', 'm/s          ',                &
-             'm/s          ', 'm/s          ', 's-1          ',                &
-             's-1          ', 'm            ', 'm            ',                &
-             'm/s          ', 'K m/s        ', 'K m/s        ',                &
-             'K m/s        ', 'K            ', 'K            ',                &
-             'm2/s2        ', 'm2/s2        ', 'kg m/s       ',                &
-             'm            ', 'kg/kg        ',                                 &
-             ( 'unknown      ', i9 = 1, dots_max-23 ) /)
-
-    CHARACTER (LEN=9), DIMENSION(300) ::  dopr_unit = 'unknown'
-
-    CHARACTER (LEN=7), DIMENSION(0:1,100) ::  do2d_unit, do3d_unit
-
-    CHARACTER (LEN=16), DIMENSION(25) ::  prt_var_names = &
-          (/ 'pt_age          ', 'pt_dvrp_size    ', 'pt_origin_x     ', &
-             'pt_origin_y     ', 'pt_origin_z     ', 'pt_radius       ', &
-             'pt_speed_x      ', 'pt_speed_y      ', 'pt_speed_z      ', &
-             'pt_weight_factor', 'pt_x            ', 'pt_y            ', &
-             'pt_z            ', 'pt_color        ', 'pt_group        ', &
-             'pt_tailpoints   ', 'pt_tail_id      ', 'pt_density_ratio', &
-             'pt_exp_arg      ', 'pt_exp_term     ', 'not_used        ', &
-             'not_used        ', 'not_used        ', 'not_used        ', &
-             'not_used        ' /)
-
-    CHARACTER (LEN=16), DIMENSION(25) ::  prt_var_units = &
-          (/ 'seconds         ', 'meters          ', 'meters          ', &
-             'meters          ', 'meters          ', 'meters          ', &
-             'm/s             ', 'm/s             ', 'm/s             ', &
-             'factor          ', 'meters          ', 'meters          ', &
-             'meters          ', 'none            ', 'none            ', &
-             'none            ', 'none            ', 'ratio           ', &
-             'none            ', 'none            ', 'not_used        ', &
-             'not_used        ', 'not_used        ', 'not_used        ', &
-             'not_used        ' /)
-
-    INTEGER(iwp) ::  id_dim_prtnum, 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_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, &
-                    id_dim_time_yz, id_dim_time_3d, id_dim_x_xy, id_dim_xu_xy, &
-                    id_dim_x_xz, id_dim_xu_xz, id_dim_x_yz, id_dim_xu_yz, &
-                    id_dim_x_3d, id_dim_xu_3d, id_dim_y_xy, id_dim_yv_xy, &
-                    id_dim_y_xz, id_dim_yv_xz, id_dim_y_yz, id_dim_yv_yz, &
-                    id_dim_y_3d, id_dim_yv_3d, id_dim_zu_xy, id_dim_zu1_xy, &
-                    id_dim_zu_xz, id_dim_zu_yz, id_dim_zu_3d, id_dim_zw_xy, &
-                    id_dim_zw_xz, id_dim_zw_yz, id_dim_zw_3d, id_set_xy, &
-                    id_set_xz, id_set_yz, id_set_3d, id_var_ind_x_yz, &
-                    id_var_ind_y_xz, id_var_ind_z_xy, id_var_time_xy, &
-                    id_var_time_xz, id_var_time_yz, id_var_time_3d, id_var_x_xy, &
-                    id_var_xu_xy, id_var_x_xz, id_var_xu_xz, id_var_x_yz, &
-                    id_var_xu_yz, id_var_x_3d, id_var_xu_3d, id_var_y_xy, &
-                    id_var_yv_xy, id_var_y_xz, id_var_yv_xz, id_var_y_yz, &
-                    id_var_yv_yz, id_var_y_3d, id_var_yv_3d, id_var_zusi_xy, &
-                    id_var_zusi_3d, id_var_zu_xy, id_var_zu1_xy, id_var_zu_xz, &
-                    id_var_zu_yz, id_var_zu_3d, id_var_zwwi_xy, id_var_zwwi_3d, &
-                    id_var_zw_xy, id_var_zw_xz, id_var_zw_yz, id_var_zw_3d
-
-    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(dopts_num,0:10) ::  id_var_dopts
-    INTEGER(iwp), DIMENSION(0:1,100)        ::  id_var_do2d, id_var_do3d
-    INTEGER(iwp), DIMENSION(100,0:9)        ::  id_dim_z_pr, id_var_dopr, &
-                                               id_var_z_pr
-    INTEGER(iwp), DIMENSION(dots_max,0:9)   ::  id_var_dots
-
-!
-!-- masked output
-    CHARACTER (LEN=7), DIMENSION(max_masks,0:1,100) ::  domask_unit
-
-    LOGICAL ::  output_for_t0 = .FALSE.
-
-    INTEGER(iwp), DIMENSION(1:max_masks,0:1) ::  id_dim_time_mask, id_dim_x_mask, &
-                   id_dim_xu_mask, id_dim_y_mask, id_dim_yv_mask, id_dim_zu_mask, &
-                   id_dim_zw_mask, &
-                   id_set_mask, &
-                   id_var_time_mask, id_var_x_mask, id_var_xu_mask, &
-                   id_var_y_mask, id_var_yv_mask, id_var_zu_mask, id_var_zw_mask, &
-                   id_var_zusi_mask, id_var_zwwi_mask
-
-    INTEGER(iwp), DIMENSION(1:max_masks,0:1,100)         ::  id_var_domask
-
-    SAVE
-
- END MODULE netcdf_control
 
 
Index: palm/trunk/SOURCE/netcdf.f90
===================================================================
--- palm/trunk/SOURCE/netcdf.f90	(revision 1782)
+++ 	(revision )
@@ -1,5371 +1,0 @@
-!> @file netcdf.f90
-!--------------------------------------------------------------------------------!
-! This file is part of PALM.
-!
-! PALM is free software: you can redistribute it and/or modify it under the terms
-! of the GNU General Public License as published by the Free Software Foundation,
-! either version 3 of the License, or (at your option) any later version.
-!
-! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
-! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
-!
-! You should have received a copy of the GNU General Public License along with
-! PALM. If not, see <http://www.gnu.org/licenses/>.
-!
-! Copyright 1997-2015 Leibniz Universitaet Hannover
-!--------------------------------------------------------------------------------!
-!
-! Current revisions:
-! ------------------
-! 
-! 
-! Former revisions:
-! -----------------
-! $Id$
-!
-! 1745 2016-02-05 13:06:51Z gronemeier
-! Bugfix: recalculating ntdim_3d, ntdim_2d_xy/xz/yz when checking the
-!         extensibility of an existing file (only when using parallel NetCDF). 
-! 
-! 1691 2015-10-26 16:17:44Z maronga
-! Added output of radiative heating rates for RRTMG. Corrected output of 
-! radiative fluxes
-! 
-! 1682 2015-10-07 23:56:08Z knoop
-! Code annotations made doxygen readable 
-! 
-! 1596 2015-05-21 09:34:28Z gronemeier
-! Bugfix in masked data output. Read 'zu_3d' when trying to extend masked data
-! 
-! 1551 2015-03-03 14:18:16Z maronga
-! Added support for land surface model and radiation model output. In the course
-! of this action a new vertical grid zs (soil) was introduced.
-! 
-! 1353 2014-04-08 15:21:23Z heinze
-! REAL constants provided with KIND-attribute
-!
-! 1322 2014-03-20 16:38:49Z raasch
-! Forgotten ONLY-attribute added to USE-statements
-!
-! 1320 2014-03-20 08:40:49Z raasch
-! ONLY-attribute added to USE-statements,
-! kind-parameters added to all INTEGER and REAL declaration statements,
-! kinds are defined in new module kinds,
-! revision history before 2012 removed,
-! comment fields (!:) to be used for variable explanations added to
-! all variable declaration statements
-!
-! 1308 2014-03-13 14:58:42Z fricke
-! +ntime_count, oldmode
-! Adjust NF90_CREATE and NF90_OPEN statement for parallel output
-! To increase the performance for parallel output, the following is done:
-! - Limit time dimension
-! - Values of axis data are only written by PE0
-! - No fill is set for all variables
-! Check the number of output time levels for restart jobs
-!
-! 1206 2013-07-18 12:49:16Z witha
-! Bugfix: typo in preprocessor directive in subroutine open_write_netcdf_file
-!
-! 1092 2013-02-02 11:24:22Z raasch
-! unused variables removed
-!
-! 1053 2012-11-13 17:11:03Z hoffmann
-! +qr, nr, prr
-!
-! 1036 2012-10-22 13:43:42Z raasch
-! code put under GPL (PALM 3.9)
-!
-! 1031 2012-10-19 14:35:30Z raasch
-! netCDF4 without parallel file support implemented, new routines
-! create_netcdf_file and open_write_netcdf_file at end
-!
-! 992 2012-09-05 15:08:26Z hoffmann
-! Removal of the informative messages PA0352 and PA0353.
-!  
-! 983 2012-08-21 14:17:57Z hoffmann
-! Bugfix in cross_profiles.
-!
-! 964 2012-07-26 09:14:24Z raasch
-! rev 951 and 959 reformatted
-!
-! 959 2012-07-24 13:13:41Z hoffmann
-! Bugfix in cross_profiles. It is not allowed to arrange more than 100
-! profiles with cross_profiles. 
-!
-! 951 2012-07-19 14:22:52Z hoffmann
-! cross_profiles, profile_rows, profile_columns are written to netCDF header
-!
-! Revision 1.1  2005/05/18 15:37:16  raasch
-! Initial revision
-!
-!
-! Description:
-! ------------
-!> In case of extend = .FALSE.:
-!> Define all necessary dimensions, axes and variables for the different
-!> netCDF datasets. This subroutine is called from check_open after a new
-!> dataset is created. It leaves the open netCDF files ready to write.
-!>
-!> In case of extend = .TRUE.:
-!> Find out if dimensions and variables of an existing file match the values
-!> of the actual run. If so, get all necessary information (ids, etc.) from
-!> this file.
-!>
-!> Parameter av can assume values 0 (non-averaged data) and 1 (time averaged
-!> data)
-!>
-!> @todo calculation of output time levels for parallel NetCDF still does not
-!>       cover every exception (change of dt_do, end_time in restart)
-!------------------------------------------------------------------------------!
-#if defined( __ibmy_special )
-@PROCESS NOOPTimize
-#endif
- SUBROUTINE define_netcdf_header( callmode, extend, av )
- 
-#if defined( __netcdf )
-
-    USE arrays_3d,                                                             &
-        ONLY:  zu, zw
-
-    USE constants,                                                             &
-        ONLY:  pi
-
-    USE control_parameters,                                                    &
-        ONLY:  averaging_interval, averaging_interval_pr,                      &
-               averaging_interval_sp, data_output_pr,  domask,  dopr_n,        &
-               dopr_time_count, dopts_time_count, dots_time_count,             &
-               dosp_time_count, do2d, do2d_xz_time_count, do3d,                &
-               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,             &
-               do3d_time_count, domask_time_count, end_time, mask_i_global,    &
-               mask_j_global, mask_k_global, message_string, mid,              &
-               netcdf_data_format, netcdf_precision, ntdim_2d_xy,              &
-               ntdim_2d_xz, ntdim_2d_yz, ntdim_3d, nz_do3d, prt_time_count,    &
-               run_description_header, section, simulated_time,                &
-               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
-
-    USE grid_variables,                                                        &
-        ONLY:  dx, dy, zu_s_inner, zw_w_inner
-
-    USE indices,                                                               &
-        ONLY:  nx, ny, nz ,nzb, nzt
-
-    USE netcdf_control
-
-    USE kinds
-
-    USE land_surface_model_mod,                                                &
-        ONLY: land_surface, nzb_soil, nzt_soil, id_dim_zs_xy, id_dim_zs_xz,    &
-              id_dim_zs_yz, id_dim_zs_3d, id_dim_zs_mask, id_var_zs_xy,        &
-              id_var_zs_xz, id_var_zs_yz ,id_var_zs_3d, id_var_zs_mask,        &
-              nzs, zs
-
-    USE pegrid
-
-    USE particle_attributes,                                                   &
-        ONLY:  maximum_number_of_particles, number_of_particle_groups
-
-    USE profil_parameter,                                                      &
-        ONLY:  crmax, cross_profiles, dopr_index, profile_columns, profile_rows
-
-    USE radiation_model_mod,                                                   &
-        ONLY:  rad_lw_in, rad_lw_out, rad_lw_cs_hr, rad_lw_hr,                 &
-               rad_sw_in, rad_sw_out, rad_sw_cs_hr, rad_sw_hr
-
-
-    USE spectrum,                                                              &
-        ONLY:  comp_spectra_level, data_output_sp, spectra_direction
-
-    USE statistics,                                                            &
-        ONLY:  hom, statistic_regions
-
-
-    IMPLICIT NONE
-
-    CHARACTER (LEN=2)              ::  suffix                !<
-    CHARACTER (LEN=2), INTENT (IN) ::  callmode              !<
-    CHARACTER (LEN=3)              ::  suffix1               !<
-    CHARACTER (LEN=4)              ::  grid_x                !<
-    CHARACTER (LEN=4)              ::  grid_y                !<
-    CHARACTER (LEN=4)              ::  grid_z                !<
-    CHARACTER (LEN=6)              ::  mode                  !<
-    CHARACTER (LEN=10)             ::  netcdf_var_name       !<
-    CHARACTER (LEN=10)             ::  precision             !<
-    CHARACTER (LEN=10)             ::  var                   !<
-    CHARACTER (LEN=80)             ::  time_average_text     !<
-    CHARACTER (LEN=2000)           ::  char_cross_profiles   !<
-    CHARACTER (LEN=2000)           ::  var_list              !<
-    CHARACTER (LEN=2000)           ::  var_list_old          !<
-
-    CHARACTER (LEN=100), DIMENSION(1:crmax) ::  cross_profiles_adj   !<
-    CHARACTER (LEN=100), DIMENSION(1:crmax) ::  cross_profiles_char  !<
-
-    INTEGER(iwp) ::  av                                      !<
-    INTEGER(iwp) ::  cross_profiles_count                    !<
-    INTEGER(iwp) ::  cross_profiles_maxi                     !<
-    INTEGER(iwp) ::  delim                                   !<
-    INTEGER(iwp) ::  delim_old                               !<
-    INTEGER(iwp) ::  file_id                                 !<
-    INTEGER(iwp) ::  i                                       !<
-    INTEGER(iwp) ::  id_last                                 !<
-    INTEGER(iwp) ::  id_x                                    !<
-    INTEGER(iwp) ::  id_y                                    !<
-    INTEGER(iwp) ::  id_z                                    !<
-    INTEGER(iwp) ::  j                                       !<
-    INTEGER(iwp) ::  k                                       !<
-    INTEGER(iwp) ::  kk                                      !<
-    INTEGER(iwp) ::  ns                                      !<
-    INTEGER(iwp) ::  ns_do                                   !< actual value of ns for soil model data
-    INTEGER(iwp) ::  ns_old                                  !<
-    INTEGER(iwp) ::  ntime_count                             !< number of time levels found in file
-    INTEGER(iwp) ::  nz_old                                  !<
-
-    INTEGER(iwp), SAVE ::  oldmode                           !<
-
-    INTEGER(iwp), DIMENSION(1) ::  id_dim_time_old           !<
-    INTEGER(iwp), DIMENSION(1) ::  id_dim_x_yz_old           !<
-    INTEGER(iwp), DIMENSION(1) ::  id_dim_y_xz_old           !<
-    INTEGER(iwp), DIMENSION(1) ::  id_dim_zu_sp_old          !<
-    INTEGER(iwp), DIMENSION(1) ::  id_dim_zu_xy_old          !<
-    INTEGER(iwp), DIMENSION(1) ::  id_dim_zu_3d_old          !<
-    INTEGER(iwp), DIMENSION(1) ::  id_dim_zu_mask_old        !<
-
-
-    INTEGER(iwp), DIMENSION(1:crmax) ::  cross_profiles_numb !<
-
-    LOGICAL ::  found                                        !<
-
-    LOGICAL, INTENT (INOUT) ::  extend                       !<
-
-    LOGICAL, SAVE ::  init_netcdf = .FALSE.                  !<
-
-    REAL(wp), DIMENSION(1) ::  last_time_coordinate          !< last time value in file
-
-    REAL(wp), DIMENSION(:), ALLOCATABLE   ::  netcdf_data    !<
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  netcdf_data_2d !<
-
-!
-!-- Initializing actions (return to calling routine check_parameters afterwards)
-    IF ( .NOT. init_netcdf )  THEN
-!
-!--    Check and set accuracy for netCDF output. First set default value
-       nc_precision = NF90_REAL4
-
-       i = 1
-       DO  WHILE ( netcdf_precision(i) /= ' ' )
-          j = INDEX( netcdf_precision(i), '_' )
-          IF ( j == 0 )  THEN
-             WRITE ( message_string, * ) 'netcdf_precision must contain a ', &
-                                         '"_"netcdf_precision(', i, ')="',   &
-                                         TRIM( netcdf_precision(i) ),'"'
-             CALL message( 'define_netcdf_header', 'PA0241', 1, 2, 0, 6, 0 )
-          ENDIF
-
-          var       = netcdf_precision(i)(1:j-1)
-          precision = netcdf_precision(i)(j+1:)
-
-          IF ( precision == 'NF90_REAL4' )  THEN
-             j = NF90_REAL4
-          ELSEIF ( precision == 'NF90_REAL8' )  THEN
-             j = NF90_REAL8
-          ELSE
-             WRITE ( message_string, * ) 'illegal netcdf precision: ',  &
-                                         'netcdf_precision(', i, ')="', &
-                                         TRIM( netcdf_precision(i) ),'"'
-             CALL message( 'define_netcdf_header', 'PA0242', 1, 2, 0, 6, 0 )
-          ENDIF
-
-          SELECT CASE ( var )
-             CASE ( 'xy' )
-                nc_precision(1) = j
-             CASE ( 'xz' )
-                nc_precision(2) = j
-             CASE ( 'yz' )
-                nc_precision(3) = j
-             CASE ( '2d' )
-                nc_precision(1:3) = j
-             CASE ( '3d' )
-                nc_precision(4) = j
-             CASE ( 'pr' )
-                nc_precision(5) = j
-             CASE ( 'ts' )
-                nc_precision(6) = j
-             CASE ( 'sp' )
-                nc_precision(7) = j
-             CASE ( 'prt' )
-                nc_precision(8) = j
-             CASE ( 'masks' )
-                nc_precision(11) = j
-             CASE ( 'all' )
-                nc_precision    = j
-
-             CASE DEFAULT
-                WRITE ( message_string, * ) 'unknown variable in inipar ',    & 
-                                  'assignment: netcdf_precision(', i, ')="',  &
-                                            TRIM( netcdf_precision(i) ),'"'
-                CALL message( 'define_netcdf_header', 'PA0243', 1, 2, 0, 6, 0 ) 
-
-          END SELECT
-
-          i = i + 1
-          IF ( i > 50 )  EXIT
-       ENDDO
-
-       init_netcdf = .TRUE.
-
-       RETURN
-
-    ENDIF
-
-!
-!-- Determine the mode to be processed
-    IF ( extend )  THEN
-       mode = callmode // '_ext'
-    ELSE
-       mode = callmode // '_new'
-    ENDIF
-
-!
-!-- Select the mode to be processed. Possibilities are 3d, mask, xy, xz, yz, 
-!-- pr and ts.
-    SELECT CASE ( mode )
-
-       CASE ( 'ma_new' )
-
-!
-!--       decompose actual parameter file_id (=formal parameter av) into 
-!--       mid and av
-          file_id = av
-          IF ( file_id <= 200+max_masks )  THEN
-             mid = file_id - 200
-             av = 0
-          ELSE
-             mid = file_id - (200+max_masks)
-             av = 1
-          ENDIF
-
-!
-!--       Define some global attributes of the dataset
-          nc_stat = NF90_PUT_ATT( id_set_mask(mid,av), NF90_GLOBAL, &
-               'Conventions', 'COARDS' )
-          CALL handle_netcdf_error( 'netcdf', 464 )
-
-          IF ( av == 0 )  THEN
-             time_average_text = ' '
-          ELSE
-             WRITE (time_average_text, '('', '',F7.1,'' s average'')') &
-                                                            averaging_interval
-          ENDIF
-          nc_stat = NF90_PUT_ATT( id_set_mask(mid,av), NF90_GLOBAL, 'title', &
-                                  TRIM( run_description_header ) //    &
-                                  TRIM( time_average_text ) )
-          CALL handle_netcdf_error( 'netcdf', 465 )
-          IF ( av == 1 )  THEN
-             WRITE ( time_average_text,'(F7.1,'' s avg'')' )  averaging_interval
-             nc_stat = NF90_PUT_ATT( id_set_mask(mid,av), NF90_GLOBAL, &
-                                     'time_avg', TRIM( time_average_text ) )
-             CALL handle_netcdf_error( 'netcdf', 466 )
-          ENDIF
-
-!
-!--       Define time coordinate for volume data (unlimited dimension)
-          nc_stat = NF90_DEF_DIM( id_set_mask(mid,av), 'time', NF90_UNLIMITED, &
-                                  id_dim_time_mask(mid,av) )
-          CALL handle_netcdf_error( 'netcdf', 467 )
-
-          nc_stat = NF90_DEF_VAR( id_set_mask(mid,av), 'time', NF90_DOUBLE, &
-                                  id_dim_time_mask(mid,av), &
-                                  id_var_time_mask(mid,av) )
-          CALL handle_netcdf_error( 'netcdf', 468 )
-
-          nc_stat = NF90_PUT_ATT( id_set_mask(mid,av), &
-                                  id_var_time_mask(mid,av), 'units', &
-                                  'seconds')
-          CALL handle_netcdf_error( 'netcdf', 469 )
-
-!
-!--       Define spatial dimensions and coordinates:
-!--       Define vertical coordinate grid (zu grid)
-          nc_stat = NF90_DEF_DIM( id_set_mask(mid,av), 'zu_3d', &
-                                  mask_size(mid,3), id_dim_zu_mask(mid,av) )
-          CALL handle_netcdf_error( 'netcdf', 470 )
-
-          nc_stat = NF90_DEF_VAR( id_set_mask(mid,av), 'zu_3d', NF90_DOUBLE, &
-                                  id_dim_zu_mask(mid,av), &
-                                  id_var_zu_mask(mid,av) )
-          CALL handle_netcdf_error( 'netcdf', 471 )
-
-          nc_stat = NF90_PUT_ATT( id_set_mask(mid,av), id_var_zu_mask(mid,av), &
-                                  'units', 'meters' )
-          CALL handle_netcdf_error( 'netcdf', 472 )
-
-!
-!--       Define vertical coordinate grid (zw grid)
-          nc_stat = NF90_DEF_DIM( id_set_mask(mid,av), 'zw_3d', &
-                                  mask_size(mid,3), id_dim_zw_mask(mid,av) )
-          CALL handle_netcdf_error( 'netcdf', 473 )
-
-          nc_stat = NF90_DEF_VAR( id_set_mask(mid,av), 'zw_3d', NF90_DOUBLE, &
-                                  id_dim_zw_mask(mid,av), &
-                                  id_var_zw_mask(mid,av) )
-          CALL handle_netcdf_error( 'netcdf', 474 )
-
-          nc_stat = NF90_PUT_ATT( id_set_mask(mid,av), id_var_zw_mask(mid,av), &
-                                  'units', 'meters' )
-          CALL handle_netcdf_error( 'netcdf', 475 )
-
-!
-!--       Define x-axis (for scalar position)
-          nc_stat = NF90_DEF_DIM( id_set_mask(mid,av), 'x', &
-                                  mask_size(mid,1), id_dim_x_mask(mid,av) )
-          CALL handle_netcdf_error( 'netcdf', 476 )
-
-          nc_stat = NF90_DEF_VAR( id_set_mask(mid,av), 'x', NF90_DOUBLE, &
-                                  id_dim_x_mask(mid,av), id_var_x_mask(mid,av) )
-          CALL handle_netcdf_error( 'netcdf', 477 )
-
-          nc_stat = NF90_PUT_ATT( id_set_mask(mid,av), id_var_x_mask(mid,av), &
-                                  'units', 'meters' )
-          CALL handle_netcdf_error( 'netcdf', 478 )
-
-!
-!--       Define x-axis (for u position)
-          nc_stat = NF90_DEF_DIM( id_set_mask(mid,av), 'xu', &
-                                  mask_size(mid,1), id_dim_xu_mask(mid,av) )
-          CALL handle_netcdf_error( 'netcdf', 479 )
-
-          nc_stat = NF90_DEF_VAR( id_set_mask(mid,av), 'xu', NF90_DOUBLE, &
-                                  id_dim_xu_mask(mid,av), &
-                                  id_var_xu_mask(mid,av) )
-          CALL handle_netcdf_error( 'netcdf', 480 )
-
-          nc_stat = NF90_PUT_ATT( id_set_mask(mid,av), id_var_xu_mask(mid,av), &
-                                  'units', 'meters' )
-          CALL handle_netcdf_error( 'netcdf', 481 )
-
-!
-!--       Define y-axis (for scalar position)
-          nc_stat = NF90_DEF_DIM( id_set_mask(mid,av), 'y', &
-                                  mask_size(mid,2), id_dim_y_mask(mid,av) )
-          CALL handle_netcdf_error( 'netcdf', 482 )
-
-          nc_stat = NF90_DEF_VAR( id_set_mask(mid,av), 'y', NF90_DOUBLE, &
-                                  id_dim_y_mask(mid,av), id_var_y_mask(mid,av) )
-          CALL handle_netcdf_error( 'netcdf', 483 )
-
-          nc_stat = NF90_PUT_ATT( id_set_mask(mid,av), id_var_y_mask(mid,av), &
-                                  'units', 'meters' )
-          CALL handle_netcdf_error( 'netcdf', 484 )
-
-!
-!--       Define y-axis (for v position)
-          nc_stat = NF90_DEF_DIM( id_set_mask(mid,av), 'yv', &
-                                  mask_size(mid,2), id_dim_yv_mask(mid,av) )
-          CALL handle_netcdf_error( 'netcdf', 485 )
-
-          nc_stat = NF90_DEF_VAR( id_set_mask(mid,av), 'yv', NF90_DOUBLE, &
-                                  id_dim_yv_mask(mid,av), &
-                                  id_var_yv_mask(mid,av) )
-          CALL handle_netcdf_error( 'netcdf', 486 )
-
-          nc_stat = NF90_PUT_ATT( id_set_mask(mid,av), id_var_yv_mask(mid,av), &
-                                  'units', 'meters' )
-          CALL handle_netcdf_error( 'netcdf', 487 )
-
-!
-!--       In case of non-flat topography define 2d-arrays containing the height
-!--       information
-          IF ( TRIM( topography ) /= 'flat' )  THEN
-!
-!--          Define zusi = zu(nzb_s_inner)
-             nc_stat = NF90_DEF_VAR( id_set_mask(mid,av), 'zusi', NF90_DOUBLE, &
-                                     (/ id_dim_x_mask(mid,av),    &
-                                        id_dim_y_mask(mid,av) /), &
-                                     id_var_zusi_mask(mid,av) )
-             CALL handle_netcdf_error( 'netcdf', 488 )
-             
-             nc_stat = NF90_PUT_ATT( id_set_mask(mid,av), &
-                                     id_var_zusi_mask(mid,av), &
-                                     'units', 'meters' )
-             CALL handle_netcdf_error( 'netcdf', 489 )
-             
-             nc_stat = NF90_PUT_ATT( id_set_mask(mid,av), &
-                                     id_var_zusi_mask(mid,av), &
-                                     'long_name', 'zu(nzb_s_inner)' )
-             CALL handle_netcdf_error( 'netcdf', 490 )
-
-!             
-!--          Define zwwi = zw(nzb_w_inner)
-             nc_stat = NF90_DEF_VAR( id_set_mask(mid,av), 'zwwi', NF90_DOUBLE, &
-                                     (/ id_dim_x_mask(mid,av),    &
-                                        id_dim_y_mask(mid,av) /), &
-                                     id_var_zwwi_mask(mid,av) )
-             CALL handle_netcdf_error( 'netcdf', 491 )
-             
-             nc_stat = NF90_PUT_ATT( id_set_mask(mid,av), &
-                                     id_var_zwwi_mask(mid,av), &
-                                     'units', 'meters' )
-             CALL handle_netcdf_error( 'netcdf', 492 )
-             
-             nc_stat = NF90_PUT_ATT( id_set_mask(mid,av), &
-                                     id_var_zwwi_mask(mid,av), &
-                                     'long_name', 'zw(nzb_w_inner)' )
-             CALL handle_netcdf_error( 'netcdf', 493 )
-
-          ENDIF             
- 
-          IF ( land_surface )  THEN
-!
-!--          Define vertical coordinate grid (zw grid)
-             nc_stat = NF90_DEF_DIM( id_set_mask(mid,av), 'zs_3d', &
-                                     mask_size(mid,3), id_dim_zs_mask(mid,av) )
-             CALL handle_netcdf_error( 'netcdf', 536 )
-
-             nc_stat = NF90_DEF_VAR( id_set_mask(mid,av), 'zs_3d', NF90_DOUBLE, &
-                                     id_dim_zs_mask(mid,av), &
-                                     id_var_zs_mask(mid,av) )
-             CALL handle_netcdf_error( 'netcdf', 536 )
-
-             nc_stat = NF90_PUT_ATT( id_set_mask(mid,av), id_var_zs_mask(mid,av), &
-                                  'units', 'meters' )
-             CALL handle_netcdf_error( 'netcdf', 537 )
-
-          ENDIF
-
-!
-!--       Define the variables
-          var_list = ';'
-          i = 1
-
-          DO WHILE ( domask(mid,av,i)(1:1) /= ' ' )
-
-!
-!--          Check for the grid
-             found = .TRUE.
-             SELECT CASE ( domask(mid,av,i) )
-!
-!--             Most variables are defined on the scalar grid
-                CASE ( 'e', 'lpt', 'nr', 'p', 'pc', 'pr', 'prr', 'pt', 'q',    &
-                       'qc', 'ql', 'ql_c', 'ql_v', 'ql_vp', 'qr', 'qv',        &
-                       'rad_lw_cs_hr', 'rad_lw_hr', 'rad_sw_cs_hr',            &
-                       'rad_sw_hr', 'rho', 's', 'sa', 'vpt'  )
-
-                   grid_x = 'x'
-                   grid_y = 'y'
-                   grid_z = 'zu'
-!
-!--             u grid
-                CASE ( 'u' )
-
-                   grid_x = 'xu'
-                   grid_y = 'y'
-                   grid_z = 'zu'
-!
-!--             v grid
-                CASE ( 'v' )
-
-                   grid_x = 'x'
-                   grid_y = 'yv'
-                   grid_z = 'zu'
-!
-!--             w grid
-                CASE ( 'rad_lw_in', 'rad_lw_out', 'rad_sw_in', 'rad_sw_out',   &
-                       'w' )
-
-                   grid_x = 'x'
-                   grid_y = 'y'
-                   grid_z = 'zw'
-!
-!--             soil grid
-                CASE ( 'm_soil', 't_soil' )
-
-                   grid_x = 'x'
-                   grid_y = 'y'
-                   grid_z = 'zs'
-
-                CASE DEFAULT
-!
-!--                Check for user-defined quantities
-                   CALL user_define_netcdf_grid( domask(mid,av,i), found, &
-                                                 grid_x, grid_y, grid_z )
-
-             END SELECT
-
-!
-!--          Select the respective dimension ids
-             IF ( grid_x == 'x' )  THEN
-                id_x = id_dim_x_mask(mid,av)
-             ELSEIF ( grid_x == 'xu' )  THEN
-                id_x = id_dim_xu_mask(mid,av)
-             ENDIF
-
-             IF ( grid_y == 'y' )  THEN
-                id_y = id_dim_y_mask(mid,av)
-             ELSEIF ( grid_y == 'yv' )  THEN
-                id_y = id_dim_yv_mask(mid,av)
-             ENDIF
-
-             IF ( grid_z == 'zu' )  THEN
-                id_z = id_dim_zu_mask(mid,av)
-             ELSEIF ( grid_z == 'zw' )  THEN
-                id_z = id_dim_zw_mask(mid,av)
-             ELSEIF ( grid_z == "zs" )  THEN
-                id_z = id_dim_zs_mask(mid,av)
-             ENDIF
-
-!
-!--          Define the grid
-             nc_stat = NF90_DEF_VAR( id_set_mask(mid,av), domask(mid,av,i), &
-                                     nc_precision(11),            &
-                                     (/ id_x, id_y, id_z,                   &
-                                        id_dim_time_mask(mid,av) /),        &
-                                     id_var_domask(mid,av,i) )
-
-             IF ( .NOT. found )  THEN
-                WRITE ( message_string, * ) 'no grid defined for', &
-                     ' variable ', TRIM( domask(mid,av,i) )
-                CALL message( 'define_netcdf_header', 'PA0244', 0, 1, 0, 6, 0 )
-             ENDIF
-
-             var_list = TRIM( var_list ) // TRIM( domask(mid,av,i) ) // ';'
-
-             CALL handle_netcdf_error( 'netcdf', 494 )
-!
-!--          Store the 'real' name of the variable (with *, for example)
-!--          in the long_name attribute. This is evaluated by Ferret,
-!--          for example.
-             nc_stat = NF90_PUT_ATT( id_set_mask(mid,av),     &
-                                     id_var_domask(mid,av,i), &
-                                     'long_name', domask(mid,av,i) )
-             CALL handle_netcdf_error( 'netcdf', 495 )
-!
-!--          Define the variable's unit
-             nc_stat = NF90_PUT_ATT( id_set_mask(mid,av), &
-                                     id_var_domask(mid,av,i), &
-                                     'units', TRIM( domask_unit(mid,av,i) ) )
-             CALL handle_netcdf_error( 'netcdf', 496 )
-
-             i = i + 1
-
-          ENDDO
-
-!
-!--       No arrays to output
-          IF ( i == 1 )  RETURN
-
-!
-!--       Write the list of variables as global attribute (this is used by
-!--       restart runs and by combine_plot_fields)
-          nc_stat = NF90_PUT_ATT( id_set_mask(mid,av), NF90_GLOBAL, &
-                                  'VAR_LIST', var_list )
-          CALL handle_netcdf_error( 'netcdf', 497 )
-
-!
-!--       Leave netCDF define mode
-          nc_stat = NF90_ENDDEF( id_set_mask(mid,av) )
-          CALL handle_netcdf_error( 'netcdf', 498 )
-
-!
-!--       Write data for x (shifted by +dx/2) and xu axis
-          ALLOCATE( netcdf_data(mask_size(mid,1)) )
-
-          netcdf_data = ( mask_i_global(mid,:mask_size(mid,1)) + 0.5_wp ) * dx
-
-          nc_stat = NF90_PUT_VAR( id_set_mask(mid,av), id_var_x_mask(mid,av), &
-                                  netcdf_data, start = (/ 1 /),               &
-                                  count = (/ mask_size(mid,1) /) )
-          CALL handle_netcdf_error( 'netcdf', 499 )
-
-          netcdf_data = mask_i_global(mid,:mask_size(mid,1)) * dx
-
-          nc_stat = NF90_PUT_VAR( id_set_mask(mid,av), id_var_xu_mask(mid,av),&
-                                  netcdf_data, start = (/ 1 /),               &
-                                  count = (/ mask_size(mid,1) /) )
-          CALL handle_netcdf_error( 'netcdf', 500 )
-
-          DEALLOCATE( netcdf_data )
-
-!
-!--       Write data for y (shifted by +dy/2) and yv axis
-          ALLOCATE( netcdf_data(mask_size(mid,2)) )
-
-          netcdf_data = ( mask_j_global(mid,:mask_size(mid,2)) + 0.5_wp ) * dy
-
-          nc_stat = NF90_PUT_VAR( id_set_mask(mid,av), id_var_y_mask(mid,av), &
-                                  netcdf_data, start = (/ 1 /),               &
-                                  count = (/ mask_size(mid,2) /))
-          CALL handle_netcdf_error( 'netcdf', 501 )
-
-          netcdf_data = mask_j_global(mid,:mask_size(mid,2)) * dy
-
-          nc_stat = NF90_PUT_VAR( id_set_mask(mid,av), id_var_yv_mask(mid,av), &
-                                  netcdf_data, start = (/ 1 /),    &
-                                  count = (/ mask_size(mid,2) /))
-          CALL handle_netcdf_error( 'netcdf', 502 )
-
-          DEALLOCATE( netcdf_data )
-
-!
-!--       Write zu and zw data (vertical axes)
-          ALLOCATE( netcdf_data(mask_size(mid,3)) )
-
-          netcdf_data = zu( mask_k_global(mid,:mask_size(mid,3)) )
-
-          nc_stat = NF90_PUT_VAR( id_set_mask(mid,av), id_var_zu_mask(mid,av), &
-                                  netcdf_data, start = (/ 1 /), &
-                                  count = (/ mask_size(mid,3) /) )
-          CALL handle_netcdf_error( 'netcdf', 503 )
-
-          netcdf_data = zw( mask_k_global(mid,:mask_size(mid,3)) )
-
-          nc_stat = NF90_PUT_VAR( id_set_mask(mid,av), id_var_zw_mask(mid,av), &
-                                  netcdf_data, start = (/ 1 /), &
-                                  count = (/ mask_size(mid,3) /) )
-          CALL handle_netcdf_error( 'netcdf', 504 )
-
-          DEALLOCATE( netcdf_data )
-
-!
-!--       In case of non-flat topography write height information
-          IF ( TRIM( topography ) /= 'flat' )  THEN
-
-             ALLOCATE( netcdf_data_2d(mask_size(mid,1),mask_size(mid,2)) )
-             netcdf_data_2d = zu_s_inner( mask_i_global(mid,:mask_size(mid,1)),&
-                                          mask_j_global(mid,:mask_size(mid,2)) )
-
-             nc_stat = NF90_PUT_VAR( id_set_mask(mid,av),         &
-                                     id_var_zusi_mask(mid,av),    &
-                                     netcdf_data_2d,              &
-                                     start = (/ 1, 1 /),          &
-                                     count = (/ mask_size(mid,1), &
-                                                mask_size(mid,2) /) )
-             CALL handle_netcdf_error( 'netcdf', 505 )
-
-             netcdf_data_2d = zw_w_inner( mask_i_global(mid,:mask_size(mid,1)),&
-                                          mask_j_global(mid,:mask_size(mid,2)) )
-
-             nc_stat = NF90_PUT_VAR( id_set_mask(mid,av),         &
-                                     id_var_zwwi_mask(mid,av),    &
-                                     netcdf_data_2d,              &
-                                     start = (/ 1, 1 /),          &
-                                     count = (/ mask_size(mid,1), &
-                                                mask_size(mid,2) /) )
-             CALL handle_netcdf_error( 'netcdf', 506 )
-
-             DEALLOCATE( netcdf_data_2d )
-
-          ENDIF
-
-          IF ( land_surface )  THEN
-!
-!--          Write zs data (vertical axes for soil model), use negative values
-!--          to indicate soil depth
-             ALLOCATE( netcdf_data(mask_size(mid,3)) )
-
-             netcdf_data = zs( mask_k_global(mid,:mask_size(mid,3)) )
-
-             nc_stat = NF90_PUT_VAR( id_set_mask(mid,av), id_var_zs_mask(mid,av), &
-                                     netcdf_data, start = (/ 1 /), &
-                                     count = (/ mask_size(mid,3) /) )
-             CALL handle_netcdf_error( 'netcdf', 538 )
-
-             DEALLOCATE( netcdf_data )
-
-          ENDIF
-
-!
-!--       restore original parameter file_id (=formal parameter av) into av
-          av = file_id
-
-
-       CASE ( 'ma_ext' )
-
-!
-!--       decompose actual parameter file_id (=formal parameter av) into 
-!--       mid and av
-          file_id = av
-          IF ( file_id <= 200+max_masks )  THEN
-             mid = file_id - 200
-             av = 0
-          ELSE
-             mid = file_id - (200+max_masks)
-             av = 1
-          ENDIF
-
-!
-!--       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_mask(mid,av), NF90_GLOBAL, 'VAR_LIST',&
-                                  var_list_old )
-          CALL handle_netcdf_error( 'netcdf', 507 )
-
-          var_list = ';'
-          i = 1
-          DO WHILE ( domask(mid,av,i)(1:1) /= ' ' )
-             var_list = TRIM(var_list) // TRIM( domask(mid,av,i) ) // ';'
-             i = i + 1
-          ENDDO
-
-          IF ( av == 0 )  THEN
-             var = '(mask)'
-          ELSE
-             var = '(mask_av)'
-          ENDIF
-
-          IF ( TRIM( var_list ) /= TRIM( var_list_old ) )  THEN
-             WRITE ( message_string, * ) 'netCDF file for ', TRIM( var ), &
-                  ' data for mask', mid, ' from previous run found,', &
-                  '&but this file cannot be extended due to variable ', &
-                  'mismatch.&New file is created instead.'
-             CALL message( 'define_netcdf_header', 'PA0335', 0, 1, 0, 6, 0 )
-             extend = .FALSE.
-             RETURN
-          ENDIF
-
-!
-!--       Get and compare the number of vertical gridpoints
-          nc_stat = NF90_INQ_VARID( id_set_mask(mid,av), 'zu_3d', &
-                                    id_var_zu_mask(mid,av) )
-          CALL handle_netcdf_error( 'netcdf', 508 )
-
-          nc_stat = NF90_INQUIRE_VARIABLE( id_set_mask(mid,av),     &
-                                           id_var_zu_mask(mid,av),  &
-                                           dimids = id_dim_zu_mask_old )
-          CALL handle_netcdf_error( 'netcdf', 509 )
-          id_dim_zu_mask(mid,av) = id_dim_zu_mask_old(1)
-
-          nc_stat = NF90_INQUIRE_DIMENSION( id_set_mask(mid,av),    &
-                                            id_dim_zu_mask(mid,av), &
-                                            len = nz_old )
-          CALL handle_netcdf_error( 'netcdf', 510 )
-
-          IF ( mask_size(mid,3) /= nz_old )  THEN
-             WRITE ( message_string, * ) 'netCDF file for ', TRIM( var ), &
-                  ' data for mask', mid, ' from previous run found,', &
-                  '&but this file cannot be extended due to mismatch in ', &
-                  ' number of&vertical grid points.', &
-                  '&New file is created instead.'
-             CALL message( 'define_netcdf_header', 'PA0336', 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 plmask..count+1.
-!--       The current time must be larger than the last output time
-!--       on the file.
-          nc_stat = NF90_INQ_VARID( id_set_mask(mid,av), 'time', &
-                                    id_var_time_mask(mid,av) )
-          CALL handle_netcdf_error( 'netcdf', 511 )
-
-          nc_stat = NF90_INQUIRE_VARIABLE( id_set_mask(mid,av), &
-                                           id_var_time_mask(mid,av), &
-                                           dimids = id_dim_time_old )
-          CALL handle_netcdf_error( 'netcdf', 512 )
-          id_dim_time_mask(mid,av) = id_dim_time_old(1)
-
-          nc_stat = NF90_INQUIRE_DIMENSION( id_set_mask(mid,av), &
-                                            id_dim_time_mask(mid,av), &
-                                            len = domask_time_count(mid,av) )
-          CALL handle_netcdf_error( 'netcdf', 513 )
-
-          nc_stat = NF90_GET_VAR( id_set_mask(mid,av), &
-                                  id_var_time_mask(mid,av), &
-                                  last_time_coordinate,              &
-                                  start = (/ domask_time_count(mid,av) /), &
-                                  count = (/ 1 /) )
-          CALL handle_netcdf_error( 'netcdf', 514 )
-
-          IF ( last_time_coordinate(1) >= simulated_time )  THEN
-             WRITE ( message_string, * ) 'netCDF file for ', TRIM( var ), &
-                  ' data for mask', mid, ' from previous run found,', &
-                  '&but this file cannot be extended because the current ', &
-                  'output time&is less or equal than the last output time ', &
-                  'on this file.&New file is created instead.'
-             CALL message( 'define_netcdf_header', 'PA0337', 0, 1, 0, 6, 0 )
-             domask_time_count(mid,av) = 0
-             extend = .FALSE.
-             RETURN
-          ENDIF
-
-!
-!--       Dataset seems to be extendable.
-!--       Now get the variable ids.
-          i = 1
-          DO WHILE ( domask(mid,av,i)(1:1) /= ' ' )
-             nc_stat = NF90_INQ_VARID( id_set_mask(mid,av), &
-                                       TRIM( domask(mid,av,i) ), &
-                                       id_var_domask(mid,av,i) )
-             CALL handle_netcdf_error( 'netcdf', 515 )
-             i = i + 1
-          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.
-          IF ( av == 0 )  THEN
-             time_average_text = ' '
-          ELSE
-             WRITE (time_average_text, '('', '',F7.1,'' s average'')') &
-                                                            averaging_interval
-          ENDIF
-          nc_stat = NF90_REDEF( id_set_mask(mid,av) )
-          CALL handle_netcdf_error( 'netcdf', 516 )
-          nc_stat = NF90_PUT_ATT( id_set_mask(mid,av), NF90_GLOBAL, 'title', &
-                                  TRIM( run_description_header ) //    &
-                                  TRIM( time_average_text ) )
-          CALL handle_netcdf_error( 'netcdf', 517 )
-          nc_stat = NF90_ENDDEF( id_set_mask(mid,av) )
-          CALL handle_netcdf_error( 'netcdf', 518 )
-          WRITE ( message_string, * ) 'netCDF file for ', TRIM( var ), &
-               ' data for mask', mid, ' from previous run found.', &
-               '&This file will be extended.'
-          CALL message( 'define_netcdf_header', 'PA0338', 0, 0, 0, 6, 0 )
-!
-!--       restore original parameter file_id (=formal parameter av) into av
-          av = file_id
-
-
-       CASE ( '3d_new' )
-
-!
-!--       Define some global attributes of the dataset
-          nc_stat = NF90_PUT_ATT( id_set_3d(av), NF90_GLOBAL, 'Conventions', &
-                                  'COARDS' )
-          CALL handle_netcdf_error( 'netcdf', 62 )
-
-          IF ( av == 0 )  THEN
-             time_average_text = ' '
-          ELSE
-             WRITE (time_average_text, '('', '',F7.1,'' s average'')') &
-                                                            averaging_interval
-          ENDIF
-          nc_stat = NF90_PUT_ATT( id_set_3d(av), NF90_GLOBAL, 'title', &
-                                  TRIM( run_description_header ) //    &
-                                  TRIM( time_average_text ) )
-          CALL handle_netcdf_error( 'netcdf', 63 )
-          IF ( av == 1 )  THEN
-             WRITE ( time_average_text,'(F7.1,'' s avg'')' )  averaging_interval
-             nc_stat = NF90_PUT_ATT( id_set_3d(av), NF90_GLOBAL, 'time_avg', &
-                                     TRIM( time_average_text ) )
-             CALL handle_netcdf_error( 'netcdf', 63 )
-          ENDIF
-
-!
-!--       Define time coordinate for volume data.
-!--       For parallel output the time dimensions has to be limited, otherwise
-!--       the performance drops significantly.
-          IF ( netcdf_data_format < 5 )  THEN
-             nc_stat = NF90_DEF_DIM( id_set_3d(av), 'time', NF90_UNLIMITED, &
-                                     id_dim_time_3d(av) )
-             CALL handle_netcdf_error( 'netcdf', 64 )
-          ELSE
-             nc_stat = NF90_DEF_DIM( id_set_3d(av), 'time', ntdim_3d(av), &
-                                     id_dim_time_3d(av) )
-             CALL handle_netcdf_error( 'netcdf', 523 )
-          ENDIF
-
-          nc_stat = NF90_DEF_VAR( id_set_3d(av), 'time', NF90_DOUBLE, &
-                                  id_dim_time_3d(av), id_var_time_3d(av) )
-          CALL handle_netcdf_error( 'netcdf', 65 )
-
-          nc_stat = NF90_PUT_ATT( id_set_3d(av), id_var_time_3d(av), 'units', &
-                                  'seconds')
-          CALL handle_netcdf_error( 'netcdf', 66 )
-
-!
-!--       Define spatial dimensions and coordinates:
-!--       Define vertical coordinate grid (zu grid)
-          nc_stat = NF90_DEF_DIM( id_set_3d(av), 'zu_3d', nz_do3d-nzb+1, &
-                                  id_dim_zu_3d(av) )
-          CALL handle_netcdf_error( 'netcdf', 67 )
-
-          nc_stat = NF90_DEF_VAR( id_set_3d(av), 'zu_3d', NF90_DOUBLE, &
-                                  id_dim_zu_3d(av), id_var_zu_3d(av) )
-          CALL handle_netcdf_error( 'netcdf', 68 )
-
-          nc_stat = NF90_PUT_ATT( id_set_3d(av), id_var_zu_3d(av), 'units', &
-                                  'meters' )
-          CALL handle_netcdf_error( 'netcdf', 69 )
-
-!
-!--       Define vertical coordinate grid (zw grid)
-          nc_stat = NF90_DEF_DIM( id_set_3d(av), 'zw_3d', nz_do3d-nzb+1, &
-                                  id_dim_zw_3d(av) )
-          CALL handle_netcdf_error( 'netcdf', 70 )
-
-          nc_stat = NF90_DEF_VAR( id_set_3d(av), 'zw_3d', NF90_DOUBLE, &
-                                  id_dim_zw_3d(av), id_var_zw_3d(av) )
-          CALL handle_netcdf_error( 'netcdf', 71 )
-
-          nc_stat = NF90_PUT_ATT( id_set_3d(av), id_var_zw_3d(av), 'units', &
-                                  'meters' )
-          CALL handle_netcdf_error( 'netcdf', 72 )
-
-!
-!--       Define x-axis (for scalar position)
-          nc_stat = NF90_DEF_DIM( id_set_3d(av), 'x', nx+2, id_dim_x_3d(av) )
-          CALL handle_netcdf_error( 'netcdf', 73 )
-
-          nc_stat = NF90_DEF_VAR( id_set_3d(av), 'x', NF90_DOUBLE, &
-                                  id_dim_x_3d(av), id_var_x_3d(av) )
-          CALL handle_netcdf_error( 'netcdf', 74 )
-
-          nc_stat = NF90_PUT_ATT( id_set_3d(av), id_var_x_3d(av), 'units', &
-                                  'meters' )
-          CALL handle_netcdf_error( 'netcdf', 75 )
-
-!
-!--       Define x-axis (for u position)
-          nc_stat = NF90_DEF_DIM( id_set_3d(av), 'xu', nx+2, id_dim_xu_3d(av) )
-          CALL handle_netcdf_error( 'netcdf', 358 )
-
-          nc_stat = NF90_DEF_VAR( id_set_3d(av), 'xu', NF90_DOUBLE, &
-                                  id_dim_xu_3d(av), id_var_xu_3d(av) )
-          CALL handle_netcdf_error( 'netcdf', 359 )
-
-          nc_stat = NF90_PUT_ATT( id_set_3d(av), id_var_xu_3d(av), 'units', &
-                                  'meters' )
-          CALL handle_netcdf_error( 'netcdf', 360 )
-
-!
-!--       Define y-axis (for scalar position)
-          nc_stat = NF90_DEF_DIM( id_set_3d(av), 'y', ny+2, id_dim_y_3d(av) )
-          CALL handle_netcdf_error( 'netcdf', 76 )
-
-          nc_stat = NF90_DEF_VAR( id_set_3d(av), 'y', NF90_DOUBLE, &
-                                  id_dim_y_3d(av), id_var_y_3d(av) )
-          CALL handle_netcdf_error( 'netcdf', 77 )
-
-          nc_stat = NF90_PUT_ATT( id_set_3d(av), id_var_y_3d(av), 'units', &
-                                  'meters' )
-          CALL handle_netcdf_error( 'netcdf', 78 )
-
-!
-!--       Define y-axis (for v position)
-          nc_stat = NF90_DEF_DIM( id_set_3d(av), 'yv', ny+2, id_dim_yv_3d(av) )
-          CALL handle_netcdf_error( 'netcdf', 361 )
-
-          nc_stat = NF90_DEF_VAR( id_set_3d(av), 'yv', NF90_DOUBLE, &
-                                  id_dim_yv_3d(av), id_var_yv_3d(av) )
-          CALL handle_netcdf_error( 'netcdf', 362 )
-
-          nc_stat = NF90_PUT_ATT( id_set_3d(av), id_var_yv_3d(av), 'units', &
-                                  'meters' )
-          CALL handle_netcdf_error( 'netcdf', 363 )
-
-!
-!--       In case of non-flat topography define 2d-arrays containing the height
-!--       information
-          IF ( TRIM( topography ) /= 'flat' )  THEN
-!
-!--          Define zusi = zu(nzb_s_inner)
-             nc_stat = NF90_DEF_VAR( id_set_3d(av), 'zusi', NF90_DOUBLE,     &
-                                     (/ id_dim_x_3d(av), id_dim_y_3d(av) /), &
-                                     id_var_zusi_3d(av) )
-             CALL handle_netcdf_error( 'netcdf', 413 )
-             
-             nc_stat = NF90_PUT_ATT( id_set_3d(av), id_var_zusi_3d(av), &
-                                     'units', 'meters' )
-             CALL handle_netcdf_error( 'netcdf', 414 )
-             
-             nc_stat = NF90_PUT_ATT( id_set_3d(av), id_var_zusi_3d(av), &
-                                     'long_name', 'zu(nzb_s_inner)' )
-             CALL handle_netcdf_error( 'netcdf', 415 )
-
-!             
-!--          Define zwwi = zw(nzb_w_inner)
-             nc_stat = NF90_DEF_VAR( id_set_3d(av), 'zwwi', NF90_DOUBLE,     &
-                                     (/ id_dim_x_3d(av), id_dim_y_3d(av) /), &
-                                     id_var_zwwi_3d(av) )
-             CALL handle_netcdf_error( 'netcdf', 416 )
-             
-             nc_stat = NF90_PUT_ATT( id_set_3d(av), id_var_zwwi_3d(av), &
-                                     'units', 'meters' )
-             CALL handle_netcdf_error( 'netcdf', 417 )
-             
-             nc_stat = NF90_PUT_ATT( id_set_3d(av), id_var_zwwi_3d(av), &
-                                     'long_name', 'zw(nzb_w_inner)' )
-             CALL handle_netcdf_error( 'netcdf', 418 )
-
-          ENDIF             
-
-          IF ( land_surface )  THEN
-!
-!--          Define vertical coordinate grid (zs grid)
-             nc_stat = NF90_DEF_DIM( id_set_3d(av), 'zs_3d', nzt_soil-nzb_soil+1, &
-                                     id_dim_zs_3d(av) )
-             CALL handle_netcdf_error( 'netcdf', 70 )
-
-             nc_stat = NF90_DEF_VAR( id_set_3d(av), 'zs_3d', NF90_DOUBLE, &
-                                     id_dim_zs_3d(av), id_var_zs_3d(av) )
-             CALL handle_netcdf_error( 'netcdf', 71 )
-
-             nc_stat = NF90_PUT_ATT( id_set_3d(av), id_var_zs_3d(av), 'units', &
-                                     'meters' )
-             CALL handle_netcdf_error( 'netcdf', 72 )
-
-          ENDIF
-
-!
-!--       Define the variables
-          var_list = ';'
-          i = 1
-
-          DO WHILE ( do3d(av,i)(1:1) /= ' ' )
-
-!
-!--          Check for the grid
-             found = .TRUE.
-             SELECT CASE ( do3d(av,i) )
-!
-!--             Most variables are defined on the scalar grid
-                CASE ( 'e', 'lpt', 'nr', 'p', 'pc', 'pr', 'prr', 'pt', 'q',    &
-                       'qc', 'ql', 'ql_c', 'ql_v', 'ql_vp', 'qr', 'qv', 'rho', &
-                       's', 'sa', 'vpt' , 'rad_lw_cs_hr', 'rad_lw_hr',         &
-                       'rad_sw_cs_hr', 'rad_sw_hr' )
-
-                   grid_x = 'x'
-                   grid_y = 'y'
-                   grid_z = 'zu'
-!
-!--             u grid
-                CASE ( 'u' )
-
-                   grid_x = 'xu'
-                   grid_y = 'y'
-                   grid_z = 'zu'
-!
-!--             v grid
-                CASE ( 'v' )
-
-                   grid_x = 'x'
-                   grid_y = 'yv'
-                   grid_z = 'zu'
-!
-!--             w grid
-                CASE ( 'rad_lw_in', 'rad_lw_out', 'rad_sw_in', 'rad_sw_out',   &
-                       'w' )
-
-                   grid_x = 'x'
-                   grid_y = 'y'
-                   grid_z = 'zw'
-!
-!--             soil grid
-                CASE ( 'm_soil', 't_soil' )
-
-                   grid_x = 'x'
-                   grid_y = 'y'
-                   grid_z = 'zs'
-
-                CASE DEFAULT
-!
-!--                Check for user-defined quantities
-                   CALL user_define_netcdf_grid( do3d(av,i), found, grid_x, &
-                                                 grid_y, grid_z )
-
-             END SELECT
-
-!
-!--          Select the respective dimension ids
-             IF ( grid_x == 'x' )  THEN
-                id_x = id_dim_x_3d(av)
-             ELSEIF ( grid_x == 'xu' )  THEN
-                id_x = id_dim_xu_3d(av)
-             ENDIF
-
-             IF ( grid_y == 'y' )  THEN
-                id_y = id_dim_y_3d(av)
-             ELSEIF ( grid_y == 'yv' )  THEN
-                id_y = id_dim_yv_3d(av)
-             ENDIF
-
-             IF ( grid_z == 'zu' )  THEN
-                id_z = id_dim_zu_3d(av)
-             ELSEIF ( grid_z == 'zw' )  THEN
-                id_z = id_dim_zw_3d(av)
-             ELSEIF ( grid_z == 'zs' )  THEN
-                id_z = id_dim_zs_3d(av)
-             ENDIF
-
-!
-!--          Define the grid
-             nc_stat = NF90_DEF_VAR( id_set_3d(av), do3d(av,i),                &
-                                     nc_precision(4),                          &
-                                   (/ id_x, id_y, id_z, id_dim_time_3d(av) /), &
-                                     id_var_do3d(av,i) )
-
-             IF ( .NOT. found )  THEN
-                WRITE ( message_string, * ) 'no grid defined for', &
-                                            ' variable ', TRIM( do3d(av,i) )
-                CALL message( 'define_netcdf_header', 'PA0244', 0, 1, 0, 6, 0 )
-             ENDIF
-
-             var_list = TRIM( var_list ) // TRIM( do3d(av,i) ) // ';'
-
-             CALL handle_netcdf_error( 'netcdf', 79 )
-!
-!--          Store the 'real' name of the variable (with *, for example)
-!--          in the long_name attribute. This is evaluated by Ferret,
-!--          for example.
-             nc_stat = NF90_PUT_ATT( id_set_3d(av), id_var_do3d(av,i), &
-                                     'long_name', do3d(av,i) )
-             CALL handle_netcdf_error( 'netcdf', 80 )
-!
-!--          Define the variable's unit
-             nc_stat = NF90_PUT_ATT( id_set_3d(av), id_var_do3d(av,i), &
-                                     'units', TRIM( do3d_unit(av,i) ) )
-             CALL handle_netcdf_error( 'netcdf', 357 )
-#if defined( __netcdf4_parallel )
-             IF ( netcdf_data_format > 4 )  THEN
-!
-!--             Set no fill for every variable to increase performance.
-                nc_stat = NF90_DEF_VAR_FILL( id_set_3d(av),     &
-                                             id_var_do3d(av,i), &
-                                             1, 0 )
-                CALL handle_netcdf_error( 'netcdf', 532 )
-!
-!--             Set collective io operations for parallel io
-                nc_stat = NF90_VAR_PAR_ACCESS( id_set_3d(av),     &
-                                               id_var_do3d(av,i), &
-                                               NF90_COLLECTIVE )
-                CALL handle_netcdf_error( 'netcdf', 445 )
-             ENDIF
-#endif
-             i = i + 1
-
-          ENDDO
-
-!
-!--       No arrays to output
-          IF ( i == 1 )  RETURN
-
-!
-!--       Write the list of variables as global attribute (this is used by
-!--       restart runs and by combine_plot_fields)
-          nc_stat = NF90_PUT_ATT( id_set_3d(av), NF90_GLOBAL, 'VAR_LIST', &
-                                  var_list )
-          CALL handle_netcdf_error( 'netcdf', 81 )
-
-!
-!--       Set general no fill, otherwise the performance drops significantly for
-!--       parallel output.
-          nc_stat = NF90_SET_FILL( id_set_3d(av), NF90_NOFILL, oldmode )
-          CALL handle_netcdf_error( 'netcdf', 528 )
-
-!
-!--       Leave netCDF define mode
-          nc_stat = NF90_ENDDEF( id_set_3d(av) )
-          CALL handle_netcdf_error( 'netcdf', 82 )
-
-!
-!--       These data are only written by PE0 for parallel output to increase
-!--       the performance.
-          IF ( myid == 0  .OR.  netcdf_data_format < 5 )  THEN
-!
-!--          Write data for x (shifted by +dx/2) and xu axis
-             ALLOCATE( netcdf_data(0:nx+1) )
-
-             DO  i = 0, nx+1
-                netcdf_data(i) = ( i + 0.5 ) * dx
-             ENDDO
-
-             nc_stat = NF90_PUT_VAR( id_set_3d(av), id_var_x_3d(av),  &
-                                     netcdf_data, start = (/ 1 /),    &
-                                     count = (/ nx+2 /) )
-             CALL handle_netcdf_error( 'netcdf', 83 )
-
-             DO  i = 0, nx+1
-                netcdf_data(i) = i * dx
-             ENDDO
-
-             nc_stat = NF90_PUT_VAR( id_set_3d(av), id_var_xu_3d(av), &
-                                     netcdf_data, start = (/ 1 /),    &
-                                     count = (/ nx+2 /) )
-             CALL handle_netcdf_error( 'netcdf', 385 )
-
-             DEALLOCATE( netcdf_data )
-
-!
-!--          Write data for y (shifted by +dy/2) and yv axis
-             ALLOCATE( netcdf_data(0:ny+1) )
-
-             DO  i = 0, ny+1
-                netcdf_data(i) = ( i + 0.5_wp ) * dy
-             ENDDO
-
-             nc_stat = NF90_PUT_VAR( id_set_3d(av), id_var_y_3d(av),  &
-                                     netcdf_data, start = (/ 1 /),    &
-                                     count = (/ ny+2 /) )
-             CALL handle_netcdf_error( 'netcdf', 84 )
-
-             DO  i = 0, ny+1
-                netcdf_data(i) = i * dy
-             ENDDO
-
-             nc_stat = NF90_PUT_VAR( id_set_3d(av), id_var_yv_3d(av), &
-                                     netcdf_data, start = (/ 1 /),    &
-                                     count = (/ ny+2 /))
-             CALL handle_netcdf_error( 'netcdf', 387 )
-
-             DEALLOCATE( netcdf_data )
-
-!
-!--          Write zu and zw data (vertical axes)
-             nc_stat = NF90_PUT_VAR( id_set_3d(av), id_var_zu_3d(av),  &
-                                     zu(nzb:nz_do3d), start = (/ 1 /), &
-                                     count = (/ nz_do3d-nzb+1 /) )
-             CALL handle_netcdf_error( 'netcdf', 85 )
-
-
-             nc_stat = NF90_PUT_VAR( id_set_3d(av), id_var_zw_3d(av),  &
-                                     zw(nzb:nz_do3d), start = (/ 1 /), &
-                                     count = (/ nz_do3d-nzb+1 /) )
-             CALL handle_netcdf_error( 'netcdf', 86 )
-
-!
-!--          In case of non-flat topography write height information
-             IF ( TRIM( topography ) /= 'flat' )  THEN
-
-                nc_stat = NF90_PUT_VAR( id_set_3d(av), id_var_zusi_3d(av), &
-                                        zu_s_inner(0:nx+1,0:ny+1), &
-                                        start = (/ 1, 1 /), &
-                                        count = (/ nx+2, ny+2 /) )
-                CALL handle_netcdf_error( 'netcdf', 419 )
-
-                nc_stat = NF90_PUT_VAR( id_set_3d(av), id_var_zwwi_3d(av), &
-                                        zw_w_inner(0:nx+1,0:ny+1), &
-                                        start = (/ 1, 1 /), &
-                                        count = (/ nx+2, ny+2 /) )
-                CALL handle_netcdf_error( 'netcdf', 420 )
-
-             ENDIF
-
-             IF ( land_surface )  THEN
-!
-!--             Write zs grid
-                nc_stat = NF90_PUT_VAR( id_set_3d(av), id_var_zs_3d(av),  &
-                                        - zs(nzb_soil:nzt_soil), start = (/ 1 /), &
-                                        count = (/ nzt_soil-nzb_soil+1 /) )
-                CALL handle_netcdf_error( 'netcdf', 86 )
-             ENDIF
-
-          ENDIF
-
-       CASE ( '3d_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_3d(av), NF90_GLOBAL, 'VAR_LIST', &
-                                  var_list_old )
-          CALL handle_netcdf_error( 'netcdf', 87 )
-
-          var_list = ';'
-          i = 1
-          DO WHILE ( do3d(av,i)(1:1) /= ' ' )
-             var_list = TRIM(var_list) // TRIM( do3d(av,i) ) // ';'
-             i = i + 1
-          ENDDO
-
-          IF ( av == 0 )  THEN
-             var = '(3d)'
-          ELSE
-             var = '(3d_av)'
-          ENDIF
-
-          IF ( TRIM( var_list ) /= TRIM( var_list_old ) )  THEN
-             message_string = 'netCDF file for volume data ' //             &
-                              TRIM( var ) // ' from previous run found,' // &
-                              '&but this file cannot be extended due to' // &
-                              ' variable mismatch.' //                      &
-                              '&New file is created instead.'
-             CALL message( 'define_netcdf_header', 'PA0245', 0, 1, 0, 6, 0 )
-             extend = .FALSE.
-             RETURN
-          ENDIF
-
-!
-!--       Get and compare the number of vertical gridpoints
-          nc_stat = NF90_INQ_VARID( id_set_3d(av), 'zu_3d', id_var_zu_3d(av) )
-          CALL handle_netcdf_error( 'netcdf', 88 )
-
-          nc_stat = NF90_INQUIRE_VARIABLE( id_set_3d(av), id_var_zu_3d(av), &
-                                           dimids = id_dim_zu_3d_old )
-          CALL handle_netcdf_error( 'netcdf', 89 )
-          id_dim_zu_3d(av) = id_dim_zu_3d_old(1)
-
-          nc_stat = NF90_INQUIRE_DIMENSION( id_set_3d(av), id_dim_zu_3d(av), &
-                                            len = nz_old )
-          CALL handle_netcdf_error( 'netcdf', 90 )
-
-          IF ( nz_do3d-nzb+1 /= nz_old )  THEN
-              message_string = 'netCDF file for volume data ' //             &
-                               TRIM( var ) // ' from previous run found,' // &
-                               '&but this file cannot be extended due to' // &
-                               ' mismatch in number of' //                   &
-                               '&vertical grid points (nz_do3d).' //         &
-                               '&New file is created instead.'
-             CALL message( 'define_netcdf_header', 'PA0246', 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 pl3d..count+1.
-!--       The current time must be larger than the last output time
-!--       on the file.
-          nc_stat = NF90_INQ_VARID( id_set_3d(av), 'time', id_var_time_3d(av) )
-          CALL handle_netcdf_error( 'netcdf', 91 )
-
-          nc_stat = NF90_INQUIRE_VARIABLE( id_set_3d(av), id_var_time_3d(av), &
-                                           dimids = id_dim_time_old )
-          CALL handle_netcdf_error( 'netcdf', 92 )
-
-          id_dim_time_3d(av) = id_dim_time_old(1)
-
-          nc_stat = NF90_INQUIRE_DIMENSION( id_set_3d(av), id_dim_time_3d(av), &
-                                            len = ntime_count )
-          CALL handle_netcdf_error( 'netcdf', 93 )
-
-!
-!--       For non-parallel output use the last output time level of the netcdf
-!--       file because the time dimension is unlimited. In case of parallel
-!--       output the variable ntime_count could get the value of 9*10E36 because
-!--       the time dimension is limited.
-          IF ( netcdf_data_format < 5 ) do3d_time_count(av) = ntime_count
-
-          nc_stat = NF90_GET_VAR( id_set_3d(av), id_var_time_3d(av), &
-                                  last_time_coordinate,              &
-                                  start = (/ do3d_time_count(av) /), &
-                                  count = (/ 1 /) )
-          CALL handle_netcdf_error( 'netcdf', 94 )
-
-          IF ( last_time_coordinate(1) >= simulated_time )  THEN
-             message_string = 'netCDF file for volume data ' // &
-                              TRIM( var ) // ' 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', 'PA0247', 0, 1, 0, 6, 0 )
-             do3d_time_count(av) = 0
-             extend = .FALSE.
-             RETURN
-          ENDIF
-
-          IF ( netcdf_data_format > 4 )  THEN
-!
-!--          Set needed time levels (ntdim) to 
-!--          saved time levels + to be saved time levels.
-             IF ( av == 0 )  THEN 
-                ntdim_3d(0) = do3d_time_count(0) + CEILING(                &
-                              ( end_time - MAX( skip_time_do3d,            &
-                                                simulated_time_at_begin )  &
-                              ) / dt_do3d )
-             ELSE
-                ntdim_3d(1) = do3d_time_count(1) + CEILING(                &
-                              ( end_time - MAX( skip_time_data_output_av,  &
-                                                simulated_time_at_begin )  &
-                              ) / dt_data_output_av )
-             ENDIF
-
-!
-!--          Check if the needed number of output time levels is increased
-!--          compared to the number of time levels in the existing file.
-             IF ( ntdim_3d(av) > ntime_count )  THEN
-                message_string = 'netCDF file for volume data ' // &
-                                 TRIM( var ) // ' from previous run found,' // &
-                                 '&but this file cannot be extended becaus' // &
-                                 'e the number of output time levels&has b' // &
-                                 'een increased compared to the previous s' // &
-                                 'imulation.' //                               &
-                                 '&New file is created instead.'
-                CALL message( 'define_netcdf_header', 'PA0388', 0, 1, 0, 6, 0 )
-                do3d_time_count(av) = 0
-                extend = .FALSE.
-!
-!--             Recalculate the needed time levels for the new file.
-                IF ( av == 0 )  THEN
-                   ntdim_3d(0) = CEILING(                               &
-                           ( end_time - MAX( skip_time_do3d,            &
-                                             simulated_time_at_begin )  &
-                           ) / dt_do3d )
-                ELSE
-                   ntdim_3d(1) = CEILING(                               &
-                           ( end_time - MAX( skip_time_data_output_av,  &
-                                             simulated_time_at_begin )  &
-                           ) / dt_data_output_av )
-                ENDIF
-                RETURN
-             ENDIF
-          ENDIF
-
-!
-!--       Dataset seems to be extendable.
-!--       Now get the variable ids.
-          i = 1
-          DO WHILE ( do3d(av,i)(1:1) /= ' ' )
-             nc_stat = NF90_INQ_VARID( id_set_3d(av), TRIM( do3d(av,i) ), &
-                                       id_var_do3d(av,i) )
-             CALL handle_netcdf_error( 'netcdf', 95 )
-#if defined( __netcdf4_parallel )
-!
-!--          Set collective io operations for parallel io
-             IF ( netcdf_data_format > 4 )  THEN
-                nc_stat = NF90_VAR_PAR_ACCESS( id_set_3d(av),     &
-                                               id_var_do3d(av,i), &
-                                               NF90_COLLECTIVE )
-                CALL handle_netcdf_error( 'netcdf', 453 )
-             ENDIF
-#endif
-             i = i + 1
-          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. Maybe revise later.
-          IF ( av == 0 )  THEN
-             time_average_text = ' '
-          ELSE
-             WRITE (time_average_text, '('', '',F7.1,'' s average'')') &
-                                                            averaging_interval
-          ENDIF
-          nc_stat = NF90_REDEF( id_set_3d(av) )
-          CALL handle_netcdf_error( 'netcdf', 429 )
-          nc_stat = NF90_PUT_ATT( id_set_3d(av), NF90_GLOBAL, 'title', &
-                                  TRIM( run_description_header ) //    &
-                                  TRIM( time_average_text ) )
-          CALL handle_netcdf_error( 'netcdf', 96 )
-          nc_stat = NF90_ENDDEF( id_set_3d(av) )
-          CALL handle_netcdf_error( 'netcdf', 430 )
-          message_string = 'netCDF file for volume data ' //             &
-                           TRIM( var ) // ' from previous run found.' // &
-                           '&This file will be extended.'
-          CALL message( 'define_netcdf_header', 'PA0248', 0, 0, 0, 6, 0 )
-
-       CASE ( 'xy_new' )
-
-!
-!--       Define some global attributes of the dataset
-          nc_stat = NF90_PUT_ATT( id_set_xy(av), NF90_GLOBAL, 'Conventions', &
-                                  'COARDS' )
-          CALL handle_netcdf_error( 'netcdf', 97 )
-
-          IF ( av == 0 )  THEN
-             time_average_text = ' '
-          ELSE
-             WRITE (time_average_text, '('', '',F7.1,'' s average'')') &
-                                                            averaging_interval
-          ENDIF
-          nc_stat = NF90_PUT_ATT( id_set_xy(av), NF90_GLOBAL, 'title', &
-                                  TRIM( run_description_header ) //    &
-                                  TRIM( time_average_text ) )
-          CALL handle_netcdf_error( 'netcdf', 98 )
-          IF ( av == 1 )  THEN
-             WRITE ( time_average_text,'(F7.1,'' s avg'')' )  averaging_interval
-             nc_stat = NF90_PUT_ATT( id_set_xy(av), NF90_GLOBAL, 'time_avg', &
-                                     TRIM( time_average_text ) )
-             CALL handle_netcdf_error( 'netcdf', 98 )
-          ENDIF
-
-!
-!--       Define time coordinate for xy sections.
-!--       For parallel output the time dimensions has to be limited, otherwise
-!--       the performance drops significantly.
-          IF ( netcdf_data_format < 5 )  THEN
-             nc_stat = NF90_DEF_DIM( id_set_xy(av), 'time', NF90_UNLIMITED, &
-                                     id_dim_time_xy(av) )
-             CALL handle_netcdf_error( 'netcdf', 99 )
-          ELSE
-             nc_stat = NF90_DEF_DIM( id_set_xy(av), 'time', ntdim_2d_xy(av), &
-                                     id_dim_time_xy(av) )
-             CALL handle_netcdf_error( 'netcdf', 524 )
-          ENDIF
-
-          nc_stat = NF90_DEF_VAR( id_set_xy(av), 'time', NF90_DOUBLE, &
-                                  id_dim_time_xy(av), id_var_time_xy(av) )
-          CALL handle_netcdf_error( 'netcdf', 100 )
-
-          nc_stat = NF90_PUT_ATT( id_set_xy(av), id_var_time_xy(av), 'units', &
-                                  'seconds')
-          CALL handle_netcdf_error( 'netcdf', 101 )
-
-!
-!--       Define the spatial dimensions and coordinates for xy-sections.
-!--       First, determine the number of horizontal sections to be written.
-          IF ( section(1,1) == -9999 )  THEN
-             RETURN
-          ELSE
-             ns = 1
-             DO WHILE ( section(ns,1) /= -9999  .AND.  ns <= 100 )
-                ns = ns + 1
-             ENDDO
-             ns = ns - 1
-          ENDIF
-
-!
-!--       Define vertical coordinate grid (zu grid)
-          nc_stat = NF90_DEF_DIM( id_set_xy(av), 'zu_xy', ns, id_dim_zu_xy(av) )
-          CALL handle_netcdf_error( 'netcdf', 102 )
-
-          nc_stat = NF90_DEF_VAR( id_set_xy(av), 'zu_xy', NF90_DOUBLE, &
-                                  id_dim_zu_xy(av), id_var_zu_xy(av) )
-          CALL handle_netcdf_error( 'netcdf', 103 )
-
-          nc_stat = NF90_PUT_ATT( id_set_xy(av), id_var_zu_xy(av), 'units', &
-                                  'meters' )
-          CALL handle_netcdf_error( 'netcdf', 104 )
-
-!
-!--       Define vertical coordinate grid (zw grid)
-          nc_stat = NF90_DEF_DIM( id_set_xy(av), 'zw_xy', ns, id_dim_zw_xy(av) )
-          CALL handle_netcdf_error( 'netcdf', 105 )
-
-          nc_stat = NF90_DEF_VAR( id_set_xy(av), 'zw_xy', NF90_DOUBLE, &
-                                  id_dim_zw_xy(av), id_var_zw_xy(av) )
-          CALL handle_netcdf_error( 'netcdf', 106 )
-
-          nc_stat = NF90_PUT_ATT( id_set_xy(av), id_var_zw_xy(av), 'units', &
-                                  'meters' )
-          CALL handle_netcdf_error( 'netcdf', 107 )
-
-
-          IF ( land_surface )  THEN
-
-             ns_do = 0
-             DO WHILE ( section(ns_do+1,1) < nzs )
-                ns_do = ns_do + 1
-             ENDDO
-!
-!--          Define vertical coordinate grid (zs grid)
-             nc_stat = NF90_DEF_DIM( id_set_xy(av), 'zs_xy', ns_do, id_dim_zs_xy(av) )
-             CALL handle_netcdf_error( 'netcdf', 539 )
-
-             nc_stat = NF90_DEF_VAR( id_set_xy(av), 'zs_xy', NF90_DOUBLE, &
-                                     id_dim_zs_xy(av), id_var_zs_xy(av) )
-             CALL handle_netcdf_error( 'netcdf', 540 )
-
-             nc_stat = NF90_PUT_ATT( id_set_xy(av), id_var_zs_xy(av), 'units', &
-                                  'meters' )
-             CALL handle_netcdf_error( 'netcdf', 541 )
-
-          ENDIF
-
-!
-!--       Define a pseudo vertical coordinate grid for the surface variables
-!--       u* and t* to store their height level
-          nc_stat = NF90_DEF_DIM( id_set_xy(av), 'zu1_xy', 1, &
-                                  id_dim_zu1_xy(av) )
-          CALL handle_netcdf_error( 'netcdf', 108 )
-
-          nc_stat = NF90_DEF_VAR( id_set_xy(av), 'zu1_xy', NF90_DOUBLE, &
-                                  id_dim_zu1_xy(av), id_var_zu1_xy(av) )
-          CALL handle_netcdf_error( 'netcdf', 109 )
-
-          nc_stat = NF90_PUT_ATT( id_set_xy(av), id_var_zu1_xy(av), 'units', &
-                                  'meters' )
-          CALL handle_netcdf_error( 'netcdf', 110 )
-
-!
-!--       Define a variable to store the layer indices of the horizontal cross
-!--       sections, too
-          nc_stat = NF90_DEF_VAR( id_set_xy(av), 'ind_z_xy', NF90_DOUBLE, &
-                                  id_dim_zu_xy(av), id_var_ind_z_xy(av) )
-          CALL handle_netcdf_error( 'netcdf', 111 )
-
-          nc_stat = NF90_PUT_ATT( id_set_xy(av), id_var_ind_z_xy(av), 'units', &
-                                  'gridpoints')
-          CALL handle_netcdf_error( 'netcdf', 112 )
-
-!
-!--       Define x-axis (for scalar position)
-          nc_stat = NF90_DEF_DIM( id_set_xy(av), 'x', nx+2, id_dim_x_xy(av) )
-          CALL handle_netcdf_error( 'netcdf', 113 )
-
-          nc_stat = NF90_DEF_VAR( id_set_xy(av), 'x', NF90_DOUBLE, &
-                                  id_dim_x_xy(av), id_var_x_xy(av) )
-          CALL handle_netcdf_error( 'netcdf', 114 )
-
-
-          nc_stat = NF90_PUT_ATT( id_set_xy(av), id_var_x_xy(av), 'units', &
-                                  'meters' )
-          CALL handle_netcdf_error( 'netcdf', 115 )
-
-!
-!--       Define x-axis (for u position)
-          nc_stat = NF90_DEF_DIM( id_set_xy(av), 'xu', nx+2, id_dim_xu_xy(av) )
-          CALL handle_netcdf_error( 'netcdf', 388 )
-
-          nc_stat = NF90_DEF_VAR( id_set_xy(av), 'xu', NF90_DOUBLE, &
-                                  id_dim_xu_xy(av), id_var_xu_xy(av) )
-          CALL handle_netcdf_error( 'netcdf', 389 )
-
-          nc_stat = NF90_PUT_ATT( id_set_xy(av), id_var_xu_xy(av), 'units', &
-                                  'meters' )
-          CALL handle_netcdf_error( 'netcdf', 390 )
-
-!
-!--       Define y-axis (for scalar position)
-          nc_stat = NF90_DEF_DIM( id_set_xy(av), 'y', ny+2, id_dim_y_xy(av) )
-          CALL handle_netcdf_error( 'netcdf', 116 )
-
-          nc_stat = NF90_DEF_VAR( id_set_xy(av), 'y', NF90_DOUBLE, &
-                                  id_dim_y_xy(av), id_var_y_xy(av) )
-          CALL handle_netcdf_error( 'netcdf', 117 )
-
-          nc_stat = NF90_PUT_ATT( id_set_xy(av), id_var_y_xy(av), 'units', &
-                                  'meters' )
-          CALL handle_netcdf_error( 'netcdf', 118 )
-
-!
-!--       Define y-axis (for scalar position)
-          nc_stat = NF90_DEF_DIM( id_set_xy(av), 'yv', ny+2, id_dim_yv_xy(av) )
-          CALL handle_netcdf_error( 'netcdf', 364 )
-
-          nc_stat = NF90_DEF_VAR( id_set_xy(av), 'yv', NF90_DOUBLE, &
-                                  id_dim_yv_xy(av), id_var_yv_xy(av) )
-          CALL handle_netcdf_error( 'netcdf', 365 )
-
-          nc_stat = NF90_PUT_ATT( id_set_xy(av), id_var_yv_xy(av), 'units', &
-                                  'meters' )
-          CALL handle_netcdf_error( 'netcdf', 366 )
-
-!
-!--       In case of non-flat topography define 2d-arrays containing the height
-!--       information
-          IF ( TRIM( topography ) /= 'flat' )  THEN
-!
-!--          Define zusi = zu(nzb_s_inner)
-             nc_stat = NF90_DEF_VAR( id_set_xy(av), 'zusi', NF90_DOUBLE, &
-                                     (/ id_dim_x_xy(av), id_dim_y_xy(av) /), &
-                                     id_var_zusi_xy(av) )
-             CALL handle_netcdf_error( 'netcdf', 421 )
-             
-             nc_stat = NF90_PUT_ATT( id_set_xy(av), id_var_zusi_xy(av), &
-                                     'units', 'meters' )
-             CALL handle_netcdf_error( 'netcdf', 422 )
-             
-             nc_stat = NF90_PUT_ATT( id_set_xy(av), id_var_zusi_xy(av), &
-                                     'long_name', 'zu(nzb_s_inner)' )
-             CALL handle_netcdf_error( 'netcdf', 423 )
-
-!             
-!--          Define zwwi = zw(nzb_w_inner)
-             nc_stat = NF90_DEF_VAR( id_set_xy(av), 'zwwi', NF90_DOUBLE, &
-                                     (/ id_dim_x_xy(av), id_dim_y_xy(av) /), &
-                                     id_var_zwwi_xy(av) )
-             CALL handle_netcdf_error( 'netcdf', 424 )
-             
-             nc_stat = NF90_PUT_ATT( id_set_xy(av), id_var_zwwi_xy(av), &
-                                     'units', 'meters' )
-             CALL handle_netcdf_error( 'netcdf', 425 )
-             
-             nc_stat = NF90_PUT_ATT( id_set_xy(av), id_var_zwwi_xy(av), &
-                                     'long_name', 'zw(nzb_w_inner)' )
-             CALL handle_netcdf_error( 'netcdf', 426 )
-
-          ENDIF
-
-!
-!--       Define the variables
-          var_list = ';'
-          i = 1
-
-          DO WHILE ( do2d(av,i)(1:1) /= ' ' )
-
-             IF ( INDEX( do2d(av,i), 'xy' ) /= 0 )  THEN
-!
-!--             If there is a star in the variable name (u* or t*), it is a
-!--             surface variable. Define it with id_dim_zu1_xy.
-                IF ( INDEX( do2d(av,i), '*' ) /= 0 )  THEN
-
-                   nc_stat = NF90_DEF_VAR( id_set_xy(av),  do2d(av,i),         &
-                                           nc_precision(1),                    &
-                                           (/ id_dim_x_xy(av), id_dim_y_xy(av),&
-                                              id_dim_zu1_xy(av),               &
-                                              id_dim_time_xy(av) /),           &
-                                           id_var_do2d(av,i) )
-
-                   var_list = TRIM(var_list) // TRIM( do2d(av,i) ) // ';'
-
-                ELSE
-
-!
-!--                Check for the grid
-                   found = .TRUE.
-                   SELECT CASE ( do2d(av,i) )
-!
-!--                   Most variables are defined on the zu grid
-                      CASE ( 'e_xy', 'lpt_xy', 'nr_xy', 'p_xy', 'pc_xy',       &
-                             'pr_xy', 'prr_xy', 'pt_xy', 'q_xy', 'qc_xy',      &
-                             'ql_xy', 'ql_c_xy', 'ql_v_xy', 'ql_vp_xy',        &
-                             'qr_xy', 'qv_xy', 'rad_lw_cs_hr_xy',              &
-                             'rad_lw_hr_xy', 'rad_sw_cs_hr_xy', 'rad_sw_hr_xy',& 
-                             'rho_xy', 's_xy', 'sa_xy', 'vpt_xy' )
-
-                         grid_x = 'x'
-                         grid_y = 'y'
-                         grid_z = 'zu'
-!
-!--                   u grid
-                      CASE ( 'u_xy' )
-
-                         grid_x = 'xu'
-                         grid_y = 'y'
-                         grid_z = 'zu'
-!
-!--                   v grid
-                      CASE ( 'v_xy' )
-
-                         grid_x = 'x'
-                         grid_y = 'yv'
-                         grid_z = 'zu'
-!
-!--                   w grid
-                      CASE ( 'rad_lw_in_xy', 'rad_lw_out_xy', 'rad_sw_in_xy',  &
-                             'rad_sw_out_xy' , 'w_xy' )
-
-                         grid_x = 'x'
-                         grid_y = 'y'
-                         grid_z = 'zw'
-!
-!--                   soil grid
-                      CASE ( 'm_soil_xy', 't_soil_xy' )
-                         grid_x = 'x'
-                         grid_y = 'y'
-                         grid_z = 'zs'
-
-                      CASE DEFAULT
-!
-!--                      Check for user-defined quantities
-                         CALL user_define_netcdf_grid( do2d(av,i), found, &
-                                                       grid_x, grid_y, grid_z )
-
-                   END SELECT
-
-!
-!--                Select the respective dimension ids
-                   IF ( grid_x == 'x' )  THEN
-                      id_x = id_dim_x_xy(av)
-                   ELSEIF ( grid_x == 'xu' )  THEN
-                      id_x = id_dim_xu_xy(av)
-                   ENDIF
-
-                   IF ( grid_y == 'y' )  THEN
-                      id_y = id_dim_y_xy(av)
-                   ELSEIF ( grid_y == 'yv' )  THEN
-                      id_y = id_dim_yv_xy(av)
-                   ENDIF
-
-                   IF ( grid_z == 'zu' )  THEN
-                      id_z = id_dim_zu_xy(av)
-                   ELSEIF ( grid_z == 'zw' )  THEN
-                      id_z = id_dim_zw_xy(av)
-                   ELSEIF ( grid_z == 'zs' )  THEN
-                      id_z = id_dim_zs_xy(av)
-                   ENDIF
-
-!
-!--                Define the grid
-                   nc_stat = NF90_DEF_VAR( id_set_xy(av), do2d(av,i),          &
-                                           nc_precision(1),                    &
-                                   (/ id_x, id_y, id_z, id_dim_time_xy(av) /), &
-                                           id_var_do2d(av,i) )
-
-                   IF ( .NOT. found )  THEN
-                      WRITE ( message_string, * ) 'no grid defined for', &
-                                            ' variable ', TRIM( do2d(av,i) )
-                      CALL message( 'define_netcdf_header', 'PA0244',&
-                                                                0, 1, 0, 6, 0 )
-                   ENDIF
-
-                   var_list = TRIM( var_list ) // TRIM( do2d(av,i) ) // ';'
-
-                ENDIF
-
-                CALL handle_netcdf_error( 'netcdf', 119 )
-!
-!--             Store the 'real' name of the variable (with *, for example)
-!--             in the long_name attribute. This is evaluated by Ferret,
-!--             for example.
-                nc_stat = NF90_PUT_ATT( id_set_xy(av), id_var_do2d(av,i), &
-                                        'long_name', do2d(av,i) )
-                CALL handle_netcdf_error( 'netcdf', 120 )
-!
-!--             Define the variable's unit
-                nc_stat = NF90_PUT_ATT( id_set_xy(av), id_var_do2d(av,i), &
-                                        'units', TRIM( do2d_unit(av,i) ) )
-                CALL handle_netcdf_error( 'netcdf', 354 ) 
-#if defined( __netcdf4_parallel )
-                IF ( netcdf_data_format > 4 )  THEN
-!
-!--                Set no fill for every variable to increase performance.
-                   nc_stat = NF90_DEF_VAR_FILL( id_set_xy(av),     &
-                                                id_var_do2d(av,i), &
-                                                1, 0 )
-                   CALL handle_netcdf_error( 'netcdf', 533 )
-!
-!--                Set collective io operations for parallel io
-                   nc_stat = NF90_VAR_PAR_ACCESS( id_set_xy(av),     &
-                                                  id_var_do2d(av,i), &
-                                                  NF90_COLLECTIVE )
-                   CALL handle_netcdf_error( 'netcdf', 448 )
-                ENDIF
-#endif
-             ENDIF
-
-             i = i + 1
-
-          ENDDO
-
-!
-!--       No arrays to output. Close the netcdf file and return.
-          IF ( i == 1 )  RETURN
-
-!
-!--       Write the list of variables as global attribute (this is used by
-!--       restart runs and by combine_plot_fields)
-          nc_stat = NF90_PUT_ATT( id_set_xy(av), NF90_GLOBAL, 'VAR_LIST', &
-                                  var_list )
-          CALL handle_netcdf_error( 'netcdf', 121 ) 
-
-!
-!--       Set general no fill, otherwise the performance drops significantly for
-!--       parallel output.
-          nc_stat = NF90_SET_FILL( id_set_xy(av), NF90_NOFILL, oldmode )
-          CALL handle_netcdf_error( 'netcdf', 529 )
-
-!
-!--       Leave netCDF define mode
-          nc_stat = NF90_ENDDEF( id_set_xy(av) )
-          CALL handle_netcdf_error( 'netcdf', 122 ) 
-
-!
-!--       These data are only written by PE0 for parallel output to increase
-!--       the performance.
-          IF ( myid == 0  .OR.  netcdf_data_format < 5 )  THEN
-
-!
-!--          Write axis data: z_xy, x, y
-             ALLOCATE( netcdf_data(1:ns) )
-
-!
-!--          Write zu data
-             DO  i = 1, ns
-                IF( section(i,1) == -1 )  THEN
-                   netcdf_data(i) = -1.0_wp  ! section averaged along z
-                ELSE
-                   netcdf_data(i) = zu( section(i,1) )
-                ENDIF
-             ENDDO
-             nc_stat = NF90_PUT_VAR( id_set_xy(av), id_var_zu_xy(av), &
-                                     netcdf_data, start = (/ 1 /),    &
-                                     count = (/ ns /) )
-             CALL handle_netcdf_error( 'netcdf', 123 )
-
-!
-!--          Write zw data
-             DO  i = 1, ns
-                IF( section(i,1) == -1 )  THEN
-                   netcdf_data(i) = -1.0_wp  ! section averaged along z
-                ELSE
-                   netcdf_data(i) = zw( section(i,1) )
-                ENDIF
-             ENDDO
-             nc_stat = NF90_PUT_VAR( id_set_xy(av), id_var_zw_xy(av), &
-                                     netcdf_data, start = (/ 1 /),    &
-                                     count = (/ ns /) )
-             CALL handle_netcdf_error( 'netcdf', 124 )
-
-!
-!--             Write zs data
-             IF ( land_surface )  THEN
-                ns_do = 0
-                DO  i = 1, ns
-                   IF( section(i,1) == -1 )  THEN
-                      netcdf_data(i) = 1.0_wp  ! section averaged along z
-                      ns_do = ns_do + 1
-                   ELSEIF ( section(i,1) < nzs )  THEN
-                      netcdf_data(i) = - zs( section(i,1) )
-                      ns_do = ns_do + 1
-                   ENDIF
-                ENDDO
-
-                nc_stat = NF90_PUT_VAR( id_set_xy(av), id_var_zs_xy(av), &
-                                        netcdf_data(1:ns_do), start = (/ 1 /),    &
-                                        count = (/ ns_do /) )
-                CALL handle_netcdf_error( 'netcdf', 124 )
-
-             ENDIF
-
-!
-!--          Write gridpoint number data
-             netcdf_data(1:ns) = section(1:ns,1)
-             nc_stat = NF90_PUT_VAR( id_set_xy(av), id_var_ind_z_xy(av), &
-                                     netcdf_data, start = (/ 1 /),       &
-                                     count = (/ ns /) )
-             CALL handle_netcdf_error( 'netcdf', 125 )
-
-             DEALLOCATE( netcdf_data )
-
-!
-!--          Write the cross section height u*, t*
-             nc_stat = NF90_PUT_VAR( id_set_xy(av), id_var_zu1_xy(av), &
-                                     (/ zu(nzb+1) /), start = (/ 1 /), &
-                                     count = (/ 1 /) )
-             CALL handle_netcdf_error( 'netcdf', 126 )
-
-!
-!--          Write data for x (shifted by +dx/2) and xu axis
-             ALLOCATE( netcdf_data(0:nx+1) )
-
-             DO  i = 0, nx+1
-                netcdf_data(i) = ( i + 0.5_wp ) * dx
-             ENDDO
-
-             nc_stat = NF90_PUT_VAR( id_set_xy(av), id_var_x_xy(av), &
-                                     netcdf_data, start = (/ 1 /),   &
-                                     count = (/ nx+2 /) )
-             CALL handle_netcdf_error( 'netcdf', 127 )
-
-             DO  i = 0, nx+1
-                netcdf_data(i) = i * dx
-             ENDDO
-
-             nc_stat = NF90_PUT_VAR( id_set_xy(av), id_var_xu_xy(av), &
-                                     netcdf_data, start = (/ 1 /),    &
-                                     count = (/ nx+2 /) )
-             CALL handle_netcdf_error( 'netcdf', 367 )
-
-             DEALLOCATE( netcdf_data )
-
-!
-!--          Write data for y (shifted by +dy/2) and yv axis
-             ALLOCATE( netcdf_data(0:ny+1) )
-
-             DO  i = 0, ny+1
-                netcdf_data(i) = ( i + 0.5_wp ) * dy
-             ENDDO
-
-             nc_stat = NF90_PUT_VAR( id_set_xy(av), id_var_y_xy(av), &
-                                     netcdf_data, start = (/ 1 /),   &
-                                     count = (/ ny+2 /))
-             CALL handle_netcdf_error( 'netcdf', 128 )
-
-             DO  i = 0, ny+1
-                netcdf_data(i) = i * dy
-             ENDDO
-
-             nc_stat = NF90_PUT_VAR( id_set_xy(av), id_var_yv_xy(av), &
-                                     netcdf_data, start = (/ 1 /),    &
-                                     count = (/ ny+2 /))
-             CALL handle_netcdf_error( 'netcdf', 368 )
-
-             DEALLOCATE( netcdf_data )
-
-!
-!--          In case of non-flat topography write height information
-             IF ( TRIM( topography ) /= 'flat' )  THEN
-
-                nc_stat = NF90_PUT_VAR( id_set_xy(av), id_var_zusi_xy(av), &
-                                        zu_s_inner(0:nx+1,0:ny+1), &
-                                        start = (/ 1, 1 /), &
-                                        count = (/ nx+2, ny+2 /) )
-                CALL handle_netcdf_error( 'netcdf', 427 )
-
-                nc_stat = NF90_PUT_VAR( id_set_xy(av), id_var_zwwi_xy(av), &
-                                        zw_w_inner(0:nx+1,0:ny+1), &
-                                        start = (/ 1, 1 /), &
-                                        count = (/ nx+2, ny+2 /) )
-                CALL handle_netcdf_error( 'netcdf', 428 )
-
-             ENDIF
-
-
-
-          ENDIF
-
-       CASE ( 'xy_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_xy(av), NF90_GLOBAL, 'VAR_LIST', &
-                                  var_list_old )
-          CALL handle_netcdf_error( 'netcdf', 129 )
-
-          var_list = ';'
-          i = 1
-          DO WHILE ( do2d(av,i)(1:1) /= ' ' )
-             IF ( INDEX( do2d(av,i), 'xy' ) /= 0 )  THEN
-                var_list = TRIM( var_list ) // TRIM( do2d(av,i) ) // ';'
-             ENDIF
-             i = i + 1
-          ENDDO
-
-          IF ( av == 0 )  THEN
-             var = '(xy)'
-          ELSE
-             var = '(xy_av)'
-          ENDIF
-
-          IF ( TRIM( var_list ) /= TRIM( var_list_old ) )  THEN
-             message_string = 'netCDF file for cross-sections ' //           &
-                              TRIM( var ) // ' from previous run found,' //  &
-                              '& but this file cannot be extended due to' // &
-                              ' variable mismatch.' //                       &
-                              '&New file is created instead.'
-             CALL message( 'define_netcdf_header', 'PA0249', 0, 1, 0, 6, 0 )
-             extend = .FALSE.
-             RETURN
-          ENDIF
-
-!
-!--       Calculate the number of current sections
-          ns = 1
-          DO WHILE ( section(ns,1) /= -9999  .AND.  ns <= 100 )
-             ns = ns + 1
-          ENDDO
-          ns = ns - 1
-
-!
-!--       Get and compare the number of horizontal cross sections
-          nc_stat = NF90_INQ_VARID( id_set_xy(av), 'zu_xy', id_var_zu_xy(av) )
-          CALL handle_netcdf_error( 'netcdf', 130 )
-
-          nc_stat = NF90_INQUIRE_VARIABLE( id_set_xy(av), id_var_zu_xy(av), &
-                                           dimids = id_dim_zu_xy_old )
-          CALL handle_netcdf_error( 'netcdf', 131 )
-          id_dim_zu_xy(av) = id_dim_zu_xy_old(1)
-
-          nc_stat = NF90_INQUIRE_DIMENSION( id_set_xy(av), id_dim_zu_xy(av), &
-                                            len = ns_old )
-          CALL handle_netcdf_error( 'netcdf', 132 )
-
-          IF ( ns /= ns_old )  THEN
-             message_string = 'netCDF file for cross-sections ' //          &
-                              TRIM( var ) // ' from previous run found,' // &
-                              '&but this file cannot be extended due to' // &
-                              ' mismatch in number of' //                   &
-                              '&cross sections.' //                         &
-                              '&New file is created instead.'
-             CALL message( 'define_netcdf_header', 'PA0250', 0, 1, 0, 6, 0 )
-             extend = .FALSE.
-             RETURN
-          ENDIF
-
-!
-!--       Get and compare the heights of the cross sections
-          ALLOCATE( netcdf_data(1:ns_old) )
-
-          nc_stat = NF90_GET_VAR( id_set_xy(av), id_var_zu_xy(av), netcdf_data )
-          CALL handle_netcdf_error( 'netcdf', 133 )
-
-          DO  i = 1, ns
-             IF ( section(i,1) /= -1 )  THEN
-                IF ( zu(section(i,1)) /= netcdf_data(i) )  THEN
-                   message_string = 'netCDF file for cross-sections ' //     &
-                               TRIM( var ) // ' from previous run found,' // &
-                               '&but this file cannot be extended' //        &
-                               ' due to mismatch in cross' //                &
-                               '&section levels.' //                         &
-                               '&New file is created instead.'
-                   CALL message( 'define_netcdf_header', 'PA0251', &
-                                                                 0, 1, 0, 6, 0 )
-                   extend = .FALSE.
-                   RETURN
-                ENDIF
-             ELSE
-                IF ( -1.0_wp /= netcdf_data(i) )  THEN
-                   message_string = 'netCDF file for cross-sections ' //     &
-                               TRIM( var ) // ' from previous run found,' // &
-                               '&but this file cannot be extended' //        &
-                               ' due to mismatch in cross' //                &
-                               '&section levels.' //                         &
-                               '&New file is created instead.'
-                   CALL message( 'define_netcdf_header', 'PA0251',&
-                                                                 0, 1, 0, 6, 0 )
-                   extend = .FALSE.
-                   RETURN
-                ENDIF
-             ENDIF
-          ENDDO
-
-          DEALLOCATE( netcdf_data )
-
-!
-!--       Get the id of the time coordinate (unlimited coordinate) and its
-!--       last index on the file. The next time level is do2d..count+1.
-!--       The current time must be larger than the last output time
-!--       on the file.
-          nc_stat = NF90_INQ_VARID( id_set_xy(av), 'time', id_var_time_xy(av) )
-          CALL handle_netcdf_error( 'netcdf', 134 )
-
-          nc_stat = NF90_INQUIRE_VARIABLE( id_set_xy(av), id_var_time_xy(av), &
-                                           dimids = id_dim_time_old )
-          CALL handle_netcdf_error( 'netcdf', 135 )
-          id_dim_time_xy(av) = id_dim_time_old(1)
-
-          nc_stat = NF90_INQUIRE_DIMENSION( id_set_xy(av), id_dim_time_xy(av), &
-                                            len = ntime_count )
-          CALL handle_netcdf_error( 'netcdf', 136 )
-
-!
-!--       For non-parallel output use the last output time level of the netcdf
-!--       file because the time dimension is unlimited. In case of parallel
-!--       output the variable ntime_count could get the value of 9*10E36 because
-!--       the time dimension is limited.
-          IF ( netcdf_data_format < 5 ) do2d_xy_time_count(av) = ntime_count
-
-          nc_stat = NF90_GET_VAR( id_set_xy(av), id_var_time_xy(av),    &
-                                  last_time_coordinate,                 &
-                                  start = (/ do2d_xy_time_count(av) /), &
-                                  count = (/ 1 /) )
-          CALL handle_netcdf_error( 'netcdf', 137 )
-
-          IF ( last_time_coordinate(1) >= simulated_time )  THEN
-             message_string = 'netCDF file for cross sections ' //          &
-                              TRIM( var ) // ' 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', 'PA0252', 0, 1, 0, 6, 0 )
-             do2d_xy_time_count(av) = 0
-             extend = .FALSE.
-             RETURN
-          ENDIF
-
-          IF ( netcdf_data_format > 4 )  THEN
-!
-!--          Set needed time levels (ntdim) to 
-!--          saved time levels + to be saved time levels.
-             IF ( av == 0 )  THEN 
-                ntdim_2d_xy(0) = do2d_xy_time_count(0) + CEILING(             &
-                                 ( end_time - MAX( skip_time_do2d_xy,         &
-                                                   simulated_time_at_begin )  &
-                                 ) / dt_do2d_xy )
-             ELSE
-                ntdim_2d_xy(1) = do2d_xy_time_count(1) + CEILING(             &
-                                 ( end_time - MAX( skip_time_data_output_av,  &
-                                                   simulated_time_at_begin )  &
-                                 ) / dt_data_output_av )
-             ENDIF
-
-!
-!--          Check if the needed number of output time levels is increased
-!--          compared to the number of time levels in the existing file.
-             IF ( ntdim_2d_xy(av) > ntime_count )  THEN
-                message_string = 'netCDF file for cross sections ' //          &
-                                 TRIM( var ) // ' from previous run found,' // &
-                                 '&but this file cannot be extended becaus' // &
-                                 'e the number of output time levels&has b' // &
-                                 'een increased compared to the previous s' // &
-                                 'imulation.' //                               &
-                                 '&New file is created instead.'
-                CALL message( 'define_netcdf_header', 'PA0389', 0, 1, 0, 6, 0 )
-                do2d_xy_time_count(av) = 0
-                extend = .FALSE.
-!
-!--             Recalculate the needed time levels for the new file.
-                IF ( av == 0 )  THEN
-                   ntdim_2d_xy(0) = CEILING(                            &
-                           ( end_time - MAX( skip_time_do2d_xy,         &
-                                             simulated_time_at_begin )  &
-                           ) / dt_do2d_xy )
-                ELSE
-                   ntdim_2d_xy(1) = CEILING(                            &
-                           ( end_time - MAX( skip_time_data_output_av,  &
-                                             simulated_time_at_begin )  &
-                           ) / dt_data_output_av )
-                ENDIF
-                RETURN
-             ENDIF
-          ENDIF
-
-!
-!--       Dataset seems to be extendable.
-!--       Now get the variable ids.
-          i = 1
-          DO WHILE ( do2d(av,i)(1:1) /= ' ' )
-             IF ( INDEX( do2d(av,i), 'xy' ) /= 0 )  THEN
-                nc_stat = NF90_INQ_VARID( id_set_xy(av), do2d(av,i), &
-                                          id_var_do2d(av,i) )
-                CALL handle_netcdf_error( 'netcdf', 138 )
-#if defined( __netcdf4_parallel )
-!
-!--             Set collective io operations for parallel io
-                IF ( netcdf_data_format > 4 )  THEN
-                   nc_stat = NF90_VAR_PAR_ACCESS( id_set_xy(av),     &
-                                                  id_var_do2d(av,i), &
-                                                  NF90_COLLECTIVE )
-                   CALL handle_netcdf_error( 'netcdf', 454 )
-                ENDIF
-#endif
-             ENDIF
-             i = i + 1
-          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.
-          IF ( av == 0 )  THEN
-             time_average_text = ' '
-          ELSE
-             WRITE (time_average_text, '('', '',F7.1,'' s average'')') &
-                                                            averaging_interval
-          ENDIF
-          nc_stat = NF90_REDEF( id_set_xy(av) )
-          CALL handle_netcdf_error( 'netcdf', 431 )
-          nc_stat = NF90_PUT_ATT( id_set_xy(av), NF90_GLOBAL, 'title', &
-                                  TRIM( run_description_header ) //    &
-                                  TRIM( time_average_text ) )
-          CALL handle_netcdf_error( 'netcdf', 139 )
-          nc_stat = NF90_ENDDEF( id_set_xy(av) )
-          CALL handle_netcdf_error( 'netcdf', 432 )
-          message_string = 'netCDF file for cross-sections ' //           &
-                            TRIM( var ) // ' from previous run found.' // &
-                           '&This file will be extended.'
-          CALL message( 'define_netcdf_header', 'PA0253', 0, 0, 0, 6, 0 )
-          
-
-       CASE ( 'xz_new' )
-
-!
-!--       Define some global attributes of the dataset
-          nc_stat = NF90_PUT_ATT( id_set_xz(av), NF90_GLOBAL, 'Conventions', &
-                                  'COARDS' )
-          CALL handle_netcdf_error( 'netcdf', 140 )
-
-          IF ( av == 0 )  THEN
-             time_average_text = ' '
-          ELSE
-             WRITE (time_average_text, '('', '',F7.1,'' s average'')') &
-                                                            averaging_interval
-          ENDIF
-          nc_stat = NF90_PUT_ATT( id_set_xz(av), NF90_GLOBAL, 'title', &
-                                  TRIM( run_description_header )  //   &
-                                  TRIM( time_average_text ) )
-          CALL handle_netcdf_error( 'netcdf', 141 )
-          IF ( av == 1 )  THEN
-             WRITE ( time_average_text,'(F7.1,'' s avg'')' )  averaging_interval
-             nc_stat = NF90_PUT_ATT( id_set_xz(av), NF90_GLOBAL, 'time_avg', &
-                                     TRIM( time_average_text ) )
-             CALL handle_netcdf_error( 'netcdf', 141 )
-          ENDIF
-
-!
-!--       Define time coordinate for xz sections.
-!--       For parallel output the time dimensions has to be limited, otherwise
-!--       the performance drops significantly.
-          IF ( netcdf_data_format < 5 )  THEN
-             nc_stat = NF90_DEF_DIM( id_set_xz(av), 'time', NF90_UNLIMITED, &
-                                     id_dim_time_xz(av) )
-             CALL handle_netcdf_error( 'netcdf', 142 )
-          ELSE
-             nc_stat = NF90_DEF_DIM( id_set_xz(av), 'time', ntdim_2d_xz(av), &
-                                     id_dim_time_xz(av) )
-             CALL handle_netcdf_error( 'netcdf', 525 )
-          ENDIF
-
-          nc_stat = NF90_DEF_VAR( id_set_xz(av), 'time', NF90_DOUBLE, &
-                                  id_dim_time_xz(av), id_var_time_xz(av) )
-          CALL handle_netcdf_error( 'netcdf', 143 )
-
-          nc_stat = NF90_PUT_ATT( id_set_xz(av), id_var_time_xz(av), 'units', &
-                                  'seconds')
-          CALL handle_netcdf_error( 'netcdf', 144 )
-
-!
-!--       Define the spatial dimensions and coordinates for xz-sections.
-!--       First, determine the number of vertical sections to be written.
-          IF ( section(1,2) == -9999 )  THEN
-             RETURN
-          ELSE
-             ns = 1
-             DO WHILE ( section(ns,2) /= -9999  .AND.  ns <= 100 )
-                ns = ns + 1
-             ENDDO
-             ns = ns - 1
-          ENDIF
-
-!
-!--       Define y-axis (for scalar position)
-          nc_stat = NF90_DEF_DIM( id_set_xz(av), 'y_xz', ns, id_dim_y_xz(av) )
-          CALL handle_netcdf_error( 'netcdf', 145 )
-
-          nc_stat = NF90_DEF_VAR( id_set_xz(av), 'y_xz', NF90_DOUBLE, &
-                                  id_dim_y_xz(av), id_var_y_xz(av) )
-          CALL handle_netcdf_error( 'netcdf', 146 )
-
-          nc_stat = NF90_PUT_ATT( id_set_xz(av), id_var_y_xz(av), 'units', &
-                                  'meters' )
-          CALL handle_netcdf_error( 'netcdf', 147 )
-
-!
-!--       Define y-axis (for v position)
-          nc_stat = NF90_DEF_DIM( id_set_xz(av), 'yv_xz', ns, id_dim_yv_xz(av) )
-          CALL handle_netcdf_error( 'netcdf', 369 )
-
-          nc_stat = NF90_DEF_VAR( id_set_xz(av), 'yv_xz', NF90_DOUBLE, &
-                                  id_dim_yv_xz(av), id_var_yv_xz(av) )
-          CALL handle_netcdf_error( 'netcdf', 370 )
-
-          nc_stat = NF90_PUT_ATT( id_set_xz(av), id_var_yv_xz(av), 'units', &
-                                  'meters' )
-          CALL handle_netcdf_error( 'netcdf', 371 )
-
-!
-!--       Define a variable to store the layer indices of the vertical cross
-!--       sections
-          nc_stat = NF90_DEF_VAR( id_set_xz(av), 'ind_y_xz', NF90_DOUBLE, &
-                                  id_dim_y_xz(av), id_var_ind_y_xz(av) )
-          CALL handle_netcdf_error( 'netcdf', 148 )
-
-          nc_stat = NF90_PUT_ATT( id_set_xz(av), id_var_ind_y_xz(av), 'units', &
-                                  'gridpoints')
-          CALL handle_netcdf_error( 'netcdf', 149 )
-
-!
-!--       Define x-axis (for scalar position)
-          nc_stat = NF90_DEF_DIM( id_set_xz(av), 'x', nx+2, id_dim_x_xz(av) )
-          CALL handle_netcdf_error( 'netcdf', 150 )
-
-          nc_stat = NF90_DEF_VAR( id_set_xz(av), 'x', NF90_DOUBLE, &
-                                  id_dim_x_xz(av), id_var_x_xz(av) )
-          CALL handle_netcdf_error( 'netcdf', 151 )
-
-          nc_stat = NF90_PUT_ATT( id_set_xz(av), id_var_x_xz(av), 'units', &
-                                  'meters' )
-          CALL handle_netcdf_error( 'netcdf', 152 )
-
-!
-!--       Define x-axis (for u position)
-          nc_stat = NF90_DEF_DIM( id_set_xz(av), 'xu', nx+2, id_dim_xu_xz(av) )
-          CALL handle_netcdf_error( 'netcdf', 372 )
-
-          nc_stat = NF90_DEF_VAR( id_set_xz(av), 'xu', NF90_DOUBLE, &
-                                  id_dim_xu_xz(av), id_var_xu_xz(av) )
-          CALL handle_netcdf_error( 'netcdf', 373 )
-
-          nc_stat = NF90_PUT_ATT( id_set_xz(av), id_var_xu_xz(av), 'units', &
-                                  'meters' )
-          CALL handle_netcdf_error( 'netcdf', 374 )
-
-!
-!--       Define the three z-axes (zu, zw, and zs)
-          nc_stat = NF90_DEF_DIM( id_set_xz(av), 'zu', nz+2, id_dim_zu_xz(av) )
-          CALL handle_netcdf_error( 'netcdf', 153 )
-
-          nc_stat = NF90_DEF_VAR( id_set_xz(av), 'zu', NF90_DOUBLE, &
-                                  id_dim_zu_xz(av), id_var_zu_xz(av) )
-          CALL handle_netcdf_error( 'netcdf', 154 )
-
-          nc_stat = NF90_PUT_ATT( id_set_xz(av), id_var_zu_xz(av), 'units', &
-                                  'meters' )
-          CALL handle_netcdf_error( 'netcdf', 155 )
-
-          nc_stat = NF90_DEF_DIM( id_set_xz(av), 'zw', nz+2, id_dim_zw_xz(av) )
-          CALL handle_netcdf_error( 'netcdf', 156 )
-
-          nc_stat = NF90_DEF_VAR( id_set_xz(av), 'zw', NF90_DOUBLE, &
-                                  id_dim_zw_xz(av), id_var_zw_xz(av) )
-          CALL handle_netcdf_error( 'netcdf', 157 )
-
-          nc_stat = NF90_PUT_ATT( id_set_xz(av), id_var_zw_xz(av), 'units', &
-                                  'meters' )
-          CALL handle_netcdf_error( 'netcdf', 158 )
-
-          IF ( land_surface )  THEN
-
-             nc_stat = NF90_DEF_DIM( id_set_xz(av), 'zs', nzs, id_dim_zs_xz(av) )
-             CALL handle_netcdf_error( 'netcdf', 542 )
-
-             nc_stat = NF90_DEF_VAR( id_set_xz(av), 'zs', NF90_DOUBLE, &
-                                     id_dim_zs_xz(av), id_var_zs_xz(av) )
-             CALL handle_netcdf_error( 'netcdf', 543 )
-
-             nc_stat = NF90_PUT_ATT( id_set_xz(av), id_var_zs_xz(av), 'units', &
-                                     'meters' )
-             CALL handle_netcdf_error( 'netcdf', 544 )
-
-          ENDIF
-
-!
-!--       Define the variables
-          var_list = ';'
-          i = 1
-
-          DO WHILE ( do2d(av,i)(1:1) /= ' ' )
-
-             IF ( INDEX( do2d(av,i), 'xz' ) /= 0 )  THEN
-
-!
-!--             Check for the grid
-                found = .TRUE.
-                SELECT CASE ( do2d(av,i) )
-!
-!--                Most variables are defined on the zu grid
-                   CASE ( 'e_xz', 'lpt_xz', 'nr_xz', 'p_xz', 'pc_xz', 'pr_xz', &
-                          'prr_xz', 'pt_xz', 'q_xz', 'qc_xz', 'ql_xz',         &
-                          'ql_c_xz', 'ql_v_xz', 'ql_vp_xz', 'qr_xz', 'qv_xz',  &
-                          'rad_lw_cs_hr_xz', 'rad_lw_hr_xz',                   &
-                          'rad_sw_cs_hr_xz', 'rad_sw_hr_xz''rho_xz', 's_xz',   &
-                          'sa_xz', 'vpt_xz' )
-
-                      grid_x = 'x'
-                      grid_y = 'y'
-                      grid_z = 'zu'
-!
-!--                u grid
-                   CASE ( 'u_xz' )
-
-                      grid_x = 'xu'
-                      grid_y = 'y'
-                      grid_z = 'zu'
-!
-!--                v grid
-                   CASE ( 'v_xz' )
-
-                      grid_x = 'x'
-                      grid_y = 'yv'
-                      grid_z = 'zu'
-!
-!--                w grid
-                   CASE ( 'rad_lw_in_xz', 'rad_lw_out_xz', 'rad_sw_in_xz',     &
-                          'rad_sw_out_xz', 'w_xz' )
-
-                      grid_x = 'x'
-                      grid_y = 'y'
-                      grid_z = 'zw'
-
-!
-!--                soil grid
-                   CASE ( 'm_soil_xz', 't_soil_xz' )
-
-                      grid_x = 'x'
-                      grid_y = 'y'
-                      grid_z = 'zs'
-
-                   CASE DEFAULT
-!
-!--                   Check for user-defined quantities
-                      CALL user_define_netcdf_grid( do2d(av,i), found, &
-                                                    grid_x, grid_y, grid_z )
-
-                END SELECT
-
-!
-!--             Select the respective dimension ids
-                IF ( grid_x == 'x' )  THEN
-                   id_x = id_dim_x_xz(av)
-                ELSEIF ( grid_x == 'xu' )  THEN
-                   id_x = id_dim_xu_xz(av)
-                ENDIF
-
-                IF ( grid_y == 'y' )  THEN
-                   id_y = id_dim_y_xz(av)
-                ELSEIF ( grid_y == 'yv' )  THEN
-                   id_y = id_dim_yv_xz(av)
-                ENDIF
-
-                IF ( grid_z == 'zu' )  THEN
-                   id_z = id_dim_zu_xz(av)
-                ELSEIF ( grid_z == 'zw' )  THEN
-                   id_z = id_dim_zw_xz(av)
-                ELSEIF ( grid_z == 'zs' )  THEN
-                   id_z = id_dim_zs_xz(av)
-                ENDIF
-
-!
-!--             Define the grid
-                nc_stat = NF90_DEF_VAR( id_set_xz(av), do2d(av,i),             &
-                                        nc_precision(2),                       &
-                                   (/ id_x, id_y, id_z, id_dim_time_xz(av) /), &
-                                        id_var_do2d(av,i) )
-
-                IF ( .NOT. found )  THEN
-                   WRITE ( message_string, * ) 'no grid defined for', &
-                                            ' variable ', TRIM( do2d(av,i) )
-                   CALL message( 'define_netcdf_header', 'PA0244',&
-                                                                 0, 1, 0, 6, 0 )
-                ENDIF
-
-                var_list = TRIM( var_list ) // TRIM( do2d(av,i) ) // ';'
-
-                CALL handle_netcdf_error( 'netcdf', 159 )
-!
-!--             Store the 'real' name of the variable (with *, for example)
-!--             in the long_name attribute. This is evaluated by Ferret,
-!--             for example.
-                nc_stat = NF90_PUT_ATT( id_set_xz(av), id_var_do2d(av,i), &
-                                        'long_name', do2d(av,i) )
-                CALL handle_netcdf_error( 'netcdf', 160 )
-!
-!--             Define the variable's unit
-                nc_stat = NF90_PUT_ATT( id_set_xz(av), id_var_do2d(av,i), &
-                                        'units', TRIM( do2d_unit(av,i) ) )
-                CALL handle_netcdf_error( 'netcdf', 355 )
-#if defined( __netcdf4_parallel )
-
-                IF ( netcdf_data_format > 4 )  THEN
-!
-!--                Set no fill for every variable to increase performance.
-                   nc_stat = NF90_DEF_VAR_FILL( id_set_xz(av),     &
-                                                id_var_do2d(av,i), &
-                                                1, 0 )
-                   CALL handle_netcdf_error( 'netcdf', 534 )
-!
-!--                Set independent io operations for parallel io. Collective io
-!--                is only allowed in case of a 1d-decomposition along x,
-!--                because otherwise, not all PEs have output data.
-                   IF ( npey == 1 )  THEN
-                      nc_stat = NF90_VAR_PAR_ACCESS( id_set_xz(av),     &
-                                                     id_var_do2d(av,i), &
-                                                     NF90_COLLECTIVE )
-                   ELSE
-!
-!--                   Test simulations showed that the output of cross sections
-!--                   by all PEs in data_output_2d using NF90_COLLECTIVE is 
-!--                   faster than the output by the first row of PEs in
-!--                   x-direction using NF90_INDEPENDENT.
-                      nc_stat = NF90_VAR_PAR_ACCESS( id_set_xz(av),    & 
-                                                    id_var_do2d(av,i), &
-                                                    NF90_COLLECTIVE )
-!                      nc_stat = NF90_VAR_PAR_ACCESS( id_set_xz(av),     &
-!                                                     id_var_do2d(av,i), &
-!                                                     NF90_INDEPENDENT )
-                   ENDIF
-                   CALL handle_netcdf_error( 'netcdf', 449 )
-                ENDIF
-#endif
-             ENDIF
-
-             i = i + 1
-
-          ENDDO
-
-!
-!--       No arrays to output. Close the netcdf file and return.
-          IF ( i == 1 )  RETURN
-
-!
-!--       Write the list of variables as global attribute (this is used by
-!--       restart runs and by combine_plot_fields)
-          nc_stat = NF90_PUT_ATT( id_set_xz(av), NF90_GLOBAL, 'VAR_LIST', &
-                                  var_list )
-          CALL handle_netcdf_error( 'netcdf', 161 )
-
-!
-!--       Set general no fill, otherwise the performance drops significantly for
-!--       parallel output.
-          nc_stat = NF90_SET_FILL( id_set_xz(av), NF90_NOFILL, oldmode )
-          CALL handle_netcdf_error( 'netcdf', 530 )
-
-!
-!--       Leave netCDF define mode
-          nc_stat = NF90_ENDDEF( id_set_xz(av) )
-          CALL handle_netcdf_error( 'netcdf', 162 )
-
-!
-!--       These data are only written by PE0 for parallel output to increase
-!--       the performance.
-          IF ( myid == 0  .OR.  netcdf_data_format < 5 )  THEN
-
-!
-!--          Write axis data: y_xz, x, zu, zw
-             ALLOCATE( netcdf_data(1:ns) )
-
-!
-!--          Write y_xz data (shifted by +dy/2)
-             DO  i = 1, ns
-                IF( section(i,2) == -1 )  THEN
-                   netcdf_data(i) = -1.0_wp  ! section averaged along y
-                ELSE
-                   netcdf_data(i) = ( section(i,2) + 0.5_wp ) * dy
-                ENDIF
-             ENDDO
-             nc_stat = NF90_PUT_VAR( id_set_xz(av), id_var_y_xz(av), &
-                                     netcdf_data, start = (/ 1 /),   &
-                                     count = (/ ns /) )
-             CALL handle_netcdf_error( 'netcdf', 163 )
-
-!
-!--          Write yv_xz data
-             DO  i = 1, ns
-                IF( section(i,2) == -1 )  THEN
-                   netcdf_data(i) = -1.0_wp  ! section averaged along y
-                ELSE
-                   netcdf_data(i) = section(i,2) * dy
-                ENDIF
-             ENDDO
-             nc_stat = NF90_PUT_VAR( id_set_xz(av), id_var_yv_xz(av), &
-                                     netcdf_data, start = (/ 1 /),    &
-                                     count = (/ ns /) )
-             CALL handle_netcdf_error( 'netcdf', 375 )
-
-!
-!--          Write gridpoint number data
-             netcdf_data(1:ns) = section(1:ns,2)
-             nc_stat = NF90_PUT_VAR( id_set_xz(av), id_var_ind_y_xz(av), &
-                                     netcdf_data, start = (/ 1 /),       &
-                                     count = (/ ns /) )
-             CALL handle_netcdf_error( 'netcdf', 164 )
-
-
-             DEALLOCATE( netcdf_data )
-
-!
-!--          Write data for x (shifted by +dx/2) and xu axis
-             ALLOCATE( netcdf_data(0:nx+1) )
-
-             DO  i = 0, nx+1
-                netcdf_data(i) = ( i + 0.5_wp ) * dx
-             ENDDO
-
-             nc_stat = NF90_PUT_VAR( id_set_xz(av), id_var_x_xz(av), &
-                                     netcdf_data, start = (/ 1 /),   &
-                                     count = (/ nx+2 /) )
-             CALL handle_netcdf_error( 'netcdf', 165 )
-
-             DO  i = 0, nx+1
-                netcdf_data(i) = i * dx
-             ENDDO
-
-             nc_stat = NF90_PUT_VAR( id_set_xz(av), id_var_xu_xz(av), &
-                                     netcdf_data, start = (/ 1 /),    &
-                                     count = (/ nx+2 /) )
-             CALL handle_netcdf_error( 'netcdf', 377 )
-
-             DEALLOCATE( netcdf_data )
-
-!
-!--          Write zu and zw data (vertical axes)
-             ALLOCATE( netcdf_data(0:nz+1) )
-
-             netcdf_data(0:nz+1) = zu(nzb:nzt+1)
-             nc_stat = NF90_PUT_VAR( id_set_xz(av), id_var_zu_xz(av), &
-                                     netcdf_data, start = (/ 1 /),    &
-                                     count = (/ nz+2 /) )
-             CALL handle_netcdf_error( 'netcdf', 166 )
-
-             netcdf_data(0:nz+1) = zw(nzb:nzt+1)
-             nc_stat = NF90_PUT_VAR( id_set_xz(av), id_var_zw_xz(av), &
-                                     netcdf_data, start = (/ 1 /),    &
-                                     count = (/ nz+2 /) )
-             CALL handle_netcdf_error( 'netcdf', 167 )
-
-!
-!--          Write zs data
-             IF ( land_surface )  THEN
-                netcdf_data(0:nzs-1) = - zs(nzb_soil:nzt_soil)
-                nc_stat = NF90_PUT_VAR( id_set_xz(av), id_var_zs_xz(av), &
-                                        netcdf_data(0:nzs), start = (/ 1 /),    &
-                                        count = (/ nzt_soil-nzb_soil+1 /) )
-               CALL handle_netcdf_error( 'netcdf', 548 )
-             ENDIF
-
-
-             DEALLOCATE( netcdf_data )
-
-          ENDIF
-
-
-       CASE ( 'xz_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_xz(av), NF90_GLOBAL, 'VAR_LIST', &
-                                  var_list_old )
-          CALL handle_netcdf_error( 'netcdf', 168 )
-
-          var_list = ';'
-          i = 1
-          DO WHILE ( do2d(av,i)(1:1) /= ' ' )
-             IF ( INDEX( do2d(av,i), 'xz' ) /= 0 )  THEN
-                var_list = TRIM( var_list ) // TRIM( do2d(av,i) ) // ';'
-             ENDIF
-             i = i + 1
-          ENDDO
-
-          IF ( av == 0 )  THEN
-             var = '(xz)'
-          ELSE
-             var = '(xz_av)'
-          ENDIF
-
-          IF ( TRIM( var_list ) /= TRIM( var_list_old ) )  THEN
-             message_string = 'netCDF file for cross-sections ' //           &
-                              TRIM( var ) // ' from previous run found,' //  &
-                              '& but this file cannot be extended due to' // &
-                              ' variable mismatch.' //                       &
-                              '&New file is created instead.'
-             CALL message( 'define_netcdf_header', 'PA0249', 0, 1, 0, 6, 0 )
-             extend = .FALSE.
-             RETURN
-          ENDIF
-
-!
-!--       Calculate the number of current sections
-          ns = 1
-          DO WHILE ( section(ns,2) /= -9999  .AND.  ns <= 100 )
-             ns = ns + 1
-          ENDDO
-          ns = ns - 1
-
-!
-!--       Get and compare the number of vertical cross sections
-          nc_stat = NF90_INQ_VARID( id_set_xz(av), 'y_xz', id_var_y_xz(av) )
-          CALL handle_netcdf_error( 'netcdf', 169 )
-
-          nc_stat = NF90_INQUIRE_VARIABLE( id_set_xz(av), id_var_y_xz(av), &
-                                           dimids = id_dim_y_xz_old )
-          CALL handle_netcdf_error( 'netcdf', 170 )
-          id_dim_y_xz(av) = id_dim_y_xz_old(1)
-
-          nc_stat = NF90_INQUIRE_DIMENSION( id_set_xz(av), id_dim_y_xz(av), &
-                                            len = ns_old )
-          CALL handle_netcdf_error( 'netcdf', 171 )
-
-          IF ( ns /= ns_old )  THEN
-             message_string = 'netCDF file for cross-sections ' //          &
-                              TRIM( var ) // ' from previous run found,' // &
-                              '&but this file cannot be extended due to' // &
-                              ' mismatch in number of' //                   & 
-                              '&cross sections.' //                         &
-                              '&New file is created instead.'
-             CALL message( 'define_netcdf_header', 'PA0250', 0, 1, 0, 6, 0 )
-             extend = .FALSE.
-             RETURN
-          ENDIF
-
-!
-!--       Get and compare the heights of the cross sections
-          ALLOCATE( netcdf_data(1:ns_old) )
-
-          nc_stat = NF90_GET_VAR( id_set_xz(av), id_var_y_xz(av), netcdf_data )
-          CALL handle_netcdf_error( 'netcdf', 172 )
-
-          DO  i = 1, ns
-             IF ( section(i,2) /= -1 )  THEN
-                IF ( ( ( section(i,2) + 0.5 ) * dy ) /= netcdf_data(i) )  THEN
-                   message_string = 'netCDF file for cross-sections ' //     &
-                               TRIM( var ) // ' from previous run found,' // &
-                               '&but this file cannot be extended' //        &
-                               ' due to mismatch in cross' //                &
-                               '&section levels.' //                         &
-                                '&New file is created instead.'
-                   CALL message( 'define_netcdf_header', 'PA0251', &
-                                                                 0, 1, 0, 6, 0 )
-                   extend = .FALSE.
-                   RETURN
-                ENDIF
-             ELSE
-                IF ( -1.0_wp /= netcdf_data(i) )  THEN
-                   message_string = 'netCDF file for cross-sections ' //     &
-                               TRIM( var ) // ' from previous run found,' // &
-                               '&but this file cannot be extended' //        &
-                               ' due to mismatch in cross' //                &
-                               '&section levels.' //                         &
-                               '&New file is created instead.'
-                   CALL message( 'define_netcdf_header', 'PA0251', &
-                                                                 0, 1, 0, 6, 0 )
-                   extend = .FALSE.
-                   RETURN
-                ENDIF
-             ENDIF
-          ENDDO
-
-          DEALLOCATE( netcdf_data )
-
-!
-!--       Get the id of the time coordinate (unlimited coordinate) and its
-!--       last index on the file. The next time level is do2d..count+1.
-!--       The current time must be larger than the last output time
-!--       on the file.
-          nc_stat = NF90_INQ_VARID( id_set_xz(av), 'time', id_var_time_xz(av) )
-          CALL handle_netcdf_error( 'netcdf', 173 )
-
-          nc_stat = NF90_INQUIRE_VARIABLE( id_set_xz(av), id_var_time_xz(av), &
-                                           dimids = id_dim_time_old )
-          CALL handle_netcdf_error( 'netcdf', 174 )
-          id_dim_time_xz(av) = id_dim_time_old(1)
-
-          nc_stat = NF90_INQUIRE_DIMENSION( id_set_xz(av), id_dim_time_xz(av), &
-                                            len = ntime_count )
-          CALL handle_netcdf_error( 'netcdf', 175 )
-
-!
-!--       For non-parallel output use the last output time level of the netcdf
-!--       file because the time dimension is unlimited. In case of parallel
-!--       output the variable ntime_count could get the value of 9*10E36 because
-!--       the time dimension is limited.
-          IF ( netcdf_data_format < 5 ) do2d_xz_time_count(av) = ntime_count
-
-          nc_stat = NF90_GET_VAR( id_set_xz(av), id_var_time_xz(av),    &
-                                  last_time_coordinate,                 &
-                                  start = (/ do2d_xz_time_count(av) /), &
-                                  count = (/ 1 /) )
-          CALL handle_netcdf_error( 'netcdf', 176 )
-
-          IF ( last_time_coordinate(1) >= simulated_time )  THEN
-             message_string = 'netCDF file for cross sections ' //          &
-                              TRIM( var ) // ' 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', 'PA0252', 0, 1, 0, 6, 0 )
-             do2d_xz_time_count(av) = 0
-             extend = .FALSE.
-             RETURN
-          ENDIF
-
-          IF ( netcdf_data_format > 4 )  THEN
-!
-!--          Set needed time levels (ntdim) to 
-!--          saved time levels + to be saved time levels.
-             IF ( av == 0 )  THEN 
-                ntdim_2d_xz(0) = do2d_xz_time_count(0) + CEILING(             &
-                                 ( end_time - MAX( skip_time_do2d_xz,         &
-                                                   simulated_time_at_begin )  &
-                                 ) / dt_do2d_xz )
-             ELSE
-                ntdim_2d_xz(1) = do2d_xz_time_count(1) + CEILING(             &
-                                 ( end_time - MAX( skip_time_data_output_av,  &
-                                                   simulated_time_at_begin )  &
-                                 ) / dt_data_output_av )
-             ENDIF
-
-!
-!--          Check if the needed number of output time levels is increased
-!--          compared to the number of time levels in the existing file.
-             IF ( ntdim_2d_xz(av) > ntime_count )  THEN
-                message_string = 'netCDF file for cross sections ' // &
-                                 TRIM( var ) // ' from previous run found,' // &
-                                 '&but this file cannot be extended becaus' // &
-                                 'e the number of output time levels&has b' // &
-                                 'een increased compared to the previous s' // &
-                                 'imulation.' //                               &
-                                 '&New file is created instead.'
-                CALL message( 'define_netcdf_header', 'PA0390', 0, 1, 0, 6, 0 )
-                do2d_xz_time_count(av) = 0
-                extend = .FALSE.
-!
-!--             Recalculate the needed time levels for the new file.
-                IF ( av == 0 )  THEN
-                   ntdim_2d_xz(0) = CEILING(                            &
-                           ( end_time - MAX( skip_time_do2d_xz,         &
-                                             simulated_time_at_begin )  &
-                           ) / dt_do2d_xz )
-                ELSE
-                   ntdim_2d_xz(1) = CEILING(                            &
-                           ( end_time - MAX( skip_time_data_output_av,  &
-                                             simulated_time_at_begin )  &
-                           ) / dt_data_output_av )
-                ENDIF
-                RETURN
-             ENDIF
-          ENDIF
-
-!
-!--       Dataset seems to be extendable.
-!--       Now get the variable ids.
-          i = 1
-          DO WHILE ( do2d(av,i)(1:1) /= ' ' )
-             IF ( INDEX( do2d(av,i), 'xz' ) /= 0 )  THEN
-                nc_stat = NF90_INQ_VARID( id_set_xz(av), do2d(av,i), &
-                                          id_var_do2d(av,i) )
-                CALL handle_netcdf_error( 'netcdf', 177 )
-#if defined( __netcdf4_parallel )
-!
-!--             Set independent io operations for parallel io. Collective io
-!--             is only allowed in case of a 1d-decomposition along x, because
-!--             otherwise, not all PEs have output data.
-                IF ( netcdf_data_format > 4 )  THEN
-                   IF ( npey == 1 )  THEN
-                      nc_stat = NF90_VAR_PAR_ACCESS( id_set_xz(av),     &
-                                                     id_var_do2d(av,i), &
-                                                     NF90_COLLECTIVE )
-                   ELSE
-!
-!--                   Test simulations showed that the output of cross sections
-!--                   by all PEs in data_output_2d using NF90_COLLECTIVE is 
-!--                   faster than the output by the first row of PEs in
-!--                   x-direction using NF90_INDEPENDENT.
-                      nc_stat = NF90_VAR_PAR_ACCESS( id_set_xz(av),     &
-                                                     id_var_do2d(av,i), &
-                                                     NF90_COLLECTIVE )
-!                      nc_stat = NF90_VAR_PAR_ACCESS( id_set_xz(av),     &
-!                                                     id_var_do2d(av,i), &
-!                                                     NF90_INDEPENDENT )
-                   ENDIF
-                   CALL handle_netcdf_error( 'netcdf', 455 )
-                ENDIF
-#endif
-             ENDIF
-             i = i + 1
-          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.
-          IF ( av == 0 )  THEN
-             time_average_text = ' '
-          ELSE
-             WRITE (time_average_text, '('', '',F7.1,'' s average'')') &
-                                                            averaging_interval
-          ENDIF
-          nc_stat = NF90_REDEF( id_set_xz(av) )
-          CALL handle_netcdf_error( 'netcdf', 433 )
-          nc_stat = NF90_PUT_ATT( id_set_xz(av), NF90_GLOBAL, 'title', &
-                                  TRIM( run_description_header ) //    &
-                                  TRIM( time_average_text ) )
-          CALL handle_netcdf_error( 'netcdf', 178 )
-          nc_stat = NF90_ENDDEF( id_set_xz(av) )
-          CALL handle_netcdf_error( 'netcdf', 434 )
-          message_string = 'netCDF file for cross-sections ' //           &
-                            TRIM( var ) // ' from previous run found.' // &
-                           '&This file will be extended.'
-          CALL message( 'define_netcdf_header', 'PA0253', 0, 0, 0, 6, 0 )
-
-
-       CASE ( 'yz_new' )
-
-!
-!--       Define some global attributes of the dataset
-          nc_stat = NF90_PUT_ATT( id_set_yz(av), NF90_GLOBAL, 'Conventions', &
-                                  'COARDS' )
-          CALL handle_netcdf_error( 'netcdf', 179 )
-
-          IF ( av == 0 )  THEN
-             time_average_text = ' '
-          ELSE
-             WRITE (time_average_text, '('', '',F7.1,'' s average'')') &
-                                                            averaging_interval
-          ENDIF
-          nc_stat = NF90_PUT_ATT( id_set_yz(av), NF90_GLOBAL, 'title', &
-                                  TRIM( run_description_header ) //    &
-                                  TRIM( time_average_text ) )
-          CALL handle_netcdf_error( 'netcdf', 180 )
-          IF ( av == 1 )  THEN
-             WRITE ( time_average_text,'(F7.1,'' s avg'')' )  averaging_interval
-             nc_stat = NF90_PUT_ATT( id_set_yz(av), NF90_GLOBAL, 'time_avg', &
-                                     TRIM( time_average_text ) )
-             CALL handle_netcdf_error( 'netcdf', 180 )
-          ENDIF
-
-!
-!--       Define time coordinate for yz sections.
-!--       For parallel output the time dimensions has to be limited, otherwise
-!--       the performance drops significantly.
-          IF ( netcdf_data_format < 5 )  THEN
-             nc_stat = NF90_DEF_DIM( id_set_yz(av), 'time', NF90_UNLIMITED, &
-                                     id_dim_time_yz(av) )
-             CALL handle_netcdf_error( 'netcdf', 181 )
-          ELSE
-             nc_stat = NF90_DEF_DIM( id_set_yz(av), 'time', ntdim_2d_yz(av), &
-                                     id_dim_time_yz(av) )
-             CALL handle_netcdf_error( 'netcdf', 526 )
-          ENDIF
-
-          nc_stat = NF90_DEF_VAR( id_set_yz(av), 'time', NF90_DOUBLE, &
-                                  id_dim_time_yz(av), id_var_time_yz(av) )
-          CALL handle_netcdf_error( 'netcdf', 182 )
-
-          nc_stat = NF90_PUT_ATT( id_set_yz(av), id_var_time_yz(av), 'units', &
-                                  'seconds')
-          CALL handle_netcdf_error( 'netcdf', 183 )
-
-!
-!--       Define the spatial dimensions and coordinates for yz-sections.
-!--       First, determine the number of vertical sections to be written.
-          IF ( section(1,3) == -9999 )  THEN
-             RETURN
-          ELSE
-             ns = 1
-             DO WHILE ( section(ns,3) /= -9999  .AND.  ns <= 100 )
-                ns = ns + 1
-             ENDDO
-             ns = ns - 1
-          ENDIF
-
-!
-!--       Define x axis (for scalar position)
-          nc_stat = NF90_DEF_DIM( id_set_yz(av), 'x_yz', ns, id_dim_x_yz(av) )
-          CALL handle_netcdf_error( 'netcdf', 184 )
-
-          nc_stat = NF90_DEF_VAR( id_set_yz(av), 'x_yz', NF90_DOUBLE, &
-                                  id_dim_x_yz(av), id_var_x_yz(av) )
-          CALL handle_netcdf_error( 'netcdf', 185 )
-
-          nc_stat = NF90_PUT_ATT( id_set_yz(av), id_var_x_yz(av), 'units', &
-                                  'meters' )
-          CALL handle_netcdf_error( 'netcdf', 186 )
-
-!
-!--       Define x axis (for u position)
-          nc_stat = NF90_DEF_DIM( id_set_yz(av), 'xu_yz', ns, id_dim_xu_yz(av) )
-          CALL handle_netcdf_error( 'netcdf', 377 )
-
-          nc_stat = NF90_DEF_VAR( id_set_yz(av), 'xu_yz', NF90_DOUBLE, &
-                                  id_dim_xu_yz(av), id_var_xu_yz(av) )
-          CALL handle_netcdf_error( 'netcdf', 378 )
-
-          nc_stat = NF90_PUT_ATT( id_set_yz(av), id_var_xu_yz(av), 'units', &
-                                  'meters' )
-          CALL handle_netcdf_error( 'netcdf', 379 )
-
-!
-!--       Define a variable to store the layer indices of the vertical cross
-!--       sections
-          nc_stat = NF90_DEF_VAR( id_set_yz(av), 'ind_x_yz', NF90_DOUBLE, &
-                                  id_dim_x_yz(av), id_var_ind_x_yz(av) )
-          CALL handle_netcdf_error( 'netcdf', 187 )
-
-          nc_stat = NF90_PUT_ATT( id_set_yz(av), id_var_ind_x_yz(av), 'units', &
-                                  'gridpoints')
-          CALL handle_netcdf_error( 'netcdf', 188 )
-
-!
-!--       Define y-axis (for scalar position)
-          nc_stat = NF90_DEF_DIM( id_set_yz(av), 'y', ny+2, id_dim_y_yz(av) )
-          CALL handle_netcdf_error( 'netcdf', 189 )
-
-          nc_stat = NF90_DEF_VAR( id_set_yz(av), 'y', NF90_DOUBLE, &
-                                  id_dim_y_yz(av), id_var_y_yz(av) )
-          CALL handle_netcdf_error( 'netcdf', 190 )
-
-          nc_stat = NF90_PUT_ATT( id_set_yz(av), id_var_y_yz(av), 'units', &
-                                  'meters' )
-          CALL handle_netcdf_error( 'netcdf', 191 )
-
-!
-!--       Define y-axis (for v position)
-          nc_stat = NF90_DEF_DIM( id_set_yz(av), 'yv', ny+2, id_dim_yv_yz(av) )
-          CALL handle_netcdf_error( 'netcdf', 380 )
-
-          nc_stat = NF90_DEF_VAR( id_set_yz(av), 'yv', NF90_DOUBLE, &
-                                  id_dim_yv_yz(av), id_var_yv_yz(av) )
-          CALL handle_netcdf_error( 'netcdf', 381 )
-
-          nc_stat = NF90_PUT_ATT( id_set_yz(av), id_var_yv_yz(av), 'units', &
-                                  'meters' )
-          CALL handle_netcdf_error( 'netcdf', 382 )
-
-!
-!--       Define the two z-axes (zu and zw)
-          nc_stat = NF90_DEF_DIM( id_set_yz(av), 'zu', nz+2, id_dim_zu_yz(av) )
-          CALL handle_netcdf_error( 'netcdf', 192 )
-
-          nc_stat = NF90_DEF_VAR( id_set_yz(av), 'zu', NF90_DOUBLE, &
-                                  id_dim_zu_yz(av), id_var_zu_yz(av) )
-          CALL handle_netcdf_error( 'netcdf', 193 )
-
-          nc_stat = NF90_PUT_ATT( id_set_yz(av), id_var_zu_yz(av), 'units', &
-                                  'meters' )
-          CALL handle_netcdf_error( 'netcdf', 194 )
-
-          nc_stat = NF90_DEF_DIM( id_set_yz(av), 'zw', nz+2, id_dim_zw_yz(av) )
-          CALL handle_netcdf_error( 'netcdf', 195 )
-
-          nc_stat = NF90_DEF_VAR( id_set_yz(av), 'zw', NF90_DOUBLE, &
-                                  id_dim_zw_yz(av), id_var_zw_yz(av) )
-          CALL handle_netcdf_error( 'netcdf', 196 )
-
-          nc_stat = NF90_PUT_ATT( id_set_yz(av), id_var_zw_yz(av), 'units', &
-                                  'meters' )
-          CALL handle_netcdf_error( 'netcdf', 197 )
-
-          IF ( land_surface )  THEN
-
-             nc_stat = NF90_DEF_DIM( id_set_yz(av), 'zs', nzs, id_dim_zs_yz(av) )
-             CALL handle_netcdf_error( 'netcdf', 545 )
-
-             nc_stat = NF90_DEF_VAR( id_set_yz(av), 'zs', NF90_DOUBLE, &
-                                     id_dim_zs_yz(av), id_var_zs_yz(av) )
-             CALL handle_netcdf_error( 'netcdf', 546 )
-
-             nc_stat = NF90_PUT_ATT( id_set_yz(av), id_var_zs_yz(av), 'units', &
-                                     'meters' )
-             CALL handle_netcdf_error( 'netcdf', 547 )
-
-          ENDIF
-
-
-!
-!--       Define the variables
-          var_list = ';'
-          i = 1
-
-          DO WHILE ( do2d(av,i)(1:1) /= ' ' )
-
-             IF ( INDEX( do2d(av,i), 'yz' ) /= 0 )  THEN
-
-!
-!--             Check for the grid
-                found = .TRUE.
-                SELECT CASE ( do2d(av,i) )
-!
-!--                Most variables are defined on the zu grid
-                   CASE ( 'e_yz', 'lpt_yz', 'nr_yz', 'p_yz', 'pc_yz', 'pr_yz', &
-                          'prr_yz', 'pt_yz', 'q_yz', 'qc_yz', 'ql_yz',         &
-                          'ql_c_yz', 'ql_v_yz', 'ql_vp_yz', 'qr_yz', 'qv_yz',  &
-                          'rad_lw_cs_hr_yz', 'rad_lw_hr_yz',                   &
-                          'rad_sw_cs_hr_yz', 'rad_sw_hr_yz''rho_yz', 's_yz',   &
-                          'sa_yz', 'vpt_yz' )
-
-                      grid_x = 'x'
-                      grid_y = 'y'
-                      grid_z = 'zu'
-!
-!--                u grid
-                   CASE ( 'u_yz' )
-
-                      grid_x = 'xu'
-                      grid_y = 'y'
-                      grid_z = 'zu'
-!
-!--                v grid
-                   CASE ( 'v_yz' )
-
-                      grid_x = 'x'
-                      grid_y = 'yv'
-                      grid_z = 'zu'
-!
-!--                w grid
-                   CASE ( 'rad_lw_in_yz', 'rad_lw_out_yz', 'rad_sw_in_yz',     &
-                          'rad_sw_out_yz', 'w_yz' )
-
-                      grid_x = 'x'
-                      grid_y = 'y'
-                      grid_z = 'zw'
-!
-!--                soil grid
-                   CASE ( 'm_soil_yz', 't_soil_yz' )
-
-                      grid_x = 'x'
-                      grid_y = 'y'
-                      grid_z = 'zs'
-
-                   CASE DEFAULT
-!
-!--                   Check for user-defined quantities
-                      CALL user_define_netcdf_grid( do2d(av,i), found, &
-                                                    grid_x, grid_y, grid_z )
-
-                END SELECT
-
-!
-!--             Select the respective dimension ids
-                IF ( grid_x == 'x' )  THEN
-                   id_x = id_dim_x_yz(av)
-                ELSEIF ( grid_x == 'xu' )  THEN
-                   id_x = id_dim_xu_yz(av)
-                ENDIF
-
-                IF ( grid_y == 'y' )  THEN
-                   id_y = id_dim_y_yz(av)
-                ELSEIF ( grid_y == 'yv' )  THEN
-                   id_y = id_dim_yv_yz(av)
-                ENDIF
-
-                IF ( grid_z == 'zu' )  THEN
-                   id_z = id_dim_zu_yz(av)
-                ELSEIF ( grid_z == 'zw' )  THEN
-                   id_z = id_dim_zw_yz(av)
-                ELSEIF ( grid_z == 'zs' )  THEN
-                   id_z = id_dim_zs_yz(av)
-                ENDIF
-
-!
-!--             Define the grid
-                nc_stat = NF90_DEF_VAR( id_set_yz(av), do2d(av,i),             &
-                                        nc_precision(3),                       &
-                                   (/ id_x, id_y, id_z, id_dim_time_yz(av) /), &
-                                        id_var_do2d(av,i) )
-
-                IF ( .NOT. found )  THEN
-                   WRITE ( message_string, * ) 'no grid defined for', &
-                                            ' variable ', TRIM( do2d(av,i) )
-                   CALL message( 'define_netcdf_header', 'PA0244',&
-                                                                 0, 1, 0, 6, 0 )
-                ENDIF
-
-                var_list = TRIM( var_list ) // TRIM( do2d(av,i) ) // ';'
-
-                CALL handle_netcdf_error( 'netcdf', 198 )
-!
-!--             Store the 'real' name of the variable (with *, for example)
-!--             in the long_name attribute. This is evaluated by Ferret,
-!--             for example.
-                nc_stat = NF90_PUT_ATT( id_set_yz(av), id_var_do2d(av,i), &
-                                        'long_name', do2d(av,i) )
-                CALL handle_netcdf_error( 'netcdf', 199 )
-!
-!--             Define the variable's unit
-                nc_stat = NF90_PUT_ATT( id_set_yz(av), id_var_do2d(av,i), &
-                                        'units', TRIM( do2d_unit(av,i) ) )
-                CALL handle_netcdf_error( 'netcdf', 356 )
-#if defined( __netcdf4_parallel )
-                IF ( netcdf_data_format > 4 )  THEN
-!
-!--                Set no fill for every variable to increase performance.
-                   nc_stat = NF90_DEF_VAR_FILL( id_set_yz(av),     &
-                                                id_var_do2d(av,i), &
-                                                1, 0 )
-                   CALL handle_netcdf_error( 'netcdf', 535 )
-!
-!--                Set independent io operations for parallel io. Collective io
-!--                is only allowed in case of a 1d-decomposition along y,
-!--                because otherwise, not all PEs have output data.
-                   IF ( npex == 1 )  THEN
-                      nc_stat = NF90_VAR_PAR_ACCESS( id_set_yz(av),     &
-                                                     id_var_do2d(av,i), &
-                                                     NF90_COLLECTIVE )
-                   ELSE
-!
-!--                   Test simulations showed that the output of cross sections
-!--                   by all PEs in data_output_2d using NF90_COLLECTIVE is 
-!--                   faster than the output by the first row of PEs in
-!--                   y-direction using NF90_INDEPENDENT.
-                      nc_stat = NF90_VAR_PAR_ACCESS( id_set_yz(av),     &
-                                                     id_var_do2d(av,i), &
-                                                     NF90_COLLECTIVE )
-!                      nc_stat = NF90_VAR_PAR_ACCESS( id_set_yz(av),     &
-!                                                     id_var_do2d(av,i), &
-!                                                     NF90_INDEPENDENT )
-                   ENDIF
-                   CALL handle_netcdf_error( 'netcdf', 450 )
-                ENDIF
-#endif
-             ENDIF
-
-             i = i + 1
-
-          ENDDO
-
-!
-!--       No arrays to output. Close the netcdf file and return.
-          IF ( i == 1 )  RETURN
-
-!
-!--       Write the list of variables as global attribute (this is used by
-!--       restart runs and by combine_plot_fields)
-          nc_stat = NF90_PUT_ATT( id_set_yz(av), NF90_GLOBAL, 'VAR_LIST', &
-                                  var_list )
-          CALL handle_netcdf_error( 'netcdf', 200 )
-
-!
-!--       Set general no fill, otherwise the performance drops significantly for
-!--       parallel output.
-          nc_stat = NF90_SET_FILL( id_set_yz(av), NF90_NOFILL, oldmode )
-          CALL handle_netcdf_error( 'netcdf', 531 )
-
-!
-!--       Leave netCDF define mode
-          nc_stat = NF90_ENDDEF( id_set_yz(av) )
-          CALL handle_netcdf_error( 'netcdf', 201 )
-
-!
-!--       These data are only written by PE0 for parallel output to increase
-!--       the performance.
-          IF ( myid == 0  .OR.  netcdf_data_format < 5 )  THEN
-
-!
-!--          Write axis data: x_yz, y, zu, zw
-             ALLOCATE( netcdf_data(1:ns) )
-
-!
-!--          Write x_yz data (shifted by +dx/2)
-             DO  i = 1, ns
-                IF( section(i,3) == -1 )  THEN
-                   netcdf_data(i) = -1.0_wp  ! section averaged along x
-                ELSE
-                   netcdf_data(i) = ( section(i,3) + 0.5_wp ) * dx
-                ENDIF
-             ENDDO
-             nc_stat = NF90_PUT_VAR( id_set_yz(av), id_var_x_yz(av), &
-                                     netcdf_data, start = (/ 1 /),   &
-                                     count = (/ ns /) )
-             CALL handle_netcdf_error( 'netcdf', 202 ) 
-
-!
-!--          Write x_yz data (xu grid)
-             DO  i = 1, ns
-                IF( section(i,3) == -1 )  THEN
-                   netcdf_data(i) = -1.0_wp  ! section averaged along x
-                ELSE
-                   netcdf_data(i) = section(i,3) * dx
-                ENDIF
-             ENDDO
-             nc_stat = NF90_PUT_VAR( id_set_yz(av), id_var_xu_yz(av), &
-                                     netcdf_data, start = (/ 1 /),    &
-                                     count = (/ ns /) )
-             CALL handle_netcdf_error( 'netcdf', 383 ) 
-
-!
-!--          Write gridpoint number data
-             netcdf_data(1:ns) = section(1:ns,3)
-             nc_stat = NF90_PUT_VAR( id_set_yz(av), id_var_ind_x_yz(av), &
-                                     netcdf_data, start = (/ 1 /),       &
-                                     count = (/ ns /) )
-             CALL handle_netcdf_error( 'netcdf', 203 ) 
-
-             DEALLOCATE( netcdf_data )
-
-!
-!--          Write data for y (shifted by +dy/2) and yv axis
-             ALLOCATE( netcdf_data(0:ny+1) )
-
-             DO  j = 0, ny+1
-                netcdf_data(j) = ( j + 0.5_wp ) * dy
-             ENDDO
-
-             nc_stat = NF90_PUT_VAR( id_set_yz(av), id_var_y_yz(av), &
-                                     netcdf_data, start = (/ 1 /),   &
-                                     count = (/ ny+2 /) )
-             CALL handle_netcdf_error( 'netcdf', 204 ) 
-
-             DO  j = 0, ny+1
-                netcdf_data(j) = j * dy
-             ENDDO
-
-             nc_stat = NF90_PUT_VAR( id_set_yz(av), id_var_yv_yz(av), &
-                                     netcdf_data, start = (/ 1 /),    &
-                                     count = (/ ny+2 /) )
-             CALL handle_netcdf_error( 'netcdf', 384 ) 
-
-             DEALLOCATE( netcdf_data )
-
-!
-!--          Write zu and zw data (vertical axes)
-             ALLOCATE( netcdf_data(0:nz+1) )
-
-             netcdf_data(0:nz+1) = zu(nzb:nzt+1)
-             nc_stat = NF90_PUT_VAR( id_set_yz(av), id_var_zu_yz(av), &
-                                     netcdf_data, start = (/ 1 /),    &
-                                     count = (/ nz+2 /) )
-             CALL handle_netcdf_error( 'netcdf', 205 ) 
-
-             netcdf_data(0:nz+1) = zw(nzb:nzt+1)
-             nc_stat = NF90_PUT_VAR( id_set_yz(av), id_var_zw_yz(av), &
-                                     netcdf_data, start = (/ 1 /),    &
-                                     count = (/ nz+2 /) )
-             CALL handle_netcdf_error( 'netcdf', 206 ) 
-
-             DEALLOCATE( netcdf_data )
-
-          ENDIF
-
-
-       CASE ( 'yz_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_yz(av), NF90_GLOBAL, 'VAR_LIST', &
-                                  var_list_old )
-          CALL handle_netcdf_error( 'netcdf', 207 )
-
-          var_list = ';'
-          i = 1
-          DO WHILE ( do2d(av,i)(1:1) /= ' ' )
-             IF ( INDEX( do2d(av,i), 'yz' ) /= 0 )  THEN
-                var_list = TRIM( var_list ) // TRIM( do2d(av,i) ) // ';'
-             ENDIF
-             i = i + 1
-          ENDDO
-
-          IF ( av == 0 )  THEN
-             var = '(yz)'
-          ELSE
-             var = '(yz_av)'
-          ENDIF
-
-          IF ( TRIM( var_list ) /= TRIM( var_list_old ) )  THEN
-             message_string = 'netCDF file for cross-sections ' //           &
-                              TRIM( var ) // ' from previous run found,' //  &
-                              '& but this file cannot be extended due to' // &
-                              ' variable mismatch.' //                       & 
-                              '&New file is created instead.'
-             CALL message( 'define_netcdf_header', 'PA0249', 0, 1, 0, 6, 0 )
-             extend = .FALSE.
-             RETURN
-          ENDIF
-
-!
-!--       Calculate the number of current sections
-          ns = 1
-          DO WHILE ( section(ns,3) /= -9999  .AND.  ns <= 100 )
-             ns = ns + 1
-          ENDDO
-          ns = ns - 1
-
-!
-!--       Get and compare the number of vertical cross sections
-          nc_stat = NF90_INQ_VARID( id_set_yz(av), 'x_yz', id_var_x_yz(av) )
-          CALL handle_netcdf_error( 'netcdf', 208 )
-
-          nc_stat = NF90_INQUIRE_VARIABLE( id_set_yz(av), id_var_x_yz(av), &
-                                           dimids = id_dim_x_yz_old )
-          CALL handle_netcdf_error( 'netcdf', 209 )
-          id_dim_x_yz(av) = id_dim_x_yz_old(1)
-
-          nc_stat = NF90_INQUIRE_DIMENSION( id_set_yz(av), id_dim_x_yz(av), &
-                                            len = ns_old )
-          CALL handle_netcdf_error( 'netcdf', 210 )
-
-          IF ( ns /= ns_old )  THEN
-             message_string = 'netCDF file for cross-sections ' //          &
-                              TRIM( var ) // ' from previous run found,' // &
-                              '&but this file cannot be extended due to' // &
-                              ' mismatch in number of' //                   &
-                              '&cross sections.' //                         &
-                              '&New file is created instead.'
-             CALL message( 'define_netcdf_header', 'PA0250', 0, 1, 0, 6, 0 )
-             extend = .FALSE.
-             RETURN
-          ENDIF
-
-!
-!--       Get and compare the heights of the cross sections
-          ALLOCATE( netcdf_data(1:ns_old) )
-
-          nc_stat = NF90_GET_VAR( id_set_yz(av), id_var_x_yz(av), netcdf_data )
-          CALL handle_netcdf_error( 'netcdf', 211 )
-
-          DO  i = 1, ns
-             IF ( section(i,3) /= -1 )  THEN
-                IF ( ( ( section(i,3) + 0.5 ) * dx ) /= netcdf_data(i) )  THEN
-                   message_string = 'netCDF file for cross-sections ' //    &
-                              TRIM( var ) // ' from previous run found,' // &
-                              '&but this file cannot be extended' //        &
-                              ' due to mismatch in cross' //                &
-                              '&section levels.' //                         &
-                             '&New file is created instead.'
-                   CALL message( 'define_netcdf_header', 'PA0251', &
-                                                                 0, 1, 0, 6, 0 )
-                   extend = .FALSE.
-                   RETURN
-                ENDIF
-             ELSE
-                IF ( -1.0_wp /= netcdf_data(i) )  THEN
-                   message_string = 'netCDF file for cross-sections ' //    &
-                              TRIM( var ) // ' from previous run found,' // &
-                              '&but this file cannot be extended' //        &
-                              ' due to mismatch in cross' //                &
-                              '&section levels.' //                         &
-                              '&New file is created instead.'
-                   CALL message( 'define_netcdf_header', 'PA0251', &
-                                                                 0, 1, 0, 6, 0 )
-                   extend = .FALSE.
-                   RETURN
-                ENDIF
-             ENDIF
-          ENDDO
-
-          DEALLOCATE( netcdf_data )
-
-!
-!--       Get the id of the time coordinate (unlimited coordinate) and its
-!--       last index on the file. The next time level is pl2d..count+1.
-!--       The current time must be larger than the last output time
-!--       on the file.
-          nc_stat = NF90_INQ_VARID( id_set_yz(av), 'time', id_var_time_yz(av) )
-          CALL handle_netcdf_error( 'netcdf', 212 )
-
-          nc_stat = NF90_INQUIRE_VARIABLE( id_set_yz(av), id_var_time_yz(av), &
-                                           dimids = id_dim_time_old )
-          CALL handle_netcdf_error( 'netcdf', 213 )
-          id_dim_time_yz(av) = id_dim_time_old(1)
-
-          nc_stat = NF90_INQUIRE_DIMENSION( id_set_yz(av), id_dim_time_yz(av), &
-                                            len = ntime_count )
-          CALL handle_netcdf_error( 'netcdf', 214 )
-
-!
-!--       For non-parallel output use the last output time level of the netcdf
-!--       file because the time dimension is unlimited. In case of parallel
-!--       output the variable ntime_count could get the value of 9*10E36 because
-!--       the time dimension is limited.
-          IF ( netcdf_data_format < 5 ) do2d_yz_time_count(av) = ntime_count
-
-          nc_stat = NF90_GET_VAR( id_set_yz(av), id_var_time_yz(av),    &
-                                  last_time_coordinate,                 &
-                                  start = (/ do2d_yz_time_count(av) /), &
-                                  count = (/ 1 /) )
-          CALL handle_netcdf_error( 'netcdf', 215 )
-
-          IF ( last_time_coordinate(1) >= simulated_time )  THEN
-             message_string = 'netCDF file for cross sections ' //          &
-                              TRIM( var ) // ' 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', 'PA0252', 0, 1, 0, 6, 0 )
-             do2d_yz_time_count(av) = 0
-             extend = .FALSE.
-             RETURN
-          ENDIF
-
-          IF ( netcdf_data_format > 4 )  THEN
-!
-!--          Set needed time levels (ntdim) to 
-!--          saved time levels + to be saved time levels.
-             IF ( av == 0 )  THEN 
-                ntdim_2d_yz(0) = do2d_yz_time_count(0) + CEILING(             &
-                                 ( end_time - MAX( skip_time_do2d_yz,         &
-                                                   simulated_time_at_begin )  &
-                                 ) / dt_do2d_yz )
-             ELSE
-                ntdim_2d_yz(1) = do2d_yz_time_count(1) + CEILING(             &
-                                 ( end_time - MAX( skip_time_data_output_av,  &
-                                                   simulated_time_at_begin )  &
-                                 ) / dt_data_output_av )
-             ENDIF
-
-!
-!--          Check if the needed number of output time levels is increased
-!--          compared to the number of time levels in the existing file.
-             IF ( ntdim_2d_yz(av) > ntime_count )  THEN
-                message_string = 'netCDF file for cross sections ' //          &
-                                 TRIM( var ) // ' from previous run found,' // &
-                                 '&but this file cannot be extended becaus' // &
-                                 'e the number of output time levels&has b' // &
-                                 'een increased compared to the previous s' // &
-                                 'imulation.' //                               &
-                                 '&New file is created instead.'
-                CALL message( 'define_netcdf_header', 'PA0391', 0, 1, 0, 6, 0 )
-                do2d_yz_time_count(av) = 0
-                extend = .FALSE.
-!
-!--             Recalculate the needed time levels for the new file.
-                IF ( av == 0 )  THEN
-                   ntdim_2d_yz(0) = CEILING(                            &
-                           ( end_time - MAX( skip_time_do2d_yz,         &
-                                             simulated_time_at_begin )  &
-                           ) / dt_do2d_yz )
-                ELSE
-                   ntdim_2d_yz(1) = CEILING(                            &
-                           ( end_time - MAX( skip_time_data_output_av,  &
-                                             simulated_time_at_begin )  &
-                           ) / dt_data_output_av )
-                ENDIF
-                RETURN
-             ENDIF
-          ENDIF
-
-!
-!--       Dataset seems to be extendable.
-!--       Now get the variable ids.
-          i = 1
-          DO WHILE ( do2d(av,i)(1:1) /= ' ' )
-             IF ( INDEX( do2d(av,i), 'yz' ) /= 0 )  THEN
-                nc_stat = NF90_INQ_VARID( id_set_yz(av), do2d(av,i), &
-                                          id_var_do2d(av,i) )
-                CALL handle_netcdf_error( 'netcdf', 216 )
-#if defined( __netcdf4_parallel )
-!
-!--             Set independent io operations for parallel io. Collective io
-!--             is only allowed in case of a 1d-decomposition along y, because
-!--             otherwise, not all PEs have output data.
-                IF ( netcdf_data_format > 4 )  THEN
-                   IF ( npex == 1 )  THEN
-                      nc_stat = NF90_VAR_PAR_ACCESS( id_set_yz(av),     &
-                                                     id_var_do2d(av,i), &
-                                                     NF90_COLLECTIVE )
-                   ELSE
-!
-!--                   Test simulations showed that the output of cross sections
-!--                   by all PEs in data_output_2d using NF90_COLLECTIVE is 
-!--                   faster than the output by the first row of PEs in
-!--                   y-direction using NF90_INDEPENDENT.
-                      nc_stat = NF90_VAR_PAR_ACCESS( id_set_yz(av),     &
-                                                     id_var_do2d(av,i), &
-                                                     NF90_COLLECTIVE )
-!                      nc_stat = NF90_VAR_PAR_ACCESS( id_set_yz(av),     &
-!                                                     id_var_do2d(av,i), &
-!                                                     NF90_INDEPENDENT )
-                   ENDIF
-                   CALL handle_netcdf_error( 'netcdf', 450 )
-                ENDIF
-#endif
-             ENDIF
-             i = i + 1
-          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.
-          IF ( av == 0 )  THEN
-             time_average_text = ' '
-          ELSE
-             WRITE (time_average_text, '('', '',F7.1,'' s average'')') &
-                                                            averaging_interval
-          ENDIF
-          nc_stat = NF90_REDEF( id_set_yz(av) )
-          CALL handle_netcdf_error( 'netcdf', 435 )
-          nc_stat = NF90_PUT_ATT( id_set_yz(av), NF90_GLOBAL, 'title', &
-                                  TRIM( run_description_header ) //    &
-                                  TRIM( time_average_text ) )
-          CALL handle_netcdf_error( 'netcdf', 217 )
-          nc_stat = NF90_ENDDEF( id_set_yz(av) )
-          CALL handle_netcdf_error( 'netcdf', 436 )
-          message_string = 'netCDF file for cross-sections ' //           &
-                            TRIM( var ) // ' from previous run found.' // &
-                           '&This file will be extended.'
-          CALL message( 'define_netcdf_header', 'PA0253', 0, 0, 0, 6, 0 )
-
-
-       CASE ( 'pr_new' )
-
-!
-!--       Define some global attributes of the dataset
-          IF ( averaging_interval_pr /= 0.0_wp )  THEN
-             WRITE (time_average_text,'('', '',F7.1,'' s average'')') &
-                                                            averaging_interval_pr
-             nc_stat = NF90_PUT_ATT( id_set_pr, NF90_GLOBAL, 'title',   &
-                                     TRIM( run_description_header ) //  &
-                                     TRIM( time_average_text ) )
-             CALL handle_netcdf_error( 'netcdf', 218 )
-
-             WRITE ( time_average_text,'(F7.1,'' s avg'')' ) averaging_interval_pr
-             nc_stat = NF90_PUT_ATT( id_set_pr, NF90_GLOBAL, 'time_avg', &
-                                     TRIM( time_average_text ) )
-          ELSE
-             nc_stat = NF90_PUT_ATT( id_set_pr, NF90_GLOBAL, 'title', &
-                                     TRIM( run_description_header ) )
-          ENDIF
-          CALL handle_netcdf_error( 'netcdf', 219 )
-
-!
-!--       Write number of columns and rows of coordinate systems to be plotted
-!--       on one page to the netcdf header.
-!--       This information can be used by palmplot.
-          nc_stat = NF90_PUT_ATT( id_set_pr, NF90_GLOBAL,                     &
-                                  'no_rows',                                  & 
-                                  profile_rows ) 
-          CALL handle_netcdf_error( 'netcdf', 519 )
-
-          nc_stat = NF90_PUT_ATT( id_set_pr, NF90_GLOBAL,                     &
-                                  'no_columns',                               & 
-                                  profile_columns ) 
-          CALL handle_netcdf_error( 'netcdf', 520 )
-
-
-          cross_profiles_adj  = ADJUSTL( cross_profiles )
-          cross_profiles_numb = 999999
-          cross_profiles_char = ''
-
-!
-!--       Each profile defined in cross_profiles is written to an array
-!--       (cross_profiles_char). The number of the respective coordinate
-!--       system is assigned in a second array (cross_profiles_numb).
-          k = 1
-
-          DO  i = 1, crmax
-
-             IF ( TRIM( cross_profiles_adj(i) ) == ' ' )  EXIT
-             delim_old = 0
-
-             DO   j = 1, crmax
-                delim = INDEX( cross_profiles_adj(i)(delim_old+1:), ' ' )
-                IF ( delim == 1 )  EXIT
-                kk = MIN( crmax, k )
-                cross_profiles_char(kk) = cross_profiles_adj(i)(delim_old+1: &
-                                                              delim_old+delim-1)
-                cross_profiles_numb(kk) = i
-                k = k + 1
-                cross_profiles_maxi  = i
-                delim_old = delim_old + delim
-             ENDDO
-
-          ENDDO
-
-          cross_profiles_count = MIN( crmax, k-1 )
-!
-!--       Check if all profiles defined in cross_profiles are defined in 
-!--       data_output_pr. If not, they will be skipped.
-
-          DO  i = 1, cross_profiles_count
-             DO  j = 1, dopr_n
-
-                IF ( TRIM(cross_profiles_char(i)) == TRIM(data_output_pr(j)) ) &
-                THEN
-                   EXIT
-                ENDIF
-
-                IF ( j == dopr_n )  THEN
-                   cross_profiles_numb(i) = 999999
-                ENDIF
-
-             ENDDO
-          ENDDO
-
-          DO i = 1, crmax
-             IF ( cross_profiles_numb(i) == 999999 ) THEN
-                DO j = i + 1, crmax
-                   IF ( cross_profiles_numb(j) /= 999999 ) THEN
-                      cross_profiles_char(i) = cross_profiles_char(j)
-                      cross_profiles_numb(i) = cross_profiles_numb(j)
-                      cross_profiles_numb(j) = 999999
-                      EXIT
-                   ENDIF
-                ENDDO
-             ENDIF
-          ENDDO
-
-          DO i = 1, crmax-1
-             IF ( cross_profiles_numb(i + 1) == 999999 ) THEN
-                cross_profiles_count = i
-                EXIT
-             ENDIF
-          ENDDO
-!
-!--       Check if all profiles defined in data_output_pr are defined in 
-!--       cross_profiles. If not, they will be added to cross_profiles.
-
-          DO  i = 1, dopr_n
-             DO  j = 1, cross_profiles_count
-
-                IF ( TRIM(cross_profiles_char(j)) == TRIM(data_output_pr(i))) &
-                THEN
-                   EXIT
-                ENDIF
-
-                IF (( j == cross_profiles_count ) .AND.                        &
-                    ( cross_profiles_count <= crmax - 1))  THEN
-                   cross_profiles_count = cross_profiles_count + 1
-                   cross_profiles_maxi  = cross_profiles_maxi  + 1
-                   cross_profiles_char(MIN( crmax, cross_profiles_count )) =  &
-                                                      TRIM( data_output_pr(i) )
-                   cross_profiles_numb(MIN( crmax, cross_profiles_count )) =  &
-                                                      cross_profiles_maxi
-                ENDIF
-
-             ENDDO
-          ENDDO
-
-          IF ( cross_profiles_count >= crmax )  THEN
-             message_string = 'It is not allowed to arrange more than '     &
-                              // '100 profiles with & cross_profiles. Apart'&
-                              // ' from that, all profiles are saved & to ' &
-                              // 'the netCDF file.'
-             CALL message( 'define_netcdf_header', 'PA0354', 0, 0, 0, 6, 0 )
-          ENDIF
-
-!
-!--       Writing cross_profiles to netcdf header. This information can be 
-!--       used by palmplot. Each profile is separated by ",", each cross is 
-!--       separated by ";".
-          char_cross_profiles = ';'
-          id_last = 1
-          cross_profiles_count = MIN( cross_profiles_count, crmax )
-
-          DO  i = 1, cross_profiles_count
-
-             IF ( cross_profiles_numb(i) /= 999999 )  THEN
-                IF ( TRIM( char_cross_profiles ) == ';' )  THEN
-                   char_cross_profiles = TRIM( char_cross_profiles ) // &
-                                         TRIM( cross_profiles_char(i) )
-                ELSEIF ( id_last == cross_profiles_numb(i) )  THEN
-                   char_cross_profiles = TRIM( char_cross_profiles ) // &
-                                         ',' // TRIM( cross_profiles_char(i) )
-                ELSE
-                   char_cross_profiles = TRIM( char_cross_profiles ) // &
-                                         ';' // TRIM( cross_profiles_char(i) )
-                ENDIF
-                id_last = cross_profiles_numb(i)
-             ENDIF
-
-          ENDDO
-
-          char_cross_profiles = TRIM( char_cross_profiles ) // ';'
-
-          nc_stat = NF90_PUT_ATT( id_set_pr, NF90_GLOBAL, 'cross_profiles',   &
-                                  TRIM( char_cross_profiles ) )
-          CALL handle_netcdf_error( 'netcdf', 521 )
-
-!
-!--       Define time coordinate for profiles (unlimited dimension)
-          nc_stat = NF90_DEF_DIM( id_set_pr, 'time', NF90_UNLIMITED, &
-                                  id_dim_time_pr )
-          CALL handle_netcdf_error( 'netcdf', 220 )
-
-          nc_stat = NF90_DEF_VAR( id_set_pr, 'time', NF90_DOUBLE, &
-                                  id_dim_time_pr, id_var_time_pr )
-          CALL handle_netcdf_error( 'netcdf', 221 )
-
-          nc_stat = NF90_PUT_ATT( id_set_pr, id_var_time_pr, 'units', 'seconds')
-          CALL handle_netcdf_error( 'netcdf', 222 )
-
-!
-!--       Define the variables
-          var_list = ';'
-          DO  i = 1, dopr_n
-
-             IF ( statistic_regions == 0 )  THEN
-
-!
-!--             Define the z-axes (each variable gets its own z-axis)
-                nc_stat = NF90_DEF_DIM( id_set_pr,                        &
-                                        'z' // TRIM( data_output_pr(i) ), &
-                                        nzt+2-nzb, id_dim_z_pr(i,0) )
-                CALL handle_netcdf_error( 'netcdf', 223 )
-
-                nc_stat = NF90_DEF_VAR( id_set_pr,                        &
-                                        'z' // TRIM( data_output_pr(i) ), &
-                                        NF90_DOUBLE, id_dim_z_pr(i,0),    &
-                                        id_var_z_pr(i,0) )
-                CALL handle_netcdf_error( 'netcdf', 224 )
-
-                nc_stat = NF90_PUT_ATT( id_set_pr, id_var_z_pr(i,0), 'units', &
-                                        'meters' )
-                CALL handle_netcdf_error( 'netcdf', 225 )
-!
-!--             Define the variable
-                nc_stat = NF90_DEF_VAR( id_set_pr, data_output_pr(i),         &
-                                        nc_precision(5), (/ id_dim_z_pr(i,0), &
-                                        id_dim_time_pr /), id_var_dopr(i,0) )
-                CALL handle_netcdf_error( 'netcdf', 226 )
-
-                nc_stat = NF90_PUT_ATT( id_set_pr, id_var_dopr(i,0), &
-                                        'long_name', TRIM( data_output_pr(i) ) )
-                CALL handle_netcdf_error( 'netcdf', 227 )
-
-                nc_stat = NF90_PUT_ATT( id_set_pr, id_var_dopr(i,0), &
-                                        'units', TRIM( dopr_unit(i) ) )
-                CALL handle_netcdf_error( 'netcdf', 228 )
-
-                var_list = TRIM( var_list ) // TRIM( data_output_pr(i) ) //  ';'
-
-             ELSE
-!
-!--             If statistic regions are defined, add suffix _SR+#SR to the
-!--             names
-                DO  j = 0, statistic_regions
-                   WRITE ( suffix, '(''_'',I1)' )  j
-
-!
-!--                Define the z-axes (each variable gets it own z-axis)
-                   nc_stat = NF90_DEF_DIM( id_set_pr,                          &
-                                         'z'//TRIM(data_output_pr(i))//suffix, &
-                                           nzt+2-nzb, id_dim_z_pr(i,j) )
-                   CALL handle_netcdf_error( 'netcdf', 229 )
-
-                   nc_stat = NF90_DEF_VAR( id_set_pr,                          &
-                                         'z'//TRIM(data_output_pr(i))//suffix, &
-                                           nc_precision(5), id_dim_z_pr(i,j),  &
-                                           id_var_z_pr(i,j) )
-                   CALL handle_netcdf_error( 'netcdf', 230 )
-
-                   nc_stat = NF90_PUT_ATT( id_set_pr, id_var_z_pr(i,j), &
-                                           'units', 'meters' )
-                   CALL handle_netcdf_error( 'netcdf', 231 )
-
-!
-!--                Define the variable
-                   nc_stat = NF90_DEF_VAR( id_set_pr,                         &
-                                           TRIM(data_output_pr(i)) // suffix, &
-                                           nc_precision(5),                   &
-                                           (/ id_dim_z_pr(i,j),               &
-                                           id_dim_time_pr /), id_var_dopr(i,j) )
-                   CALL handle_netcdf_error( 'netcdf', 232 )
-
-                   nc_stat = NF90_PUT_ATT( id_set_pr, id_var_dopr(i,j),        &
-                                           'long_name',                        &
-                                           TRIM( data_output_pr(i) ) // ' SR ' &
-                                           // suffix(2:2) )
-                   CALL handle_netcdf_error( 'netcdf', 233 )
-
-                   nc_stat = NF90_PUT_ATT( id_set_pr, id_var_dopr(i,j), &
-                                           'units', TRIM( dopr_unit(i) ) )
-                   CALL handle_netcdf_error( 'netcdf', 234 )
-
-                   var_list = TRIM( var_list ) // TRIM( data_output_pr(i) ) // &
-                              suffix // ';'
-
-                ENDDO
-
-             ENDIF
-
-          ENDDO
-
-!
-!--       Write the list of variables as global attribute (this is used by
-!--       restart runs)
-          nc_stat = NF90_PUT_ATT( id_set_pr, NF90_GLOBAL, 'VAR_LIST', var_list )
-          CALL handle_netcdf_error( 'netcdf', 235 )
-
-!
-!--       Define normalization variables (as time series)
-          DO  i = 1, dopr_norm_num
-
-             nc_stat = NF90_DEF_VAR( id_set_pr, 'NORM_' // &
-                                     TRIM( dopr_norm_names(i) ), &
-                                     nc_precision(5), (/ id_dim_time_pr /), &
-                                     id_var_norm_dopr(i) )
-             CALL handle_netcdf_error( 'netcdf', 236 )
-
-             nc_stat = NF90_PUT_ATT( id_set_pr, id_var_norm_dopr(i), &
-                                     'long_name',                    &
-                                     TRIM( dopr_norm_longnames(i) ) )
-             CALL handle_netcdf_error( 'netcdf', 237 )
-
-          ENDDO
-
-!
-!--       Leave netCDF define mode
-          nc_stat = NF90_ENDDEF( id_set_pr )
-          CALL handle_netcdf_error( 'netcdf', 238 )
-
-!
-!--       Write z-axes data
-          DO  i = 1, dopr_n
-             DO  j = 0, statistic_regions
-
-                nc_stat = NF90_PUT_VAR( id_set_pr, id_var_z_pr(i,j),      &
-                                        hom(nzb:nzt+1,2,dopr_index(i),0), &
-                                        start = (/ 1 /),                  &
-                                        count = (/ nzt-nzb+2 /) )
-                CALL handle_netcdf_error( 'netcdf', 239 )
-
-             ENDDO
-          ENDDO
-
-
-       CASE ( 'pr_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_pr, NF90_GLOBAL, 'VAR_LIST', &
-                                  var_list_old )
-          CALL handle_netcdf_error( 'netcdf', 240 )
-
-          var_list = ';'
-          DO  i = 1, dopr_n
-
-             IF ( statistic_regions == 0 )  THEN
-                var_list = TRIM( var_list ) // TRIM( data_output_pr(i) ) // ';'
-             ELSE
-                DO  j = 0, statistic_regions
-                   WRITE ( suffix, '(''_'',I1)' )  j
-                   var_list = TRIM( var_list ) // TRIM( data_output_pr(i) ) // &
-                              suffix // ';'
-                ENDDO
-             ENDIF
-
-          ENDDO
-
-          IF ( TRIM( var_list ) /= TRIM( var_list_old ) )  THEN
-             message_string = 'netCDF file for vertical profiles ' //        &
-                              'from previous run found,' //                  &
-                              '& but this file cannot be extended due to' // &
-                              ' variable mismatch.' //                       &
-                              '&New file is created instead.'
-             CALL message( 'define_netcdf_header', 'PA0254', 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 dopr..count+1.
-!--       The current time must be larger than the last output time
-!--       on the file.
-          nc_stat = NF90_INQ_VARID( id_set_pr, 'time', id_var_time_pr )
-          CALL handle_netcdf_error( 'netcdf', 241 )
-
-          nc_stat = NF90_INQUIRE_VARIABLE( id_set_pr, id_var_time_pr, &
-                                           dimids = id_dim_time_old )
-          CALL handle_netcdf_error( 'netcdf', 242 )
-          id_dim_time_pr = id_dim_time_old(1)
-
-          nc_stat = NF90_INQUIRE_DIMENSION( id_set_pr, id_dim_time_pr, &
-                                            len = dopr_time_count )
-          CALL handle_netcdf_error( 'netcdf', 243 )
-
-          nc_stat = NF90_GET_VAR( id_set_pr, id_var_time_pr,        &
-                                  last_time_coordinate,             &
-                                  start = (/ dopr_time_count /), &
-                                  count = (/ 1 /) )
-          CALL handle_netcdf_error( 'netcdf', 244 )
-
-          IF ( last_time_coordinate(1) >= simulated_time )  THEN
-             message_string = 'netCDF file for vertical profiles ' //       &
-                              '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', 'PA0255', 0, 1, 0, 6, 0 )
-             dopr_time_count = 0
-             extend = .FALSE.
-             RETURN
-          ENDIF
-
-!
-!--       Dataset seems to be extendable.
-!--       Now get the variable ids.
-          i = 1
-          DO  i = 1, dopr_n
- 
-             IF ( statistic_regions == 0 )  THEN
-                nc_stat = NF90_INQ_VARID( id_set_pr, data_output_pr(i), &
-                                          id_var_dopr(i,0) )
-                CALL handle_netcdf_error( 'netcdf', 245 )
-             ELSE
-                DO  j = 0, statistic_regions
-                   WRITE ( suffix, '(''_'',I1)' )  j
-                   netcdf_var_name = TRIM( data_output_pr(i) ) // suffix
-                   nc_stat = NF90_INQ_VARID( id_set_pr, netcdf_var_name, &
-                                             id_var_dopr(i,j) )
-                   CALL handle_netcdf_error( 'netcdf', 246 )
-                ENDDO
-             ENDIF
-
-          ENDDO
-
-!
-!--       Get ids of the normalization variables
-          DO  i = 1, dopr_norm_num
-             nc_stat = NF90_INQ_VARID( id_set_pr,                             &
-                                       'NORM_' // TRIM( dopr_norm_names(i) ), &
-                                       id_var_norm_dopr(i) )
-             CALL handle_netcdf_error( 'netcdf', 247 )
-          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.
-          IF ( averaging_interval_pr == 0.0_wp )  THEN
-             time_average_text = ' '
-          ELSE
-             WRITE (time_average_text, '('', '',F7.1,'' s average'')') &
-                                                            averaging_interval_pr
-          ENDIF
-          nc_stat = NF90_REDEF( id_set_pr )
-          CALL handle_netcdf_error( 'netcdf', 437 )
-          nc_stat = NF90_PUT_ATT( id_set_pr, NF90_GLOBAL, 'title', &
-                                  TRIM( run_description_header ) //    &
-                                  TRIM( time_average_text ) )
-          CALL handle_netcdf_error( 'netcdf', 248 )
-
-          nc_stat = NF90_ENDDEF( id_set_pr )
-          CALL handle_netcdf_error( 'netcdf', 438 )
-          message_string = 'netCDF file for vertical profiles ' // &
-                           'from previous run found.' //           &
-                           '&This file will be extended.'
-          CALL message( 'define_netcdf_header', 'PA0256', 0, 0, 0, 6, 0 )
-
-
-       CASE ( 'ts_new' )
-
-!
-!--       Define some global attributes of the dataset
-          nc_stat = NF90_PUT_ATT( id_set_ts, NF90_GLOBAL, 'title', &
-                                  TRIM( run_description_header ) )
-          CALL handle_netcdf_error( 'netcdf', 249 )
-
-!
-!--       Define time coordinate for time series (unlimited dimension)
-          nc_stat = NF90_DEF_DIM( id_set_ts, 'time', NF90_UNLIMITED, &
-                                  id_dim_time_ts )
-          CALL handle_netcdf_error( 'netcdf', 250 )
-
-          nc_stat = NF90_DEF_VAR( id_set_ts, 'time', NF90_DOUBLE, &
-                                  id_dim_time_ts, id_var_time_ts )
-          CALL handle_netcdf_error( 'netcdf', 251 )
-
-          nc_stat = NF90_PUT_ATT( id_set_ts, id_var_time_ts, 'units', 'seconds')
-          CALL handle_netcdf_error( 'netcdf', 252 )
-
-!
-!--       Define the variables
-          var_list = ';'
-          DO  i = 1, dots_num
-
-             IF ( statistic_regions == 0 )  THEN
-
-                nc_stat = NF90_DEF_VAR( id_set_ts, dots_label(i),              &
-                                        nc_precision(6), (/ id_dim_time_ts /), &
-                                        id_var_dots(i,0) )
-                CALL handle_netcdf_error( 'netcdf', 253 )
-
-                nc_stat = NF90_PUT_ATT( id_set_ts, id_var_dots(i,0), &
-                                        'long_name', TRIM( dots_label(i) ) )
-                CALL handle_netcdf_error( 'netcdf', 254 )
-
-                nc_stat = NF90_PUT_ATT( id_set_ts, id_var_dots(i,0), &
-                                        'units', TRIM( dots_unit(i) ) )
-                CALL handle_netcdf_error( 'netcdf', 255 )
-
-                var_list = TRIM( var_list ) // TRIM( dots_label(i) ) // ';'
-
-             ELSE
-!
-!--             If statistic regions are defined, add suffix _SR+#SR to the
-!--             names
-                DO  j = 0, statistic_regions
-                   WRITE ( suffix, '(''_'',I1)' )  j
-
-                   nc_stat = NF90_DEF_VAR( id_set_ts,                         &
-                                           TRIM( dots_label(i) ) // suffix,   &
-                                           nc_precision(6),                   &
-                                           (/ id_dim_time_ts /),              &
-                                           id_var_dots(i,j) )
-                   CALL handle_netcdf_error( 'netcdf', 256 )
-
-                   nc_stat = NF90_PUT_ATT( id_set_ts, id_var_dots(i,j),       &
-                                           'long_name',                       &
-                                           TRIM( dots_label(i) ) // ' SR ' // &
-                                           suffix(2:2) )
-                   CALL handle_netcdf_error( 'netcdf', 257 )
-
-                   nc_stat = NF90_PUT_ATT( id_set_ts, id_var_dots(i,j), &
-                                           'units', TRIM( dots_unit(i) ) )
-                   CALL handle_netcdf_error( 'netcdf', 347 )
-
-                   var_list = TRIM( var_list ) // TRIM( dots_label(i) ) // &
-                              suffix // ';'
-
-                ENDDO
-
-             ENDIF
-
-          ENDDO
-
-!
-!--       Write the list of variables as global attribute (this is used by
-!--       restart runs)
-          nc_stat = NF90_PUT_ATT( id_set_ts, NF90_GLOBAL, 'VAR_LIST', var_list )
-          CALL handle_netcdf_error( 'netcdf', 258 )
-
-!
-!--       Leave netCDF define mode
-          nc_stat = NF90_ENDDEF( id_set_ts )
-          CALL handle_netcdf_error( 'netcdf', 259 )
-
-
-       CASE ( 'ts_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_ts, NF90_GLOBAL, 'VAR_LIST', &
-                                  var_list_old )
-          CALL handle_netcdf_error( 'netcdf', 260 )
-
-          var_list = ';'
-          i = 1
-          DO  i = 1, dots_num
-
-             IF ( statistic_regions == 0 )  THEN
-                var_list = TRIM( var_list ) // TRIM( dots_label(i) ) // ';'
-             ELSE
-                DO  j = 0, statistic_regions
-                   WRITE ( suffix, '(''_'',I1)' )  j
-                   var_list = TRIM( var_list ) // TRIM( dots_label(i) ) // &
-                              suffix // ';'
-                ENDDO
-             ENDIF
-
-          ENDDO
-
-          IF ( TRIM( var_list ) /= TRIM( var_list_old ) )  THEN
-             message_string = 'netCDF file for 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 dots..count+1.
-!--       The current time must be larger than the last output time
-!--       on the file.
-          nc_stat = NF90_INQ_VARID( id_set_ts, 'time', id_var_time_ts )
-          CALL handle_netcdf_error( 'netcdf', 261 )
-
-          nc_stat = NF90_INQUIRE_VARIABLE( id_set_ts, id_var_time_ts, &
-                                           dimids = id_dim_time_old )
-          CALL handle_netcdf_error( 'netcdf', 262 )
-          id_dim_time_ts = id_dim_time_old(1)
-
-          nc_stat = NF90_INQUIRE_DIMENSION( id_set_ts, id_dim_time_ts, &
-                                            len = dots_time_count )
-          CALL handle_netcdf_error( 'netcdf', 263 )
-
-          nc_stat = NF90_GET_VAR( id_set_ts, id_var_time_ts,        &
-                                  last_time_coordinate,             &
-                                  start = (/ dots_time_count /), &
-                                  count = (/ 1 /) )
-          CALL handle_netcdf_error( 'netcdf', 264 )
-
-          IF ( last_time_coordinate(1) >= simulated_time )  THEN
-             message_string = 'netCDF file for 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 )
-             dots_time_count = 0
-             extend = .FALSE.
-             RETURN
-          ENDIF
-
-!
-!--       Dataset seems to be extendable.
-!--       Now get the variable ids
-          i = 1
-          DO  i = 1, dots_num
- 
-             IF ( statistic_regions == 0 )  THEN
-                nc_stat = NF90_INQ_VARID( id_set_ts, dots_label(i), &
-                                          id_var_dots(i,0) )
-                CALL handle_netcdf_error( 'netcdf', 265 )
-             ELSE
-                DO  j = 0, statistic_regions
-                   WRITE ( suffix, '(''_'',I1)' )  j
-                   netcdf_var_name = TRIM( dots_label(i) ) // suffix
-                   nc_stat = NF90_INQ_VARID( id_set_ts, netcdf_var_name, &
-                                             id_var_dots(i,j) )
-                   CALL handle_netcdf_error( 'netcdf', 266 )
-                ENDDO
-             ENDIF
-
-          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_ts )
-          CALL handle_netcdf_error( 'netcdf', 439 )
-          nc_stat = NF90_PUT_ATT( id_set_ts, NF90_GLOBAL, 'title', &
-                                  TRIM( run_description_header ) )
-          CALL handle_netcdf_error( 'netcdf', 267 )
-          nc_stat = NF90_ENDDEF( id_set_ts )
-          CALL handle_netcdf_error( 'netcdf', 440 )
-          message_string = 'netCDF file for time series ' // &
-                           'from previous run found.' //     &
-                           '&This file will be extended.'
-          CALL message( 'define_netcdf_header', 'PA0259', 0, 0, 0, 6, 0 )
-
-
-       CASE ( 'sp_new' )
-
-!
-!--       Define some global attributes of the dataset
-          IF ( averaging_interval_sp /= 0.0_wp )  THEN
-             WRITE (time_average_text,'('', '',F7.1,'' s average'')') &
-                                                            averaging_interval_sp
-             nc_stat = NF90_PUT_ATT( id_set_sp, NF90_GLOBAL, 'title',  &
-                                     TRIM( run_description_header ) // &
-                                     TRIM( time_average_text ) )
-             CALL handle_netcdf_error( 'netcdf', 268 )
-
-             WRITE ( time_average_text,'(F7.1,'' s avg'')' )  averaging_interval_sp
-             nc_stat = NF90_PUT_ATT( id_set_sp, NF90_GLOBAL, 'time_avg', &
-                                     TRIM( time_average_text ) )
-          ELSE
-             nc_stat = NF90_PUT_ATT( id_set_sp, NF90_GLOBAL, 'title', &
-                                     TRIM( run_description_header ) )
-          ENDIF
-          CALL handle_netcdf_error( 'netcdf', 269 )
-
-!
-!--       Define time coordinate for spectra (unlimited dimension)
-          nc_stat = NF90_DEF_DIM( id_set_sp, 'time', NF90_UNLIMITED, &
-                                  id_dim_time_sp )
-          CALL handle_netcdf_error( 'netcdf', 270 )
-
-          nc_stat = NF90_DEF_VAR( id_set_sp, 'time', NF90_DOUBLE, &
-                                  id_dim_time_sp, id_var_time_sp )
-          CALL handle_netcdf_error( 'netcdf', 271 )
-
-          nc_stat = NF90_PUT_ATT( id_set_sp, id_var_time_sp, 'units', 'seconds')
-          CALL handle_netcdf_error( 'netcdf', 272 )
-
-!
-!--       Define the spatial dimensions and coordinates for spectra.
-!--       First, determine the number of vertical levels for which spectra
-!--       are to be output.
-          ns = 1
-          DO WHILE ( comp_spectra_level(ns) /= 999999  .AND.  ns <= 100 )
-             ns = ns + 1
-          ENDDO
-          ns = ns - 1
-
-!
-!--       Define vertical coordinate grid (zu grid)
-          nc_stat = NF90_DEF_DIM( id_set_sp, 'zu_sp', ns, id_dim_zu_sp )
-          CALL handle_netcdf_error( 'netcdf', 273 )
-
-          nc_stat = NF90_DEF_VAR( id_set_sp, 'zu_sp', NF90_DOUBLE, &
-                                  id_dim_zu_sp, id_var_zu_sp )
-          CALL handle_netcdf_error( 'netcdf', 274 )
-
-          nc_stat = NF90_PUT_ATT( id_set_sp, id_var_zu_sp, 'units', 'meters' )
-          CALL handle_netcdf_error( 'netcdf', 275 )
-
-!
-!--       Define vertical coordinate grid (zw grid)
-          nc_stat = NF90_DEF_DIM( id_set_sp, 'zw_sp', ns, id_dim_zw_sp )
-          CALL handle_netcdf_error( 'netcdf', 276 )
-
-          nc_stat = NF90_DEF_VAR( id_set_sp, 'zw_sp', NF90_DOUBLE, &
-                                  id_dim_zw_sp, id_var_zw_sp )
-          CALL handle_netcdf_error( 'netcdf', 277 )
-
-          nc_stat = NF90_PUT_ATT( id_set_sp, id_var_zw_sp, 'units', 'meters' )
-          CALL handle_netcdf_error( 'netcdf', 278 )
-
-!
-!--       Define x-axis
-          nc_stat = NF90_DEF_DIM( id_set_sp, 'k_x', nx/2, id_dim_x_sp )
-          CALL handle_netcdf_error( 'netcdf', 279 )
-
-          nc_stat = NF90_DEF_VAR( id_set_sp, 'k_x', NF90_DOUBLE, id_dim_x_sp, &
-                                  id_var_x_sp )
-          CALL handle_netcdf_error( 'netcdf', 280 )
-
-          nc_stat = NF90_PUT_ATT( id_set_sp, id_var_x_sp, 'units', 'm-1' )
-          CALL handle_netcdf_error( 'netcdf', 281 )
-
-!
-!--       Define y-axis
-          nc_stat = NF90_DEF_DIM( id_set_sp, 'k_y', ny/2, id_dim_y_sp )
-          CALL handle_netcdf_error( 'netcdf', 282 )
-
-          nc_stat = NF90_DEF_VAR( id_set_sp, 'k_y', NF90_DOUBLE, id_dim_y_sp, &
-                                  id_var_y_sp )
-          CALL handle_netcdf_error( 'netcdf', 283 )
-
-          nc_stat = NF90_PUT_ATT( id_set_sp, id_var_y_sp, 'units', 'm-1' )
-          CALL handle_netcdf_error( 'netcdf', 284 )
-
-!
-!--       Define the variables
-          var_list = ';'
-          i = 1
-          DO WHILE ( data_output_sp(i) /= ' '  .AND.  i <= 10 )
-!
-!--          First check for the vertical grid
-             found = .TRUE.
-             SELECT CASE ( data_output_sp(i) )
-!
-!--             Most variables are defined on the zu levels
-                CASE ( 'e', 'lpt', 'nr', 'p', 'pc', 'pr', 'prr', 'pt', 'q',   &
-                       'qc', 'ql', 'ql_c', 'ql_v', 'ql_vp', 'qr', 'qv', 'rho',&
-                       's', 'sa', 'u', 'v', 'vpt' )
-
-                   grid_z = 'zu'
-!
-!--             zw levels
-                CASE ( 'w' )
-
-                   grid_z = 'zw'
-
-                CASE DEFAULT
-!
-!--                Check for user-defined quantities (found, grid_x and grid_y
-!--                are dummies)
-                   CALL user_define_netcdf_grid( data_output_sp(i), found,  &
-                                                 grid_x, grid_y, grid_z )
-
-             END SELECT
-
-             IF ( INDEX( spectra_direction(i), 'x' ) /= 0 )  THEN
-
-!
-!--             Define the variable
-                netcdf_var_name = TRIM( data_output_sp(i) ) // '_x'
-                IF ( TRIM( grid_z ) == 'zw' )  THEN
-                   nc_stat = NF90_DEF_VAR( id_set_sp, netcdf_var_name,      &
-                                           nc_precision(7), (/ id_dim_x_sp, &
-                                           id_dim_zw_sp, id_dim_time_sp /), &
-                                           id_var_dospx(i) )
-                ELSE
-                   nc_stat = NF90_DEF_VAR( id_set_sp, netcdf_var_name,      &
-                                           nc_precision(7), (/ id_dim_x_sp, &
-                                           id_dim_zu_sp, id_dim_time_sp /), &
-                                           id_var_dospx(i) )
-                ENDIF
-                CALL handle_netcdf_error( 'netcdf', 285 )
-
-                nc_stat = NF90_PUT_ATT( id_set_sp, id_var_dospx(i), &
-                                        'long_name', netcdf_var_name )
-                CALL handle_netcdf_error( 'netcdf', 286 )
-
-                nc_stat = NF90_PUT_ATT( id_set_sp, id_var_dospx(i), &
-                                        'units', 'unknown' )
-                CALL handle_netcdf_error( 'netcdf', 287 )
-
-                var_list = TRIM( var_list ) // TRIM( netcdf_var_name ) // ';'
-
-             ENDIF
-
-             IF ( INDEX( spectra_direction(i), 'y' ) /= 0 )  THEN
-
-!
-!--             Define the variable
-                netcdf_var_name = TRIM( data_output_sp(i) ) // '_y'
-                IF ( TRIM( grid_z ) == 'zw' )  THEN
-                   nc_stat = NF90_DEF_VAR( id_set_sp, netcdf_var_name,      &
-                                           nc_precision(7), (/ id_dim_y_sp, &
-                                           id_dim_zw_sp, id_dim_time_sp /), &
-                                           id_var_dospy(i) )
-                ELSE
-                   nc_stat = NF90_DEF_VAR( id_set_sp, netcdf_var_name,      &
-                                           nc_precision(7), (/ id_dim_y_sp, &
-                                           id_dim_zu_sp, id_dim_time_sp /), &
-                                           id_var_dospy(i) )
-                ENDIF
-                CALL handle_netcdf_error( 'netcdf', 288 )
-
-                nc_stat = NF90_PUT_ATT( id_set_sp, id_var_dospy(i), &
-                                        'long_name', netcdf_var_name )
-                CALL handle_netcdf_error( 'netcdf', 289 )
-
-                nc_stat = NF90_PUT_ATT( id_set_sp, id_var_dospy(i), &
-                                        'units', 'unknown' )
-                CALL handle_netcdf_error( 'netcdf', 290 )
-
-                var_list = TRIM( var_list ) // TRIM( netcdf_var_name ) // ';'
-
-             ENDIF
-
-             i = i + 1
-
-          ENDDO
-
-!
-!--       Write the list of variables as global attribute (this is used by
-!--       restart runs)
-          nc_stat = NF90_PUT_ATT( id_set_sp, NF90_GLOBAL, 'VAR_LIST', var_list )
-          CALL handle_netcdf_error( 'netcdf', 291 )
-
-!
-!--       Leave netCDF define mode
-          nc_stat = NF90_ENDDEF( id_set_sp )
-          CALL handle_netcdf_error( 'netcdf', 292 )
-
-!
-!--       Write axis data: zu_sp, zw_sp, k_x, k_y
-          ALLOCATE( netcdf_data(1:ns) )
-
-!
-!--       Write zu data
-          netcdf_data(1:ns) = zu( comp_spectra_level(1:ns) )
-          nc_stat = NF90_PUT_VAR( id_set_sp, id_var_zu_sp, netcdf_data, &
-                                  start = (/ 1 /), count = (/ ns /) )
-          CALL handle_netcdf_error( 'netcdf', 293 )
-
-!
-!--       Write zw data
-          netcdf_data(1:ns) = zw( comp_spectra_level(1:ns) )
-          nc_stat = NF90_PUT_VAR( id_set_sp, id_var_zw_sp, netcdf_data, &
-                                  start = (/ 1 /), count = (/ ns /) )
-          CALL handle_netcdf_error( 'netcdf', 294 )
-
-          DEALLOCATE( netcdf_data )
-
-!
-!--       Write data for x and y axis (wavenumbers)
-          ALLOCATE( netcdf_data(nx/2) )
-          DO  i = 1, nx/2
-             netcdf_data(i) = 2.0_wp * pi * i / ( dx * ( nx + 1 ) )
-          ENDDO
-
-          nc_stat = NF90_PUT_VAR( id_set_sp, id_var_x_sp, netcdf_data, &
-                                  start = (/ 1 /), count = (/ nx/2 /) )
-          CALL handle_netcdf_error( 'netcdf', 295 )
-
-          DEALLOCATE( netcdf_data )
-
-          ALLOCATE( netcdf_data(ny/2) )
-          DO  i = 1, ny/2
-             netcdf_data(i) = 2.0_wp * pi * i / ( dy * ( ny + 1 ) )
-          ENDDO
-
-          nc_stat = NF90_PUT_VAR( id_set_sp, id_var_y_sp, netcdf_data, &
-                                  start = (/ 1 /), count = (/ ny/2 /) )
-          CALL handle_netcdf_error( 'netcdf', 296 )
-
-          DEALLOCATE( netcdf_data )
-
-
-       CASE ( 'sp_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_sp, NF90_GLOBAL, 'VAR_LIST', &
-                                  var_list_old )
-          CALL handle_netcdf_error( 'netcdf', 297 )
-          var_list = ';'
-          i = 1
-          DO WHILE ( data_output_sp(i) /= ' '  .AND.  i <= 10 )
-
-             IF ( INDEX( spectra_direction(i), 'x' ) /= 0 )  THEN
-                netcdf_var_name = TRIM( data_output_sp(i) ) // '_x'
-                var_list = TRIM( var_list ) // TRIM( netcdf_var_name ) // ';'
-             ENDIF
-
-             IF ( INDEX( spectra_direction(i), 'y' ) /= 0 )  THEN
-                netcdf_var_name = TRIM( data_output_sp(i) ) // '_y'
-                var_list = TRIM( var_list ) // TRIM( netcdf_var_name ) // ';'
-             ENDIF
-
-             i = i + 1
-
-          ENDDO
-
-          IF ( TRIM( var_list ) /= TRIM( var_list_old ) )  THEN
-             message_string = 'netCDF file for spectra  ' //                 &
-                              'from previous run found,' //                  &
-                              '& but this file cannot be extended due to' // &
-                              ' variable mismatch.' //                       &
-                              '&New file is created instead.'
-             CALL message( 'define_netcdf_header', 'PA0260', 0, 1, 0, 6, 0 )
-             extend = .FALSE.
-             RETURN
-          ENDIF
-
-!
-!--       Determine the number of current vertical levels for which spectra
-!--       shall be output
-          ns = 1
-          DO WHILE ( comp_spectra_level(ns) /= 999999  .AND.  ns <= 100 )
-             ns = ns + 1
-          ENDDO
-          ns = ns - 1
-
-!
-!--       Get and compare the number of vertical levels
-          nc_stat = NF90_INQ_VARID( id_set_sp, 'zu_sp', id_var_zu_sp )
-          CALL handle_netcdf_error( 'netcdf', 298 )
-
-          nc_stat = NF90_INQUIRE_VARIABLE( id_set_sp, id_var_zu_sp, &
-                                           dimids = id_dim_zu_sp_old )
-          CALL handle_netcdf_error( 'netcdf', 299 )
-          id_dim_zu_sp = id_dim_zu_sp_old(1)
-
-          nc_stat = NF90_INQUIRE_DIMENSION( id_set_sp, id_dim_zu_sp, &
-                                            len = ns_old )
-          CALL handle_netcdf_error( 'netcdf', 300 )
-
-          IF ( ns /= ns_old )  THEN
-             message_string = 'netCDF file for spectra ' //                 &
-                              ' from previous run found,' //                &
-                              '&but this file cannot be extended due to' // &
-                              ' mismatch in number of' //                   &
-                              '&vertical levels.' //                        &
-                              '&New file is created instead.'
-             CALL message( 'define_netcdf_header', 'PA0261', 0, 1, 0, 6, 0 )
-             extend = .FALSE.
-             RETURN
-          ENDIF
-
-!
-!--       Get and compare the heights of the cross sections
-          ALLOCATE( netcdf_data(1:ns_old) )
-
-          nc_stat = NF90_GET_VAR( id_set_sp, id_var_zu_sp, netcdf_data )
-          CALL handle_netcdf_error( 'netcdf', 301 )
-
-          DO  i = 1, ns
-             IF ( zu(comp_spectra_level(i)) /= netcdf_data(i) )  THEN
-                message_string = 'netCDF file for spectra ' //                 &
-                                 ' from previous run found,' //                &
-                                 '&but this file cannot be extended due to' // &
-                                 ' mismatch in heights of' //                  &
-                                 '&vertical levels.' //                        &
-                                 '&New file is created instead.'
-                CALL message( 'define_netcdf_header', 'PA0262', 0, 1, 0, 6, 0 )
-                extend = .FALSE.
-                RETURN
-             ENDIF
-          ENDDO
-
-          DEALLOCATE( netcdf_data )
-
-!
-!--       Get the id of the time coordinate (unlimited coordinate) and its
-!--       last index on the file. The next time level is plsp..count+1.
-!--       The current time must be larger than the last output time
-!--       on the file.
-          nc_stat = NF90_INQ_VARID( id_set_sp, 'time', id_var_time_sp )
-          CALL handle_netcdf_error( 'netcdf', 302 )
-
-          nc_stat = NF90_INQUIRE_VARIABLE( id_set_sp, id_var_time_sp, &
-                                           dimids = id_dim_time_old )
-          CALL handle_netcdf_error( 'netcdf', 303 )
-          id_dim_time_sp = id_dim_time_old(1)
-
-          nc_stat = NF90_INQUIRE_DIMENSION( id_set_sp, id_dim_time_sp, &
-                                            len = dosp_time_count )
-          CALL handle_netcdf_error( 'netcdf', 304 )
-
-          nc_stat = NF90_GET_VAR( id_set_sp, id_var_time_sp,        &
-                                  last_time_coordinate,             &
-                                  start = (/ dosp_time_count /), &
-                                  count = (/ 1 /) )
-          CALL handle_netcdf_error( 'netcdf', 305 )
-
-          IF ( last_time_coordinate(1) >= simulated_time )  THEN
-             message_string = 'netCDF file for spectra ' //                 &
-                              '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', 'PA0263', 0, 1, 0, 6, 0 )
-             dosp_time_count = 0
-             extend = .FALSE.
-             RETURN
-          ENDIF
-
-!
-!--       Dataset seems to be extendable.
-!--       Now get the variable ids.
-          i = 1
-          DO WHILE ( data_output_sp(i) /= ' '  .AND.  i <= 10 )
-
-             IF ( INDEX( spectra_direction(i), 'x' ) /= 0 )  THEN
-                netcdf_var_name = TRIM( data_output_sp(i) ) // '_x'
-                nc_stat = NF90_INQ_VARID( id_set_sp, netcdf_var_name, &
-                                          id_var_dospx(i) )
-                CALL handle_netcdf_error( 'netcdf', 306 )
-             ENDIF
-
-             IF ( INDEX( spectra_direction(i), 'y' ) /= 0 )  THEN
-                netcdf_var_name = TRIM( data_output_sp(i) ) // '_y'
-                nc_stat = NF90_INQ_VARID( id_set_sp, netcdf_var_name, &
-                                          id_var_dospy(i) )
-                CALL handle_netcdf_error( 'netcdf', 307 )
-             ENDIF
-
-             i = i + 1
-
-          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_sp )
-          CALL handle_netcdf_error( 'netcdf', 441 )
-          IF ( averaging_interval_sp /= 0.0_wp )  THEN
-             WRITE (time_average_text,'('', '',F7.1,'' s average'')') &
-                                                           averaging_interval_sp
-             nc_stat = NF90_PUT_ATT( id_set_sp, NF90_GLOBAL, 'title',  &
-                                     TRIM( run_description_header ) // &
-                                     TRIM( time_average_text ) )
-             CALL handle_netcdf_error( 'netcdf', 308 )
-
-             WRITE ( time_average_text,'(F7.1,'' s avg'')' )  averaging_interval_sp
-             nc_stat = NF90_PUT_ATT( id_set_sp, NF90_GLOBAL, 'time_avg', &
-                                     TRIM( time_average_text ) )
-          ELSE
-             nc_stat = NF90_PUT_ATT( id_set_sp, NF90_GLOBAL, 'title', &
-                                     TRIM( run_description_header ) )
-          ENDIF
-          CALL handle_netcdf_error( 'netcdf', 309 )
-          nc_stat = NF90_ENDDEF( id_set_sp )
-          CALL handle_netcdf_error( 'netcdf', 442 )
-          message_string = 'netCDF file for spectra ' //     &
-                           'from previous run found.' //     &
-                           '&This file will be extended.'
-          CALL message( 'define_netcdf_header', 'PA0264', 0, 0, 0, 6, 0 )
-
-
-       CASE ( 'pt_new' )
-
-!
-!--       Define some global attributes of the dataset
-          nc_stat = NF90_PUT_ATT( id_set_prt, NF90_GLOBAL, 'title', &
-                                  TRIM( run_description_header ) )
-          CALL handle_netcdf_error( 'netcdf', 310 )
-
-!
-!--       Define time coordinate for particles (unlimited dimension)
-          nc_stat = NF90_DEF_DIM( id_set_prt, 'time', NF90_UNLIMITED, &
-                                  id_dim_time_prt )
-          CALL handle_netcdf_error( 'netcdf', 311 )
-
-          nc_stat = NF90_DEF_VAR( id_set_prt, 'time', NF90_DOUBLE, &
-                                  id_dim_time_prt, id_var_time_prt )
-          CALL handle_netcdf_error( 'netcdf', 312 )
-
-          nc_stat = NF90_PUT_ATT( id_set_prt, id_var_time_prt, 'units', &
-                                  'seconds' )
-          CALL handle_netcdf_error( 'netcdf', 313 )
-
-!
-!--       Define particle coordinate (maximum particle number)
-          IF ( netcdf_data_format < 3 )  THEN
-             nc_stat = NF90_DEF_DIM( id_set_prt, 'particle_number', &
-                                     maximum_number_of_particles, id_dim_prtnum)
-          ELSE
-!
-!--          netCDF4 allows more than one unlimited dimension
-             nc_stat = NF90_DEF_DIM( id_set_prt, 'particle_number', &
-                                     NF90_UNLIMITED, id_dim_prtnum)
-          ENDIF
-          CALL handle_netcdf_error( 'netcdf', 314 )
-
-          nc_stat = NF90_DEF_VAR( id_set_prt, 'particle_number', NF90_DOUBLE, &
-                                  id_dim_prtnum, id_var_prtnum )
-          CALL handle_netcdf_error( 'netcdf', 315 )
-
-          nc_stat = NF90_PUT_ATT( id_set_prt, id_var_prtnum, 'units', &
-                                  'particle number' )
-          CALL handle_netcdf_error( 'netcdf', 316 )
-
-!
-!--       Define variable which contains the real number of particles in use
-          nc_stat = NF90_DEF_VAR( id_set_prt, 'real_num_of_prt', NF90_INT, &
-                                  id_dim_time_prt, id_var_rnop_prt )
-          CALL handle_netcdf_error( 'netcdf', 317 )
-
-          nc_stat = NF90_PUT_ATT( id_set_prt, id_var_rnop_prt, 'units', &
-                                  'particle number' )
-          CALL handle_netcdf_error( 'netcdf', 318 )
-
-!
-!--       Define the variables
-          DO  i = 1, 17
-
-             nc_stat = NF90_DEF_VAR( id_set_prt, prt_var_names(i),         &
-                                     nc_precision(8),                      &
-                                     (/ id_dim_prtnum, id_dim_time_prt /), &
-                                     id_var_prt(i) )
-             CALL handle_netcdf_error( 'netcdf', 319 )
-
-             nc_stat = NF90_PUT_ATT( id_set_prt, id_var_prt(i), &
-                                     'long_name', TRIM( prt_var_names(i) ) )
-             CALL handle_netcdf_error( 'netcdf', 320 )
-
-             nc_stat = NF90_PUT_ATT( id_set_prt, id_var_prt(i), &
-                                     'units', TRIM( prt_var_units(i) ) )
-             CALL handle_netcdf_error( 'netcdf', 321 )
-
-          ENDDO
-
-!
-!--       Leave netCDF define mode
-          nc_stat = NF90_ENDDEF( id_set_prt )
-          CALL handle_netcdf_error( 'netcdf', 322 )
-
-
-       CASE ( 'pt_ext' )
-
-!
-!--       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_prt, 'time', id_var_time_prt )
-          CALL handle_netcdf_error( 'netcdf', 323 )
-
-          nc_stat = NF90_INQUIRE_VARIABLE( id_set_prt, id_var_time_prt, &
-                                           dimids = id_dim_time_old )
-          CALL handle_netcdf_error( 'netcdf', 324 )
-          id_dim_time_prt = id_dim_time_old(1)
-
-          nc_stat = NF90_INQUIRE_DIMENSION( id_set_prt, id_dim_time_prt, &
-                                            len = prt_time_count )
-          CALL handle_netcdf_error( 'netcdf', 325 )
-
-          nc_stat = NF90_GET_VAR( id_set_prt, id_var_time_prt,  &
-                                  last_time_coordinate,         &
-                                  start = (/ prt_time_count /), &
-                                  count = (/ 1 /) )
-          CALL handle_netcdf_error( 'netcdf', 326 )
-
-          IF ( last_time_coordinate(1) >= simulated_time )  THEN
-             message_string = 'netCDF file for particles ' //               &
-                              '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 )
-             prt_time_count = 0
-             extend = .FALSE.
-             RETURN
-          ENDIF
-
-!
-!--       Dataset seems to be extendable.
-!--       Now get the variable ids.
-          nc_stat = NF90_INQ_VARID( id_set_prt, 'real_num_of_prt', &
-                                    id_var_rnop_prt )
-          CALL handle_netcdf_error( 'netcdf', 327 )
-
-          DO  i = 1, 17
-
-             nc_stat = NF90_INQ_VARID( id_set_prt, prt_var_names(i), &
-                                       id_var_prt(i) )
-             CALL handle_netcdf_error( 'netcdf', 328 )
-
-          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 ( 'ps_new' )
-
-!
-!--       Define some global attributes of the dataset
-          nc_stat = NF90_PUT_ATT( id_set_pts, NF90_GLOBAL, 'title', &
-                                  TRIM( run_description_header ) )
-          CALL handle_netcdf_error( 'netcdf', 396 )
-
-!
-!--       Define time coordinate for particle time series (unlimited dimension)
-          nc_stat = NF90_DEF_DIM( id_set_pts, 'time', NF90_UNLIMITED, &
-                                  id_dim_time_pts )
-          CALL handle_netcdf_error( 'netcdf', 397 )
-
-          nc_stat = NF90_DEF_VAR( id_set_pts, 'time', NF90_DOUBLE, &
-                                  id_dim_time_pts, id_var_time_pts )
-          CALL handle_netcdf_error( 'netcdf', 398 )
-
-          nc_stat = NF90_PUT_ATT( id_set_pts, id_var_time_pts, 'units', &
-                                  'seconds')
-          CALL handle_netcdf_error( 'netcdf', 399 )
-
-!
-!--       Define the variables. If more than one particle group is defined,
-!--       define seperate variables for each group
-          var_list = ';'
-          DO  i = 1, dopts_num
-
-             DO  j = 0, number_of_particle_groups
-
-                IF ( j == 0 )  THEN
-                   suffix1 = ''
-                ELSE
-                   WRITE ( suffix1, '(''_'',I2.2)' )  j
-                ENDIF
-
-                nc_stat = NF90_DEF_VAR( id_set_pts,                         &
-                                        TRIM( dopts_label(i) ) // suffix1,  &
-                                        nc_precision(6),                    &
-                                        (/ id_dim_time_pts /),              &
-                                        id_var_dopts(i,j) )
-                CALL handle_netcdf_error( 'netcdf', 400 )
-
-                IF ( j == 0 )  THEN
-                   nc_stat = NF90_PUT_ATT( id_set_pts, id_var_dopts(i,j), &
-                                           'long_name', &
-                                           TRIM( dopts_label(i) ) )
-                ELSE
-                   nc_stat = NF90_PUT_ATT( id_set_pts, id_var_dopts(i,j), &
-                                           'long_name', &
-                                           TRIM( dopts_label(i) ) // ' PG ' // &
-                                           suffix1(2:3) )
-                ENDIF
-                CALL handle_netcdf_error( 'netcdf', 401 )
-
-                nc_stat = NF90_PUT_ATT( id_set_pts, id_var_dopts(i,j), &
-                                        'units', TRIM( dopts_unit(i) ) )
-                CALL handle_netcdf_error( 'netcdf', 402 )
-
-                var_list = TRIM( var_list ) // TRIM( dopts_label(i) ) // &
-                           suffix1 // ';'
-
-                IF ( number_of_particle_groups == 1 )  EXIT
-
-             ENDDO
-
-          ENDDO
-
-!
-!--       Write the list of variables as global attribute (this is used by
-!--       restart runs)
-          nc_stat = NF90_PUT_ATT( id_set_pts, NF90_GLOBAL, 'VAR_LIST', &
-                                  var_list )
-          CALL handle_netcdf_error( 'netcdf', 403 )
-
-
-!
-!--       Leave netCDF define mode
-          nc_stat = NF90_ENDDEF( id_set_pts )
-          CALL handle_netcdf_error( 'netcdf', 404 )
-
-
-       CASE ( 'ps_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_pts, NF90_GLOBAL, 'VAR_LIST', &
-                                  var_list_old )
-          CALL handle_netcdf_error( 'netcdf', 405 )
-
-          var_list = ';'
-          i = 1
-          DO  i = 1, dopts_num
-
-             DO  j = 0, number_of_particle_groups
-
-                IF ( j == 0 )  THEN
-                   suffix1 = ''
-                ELSE
-                   WRITE ( suffix1, '(''_'',I2.2)' )  j
-                ENDIF
-
-                var_list = TRIM( var_list ) // TRIM( dopts_label(i) ) // &
-                           suffix1 // ';'
-
-                IF ( number_of_particle_groups == 1 )  EXIT
-
-             ENDDO
-
-          ENDDO
-
-          IF ( TRIM( var_list ) /= TRIM( var_list_old ) )  THEN
-             message_string = 'netCDF file for particle 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', 'PA0267', 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 dots..count+1.
-!--       The current time must be larger than the last output time
-!--       on the file.
-          nc_stat = NF90_INQ_VARID( id_set_pts, 'time', id_var_time_pts )
-          CALL handle_netcdf_error( 'netcdf', 406 )
-
-          nc_stat = NF90_INQUIRE_VARIABLE( id_set_pts, id_var_time_pts, &
-                                           dimids = id_dim_time_old )
-          CALL handle_netcdf_error( 'netcdf', 407 )
-          id_dim_time_pts = id_dim_time_old(1)
-
-          nc_stat = NF90_INQUIRE_DIMENSION( id_set_pts, id_dim_time_pts, &
-                                            len = dopts_time_count )
-          CALL handle_netcdf_error( 'netcdf', 408 )
-
-          nc_stat = NF90_GET_VAR( id_set_pts, id_var_time_pts,    &
-                                  last_time_coordinate,           &
-                                  start = (/ dopts_time_count /), &
-                                  count = (/ 1 /) )
-          CALL handle_netcdf_error( 'netcdf', 409 )
-
-          IF ( last_time_coordinate(1) >= simulated_time )  THEN
-             message_string = 'netCDF file for particle 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', 'PA0268', 0, 1, 0, 6, 0 )
-             dopts_time_count = 0
-             extend = .FALSE.
-             RETURN
-          ENDIF
-
-!
-!--       Dataset seems to be extendable.
-!--       Now get the variable ids
-          i = 1
-          DO  i = 1, dopts_num
- 
-             DO  j = 0, number_of_particle_groups
-
-                IF ( j == 0 )  THEN
-                   suffix1 = ''
-                ELSE
-                   WRITE ( suffix1, '(''_'',I2.2)' )  j
-                ENDIF
-
-                netcdf_var_name = TRIM( dopts_label(i) ) // suffix1
-
-                nc_stat = NF90_INQ_VARID( id_set_pts, netcdf_var_name, &
-                                          id_var_dopts(i,j) )
-                CALL handle_netcdf_error( 'netcdf', 410 )
-
-                IF ( number_of_particle_groups == 1 )  EXIT
-
-             ENDDO
-
-          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_pts )
-          CALL handle_netcdf_error( 'netcdf', 443 )
-          nc_stat = NF90_PUT_ATT( id_set_pts, NF90_GLOBAL, 'title', &
-                                  TRIM( run_description_header ) )
-          CALL handle_netcdf_error( 'netcdf', 411 )
-          nc_stat = NF90_ENDDEF( id_set_pts )
-          CALL handle_netcdf_error( 'netcdf', 444 )
-          message_string = 'netCDF file for particle time series ' // &
-                           'from previous run found.' //              &
-                           '&This file will be extended.'
-          CALL message( 'define_netcdf_header', 'PA0269', 0, 0, 0, 6, 0 )
-
-
-       CASE DEFAULT
-
-          message_string = 'mode "' // TRIM( mode) // '" not supported'
-          CALL message( 'define_netcdf_header', 'PA0270', 0, 0, 0, 6, 0 )
-
-    END SELECT
-
-#endif
- END SUBROUTINE define_netcdf_header
-
-
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Creates a netCDF file and give back the id. The parallel flag has to be TRUE
-!> for parallel netCDF output support.
-!------------------------------------------------------------------------------!
- 
- SUBROUTINE create_netcdf_file( filename , id, parallel, errno )
-#if defined( __netcdf )
-
-
-    USE control_parameters
-    USE netcdf
-    USE netcdf_control
-    USE pegrid
-
-    IMPLICIT NONE
-
-    CHARACTER (LEN=*), INTENT(IN) :: filename
-    INTEGER, INTENT(IN)           :: errno
-    INTEGER, INTENT(OUT)          :: id
-    LOGICAL, INTENT(IN)           :: parallel
-
-
-!
-!-- Create a new netCDF output file with requested netCDF format
-    IF ( netcdf_data_format == 1 )  THEN
-!
-!--    Classic netCDF format
-       nc_stat = NF90_CREATE( filename, NF90_NOCLOBBER, id )
-
-    ELSEIF ( netcdf_data_format == 2 )  THEN
-!
-!--    64bit-offset format
-       nc_stat = NF90_CREATE( filename,                                        &
-                              IOR( NF90_NOCLOBBER, NF90_64BIT_OFFSET ), id )
-
-#if defined( __netcdf4 )
-    ELSEIF ( netcdf_data_format == 3  .OR.                                     &
-             ( .NOT. parallel  .AND.  netcdf_data_format == 5 ) )  THEN
-!
-!--    netCDF4/HDF5 format
-       nc_stat = NF90_CREATE( filename, IOR( NF90_NOCLOBBER, NF90_NETCDF4 ), id )
-
-    ELSEIF ( netcdf_data_format == 4  .OR.                                     &
-             ( .NOT. parallel  .AND.  netcdf_data_format == 6 ) )  THEN
-!
-!--    netCDF4/HDF5 format with classic model flag
-       nc_stat = NF90_CREATE( filename,                                        &
-                              IOR( NF90_NOCLOBBER,                             &
-                              IOR( NF90_CLASSIC_MODEL, NF90_HDF5 ) ), id )
-
-#if defined( __netcdf4_parallel )
-    ELSEIF ( netcdf_data_format == 5  .AND.  parallel )  THEN
-!
-!--    netCDF4/HDF5 format, parallel
-       nc_stat = NF90_CREATE( filename,                                        &
-                              IOR( NF90_NOCLOBBER,                             &
-                              IOR( NF90_NETCDF4, NF90_MPIIO ) ),               &
-                              id, COMM = comm2d, INFO = MPI_INFO_NULL )
-
-    ELSEIF ( netcdf_data_format == 6  .AND.  parallel )  THEN
-!
-!--    netCDF4/HDF5 format with classic model flag, parallel
-       nc_stat = NF90_CREATE( filename,                                        &
-                              IOR( NF90_NOCLOBBER,                             &
-                              IOR( NF90_MPIIO,                                 &
-                              IOR( NF90_CLASSIC_MODEL, NF90_HDF5 ) ) ),        &
-                              id, COMM = comm2d, INFO = MPI_INFO_NULL )
-
-#endif
-#endif
-    ENDIF
-
-    CALL handle_netcdf_error( 'create_netcdf_file', errno )
-#endif
- END SUBROUTINE create_netcdf_file
-
-
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Opens an existing netCDF file for writing and gives back the id.
-!> The parallel flag has to be TRUE for parallel netCDF output support.
-!------------------------------------------------------------------------------!
- 
- SUBROUTINE open_write_netcdf_file( filename, id, parallel, errno )
-#if defined( __netcdf )
-
-
-    USE control_parameters,                                                    &
-        ONLY:  netcdf_data_format
-
-    USE netcdf
-
-    USE netcdf_control
-
-    USE pegrid
-
-    IMPLICIT NONE
-
-    CHARACTER (LEN=*), INTENT(IN) :: filename
-    INTEGER, INTENT(IN)           :: errno
-    INTEGER, INTENT(OUT)          :: id
-    LOGICAL, INTENT(IN)           :: parallel
-
-
-    IF ( netcdf_data_format < 5  .OR.  .NOT. parallel )  THEN
-       nc_stat = NF90_OPEN( filename, NF90_WRITE, id )
-#if defined( __netcdf4 )
-#if defined( __netcdf4_parallel )
-    ELSEIF ( netcdf_data_format > 4  .AND.  parallel )  THEN
-       nc_stat = NF90_OPEN( filename, IOR( NF90_WRITE, NF90_MPIIO ), id,  &
-                            COMM = comm2d, INFO = MPI_INFO_NULL )
-#endif
-#endif
-    ENDIF
-
-    CALL handle_netcdf_error( 'open_write_netcdf_file', errno )
-#endif
- END SUBROUTINE open_write_netcdf_file
-
-
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Prints out a text message corresponding to the current status.
-!------------------------------------------------------------------------------!
- 
- SUBROUTINE handle_netcdf_error( routine_name, errno )
-#if defined( __netcdf )
-
-
-    USE control_parameters,                                                    &
-        ONLY:  message_string
-
-    USE netcdf
-
-    USE netcdf_control
-
-    USE pegrid
-
-    IMPLICIT NONE
-
-    CHARACTER(LEN=6) ::  message_identifier
-    CHARACTER(LEN=*) ::  routine_name
-
-    INTEGER(iwp) ::  errno
-
-    IF ( nc_stat /= NF90_NOERR )  THEN
-
-       WRITE( message_identifier, '(''NC'',I4.4)' )  errno
-       message_string = TRIM( NF90_STRERROR( nc_stat ) )
-
-       CALL message( routine_name, message_identifier, 2, 2, 0, 6, 1 )
-
-    ENDIF
-
-#endif
- END SUBROUTINE handle_netcdf_error
Index: palm/trunk/SOURCE/netcdf_interface.f90
===================================================================
--- palm/trunk/SOURCE/netcdf_interface.f90	(revision 1783)
+++ palm/trunk/SOURCE/netcdf_interface.f90	(revision 1783)
@@ -0,0 +1,5119 @@
+!> @file netcdf_interface.f90
+!--------------------------------------------------------------------------------!
+! This file is part of PALM.
+!
+! PALM is free software: you can redistribute it and/or modify it under the terms
+! of the GNU General Public License as published by the Free Software Foundation,
+! either version 3 of the License, or (at your option) any later version.
+!
+! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
+! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+!
+! You should have received a copy of the GNU General Public License along with
+! PALM. If not, see <http://www.gnu.org/licenses/>.
+!
+! Copyright 1997-2015 Leibniz Universitaet Hannover
+!--------------------------------------------------------------------------------!
+!
+! Current revisions:
+! ------------------
+! netcdf interface has been modularized, former file netcdf renamed to
+! netcdf_interface, creation of netcdf-dimensions and -variables moved to
+! specific new subroutines create_netcdf_dim and create_netcdf_var,
+! compression (deflation) of variables implemented,
+! ibmy special cpp directive removed
+! 
+! Former revisions:
+! -----------------
+! $Id$
+!
+! 1745 2016-02-05 13:06:51Z gronemeier
+! Bugfix: recalculating ntdim_3d, ntdim_2d_xy/xz/yz when checking the
+!         extensibility of an existing file (only when using parallel NetCDF). 
+! 
+! 1691 2015-10-26 16:17:44Z maronga
+! Added output of radiative heating rates for RRTMG. Corrected output of 
+! radiative fluxes
+! 
+! 1682 2015-10-07 23:56:08Z knoop
+! Code annotations made doxygen readable 
+! 
+! 1596 2015-05-21 09:34:28Z gronemeier
+! Bugfix in masked data output. Read 'zu_3d' when trying to extend masked data
+! 
+! 1551 2015-03-03 14:18:16Z maronga
+! Added support for land surface model and radiation model output. In the course
+! of this action a new vertical grid zs (soil) was introduced.
+! 
+! 1353 2014-04-08 15:21:23Z heinze
+! REAL constants provided with KIND-attribute
+!
+! 1322 2014-03-20 16:38:49Z raasch
+! Forgotten ONLY-attribute added to USE-statements
+!
+! 1320 2014-03-20 08:40:49Z raasch
+! ONLY-attribute added to USE-statements,
+! kind-parameters added to all INTEGER and REAL declaration statements,
+! kinds are defined in new module kinds,
+! revision history before 2012 removed,
+! comment fields (!:) to be used for variable explanations added to
+! all variable declaration statements
+!
+! 1308 2014-03-13 14:58:42Z fricke
+! +ntime_count, oldmode
+! Adjust NF90_CREATE and NF90_OPEN statement for parallel output
+! To increase the performance for parallel output, the following is done:
+! - Limit time dimension
+! - Values of axis data are only written by PE0
+! - No fill is set for all variables
+! Check the number of output time levels for restart jobs
+!
+! 1206 2013-07-18 12:49:16Z witha
+! Bugfix: typo in preprocessor directive in subroutine open_write_netcdf_file
+!
+! 1092 2013-02-02 11:24:22Z raasch
+! unused variables removed
+!
+! 1053 2012-11-13 17:11:03Z hoffmann
+! +qr, nr, prr
+!
+! 1036 2012-10-22 13:43:42Z raasch
+! code put under GPL (PALM 3.9)
+!
+! 1031 2012-10-19 14:35:30Z raasch
+! netCDF4 without parallel file support implemented, new routines
+! create_netcdf_file and open_write_netcdf_file at end
+!
+! 992 2012-09-05 15:08:26Z hoffmann
+! Removal of the informative messages PA0352 and PA0353.
+!  
+! 983 2012-08-21 14:17:57Z hoffmann
+! Bugfix in cross_profiles.
+!
+! 964 2012-07-26 09:14:24Z raasch
+! rev 951 and 959 reformatted
+!
+! 959 2012-07-24 13:13:41Z hoffmann
+! Bugfix in cross_profiles. It is not allowed to arrange more than 100
+! profiles with cross_profiles. 
+!
+! 951 2012-07-19 14:22:52Z hoffmann
+! cross_profiles, profile_rows, profile_columns are written to netCDF header
+!
+! Revision 1.1  2005/05/18 15:37:16  raasch
+! Initial revision
+!
+!
+! Description:
+! ------------
+!> In case of extend = .FALSE.:
+!> Define all necessary dimensions, axes and variables for the different
+!> netCDF datasets. This subroutine is called from check_open after a new
+!> dataset is created. It leaves the open netCDF files ready to write.
+!>
+!> In case of extend = .TRUE.:
+!> Find out if dimensions and variables of an existing file match the values
+!> of the actual run. If so, get all necessary information (ids, etc.) from
+!> this file.
+!>
+!> Parameter av can assume values 0 (non-averaged data) and 1 (time averaged
+!> data)
+!>
+!> @todo calculation of output time levels for parallel NetCDF still does not
+!>       cover every exception (change of dt_do, end_time in restart)
+!------------------------------------------------------------------------------!
+ MODULE netcdf_interface
+
+    USE control_parameters, ONLY: max_masks
+    USE kinds
+#if defined( __netcdf )
+    USE NETCDF
+#endif
+
+    PRIVATE
+
+    INTEGER(iwp), PARAMETER ::  dopr_norm_num = 7, dopts_num = 29, dots_max = 100
+
+    CHARACTER (LEN=6), DIMENSION(dopr_norm_num) ::  dopr_norm_names =          &
+         (/ 'wpt0  ', 'ws2   ', 'tsw2  ', 'ws3   ', 'ws2tsw', 'wstsw2',        &
+            'z_i   ' /)
+
+    CHARACTER (LEN=6), DIMENSION(dopr_norm_num) ::  dopr_norm_longnames =      &
+         (/ 'wpt0  ', 'w*2   ', 't*w2  ', 'w*3   ', 'w*2t*w', 'w*t*w2',        &
+            'z_i   ' /)
+
+    CHARACTER (LEN=7), DIMENSION(dopts_num) :: dopts_label =                   &
+          (/ 'tnpt   ', 'x_     ', 'y_     ', 'z_     ', 'z_abs  ', 'u      ', &
+             'v      ', 'w      ', 'u"     ', 'v"     ', 'w"     ', 'npt_up ', &
+             'w_up   ', 'w_down ', 'radius ', 'r_min  ', 'r_max  ', 'npt_max', &
+             'npt_min', 'x*2    ', 'y*2    ', 'z*2    ', 'u*2    ', 'v*2    ', &
+             'w*2    ', 'u"2    ', 'v"2    ', 'w"2    ', 'npt*2  ' /)
+
+    CHARACTER (LEN=7), DIMENSION(dopts_num) :: dopts_unit =                    &
+          (/ 'number ', 'm      ', 'm      ', 'm      ', 'm      ', 'm/s    ', &
+             'm/s    ', 'm/s    ', 'm/s    ', 'm/s    ', 'm/s    ', 'number ', &
+             'm/s    ', 'm/s    ', 'm      ', 'm      ', 'm      ', 'number ', &
+             'number ', 'm2     ', 'm2     ', 'm2     ', 'm2/s2  ', 'm2/s2  ', &
+             'm2/s2  ', 'm2/s2  ', 'm2/s2  ', 'm2/s2  ', 'number2' /)
+
+    INTEGER(iwp) ::  dots_num  = 31  !< number of timeseries defined by default
+    INTEGER(iwp) ::  dots_soil = 24  !< starting index for soil-timeseries
+    INTEGER(iwp) ::  dots_rad  = 32  !< starting index for radiation-timeseries
+
+    CHARACTER (LEN=13), DIMENSION(dots_max) :: dots_label =                    &
+          (/ 'E            ', 'E*           ', 'dt           ',                &
+             'u*           ', 'th*          ', 'umax         ',                &
+             'vmax         ', 'wmax         ', 'div_new      ',                &
+             'div_old      ', 'z_i_wpt      ', 'z_i_pt       ',                &
+             'w*           ', 'w"pt"0       ', 'w"pt"        ',                &
+             'wpt          ', 'pt(0)        ', 'pt(z_mo)     ',                &
+             'w"u"0        ', 'w"v"0        ', 'w"q"0        ',                &
+             'ol           ', 'q*           ', 'ghf_eb       ',                &
+             'shf_eb       ', 'qsws_eb      ', 'qsws_liq_eb  ',                &
+             'qsws_soil_eb ', 'qsws_veg_eb  ', 'r_a          ',                &
+             'r_s          ', 'rad_net      ', 'rad_lw_in    ',                &
+             'rad_lw_out   ', 'rad_sw_in    ', 'rad_sw_out   ',                &
+             'rrtm_aldif   ', 'rrtm_aldir   ', 'rrtm_asdif   ',                &
+             'rrtm_asdir   ',                                                  &
+             ( 'unknown      ', i9 = 1, dots_max-40 ) /)
+
+    CHARACTER (LEN=13), DIMENSION(dots_max) :: dots_unit =                     &
+          (/ 'm2/s2        ', 'm2/s2        ', 's            ',                &
+             'm/s          ', 'K            ', 'm/s          ',                &
+             'm/s          ', 'm/s          ', 's-1          ',                &
+             's-1          ', 'm            ', 'm            ',                &
+             'm/s          ', 'K m/s        ', 'K m/s        ',                &
+             'K m/s        ', 'K            ', 'K            ',                &
+             'm2/s2        ', 'm2/s2        ', 'kg m/s       ',                &
+             'm            ', 'kg/kg        ', '             ',                &
+             '             ', '             ', '             ',                &
+             '             ', 'W/m2         ', 's/m          ',                &
+             '             ', 'W/m2         ', 'W/m2         ',                &
+             'W/m2         ', 'W/m2         ', 'W/m2         ',                &
+             '             ', '             ', '             ',                &
+             '             ',                                                  &
+             ( 'unknown      ', i9 = 1, dots_max-40 ) /)
+
+    CHARACTER (LEN=9), DIMENSION(300) ::  dopr_unit = 'unknown'
+
+    CHARACTER (LEN=7), DIMENSION(0:1,100) ::  do2d_unit, do3d_unit
+
+    CHARACTER (LEN=16), DIMENSION(25) ::  prt_var_names = &
+          (/ 'pt_age          ', 'pt_dvrp_size    ', 'pt_origin_x     ', &
+             'pt_origin_y     ', 'pt_origin_z     ', 'pt_radius       ', &
+             'pt_speed_x      ', 'pt_speed_y      ', 'pt_speed_z      ', &
+             'pt_weight_factor', 'pt_x            ', 'pt_y            ', &
+             'pt_z            ', 'pt_color        ', 'pt_group        ', &
+             'pt_tailpoints   ', 'pt_tail_id      ', 'pt_density_ratio', &
+             'pt_exp_arg      ', 'pt_exp_term     ', 'not_used        ', &
+             'not_used        ', 'not_used        ', 'not_used        ', &
+             'not_used        ' /)
+
+    CHARACTER (LEN=16), DIMENSION(25) ::  prt_var_units = &
+          (/ 'seconds         ', 'meters          ', 'meters          ', &
+             'meters          ', 'meters          ', 'meters          ', &
+             'm/s             ', 'm/s             ', 'm/s             ', &
+             'factor          ', 'meters          ', 'meters          ', &
+             'meters          ', 'none            ', 'none            ', &
+             'none            ', 'none            ', 'ratio           ', &
+             'none            ', 'none            ', 'not_used        ', &
+             'not_used        ', 'not_used        ', 'not_used        ', &
+             'not_used        ' /)
+
+    CHARACTER(LEN=20), DIMENSION(11) ::  netcdf_precision = ' '
+    CHARACTER(LEN=40) ::  netcdf_data_format_string
+
+    INTEGER(iwp) ::  id_dim_prtnum, 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_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, &
+                    id_dim_time_yz, id_dim_time_3d, id_dim_x_xy, id_dim_xu_xy, &
+                    id_dim_x_xz, id_dim_xu_xz, id_dim_x_yz, id_dim_xu_yz, &
+                    id_dim_x_3d, id_dim_xu_3d, id_dim_y_xy, id_dim_yv_xy, &
+                    id_dim_y_xz, id_dim_yv_xz, id_dim_y_yz, id_dim_yv_yz, &
+                    id_dim_y_3d, id_dim_yv_3d, id_dim_zs_xy, id_dim_zs_xz, &
+                    id_dim_zs_yz, id_dim_zs_3d, id_dim_zu_xy, id_dim_zu1_xy, &
+                    id_dim_zu_xz, id_dim_zu_yz, id_dim_zu_3d, id_dim_zw_xy, &
+                    id_dim_zw_xz, id_dim_zw_yz, id_dim_zw_3d, id_set_xy, &
+                    id_set_xz, id_set_yz, id_set_3d, id_var_ind_x_yz, &
+                    id_var_ind_y_xz, id_var_ind_z_xy, id_var_time_xy, &
+                    id_var_time_xz, id_var_time_yz, id_var_time_3d, id_var_x_xy, &
+                    id_var_xu_xy, id_var_x_xz, id_var_xu_xz, id_var_x_yz, &
+                    id_var_xu_yz, id_var_x_3d, id_var_xu_3d, id_var_y_xy, &
+                    id_var_yv_xy, id_var_y_xz, id_var_yv_xz, id_var_y_yz, &
+                    id_var_yv_yz, id_var_y_3d, id_var_yv_3d, id_var_zs_xy, &
+                    id_var_zs_xz, id_var_zs_yz, id_var_zs_3d, id_var_zusi_xy, &
+                    id_var_zusi_3d, id_var_zu_xy, id_var_zu1_xy, id_var_zu_xz, &
+                    id_var_zu_yz, id_var_zu_3d, id_var_zwwi_xy, id_var_zwwi_3d, &
+                    id_var_zw_xy, id_var_zw_xz, id_var_zw_yz, id_var_zw_3d
+
+    INTEGER ::  netcdf_data_format = 2  !< NetCDF3 64bit offset format
+    INTEGER ::  netcdf_deflate = 0      !< NetCDF compression, default: no
+                                        !< compression
+
+    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(dopts_num,0:10) ::  id_var_dopts
+    INTEGER(iwp), DIMENSION(0:1,100)        ::  id_var_do2d, id_var_do3d
+    INTEGER(iwp), DIMENSION(100,0:9)        ::  id_dim_z_pr, id_var_dopr, &
+                                                id_var_z_pr
+    INTEGER(iwp), DIMENSION(dots_max,0:9)   ::  id_var_dots
+
+!
+!-- Masked output
+    CHARACTER (LEN=7), DIMENSION(max_masks,0:1,100) ::  domask_unit
+
+    LOGICAL ::  output_for_t0 = .FALSE.
+
+    INTEGER(iwp), DIMENSION(1:max_masks,0:1) ::  id_dim_time_mask, id_dim_x_mask, &
+                   id_dim_xu_mask, id_dim_y_mask, id_dim_yv_mask, id_dim_zs_mask, &
+                   id_dim_zu_mask, id_dim_zw_mask, &
+                   id_set_mask, &
+                   id_var_time_mask, id_var_x_mask, id_var_xu_mask, &
+                   id_var_y_mask, id_var_yv_mask, id_var_zs_mask, &
+                   id_var_zu_mask, id_var_zw_mask, &
+                   id_var_zusi_mask, id_var_zwwi_mask
+
+    INTEGER(iwp), DIMENSION(1:max_masks,0:1,100) ::  id_var_domask
+
+
+    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_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
+
+    SAVE
+
+    INTERFACE netcdf_create_dim
+       MODULE PROCEDURE netcdf_create_dim
+    END INTERFACE netcdf_create_dim
+
+    INTERFACE netcdf_create_file
+       MODULE PROCEDURE netcdf_create_file
+    END INTERFACE netcdf_create_file
+
+    INTERFACE netcdf_create_var
+       MODULE PROCEDURE netcdf_create_var
+    END INTERFACE netcdf_create_var
+
+    INTERFACE netcdf_define_header
+       MODULE PROCEDURE netcdf_define_header
+    END INTERFACE netcdf_define_header
+
+    INTERFACE netcdf_handle_error
+       MODULE PROCEDURE netcdf_handle_error
+    END INTERFACE netcdf_handle_error
+
+    INTERFACE netcdf_open_write_file
+       MODULE PROCEDURE netcdf_open_write_file
+    END INTERFACE netcdf_open_write_file
+
+    PUBLIC netcdf_create_file, netcdf_define_header, netcdf_handle_error,      &
+           netcdf_open_write_file
+
+ CONTAINS
+
+ SUBROUTINE netcdf_define_header( callmode, extend, av )
+ 
+#if defined( __netcdf )
+
+    USE arrays_3d,                                                             &
+        ONLY:  zu, zw
+
+    USE constants,                                                             &
+        ONLY:  pi
+
+    USE control_parameters,                                                    &
+        ONLY:  averaging_interval, averaging_interval_pr,                      &
+               averaging_interval_sp, data_output_pr,  domask,  dopr_n,        &
+               dopr_time_count, dopts_time_count, dots_time_count,             &
+               dosp_time_count, do2d, do2d_xz_time_count, do3d,                &
+               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,             &
+               do3d_time_count, domask_time_count, end_time, mask_i_global,    &
+               mask_j_global, mask_k_global, message_string, mid, ntdim_2d_xy, &
+               ntdim_2d_xz, ntdim_2d_yz, ntdim_3d, nz_do3d, prt_time_count,    &
+               run_description_header, section, simulated_time,                &
+               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
+
+    USE grid_variables,                                                        &
+        ONLY:  dx, dy, zu_s_inner, zw_w_inner
+
+    USE indices,                                                               &
+        ONLY:  nx, ny, nz ,nzb, nzt
+
+    USE kinds
+
+    USE land_surface_model_mod,                                                &
+        ONLY: land_surface, nzb_soil, nzt_soil, nzs, zs
+
+    USE pegrid
+
+    USE particle_attributes,                                                   &
+        ONLY:  maximum_number_of_particles, number_of_particle_groups
+
+    USE profil_parameter,                                                      &
+        ONLY:  crmax, cross_profiles, dopr_index, profile_columns, profile_rows
+
+    USE spectrum,                                                              &
+        ONLY:  comp_spectra_level, data_output_sp, spectra_direction
+
+    USE statistics,                                                            &
+        ONLY:  hom, statistic_regions
+
+
+    IMPLICIT NONE
+
+    CHARACTER (LEN=2)              ::  suffix                !<
+    CHARACTER (LEN=2), INTENT (IN) ::  callmode              !<
+    CHARACTER (LEN=3)              ::  suffix1               !<
+    CHARACTER (LEN=4)              ::  grid_x                !<
+    CHARACTER (LEN=4)              ::  grid_y                !<
+    CHARACTER (LEN=4)              ::  grid_z                !<
+    CHARACTER (LEN=6)              ::  mode                  !<
+    CHARACTER (LEN=10)             ::  netcdf_var_name       !<
+    CHARACTER (LEN=10)             ::  precision             !<
+    CHARACTER (LEN=10)             ::  var                   !<
+    CHARACTER (LEN=80)             ::  time_average_text     !<
+    CHARACTER (LEN=2000)           ::  char_cross_profiles   !<
+    CHARACTER (LEN=2000)           ::  var_list              !<
+    CHARACTER (LEN=2000)           ::  var_list_old          !<
+
+    CHARACTER (LEN=100), DIMENSION(1:crmax) ::  cross_profiles_adj   !<
+    CHARACTER (LEN=100), DIMENSION(1:crmax) ::  cross_profiles_char  !<
+
+    INTEGER(iwp) ::  av                                      !<
+    INTEGER(iwp) ::  cross_profiles_count                    !<
+    INTEGER(iwp) ::  cross_profiles_maxi                     !<
+    INTEGER(iwp) ::  delim                                   !<
+    INTEGER(iwp) ::  delim_old                               !<
+    INTEGER(iwp) ::  file_id                                 !<
+    INTEGER(iwp) ::  i                                       !<
+    INTEGER(iwp) ::  id_last                                 !<
+    INTEGER(iwp) ::  id_x                                    !<
+    INTEGER(iwp) ::  id_y                                    !<
+    INTEGER(iwp) ::  id_z                                    !<
+    INTEGER(iwp) ::  j                                       !<
+    INTEGER(iwp) ::  k                                       !<
+    INTEGER(iwp) ::  kk                                      !<
+    INTEGER(iwp) ::  ns                                      !<
+    INTEGER(iwp) ::  ns_do                                   !< actual value of ns for soil model data
+    INTEGER(iwp) ::  ns_old                                  !<
+    INTEGER(iwp) ::  ntime_count                             !< number of time levels found in file
+    INTEGER(iwp) ::  nz_old                                  !<
+
+    INTEGER(iwp), SAVE ::  oldmode                           !<
+
+    INTEGER(iwp), DIMENSION(1) ::  id_dim_time_old           !<
+    INTEGER(iwp), DIMENSION(1) ::  id_dim_x_yz_old           !<
+    INTEGER(iwp), DIMENSION(1) ::  id_dim_y_xz_old           !<
+    INTEGER(iwp), DIMENSION(1) ::  id_dim_zu_sp_old          !<
+    INTEGER(iwp), DIMENSION(1) ::  id_dim_zu_xy_old          !<
+    INTEGER(iwp), DIMENSION(1) ::  id_dim_zu_3d_old          !<
+    INTEGER(iwp), DIMENSION(1) ::  id_dim_zu_mask_old        !<
+
+
+    INTEGER(iwp), DIMENSION(1:crmax) ::  cross_profiles_numb !<
+
+    LOGICAL ::  found                                        !<
+
+    LOGICAL, INTENT (INOUT) ::  extend                       !<
+
+    LOGICAL, SAVE ::  init_netcdf = .FALSE.                  !<
+
+    REAL(wp), DIMENSION(1) ::  last_time_coordinate          !< last time value in file
+
+    REAL(wp), DIMENSION(:), ALLOCATABLE   ::  netcdf_data    !<
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  netcdf_data_2d !<
+
+!
+!-- Initializing actions
+    IF ( .NOT. init_netcdf )  THEN
+!
+!--    Check and set accuracy for netCDF output. First set default value
+       nc_precision = NF90_REAL4
+
+       i = 1
+       DO  WHILE ( netcdf_precision(i) /= ' ' )
+          j = INDEX( netcdf_precision(i), '_' )
+          IF ( j == 0 )  THEN
+             WRITE ( message_string, * ) 'netcdf_precision must contain a ', &
+                                         '"_"netcdf_precision(', i, ')="',   &
+                                         TRIM( netcdf_precision(i) ),'"'
+             CALL message( 'netcdf_define_header', 'PA0241', 2, 2, 0, 6, 0 )
+          ENDIF
+
+          var       = netcdf_precision(i)(1:j-1)
+          precision = netcdf_precision(i)(j+1:)
+
+          IF ( precision == 'NF90_REAL4' )  THEN
+             j = NF90_REAL4
+          ELSEIF ( precision == 'NF90_REAL8' )  THEN
+             j = NF90_REAL8
+          ELSE
+             WRITE ( message_string, * ) 'illegal netcdf precision: ',  &
+                                         'netcdf_precision(', i, ')="', &
+                                         TRIM( netcdf_precision(i) ),'"'
+             CALL message( 'netcdf_define_header', 'PA0242', 1, 2, 0, 6, 0 )
+          ENDIF
+
+          SELECT CASE ( var )
+             CASE ( 'xy' )
+                nc_precision(1) = j
+             CASE ( 'xz' )
+                nc_precision(2) = j
+             CASE ( 'yz' )
+                nc_precision(3) = j
+             CASE ( '2d' )
+                nc_precision(1:3) = j
+             CASE ( '3d' )
+                nc_precision(4) = j
+             CASE ( 'pr' )
+                nc_precision(5) = j
+             CASE ( 'ts' )
+                nc_precision(6) = j
+             CASE ( 'sp' )
+                nc_precision(7) = j
+             CASE ( 'prt' )
+                nc_precision(8) = j
+             CASE ( 'masks' )
+                nc_precision(11) = j
+             CASE ( 'all' )
+                nc_precision    = j
+
+             CASE DEFAULT
+                WRITE ( message_string, * ) 'unknown variable in inipar ',    & 
+                                  'assignment: netcdf_precision(', i, ')="',  &
+                                            TRIM( netcdf_precision(i) ),'"'
+                CALL message( 'netcdf_define_header', 'PA0243', 1, 2, 0, 6, 0 )
+
+          END SELECT
+
+          i = i + 1
+          IF ( i > 50 )  EXIT
+       ENDDO
+
+!
+!--    Check for allowed parameter range
+       IF ( netcdf_deflate < 0  .OR.  netcdf_deflate > 9 )  THEN
+          WRITE ( message_string, '(A,I3,A)' ) 'netcdf_deflate out of ' //     &
+                                      'range & given value: ', netcdf_deflate, &
+                                      ', allowed range: 0-9'
+          CALL message( 'netcdf_define_header', 'PA0355', 2, 2, 0, 6, 0 )
+       ENDIF
+!
+!--    Data compression does not work with parallel NetCDF/HDF5
+       IF ( netcdf_deflate > 0  .AND.  netcdf_data_format /= 3 )  THEN
+          message_string = 'netcdf_deflate reset to 0'
+          CALL message( 'netcdf_define_header', 'PA0356', 0, 1, 0, 6, 0 )
+
+          netcdf_deflate = 0
+       ENDIF
+
+       init_netcdf = .TRUE.
+
+    ENDIF
+
+!
+!-- Determine the mode to be processed
+    IF ( extend )  THEN
+       mode = callmode // '_ext'
+    ELSE
+       mode = callmode // '_new'
+    ENDIF
+
+!
+!-- Select the mode to be processed. Possibilities are 3d, mask, xy, xz, yz, 
+!-- pr and ts.
+    SELECT CASE ( mode )
+
+       CASE ( 'ma_new' )
+
+!
+!--       decompose actual parameter file_id (=formal parameter av) into 
+!--       mid and av
+          file_id = av
+          IF ( file_id <= 200+max_masks )  THEN
+             mid = file_id - 200
+             av = 0
+          ELSE
+             mid = file_id - (200+max_masks)
+             av = 1
+          ENDIF
+
+!
+!--       Define some global attributes of the dataset
+          nc_stat = NF90_PUT_ATT( id_set_mask(mid,av), NF90_GLOBAL, &
+                                  'Conventions', 'COARDS' )
+          CALL netcdf_handle_error( 'netcdf_define_header', 464 )
+
+          IF ( av == 0 )  THEN
+             time_average_text = ' '
+          ELSE
+             WRITE (time_average_text, '('', '',F7.1,'' s average'')') &
+                                                            averaging_interval
+          ENDIF
+          nc_stat = NF90_PUT_ATT( id_set_mask(mid,av), NF90_GLOBAL, 'title', &
+                                  TRIM( run_description_header ) //    &
+                                  TRIM( time_average_text ) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 465 )
+          IF ( av == 1 )  THEN
+             WRITE ( time_average_text,'(F7.1,'' s avg'')' )  averaging_interval
+             nc_stat = NF90_PUT_ATT( id_set_mask(mid,av), NF90_GLOBAL, &
+                                     'time_avg', TRIM( time_average_text ) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 466 )
+          ENDIF
+
+!
+!--       Define time coordinate for volume data (unlimited dimension)
+          CALL netcdf_create_dim( id_set_mask(mid,av), 'time', NF90_UNLIMITED, &
+                                  id_dim_time_mask(mid,av), 467 )
+          CALL netcdf_create_var( id_set_mask(mid,av),                         &
+                                  (/ id_dim_time_mask(mid,av) /), 'time',      &
+                                  NF90_DOUBLE, id_var_time_mask(mid,av),       &
+                                 'seconds', '', 468, 469, 000 )
+!
+!--       Define spatial dimensions and coordinates:
+!--       Define vertical coordinate grid (zu grid)
+          CALL netcdf_create_dim( id_set_mask(mid,av), 'zu_3d',                &
+                                  mask_size(mid,3), id_dim_zu_mask(mid,av),    &
+                                  470 )
+          CALL netcdf_create_var( id_set_mask(mid,av),                         &
+                                  (/ id_dim_zu_mask(mid,av) /), 'zu_3d',       &
+                                  NF90_DOUBLE, id_var_zu_mask(mid,av),         &
+                                  'meters', '', 471, 472, 000 )
+!
+!--       Define vertical coordinate grid (zw grid)
+          CALL netcdf_create_dim( id_set_mask(mid,av), 'zw_3d',                &
+                                  mask_size(mid,3), id_dim_zw_mask(mid,av),    &
+                                  473 )
+          CALL netcdf_create_var( id_set_mask(mid,av),                         &
+                                  (/ id_dim_zw_mask(mid,av) /), 'zw_3d',       &
+                                  NF90_DOUBLE, id_var_zw_mask(mid,av),         &
+                                 'meters', '', 474, 475, 000 )
+!
+!--       Define x-axis (for scalar position)
+          CALL netcdf_create_dim( id_set_mask(mid,av), 'x', mask_size(mid,1),  &
+                                  id_dim_x_mask(mid,av), 476 )
+          CALL netcdf_create_var( id_set_mask(mid,av),                         &
+                                  (/ id_dim_x_mask(mid,av) /), 'x',            &
+                                  NF90_DOUBLE, id_var_x_mask(mid,av),          &
+                                  'meters', '', 477, 478, 000 )
+!
+!--       Define x-axis (for u position)
+          CALL netcdf_create_dim( id_set_mask(mid,av), 'xu', mask_size(mid,1), &
+                                  id_dim_xu_mask(mid,av), 479 )
+          CALL netcdf_create_var( id_set_mask(mid,av),                         &
+                                  (/ id_dim_xu_mask(mid,av) /), 'xu',          &
+                                  NF90_DOUBLE, id_var_xu_mask(mid,av),         &
+                                  'meters', '', 480, 481, 000 )
+!
+!--       Define y-axis (for scalar position)
+          CALL netcdf_create_dim( id_set_mask(mid,av), 'y', mask_size(mid,2),  &
+                                  id_dim_y_mask(mid,av), 482 )
+          CALL netcdf_create_var( id_set_mask(mid,av),                         &
+                                  (/ id_dim_y_mask(mid,av) /), 'y',            &
+                                  NF90_DOUBLE, id_var_y_mask(mid,av),          &
+                                  'meters', '', 483, 484, 000 )
+!
+!--       Define y-axis (for v position)
+          CALL netcdf_create_dim( id_set_mask(mid,av), 'yv', mask_size(mid,2), &
+                                  id_dim_yv_mask(mid,av), 485 )
+          CALL netcdf_create_var( id_set_mask(mid,av),                         &
+                                  (/ id_dim_yv_mask(mid,av) /),                &
+                                  'yv', NF90_DOUBLE, id_var_yv_mask(mid,av),   &
+                                  'meters', '', 486, 487, 000 )
+!
+!--       In case of non-flat topography define 2d-arrays containing the height
+!--       information
+          IF ( TRIM( topography ) /= 'flat' )  THEN
+!
+!--          Define zusi = zu(nzb_s_inner)
+             CALL netcdf_create_var( id_set_mask(mid,av),                      &
+                                     (/ id_dim_x_mask(mid,av),                 &
+                                        id_dim_y_mask(mid,av) /), 'zusi',      &
+                                     NF90_DOUBLE, id_var_zusi_mask(mid,av),    &
+                                     'meters', 'zu(nzb_s_inner)', 488, 489,    &
+                                     490 )
+!             
+!--          Define zwwi = zw(nzb_w_inner)
+             CALL netcdf_create_var( id_set_mask(mid,av),                      &
+                                     (/ id_dim_x_mask(mid,av),                 &
+                                        id_dim_y_mask(mid,av) /), 'zwwi',      &
+                                     NF90_DOUBLE, id_var_zwwi_mask(mid,av),    &
+                                     'meters', 'zw(nzb_w_inner)', 491, 492,    &
+                                     493 )
+          ENDIF             
+ 
+          IF ( land_surface )  THEN
+!
+!--          Define vertical coordinate grid (zw grid)
+             CALL netcdf_create_dim( id_set_mask(mid,av), 'zs_3d',             &
+                                     mask_size(mid,3), id_dim_zs_mask(mid,av), &
+                                     536 )
+             CALL netcdf_create_var( id_set_mask(mid,av),                      &
+                                     (/ id_dim_zs_mask(mid,av) /), 'zs_3d',    &
+                                     NF90_DOUBLE, id_var_zs_mask(mid,av),      &
+                                     'meters', '', 537, 555, 000 )
+          ENDIF
+
+!
+!--       Define the variables
+          var_list = ';'
+          i = 1
+
+          DO WHILE ( domask(mid,av,i)(1:1) /= ' ' )
+
+!
+!--          Check for the grid
+             found = .TRUE.
+             SELECT CASE ( domask(mid,av,i) )
+!
+!--             Most variables are defined on the scalar grid
+                CASE ( 'e', 'lpt', 'nr', 'p', 'pc', 'pr', 'prr', 'pt', 'q',    &
+                       'qc', 'ql', 'ql_c', 'ql_v', 'ql_vp', 'qr', 'qv',        &
+                       'rad_lw_cs_hr', 'rad_lw_hr', 'rad_sw_cs_hr',            &
+                       'rad_sw_hr', 'rho', 's', 'sa', 'vpt'  )
+
+                   grid_x = 'x'
+                   grid_y = 'y'
+                   grid_z = 'zu'
+!
+!--             u grid
+                CASE ( 'u' )
+
+                   grid_x = 'xu'
+                   grid_y = 'y'
+                   grid_z = 'zu'
+!
+!--             v grid
+                CASE ( 'v' )
+
+                   grid_x = 'x'
+                   grid_y = 'yv'
+                   grid_z = 'zu'
+!
+!--             w grid
+                CASE ( 'rad_lw_in', 'rad_lw_out', 'rad_sw_in', 'rad_sw_out',   &
+                       'w' )
+
+                   grid_x = 'x'
+                   grid_y = 'y'
+                   grid_z = 'zw'
+!
+!--             soil grid
+                CASE ( 'm_soil', 't_soil' )
+
+                   grid_x = 'x'
+                   grid_y = 'y'
+                   grid_z = 'zs'
+
+                CASE DEFAULT
+!
+!--                Check for user-defined quantities
+                   CALL user_define_netcdf_grid( domask(mid,av,i), found,      &
+                                                 grid_x, grid_y, grid_z )
+
+                   IF ( .NOT. found )  THEN
+                      WRITE ( message_string, * ) 'no grid defined for',       &
+                           ' variable ', TRIM( domask(mid,av,i) )
+                      CALL message( 'define_netcdf_header', 'PA0244', 0, 1, 0, &
+                                    6, 0 )
+                   ENDIF
+
+             END SELECT
+
+!
+!--          Select the respective dimension ids
+             IF ( grid_x == 'x' )  THEN
+                id_x = id_dim_x_mask(mid,av)
+             ELSEIF ( grid_x == 'xu' )  THEN
+                id_x = id_dim_xu_mask(mid,av)
+             ENDIF
+
+             IF ( grid_y == 'y' )  THEN
+                id_y = id_dim_y_mask(mid,av)
+             ELSEIF ( grid_y == 'yv' )  THEN
+                id_y = id_dim_yv_mask(mid,av)
+             ENDIF
+
+             IF ( grid_z == 'zu' )  THEN
+                id_z = id_dim_zu_mask(mid,av)
+             ELSEIF ( grid_z == 'zw' )  THEN
+                id_z = id_dim_zw_mask(mid,av)
+             ELSEIF ( grid_z == "zs" )  THEN
+                id_z = id_dim_zs_mask(mid,av)
+             ENDIF
+
+!
+!--          Define the grid
+             CALL netcdf_create_var( id_set_mask(mid,av), (/ id_x, id_y, id_z, &
+                                     id_dim_time_mask(mid,av) /),              &
+                                     domask(mid,av,i), nc_precision(11),       &
+                                     id_var_domask(mid,av,i),                  &
+                                     TRIM( domask_unit(mid,av,i) ),            &
+                                     domask(mid,av,i), 494, 495, 496 )
+
+             var_list = TRIM( var_list ) // TRIM( domask(mid,av,i) ) // ';'
+
+             i = i + 1
+
+          ENDDO
+
+!
+!--       No arrays to output
+          IF ( i == 1 )  RETURN
+
+!
+!--       Write the list of variables as global attribute (this is used by
+!--       restart runs and by combine_plot_fields)
+          nc_stat = NF90_PUT_ATT( id_set_mask(mid,av), NF90_GLOBAL, &
+                                  'VAR_LIST', var_list )
+          CALL netcdf_handle_error( 'netcdf_define_header', 497 )
+
+!
+!--       Leave netCDF define mode
+          nc_stat = NF90_ENDDEF( id_set_mask(mid,av) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 498 )
+
+!
+!--       Write data for x (shifted by +dx/2) and xu axis
+          ALLOCATE( netcdf_data(mask_size(mid,1)) )
+
+          netcdf_data = ( mask_i_global(mid,:mask_size(mid,1)) + 0.5_wp ) * dx
+
+          nc_stat = NF90_PUT_VAR( id_set_mask(mid,av), id_var_x_mask(mid,av), &
+                                  netcdf_data, start = (/ 1 /),               &
+                                  count = (/ mask_size(mid,1) /) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 499 )
+
+          netcdf_data = mask_i_global(mid,:mask_size(mid,1)) * dx
+
+          nc_stat = NF90_PUT_VAR( id_set_mask(mid,av), id_var_xu_mask(mid,av),&
+                                  netcdf_data, start = (/ 1 /),               &
+                                  count = (/ mask_size(mid,1) /) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 500 )
+
+          DEALLOCATE( netcdf_data )
+
+!
+!--       Write data for y (shifted by +dy/2) and yv axis
+          ALLOCATE( netcdf_data(mask_size(mid,2)) )
+
+          netcdf_data = ( mask_j_global(mid,:mask_size(mid,2)) + 0.5_wp ) * dy
+
+          nc_stat = NF90_PUT_VAR( id_set_mask(mid,av), id_var_y_mask(mid,av), &
+                                  netcdf_data, start = (/ 1 /),               &
+                                  count = (/ mask_size(mid,2) /))
+          CALL netcdf_handle_error( 'netcdf_define_header', 501 )
+
+          netcdf_data = mask_j_global(mid,:mask_size(mid,2)) * dy
+
+          nc_stat = NF90_PUT_VAR( id_set_mask(mid,av), id_var_yv_mask(mid,av), &
+                                  netcdf_data, start = (/ 1 /),    &
+                                  count = (/ mask_size(mid,2) /))
+          CALL netcdf_handle_error( 'netcdf_define_header', 502 )
+
+          DEALLOCATE( netcdf_data )
+
+!
+!--       Write zu and zw data (vertical axes)
+          ALLOCATE( netcdf_data(mask_size(mid,3)) )
+
+          netcdf_data = zu( mask_k_global(mid,:mask_size(mid,3)) )
+
+          nc_stat = NF90_PUT_VAR( id_set_mask(mid,av), id_var_zu_mask(mid,av), &
+                                  netcdf_data, start = (/ 1 /), &
+                                  count = (/ mask_size(mid,3) /) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 503 )
+
+          netcdf_data = zw( mask_k_global(mid,:mask_size(mid,3)) )
+
+          nc_stat = NF90_PUT_VAR( id_set_mask(mid,av), id_var_zw_mask(mid,av), &
+                                  netcdf_data, start = (/ 1 /), &
+                                  count = (/ mask_size(mid,3) /) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 504 )
+
+          DEALLOCATE( netcdf_data )
+
+!
+!--       In case of non-flat topography write height information
+          IF ( TRIM( topography ) /= 'flat' )  THEN
+
+             ALLOCATE( netcdf_data_2d(mask_size(mid,1),mask_size(mid,2)) )
+             netcdf_data_2d = zu_s_inner( mask_i_global(mid,:mask_size(mid,1)),&
+                                          mask_j_global(mid,:mask_size(mid,2)) )
+
+             nc_stat = NF90_PUT_VAR( id_set_mask(mid,av),         &
+                                     id_var_zusi_mask(mid,av),    &
+                                     netcdf_data_2d,              &
+                                     start = (/ 1, 1 /),          &
+                                     count = (/ mask_size(mid,1), &
+                                                mask_size(mid,2) /) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 505 )
+
+             netcdf_data_2d = zw_w_inner( mask_i_global(mid,:mask_size(mid,1)),&
+                                          mask_j_global(mid,:mask_size(mid,2)) )
+
+             nc_stat = NF90_PUT_VAR( id_set_mask(mid,av),         &
+                                     id_var_zwwi_mask(mid,av),    &
+                                     netcdf_data_2d,              &
+                                     start = (/ 1, 1 /),          &
+                                     count = (/ mask_size(mid,1), &
+                                                mask_size(mid,2) /) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 506 )
+
+             DEALLOCATE( netcdf_data_2d )
+
+          ENDIF
+
+          IF ( land_surface )  THEN
+!
+!--          Write zs data (vertical axes for soil model), use negative values
+!--          to indicate soil depth
+             ALLOCATE( netcdf_data(mask_size(mid,3)) )
+
+             netcdf_data = zs( mask_k_global(mid,:mask_size(mid,3)) )
+
+             nc_stat = NF90_PUT_VAR( id_set_mask(mid,av), id_var_zs_mask(mid,av), &
+                                     netcdf_data, start = (/ 1 /), &
+                                     count = (/ mask_size(mid,3) /) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 538 )
+
+             DEALLOCATE( netcdf_data )
+
+          ENDIF
+
+!
+!--       restore original parameter file_id (=formal parameter av) into av
+          av = file_id
+
+
+       CASE ( 'ma_ext' )
+
+!
+!--       decompose actual parameter file_id (=formal parameter av) into 
+!--       mid and av
+          file_id = av
+          IF ( file_id <= 200+max_masks )  THEN
+             mid = file_id - 200
+             av = 0
+          ELSE
+             mid = file_id - (200+max_masks)
+             av = 1
+          ENDIF
+
+!
+!--       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_mask(mid,av), NF90_GLOBAL, 'VAR_LIST',&
+                                  var_list_old )
+          CALL netcdf_handle_error( 'netcdf_define_header', 507 )
+
+          var_list = ';'
+          i = 1
+          DO WHILE ( domask(mid,av,i)(1:1) /= ' ' )
+             var_list = TRIM(var_list) // TRIM( domask(mid,av,i) ) // ';'
+             i = i + 1
+          ENDDO
+
+          IF ( av == 0 )  THEN
+             var = '(mask)'
+          ELSE
+             var = '(mask_av)'
+          ENDIF
+
+          IF ( TRIM( var_list ) /= TRIM( var_list_old ) )  THEN
+             WRITE ( message_string, * ) 'netCDF file for ', TRIM( var ), &
+                  ' data for mask', mid, ' from previous run found,', &
+                  '&but this file cannot be extended due to variable ', &
+                  'mismatch.&New file is created instead.'
+             CALL message( 'define_netcdf_header', 'PA0335', 0, 1, 0, 6, 0 )
+             extend = .FALSE.
+             RETURN
+          ENDIF
+
+!
+!--       Get and compare the number of vertical gridpoints
+          nc_stat = NF90_INQ_VARID( id_set_mask(mid,av), 'zu_3d', &
+                                    id_var_zu_mask(mid,av) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 508 )
+
+          nc_stat = NF90_INQUIRE_VARIABLE( id_set_mask(mid,av),     &
+                                           id_var_zu_mask(mid,av),  &
+                                           dimids = id_dim_zu_mask_old )
+          CALL netcdf_handle_error( 'netcdf_define_header', 509 )
+          id_dim_zu_mask(mid,av) = id_dim_zu_mask_old(1)
+
+          nc_stat = NF90_INQUIRE_DIMENSION( id_set_mask(mid,av),    &
+                                            id_dim_zu_mask(mid,av), &
+                                            len = nz_old )
+          CALL netcdf_handle_error( 'netcdf_define_header', 510 )
+
+          IF ( mask_size(mid,3) /= nz_old )  THEN
+             WRITE ( message_string, * ) 'netCDF file for ', TRIM( var ), &
+                  ' data for mask', mid, ' from previous run found,', &
+                  '&but this file cannot be extended due to mismatch in ', &
+                  ' number of&vertical grid points.', &
+                  '&New file is created instead.'
+             CALL message( 'define_netcdf_header', 'PA0336', 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 plmask..count+1.
+!--       The current time must be larger than the last output time
+!--       on the file.
+          nc_stat = NF90_INQ_VARID( id_set_mask(mid,av), 'time', &
+                                    id_var_time_mask(mid,av) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 511 )
+
+          nc_stat = NF90_INQUIRE_VARIABLE( id_set_mask(mid,av), &
+                                           id_var_time_mask(mid,av), &
+                                           dimids = id_dim_time_old )
+          CALL netcdf_handle_error( 'netcdf_define_header', 512 )
+          id_dim_time_mask(mid,av) = id_dim_time_old(1)
+
+          nc_stat = NF90_INQUIRE_DIMENSION( id_set_mask(mid,av), &
+                                            id_dim_time_mask(mid,av), &
+                                            len = domask_time_count(mid,av) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 513 )
+
+          nc_stat = NF90_GET_VAR( id_set_mask(mid,av), &
+                                  id_var_time_mask(mid,av), &
+                                  last_time_coordinate,              &
+                                  start = (/ domask_time_count(mid,av) /), &
+                                  count = (/ 1 /) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 514 )
+
+          IF ( last_time_coordinate(1) >= simulated_time )  THEN
+             WRITE ( message_string, * ) 'netCDF file for ', TRIM( var ), &
+                  ' data for mask', mid, ' from previous run found,', &
+                  '&but this file cannot be extended because the current ', &
+                  'output time&is less or equal than the last output time ', &
+                  'on this file.&New file is created instead.'
+             CALL message( 'define_netcdf_header', 'PA0337', 0, 1, 0, 6, 0 )
+             domask_time_count(mid,av) = 0
+             extend = .FALSE.
+             RETURN
+          ENDIF
+
+!
+!--       Dataset seems to be extendable.
+!--       Now get the variable ids.
+          i = 1
+          DO WHILE ( domask(mid,av,i)(1:1) /= ' ' )
+             nc_stat = NF90_INQ_VARID( id_set_mask(mid,av), &
+                                       TRIM( domask(mid,av,i) ), &
+                                       id_var_domask(mid,av,i) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 515 )
+             i = i + 1
+          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.
+          IF ( av == 0 )  THEN
+             time_average_text = ' '
+          ELSE
+             WRITE (time_average_text, '('', '',F7.1,'' s average'')') &
+                                                            averaging_interval
+          ENDIF
+          nc_stat = NF90_REDEF( id_set_mask(mid,av) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 516 )
+          nc_stat = NF90_PUT_ATT( id_set_mask(mid,av), NF90_GLOBAL, 'title', &
+                                  TRIM( run_description_header ) //    &
+                                  TRIM( time_average_text ) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 517 )
+          nc_stat = NF90_ENDDEF( id_set_mask(mid,av) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 518 )
+          WRITE ( message_string, * ) 'netCDF file for ', TRIM( var ), &
+               ' data for mask', mid, ' from previous run found.', &
+               '&This file will be extended.'
+          CALL message( 'define_netcdf_header', 'PA0338', 0, 0, 0, 6, 0 )
+!
+!--       restore original parameter file_id (=formal parameter av) into av
+          av = file_id
+
+
+       CASE ( '3d_new' )
+
+!
+!--       Define some global attributes of the dataset
+          nc_stat = NF90_PUT_ATT( id_set_3d(av), NF90_GLOBAL, 'Conventions', &
+                                  'COARDS' )
+          CALL netcdf_handle_error( 'netcdf_define_header', 62 )
+
+          IF ( av == 0 )  THEN
+             time_average_text = ' '
+          ELSE
+             WRITE (time_average_text, '('', '',F7.1,'' s average'')') &
+                                                            averaging_interval
+          ENDIF
+          nc_stat = NF90_PUT_ATT( id_set_3d(av), NF90_GLOBAL, 'title', &
+                                  TRIM( run_description_header ) //    &
+                                  TRIM( time_average_text ) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 63 )
+          IF ( av == 1 )  THEN
+             WRITE ( time_average_text,'(F7.1,'' s avg'')' )  averaging_interval
+             nc_stat = NF90_PUT_ATT( id_set_3d(av), NF90_GLOBAL, 'time_avg', &
+                                     TRIM( time_average_text ) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 63 )
+          ENDIF
+
+!
+!--       Define time coordinate for volume data.
+!--       For parallel output the time dimensions has to be limited, otherwise
+!--       the performance drops significantly.
+          IF ( netcdf_data_format < 5 )  THEN
+             CALL netcdf_create_dim( id_set_3d(av), 'time', NF90_UNLIMITED,    &
+                                     id_dim_time_3d(av), 64 )
+          ELSE
+             CALL netcdf_create_dim( id_set_3d(av), 'time', ntdim_3d(av),      &
+                                     id_dim_time_3d(av), 523 )
+          ENDIF
+
+          CALL netcdf_create_var( id_set_3d(av), (/ id_dim_time_3d(av) /),     &
+                                  'time', NF90_DOUBLE, id_var_time_3d(av),     &
+                                  'seconds', '', 65, 66, 00 )
+!
+!--       Define spatial dimensions and coordinates:
+!--       Define vertical coordinate grid (zu grid)
+          CALL netcdf_create_dim( id_set_3d(av), 'zu_3d', nz_do3d-nzb+1,       &
+                                  id_dim_zu_3d(av), 67 )
+          CALL netcdf_create_var( id_set_3d(av), (/ id_dim_zu_3d(av) /),       &
+                                  'zu_3d', NF90_DOUBLE, id_var_zu_3d(av),      &
+                                  'meters', '', 68, 69, 00 )
+!
+!--       Define vertical coordinate grid (zw grid)
+          CALL netcdf_create_dim( id_set_3d(av), 'zw_3d', nz_do3d-nzb+1,       &
+                                  id_dim_zw_3d(av), 70 )
+          CALL netcdf_create_var( id_set_3d(av), (/ id_dim_zw_3d(av) /),       &
+                                  'zw_3d', NF90_DOUBLE, id_var_zw_3d(av),      &
+                                  'meters', '', 71, 72, 00 )
+!
+!--       Define x-axis (for scalar position)
+          CALL netcdf_create_dim( id_set_3d(av), 'x', nx+2, id_dim_x_3d(av),   &
+                                  73 )
+          CALL netcdf_create_var( id_set_3d(av), (/ id_dim_x_3d(av) /), 'x',   &
+                                  NF90_DOUBLE, id_var_x_3d(av), 'meters', '',  &
+                                  74, 75, 00 )
+!
+!--       Define x-axis (for u position)
+          CALL netcdf_create_dim( id_set_3d(av), 'xu', nx+2, id_dim_xu_3d(av), &
+                                  358 )
+          CALL netcdf_create_var( id_set_3d(av), (/ id_dim_xu_3d(av) /), 'xu', &
+                                  NF90_DOUBLE, id_var_xu_3d(av), 'meters', '', &
+                                  359, 360, 000 )
+!
+!--       Define y-axis (for scalar position)
+          CALL netcdf_create_dim( id_set_3d(av), 'y', ny+2, id_dim_y_3d(av),   &
+                                  76 )
+          CALL netcdf_create_var( id_set_3d(av), (/ id_dim_y_3d(av) /), 'y',   &
+                                  NF90_DOUBLE, id_var_y_3d(av), 'meters', '',  &
+                                  77, 78, 00 )
+!
+!--       Define y-axis (for v position)
+          CALL netcdf_create_dim( id_set_3d(av), 'yv', ny+2, id_dim_yv_3d(av), &
+                                  361 )
+          CALL netcdf_create_var( id_set_3d(av), (/ id_dim_yv_3d(av) /), 'yv', &
+                                  NF90_DOUBLE, id_var_yv_3d(av), 'meters', '', &
+                                  362, 363, 000 )
+!
+!--       In case of non-flat topography define 2d-arrays containing the height
+!--       information
+          IF ( TRIM( topography ) /= 'flat' )  THEN
+!
+!--          Define zusi = zu(nzb_s_inner)
+             CALL netcdf_create_var( id_set_3d(av), (/ id_dim_x_3d(av),        &
+                                     id_dim_y_3d(av) /), 'zusi', NF90_DOUBLE,  &
+                                     id_var_zusi_3d(av), 'meters',             &
+                                     'zu(nzb_s_inner)', 413, 414, 415 )
+!             
+!--          Define zwwi = zw(nzb_w_inner)
+             CALL netcdf_create_var( id_set_3d(av), (/ id_dim_x_3d(av),        &
+                                     id_dim_y_3d(av) /), 'zwwi', NF90_DOUBLE,  &
+                                     id_var_zwwi_3d(av), 'meters',             &
+                                     'zw(nzb_w_inner)', 416, 417, 418 )
+
+          ENDIF             
+
+          IF ( land_surface )  THEN
+!
+!--          Define vertical coordinate grid (zs grid)
+             CALL netcdf_create_dim( id_set_3d(av), 'zs_3d',                   &
+                                     nzt_soil-nzb_soil+1, id_dim_zs_3d(av), 70 )
+             CALL netcdf_create_var( id_set_3d(av), (/ id_dim_zs_3d(av) /),    &
+                                     'zs_3d', NF90_DOUBLE, id_var_zs_3d(av),   &
+                                     'meters', '', 71, 72, 00 )
+
+          ENDIF
+
+!
+!--       Define the variables
+          var_list = ';'
+          i = 1
+
+          DO WHILE ( do3d(av,i)(1:1) /= ' ' )
+
+!
+!--          Check for the grid
+             found = .TRUE.
+             SELECT CASE ( do3d(av,i) )
+!
+!--             Most variables are defined on the scalar grid
+                CASE ( 'e', 'lpt', 'nr', 'p', 'pc', 'pr', 'prr', 'pt', 'q',    &
+                       'qc', 'ql', 'ql_c', 'ql_v', 'ql_vp', 'qr', 'qv', 'rho', &
+                       's', 'sa', 'vpt' , 'rad_lw_cs_hr', 'rad_lw_hr',         &
+                       'rad_sw_cs_hr', 'rad_sw_hr' )
+
+                   grid_x = 'x'
+                   grid_y = 'y'
+                   grid_z = 'zu'
+!
+!--             u grid
+                CASE ( 'u' )
+
+                   grid_x = 'xu'
+                   grid_y = 'y'
+                   grid_z = 'zu'
+!
+!--             v grid
+                CASE ( 'v' )
+
+                   grid_x = 'x'
+                   grid_y = 'yv'
+                   grid_z = 'zu'
+!
+!--             w grid
+                CASE ( 'rad_lw_in', 'rad_lw_out', 'rad_sw_in', 'rad_sw_out',   &
+                       'w' )
+
+                   grid_x = 'x'
+                   grid_y = 'y'
+                   grid_z = 'zw'
+!
+!--             soil grid
+                CASE ( 'm_soil', 't_soil' )
+
+                   grid_x = 'x'
+                   grid_y = 'y'
+                   grid_z = 'zs'
+
+                CASE DEFAULT
+!
+!--                Check for user-defined quantities
+                   CALL user_define_netcdf_grid( do3d(av,i), found, grid_x, &
+                                                 grid_y, grid_z )
+
+                   IF ( .NOT. found )  THEN
+                      WRITE ( message_string, * ) 'no grid defined for varia', &
+                                                  'ble ', TRIM( do3d(av,i) )
+                      CALL message( 'define_netcdf_header', 'PA0244', 0, 1, 0, &
+                                    6, 0 )
+                   ENDIF
+
+             END SELECT
+
+!
+!--          Select the respective dimension ids
+             IF ( grid_x == 'x' )  THEN
+                id_x = id_dim_x_3d(av)
+             ELSEIF ( grid_x == 'xu' )  THEN
+                id_x = id_dim_xu_3d(av)
+             ENDIF
+
+             IF ( grid_y == 'y' )  THEN
+                id_y = id_dim_y_3d(av)
+             ELSEIF ( grid_y == 'yv' )  THEN
+                id_y = id_dim_yv_3d(av)
+             ENDIF
+
+             IF ( grid_z == 'zu' )  THEN
+                id_z = id_dim_zu_3d(av)
+             ELSEIF ( grid_z == 'zw' )  THEN
+                id_z = id_dim_zw_3d(av)
+             ELSEIF ( grid_z == 'zs' )  THEN
+                id_z = id_dim_zs_3d(av)
+             ENDIF
+
+!
+!--          Define the grid
+             CALL netcdf_create_var( id_set_3d(av),(/ id_x, id_y, id_z,        &
+                                     id_dim_time_3d(av) /), do3d(av,i),        &
+                                     nc_precision(4), id_var_do3d(av,i),       &
+                                     TRIM( do3d_unit(av,i) ), do3d(av,i), 79,  &
+                                     80, 357 )
+#if defined( __netcdf4_parallel )
+             IF ( netcdf_data_format > 4 )  THEN
+!
+!--             Set no fill for every variable to increase performance.
+                nc_stat = NF90_DEF_VAR_FILL( id_set_3d(av),     &
+                                             id_var_do3d(av,i), &
+                                             1, 0 )
+                CALL netcdf_handle_error( 'netcdf_define_header', 532 )
+!
+!--             Set collective io operations for parallel io
+                nc_stat = NF90_VAR_PAR_ACCESS( id_set_3d(av),     &
+                                               id_var_do3d(av,i), &
+                                               NF90_COLLECTIVE )
+                CALL netcdf_handle_error( 'netcdf_define_header', 445 )
+             ENDIF
+#endif
+             var_list = TRIM( var_list ) // TRIM( do3d(av,i) ) // ';'
+
+             i = i + 1
+
+          ENDDO
+
+!
+!--       No arrays to output
+          IF ( i == 1 )  RETURN
+
+!
+!--       Write the list of variables as global attribute (this is used by
+!--       restart runs and by combine_plot_fields)
+          nc_stat = NF90_PUT_ATT( id_set_3d(av), NF90_GLOBAL, 'VAR_LIST', &
+                                  var_list )
+          CALL netcdf_handle_error( 'netcdf_define_header', 81 )
+
+!
+!--       Set general no fill, otherwise the performance drops significantly for
+!--       parallel output.
+          nc_stat = NF90_SET_FILL( id_set_3d(av), NF90_NOFILL, oldmode )
+          CALL netcdf_handle_error( 'netcdf_define_header', 528 )
+
+!
+!--       Leave netCDF define mode
+          nc_stat = NF90_ENDDEF( id_set_3d(av) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 82 )
+
+!
+!--       These data are only written by PE0 for parallel output to increase
+!--       the performance.
+          IF ( myid == 0  .OR.  netcdf_data_format < 5 )  THEN
+!
+!--          Write data for x (shifted by +dx/2) and xu axis
+             ALLOCATE( netcdf_data(0:nx+1) )
+
+             DO  i = 0, nx+1
+                netcdf_data(i) = ( i + 0.5 ) * dx
+             ENDDO
+
+             nc_stat = NF90_PUT_VAR( id_set_3d(av), id_var_x_3d(av),  &
+                                     netcdf_data, start = (/ 1 /),    &
+                                     count = (/ nx+2 /) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 83 )
+
+             DO  i = 0, nx+1
+                netcdf_data(i) = i * dx
+             ENDDO
+
+             nc_stat = NF90_PUT_VAR( id_set_3d(av), id_var_xu_3d(av), &
+                                     netcdf_data, start = (/ 1 /),    &
+                                     count = (/ nx+2 /) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 385 )
+
+             DEALLOCATE( netcdf_data )
+
+!
+!--          Write data for y (shifted by +dy/2) and yv axis
+             ALLOCATE( netcdf_data(0:ny+1) )
+
+             DO  i = 0, ny+1
+                netcdf_data(i) = ( i + 0.5_wp ) * dy
+             ENDDO
+
+             nc_stat = NF90_PUT_VAR( id_set_3d(av), id_var_y_3d(av),  &
+                                     netcdf_data, start = (/ 1 /),    &
+                                     count = (/ ny+2 /) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 84 )
+
+             DO  i = 0, ny+1
+                netcdf_data(i) = i * dy
+             ENDDO
+
+             nc_stat = NF90_PUT_VAR( id_set_3d(av), id_var_yv_3d(av), &
+                                     netcdf_data, start = (/ 1 /),    &
+                                     count = (/ ny+2 /))
+             CALL netcdf_handle_error( 'netcdf_define_header', 387 )
+
+             DEALLOCATE( netcdf_data )
+
+!
+!--          Write zu and zw data (vertical axes)
+             nc_stat = NF90_PUT_VAR( id_set_3d(av), id_var_zu_3d(av),  &
+                                     zu(nzb:nz_do3d), start = (/ 1 /), &
+                                     count = (/ nz_do3d-nzb+1 /) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 85 )
+
+
+             nc_stat = NF90_PUT_VAR( id_set_3d(av), id_var_zw_3d(av),  &
+                                     zw(nzb:nz_do3d), start = (/ 1 /), &
+                                     count = (/ nz_do3d-nzb+1 /) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 86 )
+
+!
+!--          In case of non-flat topography write height information
+             IF ( TRIM( topography ) /= 'flat' )  THEN
+
+                nc_stat = NF90_PUT_VAR( id_set_3d(av), id_var_zusi_3d(av), &
+                                        zu_s_inner(0:nx+1,0:ny+1), &
+                                        start = (/ 1, 1 /), &
+                                        count = (/ nx+2, ny+2 /) )
+                CALL netcdf_handle_error( 'netcdf_define_header', 419 )
+
+                nc_stat = NF90_PUT_VAR( id_set_3d(av), id_var_zwwi_3d(av), &
+                                        zw_w_inner(0:nx+1,0:ny+1), &
+                                        start = (/ 1, 1 /), &
+                                        count = (/ nx+2, ny+2 /) )
+                CALL netcdf_handle_error( 'netcdf_define_header', 420 )
+
+             ENDIF
+
+             IF ( land_surface )  THEN
+!
+!--             Write zs grid
+                nc_stat = NF90_PUT_VAR( id_set_3d(av), id_var_zs_3d(av),  &
+                                        - zs(nzb_soil:nzt_soil), start = (/ 1 /), &
+                                        count = (/ nzt_soil-nzb_soil+1 /) )
+                CALL netcdf_handle_error( 'netcdf_define_header', 86 )
+             ENDIF
+
+          ENDIF
+
+       CASE ( '3d_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_3d(av), NF90_GLOBAL, 'VAR_LIST', &
+                                  var_list_old )
+          CALL netcdf_handle_error( 'netcdf_define_header', 87 )
+
+          var_list = ';'
+          i = 1
+          DO WHILE ( do3d(av,i)(1:1) /= ' ' )
+             var_list = TRIM(var_list) // TRIM( do3d(av,i) ) // ';'
+             i = i + 1
+          ENDDO
+
+          IF ( av == 0 )  THEN
+             var = '(3d)'
+          ELSE
+             var = '(3d_av)'
+          ENDIF
+
+          IF ( TRIM( var_list ) /= TRIM( var_list_old ) )  THEN
+             message_string = 'netCDF file for volume data ' //             &
+                              TRIM( var ) // ' from previous run found,' // &
+                              '&but this file cannot be extended due to' // &
+                              ' variable mismatch.' //                      &
+                              '&New file is created instead.'
+             CALL message( 'define_netcdf_header', 'PA0245', 0, 1, 0, 6, 0 )
+             extend = .FALSE.
+             RETURN
+          ENDIF
+
+!
+!--       Get and compare the number of vertical gridpoints
+          nc_stat = NF90_INQ_VARID( id_set_3d(av), 'zu_3d', id_var_zu_3d(av) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 88 )
+
+          nc_stat = NF90_INQUIRE_VARIABLE( id_set_3d(av), id_var_zu_3d(av), &
+                                           dimids = id_dim_zu_3d_old )
+          CALL netcdf_handle_error( 'netcdf_define_header', 89 )
+          id_dim_zu_3d(av) = id_dim_zu_3d_old(1)
+
+          nc_stat = NF90_INQUIRE_DIMENSION( id_set_3d(av), id_dim_zu_3d(av), &
+                                            len = nz_old )
+          CALL netcdf_handle_error( 'netcdf_define_header', 90 )
+
+          IF ( nz_do3d-nzb+1 /= nz_old )  THEN
+              message_string = 'netCDF file for volume data ' //             &
+                               TRIM( var ) // ' from previous run found,' // &
+                               '&but this file cannot be extended due to' // &
+                               ' mismatch in number of' //                   &
+                               '&vertical grid points (nz_do3d).' //         &
+                               '&New file is created instead.'
+             CALL message( 'define_netcdf_header', 'PA0246', 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 pl3d..count+1.
+!--       The current time must be larger than the last output time
+!--       on the file.
+          nc_stat = NF90_INQ_VARID( id_set_3d(av), 'time', id_var_time_3d(av) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 91 )
+
+          nc_stat = NF90_INQUIRE_VARIABLE( id_set_3d(av), id_var_time_3d(av), &
+                                           dimids = id_dim_time_old )
+          CALL netcdf_handle_error( 'netcdf_define_header', 92 )
+
+          id_dim_time_3d(av) = id_dim_time_old(1)
+
+          nc_stat = NF90_INQUIRE_DIMENSION( id_set_3d(av), id_dim_time_3d(av), &
+                                            len = ntime_count )
+          CALL netcdf_handle_error( 'netcdf_define_header', 93 )
+
+!
+!--       For non-parallel output use the last output time level of the netcdf
+!--       file because the time dimension is unlimited. In case of parallel
+!--       output the variable ntime_count could get the value of 9*10E36 because
+!--       the time dimension is limited.
+          IF ( netcdf_data_format < 5 ) do3d_time_count(av) = ntime_count
+
+          nc_stat = NF90_GET_VAR( id_set_3d(av), id_var_time_3d(av), &
+                                  last_time_coordinate,              &
+                                  start = (/ do3d_time_count(av) /), &
+                                  count = (/ 1 /) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 94 )
+
+          IF ( last_time_coordinate(1) >= simulated_time )  THEN
+             message_string = 'netCDF file for volume data ' // &
+                              TRIM( var ) // ' 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', 'PA0247', 0, 1, 0, 6, 0 )
+             do3d_time_count(av) = 0
+             extend = .FALSE.
+             RETURN
+          ENDIF
+
+          IF ( netcdf_data_format > 4 )  THEN
+!
+!--          Set needed time levels (ntdim) to 
+!--          saved time levels + to be saved time levels.
+             IF ( av == 0 )  THEN 
+                ntdim_3d(0) = do3d_time_count(0) + CEILING(                &
+                              ( end_time - MAX( skip_time_do3d,            &
+                                                simulated_time_at_begin )  &
+                              ) / dt_do3d )
+             ELSE
+                ntdim_3d(1) = do3d_time_count(1) + CEILING(                &
+                              ( end_time - MAX( skip_time_data_output_av,  &
+                                                simulated_time_at_begin )  &
+                              ) / dt_data_output_av )
+             ENDIF
+
+!
+!--          Check if the needed number of output time levels is increased
+!--          compared to the number of time levels in the existing file.
+             IF ( ntdim_3d(av) > ntime_count )  THEN
+                message_string = 'netCDF file for volume data ' // &
+                                 TRIM( var ) // ' from previous run found,' // &
+                                 '&but this file cannot be extended becaus' // &
+                                 'e the number of output time levels&has b' // &
+                                 'een increased compared to the previous s' // &
+                                 'imulation.' //                               &
+                                 '&New file is created instead.'
+                CALL message( 'define_netcdf_header', 'PA0388', 0, 1, 0, 6, 0 )
+                do3d_time_count(av) = 0
+                extend = .FALSE.
+!
+!--             Recalculate the needed time levels for the new file.
+                IF ( av == 0 )  THEN
+                   ntdim_3d(0) = CEILING(                               &
+                           ( end_time - MAX( skip_time_do3d,            &
+                                             simulated_time_at_begin )  &
+                           ) / dt_do3d )
+                ELSE
+                   ntdim_3d(1) = CEILING(                               &
+                           ( end_time - MAX( skip_time_data_output_av,  &
+                                             simulated_time_at_begin )  &
+                           ) / dt_data_output_av )
+                ENDIF
+                RETURN
+             ENDIF
+          ENDIF
+
+!
+!--       Dataset seems to be extendable.
+!--       Now get the variable ids.
+          i = 1
+          DO WHILE ( do3d(av,i)(1:1) /= ' ' )
+             nc_stat = NF90_INQ_VARID( id_set_3d(av), TRIM( do3d(av,i) ), &
+                                       id_var_do3d(av,i) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 95 )
+#if defined( __netcdf4_parallel )
+!
+!--          Set collective io operations for parallel io
+             IF ( netcdf_data_format > 4 )  THEN
+                nc_stat = NF90_VAR_PAR_ACCESS( id_set_3d(av),     &
+                                               id_var_do3d(av,i), &
+                                               NF90_COLLECTIVE )
+                CALL netcdf_handle_error( 'netcdf_define_header', 453 )
+             ENDIF
+#endif
+             i = i + 1
+          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. Maybe revise later.
+          IF ( av == 0 )  THEN
+             time_average_text = ' '
+          ELSE
+             WRITE (time_average_text, '('', '',F7.1,'' s average'')') &
+                                                            averaging_interval
+          ENDIF
+          nc_stat = NF90_REDEF( id_set_3d(av) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 429 )
+          nc_stat = NF90_PUT_ATT( id_set_3d(av), NF90_GLOBAL, 'title', &
+                                  TRIM( run_description_header ) //    &
+                                  TRIM( time_average_text ) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 96 )
+          nc_stat = NF90_ENDDEF( id_set_3d(av) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 430 )
+          message_string = 'netCDF file for volume data ' //             &
+                           TRIM( var ) // ' from previous run found.' // &
+                           '&This file will be extended.'
+          CALL message( 'define_netcdf_header', 'PA0248', 0, 0, 0, 6, 0 )
+
+       CASE ( 'xy_new' )
+
+!
+!--       Define some global attributes of the dataset
+          nc_stat = NF90_PUT_ATT( id_set_xy(av), NF90_GLOBAL, 'Conventions', &
+                                  'COARDS' )
+          CALL netcdf_handle_error( 'netcdf_define_header', 97 )
+
+          IF ( av == 0 )  THEN
+             time_average_text = ' '
+          ELSE
+             WRITE (time_average_text, '('', '',F7.1,'' s average'')') &
+                                                            averaging_interval
+          ENDIF
+          nc_stat = NF90_PUT_ATT( id_set_xy(av), NF90_GLOBAL, 'title', &
+                                  TRIM( run_description_header ) //    &
+                                  TRIM( time_average_text ) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 98 )
+          IF ( av == 1 )  THEN
+             WRITE ( time_average_text,'(F7.1,'' s avg'')' )  averaging_interval
+             nc_stat = NF90_PUT_ATT( id_set_xy(av), NF90_GLOBAL, 'time_avg', &
+                                     TRIM( time_average_text ) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 98 )
+          ENDIF
+
+!
+!--       Define time coordinate for xy sections.
+!--       For parallel output the time dimensions has to be limited, otherwise
+!--       the performance drops significantly.
+          IF ( netcdf_data_format < 5 )  THEN
+             CALL netcdf_create_dim( id_set_xy(av), 'time', NF90_UNLIMITED,    &
+                                     id_dim_time_xy(av), 99 )
+          ELSE
+             CALL netcdf_create_dim( id_set_xy(av), 'time', ntdim_2d_xy(av),   &
+                                     id_dim_time_xy(av), 524 )
+          ENDIF
+
+          CALL netcdf_create_var( id_set_xy(av), (/ id_dim_time_xy(av) /),     &
+                                  'time', NF90_DOUBLE, id_var_time_xy(av),     &
+                                  'seconds', '', 100, 101, 000 )
+!
+!--       Define the spatial dimensions and coordinates for xy-sections.
+!--       First, determine the number of horizontal sections to be written.
+          IF ( section(1,1) == -9999 )  THEN
+             RETURN
+          ELSE
+             ns = 1
+             DO WHILE ( section(ns,1) /= -9999  .AND.  ns <= 100 )
+                ns = ns + 1
+             ENDDO
+             ns = ns - 1
+          ENDIF
+
+!
+!--       Define vertical coordinate grid (zu grid)
+          CALL netcdf_create_dim( id_set_xy(av), 'zu_xy', ns,                  &
+                                  id_dim_zu_xy(av), 102 )
+          CALL netcdf_create_var( id_set_xy(av), (/ id_dim_zu_xy(av) /),       &
+                                  'zu_xy', NF90_DOUBLE, id_var_zu_xy(av),      &
+                                  'meters', '', 103, 104, 000 )
+!
+!--       Define vertical coordinate grid (zw grid)
+          CALL netcdf_create_dim( id_set_xy(av), 'zw_xy', ns,                  &
+                                  id_dim_zw_xy(av), 105 )
+          CALL netcdf_create_var( id_set_xy(av), (/ id_dim_zw_xy(av) /),       &
+                                  'zw_xy', NF90_DOUBLE, id_var_zw_xy(av),      &
+                                  'meters', '', 106, 107, 000 )
+
+          IF ( land_surface )  THEN
+
+             ns_do = 0
+             DO WHILE ( section(ns_do+1,1) < nzs )
+                ns_do = ns_do + 1
+             ENDDO
+!
+!--          Define vertical coordinate grid (zs grid)
+             CALL netcdf_create_dim( id_set_xy(av), 'zs_xy', ns_do,            &
+                                     id_dim_zs_xy(av), 539 )
+             CALL netcdf_create_var( id_set_xy(av), (/ id_dim_zs_xy(av) /),    &
+                                     'zs_xy', NF90_DOUBLE, id_var_zs_xy(av),   &
+                                     'meters', '', 540, 541, 000 )
+
+          ENDIF
+
+!
+!--       Define a pseudo vertical coordinate grid for the surface variables
+!--       u* and t* to store their height level
+          CALL netcdf_create_dim( id_set_xy(av), 'zu1_xy', 1,                  &
+                                  id_dim_zu1_xy(av), 108 )
+          CALL netcdf_create_var( id_set_xy(av), (/ id_dim_zu1_xy(av) /),      &
+                                  'zu1_xy', NF90_DOUBLE, id_var_zu1_xy(av),    &
+                                  'meters', '', 109, 110, 000 )
+!
+!--       Define a variable to store the layer indices of the horizontal cross
+!--       sections, too
+          CALL netcdf_create_var( id_set_xy(av), (/ id_dim_zu_xy(av) /),       &
+                                  'ind_z_xy', NF90_DOUBLE,                     &
+                                  id_var_ind_z_xy(av), 'gridpoints', '', 111,  &
+                                  112, 000 )
+!
+!--       Define x-axis (for scalar position)
+          CALL netcdf_create_dim( id_set_xy(av), 'x', nx+2, id_dim_x_xy(av),   &
+                                  113 )
+          CALL netcdf_create_var( id_set_xy(av), (/ id_dim_x_xy(av) /), 'x',   &
+                                  NF90_DOUBLE, id_var_x_xy(av), 'meters', '',  &
+                                  114, 115, 000 )
+!
+!--       Define x-axis (for u position)
+          CALL netcdf_create_dim( id_set_xy(av), 'xu', nx+2,                   &
+                                  id_dim_xu_xy(av), 388 )
+          CALL netcdf_create_var( id_set_xy(av), (/ id_dim_xu_xy(av) /), 'xu', &
+                                  NF90_DOUBLE, id_var_xu_xy(av), 'meters', '', &
+                                  389, 390, 000 )
+!
+!--       Define y-axis (for scalar position)
+          CALL netcdf_create_dim( id_set_xy(av), 'y', ny+2, id_dim_y_xy(av),   &
+                                  116 )
+          CALL netcdf_create_var( id_set_xy(av), (/ id_dim_y_xy(av) /), 'y',   &
+                                  NF90_DOUBLE, id_var_y_xy(av), 'meters', '',  &
+                                  117, 118, 000 )
+!
+!--       Define y-axis (for scalar position)
+          CALL netcdf_create_dim( id_set_xy(av), 'yv', ny+2,                   &
+                                  id_dim_yv_xy(av), 364 )
+          CALL netcdf_create_var( id_set_xy(av), (/ id_dim_yv_xy(av) /), 'yv', &
+                                  NF90_DOUBLE, id_var_yv_xy(av), 'meters', '', &
+                                  365, 366, 000 )
+!
+!--       In case of non-flat topography define 2d-arrays containing the height
+!--       information
+          IF ( TRIM( topography ) /= 'flat' )  THEN
+!
+!--          Define zusi = zu(nzb_s_inner)
+             CALL netcdf_create_var( id_set_xy(av), (/ id_dim_x_xy(av),        &
+                                     id_dim_y_xy(av) /), 'zusi', NF90_DOUBLE,  &
+                                     id_var_zusi_xy(av), 'meters',             &
+                                     'zu(nzb_s_inner)', 421, 422, 423 )
+!             
+!--          Define zwwi = zw(nzb_w_inner)
+             CALL netcdf_create_var( id_set_xy(av), (/ id_dim_x_xy(av),        &
+                                     id_dim_y_xy(av) /), 'zwwi', NF90_DOUBLE,  &
+                                     id_var_zwwi_xy(av), 'meters',             &
+                                     'zw(nzb_w_inner)', 424, 425, 426 )
+
+          ENDIF
+
+!
+!--       Define the variables
+          var_list = ';'
+          i = 1
+
+          DO WHILE ( do2d(av,i)(1:1) /= ' ' )
+
+             IF ( INDEX( do2d(av,i), 'xy' ) /= 0 )  THEN
+!
+!--             If there is a star in the variable name (u* or t*), it is a
+!--             surface variable. Define it with id_dim_zu1_xy.
+                IF ( INDEX( do2d(av,i), '*' ) /= 0 )  THEN
+
+                   CALL netcdf_create_var( id_set_xy(av), (/ id_dim_x_xy(av),  &
+                                           id_dim_y_xy(av), id_dim_zu1_xy(av), &
+                                           id_dim_time_xy(av) /), do2d(av,i),  &
+                                           nc_precision(1), id_var_do2d(av,i), &
+                                           TRIM( do2d_unit(av,i) ),            &
+                                           do2d(av,i), 119, 120, 354 )
+
+                ELSE
+
+!
+!--                Check for the grid
+                   found = .TRUE.
+                   SELECT CASE ( do2d(av,i) )
+!
+!--                   Most variables are defined on the zu grid
+                      CASE ( 'e_xy', 'lpt_xy', 'nr_xy', 'p_xy', 'pc_xy',       &
+                             'pr_xy', 'prr_xy', 'pt_xy', 'q_xy', 'qc_xy',      &
+                             'ql_xy', 'ql_c_xy', 'ql_v_xy', 'ql_vp_xy',        &
+                             'qr_xy', 'qv_xy', 'rad_lw_cs_hr_xy',              &
+                             'rad_lw_hr_xy', 'rad_sw_cs_hr_xy', 'rad_sw_hr_xy',& 
+                             'rho_xy', 's_xy', 'sa_xy', 'vpt_xy' )
+
+                         grid_x = 'x'
+                         grid_y = 'y'
+                         grid_z = 'zu'
+!
+!--                   u grid
+                      CASE ( 'u_xy' )
+
+                         grid_x = 'xu'
+                         grid_y = 'y'
+                         grid_z = 'zu'
+!
+!--                   v grid
+                      CASE ( 'v_xy' )
+
+                         grid_x = 'x'
+                         grid_y = 'yv'
+                         grid_z = 'zu'
+!
+!--                   w grid
+                      CASE ( 'rad_lw_in_xy', 'rad_lw_out_xy', 'rad_sw_in_xy',  &
+                             'rad_sw_out_xy' , 'w_xy' )
+
+                         grid_x = 'x'
+                         grid_y = 'y'
+                         grid_z = 'zw'
+!
+!--                   soil grid
+                      CASE ( 'm_soil_xy', 't_soil_xy' )
+                         grid_x = 'x'
+                         grid_y = 'y'
+                         grid_z = 'zs'
+
+                      CASE DEFAULT
+!
+!--                      Check for user-defined quantities
+                         CALL user_define_netcdf_grid( do2d(av,i), found, &
+                                                       grid_x, grid_y, grid_z )
+
+                         IF ( .NOT. found )  THEN
+                            WRITE ( message_string, * ) 'no grid defined for', &
+                                                ' variable ', TRIM( do2d(av,i) )
+                            CALL message( 'define_netcdf_header', 'PA0244',    &
+                                          0, 1, 0, 6, 0 )
+                         ENDIF
+
+                   END SELECT
+
+!
+!--                Select the respective dimension ids
+                   IF ( grid_x == 'x' )  THEN
+                      id_x = id_dim_x_xy(av)
+                   ELSEIF ( grid_x == 'xu' )  THEN
+                      id_x = id_dim_xu_xy(av)
+                   ENDIF
+
+                   IF ( grid_y == 'y' )  THEN
+                      id_y = id_dim_y_xy(av)
+                   ELSEIF ( grid_y == 'yv' )  THEN
+                      id_y = id_dim_yv_xy(av)
+                   ENDIF
+
+                   IF ( grid_z == 'zu' )  THEN
+                      id_z = id_dim_zu_xy(av)
+                   ELSEIF ( grid_z == 'zw' )  THEN
+                      id_z = id_dim_zw_xy(av)
+                   ELSEIF ( grid_z == 'zs' )  THEN
+                      id_z = id_dim_zs_xy(av)
+                   ENDIF
+
+!
+!--                Define the grid
+                   CALL netcdf_create_var( id_set_xy(av), (/ id_x, id_y, id_z, &
+                                           id_dim_time_xy(av) /), do2d(av,i),  &
+                                           nc_precision(1), id_var_do2d(av,i), &
+                                           TRIM( do2d_unit(av,i) ),            &
+                                           do2d(av,i), 119, 120, 354 )
+
+                ENDIF
+
+#if defined( __netcdf4_parallel )
+                IF ( netcdf_data_format > 4 )  THEN
+!
+!--                Set no fill for every variable to increase performance.
+                   nc_stat = NF90_DEF_VAR_FILL( id_set_xy(av),     &
+                                                id_var_do2d(av,i), &
+                                                1, 0 )
+                   CALL netcdf_handle_error( 'netcdf_define_header', 533 )
+!
+!--                Set collective io operations for parallel io
+                   nc_stat = NF90_VAR_PAR_ACCESS( id_set_xy(av),     &
+                                                  id_var_do2d(av,i), &
+                                                  NF90_COLLECTIVE )
+                   CALL netcdf_handle_error( 'netcdf_define_header', 448 )
+                ENDIF
+#endif
+                var_list = TRIM( var_list) // TRIM( do2d(av,i) ) // ';'
+
+             ENDIF
+
+             i = i + 1
+
+          ENDDO
+
+!
+!--       No arrays to output. Close the netcdf file and return.
+          IF ( i == 1 )  RETURN
+
+!
+!--       Write the list of variables as global attribute (this is used by
+!--       restart runs and by combine_plot_fields)
+          nc_stat = NF90_PUT_ATT( id_set_xy(av), NF90_GLOBAL, 'VAR_LIST', &
+                                  var_list )
+          CALL netcdf_handle_error( 'netcdf_define_header', 121 )
+
+!
+!--       Set general no fill, otherwise the performance drops significantly for
+!--       parallel output.
+          nc_stat = NF90_SET_FILL( id_set_xy(av), NF90_NOFILL, oldmode )
+          CALL netcdf_handle_error( 'netcdf_define_header', 529 )
+
+!
+!--       Leave netCDF define mode
+          nc_stat = NF90_ENDDEF( id_set_xy(av) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 122 )
+
+!
+!--       These data are only written by PE0 for parallel output to increase
+!--       the performance.
+          IF ( myid == 0  .OR.  netcdf_data_format < 5 )  THEN
+
+!
+!--          Write axis data: z_xy, x, y
+             ALLOCATE( netcdf_data(1:ns) )
+
+!
+!--          Write zu data
+             DO  i = 1, ns
+                IF( section(i,1) == -1 )  THEN
+                   netcdf_data(i) = -1.0_wp  ! section averaged along z
+                ELSE
+                   netcdf_data(i) = zu( section(i,1) )
+                ENDIF
+             ENDDO
+             nc_stat = NF90_PUT_VAR( id_set_xy(av), id_var_zu_xy(av), &
+                                     netcdf_data, start = (/ 1 /),    &
+                                     count = (/ ns /) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 123 )
+
+!
+!--          Write zw data
+             DO  i = 1, ns
+                IF( section(i,1) == -1 )  THEN
+                   netcdf_data(i) = -1.0_wp  ! section averaged along z
+                ELSE
+                   netcdf_data(i) = zw( section(i,1) )
+                ENDIF
+             ENDDO
+             nc_stat = NF90_PUT_VAR( id_set_xy(av), id_var_zw_xy(av), &
+                                     netcdf_data, start = (/ 1 /),    &
+                                     count = (/ ns /) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 124 )
+
+!
+!--             Write zs data
+             IF ( land_surface )  THEN
+                ns_do = 0
+                DO  i = 1, ns
+                   IF( section(i,1) == -1 )  THEN
+                      netcdf_data(i) = 1.0_wp  ! section averaged along z
+                      ns_do = ns_do + 1
+                   ELSEIF ( section(i,1) < nzs )  THEN
+                      netcdf_data(i) = - zs( section(i,1) )
+                      ns_do = ns_do + 1
+                   ENDIF
+                ENDDO
+
+                nc_stat = NF90_PUT_VAR( id_set_xy(av), id_var_zs_xy(av), &
+                                        netcdf_data(1:ns_do), start = (/ 1 /),    &
+                                        count = (/ ns_do /) )
+                CALL netcdf_handle_error( 'netcdf_define_header', 124 )
+
+             ENDIF
+
+!
+!--          Write gridpoint number data
+             netcdf_data(1:ns) = section(1:ns,1)
+             nc_stat = NF90_PUT_VAR( id_set_xy(av), id_var_ind_z_xy(av), &
+                                     netcdf_data, start = (/ 1 /),       &
+                                     count = (/ ns /) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 125 )
+
+             DEALLOCATE( netcdf_data )
+
+!
+!--          Write the cross section height u*, t*
+             nc_stat = NF90_PUT_VAR( id_set_xy(av), id_var_zu1_xy(av), &
+                                     (/ zu(nzb+1) /), start = (/ 1 /), &
+                                     count = (/ 1 /) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 126 )
+
+!
+!--          Write data for x (shifted by +dx/2) and xu axis
+             ALLOCATE( netcdf_data(0:nx+1) )
+
+             DO  i = 0, nx+1
+                netcdf_data(i) = ( i + 0.5_wp ) * dx
+             ENDDO
+
+             nc_stat = NF90_PUT_VAR( id_set_xy(av), id_var_x_xy(av), &
+                                     netcdf_data, start = (/ 1 /),   &
+                                     count = (/ nx+2 /) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 127 )
+
+             DO  i = 0, nx+1
+                netcdf_data(i) = i * dx
+             ENDDO
+
+             nc_stat = NF90_PUT_VAR( id_set_xy(av), id_var_xu_xy(av), &
+                                     netcdf_data, start = (/ 1 /),    &
+                                     count = (/ nx+2 /) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 367 )
+
+             DEALLOCATE( netcdf_data )
+
+!
+!--          Write data for y (shifted by +dy/2) and yv axis
+             ALLOCATE( netcdf_data(0:ny+1) )
+
+             DO  i = 0, ny+1
+                netcdf_data(i) = ( i + 0.5_wp ) * dy
+             ENDDO
+
+             nc_stat = NF90_PUT_VAR( id_set_xy(av), id_var_y_xy(av), &
+                                     netcdf_data, start = (/ 1 /),   &
+                                     count = (/ ny+2 /))
+             CALL netcdf_handle_error( 'netcdf_define_header', 128 )
+
+             DO  i = 0, ny+1
+                netcdf_data(i) = i * dy
+             ENDDO
+
+             nc_stat = NF90_PUT_VAR( id_set_xy(av), id_var_yv_xy(av), &
+                                     netcdf_data, start = (/ 1 /),    &
+                                     count = (/ ny+2 /))
+             CALL netcdf_handle_error( 'netcdf_define_header', 368 )
+
+             DEALLOCATE( netcdf_data )
+
+!
+!--          In case of non-flat topography write height information
+             IF ( TRIM( topography ) /= 'flat' )  THEN
+
+                nc_stat = NF90_PUT_VAR( id_set_xy(av), id_var_zusi_xy(av), &
+                                        zu_s_inner(0:nx+1,0:ny+1), &
+                                        start = (/ 1, 1 /), &
+                                        count = (/ nx+2, ny+2 /) )
+                CALL netcdf_handle_error( 'netcdf_define_header', 427 )
+
+                nc_stat = NF90_PUT_VAR( id_set_xy(av), id_var_zwwi_xy(av), &
+                                        zw_w_inner(0:nx+1,0:ny+1), &
+                                        start = (/ 1, 1 /), &
+                                        count = (/ nx+2, ny+2 /) )
+                CALL netcdf_handle_error( 'netcdf_define_header', 428 )
+
+             ENDIF
+
+
+
+          ENDIF
+
+       CASE ( 'xy_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_xy(av), NF90_GLOBAL, 'VAR_LIST', &
+                                  var_list_old )
+          CALL netcdf_handle_error( 'netcdf_define_header', 129 )
+
+          var_list = ';'
+          i = 1
+          DO WHILE ( do2d(av,i)(1:1) /= ' ' )
+             IF ( INDEX( do2d(av,i), 'xy' ) /= 0 )  THEN
+                var_list = TRIM( var_list ) // TRIM( do2d(av,i) ) // ';'
+             ENDIF
+             i = i + 1
+          ENDDO
+
+          IF ( av == 0 )  THEN
+             var = '(xy)'
+          ELSE
+             var = '(xy_av)'
+          ENDIF
+
+          IF ( TRIM( var_list ) /= TRIM( var_list_old ) )  THEN
+             message_string = 'netCDF file for cross-sections ' //           &
+                              TRIM( var ) // ' from previous run found,' //  &
+                              '& but this file cannot be extended due to' // &
+                              ' variable mismatch.' //                       &
+                              '&New file is created instead.'
+             CALL message( 'define_netcdf_header', 'PA0249', 0, 1, 0, 6, 0 )
+             extend = .FALSE.
+             RETURN
+          ENDIF
+
+!
+!--       Calculate the number of current sections
+          ns = 1
+          DO WHILE ( section(ns,1) /= -9999  .AND.  ns <= 100 )
+             ns = ns + 1
+          ENDDO
+          ns = ns - 1
+
+!
+!--       Get and compare the number of horizontal cross sections
+          nc_stat = NF90_INQ_VARID( id_set_xy(av), 'zu_xy', id_var_zu_xy(av) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 130 )
+
+          nc_stat = NF90_INQUIRE_VARIABLE( id_set_xy(av), id_var_zu_xy(av), &
+                                           dimids = id_dim_zu_xy_old )
+          CALL netcdf_handle_error( 'netcdf_define_header', 131 )
+          id_dim_zu_xy(av) = id_dim_zu_xy_old(1)
+
+          nc_stat = NF90_INQUIRE_DIMENSION( id_set_xy(av), id_dim_zu_xy(av), &
+                                            len = ns_old )
+          CALL netcdf_handle_error( 'netcdf_define_header', 132 )
+
+          IF ( ns /= ns_old )  THEN
+             message_string = 'netCDF file for cross-sections ' //          &
+                              TRIM( var ) // ' from previous run found,' // &
+                              '&but this file cannot be extended due to' // &
+                              ' mismatch in number of' //                   &
+                              '&cross sections.' //                         &
+                              '&New file is created instead.'
+             CALL message( 'define_netcdf_header', 'PA0250', 0, 1, 0, 6, 0 )
+             extend = .FALSE.
+             RETURN
+          ENDIF
+
+!
+!--       Get and compare the heights of the cross sections
+          ALLOCATE( netcdf_data(1:ns_old) )
+
+          nc_stat = NF90_GET_VAR( id_set_xy(av), id_var_zu_xy(av), netcdf_data )
+          CALL netcdf_handle_error( 'netcdf_define_header', 133 )
+
+          DO  i = 1, ns
+             IF ( section(i,1) /= -1 )  THEN
+                IF ( zu(section(i,1)) /= netcdf_data(i) )  THEN
+                   message_string = 'netCDF file for cross-sections ' //     &
+                               TRIM( var ) // ' from previous run found,' // &
+                               '&but this file cannot be extended' //        &
+                               ' due to mismatch in cross' //                &
+                               '&section levels.' //                         &
+                               '&New file is created instead.'
+                   CALL message( 'define_netcdf_header', 'PA0251', &
+                                                                 0, 1, 0, 6, 0 )
+                   extend = .FALSE.
+                   RETURN
+                ENDIF
+             ELSE
+                IF ( -1.0_wp /= netcdf_data(i) )  THEN
+                   message_string = 'netCDF file for cross-sections ' //     &
+                               TRIM( var ) // ' from previous run found,' // &
+                               '&but this file cannot be extended' //        &
+                               ' due to mismatch in cross' //                &
+                               '&section levels.' //                         &
+                               '&New file is created instead.'
+                   CALL message( 'define_netcdf_header', 'PA0251',&
+                                                                 0, 1, 0, 6, 0 )
+                   extend = .FALSE.
+                   RETURN
+                ENDIF
+             ENDIF
+          ENDDO
+
+          DEALLOCATE( netcdf_data )
+
+!
+!--       Get the id of the time coordinate (unlimited coordinate) and its
+!--       last index on the file. The next time level is do2d..count+1.
+!--       The current time must be larger than the last output time
+!--       on the file.
+          nc_stat = NF90_INQ_VARID( id_set_xy(av), 'time', id_var_time_xy(av) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 134 )
+
+          nc_stat = NF90_INQUIRE_VARIABLE( id_set_xy(av), id_var_time_xy(av), &
+                                           dimids = id_dim_time_old )
+          CALL netcdf_handle_error( 'netcdf_define_header', 135 )
+          id_dim_time_xy(av) = id_dim_time_old(1)
+
+          nc_stat = NF90_INQUIRE_DIMENSION( id_set_xy(av), id_dim_time_xy(av), &
+                                            len = ntime_count )
+          CALL netcdf_handle_error( 'netcdf_define_header', 136 )
+
+!
+!--       For non-parallel output use the last output time level of the netcdf
+!--       file because the time dimension is unlimited. In case of parallel
+!--       output the variable ntime_count could get the value of 9*10E36 because
+!--       the time dimension is limited.
+          IF ( netcdf_data_format < 5 ) do2d_xy_time_count(av) = ntime_count
+
+          nc_stat = NF90_GET_VAR( id_set_xy(av), id_var_time_xy(av),    &
+                                  last_time_coordinate,                 &
+                                  start = (/ do2d_xy_time_count(av) /), &
+                                  count = (/ 1 /) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 137 )
+
+          IF ( last_time_coordinate(1) >= simulated_time )  THEN
+             message_string = 'netCDF file for cross sections ' //          &
+                              TRIM( var ) // ' 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', 'PA0252', 0, 1, 0, 6, 0 )
+             do2d_xy_time_count(av) = 0
+             extend = .FALSE.
+             RETURN
+          ENDIF
+
+          IF ( netcdf_data_format > 4 )  THEN
+!
+!--          Set needed time levels (ntdim) to 
+!--          saved time levels + to be saved time levels.
+             IF ( av == 0 )  THEN 
+                ntdim_2d_xy(0) = do2d_xy_time_count(0) + CEILING(             &
+                                 ( end_time - MAX( skip_time_do2d_xy,         &
+                                                   simulated_time_at_begin )  &
+                                 ) / dt_do2d_xy )
+             ELSE
+                ntdim_2d_xy(1) = do2d_xy_time_count(1) + CEILING(             &
+                                 ( end_time - MAX( skip_time_data_output_av,  &
+                                                   simulated_time_at_begin )  &
+                                 ) / dt_data_output_av )
+             ENDIF
+
+!
+!--          Check if the needed number of output time levels is increased
+!--          compared to the number of time levels in the existing file.
+             IF ( ntdim_2d_xy(av) > ntime_count )  THEN
+                message_string = 'netCDF file for cross sections ' //          &
+                                 TRIM( var ) // ' from previous run found,' // &
+                                 '&but this file cannot be extended becaus' // &
+                                 'e the number of output time levels&has b' // &
+                                 'een increased compared to the previous s' // &
+                                 'imulation.' //                               &
+                                 '&New file is created instead.'
+                CALL message( 'define_netcdf_header', 'PA0389', 0, 1, 0, 6, 0 )
+                do2d_xy_time_count(av) = 0
+                extend = .FALSE.
+!
+!--             Recalculate the needed time levels for the new file.
+                IF ( av == 0 )  THEN
+                   ntdim_2d_xy(0) = CEILING(                            &
+                           ( end_time - MAX( skip_time_do2d_xy,         &
+                                             simulated_time_at_begin )  &
+                           ) / dt_do2d_xy )
+                ELSE
+                   ntdim_2d_xy(1) = CEILING(                            &
+                           ( end_time - MAX( skip_time_data_output_av,  &
+                                             simulated_time_at_begin )  &
+                           ) / dt_data_output_av )
+                ENDIF
+                RETURN
+             ENDIF
+          ENDIF
+
+!
+!--       Dataset seems to be extendable.
+!--       Now get the variable ids.
+          i = 1
+          DO WHILE ( do2d(av,i)(1:1) /= ' ' )
+             IF ( INDEX( do2d(av,i), 'xy' ) /= 0 )  THEN
+                nc_stat = NF90_INQ_VARID( id_set_xy(av), do2d(av,i), &
+                                          id_var_do2d(av,i) )
+                CALL netcdf_handle_error( 'netcdf_define_header', 138 )
+#if defined( __netcdf4_parallel )
+!
+!--             Set collective io operations for parallel io
+                IF ( netcdf_data_format > 4 )  THEN
+                   nc_stat = NF90_VAR_PAR_ACCESS( id_set_xy(av),     &
+                                                  id_var_do2d(av,i), &
+                                                  NF90_COLLECTIVE )
+                   CALL netcdf_handle_error( 'netcdf_define_header', 454 )
+                ENDIF
+#endif
+             ENDIF
+             i = i + 1
+          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.
+          IF ( av == 0 )  THEN
+             time_average_text = ' '
+          ELSE
+             WRITE (time_average_text, '('', '',F7.1,'' s average'')') &
+                                                            averaging_interval
+          ENDIF
+          nc_stat = NF90_REDEF( id_set_xy(av) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 431 )
+          nc_stat = NF90_PUT_ATT( id_set_xy(av), NF90_GLOBAL, 'title', &
+                                  TRIM( run_description_header ) //    &
+                                  TRIM( time_average_text ) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 139 )
+          nc_stat = NF90_ENDDEF( id_set_xy(av) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 432 )
+          message_string = 'netCDF file for cross-sections ' //           &
+                            TRIM( var ) // ' from previous run found.' // &
+                           '&This file will be extended.'
+          CALL message( 'define_netcdf_header', 'PA0253', 0, 0, 0, 6, 0 )
+          
+
+       CASE ( 'xz_new' )
+
+!
+!--       Define some global attributes of the dataset
+          nc_stat = NF90_PUT_ATT( id_set_xz(av), NF90_GLOBAL, 'Conventions', &
+                                  'COARDS' )
+          CALL netcdf_handle_error( 'netcdf_define_header', 140 )
+
+          IF ( av == 0 )  THEN
+             time_average_text = ' '
+          ELSE
+             WRITE (time_average_text, '('', '',F7.1,'' s average'')') &
+                                                            averaging_interval
+          ENDIF
+          nc_stat = NF90_PUT_ATT( id_set_xz(av), NF90_GLOBAL, 'title', &
+                                  TRIM( run_description_header )  //   &
+                                  TRIM( time_average_text ) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 141 )
+          IF ( av == 1 )  THEN
+             WRITE ( time_average_text,'(F7.1,'' s avg'')' )  averaging_interval
+             nc_stat = NF90_PUT_ATT( id_set_xz(av), NF90_GLOBAL, 'time_avg', &
+                                     TRIM( time_average_text ) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 141 )
+          ENDIF
+
+!
+!--       Define time coordinate for xz sections.
+!--       For parallel output the time dimensions has to be limited, otherwise
+!--       the performance drops significantly.
+          IF ( netcdf_data_format < 5 )  THEN
+             CALL netcdf_create_dim( id_set_xz(av), 'time', NF90_UNLIMITED,    &
+                                     id_dim_time_xz(av), 142 )
+          ELSE
+             CALL netcdf_create_dim( id_set_xz(av), 'time', ntdim_2d_xz(av),   &
+                                     id_dim_time_xz(av), 525 )
+          ENDIF
+
+          CALL netcdf_create_var( id_set_xz(av), (/ id_dim_time_xz(av) /),     &
+                                  'time', NF90_DOUBLE, id_var_time_xz(av),     &
+                                  'seconds', '', 143, 144, 000 )
+!
+!--       Define the spatial dimensions and coordinates for xz-sections.
+!--       First, determine the number of vertical sections to be written.
+          IF ( section(1,2) == -9999 )  THEN
+             RETURN
+          ELSE
+             ns = 1
+             DO WHILE ( section(ns,2) /= -9999  .AND.  ns <= 100 )
+                ns = ns + 1
+             ENDDO
+             ns = ns - 1
+          ENDIF
+
+!
+!--       Define y-axis (for scalar position)
+          CALL netcdf_create_dim( id_set_xz(av), 'y_xz', ns, id_dim_y_xz(av),  &
+                                  145 )
+          CALL netcdf_create_var( id_set_xz(av), (/ id_dim_y_xz(av) /),        &
+                                  'y_xz', NF90_DOUBLE, id_var_y_xz(av),        &
+                                  'meters', '', 146, 147, 000 )
+!
+!--       Define y-axis (for v position)
+          CALL netcdf_create_dim( id_set_xz(av), 'yv_xz', ns,                  &
+                                  id_dim_yv_xz(av), 369 )
+          CALL netcdf_create_var( id_set_xz(av), (/ id_dim_yv_xz(av) /),       &
+                                  'yv_xz', NF90_DOUBLE, id_var_yv_xz(av),      &
+                                  'meters', '', 370, 371, 000 )
+!
+!--       Define a variable to store the layer indices of the vertical cross
+!--       sections
+          CALL netcdf_create_var( id_set_xz(av), (/ id_dim_y_xz(av) /),        &
+                                  'ind_y_xz', NF90_DOUBLE,                     &
+                                  id_var_ind_y_xz(av), 'gridpoints', '', 148,  &
+                                  149, 000 )
+!
+!--       Define x-axis (for scalar position)
+          CALL netcdf_create_dim( id_set_xz(av), 'x', nx+2, id_dim_x_xz(av),   &
+                                  150 )
+          CALL netcdf_create_var( id_set_xz(av), (/ id_dim_x_xz(av) /), 'x',   &
+                                  NF90_DOUBLE, id_var_x_xz(av), 'meters', '',  &
+                                  151, 152, 000 )
+!
+!--       Define x-axis (for u position)
+          CALL netcdf_create_dim( id_set_xz(av), 'xu', nx+2, id_dim_xu_xz(av), &
+                                  372 )
+          CALL netcdf_create_var( id_set_xz(av), (/ id_dim_xu_xz(av) /), 'xu', &
+                                  NF90_DOUBLE, id_var_xu_xz(av), 'meters', '', &
+                                  373, 374, 000 )
+!
+!--       Define the three z-axes (zu, zw, and zs)
+          CALL netcdf_create_dim( id_set_xz(av), 'zu', nz+2, id_dim_zu_xz(av), &
+                                  153 )
+          CALL netcdf_create_var( id_set_xz(av), (/ id_dim_zu_xz(av) /), 'zu', &
+                                  NF90_DOUBLE, id_var_zu_xz(av), 'meters', '', &
+                                  154, 155, 000 )
+          CALL netcdf_create_dim( id_set_xz(av), 'zw', nz+2, id_dim_zw_xz(av), &
+                                  156 )
+          CALL netcdf_create_var( id_set_xz(av), (/ id_dim_zw_xz(av) /), 'zw', &
+                                  NF90_DOUBLE, id_var_zw_xz(av), 'meters', '', &
+                                  157, 158, 000 )
+
+          IF ( land_surface )  THEN
+
+             CALL netcdf_create_dim( id_set_xz(av), 'zs', nzs,                 &
+                                     id_dim_zs_xz(av), 542 )
+             CALL netcdf_create_var( id_set_xz(av), (/ id_dim_zs_xz(av) /),    &
+                                     'zs', NF90_DOUBLE, id_var_zs_xz(av),      &
+                                     'meters', '', 543, 544, 000 )
+
+          ENDIF
+
+!
+!--       Define the variables
+          var_list = ';'
+          i = 1
+
+          DO WHILE ( do2d(av,i)(1:1) /= ' ' )
+
+             IF ( INDEX( do2d(av,i), 'xz' ) /= 0 )  THEN
+
+!
+!--             Check for the grid
+                found = .TRUE.
+                SELECT CASE ( do2d(av,i) )
+!
+!--                Most variables are defined on the zu grid
+                   CASE ( 'e_xz', 'lpt_xz', 'nr_xz', 'p_xz', 'pc_xz', 'pr_xz', &
+                          'prr_xz', 'pt_xz', 'q_xz', 'qc_xz', 'ql_xz',         &
+                          'ql_c_xz', 'ql_v_xz', 'ql_vp_xz', 'qr_xz', 'qv_xz',  &
+                          'rad_lw_cs_hr_xz', 'rad_lw_hr_xz',                   &
+                          'rad_sw_cs_hr_xz', 'rad_sw_hr_xz''rho_xz', 's_xz',   &
+                          'sa_xz', 'vpt_xz' )
+
+                      grid_x = 'x'
+                      grid_y = 'y'
+                      grid_z = 'zu'
+!
+!--                u grid
+                   CASE ( 'u_xz' )
+
+                      grid_x = 'xu'
+                      grid_y = 'y'
+                      grid_z = 'zu'
+!
+!--                v grid
+                   CASE ( 'v_xz' )
+
+                      grid_x = 'x'
+                      grid_y = 'yv'
+                      grid_z = 'zu'
+!
+!--                w grid
+                   CASE ( 'rad_lw_in_xz', 'rad_lw_out_xz', 'rad_sw_in_xz',     &
+                          'rad_sw_out_xz', 'w_xz' )
+
+                      grid_x = 'x'
+                      grid_y = 'y'
+                      grid_z = 'zw'
+
+!
+!--                soil grid
+                   CASE ( 'm_soil_xz', 't_soil_xz' )
+
+                      grid_x = 'x'
+                      grid_y = 'y'
+                      grid_z = 'zs'
+
+                   CASE DEFAULT
+!
+!--                   Check for user-defined quantities
+                      CALL user_define_netcdf_grid( do2d(av,i), found, &
+                                                    grid_x, grid_y, grid_z )
+                      IF ( .NOT. found )  THEN
+                         WRITE ( message_string, * ) 'no grid defined for', &
+                                                ' variable ', TRIM( do2d(av,i) )
+                         CALL message( 'define_netcdf_header', 'PA0244', &
+                                       0, 1, 0, 6, 0 )
+                      ENDIF
+
+                END SELECT
+
+!
+!--             Select the respective dimension ids
+                IF ( grid_x == 'x' )  THEN
+                   id_x = id_dim_x_xz(av)
+                ELSEIF ( grid_x == 'xu' )  THEN
+                   id_x = id_dim_xu_xz(av)
+                ENDIF
+
+                IF ( grid_y == 'y' )  THEN
+                   id_y = id_dim_y_xz(av)
+                ELSEIF ( grid_y == 'yv' )  THEN
+                   id_y = id_dim_yv_xz(av)
+                ENDIF
+
+                IF ( grid_z == 'zu' )  THEN
+                   id_z = id_dim_zu_xz(av)
+                ELSEIF ( grid_z == 'zw' )  THEN
+                   id_z = id_dim_zw_xz(av)
+                ELSEIF ( grid_z == 'zs' )  THEN
+                   id_z = id_dim_zs_xz(av)
+                ENDIF
+
+!
+!--             Define the grid
+                CALL netcdf_create_var( id_set_xz(av), (/ id_x, id_y, id_z,    &
+                                        id_dim_time_xz(av) /), do2d(av,i),     &
+                                        nc_precision(2), id_var_do2d(av,i),    &
+                                        TRIM( do2d_unit(av,i) ), do2d(av,i),   &
+                                        159, 160, 355 )
+
+#if defined( __netcdf4_parallel )
+
+                IF ( netcdf_data_format > 4 )  THEN
+!
+!--                Set no fill for every variable to increase performance.
+                   nc_stat = NF90_DEF_VAR_FILL( id_set_xz(av),     &
+                                                id_var_do2d(av,i), &
+                                                1, 0 )
+                   CALL netcdf_handle_error( 'netcdf_define_header', 534 )
+!
+!--                Set independent io operations for parallel io. Collective io
+!--                is only allowed in case of a 1d-decomposition along x,
+!--                because otherwise, not all PEs have output data.
+                   IF ( npey == 1 )  THEN
+                      nc_stat = NF90_VAR_PAR_ACCESS( id_set_xz(av),     &
+                                                     id_var_do2d(av,i), &
+                                                     NF90_COLLECTIVE )
+                   ELSE
+!
+!--                   Test simulations showed that the output of cross sections
+!--                   by all PEs in data_output_2d using NF90_COLLECTIVE is 
+!--                   faster than the output by the first row of PEs in
+!--                   x-direction using NF90_INDEPENDENT.
+                      nc_stat = NF90_VAR_PAR_ACCESS( id_set_xz(av),    & 
+                                                    id_var_do2d(av,i), &
+                                                    NF90_COLLECTIVE )
+!                      nc_stat = NF90_VAR_PAR_ACCESS( id_set_xz(av),     &
+!                                                     id_var_do2d(av,i), &
+!                                                     NF90_INDEPENDENT )
+                   ENDIF
+                   CALL netcdf_handle_error( 'netcdf_define_header', 449 )
+                ENDIF
+#endif
+                var_list = TRIM( var_list ) // TRIM( do2d(av,i) ) // ';'
+
+             ENDIF
+
+             i = i + 1
+
+          ENDDO
+
+!
+!--       No arrays to output. Close the netcdf file and return.
+          IF ( i == 1 )  RETURN
+
+!
+!--       Write the list of variables as global attribute (this is used by
+!--       restart runs and by combine_plot_fields)
+          nc_stat = NF90_PUT_ATT( id_set_xz(av), NF90_GLOBAL, 'VAR_LIST', &
+                                  var_list )
+          CALL netcdf_handle_error( 'netcdf_define_header', 161 )
+
+!
+!--       Set general no fill, otherwise the performance drops significantly for
+!--       parallel output.
+          nc_stat = NF90_SET_FILL( id_set_xz(av), NF90_NOFILL, oldmode )
+          CALL netcdf_handle_error( 'netcdf_define_header', 530 )
+
+!
+!--       Leave netCDF define mode
+          nc_stat = NF90_ENDDEF( id_set_xz(av) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 162 )
+
+!
+!--       These data are only written by PE0 for parallel output to increase
+!--       the performance.
+          IF ( myid == 0  .OR.  netcdf_data_format < 5 )  THEN
+
+!
+!--          Write axis data: y_xz, x, zu, zw
+             ALLOCATE( netcdf_data(1:ns) )
+
+!
+!--          Write y_xz data (shifted by +dy/2)
+             DO  i = 1, ns
+                IF( section(i,2) == -1 )  THEN
+                   netcdf_data(i) = -1.0_wp  ! section averaged along y
+                ELSE
+                   netcdf_data(i) = ( section(i,2) + 0.5_wp ) * dy
+                ENDIF
+             ENDDO
+             nc_stat = NF90_PUT_VAR( id_set_xz(av), id_var_y_xz(av), &
+                                     netcdf_data, start = (/ 1 /),   &
+                                     count = (/ ns /) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 163 )
+
+!
+!--          Write yv_xz data
+             DO  i = 1, ns
+                IF( section(i,2) == -1 )  THEN
+                   netcdf_data(i) = -1.0_wp  ! section averaged along y
+                ELSE
+                   netcdf_data(i) = section(i,2) * dy
+                ENDIF
+             ENDDO
+             nc_stat = NF90_PUT_VAR( id_set_xz(av), id_var_yv_xz(av), &
+                                     netcdf_data, start = (/ 1 /),    &
+                                     count = (/ ns /) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 375 )
+
+!
+!--          Write gridpoint number data
+             netcdf_data(1:ns) = section(1:ns,2)
+             nc_stat = NF90_PUT_VAR( id_set_xz(av), id_var_ind_y_xz(av), &
+                                     netcdf_data, start = (/ 1 /),       &
+                                     count = (/ ns /) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 164 )
+
+
+             DEALLOCATE( netcdf_data )
+
+!
+!--          Write data for x (shifted by +dx/2) and xu axis
+             ALLOCATE( netcdf_data(0:nx+1) )
+
+             DO  i = 0, nx+1
+                netcdf_data(i) = ( i + 0.5_wp ) * dx
+             ENDDO
+
+             nc_stat = NF90_PUT_VAR( id_set_xz(av), id_var_x_xz(av), &
+                                     netcdf_data, start = (/ 1 /),   &
+                                     count = (/ nx+2 /) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 165 )
+
+             DO  i = 0, nx+1
+                netcdf_data(i) = i * dx
+             ENDDO
+
+             nc_stat = NF90_PUT_VAR( id_set_xz(av), id_var_xu_xz(av), &
+                                     netcdf_data, start = (/ 1 /),    &
+                                     count = (/ nx+2 /) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 377 )
+
+             DEALLOCATE( netcdf_data )
+
+!
+!--          Write zu and zw data (vertical axes)
+             ALLOCATE( netcdf_data(0:nz+1) )
+
+             netcdf_data(0:nz+1) = zu(nzb:nzt+1)
+             nc_stat = NF90_PUT_VAR( id_set_xz(av), id_var_zu_xz(av), &
+                                     netcdf_data, start = (/ 1 /),    &
+                                     count = (/ nz+2 /) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 166 )
+
+             netcdf_data(0:nz+1) = zw(nzb:nzt+1)
+             nc_stat = NF90_PUT_VAR( id_set_xz(av), id_var_zw_xz(av), &
+                                     netcdf_data, start = (/ 1 /),    &
+                                     count = (/ nz+2 /) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 167 )
+
+!
+!--          Write zs data
+             IF ( land_surface )  THEN
+                netcdf_data(0:nzs-1) = - zs(nzb_soil:nzt_soil)
+                nc_stat = NF90_PUT_VAR( id_set_xz(av), id_var_zs_xz(av), &
+                                        netcdf_data(0:nzs), start = (/ 1 /),    &
+                                        count = (/ nzt_soil-nzb_soil+1 /) )
+               CALL netcdf_handle_error( 'netcdf_define_header', 548 )
+             ENDIF
+
+
+             DEALLOCATE( netcdf_data )
+
+          ENDIF
+
+
+       CASE ( 'xz_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_xz(av), NF90_GLOBAL, 'VAR_LIST', &
+                                  var_list_old )
+          CALL netcdf_handle_error( 'netcdf_define_header', 168 )
+
+          var_list = ';'
+          i = 1
+          DO WHILE ( do2d(av,i)(1:1) /= ' ' )
+             IF ( INDEX( do2d(av,i), 'xz' ) /= 0 )  THEN
+                var_list = TRIM( var_list ) // TRIM( do2d(av,i) ) // ';'
+             ENDIF
+             i = i + 1
+          ENDDO
+
+          IF ( av == 0 )  THEN
+             var = '(xz)'
+          ELSE
+             var = '(xz_av)'
+          ENDIF
+
+          IF ( TRIM( var_list ) /= TRIM( var_list_old ) )  THEN
+             message_string = 'netCDF file for cross-sections ' //           &
+                              TRIM( var ) // ' from previous run found,' //  &
+                              '& but this file cannot be extended due to' // &
+                              ' variable mismatch.' //                       &
+                              '&New file is created instead.'
+             CALL message( 'define_netcdf_header', 'PA0249', 0, 1, 0, 6, 0 )
+             extend = .FALSE.
+             RETURN
+          ENDIF
+
+!
+!--       Calculate the number of current sections
+          ns = 1
+          DO WHILE ( section(ns,2) /= -9999  .AND.  ns <= 100 )
+             ns = ns + 1
+          ENDDO
+          ns = ns - 1
+
+!
+!--       Get and compare the number of vertical cross sections
+          nc_stat = NF90_INQ_VARID( id_set_xz(av), 'y_xz', id_var_y_xz(av) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 169 )
+
+          nc_stat = NF90_INQUIRE_VARIABLE( id_set_xz(av), id_var_y_xz(av), &
+                                           dimids = id_dim_y_xz_old )
+          CALL netcdf_handle_error( 'netcdf_define_header', 170 )
+          id_dim_y_xz(av) = id_dim_y_xz_old(1)
+
+          nc_stat = NF90_INQUIRE_DIMENSION( id_set_xz(av), id_dim_y_xz(av), &
+                                            len = ns_old )
+          CALL netcdf_handle_error( 'netcdf_define_header', 171 )
+
+          IF ( ns /= ns_old )  THEN
+             message_string = 'netCDF file for cross-sections ' //          &
+                              TRIM( var ) // ' from previous run found,' // &
+                              '&but this file cannot be extended due to' // &
+                              ' mismatch in number of' //                   & 
+                              '&cross sections.' //                         &
+                              '&New file is created instead.'
+             CALL message( 'define_netcdf_header', 'PA0250', 0, 1, 0, 6, 0 )
+             extend = .FALSE.
+             RETURN
+          ENDIF
+
+!
+!--       Get and compare the heights of the cross sections
+          ALLOCATE( netcdf_data(1:ns_old) )
+
+          nc_stat = NF90_GET_VAR( id_set_xz(av), id_var_y_xz(av), netcdf_data )
+          CALL netcdf_handle_error( 'netcdf_define_header', 172 )
+
+          DO  i = 1, ns
+             IF ( section(i,2) /= -1 )  THEN
+                IF ( ( ( section(i,2) + 0.5 ) * dy ) /= netcdf_data(i) )  THEN
+                   message_string = 'netCDF file for cross-sections ' //     &
+                               TRIM( var ) // ' from previous run found,' // &
+                               '&but this file cannot be extended' //        &
+                               ' due to mismatch in cross' //                &
+                               '&section levels.' //                         &
+                                '&New file is created instead.'
+                   CALL message( 'define_netcdf_header', 'PA0251', &
+                                                                 0, 1, 0, 6, 0 )
+                   extend = .FALSE.
+                   RETURN
+                ENDIF
+             ELSE
+                IF ( -1.0_wp /= netcdf_data(i) )  THEN
+                   message_string = 'netCDF file for cross-sections ' //     &
+                               TRIM( var ) // ' from previous run found,' // &
+                               '&but this file cannot be extended' //        &
+                               ' due to mismatch in cross' //                &
+                               '&section levels.' //                         &
+                               '&New file is created instead.'
+                   CALL message( 'define_netcdf_header', 'PA0251', &
+                                                                 0, 1, 0, 6, 0 )
+                   extend = .FALSE.
+                   RETURN
+                ENDIF
+             ENDIF
+          ENDDO
+
+          DEALLOCATE( netcdf_data )
+
+!
+!--       Get the id of the time coordinate (unlimited coordinate) and its
+!--       last index on the file. The next time level is do2d..count+1.
+!--       The current time must be larger than the last output time
+!--       on the file.
+          nc_stat = NF90_INQ_VARID( id_set_xz(av), 'time', id_var_time_xz(av) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 173 )
+
+          nc_stat = NF90_INQUIRE_VARIABLE( id_set_xz(av), id_var_time_xz(av), &
+                                           dimids = id_dim_time_old )
+          CALL netcdf_handle_error( 'netcdf_define_header', 174 )
+          id_dim_time_xz(av) = id_dim_time_old(1)
+
+          nc_stat = NF90_INQUIRE_DIMENSION( id_set_xz(av), id_dim_time_xz(av), &
+                                            len = ntime_count )
+          CALL netcdf_handle_error( 'netcdf_define_header', 175 )
+
+!
+!--       For non-parallel output use the last output time level of the netcdf
+!--       file because the time dimension is unlimited. In case of parallel
+!--       output the variable ntime_count could get the value of 9*10E36 because
+!--       the time dimension is limited.
+          IF ( netcdf_data_format < 5 ) do2d_xz_time_count(av) = ntime_count
+
+          nc_stat = NF90_GET_VAR( id_set_xz(av), id_var_time_xz(av),    &
+                                  last_time_coordinate,                 &
+                                  start = (/ do2d_xz_time_count(av) /), &
+                                  count = (/ 1 /) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 176 )
+
+          IF ( last_time_coordinate(1) >= simulated_time )  THEN
+             message_string = 'netCDF file for cross sections ' //          &
+                              TRIM( var ) // ' 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', 'PA0252', 0, 1, 0, 6, 0 )
+             do2d_xz_time_count(av) = 0
+             extend = .FALSE.
+             RETURN
+          ENDIF
+
+          IF ( netcdf_data_format > 4 )  THEN
+!
+!--          Set needed time levels (ntdim) to 
+!--          saved time levels + to be saved time levels.
+             IF ( av == 0 )  THEN 
+                ntdim_2d_xz(0) = do2d_xz_time_count(0) + CEILING(             &
+                                 ( end_time - MAX( skip_time_do2d_xz,         &
+                                                   simulated_time_at_begin )  &
+                                 ) / dt_do2d_xz )
+             ELSE
+                ntdim_2d_xz(1) = do2d_xz_time_count(1) + CEILING(             &
+                                 ( end_time - MAX( skip_time_data_output_av,  &
+                                                   simulated_time_at_begin )  &
+                                 ) / dt_data_output_av )
+             ENDIF
+
+!
+!--          Check if the needed number of output time levels is increased
+!--          compared to the number of time levels in the existing file.
+             IF ( ntdim_2d_xz(av) > ntime_count )  THEN
+                message_string = 'netCDF file for cross sections ' // &
+                                 TRIM( var ) // ' from previous run found,' // &
+                                 '&but this file cannot be extended becaus' // &
+                                 'e the number of output time levels&has b' // &
+                                 'een increased compared to the previous s' // &
+                                 'imulation.' //                               &
+                                 '&New file is created instead.'
+                CALL message( 'define_netcdf_header', 'PA0390', 0, 1, 0, 6, 0 )
+                do2d_xz_time_count(av) = 0
+                extend = .FALSE.
+!
+!--             Recalculate the needed time levels for the new file.
+                IF ( av == 0 )  THEN
+                   ntdim_2d_xz(0) = CEILING(                            &
+                           ( end_time - MAX( skip_time_do2d_xz,         &
+                                             simulated_time_at_begin )  &
+                           ) / dt_do2d_xz )
+                ELSE
+                   ntdim_2d_xz(1) = CEILING(                            &
+                           ( end_time - MAX( skip_time_data_output_av,  &
+                                             simulated_time_at_begin )  &
+                           ) / dt_data_output_av )
+                ENDIF
+                RETURN
+             ENDIF
+          ENDIF
+
+!
+!--       Dataset seems to be extendable.
+!--       Now get the variable ids.
+          i = 1
+          DO WHILE ( do2d(av,i)(1:1) /= ' ' )
+             IF ( INDEX( do2d(av,i), 'xz' ) /= 0 )  THEN
+                nc_stat = NF90_INQ_VARID( id_set_xz(av), do2d(av,i), &
+                                          id_var_do2d(av,i) )
+                CALL netcdf_handle_error( 'netcdf_define_header', 177 )
+#if defined( __netcdf4_parallel )
+!
+!--             Set independent io operations for parallel io. Collective io
+!--             is only allowed in case of a 1d-decomposition along x, because
+!--             otherwise, not all PEs have output data.
+                IF ( netcdf_data_format > 4 )  THEN
+                   IF ( npey == 1 )  THEN
+                      nc_stat = NF90_VAR_PAR_ACCESS( id_set_xz(av),     &
+                                                     id_var_do2d(av,i), &
+                                                     NF90_COLLECTIVE )
+                   ELSE
+!
+!--                   Test simulations showed that the output of cross sections
+!--                   by all PEs in data_output_2d using NF90_COLLECTIVE is 
+!--                   faster than the output by the first row of PEs in
+!--                   x-direction using NF90_INDEPENDENT.
+                      nc_stat = NF90_VAR_PAR_ACCESS( id_set_xz(av),     &
+                                                     id_var_do2d(av,i), &
+                                                     NF90_COLLECTIVE )
+!                      nc_stat = NF90_VAR_PAR_ACCESS( id_set_xz(av),     &
+!                                                     id_var_do2d(av,i), &
+!                                                     NF90_INDEPENDENT )
+                   ENDIF
+                   CALL netcdf_handle_error( 'netcdf_define_header', 455 )
+                ENDIF
+#endif
+             ENDIF
+             i = i + 1
+          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.
+          IF ( av == 0 )  THEN
+             time_average_text = ' '
+          ELSE
+             WRITE (time_average_text, '('', '',F7.1,'' s average'')') &
+                                                            averaging_interval
+          ENDIF
+          nc_stat = NF90_REDEF( id_set_xz(av) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 433 )
+          nc_stat = NF90_PUT_ATT( id_set_xz(av), NF90_GLOBAL, 'title', &
+                                  TRIM( run_description_header ) //    &
+                                  TRIM( time_average_text ) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 178 )
+          nc_stat = NF90_ENDDEF( id_set_xz(av) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 434 )
+          message_string = 'netCDF file for cross-sections ' //           &
+                            TRIM( var ) // ' from previous run found.' // &
+                           '&This file will be extended.'
+          CALL message( 'define_netcdf_header', 'PA0253', 0, 0, 0, 6, 0 )
+
+
+       CASE ( 'yz_new' )
+
+!
+!--       Define some global attributes of the dataset
+          nc_stat = NF90_PUT_ATT( id_set_yz(av), NF90_GLOBAL, 'Conventions', &
+                                  'COARDS' )
+          CALL netcdf_handle_error( 'netcdf_define_header', 179 )
+
+          IF ( av == 0 )  THEN
+             time_average_text = ' '
+          ELSE
+             WRITE (time_average_text, '('', '',F7.1,'' s average'')') &
+                                                            averaging_interval
+          ENDIF
+          nc_stat = NF90_PUT_ATT( id_set_yz(av), NF90_GLOBAL, 'title', &
+                                  TRIM( run_description_header ) //    &
+                                  TRIM( time_average_text ) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 180 )
+          IF ( av == 1 )  THEN
+             WRITE ( time_average_text,'(F7.1,'' s avg'')' )  averaging_interval
+             nc_stat = NF90_PUT_ATT( id_set_yz(av), NF90_GLOBAL, 'time_avg', &
+                                     TRIM( time_average_text ) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 180 )
+          ENDIF
+
+!
+!--       Define time coordinate for yz sections.
+!--       For parallel output the time dimensions has to be limited, otherwise
+!--       the performance drops significantly.
+          IF ( netcdf_data_format < 5 )  THEN
+             CALL netcdf_create_dim( id_set_yz(av), 'time', NF90_UNLIMITED,    &
+                                     id_dim_time_yz(av), 181 )
+          ELSE
+             CALL netcdf_create_dim( id_set_yz(av), 'time', ntdim_2d_yz(av),   &
+                                     id_dim_time_yz(av), 526 )
+          ENDIF
+
+          CALL netcdf_create_var( id_set_yz(av), (/ id_dim_time_yz(av) /),     &
+                                  'time', NF90_DOUBLE, id_var_time_yz(av),     &
+                                  'seconds', '', 182, 183, 000 )
+!
+!--       Define the spatial dimensions and coordinates for yz-sections.
+!--       First, determine the number of vertical sections to be written.
+          IF ( section(1,3) == -9999 )  THEN
+             RETURN
+          ELSE
+             ns = 1
+             DO WHILE ( section(ns,3) /= -9999  .AND.  ns <= 100 )
+                ns = ns + 1
+             ENDDO
+             ns = ns - 1
+          ENDIF
+
+!
+!--       Define x axis (for scalar position)
+          CALL netcdf_create_dim( id_set_yz(av), 'x_yz', ns, id_dim_x_yz(av),  &
+                                  184 )
+          CALL netcdf_create_var( id_set_yz(av), (/ id_dim_x_yz(av) /),        &
+                                  'x_yz', NF90_DOUBLE, id_var_x_yz(av),        &
+                                  'meters', '', 185, 186, 000 )
+!
+!--       Define x axis (for u position)
+          CALL netcdf_create_dim( id_set_yz(av), 'xu_yz', ns,                  &
+                                  id_dim_xu_yz(av), 377 )
+          CALL netcdf_create_var( id_set_yz(av), (/ id_dim_xu_yz(av) /),       &
+                                  'xu_yz', NF90_DOUBLE, id_var_xu_yz(av),      &
+                                  'meters', '', 378, 379, 000 )
+!
+!--       Define a variable to store the layer indices of the vertical cross
+!--       sections
+          CALL netcdf_create_var( id_set_yz(av), (/ id_dim_x_yz(av) /),        &
+                                  'ind_x_yz', NF90_DOUBLE,                     &
+                                  id_var_ind_x_yz(av), 'gridpoints', '', 187,  &
+                                  188, 000 )
+!
+!--       Define y-axis (for scalar position)
+          CALL netcdf_create_dim( id_set_yz(av), 'y', ny+2, id_dim_y_yz(av),   &
+                                  189 )
+          CALL netcdf_create_var( id_set_yz(av), (/ id_dim_y_yz(av) /), 'y',   &
+                                  NF90_DOUBLE, id_var_y_yz(av), 'meters', '',  &
+                                  190, 191, 000 )
+!
+!--       Define y-axis (for v position)
+          CALL netcdf_create_dim( id_set_yz(av), 'yv', ny+2, id_dim_yv_yz(av), &
+                                  380 )
+          CALL netcdf_create_var( id_set_yz(av), (/ id_dim_yv_yz(av) /), 'yv', &
+                                  NF90_DOUBLE, id_var_yv_yz(av), 'meters', '', &
+                                  381, 382, 000 )
+!
+!--       Define the two z-axes (zu and zw)
+          CALL netcdf_create_dim( id_set_yz(av), 'zu', nz+2, id_dim_zu_yz(av), &
+                                  192 )
+          CALL netcdf_create_var( id_set_yz(av), (/ id_dim_zu_yz(av) /), 'zu', &
+                                  NF90_DOUBLE, id_var_zu_yz(av), 'meters', '', &
+                                  193, 194, 000 )
+
+          CALL netcdf_create_dim( id_set_yz(av), 'zw', nz+2, id_dim_zw_yz(av), &
+                                  195 )
+          CALL netcdf_create_var( id_set_yz(av), (/ id_dim_zw_yz(av) /), 'zw', &
+                                  NF90_DOUBLE, id_var_zw_yz(av), 'meters', '', &
+                                  196, 197, 000 )
+
+          IF ( land_surface )  THEN
+
+             CALL netcdf_create_dim( id_set_yz(av), 'zs', nzs,                 &
+                                     id_dim_zs_yz(av), 545 )
+             CALL netcdf_create_var( id_set_yz(av), (/ id_dim_zs_yz(av) /),    &
+                                     'zs', NF90_DOUBLE, id_var_zs_yz(av),      &
+                                     'meters', '', 546, 547, 000 )
+
+          ENDIF
+
+!
+!--       Define the variables
+          var_list = ';'
+          i = 1
+
+          DO WHILE ( do2d(av,i)(1:1) /= ' ' )
+
+             IF ( INDEX( do2d(av,i), 'yz' ) /= 0 )  THEN
+
+!
+!--             Check for the grid
+                found = .TRUE.
+                SELECT CASE ( do2d(av,i) )
+!
+!--                Most variables are defined on the zu grid
+                   CASE ( 'e_yz', 'lpt_yz', 'nr_yz', 'p_yz', 'pc_yz', 'pr_yz', &
+                          'prr_yz', 'pt_yz', 'q_yz', 'qc_yz', 'ql_yz',         &
+                          'ql_c_yz', 'ql_v_yz', 'ql_vp_yz', 'qr_yz', 'qv_yz',  &
+                          'rad_lw_cs_hr_yz', 'rad_lw_hr_yz',                   &
+                          'rad_sw_cs_hr_yz', 'rad_sw_hr_yz''rho_yz', 's_yz',   &
+                          'sa_yz', 'vpt_yz' )
+
+                      grid_x = 'x'
+                      grid_y = 'y'
+                      grid_z = 'zu'
+!
+!--                u grid
+                   CASE ( 'u_yz' )
+
+                      grid_x = 'xu'
+                      grid_y = 'y'
+                      grid_z = 'zu'
+!
+!--                v grid
+                   CASE ( 'v_yz' )
+
+                      grid_x = 'x'
+                      grid_y = 'yv'
+                      grid_z = 'zu'
+!
+!--                w grid
+                   CASE ( 'rad_lw_in_yz', 'rad_lw_out_yz', 'rad_sw_in_yz',     &
+                          'rad_sw_out_yz', 'w_yz' )
+
+                      grid_x = 'x'
+                      grid_y = 'y'
+                      grid_z = 'zw'
+!
+!--                soil grid
+                   CASE ( 'm_soil_yz', 't_soil_yz' )
+
+                      grid_x = 'x'
+                      grid_y = 'y'
+                      grid_z = 'zs'
+
+                   CASE DEFAULT
+!
+!--                   Check for user-defined quantities
+                      CALL user_define_netcdf_grid( do2d(av,i), found, &
+                                                    grid_x, grid_y, grid_z )
+
+                      IF ( .NOT. found )  THEN
+                         WRITE ( message_string, * ) 'no grid defined for',    &
+                                                ' variable ', TRIM( do2d(av,i) )
+                         CALL message( 'define_netcdf_header', 'PA0244',       &
+                                       0, 1, 0, 6, 0 )
+                      ENDIF
+
+                END SELECT
+
+!
+!--             Select the respective dimension ids
+                IF ( grid_x == 'x' )  THEN
+                   id_x = id_dim_x_yz(av)
+                ELSEIF ( grid_x == 'xu' )  THEN
+                   id_x = id_dim_xu_yz(av)
+                ENDIF
+
+                IF ( grid_y == 'y' )  THEN
+                   id_y = id_dim_y_yz(av)
+                ELSEIF ( grid_y == 'yv' )  THEN
+                   id_y = id_dim_yv_yz(av)
+                ENDIF
+
+                IF ( grid_z == 'zu' )  THEN
+                   id_z = id_dim_zu_yz(av)
+                ELSEIF ( grid_z == 'zw' )  THEN
+                   id_z = id_dim_zw_yz(av)
+                ELSEIF ( grid_z == 'zs' )  THEN
+                   id_z = id_dim_zs_yz(av)
+                ENDIF
+
+!
+!--             Define the grid
+                CALL netcdf_create_var( id_set_yz(av),  (/ id_x, id_y, id_z,   &
+                                        id_dim_time_yz(av) /), do2d(av,i),     &
+                                        nc_precision(3), id_var_do2d(av,i),    &
+                                        TRIM( do2d_unit(av,i) ), do2d(av,i),   &
+                                        198, 199, 356 )
+
+#if defined( __netcdf4_parallel )
+                IF ( netcdf_data_format > 4 )  THEN
+!
+!--                Set no fill for every variable to increase performance.
+                   nc_stat = NF90_DEF_VAR_FILL( id_set_yz(av),     &
+                                                id_var_do2d(av,i), &
+                                                1, 0 )
+                   CALL netcdf_handle_error( 'netcdf_define_header', 535 )
+!
+!--                Set independent io operations for parallel io. Collective io
+!--                is only allowed in case of a 1d-decomposition along y,
+!--                because otherwise, not all PEs have output data.
+                   IF ( npex == 1 )  THEN
+                      nc_stat = NF90_VAR_PAR_ACCESS( id_set_yz(av),     &
+                                                     id_var_do2d(av,i), &
+                                                     NF90_COLLECTIVE )
+                   ELSE
+!
+!--                   Test simulations showed that the output of cross sections
+!--                   by all PEs in data_output_2d using NF90_COLLECTIVE is 
+!--                   faster than the output by the first row of PEs in
+!--                   y-direction using NF90_INDEPENDENT.
+                      nc_stat = NF90_VAR_PAR_ACCESS( id_set_yz(av),     &
+                                                     id_var_do2d(av,i), &
+                                                     NF90_COLLECTIVE )
+!                      nc_stat = NF90_VAR_PAR_ACCESS( id_set_yz(av),     &
+!                                                     id_var_do2d(av,i), &
+!                                                     NF90_INDEPENDENT )
+                   ENDIF
+                   CALL netcdf_handle_error( 'netcdf_define_header', 450 )
+                ENDIF
+#endif
+                var_list = TRIM( var_list ) // TRIM( do2d(av,i) ) // ';'
+
+             ENDIF
+
+             i = i + 1
+
+          ENDDO
+
+!
+!--       No arrays to output. Close the netcdf file and return.
+          IF ( i == 1 )  RETURN
+
+!
+!--       Write the list of variables as global attribute (this is used by
+!--       restart runs and by combine_plot_fields)
+          nc_stat = NF90_PUT_ATT( id_set_yz(av), NF90_GLOBAL, 'VAR_LIST', &
+                                  var_list )
+          CALL netcdf_handle_error( 'netcdf_define_header', 200 )
+
+!
+!--       Set general no fill, otherwise the performance drops significantly for
+!--       parallel output.
+          nc_stat = NF90_SET_FILL( id_set_yz(av), NF90_NOFILL, oldmode )
+          CALL netcdf_handle_error( 'netcdf_define_header', 531 )
+
+!
+!--       Leave netCDF define mode
+          nc_stat = NF90_ENDDEF( id_set_yz(av) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 201 )
+
+!
+!--       These data are only written by PE0 for parallel output to increase
+!--       the performance.
+          IF ( myid == 0  .OR.  netcdf_data_format < 5 )  THEN
+
+!
+!--          Write axis data: x_yz, y, zu, zw
+             ALLOCATE( netcdf_data(1:ns) )
+
+!
+!--          Write x_yz data (shifted by +dx/2)
+             DO  i = 1, ns
+                IF( section(i,3) == -1 )  THEN
+                   netcdf_data(i) = -1.0_wp  ! section averaged along x
+                ELSE
+                   netcdf_data(i) = ( section(i,3) + 0.5_wp ) * dx
+                ENDIF
+             ENDDO
+             nc_stat = NF90_PUT_VAR( id_set_yz(av), id_var_x_yz(av), &
+                                     netcdf_data, start = (/ 1 /),   &
+                                     count = (/ ns /) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 202 )
+
+!
+!--          Write x_yz data (xu grid)
+             DO  i = 1, ns
+                IF( section(i,3) == -1 )  THEN
+                   netcdf_data(i) = -1.0_wp  ! section averaged along x
+                ELSE
+                   netcdf_data(i) = section(i,3) * dx
+                ENDIF
+             ENDDO
+             nc_stat = NF90_PUT_VAR( id_set_yz(av), id_var_xu_yz(av), &
+                                     netcdf_data, start = (/ 1 /),    &
+                                     count = (/ ns /) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 383 )
+
+!
+!--          Write gridpoint number data
+             netcdf_data(1:ns) = section(1:ns,3)
+             nc_stat = NF90_PUT_VAR( id_set_yz(av), id_var_ind_x_yz(av), &
+                                     netcdf_data, start = (/ 1 /),       &
+                                     count = (/ ns /) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 203 )
+
+             DEALLOCATE( netcdf_data )
+
+!
+!--          Write data for y (shifted by +dy/2) and yv axis
+             ALLOCATE( netcdf_data(0:ny+1) )
+
+             DO  j = 0, ny+1
+                netcdf_data(j) = ( j + 0.5_wp ) * dy
+             ENDDO
+
+             nc_stat = NF90_PUT_VAR( id_set_yz(av), id_var_y_yz(av), &
+                                     netcdf_data, start = (/ 1 /),   &
+                                     count = (/ ny+2 /) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 204 )
+
+             DO  j = 0, ny+1
+                netcdf_data(j) = j * dy
+             ENDDO
+
+             nc_stat = NF90_PUT_VAR( id_set_yz(av), id_var_yv_yz(av), &
+                                     netcdf_data, start = (/ 1 /),    &
+                                     count = (/ ny+2 /) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 384 )
+
+             DEALLOCATE( netcdf_data )
+
+!
+!--          Write zu and zw data (vertical axes)
+             ALLOCATE( netcdf_data(0:nz+1) )
+
+             netcdf_data(0:nz+1) = zu(nzb:nzt+1)
+             nc_stat = NF90_PUT_VAR( id_set_yz(av), id_var_zu_yz(av), &
+                                     netcdf_data, start = (/ 1 /),    &
+                                     count = (/ nz+2 /) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 205 )
+
+             netcdf_data(0:nz+1) = zw(nzb:nzt+1)
+             nc_stat = NF90_PUT_VAR( id_set_yz(av), id_var_zw_yz(av), &
+                                     netcdf_data, start = (/ 1 /),    &
+                                     count = (/ nz+2 /) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 206 )
+
+             DEALLOCATE( netcdf_data )
+
+          ENDIF
+
+
+       CASE ( 'yz_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_yz(av), NF90_GLOBAL, 'VAR_LIST', &
+                                  var_list_old )
+          CALL netcdf_handle_error( 'netcdf_define_header', 207 )
+
+          var_list = ';'
+          i = 1
+          DO WHILE ( do2d(av,i)(1:1) /= ' ' )
+             IF ( INDEX( do2d(av,i), 'yz' ) /= 0 )  THEN
+                var_list = TRIM( var_list ) // TRIM( do2d(av,i) ) // ';'
+             ENDIF
+             i = i + 1
+          ENDDO
+
+          IF ( av == 0 )  THEN
+             var = '(yz)'
+          ELSE
+             var = '(yz_av)'
+          ENDIF
+
+          IF ( TRIM( var_list ) /= TRIM( var_list_old ) )  THEN
+             message_string = 'netCDF file for cross-sections ' //           &
+                              TRIM( var ) // ' from previous run found,' //  &
+                              '& but this file cannot be extended due to' // &
+                              ' variable mismatch.' //                       & 
+                              '&New file is created instead.'
+             CALL message( 'define_netcdf_header', 'PA0249', 0, 1, 0, 6, 0 )
+             extend = .FALSE.
+             RETURN
+          ENDIF
+
+!
+!--       Calculate the number of current sections
+          ns = 1
+          DO WHILE ( section(ns,3) /= -9999  .AND.  ns <= 100 )
+             ns = ns + 1
+          ENDDO
+          ns = ns - 1
+
+!
+!--       Get and compare the number of vertical cross sections
+          nc_stat = NF90_INQ_VARID( id_set_yz(av), 'x_yz', id_var_x_yz(av) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 208 )
+
+          nc_stat = NF90_INQUIRE_VARIABLE( id_set_yz(av), id_var_x_yz(av), &
+                                           dimids = id_dim_x_yz_old )
+          CALL netcdf_handle_error( 'netcdf_define_header', 209 )
+          id_dim_x_yz(av) = id_dim_x_yz_old(1)
+
+          nc_stat = NF90_INQUIRE_DIMENSION( id_set_yz(av), id_dim_x_yz(av), &
+                                            len = ns_old )
+          CALL netcdf_handle_error( 'netcdf_define_header', 210 )
+
+          IF ( ns /= ns_old )  THEN
+             message_string = 'netCDF file for cross-sections ' //          &
+                              TRIM( var ) // ' from previous run found,' // &
+                              '&but this file cannot be extended due to' // &
+                              ' mismatch in number of' //                   &
+                              '&cross sections.' //                         &
+                              '&New file is created instead.'
+             CALL message( 'define_netcdf_header', 'PA0250', 0, 1, 0, 6, 0 )
+             extend = .FALSE.
+             RETURN
+          ENDIF
+
+!
+!--       Get and compare the heights of the cross sections
+          ALLOCATE( netcdf_data(1:ns_old) )
+
+          nc_stat = NF90_GET_VAR( id_set_yz(av), id_var_x_yz(av), netcdf_data )
+          CALL netcdf_handle_error( 'netcdf_define_header', 211 )
+
+          DO  i = 1, ns
+             IF ( section(i,3) /= -1 )  THEN
+                IF ( ( ( section(i,3) + 0.5 ) * dx ) /= netcdf_data(i) )  THEN
+                   message_string = 'netCDF file for cross-sections ' //    &
+                              TRIM( var ) // ' from previous run found,' // &
+                              '&but this file cannot be extended' //        &
+                              ' due to mismatch in cross' //                &
+                              '&section levels.' //                         &
+                             '&New file is created instead.'
+                   CALL message( 'define_netcdf_header', 'PA0251', &
+                                                                 0, 1, 0, 6, 0 )
+                   extend = .FALSE.
+                   RETURN
+                ENDIF
+             ELSE
+                IF ( -1.0_wp /= netcdf_data(i) )  THEN
+                   message_string = 'netCDF file for cross-sections ' //    &
+                              TRIM( var ) // ' from previous run found,' // &
+                              '&but this file cannot be extended' //        &
+                              ' due to mismatch in cross' //                &
+                              '&section levels.' //                         &
+                              '&New file is created instead.'
+                   CALL message( 'define_netcdf_header', 'PA0251', &
+                                                                 0, 1, 0, 6, 0 )
+                   extend = .FALSE.
+                   RETURN
+                ENDIF
+             ENDIF
+          ENDDO
+
+          DEALLOCATE( netcdf_data )
+
+!
+!--       Get the id of the time coordinate (unlimited coordinate) and its
+!--       last index on the file. The next time level is pl2d..count+1.
+!--       The current time must be larger than the last output time
+!--       on the file.
+          nc_stat = NF90_INQ_VARID( id_set_yz(av), 'time', id_var_time_yz(av) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 212 )
+
+          nc_stat = NF90_INQUIRE_VARIABLE( id_set_yz(av), id_var_time_yz(av), &
+                                           dimids = id_dim_time_old )
+          CALL netcdf_handle_error( 'netcdf_define_header', 213 )
+          id_dim_time_yz(av) = id_dim_time_old(1)
+
+          nc_stat = NF90_INQUIRE_DIMENSION( id_set_yz(av), id_dim_time_yz(av), &
+                                            len = ntime_count )
+          CALL netcdf_handle_error( 'netcdf_define_header', 214 )
+
+!
+!--       For non-parallel output use the last output time level of the netcdf
+!--       file because the time dimension is unlimited. In case of parallel
+!--       output the variable ntime_count could get the value of 9*10E36 because
+!--       the time dimension is limited.
+          IF ( netcdf_data_format < 5 ) do2d_yz_time_count(av) = ntime_count
+
+          nc_stat = NF90_GET_VAR( id_set_yz(av), id_var_time_yz(av),    &
+                                  last_time_coordinate,                 &
+                                  start = (/ do2d_yz_time_count(av) /), &
+                                  count = (/ 1 /) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 215 )
+
+          IF ( last_time_coordinate(1) >= simulated_time )  THEN
+             message_string = 'netCDF file for cross sections ' //          &
+                              TRIM( var ) // ' 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', 'PA0252', 0, 1, 0, 6, 0 )
+             do2d_yz_time_count(av) = 0
+             extend = .FALSE.
+             RETURN
+          ENDIF
+
+          IF ( netcdf_data_format > 4 )  THEN
+!
+!--          Set needed time levels (ntdim) to 
+!--          saved time levels + to be saved time levels.
+             IF ( av == 0 )  THEN 
+                ntdim_2d_yz(0) = do2d_yz_time_count(0) + CEILING(             &
+                                 ( end_time - MAX( skip_time_do2d_yz,         &
+                                                   simulated_time_at_begin )  &
+                                 ) / dt_do2d_yz )
+             ELSE
+                ntdim_2d_yz(1) = do2d_yz_time_count(1) + CEILING(             &
+                                 ( end_time - MAX( skip_time_data_output_av,  &
+                                                   simulated_time_at_begin )  &
+                                 ) / dt_data_output_av )
+             ENDIF
+
+!
+!--          Check if the needed number of output time levels is increased
+!--          compared to the number of time levels in the existing file.
+             IF ( ntdim_2d_yz(av) > ntime_count )  THEN
+                message_string = 'netCDF file for cross sections ' //          &
+                                 TRIM( var ) // ' from previous run found,' // &
+                                 '&but this file cannot be extended becaus' // &
+                                 'e the number of output time levels&has b' // &
+                                 'een increased compared to the previous s' // &
+                                 'imulation.' //                               &
+                                 '&New file is created instead.'
+                CALL message( 'define_netcdf_header', 'PA0391', 0, 1, 0, 6, 0 )
+                do2d_yz_time_count(av) = 0
+                extend = .FALSE.
+!
+!--             Recalculate the needed time levels for the new file.
+                IF ( av == 0 )  THEN
+                   ntdim_2d_yz(0) = CEILING(                            &
+                           ( end_time - MAX( skip_time_do2d_yz,         &
+                                             simulated_time_at_begin )  &
+                           ) / dt_do2d_yz )
+                ELSE
+                   ntdim_2d_yz(1) = CEILING(                            &
+                           ( end_time - MAX( skip_time_data_output_av,  &
+                                             simulated_time_at_begin )  &
+                           ) / dt_data_output_av )
+                ENDIF
+                RETURN
+             ENDIF
+          ENDIF
+
+!
+!--       Dataset seems to be extendable.
+!--       Now get the variable ids.
+          i = 1
+          DO WHILE ( do2d(av,i)(1:1) /= ' ' )
+             IF ( INDEX( do2d(av,i), 'yz' ) /= 0 )  THEN
+                nc_stat = NF90_INQ_VARID( id_set_yz(av), do2d(av,i), &
+                                          id_var_do2d(av,i) )
+                CALL netcdf_handle_error( 'netcdf_define_header', 216 )
+#if defined( __netcdf4_parallel )
+!
+!--             Set independent io operations for parallel io. Collective io
+!--             is only allowed in case of a 1d-decomposition along y, because
+!--             otherwise, not all PEs have output data.
+                IF ( netcdf_data_format > 4 )  THEN
+                   IF ( npex == 1 )  THEN
+                      nc_stat = NF90_VAR_PAR_ACCESS( id_set_yz(av),     &
+                                                     id_var_do2d(av,i), &
+                                                     NF90_COLLECTIVE )
+                   ELSE
+!
+!--                   Test simulations showed that the output of cross sections
+!--                   by all PEs in data_output_2d using NF90_COLLECTIVE is 
+!--                   faster than the output by the first row of PEs in
+!--                   y-direction using NF90_INDEPENDENT.
+                      nc_stat = NF90_VAR_PAR_ACCESS( id_set_yz(av),     &
+                                                     id_var_do2d(av,i), &
+                                                     NF90_COLLECTIVE )
+!                      nc_stat = NF90_VAR_PAR_ACCESS( id_set_yz(av),     &
+!                                                     id_var_do2d(av,i), &
+!                                                     NF90_INDEPENDENT )
+                   ENDIF
+                   CALL netcdf_handle_error( 'netcdf_define_header', 450 )
+                ENDIF
+#endif
+             ENDIF
+             i = i + 1
+          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.
+          IF ( av == 0 )  THEN
+             time_average_text = ' '
+          ELSE
+             WRITE (time_average_text, '('', '',F7.1,'' s average'')') &
+                                                            averaging_interval
+          ENDIF
+          nc_stat = NF90_REDEF( id_set_yz(av) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 435 )
+          nc_stat = NF90_PUT_ATT( id_set_yz(av), NF90_GLOBAL, 'title', &
+                                  TRIM( run_description_header ) //    &
+                                  TRIM( time_average_text ) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 217 )
+          nc_stat = NF90_ENDDEF( id_set_yz(av) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 436 )
+          message_string = 'netCDF file for cross-sections ' //           &
+                            TRIM( var ) // ' from previous run found.' // &
+                           '&This file will be extended.'
+          CALL message( 'define_netcdf_header', 'PA0253', 0, 0, 0, 6, 0 )
+
+
+       CASE ( 'pr_new' )
+
+!
+!--       Define some global attributes of the dataset
+          IF ( averaging_interval_pr /= 0.0_wp )  THEN
+             WRITE (time_average_text,'('', '',F7.1,'' s average'')') &
+                                                            averaging_interval_pr
+             nc_stat = NF90_PUT_ATT( id_set_pr, NF90_GLOBAL, 'title',   &
+                                     TRIM( run_description_header ) //  &
+                                     TRIM( time_average_text ) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 218 )
+
+             WRITE ( time_average_text,'(F7.1,'' s avg'')' ) averaging_interval_pr
+             nc_stat = NF90_PUT_ATT( id_set_pr, NF90_GLOBAL, 'time_avg', &
+                                     TRIM( time_average_text ) )
+          ELSE
+             nc_stat = NF90_PUT_ATT( id_set_pr, NF90_GLOBAL, 'title', &
+                                     TRIM( run_description_header ) )
+          ENDIF
+          CALL netcdf_handle_error( 'netcdf_define_header', 219 )
+
+!
+!--       Write number of columns and rows of coordinate systems to be plotted
+!--       on one page to the netcdf header.
+!--       This information can be used by palmplot.
+          nc_stat = NF90_PUT_ATT( id_set_pr, NF90_GLOBAL,                     &
+                                  'no_rows',                                  & 
+                                  profile_rows ) 
+          CALL netcdf_handle_error( 'netcdf_define_header', 519 )
+
+          nc_stat = NF90_PUT_ATT( id_set_pr, NF90_GLOBAL,                     &
+                                  'no_columns',                               & 
+                                  profile_columns ) 
+          CALL netcdf_handle_error( 'netcdf_define_header', 520 )
+
+
+          cross_profiles_adj  = ADJUSTL( cross_profiles )
+          cross_profiles_numb = 999999
+          cross_profiles_char = ''
+
+!
+!--       Each profile defined in cross_profiles is written to an array
+!--       (cross_profiles_char). The number of the respective coordinate
+!--       system is assigned in a second array (cross_profiles_numb).
+          k = 1
+
+          DO  i = 1, crmax
+
+             IF ( TRIM( cross_profiles_adj(i) ) == ' ' )  EXIT
+             delim_old = 0
+
+             DO   j = 1, crmax
+                delim = INDEX( cross_profiles_adj(i)(delim_old+1:), ' ' )
+                IF ( delim == 1 )  EXIT
+                kk = MIN( crmax, k )
+                cross_profiles_char(kk) = cross_profiles_adj(i)(delim_old+1: &
+                                                              delim_old+delim-1)
+                cross_profiles_numb(kk) = i
+                k = k + 1
+                cross_profiles_maxi  = i
+                delim_old = delim_old + delim
+             ENDDO
+
+          ENDDO
+
+          cross_profiles_count = MIN( crmax, k-1 )
+!
+!--       Check if all profiles defined in cross_profiles are defined in 
+!--       data_output_pr. If not, they will be skipped.
+          DO  i = 1, cross_profiles_count
+             DO  j = 1, dopr_n
+
+                IF ( TRIM(cross_profiles_char(i)) == TRIM(data_output_pr(j)) ) &
+                THEN
+                   EXIT
+                ENDIF
+
+                IF ( j == dopr_n )  THEN
+                   cross_profiles_numb(i) = 999999
+                ENDIF
+
+             ENDDO
+          ENDDO
+
+          DO i = 1, crmax
+             IF ( cross_profiles_numb(i) == 999999 ) THEN
+                DO j = i + 1, crmax
+                   IF ( cross_profiles_numb(j) /= 999999 ) THEN
+                      cross_profiles_char(i) = cross_profiles_char(j)
+                      cross_profiles_numb(i) = cross_profiles_numb(j)
+                      cross_profiles_numb(j) = 999999
+                      EXIT
+                   ENDIF
+                ENDDO
+             ENDIF
+          ENDDO
+
+          DO i = 1, crmax-1
+             IF ( cross_profiles_numb(i + 1) == 999999 ) THEN
+                cross_profiles_count = i
+                EXIT
+             ENDIF
+          ENDDO
+!
+!--       Check if all profiles defined in data_output_pr are defined in 
+!--       cross_profiles. If not, they will be added to cross_profiles.
+          DO  i = 1, dopr_n
+             DO  j = 1, cross_profiles_count
+
+                IF ( TRIM(cross_profiles_char(j)) == TRIM(data_output_pr(i))) &
+                THEN
+                   EXIT
+                ENDIF
+
+                IF (( j == cross_profiles_count ) .AND.                        &
+                    ( cross_profiles_count <= crmax - 1))  THEN
+                   cross_profiles_count = cross_profiles_count + 1
+                   cross_profiles_maxi  = cross_profiles_maxi  + 1
+                   cross_profiles_char(MIN( crmax, cross_profiles_count )) =  &
+                                                      TRIM( data_output_pr(i) )
+                   cross_profiles_numb(MIN( crmax, cross_profiles_count )) =  &
+                                                      cross_profiles_maxi
+                ENDIF
+
+             ENDDO
+          ENDDO
+
+          IF ( cross_profiles_count >= crmax )  THEN
+             message_string = 'It is not allowed to arrange more than '     &
+                              // '100 profiles with & cross_profiles. Apart'&
+                              // ' from that, all profiles are saved & to ' &
+                              // 'the netCDF file.'
+             CALL message( 'define_netcdf_header', 'PA0354', 0, 0, 0, 6, 0 )
+          ENDIF
+
+!
+!--       Writing cross_profiles to netcdf header. This information can be 
+!--       used by palmplot. Each profile is separated by ",", each cross is 
+!--       separated by ";".
+          char_cross_profiles = ';'
+          id_last = 1
+          cross_profiles_count = MIN( cross_profiles_count, crmax )
+
+          DO  i = 1, cross_profiles_count
+
+             IF ( cross_profiles_numb(i) /= 999999 )  THEN
+                IF ( TRIM( char_cross_profiles ) == ';' )  THEN
+                   char_cross_profiles = TRIM( char_cross_profiles ) // &
+                                         TRIM( cross_profiles_char(i) )
+                ELSEIF ( id_last == cross_profiles_numb(i) )  THEN
+                   char_cross_profiles = TRIM( char_cross_profiles ) // &
+                                         ',' // TRIM( cross_profiles_char(i) )
+                ELSE
+                   char_cross_profiles = TRIM( char_cross_profiles ) // &
+                                         ';' // TRIM( cross_profiles_char(i) )
+                ENDIF
+                id_last = cross_profiles_numb(i)
+             ENDIF
+
+          ENDDO
+
+          char_cross_profiles = TRIM( char_cross_profiles ) // ';'
+
+          nc_stat = NF90_PUT_ATT( id_set_pr, NF90_GLOBAL, 'cross_profiles',   &
+                                  TRIM( char_cross_profiles ) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 521 )
+
+!
+!--       Define time coordinate for profiles (unlimited dimension)
+          CALL netcdf_create_dim( id_set_pr, 'time', NF90_UNLIMITED,           &
+                                  id_dim_time_pr, 220 )
+          CALL netcdf_create_var( id_set_pr, (/ id_dim_time_pr /), 'time',     &
+                                  NF90_DOUBLE, id_var_time_pr, 'seconds', '',  &
+                                  221, 222, 000 )
+!
+!--       Define the variables
+          var_list = ';'
+          DO  i = 1, dopr_n
+
+             IF ( statistic_regions == 0 )  THEN
+
+!
+!--             Define the z-axes (each variable gets its own z-axis)
+                CALL netcdf_create_dim( id_set_pr,                             &
+                                        'z' // TRIM( data_output_pr(i) ),      &
+                                        nzt+2-nzb, id_dim_z_pr(i,0), 223 )
+                CALL netcdf_create_var( id_set_pr, (/ id_dim_z_pr(i,0) /),     &
+                                        'z' // TRIM( data_output_pr(i) ),      &
+                                       NF90_DOUBLE, id_var_z_pr(i,0),          &
+                                       'meters', '', 224, 225, 000 )
+!
+!--             Define the variable
+                CALL netcdf_create_var( id_set_pr, (/ id_dim_z_pr(i,0),        &
+                                        id_dim_time_pr /), data_output_pr(i),  &
+                                        nc_precision(5), id_var_dopr(i,0),     &
+                                        TRIM( dopr_unit(i) ),                  &
+                                        TRIM( data_output_pr(i) ), 226, 227,   &
+                                        228 )
+
+                var_list = TRIM( var_list ) // TRIM( data_output_pr(i) ) //  ';'
+
+             ELSE
+!
+!--             If statistic regions are defined, add suffix _SR+#SR to the
+!--             names
+                DO  j = 0, statistic_regions
+                   WRITE ( suffix, '(''_'',I1)' )  j
+
+!
+!--                Define the z-axes (each variable gets it own z-axis)
+                   CALL netcdf_create_dim( id_set_pr, 'z' //                   &
+                                           TRIM(data_output_pr(i)) // suffix,  &
+                                           nzt+2-nzb, id_dim_z_pr(i,j), 229 )
+                   CALL netcdf_create_var( id_set_pr, (/ id_dim_z_pr(i,j) /),  &
+                                           'z' // TRIM(data_output_pr(i)) //   &
+                                           suffix, NF90_DOUBLE,                &
+                                           id_var_z_pr(i,j), 'meters', '',     &
+                                           230, 231, 000 )
+!
+!--                Define the variable
+                   CALL netcdf_create_var( id_set_pr, (/ id_dim_z_pr(i,j),     &
+                                           id_dim_time_pr /),                  &
+                                           TRIM(data_output_pr(i)) // suffix,  &
+                                           nc_precision(5), id_var_dopr(i,j),  &
+                                           TRIM( dopr_unit(i) ),               &
+                                           TRIM( data_output_pr(i) ) //        &
+                                           ' SR ', 232, 233, 234 )
+
+                   var_list = TRIM( var_list ) // TRIM( data_output_pr(i) ) // &
+                              suffix // ';'
+
+                ENDDO
+
+             ENDIF
+
+          ENDDO
+
+!
+!--       Write the list of variables as global attribute (this is used by
+!--       restart runs)
+          nc_stat = NF90_PUT_ATT( id_set_pr, NF90_GLOBAL, 'VAR_LIST', var_list )
+          CALL netcdf_handle_error( 'netcdf_define_header', 235 )
+
+!
+!--       Define normalization variables (as time series)
+          DO  i = 1, dopr_norm_num
+
+             CALL netcdf_create_var( id_set_pr, (/ id_dim_time_pr /),          &
+                                     'NORM_' // TRIM( dopr_norm_names(i) ),    &
+                                     nc_precision(5), id_var_norm_dopr(i),     &
+                                     '', TRIM( dopr_norm_longnames(i) ), 236,  &
+                                     000, 237 )
+
+          ENDDO
+
+!
+!--       Leave netCDF define mode
+          nc_stat = NF90_ENDDEF( id_set_pr )
+          CALL netcdf_handle_error( 'netcdf_define_header', 238 )
+
+!
+!--       Write z-axes data
+          DO  i = 1, dopr_n
+             DO  j = 0, statistic_regions
+
+                nc_stat = NF90_PUT_VAR( id_set_pr, id_var_z_pr(i,j),      &
+                                        hom(nzb:nzt+1,2,dopr_index(i),0), &
+                                        start = (/ 1 /),                  &
+                                        count = (/ nzt-nzb+2 /) )
+                CALL netcdf_handle_error( 'netcdf_define_header', 239 )
+
+             ENDDO
+          ENDDO
+
+
+       CASE ( 'pr_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_pr, NF90_GLOBAL, 'VAR_LIST', &
+                                  var_list_old )
+          CALL netcdf_handle_error( 'netcdf_define_header', 240 )
+
+          var_list = ';'
+          DO  i = 1, dopr_n
+
+             IF ( statistic_regions == 0 )  THEN
+                var_list = TRIM( var_list ) // TRIM( data_output_pr(i) ) // ';'
+             ELSE
+                DO  j = 0, statistic_regions
+                   WRITE ( suffix, '(''_'',I1)' )  j
+                   var_list = TRIM( var_list ) // TRIM( data_output_pr(i) ) // &
+                              suffix // ';'
+                ENDDO
+             ENDIF
+
+          ENDDO
+
+          IF ( TRIM( var_list ) /= TRIM( var_list_old ) )  THEN
+             message_string = 'netCDF file for vertical profiles ' //        &
+                              'from previous run found,' //                  &
+                              '& but this file cannot be extended due to' // &
+                              ' variable mismatch.' //                       &
+                              '&New file is created instead.'
+             CALL message( 'define_netcdf_header', 'PA0254', 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 dopr..count+1.
+!--       The current time must be larger than the last output time
+!--       on the file.
+          nc_stat = NF90_INQ_VARID( id_set_pr, 'time', id_var_time_pr )
+          CALL netcdf_handle_error( 'netcdf_define_header', 241 )
+
+          nc_stat = NF90_INQUIRE_VARIABLE( id_set_pr, id_var_time_pr, &
+                                           dimids = id_dim_time_old )
+          CALL netcdf_handle_error( 'netcdf_define_header', 242 )
+          id_dim_time_pr = id_dim_time_old(1)
+
+          nc_stat = NF90_INQUIRE_DIMENSION( id_set_pr, id_dim_time_pr, &
+                                            len = dopr_time_count )
+          CALL netcdf_handle_error( 'netcdf_define_header', 243 )
+
+          nc_stat = NF90_GET_VAR( id_set_pr, id_var_time_pr,        &
+                                  last_time_coordinate,             &
+                                  start = (/ dopr_time_count /), &
+                                  count = (/ 1 /) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 244 )
+
+          IF ( last_time_coordinate(1) >= simulated_time )  THEN
+             message_string = 'netCDF file for vertical profiles ' //       &
+                              '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', 'PA0255', 0, 1, 0, 6, 0 )
+             dopr_time_count = 0
+             extend = .FALSE.
+             RETURN
+          ENDIF
+
+!
+!--       Dataset seems to be extendable.
+!--       Now get the variable ids.
+          i = 1
+          DO  i = 1, dopr_n
+ 
+             IF ( statistic_regions == 0 )  THEN
+                nc_stat = NF90_INQ_VARID( id_set_pr, data_output_pr(i), &
+                                          id_var_dopr(i,0) )
+                CALL netcdf_handle_error( 'netcdf_define_header', 245 )
+             ELSE
+                DO  j = 0, statistic_regions
+                   WRITE ( suffix, '(''_'',I1)' )  j
+                   netcdf_var_name = TRIM( data_output_pr(i) ) // suffix
+                   nc_stat = NF90_INQ_VARID( id_set_pr, netcdf_var_name, &
+                                             id_var_dopr(i,j) )
+                   CALL netcdf_handle_error( 'netcdf_define_header', 246 )
+                ENDDO
+             ENDIF
+
+          ENDDO
+
+!
+!--       Get ids of the normalization variables
+          DO  i = 1, dopr_norm_num
+             nc_stat = NF90_INQ_VARID( id_set_pr,                             &
+                                       'NORM_' // TRIM( dopr_norm_names(i) ), &
+                                       id_var_norm_dopr(i) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 247 )
+          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.
+          IF ( averaging_interval_pr == 0.0_wp )  THEN
+             time_average_text = ' '
+          ELSE
+             WRITE (time_average_text, '('', '',F7.1,'' s average'')') &
+                                                            averaging_interval_pr
+          ENDIF
+          nc_stat = NF90_REDEF( id_set_pr )
+          CALL netcdf_handle_error( 'netcdf_define_header', 437 )
+          nc_stat = NF90_PUT_ATT( id_set_pr, NF90_GLOBAL, 'title', &
+                                  TRIM( run_description_header ) //    &
+                                  TRIM( time_average_text ) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 248 )
+
+          nc_stat = NF90_ENDDEF( id_set_pr )
+          CALL netcdf_handle_error( 'netcdf_define_header', 438 )
+          message_string = 'netCDF file for vertical profiles ' // &
+                           'from previous run found.' //           &
+                           '&This file will be extended.'
+          CALL message( 'define_netcdf_header', 'PA0256', 0, 0, 0, 6, 0 )
+
+
+       CASE ( 'ts_new' )
+
+!
+!--       Define some global attributes of the dataset
+          nc_stat = NF90_PUT_ATT( id_set_ts, NF90_GLOBAL, 'title', &
+                                  TRIM( run_description_header ) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 249 )
+
+!
+!--       Define time coordinate for time series (unlimited dimension)
+          CALL netcdf_create_dim( id_set_ts, 'time', NF90_UNLIMITED,           &
+                                  id_dim_time_ts, 250 )
+          CALL netcdf_create_var( id_set_ts, (/ id_dim_time_ts /), 'time',     &
+                                  NF90_DOUBLE, id_var_time_ts, 'seconds', '',  &
+                                  251, 252, 000 )
+!
+!--       Define the variables
+          var_list = ';'
+          DO  i = 1, dots_num
+
+             IF ( statistic_regions == 0 )  THEN
+
+                CALL netcdf_create_var( id_set_ts, (/ id_dim_time_ts /),       &
+                                        dots_label(i), nc_precision(6),        &
+                                        id_var_dots(i,0),                      &
+                                        TRIM( dots_unit(i) ),                  &
+                                        TRIM( dots_label(i) ), 253, 254, 255 )
+
+             ELSE
+!
+!--             If statistic regions are defined, add suffix _SR+#SR to the
+!--             names
+                DO  j = 0, statistic_regions
+                   WRITE ( suffix, '(''_'',I1)' )  j
+
+                   CALL netcdf_create_var( id_set_ts, (/ id_dim_time_ts /),    &
+                                           TRIM( dots_label(i) ) // suffix,    &
+                                           nc_precision(6), id_var_dots(i,j),  &
+                                           TRIM( dots_unit(i) ),               &
+                                           TRIM( dots_label(i) ) // ' SR ' //  &
+                                           suffix(2:2), 256, 257, 347)
+
+                ENDDO
+
+             ENDIF
+
+          ENDDO
+
+!
+!--       Write the list of variables as global attribute (this is used by
+!--       restart runs)
+          nc_stat = NF90_PUT_ATT( id_set_ts, NF90_GLOBAL, 'VAR_LIST', var_list )
+          CALL netcdf_handle_error( 'netcdf_define_header', 258 )
+
+!
+!--       Leave netCDF define mode
+          nc_stat = NF90_ENDDEF( id_set_ts )
+          CALL netcdf_handle_error( 'netcdf_define_header', 259 )
+
+
+       CASE ( 'ts_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_ts, NF90_GLOBAL, 'VAR_LIST', &
+                                  var_list_old )
+          CALL netcdf_handle_error( 'netcdf_define_header', 260 )
+
+          var_list = ';'
+          i = 1
+          DO  i = 1, dots_num
+
+             IF ( statistic_regions == 0 )  THEN
+                var_list = TRIM( var_list ) // TRIM( dots_label(i) ) // ';'
+             ELSE
+                DO  j = 0, statistic_regions
+                   WRITE ( suffix, '(''_'',I1)' )  j
+                   var_list = TRIM( var_list ) // TRIM( dots_label(i) ) // &
+                              suffix // ';'
+                ENDDO
+             ENDIF
+
+          ENDDO
+
+          IF ( TRIM( var_list ) /= TRIM( var_list_old ) )  THEN
+             message_string = 'netCDF file for 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 dots..count+1.
+!--       The current time must be larger than the last output time
+!--       on the file.
+          nc_stat = NF90_INQ_VARID( id_set_ts, 'time', id_var_time_ts )
+          CALL netcdf_handle_error( 'netcdf_define_header', 261 )
+
+          nc_stat = NF90_INQUIRE_VARIABLE( id_set_ts, id_var_time_ts, &
+                                           dimids = id_dim_time_old )
+          CALL netcdf_handle_error( 'netcdf_define_header', 262 )
+          id_dim_time_ts = id_dim_time_old(1)
+
+          nc_stat = NF90_INQUIRE_DIMENSION( id_set_ts, id_dim_time_ts, &
+                                            len = dots_time_count )
+          CALL netcdf_handle_error( 'netcdf_define_header', 263 )
+
+          nc_stat = NF90_GET_VAR( id_set_ts, id_var_time_ts,        &
+                                  last_time_coordinate,             &
+                                  start = (/ dots_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 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 )
+             dots_time_count = 0
+             extend = .FALSE.
+             RETURN
+          ENDIF
+
+!
+!--       Dataset seems to be extendable.
+!--       Now get the variable ids
+          i = 1
+          DO  i = 1, dots_num
+ 
+             IF ( statistic_regions == 0 )  THEN
+                nc_stat = NF90_INQ_VARID( id_set_ts, dots_label(i), &
+                                          id_var_dots(i,0) )
+                CALL netcdf_handle_error( 'netcdf_define_header', 265 )
+             ELSE
+                DO  j = 0, statistic_regions
+                   WRITE ( suffix, '(''_'',I1)' )  j
+                   netcdf_var_name = TRIM( dots_label(i) ) // suffix
+                   nc_stat = NF90_INQ_VARID( id_set_ts, netcdf_var_name, &
+                                             id_var_dots(i,j) )
+                   CALL netcdf_handle_error( 'netcdf_define_header', 266 )
+                ENDDO
+             ENDIF
+
+          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_ts )
+          CALL netcdf_handle_error( 'netcdf_define_header', 439 )
+          nc_stat = NF90_PUT_ATT( id_set_ts, NF90_GLOBAL, 'title', &
+                                  TRIM( run_description_header ) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 267 )
+          nc_stat = NF90_ENDDEF( id_set_ts )
+          CALL netcdf_handle_error( 'netcdf_define_header', 440 )
+          message_string = 'netCDF file for time series ' // &
+                           'from previous run found.' //     &
+                           '&This file will be extended.'
+          CALL message( 'define_netcdf_header', 'PA0259', 0, 0, 0, 6, 0 )
+
+
+       CASE ( 'sp_new' )
+
+!
+!--       Define some global attributes of the dataset
+          IF ( averaging_interval_sp /= 0.0_wp )  THEN
+             WRITE (time_average_text,'('', '',F7.1,'' s average'')') &
+                                                            averaging_interval_sp
+             nc_stat = NF90_PUT_ATT( id_set_sp, NF90_GLOBAL, 'title',  &
+                                     TRIM( run_description_header ) // &
+                                     TRIM( time_average_text ) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 268 )
+
+             WRITE ( time_average_text,'(F7.1,'' s avg'')' )  averaging_interval_sp
+             nc_stat = NF90_PUT_ATT( id_set_sp, NF90_GLOBAL, 'time_avg', &
+                                     TRIM( time_average_text ) )
+          ELSE
+             nc_stat = NF90_PUT_ATT( id_set_sp, NF90_GLOBAL, 'title', &
+                                     TRIM( run_description_header ) )
+          ENDIF
+          CALL netcdf_handle_error( 'netcdf_define_header', 269 )
+
+!
+!--       Define time coordinate for spectra (unlimited dimension)
+          CALL netcdf_create_dim( id_set_sp, 'time', NF90_UNLIMITED,           &
+                                  id_dim_time_sp, 270 )
+          CALL netcdf_create_var( id_set_sp, (/ id_dim_time_sp /), 'time',     &
+                                  NF90_DOUBLE, id_var_time_sp, 'seconds', '',  &
+                                  271, 272, 000 )
+!
+!--       Define the spatial dimensions and coordinates for spectra.
+!--       First, determine the number of vertical levels for which spectra
+!--       are to be output.
+          ns = 1
+          DO WHILE ( comp_spectra_level(ns) /= 999999  .AND.  ns <= 100 )
+             ns = ns + 1
+          ENDDO
+          ns = ns - 1
+
+!
+!--       Define vertical coordinate grid (zu grid)
+          CALL netcdf_create_dim( id_set_sp, 'zu_sp', ns, id_dim_zu_sp, 273 )
+          CALL netcdf_create_var( id_set_sp, (/ id_dim_zu_sp /), 'zu_sp',      &
+                                  NF90_DOUBLE, id_var_zu_sp, 'meters', '',     &
+                                  274, 275, 000 )
+!
+!--       Define vertical coordinate grid (zw grid)
+          CALL netcdf_create_dim( id_set_sp, 'zw_sp', ns, id_dim_zw_sp, 276 )
+          CALL netcdf_create_var( id_set_sp, (/ id_dim_zw_sp /), 'zw_sp',      &
+                                  NF90_DOUBLE, id_var_zw_sp, 'meters', '',     &
+                                  277, 278, 000 )
+!
+!--       Define x-axis
+          CALL netcdf_create_dim( id_set_sp, 'k_x', nx/2, id_dim_x_sp, 279 )
+          CALL netcdf_create_var( id_set_sp, (/ id_dim_x_sp /), 'k_x',         &
+                                  NF90_DOUBLE, id_var_x_sp, 'm-1', '', 280,    &
+                                  281, 000 )
+!
+!--       Define y-axis
+          CALL netcdf_create_dim(id_set_sp, 'k_y', ny/2, id_dim_y_sp, 282 )
+          CALL netcdf_create_var( id_set_sp, (/ id_dim_y_sp /), 'k_y',         &
+                                  NF90_DOUBLE, id_var_y_sp, 'm-1', '', 283,    &
+                                  284, 000 )
+!
+!--       Define the variables
+          var_list = ';'
+          i = 1
+          DO WHILE ( data_output_sp(i) /= ' '  .AND.  i <= 10 )
+!
+!--          First check for the vertical grid
+             found = .TRUE.
+             SELECT CASE ( data_output_sp(i) )
+!
+!--             Most variables are defined on the zu levels
+                CASE ( 'e', 'lpt', 'nr', 'p', 'pc', 'pr', 'prr', 'pt', 'q',   &
+                       'qc', 'ql', 'ql_c', 'ql_v', 'ql_vp', 'qr', 'qv', 'rho',&
+                       's', 'sa', 'u', 'v', 'vpt' )
+
+                   grid_z = 'zu'
+!
+!--             zw levels
+                CASE ( 'w' )
+
+                   grid_z = 'zw'
+
+                CASE DEFAULT
+!
+!--                Check for user-defined quantities (found, grid_x and grid_y
+!--                are dummies)
+                   CALL user_define_netcdf_grid( data_output_sp(i), found,  &
+                                                 grid_x, grid_y, grid_z )
+
+             END SELECT
+
+             IF ( INDEX( spectra_direction(i), 'x' ) /= 0 )  THEN
+
+!
+!--             Define the variable
+                netcdf_var_name = TRIM( data_output_sp(i) ) // '_x'
+                IF ( TRIM( grid_z ) == 'zw' )  THEN
+                   CALL netcdf_create_var( id_set_sp, (/ id_dim_x_sp,          &
+                                           id_dim_zw_sp, id_dim_time_sp /),    &
+                                           netcdf_var_name, nc_precision(7),   &
+                                           id_var_dospx(i), 'unknown',         &
+                                           netcdf_var_name, 285, 286, 287 )
+                ELSE
+                   CALL netcdf_create_var( id_set_sp, (/ id_dim_x_sp,          &
+                                           id_dim_zu_sp, id_dim_time_sp /),    &
+                                           netcdf_var_name, nc_precision(7),   &
+                                           id_var_dospx(i), 'unknown',         &
+                                           netcdf_var_name, 285, 286, 287 )
+                ENDIF
+
+                var_list = TRIM( var_list ) // TRIM( netcdf_var_name ) // ';'
+
+             ENDIF
+
+             IF ( INDEX( spectra_direction(i), 'y' ) /= 0 )  THEN
+
+!
+!--             Define the variable
+                netcdf_var_name = TRIM( data_output_sp(i) ) // '_y'
+                IF ( TRIM( grid_z ) == 'zw' )  THEN
+                   CALL netcdf_create_var( id_set_sp, (/ id_dim_y_sp,          &
+                                           id_dim_zw_sp, id_dim_time_sp /),    &
+                                           netcdf_var_name, nc_precision(7),   &
+                                           id_var_dospy(i), 'unknown',         &
+                                           netcdf_var_name, 288, 289, 290 )
+                ELSE
+                   CALL netcdf_create_var( id_set_sp, (/ id_dim_y_sp,          &
+                                           id_dim_zu_sp, id_dim_time_sp /),    &
+                                           netcdf_var_name, nc_precision(7),   &
+                                           id_var_dospy(i), 'unknown',         &
+                                           netcdf_var_name, 288, 289, 290 )
+                ENDIF
+
+                var_list = TRIM( var_list ) // TRIM( netcdf_var_name ) // ';'
+
+             ENDIF
+
+             i = i + 1
+
+          ENDDO
+
+!
+!--       Write the list of variables as global attribute (this is used by
+!--       restart runs)
+          nc_stat = NF90_PUT_ATT( id_set_sp, NF90_GLOBAL, 'VAR_LIST', var_list )
+          CALL netcdf_handle_error( 'netcdf_define_header', 291 )
+
+!
+!--       Leave netCDF define mode
+          nc_stat = NF90_ENDDEF( id_set_sp )
+          CALL netcdf_handle_error( 'netcdf_define_header', 292 )
+
+!
+!--       Write axis data: zu_sp, zw_sp, k_x, k_y
+          ALLOCATE( netcdf_data(1:ns) )
+
+!
+!--       Write zu data
+          netcdf_data(1:ns) = zu( comp_spectra_level(1:ns) )
+          nc_stat = NF90_PUT_VAR( id_set_sp, id_var_zu_sp, netcdf_data, &
+                                  start = (/ 1 /), count = (/ ns /) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 293 )
+
+!
+!--       Write zw data
+          netcdf_data(1:ns) = zw( comp_spectra_level(1:ns) )
+          nc_stat = NF90_PUT_VAR( id_set_sp, id_var_zw_sp, netcdf_data, &
+                                  start = (/ 1 /), count = (/ ns /) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 294 )
+
+          DEALLOCATE( netcdf_data )
+
+!
+!--       Write data for x and y axis (wavenumbers)
+          ALLOCATE( netcdf_data(nx/2) )
+          DO  i = 1, nx/2
+             netcdf_data(i) = 2.0_wp * pi * i / ( dx * ( nx + 1 ) )
+          ENDDO
+
+          nc_stat = NF90_PUT_VAR( id_set_sp, id_var_x_sp, netcdf_data, &
+                                  start = (/ 1 /), count = (/ nx/2 /) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 295 )
+
+          DEALLOCATE( netcdf_data )
+
+          ALLOCATE( netcdf_data(ny/2) )
+          DO  i = 1, ny/2
+             netcdf_data(i) = 2.0_wp * pi * i / ( dy * ( ny + 1 ) )
+          ENDDO
+
+          nc_stat = NF90_PUT_VAR( id_set_sp, id_var_y_sp, netcdf_data, &
+                                  start = (/ 1 /), count = (/ ny/2 /) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 296 )
+
+          DEALLOCATE( netcdf_data )
+
+
+       CASE ( 'sp_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_sp, NF90_GLOBAL, 'VAR_LIST', &
+                                  var_list_old )
+          CALL netcdf_handle_error( 'netcdf_define_header', 297 )
+          var_list = ';'
+          i = 1
+          DO WHILE ( data_output_sp(i) /= ' '  .AND.  i <= 10 )
+
+             IF ( INDEX( spectra_direction(i), 'x' ) /= 0 )  THEN
+                netcdf_var_name = TRIM( data_output_sp(i) ) // '_x'
+                var_list = TRIM( var_list ) // TRIM( netcdf_var_name ) // ';'
+             ENDIF
+
+             IF ( INDEX( spectra_direction(i), 'y' ) /= 0 )  THEN
+                netcdf_var_name = TRIM( data_output_sp(i) ) // '_y'
+                var_list = TRIM( var_list ) // TRIM( netcdf_var_name ) // ';'
+             ENDIF
+
+             i = i + 1
+
+          ENDDO
+
+          IF ( TRIM( var_list ) /= TRIM( var_list_old ) )  THEN
+             message_string = 'netCDF file for spectra  ' //                 &
+                              'from previous run found,' //                  &
+                              '& but this file cannot be extended due to' // &
+                              ' variable mismatch.' //                       &
+                              '&New file is created instead.'
+             CALL message( 'define_netcdf_header', 'PA0260', 0, 1, 0, 6, 0 )
+             extend = .FALSE.
+             RETURN
+          ENDIF
+
+!
+!--       Determine the number of current vertical levels for which spectra
+!--       shall be output
+          ns = 1
+          DO WHILE ( comp_spectra_level(ns) /= 999999  .AND.  ns <= 100 )
+             ns = ns + 1
+          ENDDO
+          ns = ns - 1
+
+!
+!--       Get and compare the number of vertical levels
+          nc_stat = NF90_INQ_VARID( id_set_sp, 'zu_sp', id_var_zu_sp )
+          CALL netcdf_handle_error( 'netcdf_define_header', 298 )
+
+          nc_stat = NF90_INQUIRE_VARIABLE( id_set_sp, id_var_zu_sp, &
+                                           dimids = id_dim_zu_sp_old )
+          CALL netcdf_handle_error( 'netcdf_define_header', 299 )
+          id_dim_zu_sp = id_dim_zu_sp_old(1)
+
+          nc_stat = NF90_INQUIRE_DIMENSION( id_set_sp, id_dim_zu_sp, &
+                                            len = ns_old )
+          CALL netcdf_handle_error( 'netcdf_define_header', 300 )
+
+          IF ( ns /= ns_old )  THEN
+             message_string = 'netCDF file for spectra ' //                 &
+                              ' from previous run found,' //                &
+                              '&but this file cannot be extended due to' // &
+                              ' mismatch in number of' //                   &
+                              '&vertical levels.' //                        &
+                              '&New file is created instead.'
+             CALL message( 'define_netcdf_header', 'PA0261', 0, 1, 0, 6, 0 )
+             extend = .FALSE.
+             RETURN
+          ENDIF
+
+!
+!--       Get and compare the heights of the cross sections
+          ALLOCATE( netcdf_data(1:ns_old) )
+
+          nc_stat = NF90_GET_VAR( id_set_sp, id_var_zu_sp, netcdf_data )
+          CALL netcdf_handle_error( 'netcdf_define_header', 301 )
+
+          DO  i = 1, ns
+             IF ( zu(comp_spectra_level(i)) /= netcdf_data(i) )  THEN
+                message_string = 'netCDF file for spectra ' //                 &
+                                 ' from previous run found,' //                &
+                                 '&but this file cannot be extended due to' // &
+                                 ' mismatch in heights of' //                  &
+                                 '&vertical levels.' //                        &
+                                 '&New file is created instead.'
+                CALL message( 'define_netcdf_header', 'PA0262', 0, 1, 0, 6, 0 )
+                extend = .FALSE.
+                RETURN
+             ENDIF
+          ENDDO
+
+          DEALLOCATE( netcdf_data )
+
+!
+!--       Get the id of the time coordinate (unlimited coordinate) and its
+!--       last index on the file. The next time level is plsp..count+1.
+!--       The current time must be larger than the last output time
+!--       on the file.
+          nc_stat = NF90_INQ_VARID( id_set_sp, 'time', id_var_time_sp )
+          CALL netcdf_handle_error( 'netcdf_define_header', 302 )
+
+          nc_stat = NF90_INQUIRE_VARIABLE( id_set_sp, id_var_time_sp, &
+                                           dimids = id_dim_time_old )
+          CALL netcdf_handle_error( 'netcdf_define_header', 303 )
+          id_dim_time_sp = id_dim_time_old(1)
+
+          nc_stat = NF90_INQUIRE_DIMENSION( id_set_sp, id_dim_time_sp, &
+                                            len = dosp_time_count )
+          CALL netcdf_handle_error( 'netcdf_define_header', 304 )
+
+          nc_stat = NF90_GET_VAR( id_set_sp, id_var_time_sp,        &
+                                  last_time_coordinate,             &
+                                  start = (/ dosp_time_count /), &
+                                  count = (/ 1 /) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 305 )
+
+          IF ( last_time_coordinate(1) >= simulated_time )  THEN
+             message_string = 'netCDF file for spectra ' //                 &
+                              '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', 'PA0263', 0, 1, 0, 6, 0 )
+             dosp_time_count = 0
+             extend = .FALSE.
+             RETURN
+          ENDIF
+
+!
+!--       Dataset seems to be extendable.
+!--       Now get the variable ids.
+          i = 1
+          DO WHILE ( data_output_sp(i) /= ' '  .AND.  i <= 10 )
+
+             IF ( INDEX( spectra_direction(i), 'x' ) /= 0 )  THEN
+                netcdf_var_name = TRIM( data_output_sp(i) ) // '_x'
+                nc_stat = NF90_INQ_VARID( id_set_sp, netcdf_var_name, &
+                                          id_var_dospx(i) )
+                CALL netcdf_handle_error( 'netcdf_define_header', 306 )
+             ENDIF
+
+             IF ( INDEX( spectra_direction(i), 'y' ) /= 0 )  THEN
+                netcdf_var_name = TRIM( data_output_sp(i) ) // '_y'
+                nc_stat = NF90_INQ_VARID( id_set_sp, netcdf_var_name, &
+                                          id_var_dospy(i) )
+                CALL netcdf_handle_error( 'netcdf_define_header', 307 )
+             ENDIF
+
+             i = i + 1
+
+          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_sp )
+          CALL netcdf_handle_error( 'netcdf_define_header', 441 )
+          IF ( averaging_interval_sp /= 0.0_wp )  THEN
+             WRITE (time_average_text,'('', '',F7.1,'' s average'')') &
+                                                           averaging_interval_sp
+             nc_stat = NF90_PUT_ATT( id_set_sp, NF90_GLOBAL, 'title',  &
+                                     TRIM( run_description_header ) // &
+                                     TRIM( time_average_text ) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 308 )
+
+             WRITE ( time_average_text,'(F7.1,'' s avg'')' )  averaging_interval_sp
+             nc_stat = NF90_PUT_ATT( id_set_sp, NF90_GLOBAL, 'time_avg', &
+                                     TRIM( time_average_text ) )
+          ELSE
+             nc_stat = NF90_PUT_ATT( id_set_sp, NF90_GLOBAL, 'title', &
+                                     TRIM( run_description_header ) )
+          ENDIF
+          CALL netcdf_handle_error( 'netcdf_define_header', 309 )
+          nc_stat = NF90_ENDDEF( id_set_sp )
+          CALL netcdf_handle_error( 'netcdf_define_header', 442 )
+          message_string = 'netCDF file for spectra ' //     &
+                           'from previous run found.' //     &
+                           '&This file will be extended.'
+          CALL message( 'define_netcdf_header', 'PA0264', 0, 0, 0, 6, 0 )
+
+
+       CASE ( 'pt_new' )
+
+!
+!--       Define some global attributes of the dataset
+          nc_stat = NF90_PUT_ATT( id_set_prt, NF90_GLOBAL, 'title', &
+                                  TRIM( run_description_header ) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 310 )
+
+!
+!--       Define time coordinate for particles (unlimited dimension)
+          CALL netcdf_create_dim( id_set_prt, 'time', NF90_UNLIMITED,          &
+                                  id_dim_time_prt, 311 )
+          CALL netcdf_create_var( id_set_prt, (/ id_dim_time_prt /), 'time',   &
+                                  NF90_DOUBLE, id_var_time_prt, 'seconds', '', &
+                                  312, 313, 000 )
+!
+!--       Define particle coordinate (maximum particle number)
+          IF ( netcdf_data_format < 3 )  THEN
+             CALL netcdf_create_dim( id_set_prt, 'particle_number',            &
+                                     maximum_number_of_particles,              &
+                                     id_dim_prtnum, 314 )
+          ELSE
+!
+!--          netCDF4 allows more than one unlimited dimension
+             CALL netcdf_create_dim( id_set_prt, 'particle_number',            &
+                                     NF90_UNLIMITED, id_dim_prtnum, 314 )
+          ENDIF
+
+          CALL netcdf_create_var( id_set_prt, (/ id_dim_prtnum /),             &
+                                  'particle_number', NF90_DOUBLE,              &
+                                  id_var_prtnum, 'particle number', '', 315,   &
+                                  316, 000 )
+!
+!--       Define variable which contains the real number of particles in use
+          CALL netcdf_create_var( id_set_prt, (/ id_dim_time_prt /),           &
+                                  'real_num_of_prt', NF90_DOUBLE,              &
+                                  id_var_rnop_prt, 'particle number', '', 317, &
+                                  318, 000 )
+!
+!--       Define the variables
+          DO  i = 1, 17
+
+             CALL netcdf_create_var( id_set_prt, (/ id_dim_prtnum,             &
+                                     id_dim_time_prt /), prt_var_names(i),     &
+                                     nc_precision(8), id_var_prt(i),           &
+                                     TRIM( prt_var_units(i) ),                 &
+                                     TRIM( prt_var_names(i) ), 319, 320, 321 )
+
+          ENDDO
+
+!
+!--       Leave netCDF define mode
+          nc_stat = NF90_ENDDEF( id_set_prt )
+          CALL netcdf_handle_error( 'netcdf_define_header', 322 )
+
+
+       CASE ( 'pt_ext' )
+
+!
+!--       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_prt, 'time', id_var_time_prt )
+          CALL netcdf_handle_error( 'netcdf_define_header', 323 )
+
+          nc_stat = NF90_INQUIRE_VARIABLE( id_set_prt, id_var_time_prt, &
+                                           dimids = id_dim_time_old )
+          CALL netcdf_handle_error( 'netcdf_define_header', 324 )
+          id_dim_time_prt = id_dim_time_old(1)
+
+          nc_stat = NF90_INQUIRE_DIMENSION( id_set_prt, id_dim_time_prt, &
+                                            len = prt_time_count )
+          CALL netcdf_handle_error( 'netcdf_define_header', 325 )
+
+          nc_stat = NF90_GET_VAR( id_set_prt, id_var_time_prt,  &
+                                  last_time_coordinate,         &
+                                  start = (/ prt_time_count /), &
+                                  count = (/ 1 /) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 326 )
+
+          IF ( last_time_coordinate(1) >= simulated_time )  THEN
+             message_string = 'netCDF file for particles ' //               &
+                              '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 )
+             prt_time_count = 0
+             extend = .FALSE.
+             RETURN
+          ENDIF
+
+!
+!--       Dataset seems to be extendable.
+!--       Now get the variable ids.
+          nc_stat = NF90_INQ_VARID( id_set_prt, 'real_num_of_prt', &
+                                    id_var_rnop_prt )
+          CALL netcdf_handle_error( 'netcdf_define_header', 327 )
+
+          DO  i = 1, 17
+
+             nc_stat = NF90_INQ_VARID( id_set_prt, prt_var_names(i), &
+                                       id_var_prt(i) )
+             CALL netcdf_handle_error( 'netcdf_define_header', 328 )
+
+          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 ( 'ps_new' )
+
+!
+!--       Define some global attributes of the dataset
+          nc_stat = NF90_PUT_ATT( id_set_pts, NF90_GLOBAL, 'title', &
+                                  TRIM( run_description_header ) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 396 )
+
+!
+!--       Define time coordinate for particle time series (unlimited dimension)
+          CALL netcdf_create_dim( id_set_pts, 'time', NF90_UNLIMITED,          &
+                                  id_dim_time_pts, 397 )
+          CALL netcdf_create_var( id_set_pts, (/ id_dim_time_pts /), 'time',   &
+                                  NF90_DOUBLE, id_var_time_pts, 'seconds', '', &
+                                  398, 399, 000 )
+!
+!--       Define the variables. If more than one particle group is defined,
+!--       define seperate variables for each group
+          var_list = ';'
+          DO  i = 1, dopts_num
+
+             DO  j = 0, number_of_particle_groups
+
+                IF ( j == 0 )  THEN
+                   suffix1 = ''
+                ELSE
+                   WRITE ( suffix1, '(''_'',I2.2)' )  j
+                ENDIF
+
+                IF ( j == 0 )  THEN
+                   CALL netcdf_create_var( id_set_pts, (/ id_dim_time_pts /),  &
+                                           TRIM( dopts_label(i) ) // suffix1,  &
+                                           nc_precision(6), id_var_dopts(i,j), &
+                                           TRIM( dopts_unit(i) ),              &
+                                           TRIM( dopts_label(i) ), 400, 401,   &
+                                           402 )
+                ELSE
+                   CALL netcdf_create_var( id_set_pts, (/ id_dim_time_pts /),  &
+                                           TRIM( dopts_label(i) ) // suffix1,  &
+                                           nc_precision(6), id_var_dopts(i,j), &
+                                           TRIM( dopts_unit(i) ),              &
+                                           TRIM( dopts_label(i) ) // ' PG ' // &
+                                           suffix1(2:3), 400, 401, 402 )
+                ENDIF
+
+                var_list = TRIM( var_list ) // TRIM( dopts_label(i) ) // &
+                           suffix1 // ';'
+
+                IF ( number_of_particle_groups == 1 )  EXIT
+
+             ENDDO
+
+          ENDDO
+
+!
+!--       Write the list of variables as global attribute (this is used by
+!--       restart runs)
+          nc_stat = NF90_PUT_ATT( id_set_pts, NF90_GLOBAL, 'VAR_LIST', &
+                                  var_list )
+          CALL netcdf_handle_error( 'netcdf_define_header', 403 )
+
+
+!
+!--       Leave netCDF define mode
+          nc_stat = NF90_ENDDEF( id_set_pts )
+          CALL netcdf_handle_error( 'netcdf_define_header', 404 )
+
+
+       CASE ( 'ps_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_pts, NF90_GLOBAL, 'VAR_LIST', &
+                                  var_list_old )
+          CALL netcdf_handle_error( 'netcdf_define_header', 405 )
+
+          var_list = ';'
+          i = 1
+          DO  i = 1, dopts_num
+
+             DO  j = 0, number_of_particle_groups
+
+                IF ( j == 0 )  THEN
+                   suffix1 = ''
+                ELSE
+                   WRITE ( suffix1, '(''_'',I2.2)' )  j
+                ENDIF
+
+                var_list = TRIM( var_list ) // TRIM( dopts_label(i) ) // &
+                           suffix1 // ';'
+
+                IF ( number_of_particle_groups == 1 )  EXIT
+
+             ENDDO
+
+          ENDDO
+
+          IF ( TRIM( var_list ) /= TRIM( var_list_old ) )  THEN
+             message_string = 'netCDF file for particle 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', 'PA0267', 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 dots..count+1.
+!--       The current time must be larger than the last output time
+!--       on the file.
+          nc_stat = NF90_INQ_VARID( id_set_pts, 'time', id_var_time_pts )
+          CALL netcdf_handle_error( 'netcdf_define_header', 406 )
+
+          nc_stat = NF90_INQUIRE_VARIABLE( id_set_pts, id_var_time_pts, &
+                                           dimids = id_dim_time_old )
+          CALL netcdf_handle_error( 'netcdf_define_header', 407 )
+          id_dim_time_pts = id_dim_time_old(1)
+
+          nc_stat = NF90_INQUIRE_DIMENSION( id_set_pts, id_dim_time_pts, &
+                                            len = dopts_time_count )
+          CALL netcdf_handle_error( 'netcdf_define_header', 408 )
+
+          nc_stat = NF90_GET_VAR( id_set_pts, id_var_time_pts,    &
+                                  last_time_coordinate,           &
+                                  start = (/ dopts_time_count /), &
+                                  count = (/ 1 /) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 409 )
+
+          IF ( last_time_coordinate(1) >= simulated_time )  THEN
+             message_string = 'netCDF file for particle 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', 'PA0268', 0, 1, 0, 6, 0 )
+             dopts_time_count = 0
+             extend = .FALSE.
+             RETURN
+          ENDIF
+
+!
+!--       Dataset seems to be extendable.
+!--       Now get the variable ids
+          i = 1
+          DO  i = 1, dopts_num
+ 
+             DO  j = 0, number_of_particle_groups
+
+                IF ( j == 0 )  THEN
+                   suffix1 = ''
+                ELSE
+                   WRITE ( suffix1, '(''_'',I2.2)' )  j
+                ENDIF
+
+                netcdf_var_name = TRIM( dopts_label(i) ) // suffix1
+
+                nc_stat = NF90_INQ_VARID( id_set_pts, netcdf_var_name, &
+                                          id_var_dopts(i,j) )
+                CALL netcdf_handle_error( 'netcdf_define_header', 410 )
+
+                IF ( number_of_particle_groups == 1 )  EXIT
+
+             ENDDO
+
+          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_pts )
+          CALL netcdf_handle_error( 'netcdf_define_header', 443 )
+          nc_stat = NF90_PUT_ATT( id_set_pts, NF90_GLOBAL, 'title', &
+                                  TRIM( run_description_header ) )
+          CALL netcdf_handle_error( 'netcdf_define_header', 411 )
+          nc_stat = NF90_ENDDEF( id_set_pts )
+          CALL netcdf_handle_error( 'netcdf_define_header', 444 )
+          message_string = 'netCDF file for particle time series ' // &
+                           'from previous run found.' //              &
+                           '&This file will be extended.'
+          CALL message( 'netcdf_define_header', 'PA0269', 0, 0, 0, 6, 0 )
+
+
+       CASE DEFAULT
+
+          message_string = 'mode "' // TRIM( mode) // '" not supported'
+          CALL message( 'netcdf_define_header', 'PA0270', 0, 0, 0, 6, 0 )
+
+    END SELECT
+
+#endif
+ END SUBROUTINE netcdf_define_header
+
+
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Creates a netCDF file and give back the id. The parallel flag has to be TRUE
+!> for parallel netCDF output support.
+!------------------------------------------------------------------------------!
+ 
+ SUBROUTINE netcdf_create_file( filename , id, parallel, errno )
+#if defined( __netcdf )
+
+    USE pegrid
+
+    IMPLICIT NONE
+
+    CHARACTER (LEN=*), INTENT(IN) :: filename
+    INTEGER, INTENT(IN)           :: errno
+    INTEGER, INTENT(OUT)          :: id
+    LOGICAL, INTENT(IN)           :: parallel
+
+
+!
+!-- Create a new netCDF output file with requested netCDF format
+    IF ( netcdf_data_format == 1 )  THEN
+!
+!--    Classic netCDF format
+       nc_stat = NF90_CREATE( filename, NF90_NOCLOBBER, id )
+
+    ELSEIF ( netcdf_data_format == 2 )  THEN
+!
+!--    64bit-offset format
+       nc_stat = NF90_CREATE( filename,                                        &
+                              IOR( NF90_NOCLOBBER, NF90_64BIT_OFFSET ), id )
+
+#if defined( __netcdf4 )
+    ELSEIF ( netcdf_data_format == 3  .OR.                                     &
+             ( .NOT. parallel  .AND.  netcdf_data_format == 5 ) )  THEN
+!
+!--    netCDF4/HDF5 format
+       nc_stat = NF90_CREATE( filename, IOR( NF90_NOCLOBBER, NF90_NETCDF4 ), id )
+
+    ELSEIF ( netcdf_data_format == 4  .OR.                                     &
+             ( .NOT. parallel  .AND.  netcdf_data_format == 6 ) )  THEN
+!
+!--    netCDF4/HDF5 format with classic model flag
+       nc_stat = NF90_CREATE( filename,                                        &
+                              IOR( NF90_NOCLOBBER,                             &
+                              IOR( NF90_CLASSIC_MODEL, NF90_HDF5 ) ), id )
+
+#if defined( __netcdf4_parallel )
+    ELSEIF ( netcdf_data_format == 5  .AND.  parallel )  THEN
+!
+!--    netCDF4/HDF5 format, parallel
+       nc_stat = NF90_CREATE( filename,                                        &
+                              IOR( NF90_NOCLOBBER,                             &
+                              IOR( NF90_NETCDF4, NF90_MPIIO ) ),               &
+                              id, COMM = comm2d, INFO = MPI_INFO_NULL )
+
+    ELSEIF ( netcdf_data_format == 6  .AND.  parallel )  THEN
+!
+!--    netCDF4/HDF5 format with classic model flag, parallel
+       nc_stat = NF90_CREATE( filename,                                        &
+                              IOR( NF90_NOCLOBBER,                             &
+                              IOR( NF90_MPIIO,                                 &
+                              IOR( NF90_CLASSIC_MODEL, NF90_HDF5 ) ) ),        &
+                              id, COMM = comm2d, INFO = MPI_INFO_NULL )
+
+#endif
+#endif
+    ENDIF
+
+    CALL netcdf_handle_error( 'netcdf_create_file', errno )
+#endif
+ END SUBROUTINE netcdf_create_file
+
+
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Opens an existing netCDF file for writing and gives back the id.
+!> The parallel flag has to be TRUE for parallel netCDF output support.
+!------------------------------------------------------------------------------!
+ 
+ SUBROUTINE netcdf_open_write_file( filename, id, parallel, errno )
+#if defined( __netcdf )
+
+    USE pegrid
+
+    IMPLICIT NONE
+
+    CHARACTER (LEN=*), INTENT(IN) :: filename
+    INTEGER, INTENT(IN)           :: errno
+    INTEGER, INTENT(OUT)          :: id
+    LOGICAL, INTENT(IN)           :: parallel
+
+
+    IF ( netcdf_data_format < 5  .OR.  .NOT. parallel )  THEN
+       nc_stat = NF90_OPEN( filename, NF90_WRITE, id )
+#if defined( __netcdf4 )
+#if defined( __netcdf4_parallel )
+    ELSEIF ( netcdf_data_format > 4  .AND.  parallel )  THEN
+       nc_stat = NF90_OPEN( filename, IOR( NF90_WRITE, NF90_MPIIO ), id,  &
+                            COMM = comm2d, INFO = MPI_INFO_NULL )
+#endif
+#endif
+    ENDIF
+
+    CALL netcdf_handle_error( 'netcdf_open_write_file', errno )
+#endif
+ END SUBROUTINE netcdf_open_write_file
+
+
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Prints out a text message corresponding to the current status.
+!------------------------------------------------------------------------------!
+ 
+ SUBROUTINE netcdf_handle_error( routine_name, errno )
+#if defined( __netcdf )
+
+
+    USE control_parameters,                                                    &
+        ONLY:  message_string
+
+    IMPLICIT NONE
+
+    CHARACTER(LEN=6) ::  message_identifier
+    CHARACTER(LEN=*) ::  routine_name
+
+    INTEGER(iwp) ::  errno
+
+    IF ( nc_stat /= NF90_NOERR )  THEN
+
+       WRITE( message_identifier, '(''NC'',I4.4)' )  errno
+       message_string = TRIM( NF90_STRERROR( nc_stat ) )
+
+       CALL message( routine_name, message_identifier, 2, 2, 0, 6, 1 )
+
+    ENDIF
+
+#endif
+ END SUBROUTINE netcdf_handle_error
+
+
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Create a dimension in NetCDF file
+!------------------------------------------------------------------------------!
+
+ SUBROUTINE netcdf_create_dim(ncid, dim_name, ncdim_type, ncdim_id, error_no)
+
+#if defined( __netcdf )
+
+    USE kinds
+
+    IMPLICIT NONE
+
+    CHARACTER(LEN=*), INTENT(IN) ::  dim_name
+
+    INTEGER, INTENT(IN)  ::  error_no
+    INTEGER, INTENT(IN)  ::  ncid
+    INTEGER, INTENT(OUT) ::  ncdim_id
+    INTEGER, INTENT(IN)  ::  ncdim_type
+
+!
+!-- Define time coordinate for volume data (unlimited dimension)
+    nc_stat = NF90_DEF_DIM( ncid, dim_name, ncdim_type, ncdim_id )
+    CALL netcdf_handle_error( 'netcdf_create_dim', error_no )
+
+#endif
+
+ END SUBROUTINE netcdf_create_dim
+
+
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Create a one dimensional variable in specific units in NetCDF file
+!------------------------------------------------------------------------------!
+
+ SUBROUTINE netcdf_create_var( ncid, dim_id, var_name, var_type, var_id,       &
+                               unit_name, long_name, error_no1, error_no2,     &
+                               error_no3 )
+
+#if defined( __netcdf )
+    IMPLICIT NONE
+
+    CHARACTER(LEN=*), INTENT(IN) ::  long_name
+    CHARACTER(LEN=*), INTENT(IN) ::  unit_name
+    CHARACTER(LEN=*), INTENT(IN) ::  var_name
+
+    INTEGER, INTENT(IN)  ::  error_no1
+    INTEGER, INTENT(IN)  ::  error_no2
+    INTEGER, INTENT(IN)  ::  error_no3
+    INTEGER, INTENT(IN)  ::  ncid
+    INTEGER, INTENT(OUT) ::  var_id
+    INTEGER, INTENT(IN)  ::  var_type
+
+    INTEGER, DIMENSION(:), INTENT(IN) ::  dim_id
+
+!
+!-- Define variable
+    nc_stat = NF90_DEF_VAR( ncid, var_name, var_type, dim_id, var_id )
+    CALL netcdf_handle_error( 'netcdf_create_var', error_no1 )
+
+#if defined( __netcdf4 )
+!
+!-- Check if variable should be deflate (including shuffling)
+!-- and if it is possible (only NetCDF4 with HDF5 supports compression)
+    IF ( netcdf_data_format > 2  .AND.  netcdf_deflate > 0 )  THEN
+       nc_stat = NF90_DEF_VAR_DEFLATE( ncid, var_id, 1, 1, netcdf_deflate )
+       CALL netcdf_handle_error( 'netcdf_create_var_deflate', error_no1 )
+    ENDIF
+#endif
+!
+!-- Set unit name if set
+    IF ( unit_name /= '' )  THEN
+       nc_stat = NF90_PUT_ATT( ncid, var_id, 'units', unit_name )
+       CALL netcdf_handle_error( 'netcdf_create_var', error_no2 )
+    ENDIF
+
+!
+!-- Set long name if set
+    IF ( long_name /= '' )  THEN
+       nc_stat = NF90_PUT_ATT( ncid, var_id, 'long_name', long_name )
+       CALL netcdf_handle_error( 'netcdf_create_var', error_no3 )
+    ENDIF
+
+#endif
+ END SUBROUTINE netcdf_create_var
+
+ END MODULE netcdf_interface
Index: palm/trunk/SOURCE/palm.f90
===================================================================
--- palm/trunk/SOURCE/palm.f90	(revision 1782)
+++ palm/trunk/SOURCE/palm.f90	(revision 1783)
@@ -19,5 +19,5 @@
 ! Current revisions:
 ! -----------------
-!
+! required user interface version changed
 !
 ! Former revisions:
@@ -186,5 +186,5 @@
 
     version = 'PALM 4.0'
-    user_interface_required_revision = 'r1663'
+    user_interface_required_revision = 'r1783'
 
 #if defined( __parallel )
Index: palm/trunk/SOURCE/parin.f90
===================================================================
--- palm/trunk/SOURCE/parin.f90	(revision 1782)
+++ palm/trunk/SOURCE/parin.f90	(revision 1783)
@@ -19,5 +19,5 @@
 ! Current revisions:
 ! -----------------
-!
+! +netcdf_deflate in d3par, netcdf module and variable names changed
 !
 ! Former revisions:
@@ -206,4 +206,7 @@
 
     USE pegrid
+
+    USE netcdf_interface,                                                      &
+        ONLY:  netcdf_data_format, netcdf_deflate, netcdf_precision
 
     USE pmc_interface,                                                         &
@@ -297,12 +300,12 @@
              dt_run_control,end_time, force_print_header, mask_scale_x,        &
              mask_scale_y, mask_scale_z, mask_x, mask_y, mask_z, mask_x_loop,  &
-             mask_y_loop, mask_z_loop, netcdf_data_format, normalizing_region, &
-             npex, npey, nz_do3d, precipitation_amount_interval,               &
-             profile_columns, profile_rows, restart_time, section_xy,          &
-             section_xz, section_yz, skip_time_data_output,                    &
-             skip_time_data_output_av, skip_time_dopr, skip_time_do2d_xy,      &
-             skip_time_do2d_xz, skip_time_do2d_yz, skip_time_do3d,             &
-             skip_time_domask, synchronous_exchange, termination_time_needed,  &
-             z_max_do2d
+             mask_y_loop, mask_z_loop, netcdf_data_format, netcdf_deflate,     &
+             normalizing_region, npex, npey, nz_do3d,                          &
+             precipitation_amount_interval, profile_columns, profile_rows,     &
+             restart_time, section_xy, section_xz, section_yz,                 &
+             skip_time_data_output, skip_time_data_output_av, skip_time_dopr,  &
+             skip_time_do2d_xy, skip_time_do2d_xz, skip_time_do2d_yz,          &
+             skip_time_do3d, skip_time_domask, synchronous_exchange,           &
+             termination_time_needed, z_max_do2d
 
 
Index: palm/trunk/SOURCE/pmc_client.f90
===================================================================
--- palm/trunk/SOURCE/pmc_client.f90	(revision 1782)
+++ palm/trunk/SOURCE/pmc_client.f90	(revision 1783)
@@ -20,5 +20,5 @@
 ! Current revisions:
 ! ------------------
-!
+! Bugfix: wrong data-type in MPI_WIN_CREATE replaced
 !
 ! Former revisions:
@@ -263,10 +263,10 @@
 
        ALLOCATE(myInd(2*WinSize))
-       WinSize = WinSize*c_sizeof(i)
+       WinSize = 1
 
 !      local Buffer used in MPI_Get can but must not be inside the MPI Window
 !      Here, we use a dummy for MPI Window because the server PEs do not access the RMA window via MPI_get or MPI_Put
 
-       CALL MPI_Win_create (dummy, 1, iwp, MPI_INFO_NULL, me%intra_comm, indWin2, ierr);
+       CALL MPI_Win_create (dummy, WinSize, iwp, MPI_INFO_NULL, me%intra_comm, indWin2, ierr);
 
        CALL MPI_Win_fence (0, indWin2, ierr)    ! MPI_get is Non-blocking -> data in NrEle is not available until MPI_fence CALL
Index: palm/trunk/SOURCE/pmc_interface.f90
===================================================================
--- palm/trunk/SOURCE/pmc_interface.f90	(revision 1782)
+++ palm/trunk/SOURCE/pmc_interface.f90	(revision 1783)
@@ -20,5 +20,6 @@
 ! Current revisions:
 ! ------------------
-!
+! calculation of nest top area simplified,
+! interpolation and anterpolation moved to seperate wrapper subroutines
 !
 ! Former revisions:
@@ -678,5 +679,4 @@
     INTEGER(iwp), DIMENSION(5) ::  val        !:
     
-    REAL(wp)                   ::  area_t_l   !:
     REAL(wp), DIMENSION(1)     ::  fval       !:
     REAL(wp)                   ::  xcs        !:
@@ -684,6 +684,4 @@
     REAL(wp)                   ::  ycs        !:
     REAL(wp)                   ::  yce        !:
-    REAL(wp)                   ::  zct        !:
-    REAL(wp), DIMENSION(4)     ::  ztt        !:
                                              
 
@@ -830,11 +828,5 @@
 !--    Finally, compute the total area of the top-boundary face of the domain.
 !--    This is needed in the pmc_ensure_nest_mass_conservation      
-       area_t_l = ( nxr + 1 - nxl ) * ( nyn + 1 - nys ) * dx * dy
-       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
-       CALL MPI_ALLREDUCE( area_t_l, area_t, 1, MPI_REAL, MPI_SUM, comm2d, ierr )
-
-!
-!--    TO_DO: Why not just simply? test this!
-       !area_t_l = ( nx + 1 ) * (ny + 1 ) * dx * dy            
+       area_t = ( nx + 1 ) * (ny + 1 ) * dx * dy
 
     ENDIF
@@ -1079,5 +1071,4 @@
 
       REAL(wp), ALLOCATABLE, DIMENSION(:) ::  lcr   !:
-      LOGICAL  ::  more             !:
 
 !
@@ -2399,139 +2390,10 @@
           CALL cpu_log( log_point_s(73), 'PMC Client Recv', 'stop' )
 
-!
-!--       The interpolation. Add IF-condition here: IF not vertical nesting
-          IF ( nest_bound_l )  THEN   !  Left border pe
-             CALL pmci_interp_tril_lr( u,  uc,  icu, jco, kco, r1xu, r2xu, r1yo, r2yo, r1zo, r2zo,          &
-                                       nzb_u_inner, logc_u_l, logc_ratio_u_l, nzt_topo_nestbc_l, 'l', 'u' )
-             CALL pmci_interp_tril_lr( v,  vc,  ico, jcv, kco, r1xo, r2xo, r1yv, r2yv, r1zo, r2zo,          &
-                                       nzb_v_inner, logc_v_l, logc_ratio_v_l, nzt_topo_nestbc_l, 'l', 'v' )
-             CALL pmci_interp_tril_lr( w,  wc,  ico, jco, kcw, r1xo, r2xo, r1yo, r2yo, r1zw, r2zw,          &
-                                       nzb_w_inner, logc_w_l, logc_ratio_w_l, nzt_topo_nestbc_l, 'l', 'w' )
-             CALL pmci_interp_tril_lr( e,  ec,  ico, jco, kco, r1xo, r2xo, r1yo, r2yo, r1zo, r2zo,          &
-                                       nzb_s_inner, logc_u_l, logc_ratio_u_l, nzt_topo_nestbc_l, 'l', 'e' )
-             CALL pmci_interp_tril_lr( pt, ptc, ico, jco, kco, r1xo, r2xo, r1yo, r2yo, r1zo, r2zo,          &
-                                       nzb_s_inner, logc_u_l, logc_ratio_u_l, nzt_topo_nestbc_l, 'l', 's' )
-             IF ( humidity  .OR.  passive_scalar )  THEN
-                CALL pmci_interp_tril_lr( q, qc, ico, jco, kco, r1xo, r2xo, r1yo, r2yo, r1zo, r2zo,         &
-                                          nzb_s_inner, logc_u_l, logc_ratio_u_l, nzt_topo_nestbc_l, 'l', 's' )
-             ENDIF
-             IF ( nesting_mode == 'one-way' )  THEN
-                CALL pmci_extrap_ifoutflow_lr( u, nzb_u_inner, 'l', 'u' )
-                CALL pmci_extrap_ifoutflow_lr( v, nzb_v_inner, 'l', 'v' )
-                CALL pmci_extrap_ifoutflow_lr( w, nzb_w_inner, 'l', 'w' )
-                CALL pmci_extrap_ifoutflow_lr( e, nzb_s_inner, 'l', 'e' )
-                CALL pmci_extrap_ifoutflow_lr( pt,nzb_s_inner, 'l', 's' )
-                IF ( humidity  .OR.  passive_scalar )  THEN
-                   CALL pmci_extrap_ifoutflow_lr( q, nzb_s_inner, 'l', 's' )
-                ENDIF
-             ENDIF
-          ENDIF
-          IF ( nest_bound_r )  THEN   !  Right border pe
-             CALL pmci_interp_tril_lr( u,  uc,  icu, jco, kco, r1xu, r2xu, r1yo, r2yo, r1zo, r2zo,          &
-                                       nzb_u_inner, logc_u_r, logc_ratio_u_r, nzt_topo_nestbc_r, 'r', 'u' )
-             CALL pmci_interp_tril_lr( v,  vc,  ico, jcv, kco, r1xo, r2xo, r1yv, r2yv, r1zo, r2zo,          &
-                                       nzb_v_inner, logc_v_r, logc_ratio_v_r, nzt_topo_nestbc_r, 'r', 'v' )
-             CALL pmci_interp_tril_lr( w,  wc,  ico, jco, kcw, r1xo, r2xo, r1yo, r2yo, r1zw, r2zw,          &
-                                       nzb_w_inner, logc_w_r, logc_ratio_w_r, nzt_topo_nestbc_r, 'r', 'w' )
-             CALL pmci_interp_tril_lr( e,  ec,  ico, jco, kco, r1xo, r2xo, r1yo, r2yo, r1zo, r2zo,          &
-                                       nzb_s_inner, logc_u_r, logc_ratio_u_r, nzt_topo_nestbc_r, 'r', 'e' )
-             CALL pmci_interp_tril_lr( pt, ptc, ico, jco, kco, r1xo, r2xo, r1yo, r2yo, r1zo, r2zo,          &
-                                       nzb_s_inner, logc_u_r, logc_ratio_u_r, nzt_topo_nestbc_r, 'r', 's' )
-             IF ( humidity  .OR.  passive_scalar )  THEN
-                CALL pmci_interp_tril_lr( q, qc, ico, jco, kco, r1xo, r2xo, r1yo, r2yo, r1zo, r2zo,         &
-                                          nzb_s_inner, logc_u_r, logc_ratio_u_r, nzt_topo_nestbc_r, 'r', 's' )
-             ENDIF
-             IF ( nesting_mode == 'one-way' )  THEN
-                CALL pmci_extrap_ifoutflow_lr( u, nzb_u_inner, 'r', 'u' )
-                CALL pmci_extrap_ifoutflow_lr( v, nzb_v_inner, 'r', 'v' )
-                CALL pmci_extrap_ifoutflow_lr( w, nzb_w_inner, 'r', 'w' )
-                CALL pmci_extrap_ifoutflow_lr( e, nzb_s_inner, 'r', 'e' )
-                CALL pmci_extrap_ifoutflow_lr( pt,nzb_s_inner, 'r', 's' )
-                IF ( humidity  .OR.  passive_scalar )  THEN
-                   CALL pmci_extrap_ifoutflow_lr( q, nzb_s_inner, 'r', 's' )
-                ENDIF
-             ENDIF
-          ENDIF
-          IF ( nest_bound_s )  THEN   !  South border pe
-             CALL pmci_interp_tril_sn( u,  uc,  icu, jco, kco, r1xu, r2xu, r1yo, r2yo, r1zo, r2zo,          &
-                                       nzb_u_inner, logc_u_s, logc_ratio_u_s, nzt_topo_nestbc_s, 's', 'u' )
-             CALL pmci_interp_tril_sn( v,  vc,  ico, jcv, kco, r1xo, r2xo, r1yv, r2yv, r1zo, r2zo,          &
-                                       nzb_v_inner, logc_v_s, logc_ratio_v_s, nzt_topo_nestbc_s, 's', 'v' )
-             CALL pmci_interp_tril_sn( w,  wc,  ico, jco, kcw, r1xo, r2xo, r1yo, r2yo, r1zw, r2zw,          &
-                                       nzb_w_inner, logc_w_s, logc_ratio_w_s, nzt_topo_nestbc_s, 's', 'w' )
-             CALL pmci_interp_tril_sn( e,  ec,  ico, jco, kco, r1xo, r2xo, r1yo, r2yo, r1zo, r2zo,          &
-                                       nzb_s_inner, logc_u_s, logc_ratio_u_s, nzt_topo_nestbc_s, 's', 'e' )
-             CALL pmci_interp_tril_sn( pt, ptc, ico, jco, kco, r1xo, r2xo, r1yo, r2yo, r1zo, r2zo,          &
-                                       nzb_s_inner, logc_u_s, logc_ratio_u_s, nzt_topo_nestbc_s, 's', 's' )
-             IF ( humidity  .OR.  passive_scalar )  THEN
-                CALL pmci_interp_tril_sn( q, qc, ico, jco, kco, r1xo, r2xo, r1yo, r2yo, r1zo, r2zo,         &
-                                          nzb_s_inner, logc_u_s, logc_ratio_u_s, nzt_topo_nestbc_s, 's', 's' )
-             ENDIF
-             IF ( nesting_mode == 'one-way' )  THEN
-                CALL pmci_extrap_ifoutflow_sn( u, nzb_u_inner, 's', 'u' )
-                CALL pmci_extrap_ifoutflow_sn( v, nzb_v_inner, 's', 'v' )
-                CALL pmci_extrap_ifoutflow_sn( w, nzb_w_inner, 's', 'w' )
-                CALL pmci_extrap_ifoutflow_sn( e, nzb_s_inner, 's', 'e' )
-                CALL pmci_extrap_ifoutflow_sn( pt,nzb_s_inner, 's', 's' )
-                IF ( humidity  .OR.  passive_scalar )  THEN
-                   CALL pmci_extrap_ifoutflow_sn( q, nzb_s_inner, 's', 's' )
-                ENDIF
-             ENDIF
-          ENDIF
-          IF ( nest_bound_n )  THEN   !  North border pe
-             CALL pmci_interp_tril_sn( u,  uc,  icu, jco, kco, r1xu, r2xu, r1yo, r2yo, r1zo, r2zo,          &
-                                       nzb_u_inner, logc_u_n, logc_ratio_u_n, nzt_topo_nestbc_n, 'n', 'u' )
-             CALL pmci_interp_tril_sn( v,  vc,  ico, jcv, kco, r1xo, r2xo, r1yv, r2yv, r1zo, r2zo,          &
-                                       nzb_v_inner, logc_v_n, logc_ratio_v_n, nzt_topo_nestbc_n, 'n', 'v' )
-             CALL pmci_interp_tril_sn( w,  wc,  ico, jco, kcw, r1xo, r2xo, r1yo, r2yo, r1zw, r2zw,          &
-                                       nzb_w_inner, logc_w_n, logc_ratio_w_n, nzt_topo_nestbc_n, 'n', 'w' )
-             CALL pmci_interp_tril_sn( e,  ec,  ico,jco,kco,r1xo,r2xo,r1yo,r2yo,r1zo,r2zo,                  &
-                                       nzb_s_inner, logc_u_n, logc_ratio_u_n, nzt_topo_nestbc_n, 'n', 'e' )
-             CALL pmci_interp_tril_sn( pt, ptc, ico, jco, kco, r1xo, r2xo, r1yo, r2yo, r1zo, r2zo,          &
-                                       nzb_s_inner, logc_u_n, logc_ratio_u_n, nzt_topo_nestbc_n, 'n', 's' )
-             IF ( humidity  .OR.  passive_scalar )  THEN
-                CALL pmci_interp_tril_sn( q, qc, ico,jco,kco,r1xo,r2xo,r1yo,r2yo,r1zo,r2zo,                 &
-                                          nzb_s_inner, logc_u_n, logc_ratio_u_n, nzt_topo_nestbc_n, 'n', 's' )
-             ENDIF
-             IF ( nesting_mode == 'one-way' )  THEN             
-                CALL pmci_extrap_ifoutflow_sn( u, nzb_u_inner, 'n', 'u' )
-                CALL pmci_extrap_ifoutflow_sn( v, nzb_v_inner, 'n', 'v' )
-                CALL pmci_extrap_ifoutflow_sn( w, nzb_w_inner, 'n', 'w' )
-                CALL pmci_extrap_ifoutflow_sn( e, nzb_s_inner, 'n', 'e' )
-                CALL pmci_extrap_ifoutflow_sn( pt,nzb_s_inner, 'n', 's' )
-                IF ( humidity  .OR.  passive_scalar )  THEN
-                   CALL pmci_extrap_ifoutflow_sn( q, nzb_s_inner, 'n', 's' )
-                ENDIF
-             ENDIF
-          ENDIF
-
-!
-!--       All PEs are top-border PEs
-          CALL pmci_interp_tril_t( u,  uc,  icu, jco, kco, r1xu, r2xu, r1yo, r2yo, r1zo, r2zo, 'u' )
-          CALL pmci_interp_tril_t( v,  vc,  ico, jcv, kco, r1xo, r2xo, r1yv, r2yv, r1zo, r2zo, 'v' )
-          CALL pmci_interp_tril_t( w,  wc,  ico, jco, kcw, r1xo, r2xo, r1yo, r2yo, r1zw, r2zw, 'w' )
-          CALL pmci_interp_tril_t( e,  ec,  ico, jco, kco, r1xo, r2xo, r1yo, r2yo, r1zo, r2zo, 'e' )
-          CALL pmci_interp_tril_t( pt, ptc, ico, jco, kco, r1xo, r2xo, r1yo, r2yo, r1zo, r2zo, 's' )
-          IF ( humidity  .OR.  passive_scalar )  THEN
-             CALL pmci_interp_tril_t( q, qc, ico, jco, kco, r1xo, r2xo, r1yo, r2yo, r1zo, r2zo, 's' )
-          ENDIF
-          IF ( nesting_mode == 'one-way' )  THEN
-             CALL pmci_extrap_ifoutflow_t( u,  'u' )
-             CALL pmci_extrap_ifoutflow_t( v,  'v' )
-             CALL pmci_extrap_ifoutflow_t( w,  'w' )
-             CALL pmci_extrap_ifoutflow_t( e,  'e' )
-             CALL pmci_extrap_ifoutflow_t( pt, 's' )
-             IF ( humidity  .OR.  passive_scalar )  THEN
-                CALL pmci_extrap_ifoutflow_t( q, 's' )
-             ENDIF
-          ENDIF
-       ELSE    !  IF ( direction == server_to_client ) 
-          CALL pmci_anterp_tophat( u,  uc,  kceu, iflu, ifuu, jflo, jfuo, kflo, kfuo, nzb_u_inner, 'u' )
-          CALL pmci_anterp_tophat( v,  vc,  kceu, iflo, ifuo, jflv, jfuv, kflo, kfuo, nzb_v_inner, 'v' )
-          CALL pmci_anterp_tophat( w,  wc,  kcew, iflo, ifuo, jflo, jfuo, kflw, kfuw, nzb_w_inner, 'w' )
-          CALL pmci_anterp_tophat( pt, ptc, kceu, iflo, ifuo, jflo, jfuo, kflo, kfuo, nzb_s_inner, 's' )
-          IF ( humidity  .OR.  passive_scalar )  THEN
-             CALL pmci_anterp_tophat( q, qc, kceu, iflo, ifuo, jflo, jfuo, kflo, kfuo, nzb_s_inner, 's' )
-          ENDIF
+          CALL pmci_interpolation
+
+       ELSE    !  IF ( direction == server_to_client )
+
+          CALL pmci_anterpolation
+
           CALL cpu_log( log_point_s(74), 'PMC Client Send', 'start' )
           CALL pmc_c_putbuffer( WaitTime = WaitTime )
@@ -2539,6 +2401,160 @@
        ENDIF
     ENDIF
-    
+
  CONTAINS
+
+
+   SUBROUTINE pmci_interpolation
+
+!
+!--   A wrapper routine for all interpolation and extrapolation actions.
+      IMPLICIT NONE
+
+!
+!--   Add IF-condition here: IF not vertical nesting
+      IF ( nest_bound_l )  THEN   !  Left border pe
+         CALL pmci_interp_tril_lr( u,  uc,  icu, jco, kco, r1xu, r2xu, r1yo, r2yo, r1zo, r2zo,          &
+                                   nzb_u_inner, logc_u_l, logc_ratio_u_l, nzt_topo_nestbc_l, 'l', 'u' )
+         CALL pmci_interp_tril_lr( v,  vc,  ico, jcv, kco, r1xo, r2xo, r1yv, r2yv, r1zo, r2zo,          &
+                                   nzb_v_inner, logc_v_l, logc_ratio_v_l, nzt_topo_nestbc_l, 'l', 'v' )
+         CALL pmci_interp_tril_lr( w,  wc,  ico, jco, kcw, r1xo, r2xo, r1yo, r2yo, r1zw, r2zw,          &
+                                   nzb_w_inner, logc_w_l, logc_ratio_w_l, nzt_topo_nestbc_l, 'l', 'w' )
+         CALL pmci_interp_tril_lr( e,  ec,  ico, jco, kco, r1xo, r2xo, r1yo, r2yo, r1zo, r2zo,          &
+                                   nzb_s_inner, logc_u_l, logc_ratio_u_l, nzt_topo_nestbc_l, 'l', 'e' )
+         CALL pmci_interp_tril_lr( pt, ptc, ico, jco, kco, r1xo, r2xo, r1yo, r2yo, r1zo, r2zo,          &
+                                   nzb_s_inner, logc_u_l, logc_ratio_u_l, nzt_topo_nestbc_l, 'l', 's' )
+         IF ( humidity  .OR.  passive_scalar )  THEN
+            CALL pmci_interp_tril_lr( q, qc, ico, jco, kco, r1xo, r2xo, r1yo, r2yo, r1zo, r2zo,         &
+                                      nzb_s_inner, logc_u_l, logc_ratio_u_l, nzt_topo_nestbc_l, 'l', 's' )
+         ENDIF
+         IF ( nesting_mode == 'one-way' )  THEN
+            CALL pmci_extrap_ifoutflow_lr( u, nzb_u_inner, 'l', 'u' )
+            CALL pmci_extrap_ifoutflow_lr( v, nzb_v_inner, 'l', 'v' )
+            CALL pmci_extrap_ifoutflow_lr( w, nzb_w_inner, 'l', 'w' )
+            CALL pmci_extrap_ifoutflow_lr( e, nzb_s_inner, 'l', 'e' )
+            CALL pmci_extrap_ifoutflow_lr( pt,nzb_s_inner, 'l', 's' )
+            IF ( humidity  .OR.  passive_scalar )  THEN
+               CALL pmci_extrap_ifoutflow_lr( q, nzb_s_inner, 'l', 's' )
+            ENDIF
+         ENDIF
+      ENDIF
+      IF ( nest_bound_r )  THEN   !  Right border pe
+         CALL pmci_interp_tril_lr( u,  uc,  icu, jco, kco, r1xu, r2xu, r1yo, r2yo, r1zo, r2zo,          &
+                                   nzb_u_inner, logc_u_r, logc_ratio_u_r, nzt_topo_nestbc_r, 'r', 'u' )
+         CALL pmci_interp_tril_lr( v,  vc,  ico, jcv, kco, r1xo, r2xo, r1yv, r2yv, r1zo, r2zo,          &
+                                   nzb_v_inner, logc_v_r, logc_ratio_v_r, nzt_topo_nestbc_r, 'r', 'v' )
+         CALL pmci_interp_tril_lr( w,  wc,  ico, jco, kcw, r1xo, r2xo, r1yo, r2yo, r1zw, r2zw,          &
+                                   nzb_w_inner, logc_w_r, logc_ratio_w_r, nzt_topo_nestbc_r, 'r', 'w' )
+         CALL pmci_interp_tril_lr( e,  ec,  ico, jco, kco, r1xo, r2xo, r1yo, r2yo, r1zo, r2zo,          &
+                                   nzb_s_inner, logc_u_r, logc_ratio_u_r, nzt_topo_nestbc_r, 'r', 'e' )
+         CALL pmci_interp_tril_lr( pt, ptc, ico, jco, kco, r1xo, r2xo, r1yo, r2yo, r1zo, r2zo,          &
+                                   nzb_s_inner, logc_u_r, logc_ratio_u_r, nzt_topo_nestbc_r, 'r', 's' )
+         IF ( humidity  .OR.  passive_scalar )  THEN
+            CALL pmci_interp_tril_lr( q, qc, ico, jco, kco, r1xo, r2xo, r1yo, r2yo, r1zo, r2zo,         &
+                                      nzb_s_inner, logc_u_r, logc_ratio_u_r, nzt_topo_nestbc_r, 'r', 's' )
+         ENDIF
+         IF ( nesting_mode == 'one-way' )  THEN
+            CALL pmci_extrap_ifoutflow_lr( u, nzb_u_inner, 'r', 'u' )
+             CALL pmci_extrap_ifoutflow_lr( v, nzb_v_inner, 'r', 'v' )
+             CALL pmci_extrap_ifoutflow_lr( w, nzb_w_inner, 'r', 'w' )
+             CALL pmci_extrap_ifoutflow_lr( e, nzb_s_inner, 'r', 'e' )
+             CALL pmci_extrap_ifoutflow_lr( pt,nzb_s_inner, 'r', 's' )
+             IF ( humidity  .OR.  passive_scalar )  THEN
+                CALL pmci_extrap_ifoutflow_lr( q, nzb_s_inner, 'r', 's' )
+             ENDIF
+          ENDIF
+       ENDIF
+       IF ( nest_bound_s )  THEN   !  South border pe
+          CALL pmci_interp_tril_sn( u,  uc,  icu, jco, kco, r1xu, r2xu, r1yo, r2yo, r1zo, r2zo,          &
+                                    nzb_u_inner, logc_u_s, logc_ratio_u_s, nzt_topo_nestbc_s, 's', 'u' )
+          CALL pmci_interp_tril_sn( v,  vc,  ico, jcv, kco, r1xo, r2xo, r1yv, r2yv, r1zo, r2zo,          &
+                                    nzb_v_inner, logc_v_s, logc_ratio_v_s, nzt_topo_nestbc_s, 's', 'v' )
+          CALL pmci_interp_tril_sn( w,  wc,  ico, jco, kcw, r1xo, r2xo, r1yo, r2yo, r1zw, r2zw,          &
+                                    nzb_w_inner, logc_w_s, logc_ratio_w_s, nzt_topo_nestbc_s, 's', 'w' )
+          CALL pmci_interp_tril_sn( e,  ec,  ico, jco, kco, r1xo, r2xo, r1yo, r2yo, r1zo, r2zo,          &
+                                    nzb_s_inner, logc_u_s, logc_ratio_u_s, nzt_topo_nestbc_s, 's', 'e' )
+          CALL pmci_interp_tril_sn( pt, ptc, ico, jco, kco, r1xo, r2xo, r1yo, r2yo, r1zo, r2zo,          &
+                                    nzb_s_inner, logc_u_s, logc_ratio_u_s, nzt_topo_nestbc_s, 's', 's' )
+          IF ( humidity  .OR.  passive_scalar )  THEN
+             CALL pmci_interp_tril_sn( q, qc, ico, jco, kco, r1xo, r2xo, r1yo, r2yo, r1zo, r2zo,         &
+                                       nzb_s_inner, logc_u_s, logc_ratio_u_s, nzt_topo_nestbc_s, 's', 's' )
+          ENDIF
+          IF ( nesting_mode == 'one-way' )  THEN
+             CALL pmci_extrap_ifoutflow_sn( u, nzb_u_inner, 's', 'u' )
+             CALL pmci_extrap_ifoutflow_sn( v, nzb_v_inner, 's', 'v' )
+             CALL pmci_extrap_ifoutflow_sn( w, nzb_w_inner, 's', 'w' )
+             CALL pmci_extrap_ifoutflow_sn( e, nzb_s_inner, 's', 'e' )
+             CALL pmci_extrap_ifoutflow_sn( pt,nzb_s_inner, 's', 's' )
+             IF ( humidity  .OR.  passive_scalar )  THEN
+                CALL pmci_extrap_ifoutflow_sn( q, nzb_s_inner, 's', 's' )
+             ENDIF
+          ENDIF
+       ENDIF
+       IF ( nest_bound_n )  THEN   !  North border pe
+          CALL pmci_interp_tril_sn( u,  uc,  icu, jco, kco, r1xu, r2xu, r1yo, r2yo, r1zo, r2zo,          &
+                                    nzb_u_inner, logc_u_n, logc_ratio_u_n, nzt_topo_nestbc_n, 'n', 'u' )
+          CALL pmci_interp_tril_sn( v,  vc,  ico, jcv, kco, r1xo, r2xo, r1yv, r2yv, r1zo, r2zo,          &
+                                    nzb_v_inner, logc_v_n, logc_ratio_v_n, nzt_topo_nestbc_n, 'n', 'v' )
+          CALL pmci_interp_tril_sn( w,  wc,  ico, jco, kcw, r1xo, r2xo, r1yo, r2yo, r1zw, r2zw,          &
+                                    nzb_w_inner, logc_w_n, logc_ratio_w_n, nzt_topo_nestbc_n, 'n', 'w' )
+          CALL pmci_interp_tril_sn( e,  ec,  ico,jco,kco,r1xo,r2xo,r1yo,r2yo,r1zo,r2zo,                  &
+                                    nzb_s_inner, logc_u_n, logc_ratio_u_n, nzt_topo_nestbc_n, 'n', 'e' )
+          CALL pmci_interp_tril_sn( pt, ptc, ico, jco, kco, r1xo, r2xo, r1yo, r2yo, r1zo, r2zo,          &
+                                    nzb_s_inner, logc_u_n, logc_ratio_u_n, nzt_topo_nestbc_n, 'n', 's' )
+          IF ( humidity  .OR.  passive_scalar )  THEN
+             CALL pmci_interp_tril_sn( q, qc, ico,jco,kco,r1xo,r2xo,r1yo,r2yo,r1zo,r2zo,                 &
+                                       nzb_s_inner, logc_u_n, logc_ratio_u_n, nzt_topo_nestbc_n, 'n', 's' )
+          ENDIF
+          IF ( nesting_mode == 'one-way' )  THEN
+             CALL pmci_extrap_ifoutflow_sn( u, nzb_u_inner, 'n', 'u' )
+             CALL pmci_extrap_ifoutflow_sn( v, nzb_v_inner, 'n', 'v' )
+             CALL pmci_extrap_ifoutflow_sn( w, nzb_w_inner, 'n', 'w' )
+             CALL pmci_extrap_ifoutflow_sn( e, nzb_s_inner, 'n', 'e' )
+             CALL pmci_extrap_ifoutflow_sn( pt,nzb_s_inner, 'n', 's' )
+             IF ( humidity  .OR.  passive_scalar )  THEN
+                CALL pmci_extrap_ifoutflow_sn( q, nzb_s_inner, 'n', 's' )
+             ENDIF
+          ENDIF
+      ENDIF
+
+!
+!--   All PEs are top-border PEs
+       CALL pmci_interp_tril_t( u,  uc,  icu, jco, kco, r1xu, r2xu, r1yo, r2yo, r1zo, r2zo, 'u' )
+       CALL pmci_interp_tril_t( v,  vc,  ico, jcv, kco, r1xo, r2xo, r1yv, r2yv, r1zo, r2zo, 'v' )
+       CALL pmci_interp_tril_t( w,  wc,  ico, jco, kcw, r1xo, r2xo, r1yo, r2yo, r1zw, r2zw, 'w' )
+       CALL pmci_interp_tril_t( e,  ec,  ico, jco, kco, r1xo, r2xo, r1yo, r2yo, r1zo, r2zo, 'e' )
+       CALL pmci_interp_tril_t( pt, ptc, ico, jco, kco, r1xo, r2xo, r1yo, r2yo, r1zo, r2zo, 's' )
+       IF ( humidity  .OR.  passive_scalar )  THEN
+          CALL pmci_interp_tril_t( q, qc, ico, jco, kco, r1xo, r2xo, r1yo, r2yo, r1zo, r2zo, 's' )
+       ENDIF
+       IF ( nesting_mode == 'one-way' )  THEN
+          CALL pmci_extrap_ifoutflow_t( u,  'u' )
+          CALL pmci_extrap_ifoutflow_t( v,  'v' )
+          CALL pmci_extrap_ifoutflow_t( w,  'w' )
+          CALL pmci_extrap_ifoutflow_t( e,  'e' )
+          CALL pmci_extrap_ifoutflow_t( pt, 's' )
+          IF ( humidity  .OR.  passive_scalar )  THEN
+             CALL pmci_extrap_ifoutflow_t( q, 's' )
+          ENDIF
+      ENDIF
+   END SUBROUTINE pmci_interpolation
+
+
+
+   SUBROUTINE pmci_anterpolation
+
+!
+!--   A wrapper routine for all anterpolation actions.
+      IMPLICIT NONE
+
+      CALL pmci_anterp_tophat( u,  uc,  kceu, iflu, ifuu, jflo, jfuo, kflo, kfuo, nzb_u_inner, 'u' )
+      CALL pmci_anterp_tophat( v,  vc,  kceu, iflo, ifuo, jflv, jfuv, kflo, kfuo, nzb_v_inner, 'v' )
+      CALL pmci_anterp_tophat( w,  wc,  kcew, iflo, ifuo, jflo, jfuo, kflw, kfuw, nzb_w_inner, 'w' )
+      CALL pmci_anterp_tophat( pt, ptc, kceu, iflo, ifuo, jflo, jfuo, kflo, kfuo, nzb_s_inner, 's' )
+      IF ( humidity  .OR.  passive_scalar )  THEN
+         CALL pmci_anterp_tophat( q, qc, kceu, iflo, ifuo, jflo, jfuo, kflo, kfuo, nzb_s_inner, 's' )
+      ENDIF
+
+   END SUBROUTINE pmci_anterpolation
 
 
Index: palm/trunk/SOURCE/radiation_model.f90
===================================================================
--- palm/trunk/SOURCE/radiation_model.f90	(revision 1782)
+++ palm/trunk/SOURCE/radiation_model.f90	(revision 1783)
@@ -19,5 +19,7 @@
 ! Current revisions:
 ! -----------------
-! 
+! palm-netcdf-module removed in order to avoid a circular module dependency,
+! netcdf-variables moved to netcdf-module, new routine netcdf_handle_error_mod
+! added
 ! 
 ! Former revisions:
@@ -83,5 +85,4 @@
  MODULE radiation_model_mod
  
-
     USE arrays_3d,                                                             &
         ONLY:  dzw, hyp, pt, q, ql, zw
@@ -104,9 +105,6 @@
 
 #if defined ( __netcdf )
-    USE netcdf
+    USE NETCDF
 #endif
-
-    USE netcdf_control,                                                        &
-        ONLY:  dots_label, dots_num, dots_unit
 
 #if defined ( __rrtmg )
@@ -642,29 +640,4 @@
        CALL user_init_radiation
 
-
-!
-!--    Add timeseries for radiation model
-       dots_rad = dots_num + 1
-       dots_num = dots_num + 5
-
-       dots_label(dots_rad) = "rad_net"
-       dots_label(dots_rad+1) = "rad_lw_in"
-       dots_label(dots_rad+2) = "rad_lw_out"
-       dots_label(dots_rad+3) = "rad_sw_in"
-       dots_label(dots_rad+4) = "rad_sw_out"
-       dots_unit(dots_rad:dots_rad+4) = "W/m2"
-
-!
-!--    Output of albedos is only required for RRTMG
-       IF ( radiation_scheme == 'rrtmg' )  THEN
-          dots_num  = dots_num + 4
-          dots_label(dots_rad+5) = "rrtm_aldif"
-          dots_label(dots_rad+6) = "rrtm_aldir"
-          dots_label(dots_rad+7) = "rrtm_asdif"
-          dots_label(dots_rad+8) = "rrtm_asdir"
-          dots_unit(dots_num+5:dots_num+8) = ""
-
-       ENDIF
-
 !
 !--    Calculate radiative fluxes at model start
@@ -1098,6 +1071,4 @@
 !------------------------------------------------------------------------------!
     SUBROUTINE read_sounding_data
-
-       USE netcdf_control
 
        IMPLICIT NONE
@@ -1168,5 +1139,5 @@
 !--    Open file for reading
        nc_stat = NF90_OPEN( rrtm_input_file, NF90_NOWRITE, id )
-       CALL handle_netcdf_error( 'netcdf', 549 )
+       CALL netcdf_handle_error_rad( 'read_sounding_data', 549 )
 
 !
@@ -1174,5 +1145,5 @@
        nc_stat = NF90_INQ_DIMID( id, "Pressure", id_dim_zrad )
        nc_stat = NF90_INQUIRE_DIMENSION( id, id_dim_zrad, len = nz_snd )
-       CALL handle_netcdf_error( 'netcdf', 551 )
+       CALL netcdf_handle_error_rad( 'read_sounding_data', 551 )
 
 !
@@ -1186,5 +1157,5 @@
        nc_stat = NF90_GET_VAR( id, id_var, hyp_snd_tmp(:), start = (/1/),      &
                                count = (/nz_snd/) )
-       CALL handle_netcdf_error( 'netcdf', 552 )
+       CALL netcdf_handle_error_rad( 'read_sounding_data', 552 )
 
 !
@@ -1198,5 +1169,5 @@
        nc_stat = NF90_GET_VAR( id, id_var, t_snd_tmp(:), start = (/1/),        &
                                count = (/nz_snd/) )
-       CALL handle_netcdf_error( 'netcdf', 553 )
+       CALL netcdf_handle_error_rad( 'read_sounding_data', 553 )
 
 !
@@ -1371,5 +1342,4 @@
     SUBROUTINE read_trace_gas_data
 
-       USE netcdf_control
        USE rrsw_ncpar
 
@@ -1435,16 +1405,16 @@
 !--    Open file for reading
        nc_stat = NF90_OPEN( rrtm_input_file, NF90_NOWRITE, id )
-       CALL handle_netcdf_error( 'netcdf', 549 )
+       CALL netcdf_handle_error_rad( 'read_trace_gas_data', 549 )
 !
 !--    Inquire dimension ids and dimensions
        nc_stat = NF90_INQ_DIMID( id, "Pressure", id_dim )
-       CALL handle_netcdf_error( 'netcdf', 550 )
+       CALL netcdf_handle_error_rad( 'read_trace_gas_data', 550 )
        nc_stat = NF90_INQUIRE_DIMENSION( id, id_dim, len = np) 
-       CALL handle_netcdf_error( 'netcdf', 550 )
+       CALL netcdf_handle_error_rad( 'read_trace_gas_data', 550 )
 
        nc_stat = NF90_INQ_DIMID( id, "Absorber", id_dim )
-       CALL handle_netcdf_error( 'netcdf', 550 )
+       CALL netcdf_handle_error_rad( 'read_trace_gas_data', 550 )
        nc_stat = NF90_INQUIRE_DIMENSION( id, id_dim, len = nabs ) 
-       CALL handle_netcdf_error( 'netcdf', 550 )
+       CALL netcdf_handle_error_rad( 'read_trace_gas_data', 550 )
    
 
@@ -1457,12 +1427,12 @@
 
        nc_stat = NF90_INQ_VARID( id, "Pressure", id_var )
-       CALL handle_netcdf_error( 'netcdf', 550 )
+       CALL netcdf_handle_error_rad( 'read_trace_gas_data', 550 )
        nc_stat = NF90_GET_VAR( id, id_var, p_mls )
-       CALL handle_netcdf_error( 'netcdf', 550 )
+       CALL netcdf_handle_error_rad( 'read_trace_gas_data', 550 )
 
        nc_stat = NF90_INQ_VARID( id, "AbsorberAmountMLS", id_var )
-       CALL handle_netcdf_error( 'netcdf', 550 )
+       CALL netcdf_handle_error_rad( 'read_trace_gas_data', 550 )
        nc_stat = NF90_GET_VAR( id, id_var, trace_mls_tmp )
-       CALL handle_netcdf_error( 'netcdf', 550 )
+       CALL netcdf_handle_error_rad( 'read_trace_gas_data', 550 )
 
 
@@ -1484,5 +1454,5 @@
 
        nc_stat = NF90_CLOSE( id )
-       CALL handle_netcdf_error( 'netcdf', 551 )
+       CALL netcdf_handle_error_rad( 'read_trace_gas_data', 551 )
 
 !
@@ -1623,4 +1593,30 @@
     END SUBROUTINE read_trace_gas_data
 
+    SUBROUTINE netcdf_handle_error_rad( routine_name, errno )
+
+       USE control_parameters,                                                 &
+           ONLY:  message_string
+
+       USE NETCDF
+
+       USE pegrid
+
+       IMPLICIT NONE
+
+       CHARACTER(LEN=6) ::  message_identifier
+       CHARACTER(LEN=*) ::  routine_name
+
+       INTEGER(iwp) ::  errno
+
+       IF ( nc_stat /= NF90_NOERR )  THEN
+
+          WRITE( message_identifier, '(''NC'',I4.4)' )  errno
+          message_string = TRIM( NF90_STRERROR( nc_stat ) )
+
+          CALL message( routine_name, message_identifier, 2, 2, 0, 6, 1 )
+
+       ENDIF
+
+    END SUBROUTINE netcdf_handle_error_rad
 #endif
 
Index: palm/trunk/SOURCE/read_var_list.f90
===================================================================
--- palm/trunk/SOURCE/read_var_list.f90	(revision 1782)
+++ palm/trunk/SOURCE/read_var_list.f90	(revision 1783)
@@ -19,5 +19,5 @@
 ! Current revisions:
 ! ------------------
-! 
+! netcdf module name changed + related changes
 ! 
 ! Former revisions:
@@ -173,5 +173,6 @@
         ONLY:  damp_level_1d, dt_pr_1d, dt_run_control_1d, end_time_1d
 
-    USE netcdf_control
+    USE netcdf_interface,                                                      &
+        ONLY:  netcdf_precision, output_for_t0
 
     USE particle_attributes,                                                   &
Index: palm/trunk/SOURCE/time_integration.f90
===================================================================
--- palm/trunk/SOURCE/time_integration.f90	(revision 1782)
+++ palm/trunk/SOURCE/time_integration.f90	(revision 1783)
@@ -19,5 +19,6 @@
 ! Current revisions:
 ! ------------------
-!
+! switch back of netcdf data format for mask output moved to the mask output
+! routine
 !
 ! Former revisions:
@@ -200,5 +201,5 @@
                intermediate_timestep_count_max, large_scale_forcing,           &
                loop_optimization, lsf_surf, lsf_vert, masks, mid, nest_domain, &
-               netcdf_data_format, neutral, nr_timesteps_this_run, nudging,    &
+               neutral, nr_timesteps_this_run, nudging,                        &
                ocean, on_device, passive_scalar, precipitation,                &
                prho_reference, pt_reference, pt_slope_offset, random_heatflux, &
@@ -280,6 +281,4 @@
     CHARACTER (LEN=9) ::  time_to_string          !< 
 
-    INTEGER(iwp)      ::  netcdf_data_format_save !< 
-
 !
 !-- At the beginning of a simulation determine the time step as well as
@@ -1048,10 +1047,5 @@
 
 !
-!--    masked data output
-!--    Parallel netcdf output is not tested so far for masked data, hence 
-!--    netcdf_data_format is switched back to non-paralell output.
-       netcdf_data_format_save = netcdf_data_format
-       IF ( netcdf_data_format == 5 ) netcdf_data_format = 3
-       IF ( netcdf_data_format == 6 ) netcdf_data_format = 4
+!--    Masked data output
        DO  mid = 1, masks
           IF ( time_domask(mid) >= dt_domask(mid) )  THEN
@@ -1061,5 +1055,4 @@
           ENDIF
        ENDDO
-       netcdf_data_format = netcdf_data_format_save
 
 !
@@ -1071,13 +1064,7 @@
           CALL data_output_2d( 'yz', 1 )
           CALL data_output_3d( 1 )
-!--       Parallel netcdf output is not tested so far for masked data, hence 
-!--       netcdf_data_format is switched back to non-paralell output.
-          netcdf_data_format_save = netcdf_data_format
-          IF ( netcdf_data_format == 5 ) netcdf_data_format = 3
-          IF ( netcdf_data_format == 6 ) netcdf_data_format = 4
           DO  mid = 1, masks
              CALL data_output_mask( 1 )
           ENDDO
-          netcdf_data_format = netcdf_data_format_save
           time_do_av = MOD( time_do_av, MAX( dt_data_output_av, dt_3d ) )
        ENDIF
Index: palm/trunk/SOURCE/user_check_data_output_pr.f90
===================================================================
--- palm/trunk/SOURCE/user_check_data_output_pr.f90	(revision 1782)
+++ palm/trunk/SOURCE/user_check_data_output_pr.f90	(revision 1783)
@@ -19,5 +19,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! netcdf module name changed + relatec changes
 ! 
 ! Former revisions:
@@ -57,5 +57,6 @@
     USE kinds
 
-    USE netcdf_control
+    USE netcdf_interface,                                                      &
+        ONLY:  dopr_unit
 
     USE profil_parameter
Index: palm/trunk/SOURCE/user_init.f90
===================================================================
--- palm/trunk/SOURCE/user_init.f90	(revision 1782)
+++ palm/trunk/SOURCE/user_init.f90	(revision 1783)
@@ -19,5 +19,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! netcdf module name changed + related changes
 ! 
 ! Former revisions:
@@ -55,5 +55,6 @@
     USE kinds
     
-    USE netcdf_control
+    USE netcdf_interface,                                                      &
+        ONLY: dots_label, dots_unit
     
     USE pegrid
Index: palm/trunk/SOURCE/user_parin.f90
===================================================================
--- palm/trunk/SOURCE/user_parin.f90	(revision 1782)
+++ palm/trunk/SOURCE/user_parin.f90	(revision 1783)
@@ -19,5 +19,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! required interface revision changed
 ! 
 ! Former revisions:
@@ -89,5 +89,5 @@
 !-- current revision does not match with previous revisions (e.g. if routines
 !-- have been added/deleted or if parameter lists in subroutines have been changed).
-    user_interface_current_revision = 'r1663'
+    user_interface_current_revision = 'r1783'
 
 !
Index: palm/trunk/SOURCE/user_statistics.f90
===================================================================
--- palm/trunk/SOURCE/user_statistics.f90	(revision 1782)
+++ palm/trunk/SOURCE/user_statistics.f90	(revision 1783)
@@ -19,5 +19,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! netcdf module name changed + related changes
 ! 
 ! Former revisions:
@@ -66,6 +66,7 @@
     USE kinds
     
-    USE netcdf_control
-    
+    USE netcdf_interface,                                                      &
+        ONLY:  dots_max
+
     USE pegrid
     
Index: palm/trunk/SOURCE/write_var_list.f90
===================================================================
--- palm/trunk/SOURCE/write_var_list.f90	(revision 1782)
+++ palm/trunk/SOURCE/write_var_list.f90	(revision 1783)
@@ -19,5 +19,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! netcdf module name changed + related changes
 ! 
 ! Former revisions:
@@ -155,5 +155,6 @@
         ONLY:  damp_level_1d, dt_pr_1d, dt_run_control_1d, end_time_1d
     
-    USE netcdf_control
+    USE netcdf_interface,                                                      &
+        ONLY:  netcdf_precision, output_for_t0
     
     USE particle_attributes,                                                   &
