Index: palm/trunk/SOURCE/close_file.f90
===================================================================
--- palm/trunk/SOURCE/close_file.f90	(revision 4901)
+++ palm/trunk/SOURCE/close_file.f90	(revision 1)
@@ -1,38 +1,56 @@
-!> @file close_file.f90
-!--------------------------------------------------------------------------------------------------!
-! This file is part of the PALM model system.
-!
-! 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-2021 Leibniz Universitaet Hannover
-!--------------------------------------------------------------------------------------------------!
-!
-! Current revisions:
+ SUBROUTINE close_file( file_id )
+
+!------------------------------------------------------------------------------!
+! Actual revisions:
 ! -----------------
 ! 
-! 
+!
 ! Former revisions:
 ! -----------------
-! $Id$
-! file re-formatted to follow the PALM coding standard
-!
-! 4360 2020-01-07 11:25:50Z suehring
-! Corrected "Former revisions" section
-!
-! 4069 2019-07-01 14:05:51Z Giersch
-! Masked output running index mid has been introduced as a local variable to
-! avoid runtime error (Loop variable has been modified) in time_integration
-!
-! 3655 2019-01-07 16:51:22Z knoop
-! unused variables and format statements removed
+! $Log: close_file.f90,v $
+! Revision 1.10  2006/08/22 13:50:01  raasch
+! xz and yz cross sections now up to nzt+1
+!
+! Revision 1.9  2006/08/04 14:22:53  raasch
+! Closing unit 109 (particle timeseries)
+!
+! Revision 1.8  2006/02/23 10:08:23  raasch
+! Extensions for 2d/3d data averaging
+! pl2d_.._anz, pl3d_anz renamed do2d_.._n, do3d_avs_n, respectively
+! error number argument for handle_netcdf_error
+!
+! Revision 1.7  2005/05/18 15:13:12  raasch
+! Extensions for NetCDF output
+!
+! Revision 1.6  2005/03/26 19:54:23  raasch
+! +datform in global-namelist (iso2d)
+!
+! Revision 1.5  2003/03/04 11:24:46  raasch
+! AVS data format changed from float to xdr_float (needed on linux machines
+! due to the little/big endian problem)
+!
+! Revision 1.4  2002/12/19 14:06:34  raasch
+! ISO2D parameter dp set to true for ibm hosts
+!
+! Revision 1.3  2001/03/30 06:58:36  raasch
+! Translation of remaining German identifiers (variables, subroutines, etc.)
+!
+! Revision 1.2  2001/01/22 05:39:06  raasch
+! Module test_variables removed
+!
+! Revision 1.1  2001/01/02 17:23:41  raasch
+! Initial revision
+!
+! Last revision before renaming subroutine  2001/01/01  raasch
+! Subroutine name changed from close_files to close_file. Closing of a single
+! file is allowed by passing its file-id as an argument. Variable openfile now
+! is of type file_status and contains a flag which indicates if a file has
+! been opened before. Old revision remarks deleted.
+!
+! Revision 1.13 (close_files) 2000/12/20 09:10:24  letzel
+! All comments translated into English.
+!
+! Revision 1.12 (close_files) 1999/03/02 09:22:46  raasch
+! FLD-Header fuer komprimierte 3D-Daten
 !
 ! Revision 1.1 (close_files) 1997/08/11 06:11:18  raasch
@@ -42,47 +60,47 @@
 ! Description:
 ! ------------
