SUBROUTINE close_file( file_id ) !--------------------------------------------------------------------------------! ! 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 . ! ! Copyright 1997-2014 Leibniz Universitaet Hannover !--------------------------------------------------------------------------------! ! ! Current revisions: ! ----------------- ! ! ! Former revisions: ! ----------------- ! $Id: close_file.f90 1321 2014-03-20 09:40:40Z raasch $ ! ! 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 ! ! 1092 2013-02-02 11:24:22Z raasch ! unused variables removed ! ! 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 ! ! 964 2012-07-26 09:14:24Z raasch ! old profil-units (40:49) and respective code removed ! ! Revision 1.1 (close_files) 1997/08/11 06:11:18 raasch ! Initial revision ! ! ! 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. !------------------------------------------------------------------------------! USE control_parameters, & ONLY: do2d_xz_n, do2d_xy_n, do2d_yz_n, do3d_avs_n, do3d_compress, & host, iso2d_output, max_masks, mid, netcdf_data_format, & netcdf_output, nz_do3d, openfile, run_description_header, & z_max_do2d USE grid_variables, & ONLY: dy USE indices, & ONLY: nx, ny, nz USE kinds USE netcdf_control USE pegrid 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) :: planz !: LOGICAL :: checkuf = .TRUE. !: LOGICAL :: datleg = .TRUE. !: LOGICAL :: dbp = .FALSE. !: REAL(wp) :: sizex !: REAL(wp) :: sizey !: REAL(wp) :: yright !: NAMELIST /GLOBAL/ checkuf, datform, dimx, dimy, dbp, planz, & title NAMELIST /RAHMEN/ datleg ! !-- 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 CLOSE ( file_id ) openfile(file_id)%opened = .FALSE. openfile(file_id)%opened_before = .TRUE. ENDIF RETURN ENDIF ! !-- Close all open unit numbers DO fid = 1, 200+2*max_masks IF ( openfile(fid)%opened .OR. openfile(fid)%opened_before ) THEN ! !-- Last actions for certain unit numbers 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.; dbp = .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.; dbp = .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.; dbp = .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 #if defined( __netcdf ) CASE ( 101 ) IF ( netcdf_output .AND. & ( myid == 0 .OR. netcdf_data_format > 4 ) ) THEN nc_stat = NF90_CLOSE( id_set_xy(0) ) CALL handle_netcdf_error( 'close_file', 44 ) ENDIF CASE ( 102 ) IF ( netcdf_output .AND. & ( myid == 0 .OR. netcdf_data_format > 4 ) ) THEN nc_stat = NF90_CLOSE( id_set_xz(0) ) CALL handle_netcdf_error( 'close_file', 45 ) ENDIF CASE ( 103 ) IF ( netcdf_output .AND. & ( myid == 0 .OR. netcdf_data_format > 4 ) ) THEN nc_stat = NF90_CLOSE( id_set_yz(0) ) CALL handle_netcdf_error( 'close_file', 46 ) ENDIF CASE ( 104 ) IF ( myid == 0 .AND. netcdf_output ) THEN nc_stat = NF90_CLOSE( id_set_pr ) CALL handle_netcdf_error( 'close_file', 47 ) ENDIF CASE ( 105 ) IF ( myid == 0 .AND. netcdf_output ) THEN nc_stat = NF90_CLOSE( id_set_ts ) CALL handle_netcdf_error( 'close_file', 48 ) ENDIF CASE ( 106 ) IF ( netcdf_output .AND. & ( myid == 0 .OR. netcdf_data_format > 4 ) ) THEN nc_stat = NF90_CLOSE( id_set_3d(0) ) CALL handle_netcdf_error( 'close_file', 49 ) ENDIF CASE ( 107 ) IF ( myid == 0 .AND. netcdf_output ) THEN nc_stat = NF90_CLOSE( id_set_sp ) CALL handle_netcdf_error( 'close_file', 50 ) ENDIF CASE ( 108 ) IF ( netcdf_output ) THEN nc_stat = NF90_CLOSE( id_set_prt ) CALL handle_netcdf_error( 'close_file', 51 ) ENDIF CASE ( 109 ) IF ( netcdf_output ) THEN nc_stat = NF90_CLOSE( id_set_pts ) CALL handle_netcdf_error( 'close_file', 412 ) ENDIF CASE ( 111 ) IF ( netcdf_output .AND. & ( myid == 0 .OR. netcdf_data_format > 4 ) ) THEN nc_stat = NF90_CLOSE( id_set_xy(1) ) CALL handle_netcdf_error( 'close_file', 52 ) ENDIF CASE ( 112 ) IF ( netcdf_output .AND. & ( myid == 0 .OR. netcdf_data_format > 4 ) ) THEN nc_stat = NF90_CLOSE( id_set_xz(1) ) CALL handle_netcdf_error( 'close_file', 352 ) ENDIF CASE ( 113 ) IF ( netcdf_output .AND. & ( myid == 0 .OR. netcdf_data_format > 4 ) ) THEN nc_stat = NF90_CLOSE( id_set_yz(1) ) CALL handle_netcdf_error( 'close_file', 353 ) ENDIF CASE ( 116 ) IF ( netcdf_output .AND. & ( myid == 0 .OR. netcdf_data_format > 4 ) ) THEN nc_stat = NF90_CLOSE( id_set_3d(1) ) CALL handle_netcdf_error( 'close_file', 353 ) ENDIF CASE ( 201:200+2*max_masks ) IF ( netcdf_output .AND. & ( 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 handle_netcdf_error( 'close_file', 459 ) ENDIF #endif END SELECT ! !-- Close file IF ( openfile(fid)%opened ) CLOSE ( fid ) ENDIF 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