2 !TODO: remove duplication between ext_esmf_read_field and
3 !TODO: ext_esmf_write_field
5 !TODO: how to deal with time? (via current ESMF_Clock)
6 !TODO: to begin, use it as an error check...
10 SUBROUTINE ext_esmf_read_field ( DataHandle , DateStr , VarName , Field , FieldType , Comm , IOComm, &
11 DomainDesc , MemoryOrder , Stagger , DimNames , &
12 DomainStart , DomainEnd , &
13 MemoryStart , MemoryEnd , &
14 PatchStart , PatchEnd , &
18 INTEGER ,INTENT(IN) :: DataHandle
19 CHARACTER*(*) ,intent(inout) :: DateStr
20 CHARACTER*(*) ,intent(inout) :: VarName
21 integer ,intent(inout) :: FieldType
22 integer ,intent(inout) :: Comm
23 integer ,intent(inout) :: IOComm
24 integer ,intent(inout) :: DomainDesc
25 character*(*) ,intent(inout) :: MemoryOrder
26 character*(*) ,intent(inout) :: Stagger
27 character*(*) ,intent(inout) :: DimNames(*)
28 integer ,intent(inout) :: DomainStart(*), DomainEnd(*)
29 integer ,intent(inout) :: MemoryStart(*), MemoryEnd(*)
30 integer ,intent(inout) :: PatchStart(*), PatchEnd(*)
31 REAL ,INTENT(INOUT) :: Field(*)
32 integer ,intent(out) :: Status
34 INTEGER :: ids,ide,jds,jde,kds,kde
35 INTEGER :: ims,ime,jms,jme,kms,kme
36 INTEGER :: ips,ipe,jps,jpe,kps,kpe
37 TYPE(ESMF_State), POINTER :: importstate
38 TYPE(ESMF_Field) :: tmpField
39 TYPE(ESMF_Array) :: tmpArray
40 TYPE(ESMF_ArraySpec) :: arrayspec
42 REAL(ESMF_KIND_R4), POINTER :: data_esmf_real_ptr(:,:)
43 REAL(ESMF_KIND_R4), POINTER :: tmp_esmf_r4_ptr(:,:)
44 INTEGER(ESMF_KIND_I4), POINTER :: data_esmf_int_ptr(:,:)
45 INTEGER, PARAMETER :: esmf_rank = 2
46 INTEGER :: DomainEndFull(esmf_rank), idefull, jdefull, ict, i, j
47 INTEGER :: PatchEndFull(esmf_rank), ipefull, jpefull
48 ! esmf_counts is redundant. remove it as soon as ESMF_ArrayCreate no
50 INTEGER :: esmf_counts(esmf_rank)
51 INTEGER :: rc, debug_level
52 LOGICAL, EXTERNAL :: has_char
55 CALL get_wrf_debug_level( debug_level )
57 IF ( .NOT. int_valid_handle( DataHandle ) ) THEN
58 CALL wrf_error_fatal("ext_esmf_read_field: invalid data handle" )
60 IF ( .NOT. int_handle_in_use( DataHandle ) ) THEN
61 CALL wrf_error_fatal("ext_esmf_read_field: DataHandle not opened" )
63 IF ( .NOT. opened_for_read( DataHandle ) ) THEN
64 CALL wrf_error_fatal("ext_esmf_read_field: DataHandle not opened for read" )
67 write(mess,*)'ext_esmf_read_field ',DataHandle, TRIM(DateStr), TRIM(VarName)
68 call wrf_debug( 300, TRIM(mess) )
70 IF ( FieldType .EQ. WRF_REAL ) THEN
71 esmf_kind = ESMF_KIND_R4
72 ELSE IF ( FieldType .EQ. WRF_DOUBLE ) THEN
73 ! esmf_kind = ESMF_KIND_R8
74 CALL wrf_error_fatal( 'ext_esmf_read_field, WRF_DOUBLE not yet supported')
75 ELSE IF ( FieldType .EQ. WRF_INTEGER ) THEN
76 esmf_kind = ESMF_KIND_I4
77 !TODO: implement this (below)
78 CALL wrf_error_fatal( 'ext_esmf_read_field, WRF_INTEGER not yet implemented')
79 ELSE IF ( FieldType .EQ. WRF_LOGICAL ) THEN
80 CALL wrf_error_fatal( 'ext_esmf_read_field, WRF_LOGICAL not yet supported')
83 ims = MemoryStart(1) ; ime = MemoryEnd(1)
84 jms = MemoryStart(2) ; jme = MemoryEnd(2)
85 kms = MemoryStart(3) ; kme = MemoryEnd(3)
87 ips = PatchStart(1) ; ipe = PatchEnd(1)
88 jps = PatchStart(2) ; jpe = PatchEnd(2)
89 kps = PatchStart(3) ; kpe = PatchEnd(3)
91 ids = DomainStart(1) ; ide = DomainEnd(1)
92 jds = DomainStart(2) ; jde = DomainEnd(2)
93 kds = DomainStart(3) ; kde = DomainEnd(3)
95 ! For now, treat all arrays as 2D...
96 !TODO: Eventually, use ../io_netcdf subroutines Transpose() and reorder()
97 !TODO: (and etc.) to handle general array ranks and index orderings.
98 !TODO: Some copies of these exist in ../../frame/module_io.F.
99 !TODO: Then use ESMF_ArrayDataMap class to handle index mapping.
100 IF ( kms /= kme ) THEN
101 CALL wrf_error_fatal( 'ext_esmf_read_field: rank > 2 not yet supported')
104 ! The non-staggered variables come in at one-less than
105 ! domain dimensions, but io_esmf is currently hacked to use full
106 ! domain spec, so adjust if not staggered.
107 !TODO: Remove EndFull hackery once ESMF can support staggered
108 !TODO: grids in regional models. (This hack works around the current
109 !TODO: need to use only larger staggered dimensions for ESMF_Arrays.)
110 CALL ioesmf_endfullhack( esmf_rank, DomainEnd, PatchEnd, Stagger, &
111 DomainEndFull, PatchEndFull )
112 idefull = DomainEndFull(1)
113 jdefull = DomainEndFull(2)
114 ipefull = PatchEndFull(1)
115 jpefull = PatchEndFull(2)
117 write(mess,*) ' ext_esmf_read_field: okay_to_read: ', DataHandle, okay_to_read(DataHandle)
118 call wrf_debug( 300, TRIM(mess) )
120 ! case 1: the file is opened for read but not committed ("training")
121 IF ( .NOT. okay_to_read( DataHandle ) ) THEN
123 ! Training: build the ESMF import state
124 write(mess,*) ' ext_esmf_read_field: TRAINING READ: DataHandle = ', DataHandle
125 call wrf_debug( 300, TRIM(mess) )
127 ! First, build the ESMF_Grid for this DataHandle, if it does not
129 write(0,*)__FILE__,__LINE__,'ext_esmf_read_field Stagger',TRIM(Stagger)
130 write(0,*)__FILE__,__LINE__,'ext_esmf_read_field VarName',TRIM(VarName)
131 write(0,*)__FILE__,__LINE__,'ext_esmf_read_field DomainEnd ', DomainEnd(1:esmf_rank)
132 write(0,*)__FILE__,__LINE__,'ext_esmf_read_field PatchEnd ', PatchEnd(1:esmf_rank)
133 CALL ioesmf_create_grid( DataHandle, esmf_rank, MemoryOrder, Stagger, &
134 DomainStart(1:esmf_rank), DomainEnd(1:esmf_rank), &
135 MemoryStart(1:esmf_rank), MemoryEnd(1:esmf_rank), &
136 PatchStart(1:esmf_rank), PatchEnd(1:esmf_rank) )
137 ! Grab the current importState and add to it...
138 CALL ESMF_ImportStateGetCurrent( importstate, rc )
139 IF ( rc /= ESMF_SUCCESS ) THEN
140 CALL wrf_error_fatal("ext_esmf_read_field, training: ESMF_ImportStateGetCurrent failed" )
143 ! The following code does not work for reasons as-yet unknown.
144 ! A likely suspect is lbounds and ubounds which fail in other interfaces in
146 ! Build ESMF objects...
147 ! Build an ESMF_ArraySpec. The use of ESMF_ArraySpec and ESMF_Array
148 ! objects allows some of the code that follows to be type-kind-independent.
150 ! Build an ESMF_Array
151 ! Implementation note: since we do not yet have full control over how
152 ! ESMF chooses to lay out a "patch" within "memory", we must copy by
153 ! hand. (Reasons include lack of support in ESMF for asymmetric halos,
154 ! addition of "extra" rows/columns to optimize alignment on some machines,
155 ! handling of periodic boundary conditions, etc.) Thus, there
156 ! is no point in using larger "memory" sizes to build the array -- patch
157 ! is fine. Also, since we must copy anyway, might as well let ESMF manage
158 ! the memory for simplicity.
159 !TODO: Once ESMF can match WRF memory-patch mapping, replace this with a more
160 !TODO: efficient solution that does not require a copy.
161 !TODO: esmf_counts is redundant. Remove it as soon as ESMF_ArrayCreate no
162 !TODO: longer requires it.
163 ! esmf_counts(1:esmf_rank) = DomainEndFull(1:esmf_rank) - &
164 ! DomainStart(1:esmf_rank) + 1
165 ! tmpArray = ESMF_ArrayCreate(arrayspec, counts=esmf_counts, &
166 ! lbounds=DomainStart(1:esmf_rank), &
167 ! ubounds=DomainEndFull(1:esmf_rank), &
169 ! IF ( rc /= ESMF_SUCCESS ) THEN
170 ! WRITE(mess,*) ' ext_esmf_read_field: ESMF_ArrayCreate failed, rc = ', rc
171 ! CALL wrf_error_fatal( TRIM(mess) )
173 ! Determine grid staggering for this Field
174 ! IF ( has_char( Stagger, 'x' ) .AND. has_char( Stagger, 'y' ) ) THEN
175 ! CALL wrf_error_fatal( &
176 ! "ext_esmf_read_field: ESMF does not yet support XY staggering for C-grid" )
177 ! ELSE IF ( has_char( Stagger, 'x' ) ) THEN
178 ! horzrelloc=ESMF_CELL_WFACE
179 ! ELSE IF ( has_char( Stagger, 'y' ) ) THEN
180 ! horzrelloc=ESMF_CELL_SFACE
182 ! horzrelloc=ESMF_CELL_CENTER
184 ! Build an ESMF_Field
185 ! Note: though it is counter-intuitive, ESMF uses
186 ! shallow-copy-masquerading-as-reference to implement the
187 ! pseudo-equivalent of POINTER assignment under-the-hood. What this means
188 ! here is that it is OK to pass deep object tmpArray into
189 ! ESMF_FieldCreate() and then return from this subroutine. Even though
190 ! tmpArray goes out of scope, it is OK. However, if tmpArray were to be
191 ! modified after this call, the changes would not be guaranteed to always
192 ! appear in tmpField. It works that way now, but ESMF Core team has
193 ! plans that may make it break in the future. Build-it, attach-it,
194 ! flush-it will work. Build-it, attach-it, modify-it, flush-it may not
196 ! Note: unique Field name is required by ESMF_StateAddField().
197 !TODO: use CF "standard_name" once the WRF Registry supports it
198 ! tmpField = ESMF_FieldCreate( grid( DataHandle )%ptr, tmpArray, &
199 ! copyflag=ESMF_DATA_REF, &
200 ! horzrelloc=horzrelloc, name=TRIM(VarName), &
203 !TODO: Compute horzrelloc from Stagger as above once ESMF supports staggering
204 ! horzrelloc=ESMF_CELL_CENTER
205 !TODO: Add code for other data types here...
206 ! ALLOCATE( tmp_esmf_r4_ptr(ips:ipefull,jps:jpefull) )
207 ALLOCATE( tmp_esmf_r4_ptr(ips:ipe,jps:jpe) )
208 write(mess,*)'ext_esmf_read_field: calling ESMF_FieldCreate field=',trim(varname)
209 CALL wrf_debug ( 100, mess )
211 tmpField = ESMF_FieldCreate( &
212 grid( DataHandle )%ptr, &
214 datacopyflag=ESMF_DATACOPY_REFERENCE, &
215 staggerloc=ESMF_STAGGERLOC_CENTER, &
216 name=TRIM(VarName), &
218 IF ( rc /= ESMF_SUCCESS ) THEN
219 WRITE(mess,*) ' ext_esmf_read_field: ESMF_FieldCreate failed, rc = ', rc
220 CALL wrf_error_fatal( TRIM(mess) )
222 CALL wrf_debug ( 100, 'ext_esmf_read_field: back from ESMF_FieldCreate' )
223 WRITE(mess,*) 'ext_esmf_read_field: tmp_esmf_r4_ptr(', &
224 LBOUND(tmp_esmf_r4_ptr,1),':',UBOUND(tmp_esmf_r4_ptr,1),',', &
225 LBOUND(tmp_esmf_r4_ptr,2),':',UBOUND(tmp_esmf_r4_ptr,2),')'
226 CALL wrf_debug ( 100 , TRIM(mess) )
227 ! Add the Field to the import state...
228 !TODO: for now, just build ESMF_Fields and stuff them in
229 !TODO: later, use a single ESMF_Bundle
230 CALL ESMF_StateAdd( importstate, (/tmpField/), rc=rc ) ! 5.2.0r only accepts arrays, use array constructor
231 IF ( rc /= ESMF_SUCCESS ) THEN
232 CALL wrf_error_fatal("ext_esmf_read_field: ESMF_StateAddField failed" )
234 write(mess,*) ' ext_esmf_read_field: END TRAINING READ: DataHandle = ', DataHandle
235 call wrf_debug( 300, TRIM(mess) )
237 ! case 2: opened for read and committed
238 ELSE IF ( okay_to_read( DataHandle ) ) THEN
240 write(mess,*) ' ext_esmf_read_field: ACTUAL READ: DataHandle = ', DataHandle
241 call wrf_debug( 300, TRIM(mess) )
243 ! read: extract data from the ESMF import state
244 ! Grab the current importState
245 CALL ESMF_ImportStateGetCurrent( importstate, rc )
246 IF ( rc /= ESMF_SUCCESS ) THEN
247 CALL wrf_error_fatal("ext_esmf_read_field: ESMF_ImportStateGetCurrent failed" )
250 CALL ESMF_StateGet( importstate, itemName=TRIM(VarName), &
251 field=tmpfield, rc=rc )
252 IF ( rc /= ESMF_SUCCESS ) THEN
253 CALL wrf_error_fatal("ext_esmf_read_field: ESMF_StateGet failed" )
256 CALL wrf_debug ( 100, 'ext_esmf_read_field '//TRIM(VarName)//': calling ESMF_FieldPrint( tmpField ) 1' )
257 IF ( 100 .LE. debug_level ) THEN
258 CALL ESMF_FieldPrint( tmpField, rc=rc )
260 CALL wrf_debug ( 100, 'ext_esmf_read_field '//TRIM(VarName)//': back from ESMF_FieldPrint( tmpField ) 1' )
262 ! grab a pointer to the import state data and copy data into Field
263 IF ( FieldType .EQ. WRF_REAL ) THEN
264 CALL ESMF_FieldGet( tmpField, 0, data_esmf_real_ptr, rc=rc )
265 IF ( rc /= ESMF_SUCCESS ) THEN
266 CALL wrf_error_fatal("ext_esmf_read_field: ESMF_FieldGetDataPointer(r4) failed" )
268 IF ( ( PatchStart(1) /= LBOUND(data_esmf_real_ptr,1) ) .OR. &
269 ( PatchEnd(1) /= UBOUND(data_esmf_real_ptr,1) ) .OR. &
270 ( PatchStart(2) /= LBOUND(data_esmf_real_ptr,2) ) .OR. &
271 ( PatchEnd(2) /= UBOUND(data_esmf_real_ptr,2) ) ) THEN
272 WRITE( mess,* ) 'ESMF_FieldGetDataPointer bounds mismatch', &
276 ', ips:ipe,jps:jpe = ',PatchStart(1),':',PatchEnd(1),',', &
277 PatchStart(2),':',PatchEnd(2), &
278 ', data_esmf_real_ptr(BOUNDS) = ', &
279 LBOUND(data_esmf_real_ptr,1),':',UBOUND(data_esmf_real_ptr,1),',', &
280 LBOUND(data_esmf_real_ptr,2),':',UBOUND(data_esmf_real_ptr,2)
281 CALL wrf_error_fatal ( TRIM(mess) )
284 WRITE( mess,* ) 'DEBUG: ext_esmf_read_field: ips:ipe,jps:jpe = ', &
285 ips,':',ipe,',',jps,':',jpe, &
286 ', data_esmf_real_ptr(BOUNDS) = ', &
287 LBOUND(data_esmf_real_ptr,1),':',UBOUND(data_esmf_real_ptr,1),',', &
288 LBOUND(data_esmf_real_ptr,2),':',UBOUND(data_esmf_real_ptr,2)
289 CALL wrf_debug( 300, TRIM(mess) )
291 CALL ioesmf_extract_data_real( data_esmf_real_ptr, Field, &
292 ips, ipe, jps, jpe, kps, kpe, &
293 ims, ime, jms, jme, kms, kme )
294 ELSE IF ( FieldType .EQ. WRF_INTEGER ) THEN
295 CALL ESMF_FieldGet( tmpField, 0, data_esmf_int_ptr, rc=rc )
296 IF ( rc /= ESMF_SUCCESS ) THEN
297 CALL wrf_error_fatal("ext_esmf_read_field: ESMF_FieldGetDataPointer(i4) failed" )
299 IF ( ( PatchStart(1) /= LBOUND(data_esmf_int_ptr,1) ) .OR. &
300 ( PatchEnd(1) /= UBOUND(data_esmf_int_ptr,1) ) .OR. &
301 ( PatchStart(2) /= LBOUND(data_esmf_int_ptr,2) ) .OR. &
302 ( PatchEnd(2) /= UBOUND(data_esmf_int_ptr,2) ) ) THEN
303 WRITE( mess,* ) 'ESMF_FieldGetDataPointer bounds mismatch', &
307 ', ips:ipe,jps:jpe = ',PatchStart(1),':',PatchEnd(1),',', &
308 PatchStart(2),':',PatchEnd(2), &
309 ', data_esmf_int_ptr(BOUNDS) = ', &
310 LBOUND(data_esmf_int_ptr,1),':',UBOUND(data_esmf_int_ptr,1),',', &
311 LBOUND(data_esmf_int_ptr,2),':',UBOUND(data_esmf_int_ptr,2)
312 CALL wrf_error_fatal ( TRIM(mess) )
314 CALL ioesmf_extract_data_int( data_esmf_int_ptr, Field, &
315 ips, ipe, jps, jpe, kps, kpe, &
316 ims, ime, jms, jme, kms, kme )
318 write(mess,*) ' ext_esmf_read_field: END ACTUAL READ: DataHandle = ', DataHandle
319 call wrf_debug( 300, TRIM(mess) )
323 CALL wrf_debug ( 100, 'ext_esmf_read_field '//TRIM(VarName)//': calling ESMF_FieldPrint( tmpField ) 2' )
324 IF ( 100 .LE. debug_level ) THEN
325 CALL ESMF_FieldPrint( tmpField, rc=rc )
327 CALL wrf_debug ( 100, 'ext_esmf_read_field '//TRIM(VarName)//': back from ESMF_FieldPrint( tmpField ) 2' )
333 END SUBROUTINE ext_esmf_read_field