Index: palm/trunk/SOURCE/CURRENT_MODIFICATIONS
===================================================================
--- palm/trunk/SOURCE/CURRENT_MODIFICATIONS	(revision 107)
+++ palm/trunk/SOURCE/CURRENT_MODIFICATIONS	(revision 108)
@@ -8,5 +8,6 @@
 communication between the two models is done using the intercommunicator
 comm_inter,
-local files opened by the ocean model get the additional suffic "_O"
+local files opened by the ocean model get the additional suffic "_O".
+Assume saturation at k=nzb_s_inner(j,i) for atmosphere coupled to ocean.
 
 A momentum flux can be set as top boundary condition using the new
@@ -19,5 +20,7 @@
 Initial profiles are reset to constant profiles in case that initializing_actions /= 'set_constant_profiles'. (init_rankine)
 
-boundary_conds, check_open, check_parameters, diffusion_u, diffusion_v, flow_statistics, header, init_pegrid, init_rankine, init_3d_model, modules, palm, parin, pres, prognostic_equations, read_var_list, read_3d_binary, swap_timelevel, time_integration, write_var_list, write_3d_binary
+Optionally calculate km and kh from initial TKE e_init.
+
+boundary_conds, check_open, check_parameters, diffusion_u, diffusion_v, flow_statistics, header, init_pegrid, init_rankine, init_3d_model, modules, palm, parin, pres, prandtl_fluxes, prognostic_equations, read_var_list, read_3d_binary, swap_timelevel, time_integration, write_var_list, write_3d_binary
 
 New:
@@ -34,5 +37,11 @@
 velocity field after the first substep.
 
-advec_particles, init_particles, time_integration
+Initialization of km,kh = 0.00001 for ocean = .T. (for ocean = .F. it remains 0.01).
+
+Allow data_output_pr= q, wq, w"q", w*q* for humidity = .T. (instead of cloud_physics = .T.).
+
+Modifications to terminate coupled runs.
+
+advec_particles, check_for_restart, check_parameters, init_particles, init_3d_model, local_stop, timestep, time_integration
 
 
@@ -55,3 +64,5 @@
 Bugfix: Error message concerning output of particle concentration (pc) modified (check_parameters).
 
-advec_u_pw, advec_u_up, advec_v_pw, advec_v_up, boundary_conds, buoyancy, check_parameters, coriolis, diffusion_u, diffusion_v, init_pegrid, init_3d_model, modules, production_e, prognostic_equations, user_interface
+Bugfix: Rayleigh damping for ocean fixed.
+
+advec_u_pw, advec_u_up, advec_v_pw, advec_v_up, boundary_conds, buoyancy, check_parameters, coriolis, diffusion_u, diffusion_v, header, init_pegrid, init_3d_model, modules, production_e, prognostic_equations, user_interface
Index: palm/trunk/SOURCE/check_for_restart.f90
===================================================================
--- palm/trunk/SOURCE/check_for_restart.f90	(revision 107)
+++ palm/trunk/SOURCE/check_for_restart.f90	(revision 108)
@@ -4,4 +4,5 @@
 ! Actual revisions:
 ! -----------------
+! modifications to terminate coupled runs
 ! 
 !
@@ -67,4 +68,19 @@
 
 !
+!-- In case of coupled runs inform the remote model of the termination 
+!-- and its reason, provided the remote model has not already been 
+!-- informed of another termination reason (terminate_coupled > 0) before, 
+!-- or vice versa (terminate_coupled_remote > 0).
+    IF ( terminate_run .AND. TRIM( coupling_mode ) /= 'uncoupled'  &
+         .AND. terminate_coupled == 0  &
+         .AND. terminate_coupled_remote == 0 )  THEN
+       terminate_coupled = 3
+       CALL MPI_SENDRECV( &
+            terminate_coupled,        1, MPI_INTEGER, myid,  0, &
+            terminate_coupled_remote, 1, MPI_INTEGER, myid,  0, &
+            comm_inter, status, ierr )
+    ENDIF
+
+!
 !-- Set the stop flag also, if restart is forced by user
     IF ( time_restart /= 9999999.9  .AND.  time_restart < simulated_time ) &
@@ -89,5 +105,22 @@
              PRINT*, '                 new restart time is: ', time_restart, ' s'
           ENDIF
-    ELSE
+!
+!--       In case of coupled runs inform the remote model of the termination 
+!--       and its reason, provided the remote model has not already been 
+!--       informed of another termination reason (terminate_coupled > 0) before,
+!--       or vice versa (terminate_coupled_remote > 0).
+          IF ( coupling_mode /= 'uncoupled' .AND. terminate_coupled == 0  &
+               .AND. terminate_coupled_remote == 0)  THEN
+             IF ( dt_restart /= 9999999.9 )  THEN
+                terminate_coupled = 4
+             ELSE
+                terminate_coupled = 5
+             ENDIF
+             CALL MPI_SENDRECV( &
+                  terminate_coupled,        1, MPI_INTEGER, myid,  0, &
+                  terminate_coupled_remote, 1, MPI_INTEGER, myid,  0, &
+                  comm_inter, status, ierr )
+          ENDIF
+       ELSE
           time_restart = 9999999.9
        ENDIF
Index: palm/trunk/SOURCE/check_parameters.f90
===================================================================
--- palm/trunk/SOURCE/check_parameters.f90	(revision 107)
+++ palm/trunk/SOURCE/check_parameters.f90	(revision 108)
@@ -9,4 +9,8 @@
 ! Bugfix: Error message concerning output of particle concentration (pc)
 ! modified
+! More checks and more default values for coupled runs
+! allow data_output_pr= q, wq, w"q", w*q* for humidity = .T. (instead of 
+! cloud_physics = .T.)
+! Rayleigh damping for ocean fixed.
 !
 ! Former revisions:
@@ -76,8 +80,8 @@
     CHARACTER (LEN=100) ::  action
 
-    INTEGER ::  i, ilen, intervals, iter, j, k, nnxh, nnyh, position, prec
+    INTEGER ::  i, ilen, intervals, iremote = 0, iter, j, k, nnxh, nnyh, &
+         position, prec
     LOGICAL ::  found, ldum
