V3FIT
diagnostic_cdf.f
1 ! SPH010908 REPLACE INTEGER(iprec) with INTEGER
2 !*******************************************************************************
3 ! File diagnostic_cdf.f
4 ! Contains the module diagnostic_cdf
5 ! Module for defining variables and writing netCDF files, and reading
6 ! netCDF files with the derived types diagnostic_desc
7 ! (from the diagnostic_T module).
8 !
9 ! Information about the EZcdf module is at:
10 ! http://w3.pppl.gov/NTCC/EZcdf/
11 !
12 !-------------------------------------------------------------------------------
13 ! DEPENDENCIES
14 !-------------------------------------------------------------------------------
15 !
16 ! This module uses the following modules:
17 ! stel_kinds
18 ! stel_constants
19 ! mddc_T
20 ! mddc_cdf
21 ! diagnostic_T
22 ! ezcdf
23 ! v3_utilities
24 !
25 !-------------------------------------------------------------------------------
26 ! CHANGE HISTORY
27 !-------------------------------------------------------------------------------
28 !
29 ! See Section X at the end of the module.
30 !
31 !-------------------------------------------------------------------------------
32 ! USAGE
33 !-------------------------------------------------------------------------------
34 !-------------------------------------------------------------------------------
35 ! COMMENTS
36 !
37 ! 1) All cdf_define calls must be completed before the first cdf_write call.
38 !-------------------------------------------------------------------------------
39 !
40 !*******************************************************************************
41 
42 !*******************************************************************************
43 ! MODULE diagnostic_cdf
44 !
45 ! SECTION I. VARIABLE DECLARATIONS
46 ! SECTION II. INTERFACE BLOCKS
47 ! SECTION III. DEFINE SUBROUTINES
48 ! SECTION IV. WRITE SUBROUTINES
49 ! SECTION V. READ SUBROUTINES
50 ! SECTION VI. AUXILIARY FUNCTIONS AND SUBROUTINES
51 
52 ! SECTION X. COMMENTS FOR DIFFERENT REVISIONS
53 !*******************************************************************************
54 
55  MODULE diagnostic_cdf
56 
57 !*******************************************************************************
58 ! SECTION I. VARIABLE DECLARATIONS
59 !*******************************************************************************
60 
61 !-------------------------------------------------------------------------------
62 ! Type declarations - lengths of reals, integers, and complexes.
63 ! Frequently used mathematical constants, lots of extra precision.
64 !-------------------------------------------------------------------------------
65  USE stel_kinds, only : rprec, iprec, cprec
66  USE stel_constants, only : pi, twopi, one, zero
67 
68 !-------------------------------------------------------------------------------
69 ! Modules to USE
70 !-------------------------------------------------------------------------------
71 
72  USE diagnostic_t
73  USE mddc_t
74  USE mddc_cdf
75  USE ezcdf
76  USE v3_utilities
77 
78 !-------------------------------------------------------------------------------
79 ! Implicit None comes after USE statements, before other declarations
80 !-------------------------------------------------------------------------------
81  IMPLICIT NONE
82 
83 !-------------------------------------------------------------------------------
84 ! Make type declarations and constants Private, so there are no conflicts.
85 !-------------------------------------------------------------------------------
86  PRIVATE rprec, iprec, cprec, pi, twopi, one, zero
87 
88 !-------------------------------------------------------------------------------
89 ! Variable Names for netCDF. Make them Private.
90 !-------------------------------------------------------------------------------
91 
92  CHARACTER (LEN=*), PRIVATE, PARAMETER :: &
93  & vn_d_type = 'diagnostic_desc_d_type', &
94  & vn_s_name = 'diagnostic_desc_s_name', &
95  & vn_l_name = 'diagnostic_desc_l_name', &
96  & vn_units = 'diagnostic_desc_units', &
97  & vn_sigma_default = 'diagnostic_desc_sigma_default'
98 
99  CHARACTER (LEN=64), PRIVATE :: &
100  & vn_d_type_use, &
101  & vn_s_name_use, &
102  & vn_l_name_use, &
103  & vn_units_use, &
104  & vn_sigma_default_use
105 
106  CHARACTER (LEN=*), PRIVATE, PARAMETER :: &
107  & vn_desc_s_name = 'diagnostic_data_desc_s_name'
108 
109  CHARACTER (LEN=64), PRIVATE :: &
110  & vn_desc_s_name_use
111 
112 !-------------------------------------------------------------------------------
113 ! Lengths of Character Variables
114 !-------------------------------------------------------------------------------
115  INTEGER, PARAMETER, PRIVATE :: type_len=10
116  INTEGER, PARAMETER, PRIVATE :: sn_len=30
117  INTEGER, PARAMETER, PRIVATE :: ln_len=80
118  INTEGER, PARAMETER, PRIVATE :: units_len=30
119 
120 !*******************************************************************************
121 ! SECTION II. INTERFACE BLOCKS
122 !*******************************************************************************
123 !-------------------------------------------------------------------------------
124 !
125 !-------------------------------------------------------------------------------
126 
127 !-------------------------------------------------------------------------------
128 ! Generic Define
129 !-------------------------------------------------------------------------------
131  MODULE PROCEDURE diagnostic_cdf_define_desc
132  END INTERFACE
133 
134 !-------------------------------------------------------------------------------
135 ! Generic Write
136 !-------------------------------------------------------------------------------
138  MODULE PROCEDURE diagnostic_cdf_write_desc
139  END INTERFACE
140 
141 !-------------------------------------------------------------------------------
142 ! Generic Read
143 !-------------------------------------------------------------------------------
145  MODULE PROCEDURE diagnostic_cdf_read_desc
146  END INTERFACE
147 !-------------------------------------------------------------------------------
148 
149  CONTAINS
150 !*******************************************************************************
151 ! SECTION III. DEFINE SUBROUTINES
152 !*******************************************************************************
153 !-------------------------------------------------------------------------------
154 !
155 !-------------------------------------------------------------------------------
156  SUBROUTINE diagnostic_cdf_define_desc(this,iou,prefix)
157 ! Subroutine to do the appropriate netCDF definition calls for a diagnostic_desc
158 !
159 !-----------------------------------------------
160 ! D u m m y A r g u m e n t s
161 !-----------------------------------------------
162  TYPE (diagnostic_desc), INTENT (in) :: this
163  INTEGER, INTENT(in) :: iou
164  CHARACTER (len=*), INTENT(in), OPTIONAL :: prefix
165 
166 ! this diagnostic_desc - this is what gets defined and written.
167 ! iou i/o unit number of the netCDF file.
168 ! prefix character - prefixed to variable names, so that netCDF
169 ! doesn't have problems with repeated, identical names
170 !-----------------------------------------------
171 ! L o c a l V a r i a b l e s
172 !-----------------------------------------------
173  CHARACTER(len=*), PARAMETER :: sub_name = &
174  & 'diagnostic_cdf_define_desc: '
175  CHARACTER(len=32) :: prefix_use
176 
177 !-----------------------------------------------
178 ! Start of Executable Code
179 !-----------------------------------------------
180 
181 ! Define the prefix to actually use
182  IF (PRESENT(prefix)) THEN
183  prefix_use = trim(adjustl(prefix))
184  ELSE
185  prefix_use = ' '
186  ENDIF
187 
188 ! Define all vn_--_use variable names
189  CALL diagnostic_cdf_defvn_desc(prefix_use)
190 
191 ! Define Components common to all d_types
192  CALL cdf_define(iou, trim(vn_d_type_use), this % d_type)
193  CALL cdf_define(iou, trim(vn_s_name_use), this % s_name)
194  CALL cdf_define(iou, trim(vn_l_name_use), this % l_name)
195  CALL cdf_define(iou, trim(vn_units_use), this % units)
196  CALL cdf_define(iou, trim(vn_sigma_default_use), &
197  & this % sigma_default)
198 
199 ! Particular coding, depending on d_type
200 
201  SELECT CASE (this % d_type)
202 
203  CASE ('mddc') ! Magnetic Diagnostic
204  CALL mddc_cdf_define(this % mddc,iou,prefix)
205 
206  CASE DEFAULT
207  CALL err_fatal(sub_name // 'unrecognized d_type: ', &
208  & char = this % d_type)
209 
210  END SELECT
211 
212  RETURN
213 
214  END SUBROUTINE diagnostic_cdf_define_desc
215 
216 !*******************************************************************************
217 ! SECTION III. WRITE SUBROUTINES
218 !*******************************************************************************
219 !-------------------------------------------------------------------------------
220 !
221 !-------------------------------------------------------------------------------
222  SUBROUTINE diagnostic_cdf_write_desc(this,iou,prefix)
223 ! Subroutine to do the appropriate netCDF write calls for a diagnostic_desc
224 !
225 !-----------------------------------------------
226 ! D u m m y A r g u m e n t s
227 !-----------------------------------------------
228  TYPE (diagnostic_desc), INTENT (in) :: this
229  INTEGER, INTENT (in) :: iou
230  CHARACTER (len=*), INTENT (in), OPTIONAL :: prefix
231 
232 ! this diagnostic_desc - this is what gets written.
233 ! iou i/o unit number of the netCDF file.
234 ! prefix character - prefixed to variable names, so that netCDF
235 ! doesn't have problems with repeated, identical names
236 !-----------------------------------------------
237 ! L o c a l V a r i a b l e s
238 !-----------------------------------------------
239  CHARACTER(len=*), PARAMETER :: sub_name = &
240  & 'diagnostic_cdf_write_desc: '
241  CHARACTER(len=32) :: prefix_use
242 
243 !-----------------------------------------------
244 ! Start of Executable Code
245 !-----------------------------------------------
246 ! Define the prefix to actually use
247  IF (PRESENT(prefix)) THEN
248  prefix_use = trim(adjustl(prefix))
249  ELSE
250  prefix_use = ' '
251  ENDIF
252 
253 ! Define all vn_--_use variable names
254  CALL diagnostic_cdf_defvn_desc(prefix_use)
255 
256 ! Write Components
257  CALL cdf_write(iou, trim(vn_d_type_use), this % d_type)
258  CALL cdf_write(iou, trim(vn_s_name_use), this % s_name)
259  CALL cdf_write(iou, trim(vn_l_name_use), this % l_name)
260  CALL cdf_write(iou, trim(vn_units_use), this % units)
261  CALL cdf_write(iou, trim(vn_sigma_default_use), &
262  & this % sigma_default)
263 
264 ! Particular coding, depending on d_type
265 
266  SELECT CASE (this % d_type)
267 
268  CASE ('mddc') ! Magnetic Diagnostic
269  CALL mddc_cdf_write(this % mddc,iou,prefix)
270 
271  CASE DEFAULT
272  CALL err_fatal(sub_name // 'unrecognized d_type: ', &
273  & char = this % d_type)
274 
275  END SELECT
276 
277  RETURN
278 
279  END SUBROUTINE diagnostic_cdf_write_desc
280 
281 !*******************************************************************************
282 ! SECTION IV. READ SUBROUTINES
283 !*******************************************************************************
284 !-------------------------------------------------------------------------------
285 !
286 !-------------------------------------------------------------------------------
287  SUBROUTINE diagnostic_cdf_read_desc(this,iou,prefix)
288 ! Subroutine to do the appropriate netCDF read calls for a diagnostic_desc
289 !
290 !-----------------------------------------------
291 ! D u m m y A r g u m e n t s
292 !-----------------------------------------------
293  TYPE (diagnostic_desc), INTENT (inout) :: this
294  INTEGER, INTENT (in) :: iou
295  CHARACTER (len=*), INTENT (in), OPTIONAL :: prefix
296 
297 ! this diagnostic_desc - this is what gets defined and written.
298 ! iou i/o unit number of the netCDF file.
299 ! prefix character - prefixed to variable names, so that netCDF
300 ! doesn't have problems with repeated, identical names
301 !-----------------------------------------------
302 ! L o c a l V a r i a b l e s
303 !-----------------------------------------------
304  CHARACTER(len=*), PARAMETER :: sub_name = &
305  & 'diagnostic_cdf_read_desc: '
306  CHARACTER(len=32) :: prefix_use
307  INTEGER, DIMENSION(3) :: dimlens
308 
309  CHARACTER (len=type_len) :: d_type
310  CHARACTER (len=sn_len) :: s_name
311  CHARACTER (len=ln_len) :: l_name
312  CHARACTER (len=units_len) :: units
313  REAL(rprec) :: sigma_default
314  TYPE (mddc_desc) :: mddc
315 
316 !-----------------------------------------------
317 ! Start of Executable Code
318 !-----------------------------------------------
319 
320 ! Define the prefix to actually use
321  IF (PRESENT(prefix)) THEN
322  prefix_use = trim(adjustl(prefix))
323  ELSE
324  prefix_use = ' '
325  ENDIF
326 
327 ! Define all vn_--_use variable names
328  CALL diagnostic_cdf_defvn_desc(prefix_use)
329 
330 ! Read Components common to all d_types
331 ! Note: Read in to variables local to this subroutine.
332  CALL cdf_read(iou, trim(vn_d_type_use), d_type)
333  CALL cdf_read(iou, trim(vn_s_name_use), s_name)
334  CALL cdf_read(iou, trim(vn_l_name_use), l_name)
335  CALL cdf_read(iou, trim(vn_units_use), units)
336  CALL cdf_read(iou, trim(vn_sigma_default_use), &
337  & sigma_default)
338 
339 ! Particular coding, depending on d_type
340  SELECT CASE (d_type)
341 
342  CASE ('mddc') ! Magnetic Diagnostic
343  CALL mddc_cdf_read(mddc,iou,prefix)
344 
345 !...............Create the diagnostic_desc, this...............!
346  CALL diagnostic_construct(this,d_type,s_name,l_name, &
347  & units,sigma_default,mddc)
348 
349  CASE DEFAULT
350  CALL err_fatal(sub_name // 'unrecognized d_type: ', &
351  & char = d_type)
352 
353  END SELECT
354 
355 
356 
357 ! Destroy the local mddc, to avoid memory leakage
358  CALL mddc_destroy(mddc)
359 
360  RETURN
361 
362  END SUBROUTINE diagnostic_cdf_read_desc
363 
364 !*******************************************************************************
365 ! SECTION IV. AUXILIARY FUNCTIONS AND SUBROUTINES
366 !*******************************************************************************
367 !-------------------------------------------------------------------------------
368 !
369 !-------------------------------------------------------------------------------
370  SUBROUTINE diagnostic_cdf_defvn_desc(prefix_use)
371 ! Subroutine to do define the character variable names for a diagnostic_desc,
372 ! using the prefix. All the vn_ variables are module variables, and so do not
373 ! need to be declared here
374 
375 !-----------------------------------------------
376 ! D u m m y A r g u m e n t s
377 !-----------------------------------------------
378  CHARACTER (len=*), INTENT (in) :: prefix_use
379 
380 ! prefix_use character - prefixed to variable names, so that netCDF
381 ! doesn't have problems with repeated, identical names
382 
383 !-----------------------------------------------
384 ! L o c a l V a r i a b l e s
385 !-----------------------------------------------
386  CHARACTER(len=*), PARAMETER :: sub_name = &
387  & 'diagnostic_cdf_defvn_desc: '
388 
389 !-----------------------------------------------
390 ! Start of Executable Code
391 !-----------------------------------------------
392 
393 ! Define all variable names
394  vn_d_type_use = diagnostic_cdf_mknam(prefix_use,vn_d_type)
395  vn_s_name_use = diagnostic_cdf_mknam(prefix_use,vn_s_name)
396  vn_l_name_use = diagnostic_cdf_mknam(prefix_use,vn_l_name)
397  vn_units_use = diagnostic_cdf_mknam(prefix_use,vn_units)
398  vn_sigma_default_use = diagnostic_cdf_mknam(prefix_use, &
399  & vn_sigma_default)
400 
401  RETURN
402 
403  END SUBROUTINE diagnostic_cdf_defvn_desc
404 
405 !-------------------------------------------------------------------------------
406 !
407 !-------------------------------------------------------------------------------
408  FUNCTION diagnostic_cdf_mknam(c1,c2)
409 ! A simple function to help in the generation of names
410 
411 !-----------------------------------------------
412 ! F u n c t i o n N a m e
413 !-----------------------------------------------
414  CHARACTER(LEN=64) diagnostic_cdf_mknam
415 
416 !-----------------------------------------------
417 ! D u m m y A r g u m e n t s
418 !-----------------------------------------------
419  CHARACTER(LEN=*), INTENT (in) :: c1,c2
420 
421 !-----------------------------------------------
422 ! Start of Executable Code
423 !-----------------------------------------------
424  IF (len_trim(c1) .eq. 0) THEN
425  diagnostic_cdf_mknam = trim(c2)
426  ELSE
427  diagnostic_cdf_mknam = adjustl(trim(c1) // '_' // trim(c2))
428  ENDIF
429 
430  RETURN
431 
432  END FUNCTION diagnostic_cdf_mknam
433 
434 !-----------------------------------------------
435 !-----------------------------------------------
436 !*******************************************************************************
437 ! SECTION X. COMMENTS FOR DIFFERENT REVISIONS
438 !*******************************************************************************
439 !
440 ! 09-02-2004 JDH - Initial Coding, based on bsc_cdf
441 !
442 ! JDH 12-11-2004
443 ! Modified _desc subroutines, to correspond with changes in diagnostic_T.
444 !
445 ! JDH 07-01-2005
446 ! Added bsc_destroy of local bsc_coil mdcoil in subroutine
447 ! diagnostic_cdf_read_desc, to avoid memory leaks.
448 !
449 ! JDH 2007-06-11 - Altered coding to reflect definition of mddc_desc derived type
450 !
451 ! JDH 2008-01-19 - Added => null() to pointer declarations
452 !
453 ! JDH 2009-06-15
454 ! Eliminated diagnostic_data derived type - not needed.
455 
456  END MODULE diagnostic_cdf
diagnostic_cdf::diagnostic_cdf_write
Definition: diagnostic_cdf.f:137
mddc_cdf::mddc_cdf_write
Definition: mddc_cdf.f:199
diagnostic_t::diagnostic_construct
Definition: diagnostic_T.f:111
mddc_cdf::mddc_cdf_read
Definition: mddc_cdf.f:207
mddc_t::mddc_destroy
Definition: mddc_T.f:198
diagnostic_cdf::diagnostic_cdf_read
Definition: diagnostic_cdf.f:144
diagnostic_cdf::diagnostic_cdf_define
Definition: diagnostic_cdf.f:130
mddc_cdf::mddc_cdf_define
Definition: mddc_cdf.f:191