-!> Close specified file or all open files, if "0" has been given as the calling argument. In that
-!> case, execute last actions for certain unit numbers, if required.
-!--------------------------------------------------------------------------------------------------!
- SUBROUTINE close_file( file_id )
-
-
-    USE control_parameters,                                                                        &
-        ONLY:  max_masks, openfile
-
-    USE kinds
-
-#if defined( __netcdf )
-    USE NETCDF
-#endif
-
-    USE netcdf_interface,                                                      &
-        ONLY:  id_set_mask, id_set_pr, id_set_pts, id_set_sp,  id_set_ts, id_set_xy, id_set_xz,    &
-               id_set_yz, id_set_3d, id_set_fl, nc_stat, netcdf_data_format, netcdf_handle_error
-
+! Close specified file or all open files, if "0" has been given as the
+! calling argument. In that case, execute last actions for certain unit 
+! numbers, if required.
+!------------------------------------------------------------------------------!
+
+    USE control_parameters
+    USE grid_variables
+    USE indices
+    USE netcdf_control
     USE pegrid
+    USE profil_parameter
+    USE statistics
 
     IMPLICIT NONE
 
-    CHARACTER (LEN=10)  ::  datform = 'lit_endian' !<
-    CHARACTER (LEN=80)  ::  title                  !<
-
-    INTEGER(iwp) ::  av           !<
-    INTEGER(iwp) ::  dimx         !<
-    INTEGER(iwp) ::  dimy         !<
-    INTEGER(iwp) ::  fid          !<
-    INTEGER(iwp) ::  file_id      !<
-    INTEGER(iwp) ::  mid          !< masked output running index
-    INTEGER(iwp) ::  planz        !<
-
-    LOGICAL ::  checkuf = .TRUE.  !<
-    LOGICAL ::  datleg = .TRUE.   !<
-    LOGICAL ::  dbp = .FALSE.     !<
-
-    NAMELIST /GLOBAL/  checkuf, datform, dbp, dimx, dimy, planz, title
-    NAMELIST /RAHMEN/  datleg
-
-!
-!-- Close specified unit number (if opened) and set a flag that it has been opened one time at least
+    CHARACTER (LEN=2)   ::  suffix
+    CHARACTER (LEN=10)  ::  datform = 'lit_endian'
+    CHARACTER (LEN=80)  ::  rtext, title, utext = '', xtext = '', ytext = ''
+
+    INTEGER ::  anzzeile, cranz, cross_count, cross_numbers, dimx, dimy, &
+                fid, file_id, j, k, legpos = 1, planz, timodex = 1
+    INTEGER, DIMENSION(100) ::  klist, lstyle, cucol
+
+    LOGICAL ::  checkuf = .TRUE., datleg = .TRUE., dp = .FALSE., &
+                grid = .TRUE., rand = .TRUE., swap, twoxa = .TRUE., &
+                twoya = .TRUE.
+
+    REAL    ::  ansx = -999.999, ansy = -999.999, gwid = 0.1, rlegfak, &
+                sizex, sizey, texfac, utmove = 50.0, uxmax, uxmin, uymax, &
+                uymin, yright
+    REAL, DIMENSION(100) ::  lwid, normx, normy
+
+    NAMELIST /CROSS/   ansx, ansy, cucol, grid, gwid, klist, legpos, lstyle, &
+                       lwid, normx, normy, rand, rlegfak, sizex, sizey, &
+                       texfac, timodex, twoxa, twoya, utext, utmove, uxmax, &
+                       uxmin, uymax, uymin, xtext, ytext
+    NAMELIST /GLOBAL/  checkuf, datform, dimx, dimy, dp, planz, sizex, sizey, &
+                       title, yright
+    NAMELIST /RAHMEN/  anzzeile, cranz, datleg, rtext, swap
+
+!
+!-- Close specified unit number (if opened) and set a flag that it has
+!-- been opened one time at least
     IF ( file_id /= 0 )  THEN
        IF ( openfile(file_id)%opened )  THEN
@@ -96,5 +114,5 @@
 !
 !-- Close all open unit numbers
-    DO  fid = 1, 200+2*max_masks
+    DO  fid = 1, 116
 
        IF ( openfile(fid)%opened .OR. openfile(fid)%opened_before )  THEN
@@ -103,116 +121,396 @@
           SELECT CASE ( fid )
 
