1 | !> @file close_file.f90 |
---|
2 | !--------------------------------------------------------------------------------------------------! |
---|
3 | ! This file is part of the PALM model system. |
---|
4 | ! |
---|
5 | ! PALM is free software: you can redistribute it and/or modify it under the terms of the GNU General |
---|
6 | ! Public License as published by the Free Software Foundation, either version 3 of the License, or |
---|
7 | ! (at your option) any later version. |
---|
8 | ! |
---|
9 | ! PALM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the |
---|
10 | ! implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
---|
11 | ! Public License for more details. |
---|
12 | ! |
---|
13 | ! You should have received a copy of the GNU General Public License along with PALM. If not, see |
---|
14 | ! <http://www.gnu.org/licenses/>. |
---|
15 | ! |
---|
16 | ! Copyright 1997-2021 Leibniz Universitaet Hannover |
---|
17 | !--------------------------------------------------------------------------------------------------! |
---|
18 | ! |
---|
19 | ! Current revisions: |
---|
20 | ! ----------------- |
---|
21 | ! |
---|
22 | ! |
---|
23 | ! Former revisions: |
---|
24 | ! ----------------- |
---|
25 | ! $Id: close_file.f90 4828 2021-01-05 11:21:41Z monakurppa $ |
---|
26 | ! file re-formatted to follow the PALM coding standard |
---|
27 | ! |
---|
28 | ! 4360 2020-01-07 11:25:50Z suehring |
---|
29 | ! Corrected "Former revisions" section |
---|
30 | ! |
---|
31 | ! 4069 2019-07-01 14:05:51Z Giersch |
---|
32 | ! Masked output running index mid has been introduced as a local variable to |
---|
33 | ! avoid runtime error (Loop variable has been modified) in time_integration |
---|
34 | ! |
---|
35 | ! 3655 2019-01-07 16:51:22Z knoop |
---|
36 | ! unused variables and format statements removed |
---|
37 | ! |
---|
38 | ! Revision 1.1 (close_files) 1997/08/11 06:11:18 raasch |
---|
39 | ! Initial revision |
---|
40 | ! |
---|
41 | ! |
---|
42 | ! Description: |
---|
43 | ! ------------ |
---|
44 | !> Close specified file or all open files, if "0" has been given as the calling argument. In that |
---|
45 | !> case, execute last actions for certain unit numbers, if required. |
---|
46 | !--------------------------------------------------------------------------------------------------! |
---|
47 | SUBROUTINE close_file( file_id ) |
---|
48 | |
---|
49 | |
---|
50 | USE control_parameters, & |
---|
51 | ONLY: max_masks, openfile |
---|
52 | |
---|
53 | USE kinds |
---|
54 | |
---|
55 | #if defined( __netcdf ) |
---|
56 | USE NETCDF |
---|
57 | #endif |
---|
58 | |
---|
59 | USE netcdf_interface, & |
---|
60 | ONLY: id_set_mask, id_set_pr, id_set_pts, id_set_sp, id_set_ts, id_set_xy, id_set_xz, & |
---|
61 | id_set_yz, id_set_3d, id_set_fl, nc_stat, netcdf_data_format, netcdf_handle_error |
---|
62 | |
---|
63 | USE pegrid |
---|
64 | |
---|
65 | IMPLICIT NONE |
---|
66 | |
---|
67 | CHARACTER (LEN=10) :: datform = 'lit_endian' !< |
---|
68 | CHARACTER (LEN=80) :: title !< |
---|
69 | |
---|
70 | INTEGER(iwp) :: av !< |
---|
71 | INTEGER(iwp) :: dimx !< |
---|
72 | INTEGER(iwp) :: dimy !< |
---|
73 | INTEGER(iwp) :: fid !< |
---|
74 | INTEGER(iwp) :: file_id !< |
---|
75 | INTEGER(iwp) :: mid !< masked output running index |
---|
76 | INTEGER(iwp) :: planz !< |
---|
77 | |
---|
78 | LOGICAL :: checkuf = .TRUE. !< |
---|
79 | LOGICAL :: datleg = .TRUE. !< |
---|
80 | LOGICAL :: dbp = .FALSE. !< |
---|
81 | |
---|
82 | NAMELIST /GLOBAL/ checkuf, datform, dbp, dimx, dimy, planz, title |
---|
83 | NAMELIST /RAHMEN/ datleg |
---|
84 | |
---|
85 | ! |
---|
86 | !-- Close specified unit number (if opened) and set a flag that it has been opened one time at least |
---|
87 | IF ( file_id /= 0 ) THEN |
---|
88 | IF ( openfile(file_id)%opened ) THEN |
---|
89 | CLOSE ( file_id ) |
---|
90 | openfile(file_id)%opened = .FALSE. |
---|
91 | openfile(file_id)%opened_before = .TRUE. |
---|
92 | ENDIF |
---|
93 | RETURN |
---|
94 | ENDIF |
---|
95 | |
---|
96 | ! |
---|
97 | !-- Close all open unit numbers |
---|
98 | DO fid = 1, 200+2*max_masks |
---|
99 | |
---|
100 | IF ( openfile(fid)%opened .OR. openfile(fid)%opened_before ) THEN |
---|
101 | ! |
---|
102 | !-- Last actions for certain unit numbers |
---|
103 | SELECT CASE ( fid ) |
---|
104 | |
---|
105 | #if defined( __netcdf ) |
---|
106 | CASE ( 101 ) |
---|
107 | |
---|
108 | IF ( myid == 0 .OR. netcdf_data_format > 4 ) THEN |
---|
109 | nc_stat = NF90_CLOSE( id_set_xy(0) ) |
---|
110 | CALL netcdf_handle_error( 'close_file', 44 ) |
---|
111 | ENDIF |
---|
112 | |
---|
113 | CASE ( 102 ) |
---|
114 | |
---|
115 | IF ( myid == 0 .OR. netcdf_data_format > 4 ) THEN |
---|
116 | nc_stat = NF90_CLOSE( id_set_xz(0) ) |
---|
117 | CALL netcdf_handle_error( 'close_file', 45 ) |
---|
118 | ENDIF |
---|
119 | |
---|
120 | CASE ( 103 ) |
---|
121 | |
---|
122 | IF ( myid == 0 .OR. netcdf_data_format > 4 ) THEN |
---|
123 | nc_stat = NF90_CLOSE( id_set_yz(0) ) |
---|
124 | CALL netcdf_handle_error( 'close_file', 46 ) |
---|
125 | ENDIF |
---|
126 | |
---|
127 | CASE ( 104 ) |
---|
128 | |
---|
129 | IF ( myid == 0 ) THEN |
---|
130 | nc_stat = NF90_CLOSE( id_set_pr ) |
---|
131 | CALL netcdf_handle_error( 'close_file', 47 ) |
---|
132 | ENDIF |
---|
133 | |
---|
134 | CASE ( 105 ) |
---|
135 | |
---|
136 | IF ( myid == 0 ) THEN |
---|
137 | nc_stat = NF90_CLOSE( id_set_ts ) |
---|
138 | CALL netcdf_handle_error( 'close_file', 48 ) |
---|
139 | ENDIF |
---|
140 | |
---|
141 | CASE ( 106 ) |
---|
142 | |
---|
143 | IF ( myid == 0 .OR. netcdf_data_format > 4 ) THEN |
---|
144 | nc_stat = NF90_CLOSE( id_set_3d(0) ) |
---|
145 | CALL netcdf_handle_error( 'close_file', 49 ) |
---|
146 | ENDIF |
---|
147 | |
---|
148 | CASE ( 107 ) |
---|
149 | |
---|
150 | IF ( myid == 0 ) THEN |
---|
151 | nc_stat = NF90_CLOSE( id_set_sp ) |
---|
152 | CALL netcdf_handle_error( 'close_file', 50 ) |
---|
153 | ENDIF |
---|
154 | |
---|
155 | ! |
---|
156 | !-- Currently disabled |
---|
157 | ! CASE ( 108 ) |
---|
158 | |
---|
159 | ! nc_stat = NF90_CLOSE( id_set_prt ) |
---|
160 | ! CALL netcdf_handle_error( 'close_file', 51 ) |
---|
161 | |
---|
162 | CASE ( 109 ) |
---|
163 | |
---|
164 | nc_stat = NF90_CLOSE( id_set_pts ) |
---|
165 | CALL netcdf_handle_error( 'close_file', 412 ) |
---|
166 | |
---|
167 | CASE ( 111 ) |
---|
168 | |
---|
169 | IF ( myid == 0 .OR. netcdf_data_format > 4 ) THEN |
---|
170 | nc_stat = NF90_CLOSE( id_set_xy(1) ) |
---|
171 | CALL netcdf_handle_error( 'close_file', 52 ) |
---|
172 | ENDIF |
---|
173 | |
---|
174 | CASE ( 112 ) |
---|
175 | |
---|
176 | IF ( myid == 0 .OR. netcdf_data_format > 4 ) THEN |
---|
177 | nc_stat = NF90_CLOSE( id_set_xz(1) ) |
---|
178 | CALL netcdf_handle_error( 'close_file', 352 ) |
---|
179 | ENDIF |
---|
180 | |
---|
181 | CASE ( 113 ) |
---|
182 | |
---|
183 | IF ( myid == 0 .OR. netcdf_data_format > 4 ) THEN |
---|
184 | nc_stat = NF90_CLOSE( id_set_yz(1) ) |
---|
185 | CALL netcdf_handle_error( 'close_file', 353 ) |
---|
186 | ENDIF |
---|
187 | |
---|
188 | CASE ( 116 ) |
---|
189 | |
---|
190 | IF ( myid == 0 .OR. netcdf_data_format > 4 ) THEN |
---|
191 | nc_stat = NF90_CLOSE( id_set_3d(1) ) |
---|
192 | CALL netcdf_handle_error( 'close_file', 353 ) |
---|
193 | ENDIF |
---|
194 | |
---|
195 | CASE ( 199 ) |
---|
196 | |
---|
197 | IF ( myid == 0 .OR. netcdf_data_format > 4 ) THEN |
---|
198 | nc_stat = NF90_CLOSE( id_set_fl ) |
---|
199 | CALL netcdf_handle_error( 'close_file', 353 ) |
---|
200 | ENDIF |
---|
201 | |
---|
202 | CASE ( 201:200+2*max_masks ) |
---|
203 | |
---|
204 | IF ( myid == 0 .OR. netcdf_data_format > 4 ) THEN |
---|
205 | ! |
---|
206 | !-- Decompose fid into mid and av |
---|
207 | IF ( fid <= 200+max_masks ) THEN |
---|
208 | mid = fid - 200 |
---|
209 | av = 0 |
---|
210 | ELSE |
---|
211 | mid = fid - (200+max_masks) |
---|
212 | av = 1 |
---|
213 | ENDIF |
---|
214 | nc_stat = NF90_CLOSE( id_set_mask(mid,av) ) |
---|
215 | CALL netcdf_handle_error( 'close_file', 459 ) |
---|
216 | |
---|
217 | ENDIF |
---|
218 | |
---|
219 | #endif |
---|
220 | |
---|
221 | END SELECT |
---|
222 | ! |
---|
223 | !-- Close file |
---|
224 | IF ( openfile(fid)%opened ) CLOSE ( fid ) |
---|
225 | |
---|
226 | ENDIF |
---|
227 | |
---|
228 | ENDDO |
---|
229 | |
---|
230 | END SUBROUTINE close_file |
---|