-    REAL    ::  gradient, maxn, maxp
-
+    REAL    ::  gradient, maxn, maxp, remote = 0.0
 
 !
@@ -103,4 +107,114 @@
        CALL local_stop
     ENDIF
+
+!
+!-- Check dt_coupling, restart_time, dt_restart, end_time, dx, dy, nx and ny
+    IF ( coupling_mode /= 'uncoupled' )  THEN
+       IF ( dt_coupling == 9999999.9 )  THEN
+          IF ( myid == 0 )  THEN
+             PRINT*, '+++ check_parameters:'
+             PRINT*, '    dt_coupling is not set but required for coupling ', &
+                  'mode: ', TRIM( coupling_mode )
+          ENDIF
+          CALL local_stop
+       ENDIF
+#if defined( __parallel )  &&  defined( __mpi2 )
+       CALL MPI_SEND( dt_coupling, 1, MPI_REAL, myid, 11, comm_inter, ierr )
+       CALL MPI_RECV( remote, 1, MPI_REAL, myid, 11, comm_inter, status, ierr )
+       IF ( dt_coupling /= remote )  THEN
+          IF ( myid == 0 )  THEN
+             PRINT*, '+++ check_parameters:'
+             PRINT*, '    TRIM( coupling_mode ): dt_coupling = ', dt_coupling
+             PRINT*, '    is not equal to dt_coupling_remote = ', remote
+          ENDIF
+          CALL local_stop
+       ENDIF
+       CALL MPI_SEND( restart_time, 1, MPI_REAL, myid, 12, comm_inter, ierr )
+       CALL MPI_RECV( remote, 1, MPI_REAL, myid, 12, comm_inter, status, ierr )
+       IF ( restart_time /= remote )  THEN
+          IF ( myid == 0 )  THEN
+             PRINT*, '+++ check_parameters:'
+             PRINT*, '    TRIM( coupling_mode ): restart_time = ', restart_time
+             PRINT*, '    is not equal to restart_time_remote = ', remote
+          ENDIF
+          CALL local_stop
+       ENDIF
+       CALL MPI_SEND( dt_restart, 1, MPI_REAL, myid, 13, comm_inter, ierr )
+       CALL MPI_RECV( remote, 1, MPI_REAL, myid, 13, comm_inter, status, ierr )
+       IF ( dt_restart /= remote )  THEN
+          IF ( myid == 0 )  THEN
+             PRINT*, '+++ check_parameters:'
+             PRINT*, '    TRIM( coupling_mode ): dt_restart = ', dt_restart
+             PRINT*, '    is not equal to dt_restart_remote = ', remote
+          ENDIF
+          CALL local_stop
+       ENDIF
+       CALL MPI_SEND( end_time, 1, MPI_REAL, myid, 14, comm_inter, ierr )
+       CALL MPI_RECV( remote, 1, MPI_REAL, myid, 14, comm_inter, status, ierr )
+       IF ( end_time /= remote )  THEN
+          IF ( myid == 0 )  THEN
+             PRINT*, '+++ check_parameters:'
+             PRINT*, '    TRIM( coupling_mode ): end_time = ', end_time
+             PRINT*, '    is not equal to end_time_remote = ', remote
+          ENDIF
+          CALL local_stop
+       ENDIF
+       CALL MPI_SEND( dx, 1, MPI_REAL, myid, 15, comm_inter, ierr )
+       CALL MPI_RECV( remote, 1, MPI_REAL, myid, 15, comm_inter, status, ierr )
+       IF ( dx /= remote )  THEN
+          IF ( myid == 0 )  THEN
+             PRINT*, '+++ check_parameters:'
+             PRINT*, '    TRIM( coupling_mode ): dx = ', dx
+             PRINT*, '    is not equal to dx_remote = ', remote
+          ENDIF
+          CALL local_stop
+       ENDIF
+       CALL MPI_SEND( dy, 1, MPI_REAL, myid, 16, comm_inter, ierr )
+       CALL MPI_RECV( remote, 1, MPI_REAL, myid, 16, comm_inter, status, ierr )
+       IF ( dy /= remote )  THEN
+          IF ( myid == 0 )  THEN
+             PRINT*, '+++ check_parameters:'
+             PRINT*, '    TRIM( coupling_mode ): dy = ', dy
+             PRINT*, '    is not equal to dy_remote = ', remote
+          ENDIF
+          CALL local_stop
+       ENDIF
+       CALL MPI_SEND( nx, 1, MPI_INTEGER, myid, 17, comm_inter, ierr )
+       CALL MPI_RECV( iremote, 1, MPI_INTEGER, myid, 17, comm_inter, status, &
+            ierr )
+       IF ( nx /= iremote )  THEN
+          IF ( myid == 0 )  THEN
+             PRINT*, '+++ check_parameters:'
+             PRINT*, '    TRIM( coupling_mode ): nx = ', nx
+             PRINT*, '    is not equal to nx_remote = ', iremote
+          ENDIF
+          CALL local_stop
+       ENDIF
+       CALL MPI_SEND( ny, 1, MPI_INTEGER, myid, 18, comm_inter, ierr )
+       CALL MPI_RECV( iremote, 1, MPI_INTEGER, myid, 18, comm_inter, status, &
+            ierr )
+       IF ( ny /= iremote )  THEN
+          IF ( myid == 0 )  THEN
+             PRINT*, '+++ check_parameters:'
+             PRINT*, '    TRIM( coupling_mode ): ny = ', ny
+             PRINT*, '    is not equal to ny_remote = ', iremote
+          ENDIF
+          CALL local_stop
+       ENDIF
+#endif
+    ENDIF
+
+#if defined( __parallel )  &&  defined( __mpi2 )
+!
+!-- Exchange via intercommunicator
+    IF ( coupling_mode == 'atmosphere_to_ocean' )  THEN
+       CALL MPI_SEND( humidity, &
+            1, MPI_LOGICAL, myid, 19, comm_inter, ierr )
+    ELSEIF ( coupling_mode == 'ocean_to_atmosphere' )  THEN
+       CALL MPI_RECV( humidity_remote, &
+            1, MPI_LOGICAL, myid, 19, comm_inter, status, ierr )
+    ENDIF
+#endif
+
 
 !
@@ -821,6 +935,10 @@
 
 !