+             CASE ( 21 )
+!
+!--             Write ISO2D global parameters
+                IF ( myid == 0  .AND.  iso2d_output )  THEN
+                   planz  = do2d_xy_n
+                   dimx   = nx + 2
+                   dimy   = ny + 2
+                   sizex  = 100.0
+                   sizey  = 100.0
+                   title  = run_description_header
+                   yright = ( ny + 1.0 ) * dy
+                   IF ( host(1:3) == 'ibm'  .OR.  host(1:3) == 't3e' )  THEN
+                      checkuf = .FALSE.; dp = .TRUE.
+                   ENDIF
+                   IF ( host(1:3) == 'ibm'  .OR.  host(1:3) == 'nec' )  THEN
+                      datform = 'big_endian'
+                   ENDIF
+                   OPEN ( 90, FILE='PLOT2D_XY_GLOBAL', FORM='FORMATTED', &
+                              DELIM='APOSTROPHE' )
+                   WRITE ( 90, GLOBAL )
+                   CLOSE ( 90 )
+                ENDIF
+
+             CASE ( 22 )
+!
+!--             Write ISO2D global parameters
+                IF ( myid == 0 )  THEN
+                   planz  = do2d_xz_n
+                   dimx   = nx + 2
+                   dimy   = nz + 2
+                   sizex  = 100.0
+                   sizey  =  65.0
+                   title  = run_description_header
+                   yright = z_max_do2d
+                   IF ( host(1:3) == 'ibm'  .OR.  host(1:3) == 't3e' )  THEN
+                      checkuf = .FALSE.; dp = .TRUE.
+                   ENDIF
+                   IF ( host(1:3) == 'ibm'  .OR.  host(1:3) == 'nec' )  THEN
+                      datform = 'big_endian'
+                   ENDIF
+                   OPEN ( 90, FILE='PLOT2D_XZ_GLOBAL', FORM='FORMATTED', &
+                              DELIM='APOSTROPHE' )
+                   WRITE ( 90, GLOBAL )
+                   CLOSE ( 90 )
+                ENDIF
+
+             CASE ( 23 )
+!
+!--             Write ISO2D global parameters
+                IF ( myid == 0 )  THEN
+                   planz  = do2d_yz_n
+                   dimx   = ny + 2
+                   dimy   = nz + 2
+                   sizex  = 100.0
+                   sizey  =  65.0
+                   title  = run_description_header
+                   yright = z_max_do2d
+                   IF ( host(1:3) == 'ibm'  .OR.  host(1:3) == 't3e' )  THEN
+                      checkuf = .FALSE.; dp = .TRUE.
+                   ENDIF
+                   IF ( host(1:3) == 'ibm'  .OR.  host(1:3) == 'nec' )  THEN
+                      datform = 'big_endian'
+                   ENDIF
+                   OPEN ( 90, FILE='PLOT2D_YZ_GLOBAL', FORM='FORMATTED', &
+                              DELIM='APOSTROPHE' )
+                   WRITE ( 90, GLOBAL )
+                   CLOSE ( 90 )
+                ENDIF
+
+             CASE ( 32 )
+!
+!--             Write header for FLD-file
+                IF ( do3d_compress )  THEN
+                   WRITE ( 32, 3200)  ' compressed ',                       &
+                                      TRIM( run_description_header ), nx+2, &
+                                      ny+2, nz_do3d+1, do3d_avs_n
+                ELSE
+                   WRITE ( 32, 3200)  ' ', TRIM( run_description_header ), &
+                                      nx+2, ny+2, nz_do3d+1, do3d_avs_n
+                ENDIF
+
+             CASE ( 40:49 )
+!
+!--             Write PROFIL namelist parameters for 1D profiles.
+!--             First determine, how many crosses are to be drawn.
+                IF ( myid == 0 )  THEN
+                   cross_numbers = 0
+                   DO  j = 1, crmax
+                      IF ( cross_profile_number_count(j) /= 0 )  THEN
+                         cross_numbers = cross_numbers + 1
+                      ENDIF
+                   ENDDO
+
+                   IF ( cross_numbers /= 0 )  THEN
+!
+!--                   Determine remaining RAHMEN parameters
+                      swap = .FALSE.
+                      rtext = '\0.5 ' // TRIM( run_description_header ) // &
+                              '    ' // TRIM( region( fid - 40 ) )
+!
+!--                   Write RAHMEN parameters
+                      IF ( statistic_regions == 0  .AND.  fid == 40 )  THEN
+                         suffix = ''
+                      ELSE
+                         WRITE ( suffix, '(''_'',I1)' )  fid - 40
+                      ENDIF
+                      OPEN ( 90, FILE='PLOT1D_PAR' // TRIM( suffix ), &
+                                 FORM='FORMATTED', DELIM='APOSTROPHE' )
+!
+!--                   Subtitle for crosses with time averaging
+                      IF ( averaging_interval_pr /= 0.0 )  THEN
+                         WRITE ( utext, 4000 )  averaging_interval_pr
+                      ENDIF
+!
+!--                   Determine and write CROSS parameters for each individual 
+!--                   cross
+                      cross_count = 0
+                      DO  j = 1, crmax
+                         k = cross_profile_number_count(j)
+                         IF ( k /= 0 )  THEN
+                            cross_count = cross_count + 1
+!
+!--                         Write RAHMEN parameters
+                            IF ( MOD( cross_count-1, &
+                                      profile_rows*profile_columns ) == 0 ) &
+                            THEN
+!
+!--                            Determine number of crosses still to be drawn
+                               cranz = MIN( cross_numbers - cross_count + 1, &
+                                            profile_rows * profile_columns )
+!
+!--                            If the first line cannot be filled with crosses
+!--                            completely, the default number of crosses per 
+!--                            line has to be reduced.
+                               IF ( cranz < profile_columns )  THEN
+                                  anzzeile = cranz
+                               ELSE
+                                  anzzeile = profile_columns
+                               ENDIF
+
+                               WRITE ( 90, RAHMEN )
+
+                            ENDIF
+!
+!--                         Store graph numbers
+                            klist(1:k) = cross_profile_numbers(1:k,j)
+                            klist(k+1:100) = 999999
+!
+!--                         Store graph attributes
+                            cucol  = cross_linecolors(:,j)
+                            lstyle = cross_linestyles(:,j)
+                            lwid = 0.6
+!
+!--                         Sizes, text etc.
+                            sizex = 100.0; sizey = 120.0
+                            rlegfak = 0.7; texfac = 1.0
+!
+!--                         Determine range of x-axis values
+                            IF ( cross_normalized_x(j) == ' ' )  THEN
+!
+!--                            Non-normalized profiles
+                               IF ( cross_uxmin(j) == 0.0  .AND. &
+                                    cross_uxmax(j) == 0.0 )  THEN
+                                  uxmin = cross_uxmin_computed(j)
+                                  uxmax = cross_uxmax_computed(j)
+                                  IF ( uxmin == uxmax )  uxmax = uxmin + 1.0
+                               ELSE
+!
+!--                               Values set in check_parameters are used here
+                                  uxmin = cross_uxmin(j); uxmax = cross_uxmax(j)
+                               ENDIF
+                            ELSE
+!
+!--                            Normalized profiles
+                               IF ( cross_uxmin_normalized(j) == 0.0  .AND. &
+                                    cross_uxmax_normalized(j) == 0.0 )  THEN
+                                  uxmin = cross_uxmin_normalized_computed(j)
+                                  uxmax = cross_uxmax_normalized_computed(j)
+                                  IF ( uxmin == uxmax )  uxmax = uxmin + 1.0
+                               ELSE
+!
+!--                               Values set in check_parameters are used here
+                                  uxmin = cross_uxmin_normalized(j)
+                                  uxmax = cross_uxmax_normalized(j)
+                               ENDIF
+                            ENDIF
+!
+!--                         Range of y-axis values
+!--                         may be re-adjusted during normalization if required
+                            uymin = cross_uymin(j); uymax = cross_uymax(j)
+                            ytext = 'height in m'
+!
+!--                         Normalization of the axes
+                            normx = cross_normx_factor(:,j)
+                            normy = cross_normy_factor(:,j)
+!
+!--                         Labelling of the axes
+                            IF ( cross_normalized_x(j) == ' ' )  THEN
+                               xtext = cross_xtext(j)
+                            ELSE
+                               xtext = TRIM( cross_xtext(j) ) // ' / ' // &
+                                       cross_normalized_x(j)
+                            ENDIF
+                            IF ( cross_normalized_y(j) == ' ' )  THEN
+                               ytext = 'height in m'
+                            ELSE
+                               ytext = 'height in m' // ' / ' // &
+                                       cross_normalized_y(j)
+!
+!--                            Determine upper limit of value range
+                               IF ( z_max_do1d_normalized /= -1.0 )  THEN
+                                  uymax = z_max_do1d_normalized
+                               ENDIF
+                            ENDIF
+
+                            WRITE ( 90, CROSS )
+
+                         ENDIF
+                      ENDDO
+
+                      CLOSE ( 90 )
+                   ENDIF
+                ENDIF
+
+             CASE ( 50:59 )
+!
+!--             Write PROFIL namelist parameters for time series
+!--             first determine number of crosses to be drawn
+                IF ( myid == 0 )  THEN
+                   cranz = 0
+                   DO  j = 1, 12
+                      IF ( cross_ts_number_count(j) /= 0 )  cranz = cranz+1
+                   ENDDO
+
+                   IF ( cranz /= 0 )  THEN
+!
+!--                   Determine RAHMEN parameters
+                      anzzeile = 1
+                      swap = .TRUE.
+                      rtext = '\1.0 ' // TRIM( run_description_header ) // &
+                              '    ' // TRIM( region( fid - 50 ) )
+!
+!--                   Write RAHMEN parameters
+                      IF ( statistic_regions == 0  .AND.  fid == 50 )  THEN
+                         suffix = ''
+                      ELSE
+                         WRITE ( suffix, '(''_'',I1)' )  fid - 50
+                      ENDIF
+                      OPEN ( 90, FILE='PLOTTS_PAR' // TRIM( suffix ), &
+                                 FORM='FORMATTED', DELIM='APOSTROPHE' )
+                      WRITE ( 90, RAHMEN )
+!
+!--                   Determine and write CROSS parameters for each individual 
+!--                   cross
+                      DO  j = 1, 12
+                         k = cross_ts_number_count(j)
+                         IF ( k /= 0 )  THEN
+!
+!--                         Store graph numbers
+                            klist(1:k) = cross_ts_numbers(1:k,j)
+                            klist(k+1:100) = 999999
+!
+!--                         Store graph attributes
+                            cucol(1:k)  = linecolors(1:k)
+                            lstyle(1:k) = linestyles(1:k)
+                            lwid = 0.4
+!
+!--                         Sizes, text etc.
+                            sizex = 250.0; sizey = 40.0
+                            rlegfak = 1.5; texfac = 1.5
+                            xtext = 'time in s'
+                            ytext = ''
+                            utext = ''
+!
+!--                         Determine range of y-axis values
+                            IF ( cross_ts_uymin(j) == 999.999 )  THEN
+                               uymin = cross_ts_uymin_computed(j)
+                            ELSE
+                               uymin = cross_ts_uymin(j)
+                            ENDIF
+                            IF ( cross_ts_uymax(j) == 999.999 )  THEN
+                               uymax = cross_ts_uymax_computed(j)
+                            ELSE
+                               uymax = cross_ts_uymax(j)
+                            ENDIF
+                            IF ( uymin == uymax )  uymax = uymin + 1.0
+!
+!--                         Range of x-axis values
+                            uxmin = 0.0; uxmax = simulated_time
+!
+!--                         Normalizations
+                            normx = 1.0; normy = 1.0
+
+                            WRITE ( 90, CROSS )
+
+                         ENDIF
+                      ENDDO
+
+                      CLOSE ( 90 )
+                   ENDIF
+                ENDIF
+
 #if defined( __netcdf )
              CASE ( 101 )
 
