@@ -13,7 +13,9 @@ subroutine interp2(localpet, input_file)
13
13
14
14
use esmf
15
15
use netcdf
16
- use model_grid
16
+ use model_grid, only : grid_mdl, i_mdl, j_mdl, &
17
+ num_tiles, latitude_field_mdl, &
18
+ longitude_field_mdl, mask_field_mdl
17
19
use source_grid
18
20
use utils
19
21
use mpi
@@ -23,33 +25,46 @@ subroutine interp2(localpet, input_file)
23
25
character (len=* ), intent (in ) :: input_file
24
26
25
27
integer :: rc, localpet
26
- integer :: i, j, tile, n, ncid, status
27
- integer :: t
28
- integer :: clb_mdl(3 ), cub_mdl(3 )
29
- integer :: varid, record
28
+ integer :: i, j, tile, ncid, status
29
+ integer :: varid
30
30
integer :: isrctermprocessing
31
31
integer :: category, num_categories
32
32
33
33
integer (esmf_kind_i4), allocatable :: mask_mdl_one_tile(:,:)
34
34
integer (esmf_kind_i4), pointer :: unmapped_ptr(:)
35
35
36
- real (esmf_kind_r4 ), pointer :: data_mdl_ptr(:,:,:)
37
36
real (esmf_kind_r4 ), allocatable :: data_src_global(:,:)
38
37
real (esmf_kind_r4 ), allocatable :: data_src_global2(:,:,:)
39
38
real (esmf_kind_r4 ), allocatable :: data_mdl_one_tile(:,:,:)
40
- real (esmf_kind_r4 ), allocatable :: vegt_mdl_one_tile(:,:)
41
39
real (esmf_kind_r4 ), allocatable :: lat_mdl_one_tile(:,:)
42
40
real (esmf_kind_r4 ), allocatable :: sum_mdl_one_tile(:,:)
43
41
real (esmf_kind_r4 ), allocatable :: lon_mdl_one_tile(:,:)
44
42
45
43
type (esmf_regridmethod_flag) :: method
46
44
type (esmf_field) :: data_field_src
47
- type (esmf_field) :: data_field_mdl2
45
+ type (esmf_field) :: data_field_mdl
48
46
type (esmf_routehandle) :: regrid_data
49
47
type (esmf_polemethod_flag) :: pole
50
48
51
- ! get this from file.
52
- num_categories = 20
49
+ if (localpet == 0 ) then
50
+ allocate (data_src_global(i_src,j_src))
51
+ else
52
+ allocate (data_src_global(0 ,0 ))
53
+ endif
54
+
55
+ if (localpet == 0 ) then
56
+ print * ,' - OPEN SOURCE FILE ' , trim (input_file)
57
+ status = nf90_open(input_file, nf90_nowrite, ncid)
58
+ call netcdf_err(status, " IN ROUTINE INTERP OPENING SOURCE FILE" )
59
+ status = nf90_inq_varid(ncid, field_names(1 ), varid)
60
+ call netcdf_err(status, " IN ROUTINE INTERP READING FIELD ID" )
61
+ status = nf90_get_var(ncid, varid, data_src_global, start= (/ 1 ,1 ,1 / ), count= (/ i_src,j_src,1 / ))
62
+ call netcdf_err(status, " IN ROUTINE INTERP READING FIELD" )
63
+ print * ,' number of cats ' ,maxval (data_src_global)
64
+ num_categories = nint (maxval (data_src_global))
65
+ endif
66
+
67
+ call mpi_bcast(num_categories,1 ,MPI_INTEGER,0 ,MPI_COMM_WORLD,status)
53
68
54
69
print * ," - CALL FieldCreate FOR SOURCE GRID DATA."
55
70
data_field_src = ESMF_FieldCreate(grid_src, &
@@ -64,7 +79,7 @@ subroutine interp2(localpet, input_file)
64
79
call error_handler(" IN FieldCreate" , rc)
65
80
66
81
print * ," - CALL FieldCreate FOR model GRID veg DATA."
67
- data_field_mdl2 = ESMF_FieldCreate(grid_mdl, &
82
+ data_field_mdl = ESMF_FieldCreate(grid_mdl, &
68
83
typekind= ESMF_TYPEKIND_R4 , &
69
84
indexflag= ESMF_INDEX_GLOBAL, &
70
85
staggerloc= ESMF_STAGGERLOC_CENTER, &
@@ -75,85 +90,49 @@ subroutine interp2(localpet, input_file)
75
90
if (ESMF_logFoundError(rcToCheck= rc,msg= ESMF_LOGERR_PASSTHRU,line= __LINE__,file= __FILE__)) &
76
91
call error_handler(" IN FieldCreate" , rc)
77
92
78
- print * ," - CALL FieldGet FOR MODEL GRID DATA."
79
- nullify(data_mdl_ptr)
80
- call ESMF_FieldGet(data_field_mdl2, &
81
- farrayPtr= data_mdl_ptr, &
82
- computationalLBound= clb_mdl, &
83
- computationalUBound= cub_mdl, &
84
- rc= rc)
85
- if (ESMF_logFoundError(rcToCheck= rc,msg= ESMF_LOGERR_PASSTHRU,line= __LINE__,file= __FILE__)) &
86
- call error_handler(" IN FieldGet" , rc)
87
-
88
-
89
- print * ,' got here2 ' ,localpet,clb_mdl,cub_mdl
90
-
91
93
if (localpet == 0 ) then
92
- allocate (data_src_global(i_src,j_src))
93
94
allocate (data_src_global2(i_src,j_src,num_categories))
94
- else
95
- allocate (data_src_global(0 ,0 ))
96
- allocate (data_src_global2(0 ,0 ,0 ))
97
- endif
98
-
99
- print * ,' - OPEN SOURCE FILE ' , trim (input_file)
100
- status = nf90_open(input_file, nf90_nowrite, ncid)
101
- call netcdf_err(status, " IN ROUTINE INTERP OPENING SOURCE FILE" )
102
-
103
- if (localpet == 0 ) then
104
95
allocate (data_mdl_one_tile(i_mdl,j_mdl,num_categories))
105
96
allocate (mask_mdl_one_tile(i_mdl,j_mdl))
106
97
allocate (lat_mdl_one_tile(i_mdl,j_mdl))
107
98
allocate (sum_mdl_one_tile(i_mdl,j_mdl))
108
99
allocate (lon_mdl_one_tile(i_mdl,j_mdl))
109
100
else
101
+ allocate (data_src_global2(0 ,0 ,0 ))
110
102
allocate (data_mdl_one_tile(0 ,0 ,0 ))
111
103
allocate (mask_mdl_one_tile(0 ,0 ))
112
104
allocate (lat_mdl_one_tile(0 ,0 ))
113
105
allocate (sum_mdl_one_tile(0 ,0 ))
114
106
allocate (lon_mdl_one_tile(0 ,0 ))
115
107
endif
116
108
117
- record = 0
118
-
119
- TIME_LOOP : do t = 1 , num_time_recs ! loop over each time period
120
-
121
- FIELD_LOOP : do n = 1 , num_fields ! loop over each surface field.
122
-
123
- record = record + 1
124
-
125
- if (localpet == 0 ) then
126
- status = nf90_inq_varid(ncid, field_names(n), varid)
127
- call netcdf_err(status, " IN ROUTINE INTERP READING FIELD ID" )
128
- status = nf90_get_var(ncid, varid, data_src_global, start= (/ 1 ,1 ,t/ ), count= (/ i_src,j_src,1 / ))
129
- call netcdf_err(status, " IN ROUTINE INTERP READING FIELD" )
130
- data_src_global2 = 0.0
131
- do j = 1 , j_src
132
- do i = 1 , i_src
133
- category = nint (data_src_global(i,j))
134
- ! if (category < 1) category = 17
135
- if (category < 1 ) cycle
136
- data_src_global2(i,j,category) = 1.0
137
- enddo
138
- enddo
139
- endif
109
+ if (localpet == 0 ) then
110
+ data_src_global2 = 0.0
111
+ do j = 1 , j_src
112
+ do i = 1 , i_src
113
+ category = nint (data_src_global(i,j))
114
+ if (category < 1 ) cycle
115
+ data_src_global2(i,j,category) = 1.0
116
+ enddo
117
+ enddo
118
+ endif
140
119
141
- print * ," - CALL FieldScatter FOR SOURCE GRID DATA."
142
- call ESMF_FieldScatter(data_field_src, data_src_global2, rootPet= 0 , rc= rc)
143
- if (ESMF_logFoundError(rcToCheck= rc,msg= ESMF_LOGERR_PASSTHRU,line= __LINE__,file= __FILE__)) &
144
- call error_handler(" IN FieldScatter." , rc)
120
+ deallocate (data_src_global)
145
121
146
- isrctermprocessing = 1
122
+ print * ," - CALL FieldScatter FOR SOURCE GRID DATA."
123
+ call ESMF_FieldScatter(data_field_src, data_src_global2, rootPet= 0 , rc= rc)
124
+ if (ESMF_logFoundError(rcToCheck= rc,msg= ESMF_LOGERR_PASSTHRU,line= __LINE__,file= __FILE__)) &
125
+ call error_handler(" IN FieldScatter." , rc)
147
126
148
- if (record == 1 ) then
127
+ isrctermprocessing = 1
149
128
150
- method = ESMF_REGRIDMETHOD_CONSERVE
151
- pole = ESMF_POLEMETHOD_NONE
129
+ method = ESMF_REGRIDMETHOD_CONSERVE
130
+ pole = ESMF_POLEMETHOD_NONE
152
131
153
- print * ," - CALL FieldRegridStore."
154
- nullify(unmapped_ptr)
155
- call ESMF_FieldRegridStore(data_field_src, &
156
- data_field_mdl2 , &
132
+ print * ," - CALL FieldRegridStore."
133
+ nullify(unmapped_ptr)
134
+ call ESMF_FieldRegridStore(data_field_src, &
135
+ data_field_mdl , &
157
136
srcmaskvalues= (/ 0 / ), &
158
137
dstmaskvalues= (/ 0 / ), &
159
138
polemethod= pole, &
@@ -164,111 +143,70 @@ subroutine interp2(localpet, input_file)
164
143
regridmethod= method, &
165
144
unmappedDstList= unmapped_ptr, &
166
145
rc= rc)
167
- if (ESMF_logFoundError(rcToCheck= rc,msg= ESMF_LOGERR_PASSTHRU,line= __LINE__,file= __FILE__)) &
168
- call error_handler(" IN FieldRegridStore." , rc)
146
+ if (ESMF_logFoundError(rcToCheck= rc,msg= ESMF_LOGERR_PASSTHRU,line= __LINE__,file= __FILE__)) &
147
+ call error_handler(" IN FieldRegridStore." , rc)
148
+
149
+ print * ," - CALL Field_Regrid."
150
+ call ESMF_FieldRegrid(data_field_src, &
151
+ data_field_mdl, &
152
+ routehandle= regrid_data, &
153
+ termorderflag= ESMF_TERMORDER_SRCSEQ, &
154
+ rc= rc)
155
+ if (ESMF_logFoundError(rcToCheck= rc,msg= ESMF_LOGERR_PASSTHRU,line= __LINE__,file= __FILE__)) &
156
+ call error_handler(" IN FieldRegrid." , rc)
169
157
170
- endif
158
+ OUTPUT_LOOP : do tile = 1 , num_tiles
171
159
172
- print * ," - CALL Field_Regrid."
173
- call ESMF_FieldRegrid(data_field_src, &
174
- data_field_mdl2, &
175
- routehandle= regrid_data, &
176
- termorderflag= ESMF_TERMORDER_SRCSEQ, &
177
- rc= rc)
160
+ print * ," - CALL FieldGather FOR MODEL LATITUDE."
161
+ call ESMF_FieldGather(latitude_field_mdl, lat_mdl_one_tile, rootPet= 0 , tile= tile, rc= rc)
178
162
if (ESMF_logFoundError(rcToCheck= rc,msg= ESMF_LOGERR_PASSTHRU,line= __LINE__,file= __FILE__)) &
179
- call error_handler(" IN FieldRegrid." , rc)
180
-
181
- !- ----------------------------------------------------------------------
182
- ! Unmapped points are stored in "unmapped_ptr". The pointer contains
183
- ! "ij" global indices as follows:
184
- !
185
- ! tile 1: 1 thru (itile*jtile)
186
- ! tile n: (n-1)*(itile*jtile) thru n*(itile*jtile)
187
- !
188
- ! This "ij" index is converted to the tile number and i/j index of the
189
- ! field object. This logic assumes the model grid object was
190
- ! created using "GLOBAL" indices.
191
- !
192
- ! Unmapped data points are given the flag value of -9999.9, which
193
- ! will be replaced in routine "search".
194
- !- ----------------------------------------------------------------------
195
-
196
- OUTPUT_LOOP : do tile = 1 , num_tiles
197
-
198
- print * ," - CALL FieldGather FOR MODEL LATITUDE."
199
- call ESMF_FieldGather(latitude_field_mdl, lat_mdl_one_tile, rootPet= 0 , tile= tile, rc= rc)
200
- if (ESMF_logFoundError(rcToCheck= rc,msg= ESMF_LOGERR_PASSTHRU,line= __LINE__,file= __FILE__)) &
201
- call error_handler(" IN FieldGather." , rc)
202
-
203
- print * ," - CALL FieldGather FOR MODEL LONGITUDE."
204
- call ESMF_FieldGather(longitude_field_mdl, lon_mdl_one_tile, rootPet= 0 , tile= tile, rc= rc)
205
- if (ESMF_logFoundError(rcToCheck= rc,msg= ESMF_LOGERR_PASSTHRU,line= __LINE__,file= __FILE__)) &
206
- call error_handler(" IN FieldGather." , rc)
207
-
208
- print * ," - CALL FieldGather FOR MODEL GRID DATA."
209
- call ESMF_FieldGather(data_field_mdl2, data_mdl_one_tile, rootPet= 0 , tile= tile, rc= rc)
210
- if (ESMF_logFoundError(rcToCheck= rc,msg= ESMF_LOGERR_PASSTHRU,line= __LINE__,file= __FILE__)) &
211
- call error_handler(" IN FieldGather." , rc)
212
-
213
- print * ," - CALL FieldGather FOR MODEL GRID LAND MASK."
214
- call ESMF_FieldGather(mask_field_mdl, mask_mdl_one_tile, rootPet= 0 , tile= tile, rc= rc)
215
- if (ESMF_logFoundError(rcToCheck= rc,msg= ESMF_LOGERR_PASSTHRU,line= __LINE__,file= __FILE__)) &
216
- call error_handler(" IN FieldGather." , rc)
217
-
218
- select case (trim (field_names(n)))
219
- case (' substrate_temperature' ,' vegetation_greenness' ,' leaf_area_index' ,' slope_type' ,' soil_type' )
220
- print * ," - CALL FieldGather FOR MODEL GRID VEG TYPE."
221
- call ESMF_FieldGather(vegt_field_mdl, vegt_mdl_one_tile, rootPet= 0 , tile= tile, rc= rc)
222
- if (ESMF_logFoundError(rcToCheck= rc,msg= ESMF_LOGERR_PASSTHRU,line= __LINE__,file= __FILE__)) &
223
- call error_handler(" IN FieldGather." , rc)
224
- end select
225
-
226
- if (localpet == 0 ) then
227
- print * ,' - CALL SEARCH FOR TILE ' ,tile
228
- sum_mdl_one_tile = sum (data_mdl_one_tile, dim= 3 ) ! use unused variable to now.
229
- do j = 1 , j_mdl
230
- do i = 1 , i_mdl
231
-
232
- if (mask_mdl_one_tile(i,j) == 1 .and. sum_mdl_one_tile(i,j) == 0.0 ) then
233
- data_mdl_one_tile(i,j,:) = - 9999.9
234
- endif
235
-
236
- enddo
237
- enddo
163
+ call error_handler(" IN FieldGather." , rc)
238
164
239
- call search2 (data_mdl_one_tile, mask_mdl_one_tile, i_mdl, j_mdl, num_categories, tile, field_names(n))
240
- ! where(mask_mdl_one_tile == 0) data_mdl_one_tile = missing
241
- print * ,' after regrid ' ,data_mdl_one_tile(i_mdl/ 2 ,j_mdl/ 2 ,:)
242
- call output2 (data_mdl_one_tile, lat_mdl_one_tile, lon_mdl_one_tile, i_mdl, j_mdl, num_categories, tile, t, n)
243
- endif
244
-
245
- print * ,' after output ' , localpet
246
- call mpi_barrier(mpi_comm_world, rc)
247
- stop
165
+ print * ," - CALL FieldGather FOR MODEL LONGITUDE."
166
+ call ESMF_FieldGather(longitude_field_mdl, lon_mdl_one_tile, rootPet= 0 , tile= tile, rc= rc)
167
+ if (ESMF_logFoundError(rcToCheck= rc,msg= ESMF_LOGERR_PASSTHRU,line= __LINE__,file= __FILE__)) &
168
+ call error_handler(" IN FieldGather." , rc)
248
169
249
- ! if (field_names(n) == 'vegetation_type') then
250
- ! print*,"- CALL FieldScatter FOR MODEL GRID VEGETATION TYPE."
251
- ! call ESMF_FieldScatter(vegt_field_mdl, data_mdl_one_tile, rootPet=0, tile=tile, rc=rc)
252
- ! if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
253
- ! call error_handler("IN FieldScatter.", rc)
254
- ! endif
170
+ print * ," - CALL FieldGather FOR MODEL GRID DATA."
171
+ call ESMF_FieldGather(data_field_mdl, data_mdl_one_tile, rootPet= 0 , tile= tile, rc= rc)
172
+ if (ESMF_logFoundError(rcToCheck= rc,msg= ESMF_LOGERR_PASSTHRU,line= __LINE__,file= __FILE__)) &
173
+ call error_handler(" IN FieldGather." , rc)
255
174
256
- enddo OUTPUT_LOOP
175
+ print * ," - CALL FieldGather FOR MODEL GRID LAND MASK."
176
+ call ESMF_FieldGather(mask_field_mdl, mask_mdl_one_tile, rootPet= 0 , tile= tile, rc= rc)
177
+ if (ESMF_logFoundError(rcToCheck= rc,msg= ESMF_LOGERR_PASSTHRU,line= __LINE__,file= __FILE__)) &
178
+ call error_handler(" IN FieldGather." , rc)
257
179
258
- if (allocated (vegt_mdl_one_tile)) deallocate (vegt_mdl_one_tile)
180
+ if (localpet == 0 ) then
181
+ print * ,' - CALL SEARCH FOR TILE ' ,tile
182
+ sum_mdl_one_tile = sum (data_mdl_one_tile, dim= 3 ) ! use unused variable to now.
183
+ do j = 1 , j_mdl
184
+ do i = 1 , i_mdl
185
+ if (mask_mdl_one_tile(i,j) == 1 .and. sum_mdl_one_tile(i,j) == 0.0 ) then
186
+ data_mdl_one_tile(i,j,:) = - 9999.9
187
+ endif
188
+ enddo
189
+ enddo
190
+ call search2 (data_mdl_one_tile, mask_mdl_one_tile, i_mdl, j_mdl, num_categories, tile, field_names(1 ))
191
+ print * ,' after regrid ' ,data_mdl_one_tile(i_mdl/ 2 ,j_mdl/ 2 ,:)
192
+ call output2 (data_mdl_one_tile, lat_mdl_one_tile, lon_mdl_one_tile, i_mdl, j_mdl, num_categories, tile)
193
+ endif
259
194
260
- enddo FIELD_LOOP
261
- enddo TIME_LOOP
195
+ enddo OUTPUT_LOOP
262
196
263
197
status= nf90_close(ncid)
264
198
265
- deallocate (data_mdl_one_tile, mask_mdl_one_tile)
266
- deallocate (data_src_global, lat_mdl_one_tile, lon_mdl_one_tile)
199
+ deallocate (data_mdl_one_tile, mask_mdl_one_tile, data_src_global2 )
200
+ deallocate (lat_mdl_one_tile, lon_mdl_one_tile, sum_mdl_one_tile )
267
201
268
202
print * ," - CALL FieldRegridRelease."
269
203
call ESMF_FieldRegridRelease(routehandle= regrid_data, rc= rc)
270
204
271
205
print * ," - CALL FieldDestroy."
272
206
call ESMF_FieldDestroy(data_field_src, rc= rc)
273
207
208
+ print * ,' after output ' , localpet
209
+ call mpi_barrier(mpi_comm_world, rc)
210
+ stop
211
+
274
212
end subroutine interp2
0 commit comments