-!-- Ocean version is using flux boundary conditions at the top
-    IF ( ocean )  use_top_fluxes = .TRUE.
+!-- Ocean version must use flux boundary conditions at the top
+    IF ( ocean .AND. .NOT. use_top_fluxes )  THEN
+       IF ( myid == 0 )  PRINT*, '+++ check_parameters: use_top_fluxes ',&
+                                    'must be .TRUE. in ocean version'
+       CALL local_stop
+    ENDIF
 
 !
@@ -1256,14 +1374,19 @@
        CALL local_stop
     ENDIF
-    IF ( bc_uv_t == 'dirichlet' )  THEN
-       ibc_uv_t = 0
-    ELSEIF ( bc_uv_t == 'neumann' )  THEN
+    IF ( coupling_mode == 'ocean_to_atmosphere' )  THEN
+       bc_uv_t = 'neumann'
        ibc_uv_t = 1
     ELSE
-       IF ( myid == 0 )  THEN
-          PRINT*, '+++ check_parameters:'
-          PRINT*, '    unknown boundary condition: bc_uv_t = ', bc_uv_t
-       ENDIF
-       CALL local_stop
+       IF ( bc_uv_t == 'dirichlet' )  THEN
+          ibc_uv_t = 0
+       ELSEIF ( bc_uv_t == 'neumann' )  THEN
+          ibc_uv_t = 1
+       ELSE
+          IF ( myid == 0 )  THEN
+             PRINT*, '+++ check_parameters:'
+             PRINT*, '    unknown boundary condition: bc_uv_t = ', bc_uv_t
+          ENDIF
+          CALL local_stop
+       ENDIF
     ENDIF
 
@@ -1289,14 +1412,30 @@
 
     IF ( rayleigh_damping_height == -1.0 )  THEN
-       rayleigh_damping_height = 0.66666666666 * zu(nzt)
+       IF ( .NOT. ocean )  THEN
+          rayleigh_damping_height = 0.66666666666 * zu(nzt)
+       ELSE
+          rayleigh_damping_height = 0.66666666666 * zu(nzb)
+       ENDIF
     ELSE
-       IF ( rayleigh_damping_height < 0.0  .OR. &
-            rayleigh_damping_height > zu(nzt) )  THEN
-          IF ( myid == 0 )  THEN
-             PRINT*, '+++ check_parameters:'
-             PRINT*, '    rayleigh_damping_height = ', rayleigh_damping_height,&
-                          ' out of range [0.0,', zu(nzt), ']'
-          ENDIF
-          CALL local_stop
+       IF ( .NOT. ocean )  THEN
+          IF ( rayleigh_damping_height < 0.0  .OR. &
+               rayleigh_damping_height > zu(nzt) )  THEN
+             IF ( myid == 0 )  THEN
+                PRINT*, '+++ check_parameters:'
+                PRINT*, '    rayleigh_damping_height = ', rayleigh_damping_height,&
+                     ' out of range [0.0,', zu(nzt), ']'
+             ENDIF
+             CALL local_stop
+          ENDIF
+       ELSE
+          IF ( rayleigh_damping_height > 0.0  .OR. &
+               rayleigh_damping_height < zu(nzb) )  THEN
+             IF ( myid == 0 )  THEN
+                PRINT*, '+++ check_parameters:'
+                PRINT*, '    rayleigh_damping_height = ', rayleigh_damping_height,&
+                     ' out of range [0.0,', zu(nzb), ']'
+             ENDIF
+             CALL local_stop
+          ENDIF
        ENDIF
     ENDIF
@@ -1695,9 +1834,9 @@
 
           CASE ( 'q', '#q' )
-             IF ( .NOT. cloud_physics )  THEN
+             IF ( .NOT. humidity )  THEN
                 IF ( myid == 0 )  THEN
                    PRINT*, '+++ check_parameters:  data_output_pr = ', &
                            data_output_pr(i),                          &
-                           '    is not implemented for cloud_physics = FALSE'
+                           '    is not implemented for humidity = FALSE'
                 ENDIF
                 CALL local_stop
@@ -1804,9 +1943,9 @@
 
           CASE ( 'w"q"' )
-             IF ( .NOT. cloud_physics ) THEN
+             IF ( .NOT. humidity )  THEN
                 IF ( myid == 0 )  THEN
                    PRINT*, '+++ check_parameters:  data_output_pr = ', &
                            data_output_pr(i),                          &
-                           '    is not implemented for cloud_physics = FALSE'
+                           '    is not implemented for humidity = FALSE'
                 ENDIF
                 CALL local_stop
@@ -1818,9 +1957,9 @@
 
           CASE ( 'w*q*' )
-             IF ( .NOT. cloud_physics ) THEN
+             IF ( .NOT. humidity )  THEN
                 IF ( myid == 0 )  THEN
                    PRINT*, '+++ check_parameters:  data_output_pr = ', &
                            data_output_pr(i),                          &
-                           '    is not implemented for cloud_physics = FALSE'
+                           '    is not implemented for humidity = FALSE'
                 ENDIF
                 CALL local_stop
@@ -1832,9 +1971,9 @@
 
           CASE ( 'wq' )
-             IF ( .NOT. cloud_physics ) THEN
+             IF ( .NOT. humidity )  THEN
                 IF ( myid == 0 )  THEN
                    PRINT*, '+++ check_parameters:  data_output_pr = ', &
                            data_output_pr(i),                          &
-                           '    is not implemented for cloud_physics = FALSE'
+                           '    is not implemented for humidity = FALSE'
                 ENDIF
                 CALL local_stop
Index: palm/trunk/SOURCE/data_output_2d.f90
===================================================================
--- palm/trunk/SOURCE/data_output_2d.f90	(revision 107)
+++ palm/trunk/SOURCE/data_output_2d.f90	(revision 108)
@@ -732,5 +732,5 @@
                 CASE ( 'xz' )
 !
-!--                Update the NetCDF xy cross section time axis
+!--                Update the NetCDF xz cross section time axis
                    IF ( myid == 0 )  THEN
                       IF ( simulated_time /= do2d_xz_last_time(av) )  THEN