-                IF ( myid == 0  .OR.  netcdf_data_format > 4 )  THEN
+                IF ( myid == 0  .AND.  netcdf_output )  THEN
                    nc_stat = NF90_CLOSE( id_set_xy(0) )
-                   CALL netcdf_handle_error( 'close_file', 44 )
+                   IF ( nc_stat /= NF90_NOERR )  CALL handle_netcdf_error( 44 )
                 ENDIF
 
              CASE ( 102 )
 
-                IF ( myid == 0  .OR.  netcdf_data_format > 4 )  THEN
+                IF ( myid == 0  .AND.  netcdf_output )  THEN
                    nc_stat = NF90_CLOSE( id_set_xz(0) )
-                   CALL netcdf_handle_error( 'close_file', 45 )
+                   IF ( nc_stat /= NF90_NOERR )  CALL handle_netcdf_error( 45 )
                 ENDIF
 
              CASE ( 103 )
 
-                IF ( myid == 0  .OR.  netcdf_data_format > 4 )  THEN
+                IF ( myid == 0  .AND.  netcdf_output )  THEN
                    nc_stat = NF90_CLOSE( id_set_yz(0) )
-                   CALL netcdf_handle_error( 'close_file', 46 )
+                   IF ( nc_stat /= NF90_NOERR )  CALL handle_netcdf_error( 46 )
                 ENDIF
 
              CASE ( 104 )
 
