SURFEX v8.1
General documentation of Surfex
sfx_xios_check_field.F90
Go to the documentation of this file.
1 !SFX_LIC Copyright 1994-2014 CNRS, Meteo-France and Universite Paul Sabatier
2 !SFX_LIC This is part of the SURFEX software governed by the CeCILL-C licence
3 !SFX_LIC version 1. See LICENSE, CeCILL-C_V1-en.txt and CeCILL-C_V1-fr.txt
4 !SFX_LIC for details. version 1.
5 SUBROUTINE sfx_xios_check_field(U, HREC, HCOMMENT, OWRITE, PFIELD1, PFIELD2, PFIELD3, HAXIS)
6 !!
7 !!
8 !! PURPOSE
9 !! --------
10 !!
11 !! Ensure that a field is already declared to Xios.
12 !! If not, declare it using HREC, and declare it in a default
13 !! output file if this file is enabled ;
14 !! If 'units' or 'name' attribute is not defined using XIOS config
15 !! files , use HCOMMENT to declare it
16 !!
17 !! IMPLICIT ARGUMENTS :
18 !! --------------------
19 !!
20 !! YXIOS_DOMAIN
21 !!
22 !!
23 !! EXTERNAL
24 !! --------
25 !!
26 !! XIOS LIBRARY
27 !!
28 !!
29 !! REFERENCE
30 !! ---------
31 !!
32 !! XIOS Reference guide - Yann Meurdesoif - 10/10/2014 -
33 !! svn co -r 515 http://forge.ipsl.jussieu.fr/ioserver/svn/XIOS/branchs/xios-1.0 <dir>
34 !! cd <dir>/doc ; ....
35 !!
36 !! AUTHOR
37 !! ------
38 !!
39 !! S.Sénési, CNRM
40 !!
41 !! MODIFICATION
42 !! --------------
43 !!
44 !! Original 08/2015
45 !!
46 !-------------------------------------------------------------------------------
47 !
48 !* 0. DECLARATIONS
49 ! ------------
50 !
52 USE modd_surf_par, ONLY : xundef
53 USE modd_surf_atm_n, ONLY : surf_atm_t
54 USE yomhook , ONLY : lhook, dr_hook
55 USE parkind1 , ONLY : jprb
56 !
57 #ifdef WXIOS
58 USE xios, ONLY : xios_field, xios_fieldgroup, xios_file, xios_get_handle,&
59  xios_add_child, xios_set_attr, xios_is_defined_field_attr, xios_set_field_attr, &
60  xios_get_file_attr, xios_is_valid_file, xios_is_valid_field, xios_is_valid_axis, &
61  xios_is_defined_file_attr
62 #endif
63 !
64 USE modi_sfx_xios_check_field_2d
65 USE modi_abor1_sfx
66 !
67 IMPLICIT NONE
68 !
69 ! Arguments
70 !
71 TYPE(surf_atm_t) , INTENT(INOUT) :: U
72  CHARACTER(LEN=*) ,INTENT(IN) :: HREC ! name of the field to check
73  CHARACTER(LEN=100) ,INTENT(IN) :: HCOMMENT ! Comment string
74 LOGICAL ,INTENT(OUT):: OWRITE ! TRUE if no issue re. Xios for this field
75 REAL,DIMENSION(:) ,INTENT(IN) , OPTIONAL :: PFIELD1 ! value
76 REAL,DIMENSION(:,:) ,INTENT(IN) , OPTIONAL :: PFIELD2 ! value
77 REAL,DIMENSION(:,:,:) ,INTENT(IN) , OPTIONAL :: PFIELD3 ! value
78  CHARACTER(LEN=*) ,INTENT(IN) , OPTIONAL :: HAXIS ! name of the additional axis
79 !
80 ! Local variables
81 !
82 LOGICAL :: LISDEF, LLWRITE
83 INTEGER :: IPO,IPF
84 INTEGER :: KSIZE
85 REAL(KIND=JPRB) :: ZHOOK_HANDLE
86 !
87 #ifdef WXIOS
88 TYPE(xios_field) :: field_hdl, other_field_hdl
89 TYPE(xios_fieldgroup) :: fieldgroup_hdl
90 TYPE(xios_file) :: file_hdl
91 #endif
92 !
93 IF (lhook) CALL dr_hook('SFX_XIOS_CHECK_FIELD',0,zhook_handle)
94 !
95 #ifdef WXIOS
96 !
97 owrite=.true.
98 !
99 ! ----------------------------------------------------------------------
100 ! If field is 3D , just give up
101 ! ----------------------------------------------------------------------
102 !
103 IF (PRESENT(pfield3)) THEN
104  CALL abor1_sfx('THIS XIOS INTERFACE CANNOT YET HANDLE 2 AXES IN ADDITION TO HORIZONTAL SPACE AXES')
105 ENDIF
106 !
107 ! ----------------------------------------------------------------------
108 ! Check consistency between field size and current domain size
109 ! ----------------------------------------------------------------------
110 !
111 IF ((PRESENT(pfield1)) .OR. (PRESENT(pfield2)) .OR. (PRESENT(pfield3)) ) THEN
112  IF (PRESENT(pfield1)) ksize=SIZE(pfield1)
113  IF (PRESENT(pfield2)) ksize=SIZE(pfield2,1)
114  IF (PRESENT(pfield3)) ksize=SIZE(pfield3,1)
115  IF ((yxios_domain=='FULL' ) .AND. (ksize /= u%NSIZE_FULL )) owrite=.false.
116  IF ((yxios_domain=='NATURE') .AND. (ksize /= u%NSIZE_NATURE)) owrite=.false.
117  IF ((yxios_domain=='SEA ') .AND. (ksize /= u%NSIZE_SEA )) owrite=.false.
118  IF ((yxios_domain=='WATER ') .AND. (ksize /= u%NSIZE_WATER )) owrite=.false.
119  IF ((yxios_domain=='TOWN ') .AND. (ksize /= u%NSIZE_TOWN )) owrite=.false.
120  IF (.NOT. owrite) THEN
121  IF (.NOT. lxios_def_closed) THEN
122  CALL abor1_sfx('SFX_XIOS_CHECK_FIELD : Inconsistent size for field '//hrec//' on domain '//yxios_domain)
123  ENDIF
124  IF (lhook) CALL dr_hook('SFX_XIOS_CHECK_FIELD',1,zhook_handle)
125  RETURN
126  ENDIF
127 ENDIF
128 !
129 ! ----------------------------------------------------------------------
130 ! If XIOS init phase is over, just check if field is known to XIOS
131 ! and returns
132 ! ----------------------------------------------------------------------
133 !
134 IF (lxios_def_closed) THEN
135 !$OMP SINGLE
136  owrite=xios_is_valid_field(hrec)
137 !$OMP END SINGLE
138  IF (lhook) CALL dr_hook('SFX_XIOS_CHECK_FIELD',1,zhook_handle)
139  RETURN
140 ENDIF
141 !
142 !$OMP SINGLE
143 !
144 ! ----------------------------------------------------------------------
145 ! We are still in the XIOS init phase => Define field if necessary
146 ! ----------------------------------------------------------------------
147 !
148 owrite=.false.
149 IF (.NOT. xios_is_valid_field(hrec)) THEN
150  CALL xios_get_handle("field_definition",fieldgroup_hdl)
151  CALL xios_add_child(fieldgroup_hdl,field_hdl,hrec)
152  ! Inherit default values from 'default_field'
153  IF (.NOT. xios_is_valid_field("default_field")) &
154  CALL abor1_sfx('sfx_xios_check_field:cannot output field '//hrec//' : no default_field is defined')
155  ! With XIOS2, next call creates an issue
156  ! CALL XIOS_SET_ATTR(field_hdl,field_ref="default_field",name=HREC)
157  CALL xios_set_attr(field_hdl,name=hrec)
158  !
159 ELSE
160  CALL xios_get_handle(hrec,field_hdl)
161 ENDIF
162 !
163 ! ----------------------------------------------------------------------
164 ! If field enabling is not defined, set it to TRUE
165 ! ----------------------------------------------------------------------
166 !
167  CALL xios_is_defined_field_attr(hrec,enabled=lisdef)
168 IF ( .NOT. lisdef ) CALL xios_set_field_attr(hrec, enabled=.true.)
169 !
170 ! ----------------------------------------------------------------------
171 ! If field attribute 'domain' is not defined, set it
172 ! ----------------------------------------------------------------------
173 !
174  CALL xios_is_defined_field_attr(hrec,domain_ref=lisdef)
175 IF ( .NOT. lisdef ) THEN
176  CALL xios_set_field_attr(hrec, domain_ref=yxios_domain)
177 ENDIF
178 !
179 ! ----------------------------------------------------------------------
180 ! 2d fields are a special case, and may lead to (implicit) recursion
181 ! ----------------------------------------------------------------------
182 !
183 IF (PRESENT(pfield2)) THEN
184  IF (PRESENT(haxis)) THEN
185  CALL sfx_xios_check_field_2d(u, hrec, hcomment, owrite, pfield2, haxis)
186  ELSE
187  CALL sfx_xios_check_field_2d(u, hrec, hcomment, owrite, pfield2)
188  ENDIF
189 ENDIF
190 !
191 ! ----------------------------------------------------------------------
192 ! If NetCDF variable name is not defined , set it
193 ! ----------------------------------------------------------------------
194 !
195  CALL xios_is_defined_field_attr(hrec,name=lisdef)
196 IF ( .NOT. lisdef ) THEN
197  CALL xios_set_field_attr(hrec, name=hrec)
198 ENDIF
199 !
200 ! ----------------------------------------------------------------------
201 ! If field attribute 'long_name' is not defined or empty, set it
202 ! ----------------------------------------------------------------------
203 !
204  CALL xios_is_defined_field_attr(hrec,long_name=lisdef)
205 IF ( .NOT. lisdef .AND. (trim(hcomment) /= '') ) THEN
206  CALL xios_set_field_attr(hrec,long_name=trim(hcomment))
207 ENDIF
208 !
209 !
210 ! ------------------------------------------------------------------------
211 ! If field attribute 'units' is not defined or empty, try to guess a value
212 ! from HCOMMENT (using rightmost string between parenthesis)
213 ! ------------------------------------------------------------------------
214 !
215  CALL xios_is_defined_field_attr(hrec,unit=lisdef)
216 IF ( .NOT. lisdef ) THEN
217  ipo=index(hcomment,"(",.true.)
218  ipf=index(hcomment,")",.true.)
219  IF ( (ipo > 0) .AND. (ipf>ipo+1) ) THEN
220  CALL xios_set_field_attr(hrec,unit=hcomment(ipo+1:ipf-1))
221  ENDIF
222 ENDIF
223 !
224 
225 ! ----------------------------------------------------------------------
226 ! Set default value to Surfex's one
227 ! ----------------------------------------------------------------------
228 !
229  CALL xios_set_field_attr(hrec,default_value=xundef)
230 !
231 ! ----------------------------------------------------------------------
232 ! If file 'default_ouput is enabled, add field to it
233 ! ----------------------------------------------------------------------
234 !
235 IF ( xios_is_valid_file(coutput_default)) THEN
236  CALL xios_get_handle(coutput_default,file_hdl)
237  CALL xios_is_defined_file_attr(coutput_default,enabled=lisdef)
238  IF (lisdef ) CALL xios_get_file_attr(coutput_default,enabled=lisdef)
239  IF (lisdef) THEN
240  CALL xios_add_child(file_hdl,field_hdl)
241  CALL xios_set_attr(field_hdl,field_ref=hrec)
242  ENDIF
243 ELSE
244  CALL abor1_sfx('sfx_xios_check_field : cannot output field '//hrec//' : no default_output file is defined')
245 ENDIF
246 !
247 !
248 !$OMP END SINGLE
249 #else
250 owrite=.false.
251 #endif
252 !
253 IF (lhook) CALL dr_hook('SFX_XIOS_CHECK_FIELD',1,zhook_handle)
254 ! ----------------------------------------------------------------------
255 !
256 END SUBROUTINE sfx_xios_check_field
static const char * trim(const char *name, int *n)
Definition: drhook.c:2383
quick &counting sorts only inumt inumt name
subroutine sfx_xios_check_field_2d(U, HREC, HCOMMENT, OWRITE, PFIELD2, HAXIS)
subroutine abor1_sfx(YTEXT)
Definition: abor1_sfx.F90:7
real, parameter xundef
logical lxios_def_closed
Definition: modd_xios.F90:54
integer, parameter jprb
Definition: parkind1.F90:32
logical lhook
Definition: yomhook.F90:15
subroutine sfx_xios_check_field(U, HREC, HCOMMENT, OWRITE, PFIELD1, PFIELD2, PFIELD3, HAXIS)
ERROR in index
Definition: ecsort_shared.h:90
character(len=6) yxios_domain
Definition: modd_xios.F90:55
character(len=14) coutput_default
Definition: modd_xios.F90:47