Skip to content

Commit b5e90be

Browse files
More cleanup to output routine.
Fixes ufs-community#709.
1 parent 0a8f4fa commit b5e90be

File tree

2 files changed

+73
-79
lines changed

2 files changed

+73
-79
lines changed

sorc/sfc_climo_gen.fd/interp2.F90

+6-3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ subroutine interp2(localpet, method, input_file)
4343
real(esmf_kind_r4), allocatable :: data_mdl_one_tile(:,:,:)
4444
real(esmf_kind_r4), allocatable :: vegt_mdl_one_tile(:,:)
4545
real(esmf_kind_r4), allocatable :: lat_mdl_one_tile(:,:)
46+
real(esmf_kind_r4), allocatable :: sum_mdl_one_tile(:,:)
4647
real(esmf_kind_r4), allocatable :: lon_mdl_one_tile(:,:)
4748

4849
!type(esmf_regridmethod_flag),intent(in) :: method
@@ -120,11 +121,13 @@ subroutine interp2(localpet, method, input_file)
120121
allocate(data_mdl_one_tile(i_mdl,j_mdl,num_categories))
121122
allocate(mask_mdl_one_tile(i_mdl,j_mdl))
122123
allocate(lat_mdl_one_tile(i_mdl,j_mdl))
124+
allocate(sum_mdl_one_tile(i_mdl,j_mdl))
123125
allocate(lon_mdl_one_tile(i_mdl,j_mdl))
124126
else
125127
allocate(data_mdl_one_tile(0,0,0))
126128
allocate(mask_mdl_one_tile(0,0))
127129
allocate(lat_mdl_one_tile(0,0))
130+
allocate(sum_mdl_one_tile(0,0))
128131
allocate(lon_mdl_one_tile(0,0))
129132
endif
130133

@@ -271,11 +274,11 @@ subroutine interp2(localpet, method, input_file)
271274

272275
if (localpet == 0) then
273276
print*,'- CALL SEARCH FOR TILE ',tile
274-
lat_mdl_one_tile = sum(data_mdl_one_tile, dim=3) ! use unused variable to now.
277+
sum_mdl_one_tile = sum(data_mdl_one_tile, dim=3) ! use unused variable to now.
275278
do j = 1, j_mdl
276279
do i = 1, i_mdl
277280

278-
if (mask_mdl_one_tile(i,j) == 1 .and. lat_mdl_one_tile(i,j) == 0.0) then
281+
if (mask_mdl_one_tile(i,j) == 1 .and. sum_mdl_one_tile(i,j) == 0.0) then
279282
data_mdl_one_tile(i,j,:) = -9999.9
280283
endif
281284

@@ -285,7 +288,7 @@ subroutine interp2(localpet, method, input_file)
285288
call search2 (data_mdl_one_tile, mask_mdl_one_tile, i_mdl, j_mdl, tile, field_names(n))
286289
! where(mask_mdl_one_tile == 0) data_mdl_one_tile = missing
287290
print*,'after regrid ',data_mdl_one_tile(i_mdl/2,j_mdl/2,:)
288-
call output2 (data_mdl_one_tile, lat_mdl_one_tile, lon_mdl_one_tile, i_mdl, j_mdl, num_categories, tile, record, t, n)
291+
call output2 (data_mdl_one_tile, lat_mdl_one_tile, lon_mdl_one_tile, i_mdl, j_mdl, num_categories, tile, t, n)
289292
endif
290293

291294
print*,'after output ', localpet

sorc/sfc_climo_gen.fd/output2.f90

+67-76
Original file line numberDiff line numberDiff line change
@@ -11,46 +11,42 @@
1111
!! @param[in] field_idx Index of field within field name array.
1212
!! @param[in] i_mdl i dimensions of tile.
1313
!! @param[in] j_mdl j dimensions of tile.
14-
!! @param[in] record Record number to be output.
1514
!! @param[in] tile Tile number.
1615
!! @param[in] time Time period to be output.
1716
!! @author George Gayno @date 2018
1817
subroutine output2(data_one_tile, lat_one_tile, lon_one_tile, i_mdl, j_mdl, &
19-
num_categories, tile, record, time, field_idx)
18+
num_categories, tile, time, field_idx)
2019

2120
use mpi
2221
use esmf
2322
use netcdf
2423
use utils
25-
use source_grid, only : field_names, source, num_fields, &
26-
num_time_recs, num_records, day_of_rec
24+
use source_grid, only : field_names, source, &
25+
num_time_recs, day_of_rec
2726
use model_grid, only : missing, grid_tiles
2827
use program_setup, only : halo
2928