-                IF ( myid == 0 )  THEN
+                IF ( myid == 0  .AND.  netcdf_output )  THEN
                    nc_stat = NF90_CLOSE( id_set_pr )
-                   CALL netcdf_handle_error( 'close_file', 47 )
+                   IF ( nc_stat /= NF90_NOERR )  CALL handle_netcdf_error( 47 )
                 ENDIF
 
              CASE ( 105 )
 
-                IF ( myid == 0 )  THEN
+                IF ( myid == 0  .AND.  netcdf_output )  THEN
                    nc_stat = NF90_CLOSE( id_set_ts )
-                   CALL netcdf_handle_error( 'close_file', 48 )
+                   IF ( nc_stat /= NF90_NOERR )  CALL handle_netcdf_error( 48 )
                 ENDIF
 
              CASE ( 106 )
 
-                IF ( myid == 0  .OR.  netcdf_data_format > 4 )  THEN
+                IF ( myid == 0  .AND.  netcdf_output )  THEN
                    nc_stat = NF90_CLOSE( id_set_3d(0) )
-                   CALL netcdf_handle_error( 'close_file', 49 )
+                   IF ( nc_stat /= NF90_NOERR )  CALL handle_netcdf_error( 49 )
                 ENDIF
 
              CASE ( 107 )
 