Index: palm/trunk/SOURCE/header.f90
===================================================================
--- palm/trunk/SOURCE/header.f90	(revision 107)
+++ palm/trunk/SOURCE/header.f90	(revision 108)
@@ -6,4 +6,5 @@
 ! Output of informations for coupled model runs (boundary conditions etc.)
 ! + output of momentumfluxes at the top boundary
+! Rayleigh damping for ocean, e_init
 !
 ! Former revisions:
@@ -241,5 +242,11 @@
     IF ( use_upstream_for_tke )  WRITE ( io, 143 )
     IF ( rayleigh_damping_factor /= 0.0 )  THEN
-       WRITE ( io, 123 )  rayleigh_damping_height, rayleigh_damping_factor
+       IF ( .NOT. ocean )  THEN
+          WRITE ( io, 123 )  'above', rayleigh_damping_height, &
+               rayleigh_damping_factor
+       ELSE
+          WRITE ( io, 123 )  'below', rayleigh_damping_height, &
+               rayleigh_damping_factor
+       ENDIF
     ENDIF
     IF ( humidity )  THEN
@@ -1091,4 +1098,5 @@
     ENDIF
     IF ( .NOT. constant_diffusion)  THEN
+       IF ( e_init > 0.0 )  WRITE ( io, 455 )  e_init
        IF ( e_min > 0.0 )  WRITE ( io, 454 )  e_min
        IF ( wall_adjustment )  WRITE ( io, 453 )  wall_adjustment_factor
@@ -1240,5 +1248,5 @@
                   ' timestep changes')
 122 FORMAT (' --> Time differencing scheme: ',A)
