libsim Versione 8.0.0
vol7d_var_class.F90
1! Copyright (C) 2010 ARPA-SIM <urpsim@smr.arpa.emr.it>
2! authors:
3! Davide Cesari <dcesari@arpa.emr.it>
4! Paolo Patruno <ppatruno@arpa.emr.it>
5
6! This program is free software; you can redistribute it and/or
7! modify it under the terms of the GNU General Public License as
8! published by the Free Software Foundation; either version 2 of
9! the License, or (at your option) any later version.
10
11! This program is distributed in the hope that it will be useful,
12! but WITHOUT ANY WARRANTY; without even the implied warranty of
13! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14! GNU General Public License for more details.
15
16! You should have received a copy of the GNU General Public License
17! along with this program. If not, see <http://www.gnu.org/licenses/>.
18#include "config.h"
19
20!> Classe per la gestione delle variabili osservate da stazioni meteo e affini.
21!! Questo modulo definisce una classe per rappresentare variabili meteorologiche
22!! osservate, o attributi, aventi diversi tipi numerici o carattere.
23!! \ingroup vol7d
24MODULE vol7d_var_class
25USE kinds
28IMPLICIT NONE
29
30!> Definisce una variabile meteorologica osservata o un suo attributo.
31!! I membri \a r, \a d, \a i, \a b, \a c servono, internamente a vol7d,
32!! per associare le variabili agli attributi, e indicano
33!! a quale variabile, nel descrittore delle variabili, coincide
34!! la variabile corrente nel descrittore delle "variabili aventi attributo".
35!! I membri di \a vol7d_var sono pubblici e quindi liberamente
36!! accessibili e scrivibili, ma è comunque consigliato assegnarli tramite
37!! il costruttore ::init.
38TYPE vol7d_var
39 CHARACTER(len=10) :: btable=cmiss !< codice della variabile secondo la tabella B del WMO.
40 CHARACTER(len=65) :: description=cmiss !< descrizione testuale della variabile (opzionale)
41 CHARACTER(len=24) :: unit=cmiss !< descrizione testuale dell'unità di misura (opzionale)
42 INTEGER :: scalefactor=imiss !< numero di decimali nella rappresentazione intera o character (opzionale)
43
44 INTEGER :: r=imiss !< indice della variabile nel volume degli attributi reali
45 INTEGER :: d=imiss !< indice della variabile nel volume degli attributi double precision
46 INTEGER :: i=imiss !< indice della variabile nel volume degli attributi integer
47 INTEGER :: b=imiss !< indice della variabile nel volume degli attributi byte
48 INTEGER :: c=imiss !< indice della variabile nel volume degli attributi character
49 INTEGER :: gribhint(4)=imiss !< hint for conversion from/to grib when btable is not found
50 INTEGER :: feat_ind=imiss !< cached index to corresponding entry in feature table
51END TYPE vol7d_var
52
53!> Valore mancante per vol7d_var.
54TYPE(vol7d_var),PARAMETER :: vol7d_var_miss= &
55 vol7d_var(cmiss,cmiss,cmiss,imiss,imiss,imiss,imiss,imiss,imiss, &
56 (/imiss,imiss,imiss,imiss/))
57
58!> Costruttore per la classe vol7d_var.
59!! Deve essere richiamato
60!! per tutti gli oggetti di questo tipo definiti in un programma.
61INTERFACE init
62 MODULE PROCEDURE vol7d_var_init
63END INTERFACE
64
65!> Distruttore per la classe vol7d_var.
66!! Distrugge l'oggetto in maniera pulita, assegnandogli un valore mancante.
67INTERFACE delete
68 MODULE PROCEDURE vol7d_var_delete
69END INTERFACE
70
71!> Operatore logico di uguaglianza tra oggetti della classe vol7d_var.
72!! Funziona anche per
73!! confronti di tipo array-array (qualsiasi n. di dimensioni) e di tipo
74!! scalare-vettore(1-d) (ma non vettore(1-d)-scalare o tra array con più
75!! di 1 dimensione e scalari).
76INTERFACE OPERATOR (==)
77 MODULE PROCEDURE vol7d_var_eq
78END INTERFACE
79
80!> Operatore logico di disuguaglianza tra oggetti della classe vol7d_var.
81!! Funziona anche per
82!! confronti di tipo array-array (qualsiasi n. di dimensioni) e di tipo
83!! scalare-vettore(1-d) (ma non vettore(1-d)-scalare o tra array con più
84!! di 1 dimensione e scalari).
85INTERFACE OPERATOR (/=)
86 MODULE PROCEDURE vol7d_var_ne, vol7d_var_nesv
87END INTERFACE
88
89!> to be documented
90INTERFACE c_e
91 MODULE PROCEDURE vol7d_var_c_e
92END INTERFACE
93
94#define VOL7D_POLY_TYPE TYPE(vol7d_var)
95#define VOL7D_POLY_TYPES _var
96#include "array_utilities_pre.F90"
97
98!> \brief display on the screen a brief content of object
99INTERFACE display
100 MODULE PROCEDURE display_var, display_var_vect
101END INTERFACE
102
103
104TYPE vol7d_var_features
105 TYPE(vol7d_var) :: var !< the variable (only btable is relevant)
106 REAL :: posdef !< if not missing, minimum physically reasonable value for the variable
107 INTEGER :: vartype !< type of variable, one of the var_* constants
108END TYPE vol7d_var_features
109
110TYPE(vol7d_var_features),ALLOCATABLE :: var_features(:)
111
112! constants for vol7d_vartype
113INTEGER,PARAMETER :: var_ord=0 !< unclassified variable (vol7d_vartype function)
114INTEGER,PARAMETER :: var_dir360=1 !< direction in degrees (vol7d_vartype function)
115INTEGER,PARAMETER :: var_press=2 !< pressure in Pa (vol7d_vartype function)
116INTEGER,PARAMETER :: var_ucomp=3 !< u component of a vector field (vol7d_vartype function)
117INTEGER,PARAMETER :: var_vcomp=4 !< v component of a vector field (vol7d_vartype function)
118INTEGER,PARAMETER :: var_wcomp=5 !< w component of a vector field (vol7d_vartype function)
119
120PRIVATE vol7d_var_features_init
121
122CONTAINS
123
124!> Inizializza un oggetto \a vol7d_var con i parametri opzionali forniti.
125!! Se non viene passato nessun parametro opzionale l'oggetto è
126!! inizializzato a valore mancante.
127!! I membri \a r, \a d, \a i, \a b, \a c non possono essere assegnati
128!! tramite costruttore, ma solo direttamente.
129elemental SUBROUTINE vol7d_var_init(this, btable, description, unit, scalefactor)
130TYPE(vol7d_var),INTENT(INOUT) :: this !< oggetto da inizializzare
131CHARACTER(len=*),INTENT(in),OPTIONAL :: btable !< codice della variabile
132CHARACTER(len=*),INTENT(in),OPTIONAL :: description !< descrizione della variabile
133CHARACTER(len=*),INTENT(in),OPTIONAL :: unit !< unità di misura
134INTEGER,INTENT(in),OPTIONAL :: scalefactor !< decimali nella rappresentazione intera e character
135
136IF (PRESENT(btable)) THEN
137 this%btable = btable
138ELSE
139 this%btable = cmiss
140 this%description = cmiss
141 this%unit = cmiss
142 this%scalefactor = imiss
143 RETURN
144ENDIF
145IF (PRESENT(description)) THEN
146 this%description = description
147ELSE
148 this%description = cmiss
149ENDIF
150IF (PRESENT(unit)) THEN
151 this%unit = unit
152ELSE
153 this%unit = cmiss
154ENDIF
155if (present(scalefactor)) then
156 this%scalefactor = scalefactor
157else
158 this%scalefactor = imiss
159endif
160
161this%r = -1
162this%d = -1
163this%i = -1
164this%b = -1
165this%c = -1
166
167END SUBROUTINE vol7d_var_init
168
169
170ELEMENTAL FUNCTION vol7d_var_new(btable, description, unit, scalefactor) RESULT(this)
171CHARACTER(len=*),INTENT(in),OPTIONAL :: btable !< codice della variabile
172CHARACTER(len=*),INTENT(in),OPTIONAL :: description !< descrizione della variabile
173CHARACTER(len=*),INTENT(in),OPTIONAL :: unit !< unità di misura
174INTEGER,INTENT(in),OPTIONAL :: scalefactor !< decimali nella rappresentazione intera e character
175
176TYPE(vol7d_var) :: this
177
178CALL init(this, btable, description, unit, scalefactor)
179
180END FUNCTION vol7d_var_new
181
182
183!> Distrugge l'oggetto in maniera pulita, assegnandogli un valore mancante.
184elemental SUBROUTINE vol7d_var_delete(this)
185TYPE(vol7d_var),INTENT(INOUT) :: this !< oggetto da distruggre
186
187this%btable = cmiss
188this%description = cmiss
189this%unit = cmiss
190this%scalefactor = imiss
191
192END SUBROUTINE vol7d_var_delete
193
194
195ELEMENTAL FUNCTION vol7d_var_eq(this, that) RESULT(res)
196TYPE(vol7d_var),INTENT(IN) :: this, that
197LOGICAL :: res
198
199res = this%btable == that%btable
200
201END FUNCTION vol7d_var_eq
202
203
204ELEMENTAL FUNCTION vol7d_var_ne(this, that) RESULT(res)
205TYPE(vol7d_var),INTENT(IN) :: this, that
206LOGICAL :: res
207
208res = .NOT.(this == that)
209
210END FUNCTION vol7d_var_ne
211
213FUNCTION vol7d_var_nesv(this, that) RESULT(res)
214TYPE(vol7d_var),INTENT(IN) :: this, that(:)
215LOGICAL :: res(SIZE(that))
216
217INTEGER :: i
218
219DO i = 1, SIZE(that)
220 res(i) = .NOT.(this == that(i))
221ENDDO
222
223END FUNCTION vol7d_var_nesv
224
225
227!> \brief display on the screen a brief content of vol7d_var object
228subroutine display_var(this)
230TYPE(vol7d_var),INTENT(in) :: this !< vol7d_var object to display
231
232print*,"VOL7DVAR: ",this%btable,trim(this%description)," : ",this%unit,&
233 " scale factor",this%scalefactor
235end subroutine display_var
238!> \brief display on the screen a brief content of vector of vol7d_var object
239subroutine display_var_vect(this)
240
241TYPE(vol7d_var),INTENT(in) :: this(:) !< vol7d_var vector object to display
242integer :: i
243
244do i=1,size(this)
245 call display_var(this(i))
246end do
247
248end subroutine display_var_vect
250FUNCTION vol7d_var_c_e(this) RESULT(c_e)
251TYPE(vol7d_var),INTENT(IN) :: this
252LOGICAL :: c_e
253c_e = this /= vol7d_var_miss
254END FUNCTION vol7d_var_c_e
256
257SUBROUTINE vol7d_var_features_set(this)
258TYPE(vol7d_var),INTENT(inout) :: this
259INTEGER :: i
260
261CALL vol7d_var_features_init()
262
263IF (ALLOCATED(var_features)) THEN
264 DO i = 1, SIZE(var_features)
265 IF (this == var_features(i)%var) THEN
266 this%feat_ind = i
267 EXIT
268 ENDIF
269 ENDDO
270ENDIF
271
272END SUBROUTINE vol7d_var_features_set
274
275!> Initialise the global table of variable features.
276!! This subroutine reads the table of variable features from an
277!! external file and stores it in a global array. It has to be called
278!! once at the beginning of the program. At the moment it gives access
279!! to the information about type of variable and positive
280!! definitness. The table is based on the unique bufr-like variable
281!! table. The table is contained in the csv file `vargrib.csv`.
282!! It is not harmful to call this subroutine multiple times.
283SUBROUTINE vol7d_var_features_init()
284INTEGER :: un, i, n
285TYPE(csv_record) :: csv
286CHARACTER(len=1024) :: line
287
288IF (ALLOCATED(var_features)) RETURN
289
290un = open_package_file('varbufr.csv', filetype_data)
291n=0
292DO WHILE(.true.)
293 READ(un,*,END=100)
294 n = n + 1
295ENDDO
296
297100 CONTINUE
298
299rewind(un)
300ALLOCATE(var_features(n))
301
302DO i = 1, n
303 READ(un,'(A)',END=200)line
304 CALL init(csv, line)
305 CALL csv_record_getfield(csv, var_features(i)%var%btable)
306 CALL csv_record_getfield(csv)
307 CALL csv_record_getfield(csv)
308 CALL csv_record_getfield(csv, var_features(i)%posdef)
309 CALL csv_record_getfield(csv, var_features(i)%vartype)
310 CALL delete(csv)
311ENDDO
312
313200 CONTINUE
314CLOSE(un)
315
316END SUBROUTINE vol7d_var_features_init
317
318
319!> Deallocate the global table of variable features.
320!! This subroutine deallocates the table of variable features
321!! allocated in the `vol7d_var_features_init` subroutine.
322SUBROUTINE vol7d_var_features_delete()
323IF (ALLOCATED(var_features)) DEALLOCATE(var_features)
324END SUBROUTINE vol7d_var_features_delete
325
326
327!> Return the physical type of the variable.
328!! Returns a rough classification of the variable depending on the
329!! physical parameter it represents. The result is one of the
330!! constants vartype_* defined in the module. To be extended.
331!! In order for this to work, the subroutine \a
332!! vol7d_var_features_set has to be preliminary called.
333ELEMENTAL FUNCTION vol7d_var_features_vartype(this) RESULT(vartype)
334TYPE(vol7d_var),INTENT(in) :: this !< vol7d_var object to be tested
335INTEGER :: vartype
336
337IF (c_e(this%feat_ind)) THEN
338 vartype = var_features(this%feat_ind)%vartype
339ELSE
340 vartype = imiss
341ENDIF
342
343END FUNCTION vol7d_var_features_vartype
346!> Apply a positive definite flag to a variable.
347!! This subroutine resets the value of a variable depending on its
348!! positive definite flag defined in the associated \a c_func object.
349!! The \a c_func object can be obtained for example by the \a convert
350!! (interfaced to vargrib2varbufr_convert) function. The value is
351!! reset to the maximum between the value itself and and 0 (or the
352!! value set in \a c_func%posdef. These values are set from the
353!! varbufr.csv file.
354!! In order for this to work, the subroutine \a
355!! vol7d_var_features_set has to be preliminary called.
356ELEMENTAL SUBROUTINE vol7d_var_features_posdef_apply(this, val)
357TYPE(vol7d_var),INTENT(in) :: this !< vol7d_var object to be reset
358REAL,INTENT(inout) :: val !< value to be reset, it is reset in place
360IF (c_e(this%feat_ind)) THEN
361 IF (c_e(var_features(this%feat_ind)%posdef)) THEN
362 IF (c_e(val)) THEN
363 val = max(var_features(this%feat_ind)%posdef, val)
364 ENDIF
365 ENDIF
366ENDIF
367
368END SUBROUTINE vol7d_var_features_posdef_apply
369
370
371!> Return the physical type of the variable.
372!! Returns a rough classification of the variable depending on the
373!! physical parameter it represents. The result is one of the
374!! constants vartype_* defined in the module. To be extended.
375ELEMENTAL FUNCTION vol7d_vartype(this) RESULT(vartype)
376TYPE(vol7d_var),INTENT(in) :: this !< vol7d_var object to be tested
377
378INTEGER :: vartype
379
380vartype = var_ord
381SELECT CASE(this%btable)
382CASE('B01012', 'B11001', 'B11043', 'B22001') ! direction, degree true
383 vartype = var_dir360
384CASE('B07004', 'B10004', 'B10051', 'B10060') ! pressure, Pa
385 vartype = var_press
386CASE('B11003', 'B11200') ! u-component
387 vartype = var_ucomp
388CASE('B11004', 'B11201') ! v-component
389 vartype = var_vcomp
390CASE('B11005', 'B11006') ! w-component
391 vartype = var_wcomp
392END SELECT
393
394END FUNCTION vol7d_vartype
395
396
397#include "array_utilities_inc.F90"
398
399
Index method.
Distruttore per la classe vol7d_var.
display on the screen a brief content of object
Costruttore per la classe vol7d_var.
Utilities for managing files.
Definition of constants to be used for declaring variables of a desired type.
Definition kinds.F90:245
Definitions of constants and functions for working with missing values.
Classe per la gestione delle variabili osservate da stazioni meteo e affini.
Class for interpreting the records of a csv file.
Definisce una variabile meteorologica osservata o un suo attributo.

Generated with Doxygen.