-                IF ( myid == 0 )  THEN
+                IF ( myid == 0  .AND.  netcdf_output )  THEN
                    nc_stat = NF90_CLOSE( id_set_sp )
-                   CALL netcdf_handle_error( 'close_file', 50 )
-                ENDIF
-
-!
-!--           Currently disabled
-!             CASE ( 108 )
-
-!                nc_stat = NF90_CLOSE( id_set_prt )
-!                CALL netcdf_handle_error( 'close_file', 51 )
-
-             CASE ( 109 )
-
-                nc_stat = NF90_CLOSE( id_set_pts )
-                CALL netcdf_handle_error( 'close_file', 412 )
+                   IF ( nc_stat /= NF90_NOERR )  CALL handle_netcdf_error( 50 )
+                ENDIF
+
+             CASE ( 108 )
+
+                IF (  netcdf_output )  THEN
+                   nc_stat = NF90_CLOSE( id_set_prt )
+                   IF ( nc_stat /= NF90_NOERR )  CALL handle_netcdf_error( 51 )
+                ENDIF
+
+             CASE ( 109 ) 
+
+                IF (  netcdf_output )  THEN
+                   nc_stat = NF90_CLOSE( id_set_pts )
+                   IF ( nc_stat /= NF90_NOERR )  CALL handle_netcdf_error( 412 )
+                ENDIF
 
              CASE ( 111 )
 
