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