SURFEX  V8_0
Surfex V8_0 release
 All Classes Files Functions Variables
mode_gltools_wrivais.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 !GLT_LIC The GELATO model is a seaice model used in stand-alone or embedded mode.
6 !GLT_LIC It has been developed by Meteo-France. The holder of GELATO is Meteo-France.
7 !GLT_LIC
8 !GLT_LIC This software is governed by the CeCILL-C license under French law and biding
9 !GLT_LIC by the rules of distribution of free software. See the CeCILL-C_V1-en.txt
10 !GLT_LIC (English) and CeCILL-C_V1-fr.txt (French) for details. The CeCILL is a free
11 !GLT_LIC software license, explicitly compatible with the GNU GPL
12 !GLT_LIC (see http://www.gnu.org/licenses/license-list.en.html#CeCILL)
13 !GLT_LIC
14 !GLT_LIC The CeCILL-C licence agreement grants users the right to modify and re-use the
15 !GLT_LIC software governed by this free software license. The exercising of this right
16 !GLT_LIC is conditional upon the obligation to make available to the community the
17 !GLT_LIC modifications made to the source code of the software so as to contribute to
18 !GLT_LIC its evolution.
19 !GLT_LIC
20 !GLT_LIC In consideration of access to the source code and the rights to copy, modify
21 !GLT_LIC and redistribute granted by the license, users are provided only with a limited
22 !GLT_LIC warranty and the software's author, the holder of the economic rights, and the
23 !GLT_LIC successive licensors only have limited liability. In this respect, the risks
24 !GLT_LIC associated with loading, using, modifying and/or developing or reproducing the
25 !GLT_LIC software by the user are brought to the user's attention, given its Free
26 !GLT_LIC Software status, which may make it complicated to use, with the result that its
27 !GLT_LIC use is reserved for developers and experienced professionals having in-depth
28 !GLT_LIC computer knowledge. Users are therefore encouraged to load and test the
29 !GLT_LIC suitability of the software as regards their requirements in conditions enabling
30 !GLT_LIC the security of their systems and/or data to be ensured and, more generally, to
31 !GLT_LIC use and operate it in the same conditions of security.
32 !GLT_LIC
33 !GLT_LIC The GELATO sofware is cureently distibuted with the SURFEX software, available at
34 !GLT_LIC http://www.cnrm.meteo.fr/surfex. The fact that you download the software deemed that
35 !GLT_LIC you had knowledge of the CeCILL-C license and that you accept its terms.
36 !GLT_LIC Attempts to use this software in a way not complying with CeCILL-C license
37 !GLT_LIC may lead to prosecution.
38 !GLT_LIC
39 ! =======================================================================
40 ! ====================== MODULE mode_gltools_wrivais ====================
41 ! =======================================================================
42 !
43 ! Goal:
44 ! -----
45 ! Write a diagnostic field in Vairmer format
46 !
47 ! Created : 2004/03 (D. Salas y Melia)
48 ! Modified: 2010/09 (D. Salas y Melia) Introduce pwgt weights
49 ! Modified: 2012/07 (D. Salas y Melia) Parallelism
50 ! Modified: 2014/01 (D. Salas y Melia) Generalize: choice of logical unit,
51 ! single or double precision, 2d or 3d fields
52 !
53 ! -------------------- BEGIN MODULE mode_gltools_wrivais -------------------
54 !
56 !
57 INTERFACE gltools_wrivai
58  MODULE PROCEDURE gltools_wrivai_2d
59  MODULE PROCEDURE gltools_wrivai_3d
60 END INTERFACE
61 !
62  CONTAINS
63 !
64 ! -----------------------------------------------------------------------
65 ! ------------------- SUBROUTINE gltools_wrivai_2d ----------------------
66 !
67 SUBROUTINE gltools_wrivai_2d &
68  ( tpnam,pfield,kunit,kdbl,pwgt )
69 !
70  USE modd_glt_param
71  USE modd_types_glt
73  USE modi_gltools_strlower
74 #if ! defined in_surfex
75  USE mode_gltools_mpi
76  USE mode_gltools_bound
77 #else
78 #if ! defined in_arpege
80 #endif
81 #endif
82  IMPLICIT NONE
83 !
84 !* Arguments
85 !
86  TYPE(t_def), INTENT(in) :: &
87  tpnam
88  REAL, DIMENSION(:,:), INTENT(in) :: &
89  pfield
90  INTEGER, OPTIONAL, INTENT(in) :: &
91  kunit,kdbl
92  REAL, DIMENSION(:,:), OPTIONAL, INTENT(in) :: &
93  pwgt
94 !
95 !* Local variables
96 !
97  LOGICAL :: &
98  yis0d,yis2d
99  CHARACTER(1) :: &
100  ypos
101  CHARACTER(6) :: &
102  ytype
103  INTEGER :: &
104  idbl,ix,iy,ixc,iyc,ilu
105  REAL, DIMENSION(:,:), ALLOCATABLE :: &
106  zwork
107  REAL, DIMENSION(:,:), ALLOCATABLE :: &
108  zwork_g
109  REAL(KIND=4), DIMENSION(:,:), ALLOCATABLE :: &
110  zwork_gr4
111 !
112 !
113 !
114 ! 1. Initialisation
115 ! ==================
116 !
117 !
118 ! .. Define field metadata
119 !
120  ypos = tpnam%loc
121  ytype = gltools_strlower( trim(tpnam%typ) )
122 !
123 ! .. Single or double precision write
124 !
125  IF ( present(kdbl) ) THEN
126  idbl = kdbl
127  ELSE
128  idbl = 0
129  ENDIF
130 !
131 !
132 !
133 ! 2. Bound input array and convert it to single precision
134 ! ========================================================
135 !
136 ! .. Get sizes of input data field
137 !
138  ix = SIZE( pfield,1 )
139  iy = SIZE( pfield,2 )
140  yis0d = ( ix==1 .AND. iy==1 )
141  yis2d = ( ix==nx .AND. iy==ny )
142 !
143 ! .. Weighting (pwgt is generally total sea ice concentration, or
144 ! average sea ice thickness)
145 !
146  IF ( yis2d ) THEN
147  ALLOCATE( zwork(nx,ny))
148  IF ( present(pwgt) ) THEN
149  WHERE( pwgt(:,:)>0. .AND. pfield(:,:)<xbig20 )
150  zwork(:,:) = pfield(:,:) / pwgt(:,:)
151  ELSEWHERE
152  zwork(:,:) = xbig20
153  ENDWHERE
154  ELSE
155  zwork(:,:) = pfield(:,:)
156  ENDIF
157 !
158 ! .. Boundary conditions
159 !
160 #if ! defined in_surfex
161  CALL gltools_bound( ypos,ytype,zwork,pval=xbig20 )
162 #endif
163 !
164 ! .. Gather the field to be written
165 !
166  ALLOCATE( zwork_g(nxglo,nyglo) )
167  IF ( idbl==0 ) ALLOCATE( zwork_gr4(nxglo,nyglo) )
168 #if ! defined in_surfex
169  CALL gather2d( zwork,zwork_g )
170 #else
171 #if ! defined in_arpege
172  CALL gather_and_write_mpi( zwork,zwork_g )
173 #endif
174 #endif
175  DEALLOCATE( zwork)
176 !
177 ! .. Correction of bounding with large value on mask for U and V fields
178 !
179  IF ( lwg ) THEN
180  IF ( ypos=='U' .OR. ypos=='V' ) THEN
181  WHERE( zwork_g(:,:)<-xbig19 )
182  zwork_g(:,:) = xbig20
183  ENDWHERE
184  ENDIF
185  ENDIF
186 !
187  ELSE
188 !
189  ALLOCATE( zwork_g(ix,iy))
190  IF ( idbl==0 ) ALLOCATE( zwork_gr4(ix,iy) )
191  zwork_g(:,:) = pfield(:,:)
192 !
193  ENDIF
194 !
195  IF ( lwg ) THEN
196 !
197 ! .. Convert to single precision
198 !
199  IF ( idbl==0 ) zwork_gr4(:,:) = zwork_g(:,:)
200 !
201 !
202 !
203 ! 3. Write data
204 ! ==============
205 !
206 ! .. Define logical unit for writing
207 !
208  IF ( present(kunit) ) THEN
209  ilu = kunit
210  ELSE
211  IF ( yis2d ) THEN
212  ilu = n2vilu
213  ELSE IF ( yis0d ) THEN
214  ilu = n0vilu
215  ELSE
216  IF (lwg) THEN
217  WRITE(noutlu,*) '==> Input field size=',ix,iy
218  WRITE(noutlu,*) '==> Routine gltools_wrivai can only be used to write &
219  & fields with dimensions',nxglo,nyglo,' or 1,1.'
220  WRITE(noutlu,*) 'We stop.'
221  ENDIF
222  stop
223  ENDIF
224  ENDIF
225 !
226 ! .. Write data
227 !
228  WRITE(ilu) trim( tpnam%sna )
229  IF ( idbl==0 ) THEN
230  WRITE(ilu) zwork_gr4(:,:)
231  ELSE
232  WRITE(ilu) zwork_g(:,:)
233  ENDIF
234 !
235  ENDIF
236 !
237 ! .. Deallocations
238 !
239  IF ( idbl==0 ) DEALLOCATE( zwork_gr4 )
240  DEALLOCATE( zwork_g )
241 !
242 !
243 END SUBROUTINE gltools_wrivai_2d
244 !
245 ! ----------------- END SUBROUTINE gltools_wrivai_2d ----------------------
246 ! -------------------------------------------------------------------------
247 !
248 !
249 ! -----------------------------------------------------------------------
250 ! ------------------- SUBROUTINE gltools_wrivai_3d ----------------------
251 !
252 SUBROUTINE gltools_wrivai_3d &
253  ( tpnam,pfield,kunit,kdbl,pwgt )
254 !
255  USE modd_glt_param
256  USE modd_types_glt
258  USE modi_gltools_strlower
259 #if ! defined in_surfex
260  USE mode_gltools_mpi
261  USE mode_gltools_bound
262 #else
264 #endif
265  IMPLICIT NONE
266 !
267 !* Arguments
268 !
269  TYPE(t_def), INTENT(in) :: &
270  tpnam
271  REAL, DIMENSION(:,:,:), INTENT(in) :: &
272  pfield
273  INTEGER, OPTIONAL, INTENT(in) :: &
274  kunit,kdbl
275  REAL, DIMENSION(:,:,:), OPTIONAL, INTENT(in) :: &
276  pwgt
277 !
278 !* Local variables
279 !
280  LOGICAL :: &
281  yis0d,yis2d
282  CHARACTER(1) :: &
283  ypos
284  CHARACTER(6) :: &
285  ytype
286  INTEGER :: &
287  idbl,it,ix,iy,ixc,iyc,ilu
288  REAL, DIMENSION(:,:,:), ALLOCATABLE :: &
289  zwork
290  REAL, DIMENSION(:,:,:), ALLOCATABLE :: &
291  zwork_g
292  REAL(KIND=4), DIMENSION(:,:,:), ALLOCATABLE :: &
293  zwork_gr4
294 !
295 !
296 !
297 ! 1. Initialisation
298 ! ==================
299 !
300 !
301 ! .. Define field metadata
302 !
303  ypos = tpnam%loc
304  ytype = gltools_strlower( trim(tpnam%typ) )
305 !
306 ! .. Single or double precision write
307 !
308  IF ( present(kdbl) ) THEN
309  idbl = kdbl
310  ELSE
311  idbl = 0
312  ENDIF
313 !
314 !
315 !
316 ! 2. Bound input array and convert it to single precision
317 ! ========================================================
318 !
319 ! .. Get sizes of input data field
320 !
321  ix = SIZE( pfield,2 )
322  iy = SIZE( pfield,3 )
323  yis0d = ( ix==1 .AND. iy==1 )
324  yis2d = ( ix==nx .AND. iy==ny )
325 !
326 ! .. Weighting (pwgt is generally total sea ice concentration, or
327 ! average sea ice thickness)
328 !
329  IF ( yis2d ) THEN
330  ALLOCATE( zwork(nt,nx,ny))
331  IF ( present(pwgt) ) THEN
332  WHERE( pwgt(:,:,:)>0. .AND. pfield(:,:,:)<xbig20 )
333  zwork(:,:,:) = pfield(:,:,:) / pwgt(:,:,:)
334  ELSEWHERE
335  zwork(:,:,:) = xbig20
336  ENDWHERE
337  ELSE
338  zwork(:,:,:) = pfield(:,:,:)
339  ENDIF
340 !
341 ! .. Boundary conditions
342 !
343 #if ! defined in_surfex
344  CALL gltools_bound( ypos,ytype,zwork,pval=xbig20 )
345 #endif
346 !
347 ! .. Gather the field to be written
348 !
349  ALLOCATE( zwork_g(nt,nxglo,nyglo) )
350  IF ( idbl==0 ) ALLOCATE( zwork_gr4(nt,nxglo,nyglo) )
351 #if ! defined in_surfex
352  CALL gather3d( zwork,zwork_g )
353 #else
354  ! Surfex Gather function cannot yet work on 3D fields and needs
355  ! that first dimension is the one over which MPI distribution occurs ...
356  DO it=1,nt
357  CALL gather_and_write_mpi( zwork(it,:,:),zwork_g(it,:,:))
358  END DO
359 #endif
360  DEALLOCATE( zwork)
361 !
362 ! .. Correction of bounding with large value on mask for U and V fields
363 !
364  IF ( gelato_myrank == gelato_leadproc ) THEN
365  IF ( ypos=='U' .OR. ypos=='V' ) THEN
366  WHERE( zwork_g(:,:,:)<-xbig19 )
367  zwork_g(:,:,:) = xbig20
368  ENDWHERE
369  ENDIF
370  ENDIF
371 !
372  ELSE
373 !
374  ALLOCATE( zwork_g(nt,ix,iy))
375  IF ( idbl==0 ) ALLOCATE( zwork_gr4(nt,ix,iy) )
376  zwork_g(:,:,:) = pfield(:,:,:)
377 !
378  ENDIF
379 !
380  IF ( gelato_myrank == gelato_leadproc ) THEN
381 !
382 ! .. Convert to single precision
383 !
384  IF ( idbl==0 ) zwork_gr4(:,:,:) = zwork_g(:,:,:)
385 !
386 !
387 !
388 ! 3. Write data
389 ! ==============
390 !
391 ! .. Define logical unit for writing
392 !
393  IF ( present(kunit) ) THEN
394  ilu = kunit
395  ELSE
396  IF ( yis2d ) THEN
397  ilu = n2vilu
398  ELSE IF ( yis0d ) THEN
399  ilu = n0vilu
400  ELSE
401  IF (lwg) THEN
402  WRITE(noutlu,*) '==> Input field size=',ix,iy
403  WRITE(noutlu,*) '==> Routine gltools_wrivai can only be used to write &
404  & fields with dimensions',nxglo,nyglo,' or 1,1.'
405  WRITE(noutlu,*) 'We stop.'
406  ENDIF
407  stop
408  ENDIF
409  ENDIF
410 !
411 ! .. Write data
412 !
413  WRITE(ilu) trim( tpnam%sna )
414  IF ( idbl==0 ) THEN
415  WRITE(ilu) zwork_gr4(:,:,:)
416  ELSE
417  WRITE(ilu) zwork_g(:,:,:)
418  ENDIF
419 !
420  ENDIF
421 !
422 ! .. Deallocations
423 !
424  IF ( idbl==0 ) DEALLOCATE( zwork_gr4 )
425  DEALLOCATE( zwork_g )
426 !
427 !
428 END SUBROUTINE gltools_wrivai_3d
429 !
430 ! ----------------- END SUBROUTINE gltools_wrivai_3d ----------------------
431 ! -------------------------------------------------------------------------
432 END MODULE mode_gltools_wrivais
433 !
434 ! -------------------- END MODULE mode_gltools_wrivais --------------------
435 ! -------------------------------------------------------------------------
subroutine gltools_wrivai_3d(tpnam, pfield, kunit, kdbl, pwgt)
subroutine gltools_wrivai_2d(tpnam, pfield, kunit, kdbl, pwgt)
character(len=len(hstring)) function gltools_strlower(hstring)