-123 FORMAT (' --> Rayleigh-Damping active, starts above z = ',F8.2,' m'/ &
+123 FORMAT (' --> Rayleigh-Damping active, starts ',A,' z = ',F8.2,' m'/ &
             '     maximum damping coefficient: ',F5.3, ' 1/s')
 124 FORMAT ('     Spline-overshoots are being suppressed')
@@ -1468,4 +1476,5 @@
 453 FORMAT ('   Mixing length is limited to ',F4.2,' * z')
 454 FORMAT ('   TKE is not allowed to fall below ',E9.2,' (m/s)**2')
+455 FORMAT ('   initial TKE is prescribed as ',E9.2,' (m/s)**2')
 470 FORMAT (//' Actions during the simulation:'/ &
               ' -----------------------------'/)
Index: palm/trunk/SOURCE/init_3d_model.f90
===================================================================
--- palm/trunk/SOURCE/init_3d_model.f90	(revision 107)
+++ palm/trunk/SOURCE/init_3d_model.f90	(revision 108)
@@ -9,4 +9,7 @@
 ! Flux initialization in case of coupled runs, +momentum fluxes at top boundary,
 ! +arrays for phase speed c_u, c_v, c_w, indices for u|v|w_m_l|r changed
+! +qswst_remote in case of atmosphere model with humidity coupled to ocean
+! Rayleigh damping for ocean
+! optionally calculate km and kh from initial TKE e_init
 !
 ! Former revisions:
@@ -204,4 +207,8 @@
        rho => rho_1  ! routine calc_mean_profile requires density to be a
                      ! pointer
+       IF ( humidity_remote )  THEN
+          ALLOCATE( qswst_remote(nys-1:nyn+1,nxl-1:nxr+1) )
+          qswst_remote = 0.0
+       ENDIF
     ENDIF
 
@@ -507,11 +514,25 @@
              km   = km_constant
              kh   = km / prandtl_number
+             e    = 0.0
+          ELSEIF ( e_init > 0.0 )  THEN
+             DO  k = nzb+1, nzt
+                km(k,:,:) = 0.1 * l_grid(k) * SQRT( e_init )
+             ENDDO
+             km(nzb,:,:)   = km(nzb+1,:,:)
+             km(nzt+1,:,:) = km(nzt,:,:)
+             kh   = km / prandtl_number
+             e    = e_init
           ELSE
-             kh   = 0.01   ! there must exist an initial diffusion, because 
-             km   = 0.01   ! otherwise no TKE would be produced by the
-                           ! production terms, as long as not yet
-                           ! e = (u*/cm)**2 at k=nzb+1
-          ENDIF
-          e     = 0.0
+             IF ( .NOT. ocean )  THEN
+                kh   = 0.01   ! there must exist an initial diffusion, because 
+                km   = 0.01   ! otherwise no TKE would be produced by the
+                              ! production terms, as long as not yet
+                              ! e = (u*/cm)**2 at k=nzb+1
+             ELSE
+                kh   = 0.00001
+                km   = 0.00001
+             ENDIF
+             e    = 0.0
+          ENDIF
           rif   = 0.0
           ts    = 0.0
@@ -892,12 +913,23 @@
     rdf = 0.0
     IF ( rayleigh_damping_factor /= 0.0 )  THEN
-       DO  k = nzb+1, nzt
-          IF ( zu(k) >= rayleigh_damping_height )  THEN
-             rdf(k) = rayleigh_damping_factor * &
+       IF ( .NOT. ocean )  THEN
+          DO  k = nzb+1, nzt
+             IF ( zu(k) >= rayleigh_damping_height )  THEN
+                rdf(k) = rayleigh_damping_factor * &
                       ( SIN( pi * 0.5 * ( zu(k) - rayleigh_damping_height )    &
                                       / ( zu(nzt) - rayleigh_damping_height ) )&
                       )**2
-          ENDIF
-       ENDDO
+             ENDIF
+          ENDDO
+       ELSE
+          DO  k = nzt, nzb+1, -1
+             IF ( zu(k) <= rayleigh_damping_height )  THEN
+                rdf(k) = rayleigh_damping_factor * &
+                      ( SIN( pi * 0.5 * ( rayleigh_damping_height - zu(k) )    &
+                                      / ( rayleigh_damping_height - zu(nzb+1)))&
+                      )**2
+             ENDIF
+          ENDDO
+       ENDIF
     ENDIF
 
Index: palm/trunk/SOURCE/init_pegrid.f90
===================================================================
--- palm/trunk/SOURCE/init_pegrid.f90	(revision 107)
+++ palm/trunk/SOURCE/init_pegrid.f90	(revision 108)
@@ -4,6 +4,7 @@
 ! Actual revisions:
 ! -----------------
+! TEST OUTPUT (TO BE REMOVED) logging mpi2 ierr values
 ! Intercommunicator (comm_inter) and derived data type (type_xy) for
-! coupled model runs created,
+! coupled model runs created, assign coupling_mode_remote
 ! indices nxlu and nysv are calculated (needed for non-cyclic boundary
 ! conditions)
@@ -467,5 +468,5 @@
 
 !
-!--    Recieve data from the other PEs
+!--    Receive data from the other PEs
        DO  i = 1,numprocs-1
           CALL MPI_RECV( ibuf, 12, MPI_INTEGER, i, MPI_ANY_TAG, comm2d, status, &
@@ -494,6 +495,18 @@
 
           CALL MPI_OPEN_PORT( MPI_INFO_NULL, port_name, ierr )
+!
+!--       TEST OUTPUT (TO BE REMOVED)
+          WRITE(9,*)  TRIM( coupling_mode ),  &
+               ', ierr after MPI_OPEN_PORT: ', ierr
+          CALL LOCAL_FLUSH( 9 )
+
           CALL MPI_PUBLISH_NAME( 'palm_coupler', MPI_INFO_NULL, port_name, &
                                  ierr )
+!
+!--       TEST OUTPUT (TO BE REMOVED)
+          WRITE(9,*)  TRIM( coupling_mode ),  &
+               ', ierr after MPI_PUBLISH_NAME: ', ierr
+          CALL LOCAL_FLUSH( 9 )
+
 !
 !--       Write a flag file for the ocean model and the other atmosphere
@@ -519,4 +532,10 @@
 
           CALL MPI_LOOKUP_NAME( 'palm_coupler', MPI_INFO_NULL, port_name, ierr )
+!
+!--       TEST OUTPUT (TO BE REMOVED)
+          WRITE(9,*)  TRIM( coupling_mode ),  &
+               ', ierr after MPI_LOOKUP_NAME: ', ierr
+          CALL LOCAL_FLUSH( 9 )
+
 
        ENDIF
@@ -535,4 +554,6 @@
        print*, '--- ierr = ', ierr
        print*, '--- comm_inter atmosphere = ', comm_inter
+
+       coupling_mode_remote = 'ocean_to_atmosphere'
 
     ELSEIF ( coupling_mode == 'ocean_to_atmosphere' )  THEN
@@ -544,4 +565,6 @@
        print*, '--- ierr = ', ierr
        print*, '--- comm_inter ocean      = ', comm_inter
+
+       coupling_mode_remote = 'atmosphere_to_ocean'
 
     ENDIF
Index: palm/trunk/SOURCE/local_stop.f90
===================================================================
--- palm/trunk/SOURCE/local_stop.f90	(revision 107)
+++ palm/trunk/SOURCE/local_stop.f90	(revision 108)
@@ -4,4 +4,5 @@
 ! Actual revisions:
 ! -----------------
+! modifications to terminate coupled runs
 ! 
 !
@@ -24,10 +25,87 @@
 
     USE pegrid
+    USE control_parameters
 
 #if defined( __parallel )
-    CALL MPI_FINALIZE( ierr )
-#endif
+    IF ( coupling_mode == 'uncoupled' )  THEN
+       CALL MPI_FINALIZE( ierr )
+    ELSE
+
+       SELECT CASE ( terminate_coupled_remote )
+
+          CASE ( 0 )
+             IF ( myid == 0 )  THEN
+                PRINT*, '+++ local_stop:'
+                PRINT*, '    local model "', TRIM( coupling_mode ), &
+                     '" stops now'
+             ENDIF
+!
+!--          Inform the remote model of the termination and its reason, provided
+!--          the remote model has not already been informed of another 
+!--          termination reason (terminate_coupled > 0) before.
+             IF ( terminate_coupled == 0 )  THEN
+                terminate_coupled = 1
+                CALL MPI_SENDRECV( &
+                     terminate_coupled,        1, MPI_INTEGER, myid,  0, &
+                     terminate_coupled_remote, 1, MPI_INTEGER, myid,  0, &
+                     comm_inter, status, ierr )
+             ENDIF
+             CALL MPI_FINALIZE( ierr )
+
+          CASE ( 1 )
+             IF ( myid == 0 )  THEN
+                PRINT*, '+++ local_stop:'
+                PRINT*, '    remote model "', TRIM( coupling_mode_remote ), &
+                     '" stopped'
+             ENDIF
+             CALL MPI_FINALIZE( ierr )
+
+          CASE ( 2 )
+             IF ( myid == 0 )  THEN
+                PRINT*, '+++ local_stop:'
+                PRINT*, '    remote model "', TRIM( coupling_mode_remote ), &
+                     '" terminated'
+                PRINT*, '    with stop_dt = .T.'
+             ENDIF
+             stop_dt = .TRUE.
+
+          CASE ( 3 )
+             IF ( myid == 0 )  THEN
+                PRINT*, '+++ local_stop:'
+                PRINT*, '    remote model "', TRIM( coupling_mode_remote ), &
+                     '" terminated'
+                PRINT*, '    with terminate_run = .T. (CPU-time limit)'
+             ENDIF
+             terminate_run = .TRUE.
+
+          CASE ( 4 )
+             IF ( myid == 0 )  THEN
+                PRINT*, '+++ local_stop:'
+                PRINT*, '    remote model "', TRIM( coupling_mode_remote ), &
+                     '" terminated'
+                PRINT*, '    with terminate_run = .T. (restart)'
+             ENDIF
+             terminate_run = .TRUE.
+             time_restart = time_restart + dt_restart
+
+          CASE ( 5 )
+             IF ( myid == 0 )  THEN
+                PRINT*, '+++ local_stop:'
+                PRINT*, '    remote model "', TRIM( coupling_mode_remote ), &
+                     '" terminated'
+                PRINT*, '    with terminate_run = .T. (single restart)'
+             ENDIF
+             terminate_run = .TRUE.
+             time_restart = 9999999.9
+
+       END SELECT
+
+    ENDIF
+
+#else
 
     STOP
 
+#endif
+
  END SUBROUTINE local_stop    
Index: palm/trunk/SOURCE/modules.f90
===================================================================
--- palm/trunk/SOURCE/modules.f90	(revision 107)
+++ palm/trunk/SOURCE/modules.f90	(revision 108)
@@ -6,6 +6,8 @@
 ! -----------------
 ! +comm_inter, constant_top_momentumflux, coupling_char, coupling_mode,
-! c_u, c_v, c_w, dt_coupling, ngp_xy, nxlu, nysv, port_name, time_coupling,
-! top_momentumflux_u|v, type_xy, uswst*, vswst*
+! coupling_mode_remote, c_u, c_v, c_w, dt_coupling, e_init, humidity_remote, 
+! ngp_xy, nxlu, nysv, port_name, qswst_remote, terminate_coupled, 
+! terminate_coupled_remote, time_coupling, top_momentumflux_u|v, type_xy, 
+! uswst*, vswst*
 !
 ! Former revisions:
@@ -106,5 +108,5 @@
     REAL, DIMENSION(:,:), ALLOCATABLE ::                                       &
           c_u, c_v, c_w, dzu_mg, dzw_mg, f1_mg, f2_mg, f3_mg, pt_slope_ref,    &
-          qs, ts, us, z0
+          qs, qswst_remote, ts, us, z0
 
     REAL, DIMENSION(:,:), ALLOCATABLE, TARGET ::                               &
@@ -253,4 +255,5 @@
                             bc_uv_b = 'dirichlet', bc_uv_t = 'dirichlet', &
                             coupling_mode = 'uncoupled', &
+                            coupling_mode_remote = 'uncoupled', &
                             dissipation_1d = 'as_in_3d_model', &
                             fft_method = 'system-specific', &
@@ -292,4 +295,5 @@
                 nz_do1d, nz_do3d = -9999, outflow_damping_width = -1, &
                 prt_time_count = 0, runnr = 0, skip_do_avs = 0, &
+                terminate_coupled = 0, terminate_coupled_remote = 0, &
                 timestep_count = 0
 
@@ -323,5 +327,6 @@
                 first_call_advec_particles = .TRUE., &
                 force_print_header = .FALSE., galilei_transformation = .FALSE.,&
-                humidity = .FALSE., inflow_l = .FALSE., inflow_n = .FALSE., &
+                humidity = .FALSE., humidity_remote = .FALSE., &
+                inflow_l = .FALSE., inflow_n = .FALSE., &
                 inflow_r = .FALSE., inflow_s = .FALSE., iso2d_output = .FALSE.,&
                 mg_switch_to_pe0 = .FALSE., &
@@ -366,5 +371,6 @@
              dt_run_control = 60.0, dt_3d = -1.0, dz = -1.0, &
              dz_max = 9999999.9, dz_stretch_factor = 1.08, &
-             dz_stretch_level = 100000.0, e_min = 0.0, end_time = 0.0, &
+             dz_stretch_level = 100000.0, e_init = 0.0, e_min = 0.0, &
+             end_time = 0.0, &
              f = 0.0, fs = 0.0, g = 9.81, kappa = 0.4, km_constant = -1.0, &
              km_damp_max = -1.0, long_filter_factor = 0.0, &
Index: palm/trunk/SOURCE/palm.f90
===================================================================
--- palm/trunk/SOURCE/palm.f90	(revision 107)
+++ palm/trunk/SOURCE/palm.f90	(revision 108)
@@ -5,4 +5,5 @@
 ! -----------------
 ! Get coupling mode from environment variable
+! Change localtion of debug output
 !
 ! Former revisions:
@@ -101,16 +102,8 @@
     CALL MPI_COMM_RANK( comm_palm, myid, ierr )
 #endif
-    CALL init_dvrp_logging
-
-!
-!-- Read control parameters from NAMELIST files and read environment-variables
-    CALL parin
-
-!
-!-- Determine processor topology and local array indices
-    CALL init_pegrid
 
 !
 !-- Open a file for debug output
+    WRITE (myid_char,'(''_'',I4.4)')  myid
     OPEN( 9, FILE='DEBUG'//TRIM( coupling_char )//myid_char, FORM='FORMATTED' )
 
@@ -122,4 +115,14 @@
     print*, '*** PE', myid, '  ', TRIM( coupling_mode )
 #endif
+
+    CALL init_dvrp_logging
+
+!
+!-- Read control parameters from NAMELIST files and read environment-variables
+    CALL parin
+
+!
+!-- Determine processor topology and local array indices
+    CALL init_pegrid
 
 !
Index: palm/trunk/SOURCE/parin.f90
===================================================================
--- palm/trunk/SOURCE/parin.f90	(revision 107)
+++ palm/trunk/SOURCE/parin.f90	(revision 108)
@@ -4,5 +4,5 @@
 ! Actual revisions:
 ! -----------------
-! +top_momentumflux_u|v in inipar, +dt_coupling in d3par
+! +e_init, top_momentumflux_u|v in inipar, +dt_coupling in d3par
 !
 ! Former revisions:
@@ -72,5 +72,6 @@
              conserve_volume_flow, cut_spline_overshoot, damp_level_1d, &
              dissipation_1d, dt, dt_pr_1d, dt_run_control_1d, dx, dy, dz, &
-             dz_max, dz_stretch_factor, dz_stretch_level, e_min, end_time_1d, &
+             dz_max, dz_stretch_factor, dz_stretch_level, e_init, e_min, &
+             end_time_1d, &
              fft_method, galilei_transformation, grid_matching, humidity, &
              inflow_disturbance_begin, inflow_disturbance_end, &
Index: palm/trunk/SOURCE/prandtl_fluxes.f90
===================================================================
--- palm/trunk/SOURCE/prandtl_fluxes.f90	(revision 107)
+++ palm/trunk/SOURCE/prandtl_fluxes.f90	(revision 108)
@@ -4,5 +4,5 @@
 ! Actual revisions:
 ! -----------------
-! 
+! assume saturation at k=nzb_s_inner(j,i) for atmosphere coupled to ocean
 !
 ! Former revisions:
@@ -36,5 +36,5 @@
 
     INTEGER ::  i, j, k
-    REAL    ::  a, b, rifm, uv_total, z_p
+    REAL    ::  a, b, e_q, rifm, uv_total, z_p
 
 !
@@ -297,4 +297,11 @@
                 z_p = zu(k+1) - zw(k)
 
+!
+!--             assume saturation for atmosphere coupled to ocean
+                IF ( coupling_mode == 'atmosphere_to_ocean' )  THEN
+                   e_q = 6.1 * &
+                        EXP( 0.07 * ( MIN(pt(0,j,i),pt(1,j,i)) - 273.15 ) )
+                   q(k,j,i) = 0.622 * e_q / ( surface_pressure - e_q )
+                ENDIF
                 IF ( rif(j,i) >= 0.0 )  THEN
 !
Index: palm/trunk/SOURCE/production_e.f90
===================================================================
--- palm/trunk/SOURCE/production_e.f90	(revision 107)
+++ palm/trunk/SOURCE/production_e.f90	(revision 108)
@@ -8,4 +8,5 @@
 ! u_0 and v_0 are calculated for nxr+1, nyn+1 also (otherwise these values are
 ! not available in case of non-cyclic boundary conditions)
+! Bugfix for ocean density flux at bottom
 !
 ! Former revisions:
@@ -810,5 +811,8 @@
 !--             So far in the ocean no special treatment of density flux in the
 !--             bottom and top surface layer
-                DO  k = nzb_s_inner(j,i), nzt
+                k= nzb_s_inner(j,i)
+                tend(k,j,i) = tend(k,j,i) + kh(k,j,i) * g / prho_reference * &
+                     ( rho(k+1,j,i) - rho(k,j,i) ) * dzu(k+1)
+                DO  k = nzb_s_inner(j,i)+1, nzt
                    tend(k,j,i) = tend(k,j,i) + kh(k,j,i) * g / prho_reference * &
                                       ( rho(k+1,j,i) - rho(k-1,j,i) ) * dd2zu(k)
Index: palm/trunk/SOURCE/surface_coupler.f90
===================================================================
--- palm/trunk/SOURCE/surface_coupler.f90	(revision 107)
+++ palm/trunk/SOURCE/surface_coupler.f90	(revision 108)
@@ -4,4 +4,6 @@
 ! Actual revisions:
 ! -----------------
+! include latent heatflux and salinity flux for atmosphere runs with moisture
+! modifications to terminate coupled runs
 ! 
 !
@@ -27,5 +29,5 @@
     IMPLICIT NONE
 
-    INTEGER ::  k
+    INTEGER ::  i, j, k
 
     REAL    ::  simulated_time_remote
@@ -36,5 +38,30 @@
 
 !
-!-- First exchange the current simulated time between the models,
+!-- In case of model termination initiated by the remote model 
+!-- (terminate_coupled_remote > 0), initiate termination of the local model. 
+!-- The rest of the coupler must then be skipped because it would cause an MPI 
+!-- intercomminucation hang.
+!-- If necessary, the coupler will be called at the beginning of the next 
+!-- restart run.
+    CALL MPI_SENDRECV( terminate_coupled,        1, MPI_INTEGER, myid,  0, &
+                       terminate_coupled_remote, 1, MPI_INTEGER, myid,  0, &
+                       comm_inter, status, ierr )
+    IF ( terminate_coupled_remote > 0 )  THEN
+       IF ( myid == 0 )  THEN
+          PRINT*, '+++ surface_coupler:'
+          PRINT*, '    remote model "', TRIM( coupling_mode_remote ), &
+               '" terminated'
+          PRINT*, '    with terminate_coupled_remote = ', &
+               terminate_coupled_remote
+          PRINT*, '    local model  "', TRIM( coupling_mode ), &
+               '" has'
+          PRINT*, '    terminate_coupled = ', &
+               terminate_coupled
+       ENDIF
+       CALL local_stop
+       RETURN
+    ENDIF
+!
+!-- Exchange the current simulated time between the models,
 !-- currently just for testing
     CALL MPI_SEND( simulated_time, 1, MPI_REAL, myid, 11, comm_inter, ierr )
@@ -58,8 +85,19 @@
 
 !
+!--    Send humidity flux at bottom surface to the ocean model
+       IF ( humidity )  THEN
+          WRITE ( 9, * )  '*** send qsws to ocean'
+          CALL local_flush( 9 )
+          CALL MPI_SEND( qsws(nys-1,nxl-1), ngp_xy, MPI_REAL, myid, 13, &
+               comm_inter, ierr )
+          WRITE ( 9, * )  '    ready'
+          CALL local_flush( 9 )
+       ENDIF
+
+!
 !--    Receive temperature at the bottom surface from the ocean model
        WRITE ( 9, * )  '*** receive pt from ocean'
        CALL local_flush( 9 )
-       CALL MPI_RECV( pt(0,nys-1,nxl-1), 1, type_xy, myid, 13, comm_inter, &
+       CALL MPI_RECV( pt(0,nys-1,nxl-1), 1, type_xy, myid, 14, comm_inter, &
                       status, ierr )
        WRITE ( 9, * )  '    ready'
@@ -70,5 +108,5 @@
        WRITE ( 9, * )  '*** send usws to ocean'
        CALL local_flush( 9 )
-       CALL MPI_SEND( usws(nys-1,nxl-1), ngp_xy, MPI_REAL, myid, 14, &
+       CALL MPI_SEND( usws(nys-1,nxl-1), ngp_xy, MPI_REAL, myid, 15, &
                       comm_inter, ierr )
        WRITE ( 9, * )  '    ready'
@@ -79,5 +117,5 @@
        WRITE ( 9, * )  '*** send vsws to ocean'
        CALL local_flush( 9 )
-       CALL MPI_SEND( vsws(nys-1,nxl-1), ngp_xy, MPI_REAL, myid, 15, &
+       CALL MPI_SEND( vsws(nys-1,nxl-1), ngp_xy, MPI_REAL, myid, 16, &
                       comm_inter, ierr )
        WRITE ( 9, * )  '    ready'
@@ -96,7 +134,28 @@
 
 !
+!--    Receive humidity flux from the atmosphere model (bottom)
+!--    and add it to the heat flux at the sea surface (top)...
+       IF ( humidity_remote )  THEN
+          WRITE ( 9, * )  '*** receive qswst_remote from atmosphere'
+          CALL local_flush( 9 )
+          CALL MPI_RECV( qswst_remote(nys-1,nxl-1), ngp_xy, MPI_REAL, myid, &
+               13, comm_inter, status, ierr )
+          WRITE ( 9, * )  '    ready'
+          CALL local_flush( 9 )
+
+          tswst = tswst + qswst_remote * 2.2626108e6
+          !latent heat of vaporization in m2/s2, or 540 cal/g, or 40.65 kJ/mol
+!
+!--    ...and convert it to a salinity flux at the sea surface (top)
+!--       following Steinhorn (1991), JPO 21, pp. 1681-1683:
+!--       S'w' = -S * evaporation / ( rho_water * ( 1 - S ) )
+          saswst = -1.0 * sa(nzt,:,:) * qswst_remote /  &
+               ( rho(nzt,:,:) * ( 1.0 - sa(nzt,:,:) ) )
+       ENDIF
+
+!
 !--    Adjust the kinematic heat flux with respect to ocean density
 !--    (constants are the specific heat capacities for air and water)
-       tswst = tswst / rho_surface * 1005.0 / 4218.0
+       tswst = tswst / rho(nzt,:,:) * 1005.0 / 4218.0
 
 !
@@ -104,5 +163,5 @@
        WRITE ( 9, * )  '*** send pt to atmosphere'
        CALL local_flush( 9 )
-       CALL MPI_SEND( pt(nzt,nys-1,nxl-1), 1, type_xy, myid, 13, comm_inter, &
+       CALL MPI_SEND( pt(nzt,nys-1,nxl-1), 1, type_xy, myid, 14, comm_inter, &
                       ierr )
        WRITE ( 9, * )  '    ready'
@@ -114,5 +173,5 @@
        WRITE ( 9, * )  '*** receive uswst from atmosphere'
        CALL local_flush( 9 )
-       CALL MPI_RECV( uswst(nys-1,nxl-1), ngp_xy, MPI_REAL, myid, 14, &
+       CALL MPI_RECV( uswst(nys-1,nxl-1), ngp_xy, MPI_REAL, myid, 15, &
                       comm_inter, status, ierr )
        WRITE ( 9, * )  '    ready'
@@ -124,5 +183,5 @@
        WRITE ( 9, * )  '*** receive vswst from atmosphere'
        CALL local_flush( 9 )
-       CALL MPI_RECV( vswst(nys-1,nxl-1), ngp_xy, MPI_REAL, myid, 15, &
+       CALL MPI_RECV( vswst(nys-1,nxl-1), ngp_xy, MPI_REAL, myid, 16, &
                       comm_inter, status, ierr )
        WRITE ( 9, * )  '    ready'
@@ -131,6 +190,6 @@
 !
 !--    Adjust the momentum fluxes with respect to ocean density
-       uswst = uswst / rho_surface
-       vswst = vswst / rho_surface
+       uswst = uswst / rho(nzt,:,:)
+       vswst = vswst / rho(nzt,:,:)
 
     ENDIF
Index: palm/trunk/SOURCE/time_integration.f90
===================================================================
--- palm/trunk/SOURCE/time_integration.f90	(revision 107)
+++ palm/trunk/SOURCE/time_integration.f90	(revision 108)
@@ -9,4 +9,6 @@
 ! random perturbation has to be added to the velocity fields also after the
 ! first substep
+! modifications to terminate coupled runs
+! 
 !
 ! Former revisions:
@@ -78,4 +80,18 @@
     CALL run_control
 
+!
+!-- Data exchange between coupled models in case that a call has been omitted 
+!-- at the end of the previous run of a job chain.
+    IF ( coupling_mode /= 'uncoupled' )  THEN
+!
+!--    In case of model termination initiated by the local model the coupler 
+!--    must not be called because this would again cause an MPI hang. 
+       DO WHILE ( time_coupling >= dt_coupling .AND. terminate_coupled == 0 )
+          CALL surface_coupler
+          time_coupling = time_coupling - dt_coupling
+       ENDDO
+    ENDIF
+
+
 #if defined( __dvrp_graphics )
 !
@@ -306,5 +322,11 @@
        IF ( coupling_mode /= 'uncoupled' )  THEN
           time_coupling = time_coupling + dt_3d
-          DO WHILE ( time_coupling >= dt_coupling )
+!
+!--       In case of model termination initiated by the local model 
+!--       (terminate_coupled > 0), the coupler must be skipped because it would 
+!--       cause an MPI intercomminucation hang.
+!--       If necessary, the coupler will be called at the beginning of the 
+!--       next restart run.
+          DO WHILE ( time_coupling >= dt_coupling .AND. terminate_coupled == 0 )
              CALL surface_coupler
              time_coupling = time_coupling - dt_coupling
@@ -327,5 +349,6 @@
 !--    Check, if restart is necessary (because cpu-time is expiring or
 !--    because it is forced by user) and set stop flag
-       CALL check_for_restart
+!--    This call is skipped if the remote model has already initiated a restart.
+       IF ( .NOT. terminate_run )  CALL check_for_restart
 
 !
Index: palm/trunk/SOURCE/timestep.f90
===================================================================
--- palm/trunk/SOURCE/timestep.f90	(revision 107)
+++ palm/trunk/SOURCE/timestep.f90	(revision 108)
@@ -4,4 +4,5 @@
 ! Actual revisions:
 ! -----------------
+! modifications to terminate coupled runs
 ! 
 !
@@ -192,4 +193,5 @@
        IF ( dt_3d < ( 0.00001 * dt_max ) )  THEN
           stop_dt = .TRUE.
+
           IF ( myid == 0 )  THEN
              PRINT*,'+++ time_step: Time step has reached minimum limit.'
@@ -207,4 +209,16 @@
                     '  j=', w_max_ijk(2), '  i=', w_max_ijk(3)
           ENDIF
+!
+!--       In case of coupled runs inform the remote model of the termination 
+!--       and its reason, provided the remote model has not already been 
+!--       informed of another termination reason (terminate_coupled > 0) before.
+          IF ( coupling_mode /= 'uncoupled' .AND. terminate_coupled == 0 )  THEN
+             terminate_coupled = 2
+             CALL MPI_SENDRECV( &
+                  terminate_coupled,        1, MPI_INTEGER, myid,  0, &
+                  terminate_coupled_remote, 1, MPI_INTEGER, myid,  0, &
+                  comm_inter, status, ierr )
+          ENDIF
+
        ENDIF
 