-                IF ( myid == 0  .OR.  netcdf_data_format > 4 )  THEN
+                IF ( myid == 0  .AND.  netcdf_output )  THEN
                    nc_stat = NF90_CLOSE( id_set_xy(1) )
-                   CALL netcdf_handle_error( 'close_file', 52 )
+                   IF ( nc_stat /= NF90_NOERR )  CALL handle_netcdf_error( 52 )
                 ENDIF
 
              CASE ( 112 )
 
-                IF ( myid == 0  .OR.  netcdf_data_format > 4 )  THEN
+                IF ( myid == 0  .AND.  netcdf_output )  THEN
                    nc_stat = NF90_CLOSE( id_set_xz(1) )
-                   CALL netcdf_handle_error( 'close_file', 352 )
+                   IF ( nc_stat /= NF90_NOERR )  CALL handle_netcdf_error( 352 )
                 ENDIF
 
              CASE ( 113 )
 
-                IF ( myid == 0  .OR.  netcdf_data_format > 4 )  THEN
+                IF ( myid == 0  .AND.  netcdf_output )  THEN
                    nc_stat = NF90_CLOSE( id_set_yz(1) )
-                   CALL netcdf_handle_error( 'close_file', 353 )
+                   IF ( nc_stat /= NF90_NOERR )  CALL handle_netcdf_error( 353 )
                 ENDIF
 
              CASE ( 116 )
 
-                IF ( myid == 0  .OR.  netcdf_data_format > 4 )  THEN
+                IF ( myid == 0  .AND.  netcdf_output )  THEN
                    nc_stat = NF90_CLOSE( id_set_3d(1) )
-                   CALL netcdf_handle_error( 'close_file', 353 )
-                ENDIF
-
-             CASE ( 199 )
-
-                IF ( myid == 0  .OR.  netcdf_data_format > 4 )  THEN
-                   nc_stat = NF90_CLOSE( id_set_fl )
-                   CALL netcdf_handle_error( 'close_file', 353 )
-                ENDIF
-
-             CASE ( 201:200+2*max_masks )
-
-                IF ( myid == 0  .OR.  netcdf_data_format > 4 )  THEN
-!
-!--                Decompose fid into mid and av
-                   IF ( fid <= 200+max_masks )  THEN
-                      mid = fid - 200
-                      av = 0
-                   ELSE
-                      mid = fid - (200+max_masks)
-                      av = 1
-                   ENDIF
-                   nc_stat = NF90_CLOSE( id_set_mask(mid,av) )
-                   CALL netcdf_handle_error( 'close_file', 459 )
-
+                   IF ( nc_stat /= NF90_NOERR )  CALL handle_netcdf_error( 353 )
                 ENDIF
 
@@ -228,3 +526,19 @@
     ENDDO
 
+!
+!-- Formats
+3200 FORMAT ('# AVS',A,'field file'/ &
+             '#'/                &
+             '# ',A/             &
+             'ndim=3'/           &
+             'dim1=',I5/         &
+             'dim2=',I5/         &
+             'dim3=',I5/         &
+             'nspace=3'/         &
+             'veclen=',I5/       &
+             'data=xdr_float'/   &
+             'field=rectilinear')
+4000 FORMAT ('time averaged over',F7.1,' s')
+
+
  END SUBROUTINE close_file