3029
implicit none
3130

3231
integer, intent(in) :: i_mdl, j_mdl, tile, num_categories
33-
integer, intent(in) :: record, time, field_idx
32+
integer, intent(in) :: time, field_idx
3433

3534
real(esmf_kind_r4), intent(in) :: data_one_tile(i_mdl,j_mdl,num_categories)
3635
real(esmf_kind_r4) :: lat_one_tile(i_mdl,j_mdl)
3736
real(esmf_kind_r4), intent(in) :: lon_one_tile(i_mdl,j_mdl)
37+
real(esmf_kind_r4) :: sum_one_tile(i_mdl,j_mdl)
3838

3939
character(len=200) :: out_file
4040
character(len=200) :: out_file_with_halo
4141

42-
integer :: error, j
42+
integer :: error
4343
integer :: dim_x, dim_y, dim_z, id_data
4444
integer :: dim_time, id_times, ierr
4545
integer :: header_buffer_val = 16384
4646
integer :: i_out, j_out, id_lat, id_lon, id_sum
4747
integer :: i_start, i_end, j_start, j_end
4848
integer, save :: ncid(6), ncid_with_halo
4949

50-
print*,'num_time_recs/num_records/day_of_reo ',num_time_recs,num_records,day_of_rec
51-
print*,'record time ',record, time
52-
print*,'num fields ', num_fields
53-
5450
select case (field_names(field_idx))
5551
case ('soil_type')
5652
out_file = "./soil_type." // grid_tiles(tile) // ".nc"
@@ -141,24 +137,20 @@ subroutine output2(data_one_tile, lat_one_tile, lon_one_tile, i_mdl, j_mdl, &
141137
start=(/1,1/), count=(/i_out,j_out/))
142138
call netcdf_err(error, 'IN NF90_PUT_VAR FOR GEOLON' )
143139

144-
print*,'- WRITE DATA FOR RECORD: ',record
140+
print*,'- WRITE DATA '
145141
error = nf90_inq_varid( ncid(tile), field_names(field_idx), id_data)
146142
call netcdf_err(error, 'IN NF90_INQ_VARID' )
147143
error = nf90_put_var( ncid(tile), id_data, data_one_tile(i_start:i_end,j_start:j_end,:), &
148144
start=(/1,1,1,time/), count=(/i_out,j_out,num_categories,1/))
149145
call netcdf_err(error, 'IN NF90_PUT_VAR' )
150146

