source: LMDZ6/branches/DYNAMICO-conv-GC/libf/phy_common/xios_writefield.F90 @ 5440

Last change on this file since 5440 was 3323, checked in by Laurent Fairhead, 7 years ago

Adding some new routines mainly dealing with unstructured grids to the physics

File size: 10.6 KB
Line 
1MODULE xios_writefield_mod
2#ifdef CPP_XIOS
3  PRIVATE
4 
5  INTEGER,PARAMETER :: size_name=255
6  INTEGER, PARAMETER :: MaxWriteField = 100
7  CHARACTER(len=size_name), SAVE ::  FieldName(MaxWriteField)
8!$OMP THREADPRIVATE(FieldName)
9  INTEGER,                  SAVE  :: FieldIt (MaxWriteField)
10!$OMP THREADPRIVATE(FieldIt)
11  INTEGER,SAVE :: NbField = 0
12!$OMP THREADPRIVATE(NbField)
13
14  INTERFACE xios_writefield
15    MODULE PROCEDURE xios_writefield2d,xios_writefield3d
16  END INTERFACE
17
18  LOGICAL,SAVE :: output_native_grid
19!$OMP THREADPRIVATE( output_native_grid) 
20
21  INTEGER,SAVE :: ni_glo , nj_glo
22!$OMP THREADPRIVATE( ni_glo, nj_glo) 
23
24   
25  PUBLIC xios_writefield
26
27CONTAINS
28 
29  FUNCTION NameId(name_in)
30  IMPLICIT NONE
31    CHARACTER(LEN=*),INTENT(IN)      :: name_in
32    INTEGER                          :: nameId
33    CHARACTER(LEN=size_name)         :: name
34    INTEGER :: n 
35   
36    name=name_in
37     
38    DO n=1,NbField
39      IF (name==fieldName(n)) THEN
40        nameId=n
41        RETURN
42      ENDIF
43    ENDDO
44     
45    nameId=0
46    RETURN
47  END FUNCTION NameId
48     
49 
50 
51  SUBROUTINE xios_writefield2d(field,name_in)
52  USE dimphy
53  USE mod_phys_lmdz_para
54  USE xios
55  USE print_control_mod, ONLY:  lunout
56  USE mod_grid_phy_lmdz, only: nbp_lon, nbp_lat, klon_glo, grid_type, unstructured, regular_lonlat
57  USE wxios, ONLY: wxios_domain_param_unstructured, wxios_domain_param,  wxios_set_context
58  USE ioipsl
59  IMPLICIT NONE
60  REAL, DIMENSION(:), INTENT(IN) :: field
61  CHARACTER(LEN=*)                 :: name_in
62  CHARACTER(LEN=size_name)         :: name
63  TYPE(xios_context) :: xios_ctx
64  TYPE(xios_domaingroup) :: domain_definition
65  TYPE(xios_domaingroup) :: my_domaingroup
66  TYPE(xios_domain)      :: my_domain
67  TYPE(xios_domain)      :: my_regular_domain
68  TYPE(xios_filegroup)   :: file_definition
69  TYPE(xios_file)        :: my_file
70  TYPE(xios_field)       :: my_field
71  TYPE(xios_fieldgroup)  :: field_definition
72  TYPE(xios_generate_rectilinear_domain) :: generate_domain
73  TYPE(xios_interpolate_domain) :: interpolate_domain
74  INTEGER                :: id
75  REAL,DIMENSION(klon_mpi) :: field_mpi 
76  REAL :: Field2d(nbp_lon,jj_nb)
77 
78   
79    IF (size(field,1) /= klon_omp) THEN
80      WRITE(lunout,*) 'xios_writefield :: '//FieldName//' is not on the model grid'
81      RETURN
82    ENDIF
83 
84    name = TRIM(ADJUSTL(name_in))
85    id=nameId(name)
86   
87    IF (id/=0) THEN
88      IF (is_omp_master) THEN
89        CALL xios_get_handle('context_lmdz_'//TRIM(name), xios_ctx)    !Récupération
90        CALL xios_set_current_context(xios_ctx)            !Activation
91      ENDIF
92    ELSE
93      output_native_grid=.FALSE.
94      ni_glo=0
95      nj_glo=0
96      IF (is_master) CALL getin("xios_writefield_nlon",ni_glo)
97      IF (is_master) CALL getin("xios_writefield_nlat",nj_glo)
98      CALL bcast(ni_glo)
99      CALL bcast(nj_glo)
100      IF (ni_glo==0 .OR. nj_glo==0) output_native_grid=.TRUE.
101
102      IF (is_omp_master) THEN
103        CALL xios_context_initialize('context_lmdz_'//TRIM(name), COMM_LMDZ_PHY)
104        CALL xios_get_handle('context_lmdz_'//TRIM(name), xios_ctx)    !Récupération
105        CALL xios_set_current_context(xios_ctx)            !Activation
106     
107        CALL xios_define_calendar("D360")
108        CALL xios_set_start_date(xios_date(2000,1,1,0,0,0))
109        CALL xios_set_time_origin(xios_date(2000,1,1,0,0,0))
110        CALL xios_set_timestep(xios_second)
111     
112        CALL xios_get_handle("domain_definition",domain_definition)
113        CALL xios_add_child(domain_definition,my_domaingroup,"domaingroup")
114        CALL xios_add_child(my_domaingroup,my_domain,"domain")
115        IF (grid_type==unstructured .AND. .NOT. output_native_grid ) THEN
116          CALL xios_add_child(domain_definition, my_regular_domain, "regular_domain")
117          CALL xios_set_attr(my_regular_domain,ni_glo=ni_glo, nj_glo=nj_glo, type="rectilinear")
118          CALL xios_add_child(my_regular_domain,generate_domain)
119          CALL xios_set_attr(generate_domain,lon_start=-180., lat_start=90., lat_end=-90.)
120          CALL xios_add_child(my_regular_domain,interpolate_domain)
121        ENDIF
122         
123      ENDIF
124     
125      IF (grid_type==regular_lonlat) THEN
126        CALL wxios_domain_param("domain")
127      ELSE IF (grid_type==unstructured) THEN
128        CALL wxios_domain_param_unstructured("domaingroup")
129      ENDIF       
130     
131      IF (is_omp_master) THEN
132        CALL xios_get_handle("file_definition",file_definition)
133        CALL xios_add_child(file_definition, my_file)
134        CALL xios_set_attr(my_file,name=TRIM(name),output_freq=xios_timestep,sync_freq=xios_timestep,type="one_file")
135        CALL xios_get_handle("field_definition",field_definition)
136        CALL xios_add_child(field_definition, my_field, TRIM(name))
137        CALL xios_set_attr(my_field,domain_ref="domain",operation="instant")
138        CALL xios_add_child(my_file, my_field)
139        CALL xios_set_attr(my_field,field_ref=TRIM(name))
140        IF (grid_type==unstructured .AND. .NOT. output_native_grid) CALL xios_set_attr(my_field,domain_ref="regular_domain")
141
142        CALL xios_close_context_definition()
143     
144        NbField=NbField+1
145        FieldName(NbField)=TRIM(name)
146        FieldIt(NbField)=0
147        id=NbField
148      ENDIF
149    ENDIF
150 
151    CALL Gather_omp(field,field_mpi)
152
153    IF (is_omp_master) THEN
154      FieldIt(id)=FieldIt(id)+1
155      CALL xios_update_calendar(FieldIt(id))
156
157      IF (grid_type==regular_lonlat) THEN
158        CALL grid1Dto2D_mpi(field_mpi,Field2d)   
159        CALL xios_send_field(TRIM(name), Field2d)
160      ELSE IF (grid_type==unstructured) THEN
161        CALL xios_send_field(TRIM(name), field_mpi)
162      ENDIF
163    ENDIF
164   
165    CALL wxios_set_context()
166 
167  END SUBROUTINE  xios_writefield2d
168
169  SUBROUTINE xios_writefield3d(field,name_in)
170  USE dimphy
171  USE mod_phys_lmdz_para
172  USE xios
173  USE print_control_mod, ONLY:  lunout
174  USE mod_grid_phy_lmdz, only: nbp_lon, nbp_lat, klon_glo, grid_type, unstructured, regular_lonlat
175  USE wxios, ONLY: wxios_domain_param_unstructured, wxios_domain_param,  wxios_set_context
176  USE ioipsl
177  IMPLICIT NONE
178  REAL, DIMENSION(:,:), INTENT(IN) :: field
179  CHARACTER(LEN=*)                 :: name_in
180  CHARACTER(LEN=size_name)         :: name
181  TYPE(xios_context) :: xios_ctx
182  TYPE(xios_domaingroup) :: domain_definition
183  TYPE(xios_domaingroup) :: my_domaingroup
184  TYPE(xios_domain)      :: my_domain
185  TYPE(xios_domain)      :: my_regular_domain
186  TYPE(xios_filegroup)   :: file_definition
187  TYPE(xios_file)        :: my_file
188  TYPE(xios_field)       :: my_field
189  TYPE(xios_fieldgroup)  :: field_definition
190  TYPE(xios_generate_rectilinear_domain) :: generate_domain
191  TYPE(xios_interpolate_domain) :: interpolate_domain
192  TYPE(xios_axisgroup)   :: axis_definition
193  TYPE(xios_axis)        :: my_axis
194  INTEGER                :: id
195  REAL,DIMENSION(klon_mpi,size(field,2)) :: field_mpi 
196  REAL :: Field2d(nbp_lon,jj_nb,size(field,2))
197  REAL :: axis_value(size(field,2))
198  INTEGER :: i
199   
200    IF (size(field,1) /= klon_omp) THEN
201      WRITE(lunout,*) 'xios_writefield :: '//FieldName//' is not on the model grid'
202      RETURN
203    ENDIF
204 
205    name = TRIM(ADJUSTL(name_in))
206    id=nameId(name)
207   
208    IF (id/=0) THEN
209   
210      IF (is_omp_master) CALL xios_get_handle('context_lmdz_'//TRIM(name), xios_ctx)    !Récupération
211      IF (is_omp_master) CALL xios_set_current_context(xios_ctx)            !Activation
212   
213    ELSE
214      output_native_grid=.FALSE.
215      ni_glo=0
216      nj_glo=0
217      IF (is_master) CALL getin("xios_writefield_nlon",ni_glo)
218      IF (is_master) CALL getin("xios_writefield_nlat",nj_glo)
219      CALL bcast(ni_glo)
220      CALL bcast(nj_glo)
221      IF (ni_glo==0 .OR. nj_glo==0) output_native_grid=.TRUE.
222
223      IF (is_omp_master) THEN
224        CALL xios_context_initialize('context_lmdz_'//TRIM(name), COMM_LMDZ_PHY)
225        CALL xios_get_handle('context_lmdz_'//TRIM(name), xios_ctx)    !Récupération
226        CALL xios_set_current_context(xios_ctx)            !Activation
227     
228        CALL xios_define_calendar("D360")
229        CALL xios_set_start_date(xios_date(2000,1,1,0,0,0))
230        CALL xios_set_time_origin(xios_date(2000,1,1,0,0,0))
231        CALL xios_set_timestep(xios_second)
232     
233        CALL xios_get_handle("domain_definition",domain_definition)
234        CALL xios_add_child(domain_definition,my_domaingroup,"domaingroup")
235        CALL xios_add_child(my_domaingroup,my_domain,"domain")
236        IF (grid_type==unstructured .AND. .NOT. output_native_grid) THEN
237          CALL xios_add_child(domain_definition, my_regular_domain, "regular_domain")
238          CALL xios_set_attr(my_regular_domain,ni_glo=ni_glo, nj_glo=nj_glo, type="rectilinear")
239          CALL xios_add_child(my_regular_domain,generate_domain)
240          CALL xios_set_attr(generate_domain,lon_start=-180., lat_start=90., lat_end=-90.)
241          CALL xios_add_child(my_regular_domain,interpolate_domain)
242        ENDIF
243        CALL xios_get_handle("axis_definition",axis_definition)
244        CALL xios_add_child(axis_definition,my_axis,"axis")
245        axis_value=(/(i,i=1,size(field,2))/)
246        CALL xios_set_attr(my_axis,name="z",n_glo=size(field,2),value=axis_value, unit="level" )       
247      ENDIF
248     
249      IF (grid_type==regular_lonlat) THEN
250        CALL wxios_domain_param("domain")
251      ELSE IF (grid_type==unstructured) THEN
252        CALL wxios_domain_param_unstructured("domaingroup")
253      ENDIF       
254     
255      IF (is_omp_master) THEN
256        CALL xios_get_handle("file_definition",file_definition)
257        CALL xios_add_child(file_definition, my_file)
258        CALL xios_set_attr(my_file,name=TRIM(name),output_freq=xios_timestep,sync_freq=xios_timestep,type="one_file")
259        CALL xios_get_handle("field_definition",field_definition)
260        CALL xios_add_child(field_definition, my_field, TRIM(name))
261        CALL xios_set_attr(my_field,domain_ref="domain",axis_ref="axis", operation="instant")
262        CALL xios_add_child(my_file, my_field)
263        CALL xios_set_attr(my_field,field_ref=TRIM(name))
264        IF (grid_type==unstructured .AND. .NOT. output_native_grid) CALL xios_set_attr(my_field,domain_ref="regular_domain")
265
266        CALL xios_close_context_definition()
267     
268        NbField=NbField+1
269        FieldName(NbField)=TRIM(name)
270        FieldIt(NbField)=0
271        id=NbField
272      ENDIF
273    ENDIF
274 
275    CALL Gather_omp(field,field_mpi)
276
277    IF (is_omp_master) THEN
278      FieldIt(id)=FieldIt(id)+1
279      CALL xios_update_calendar(FieldIt(id))
280
281      IF (grid_type==regular_lonlat) THEN
282        CALL grid1Dto2D_mpi(field_mpi,Field2d)   
283        CALL xios_send_field(TRIM(name), Field2d)
284      ELSE IF (grid_type==unstructured) THEN
285        CALL xios_send_field(TRIM(name), field_mpi)
286      ENDIF
287    ENDIF
288   
289    CALL wxios_set_context()
290 
291  END SUBROUTINE  xios_writefield3d
292
293#endif
294END MODULE
Note: See TracBrowser for help on using the repository browser.