151147
! Temporary output of sum of %.
152-
! reuse latitude array.
153-
lat_one_tile = sum(data_one_tile, dim=3)
154-
error = nf90_put_var( ncid(tile), id_sum, lat_one_tile(i_start:i_end,j_start:j_end), &
148+
sum_one_tile = sum(data_one_tile, dim=3)
149+
error = nf90_put_var( ncid(tile), id_sum, sum_one_tile(i_start:i_end,j_start:j_end), &
155150
start=(/1,1,time/), count=(/i_out,j_out,1/))
156151

157152
error = nf90_close(ncid(tile))
158153

159-
print*,'after write'
160-
stop
161-
162154
!----------------------------------------------------------------------
163155
! For regional nests, also output files including the halo
164156
!----------------------------------------------------------------------
@@ -167,74 +159,73 @@ subroutine output2(data_one_tile, lat_one_tile, lon_one_tile, i_mdl, j_mdl, &
167159

168160
print*,"- WRITE OUT FILES THAT INCLUDE HALO REGION."
169161

170-
if (record == 1) then
171-
172-
error = nf90_create(out_file_with_halo, NF90_NETCDF4, ncid_with_halo)
173-
call netcdf_err(error, 'IN NF90_CREATE' )
174-
error = nf90_def_dim(ncid_with_halo, 'nx', i_mdl, dim_x)
175-
call netcdf_err(error, 'DEFINING NX DIMENSION' )
176-
error = nf90_def_dim(ncid_with_halo, 'ny', j_mdl, dim_y)
177-
call netcdf_err(error, 'DEFINING NY DIMENSION' )
178-
error = nf90_def_dim(ncid_with_halo, 'time', num_time_recs, dim_time)
179-
call netcdf_err(error, 'DEFINING TIME DIMENSION' )
180-
error = nf90_def_var(ncid_with_halo, 'time', NF90_FLOAT, dim_time, id_times)
181-
call netcdf_err(error, 'DEFINING TIME VARIABLE' )
182-
error = nf90_put_att(ncid_with_halo, id_times, "units", "days since 2015-1-1")
183-
call netcdf_err(error, 'DEFINING TIME ATTRIBUTE' )
184-
if (len_trim(source) > 0) then
185-
error = nf90_put_att(ncid_with_halo, nf90_global, 'source', source)
186-
call netcdf_err(error, 'DEFINING GLOBAL SOURCE ATTRIBUTE' )
187-
endif
188-
189-
error = nf90_def_var(ncid_with_halo, 'geolat', NF90_FLOAT, (/dim_x,dim_y/), id_lat)
190-
call netcdf_err(error, 'DEFINING GEOLAT FIELD' )
191-
error = nf90_put_att(ncid_with_halo, id_lat, "long_name", "Latitude")
192-
call netcdf_err(error, 'DEFINING GEOLAT NAME ATTRIBUTE' )
193-
error = nf90_put_att(ncid_with_halo, id_lat, "units", "degrees_north")
194-
call netcdf_err(error, 'DEFINING GEOLAT UNIT ATTRIBUTE' )
195-
error = nf90_def_var(ncid_with_halo, 'geolon', NF90_FLOAT, (/dim_x,dim_y/), id_lon)
196-
call netcdf_err(error, 'DEFINING GEOLON FIELD' )
197-
error = nf90_put_att(ncid_with_halo, id_lon, "long_name", "Longitude")
198-
call netcdf_err(error, 'DEFINING GEOLON NAME ATTRIBUTE' )
199-
error = nf90_put_att(ncid_with_halo, id_lon, "units", "degrees_east")
200-
call netcdf_err(error, 'DEFINING GEOLON UNIT ATTRIBUTE' )
201-
202-
do j = 1, num_fields
203-
error = nf90_def_var(ncid_with_halo, field_names(j), NF90_FLOAT, (/dim_x,dim_y,dim_time/), id_data)
204-
call netcdf_err(error, 'DEFINING FIELD VARIABLE' )
205-
error = nf90_put_att(ncid_with_halo, id_data, "missing_value", missing)
206-
call netcdf_err(error, 'DEFINING FIELD ATTRIBUTE' )
207-
error = nf90_put_att(ncid_with_halo, id_data, "coordinates", "geolon geolat")
208-
call netcdf_err(error, 'DEFINING COORD ATTRIBUTE' )
209-
enddo
210-
211-
error = nf90_enddef(ncid_with_halo, header_buffer_val,4,0,4)
212-
call netcdf_err(error, 'WRITING HEADER ENDDEF' )
213-
214-
error = nf90_put_var(ncid_with_halo, id_times, day_of_rec)
215-
call netcdf_err(error, 'WRITING TIME VARIABLE' )
216-
217-
error = nf90_put_var( ncid_with_halo, id_lat, lat_one_tile, &
218-
start=(/1,1/), count=(/i_mdl,j_mdl/))
219-
call netcdf_err(error, 'IN NF90_PUT_VAR FOR GEOLAT' )
162+
error = nf90_create(out_file_with_halo, NF90_NETCDF4, ncid_with_halo)
163+
call netcdf_err(error, 'IN NF90_CREATE' )
164+
error = nf90_def_dim(ncid_with_halo, 'nx', i_mdl, dim_x)
165+
call netcdf_err(error, 'DEFINING NX DIMENSION' )
166+
error = nf90_def_dim(ncid_with_halo, 'ny', j_mdl, dim_y)
167+
call netcdf_err(error, 'DEFINING NY DIMENSION' )
168+
error = nf90_def_dim(ncid(tile), 'num_categories', num_categories, dim_z)
169+
call netcdf_err(error, 'DEFINING NZ DIMENSION' )
170+
error = nf90_def_dim(ncid_with_halo, 'time', num_time_recs, dim_time)
171+
call netcdf_err(error, 'DEFINING TIME DIMENSION' )
172+
error = nf90_def_var(ncid_with_halo, 'time', NF90_FLOAT, dim_time, id_times)
173+
call netcdf_err(error, 'DEFINING TIME VARIABLE' )
174+
error = nf90_put_att(ncid_with_halo, id_times, "units", "days since 2015-1-1")
175+
call netcdf_err(error, 'DEFINING TIME ATTRIBUTE' )
176+
if (len_trim(source) > 0) then
177+
error = nf90_put_att(ncid_with_halo, nf90_global, 'source', source)
178+
call netcdf_err(error, 'DEFINING GLOBAL SOURCE ATTRIBUTE' )
179+
endif
220180

221-
error = nf90_put_var( ncid_with_halo, id_lon, lon_one_tile, &
222-
start=(/1,1/), count=(/i_mdl,j_mdl/))
223-
call netcdf_err(error, 'IN NF90_PUT_VAR FOR GEOLON' )
181+
error = nf90_def_var(ncid_with_halo, 'geolat', NF90_FLOAT, (/dim_x,dim_y/), id_lat)
182+
call netcdf_err(error, 'DEFINING GEOLAT FIELD' )
183+
error = nf90_put_att(ncid_with_halo, id_lat, "long_name", "Latitude")
184+
call netcdf_err(error, 'DEFINING GEOLAT NAME ATTRIBUTE' )
185+
error = nf90_put_att(ncid_with_halo, id_lat, "units", "degrees_north")
186+
call netcdf_err(error, 'DEFINING GEOLAT UNIT ATTRIBUTE' )
187+
error = nf90_def_var(ncid_with_halo, 'geolon', NF90_FLOAT, (/dim_x,dim_y/), id_lon)
188+
call netcdf_err(error, 'DEFINING GEOLON FIELD' )
189+
error = nf90_put_att(ncid_with_halo, id_lon, "long_name", "Longitude")
190+
call netcdf_err(error, 'DEFINING GEOLON NAME ATTRIBUTE' )
191+
error = nf90_put_att(ncid_with_halo, id_lon, "units", "degrees_east")
192+
call netcdf_err(error, 'DEFINING GEOLON UNIT ATTRIBUTE' )
224193

225-
endif
194+
error = nf90_def_var(ncid_with_halo, field_names(1), NF90_FLOAT, (/dim_x,dim_y,dim_z,dim_time/), id_data)
195+
call netcdf_err(error, 'DEFINING FIELD VARIABLE' )
196+
error = nf90_put_att(ncid_with_halo, id_data, "units", "percent coverage each category")
197+
call netcdf_err(error, 'DEFINING FIELD ATTRIBUTE' )
198+
error = nf90_put_att(ncid_with_halo, id_data, "missing_value", missing)
199+
call netcdf_err(error, 'DEFINING FIELD ATTRIBUTE' )
200+
error = nf90_put_att(ncid_with_halo, id_data, "coordinates", "geolon geolat")
201+
call netcdf_err(error, 'DEFINING COORD ATTRIBUTE' )
202+
203+
error = nf90_enddef(ncid_with_halo, header_buffer_val,4,0,4)
204+
call netcdf_err(error, 'WRITING HEADER ENDDEF' )
205+
206+
error = nf90_put_var(ncid_with_halo, id_times, day_of_rec)
207+
call netcdf_err(error, 'WRITING TIME VARIABLE' )
226208

227-
print*,'- WRITE DATA FOR RECORD: ',record
209+
error = nf90_put_var( ncid_with_halo, id_lat, lat_one_tile, &
210+
start=(/1,1/), count=(/i_mdl,j_mdl/))
211+
call netcdf_err(error, 'IN NF90_PUT_VAR FOR GEOLAT' )
212+
213+
error = nf90_put_var( ncid_with_halo, id_lon, lon_one_tile, &
214+
start=(/1,1/), count=(/i_mdl,j_mdl/))
215+
call netcdf_err(error, 'IN NF90_PUT_VAR FOR GEOLON' )
216+
217+
print*,'- WRITE DATA'
228218
error = nf90_inq_varid(ncid_with_halo, field_names(field_idx), id_data)
229219
call netcdf_err(error, 'IN NF90_INQ_VARID' )
230220

231221
error = nf90_put_var(ncid_with_halo, id_data, data_one_tile, &
232-
start=(/1,1,time/), count=(/i_mdl,j_mdl,1/))
222+
start=(/1,1,1,time/), count=(/i_mdl,j_mdl,num_categories,1/))
233223
call netcdf_err(error, 'IN NF90_PUT_VAR' )
234224

235-
if (record == num_records) then
236-
error = nf90_close(ncid_with_halo)
237-
endif
225+
error = nf90_close(ncid_with_halo)
226+
227+
call mpi_barrier(mpi_comm_world, error)
228+
stop
238229

239230
return
240231

0 commit comments

Comments
 (0)