Skip to content

Commit 0a8f4fa

Browse files
Begin clean up of output routine.
Fixes ufs-community#709.
1 parent 086143c commit 0a8f4fa

File tree

2 files changed

+74
-99
lines changed

2 files changed

+74
-99
lines changed

sorc/sfc_climo_gen.fd/interp2.F90

+9-6
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ subroutine interp2(localpet, method, input_file)
3131
integer :: varid, record
3232
integer :: tile_num, pt_loc_this_tile
3333
integer :: isrctermprocessing
34-
integer :: category
34+
integer :: category, num_categories
3535

3636
integer(esmf_kind_i4), allocatable :: mask_mdl_one_tile(:,:)
3737
integer(esmf_kind_i4), pointer :: unmapped_ptr(:)
@@ -52,13 +52,16 @@ subroutine interp2(localpet, method, input_file)
5252
type(esmf_routehandle) :: regrid_data
5353
type(esmf_polemethod_flag) :: pole
5454

55+
! get this from file.
56+
num_categories = 20
57+
5558
print*,"- CALL FieldCreate FOR SOURCE GRID DATA."
5659
data_field_src = ESMF_FieldCreate(grid_src, &
5760
typekind=ESMF_TYPEKIND_R4, &
5861
indexflag=ESMF_INDEX_GLOBAL, &
5962
staggerloc=ESMF_STAGGERLOC_CENTER, &
6063
ungriddedLBound=(/1/), &
61-
ungriddedUBound=(/20/), &
64+
ungriddedUBound=(/num_categories/), &
6265
name="source data", &
6366
rc=rc)
6467
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
@@ -82,7 +85,7 @@ subroutine interp2(localpet, method, input_file)
8285
indexflag=ESMF_INDEX_GLOBAL, &
8386
staggerloc=ESMF_STAGGERLOC_CENTER, &
8487
ungriddedLBound=(/1/), &
85-
ungriddedUBound=(/20/), &
88+
ungriddedUBound=(/num_categories/), &
8689
name="mdl data", &
8790
rc=rc)
8891
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
@@ -103,7 +106,7 @@ subroutine interp2(localpet, method, input_file)
103106

104107
if (localpet == 0) then
105108
allocate(data_src_global(i_src,j_src))
106-
allocate(data_src_global2(i_src,j_src,20))
109+
allocate(data_src_global2(i_src,j_src,num_categories))
107110
else
108111
allocate(data_src_global(0,0))
109112
allocate(data_src_global2(0,0,0))
@@ -114,7 +117,7 @@ subroutine interp2(localpet, method, input_file)
114117
call netcdf_err(status, "IN ROUTINE INTERP OPENING SOURCE FILE")
115118

116119
if (localpet == 0) then
117-
allocate(data_mdl_one_tile(i_mdl,j_mdl,20))
120+
allocate(data_mdl_one_tile(i_mdl,j_mdl,num_categories))
118121
allocate(mask_mdl_one_tile(i_mdl,j_mdl))
119122
allocate(lat_mdl_one_tile(i_mdl,j_mdl))
120123
allocate(lon_mdl_one_tile(i_mdl,j_mdl))
@@ -282,7 +285,7 @@ subroutine interp2(localpet, method, input_file)
282285
call search2 (data_mdl_one_tile, mask_mdl_one_tile, i_mdl, j_mdl, tile, field_names(n))
283286
! where(mask_mdl_one_tile == 0) data_mdl_one_tile = missing
284287
print*,'after regrid ',data_mdl_one_tile(i_mdl/2,j_mdl/2,:)
285-
call output2 (data_mdl_one_tile, lat_mdl_one_tile, lon_mdl_one_tile, i_mdl, j_mdl, tile, record, t, n)
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)
286289
endif
287290

288291
print*,'after output ', localpet

sorc/sfc_climo_gen.fd/output2.f90

+65-93
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
!! @param[in] time Time period to be output.
1717
!! @author George Gayno @date 2018
1818
subroutine output2(data_one_tile, lat_one_tile, lon_one_tile, i_mdl, j_mdl, &
19-
tile, record, time, field_idx)
19+
num_categories, tile, record, time, field_idx)
2020

2121
use mpi
2222
use esmf
@@ -29,46 +29,29 @@ subroutine output2(data_one_tile, lat_one_tile, lon_one_tile, i_mdl, j_mdl, &
2929

3030
implicit none
3131

32-
integer, intent(in) :: i_mdl, j_mdl, tile
32+
integer, intent(in) :: i_mdl, j_mdl, tile, num_categories
3333
integer, intent(in) :: record, time, field_idx
3434

35-
real(esmf_kind_r4), intent(in) :: data_one_tile(i_mdl,j_mdl,20)
35+
real(esmf_kind_r4), intent(in) :: data_one_tile(i_mdl,j_mdl,num_categories)
3636
real(esmf_kind_r4) :: lat_one_tile(i_mdl,j_mdl)
3737
real(esmf_kind_r4), intent(in) :: lon_one_tile(i_mdl,j_mdl)
3838

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

42-
integer :: initialsiz, fsize, error, j
42+
integer :: error, j
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+
5054
select case (field_names(field_idx))
51-
case ('substrate_temperature')
52-
out_file = "./substrate_temperature." // grid_tiles(tile) // ".nc"
53-
out_file_with_halo = "./substrate_temperature." // grid_tiles(tile) // ".halo.nc"
54-
case ('vegetation_greenness')
55-
out_file = "./vegetation_greenness." // grid_tiles(tile) // ".nc"
56-
out_file_with_halo = "./vegetation_greenness." // grid_tiles(tile) // ".halo.nc"
57-
case ('maximum_snow_albedo')
58-
out_file = "./maximum_snow_albedo." // grid_tiles(tile) // ".nc"
59-
out_file_with_halo = "./maximum_snow_albedo." // grid_tiles(tile) // ".halo.nc"
60-
case ('leaf_area_index')
61-
out_file = "./leaf_area_index." // grid_tiles(tile) // ".nc"
62-
out_file_with_halo = "./leaf_area_index." // grid_tiles(tile) // ".halo.nc"
63-
case ('visible_black_sky_albedo', 'visible_white_sky_albedo', 'near_IR_black_sky_albedo', 'near_IR_white_sky_albedo')
64-
out_file = "./snowfree_albedo." // grid_tiles(tile) // ".nc"
65-
out_file_with_halo = "./snowfree_albedo." // grid_tiles(tile) // ".halo.nc"
66-
case ('facsf')
67-
out_file = "./facsf." // grid_tiles(tile) // ".nc"
68-
out_file_with_halo = "./facsf." // grid_tiles(tile) // ".halo.nc"
69-
case ('slope_type')
70-
out_file = "./slope_type." // grid_tiles(tile) // ".nc"
71-
out_file_with_halo = "./slope_type." // grid_tiles(tile) // ".halo.nc"
7255
case ('soil_type')
7356
out_file = "./soil_type." // grid_tiles(tile) // ".nc"
7457
out_file_with_halo = "./soil_type." // grid_tiles(tile) // ".halo.nc"
@@ -101,85 +84,77 @@ subroutine output2(data_one_tile, lat_one_tile, lon_one_tile, i_mdl, j_mdl, &
10184
i_out = i_end - i_start + 1
10285
j_out = j_end - j_start + 1
10386

104-
if (record == 1) then
105-
106-
initialsiz = 0
107-
fsize = 65536
108-
error = nf90_create(out_file, IOR(NF90_NETCDF4,NF90_CLASSIC_MODEL), &
109-
ncid(tile), initialsize=initialsiz, chunksize=fsize)
110-
call netcdf_err(error, 'ERROR IN NF90_CREATE' )
111-
error = nf90_def_dim(ncid(tile), 'nx', i_out, dim_x)
112-
call netcdf_err(error, 'DEFINING NX DIMENSION' )
113-
error = nf90_def_dim(ncid(tile), 'ny', j_out, dim_y)
114-
call netcdf_err(error, 'DEFINING NY DIMENSION' )
115-
error = nf90_def_dim(ncid(tile), 'nz', 20, dim_z)
116-
call netcdf_err(error, 'DEFINING NZ DIMENSION' )
117-
error = nf90_def_dim(ncid(tile), 'time', num_time_recs, dim_time)
118-
call netcdf_err(error, 'DEFINING TIME DIMENSION' )
119-
error = nf90_def_var(ncid(tile), 'time', NF90_FLOAT, dim_time, id_times)
120-
call netcdf_err(error, 'DEFINING TIME VARIABLE' )
121-
error = nf90_put_att(ncid(tile), id_times, "units", "days since 2015-1-1")
122-
call netcdf_err(error, 'DEFINING TIME ATTRIBUTE' )
123-
if (len_trim(source) > 0) then
124-
error = nf90_put_att(ncid(tile), nf90_global, 'source', source)
125-
call netcdf_err(error, 'DEFINING GLOBAL SOURCE ATTRIBUTE' )
126-
endif
127-
128-
error = nf90_def_var(ncid(tile), 'geolat', NF90_FLOAT, (/dim_x,dim_y/), id_lat)
129-
call netcdf_err(error, 'DEFINING GEOLAT FIELD' )
130-
error = nf90_put_att(ncid(tile), id_lat, "long_name", "Latitude")
131-
call netcdf_err(error, 'DEFINING GEOLAT NAME ATTRIBUTE' )
132-
error = nf90_put_att(ncid(tile), id_lat, "units", "degrees_north")
133-
call netcdf_err(error, 'DEFINING GEOLAT UNIT ATTRIBUTE' )
134-
error = nf90_def_var(ncid(tile), 'geolon', NF90_FLOAT, (/dim_x,dim_y/), id_lon)
135-
call netcdf_err(error, 'DEFINING GEOLON FIELD' )
136-
error = nf90_put_att(ncid(tile), id_lon, "long_name", "Longitude")
137-
call netcdf_err(error, 'DEFINING GEOLON NAME ATTRIBUTE' )
138-
error = nf90_put_att(ncid(tile), id_lon, "units", "degrees_east")
139-
call netcdf_err(error, 'DEFINING GEOLON UNIT ATTRIBUTE' )
140-
141-
do j = 1, num_fields
142-
error = nf90_def_var(ncid(tile), trim(field_names(j)), NF90_FLOAT, (/dim_x,dim_y,dim_z,dim_time/), id_data)
143-
call netcdf_err(error, 'DEFINING FIELD' )
144-
error = nf90_put_att(ncid(tile), id_data, "missing_value", missing)
145-
call netcdf_err(error, 'DEFINING FIELD ATTRIBUTE' )
146-
error = nf90_put_att(ncid(tile), id_data, "coordinates", "geolon geolat")
147-
call netcdf_err(error, 'DEFINING COORD ATTRIBUTE' )
148-
error = nf90_def_var(ncid(tile), 'sum', NF90_FLOAT, (/dim_x,dim_y,dim_time/), id_sum)
149-
call netcdf_err(error, 'DEFINING FIELD' )
150-
enddo
151-
152-
error = nf90_enddef(ncid(tile), header_buffer_val,4,0,4)
153-
call netcdf_err(error, 'IN NF90_ENDDEF' )
154-
155-
error = nf90_put_var( ncid(tile), id_times, day_of_rec)
156-
call netcdf_err(error, 'WRITING TIME FIELD' )
87+
error = nf90_create(out_file, NF90_NETCDF4, ncid(tile))
88+
call netcdf_err(error, 'ERROR IN NF90_CREATE' )
89+
error = nf90_def_dim(ncid(tile), 'nx', i_out, dim_x)
90+
call netcdf_err(error, 'DEFINING NX DIMENSION' )
91+
error = nf90_def_dim(ncid(tile), 'ny', j_out, dim_y)
92+
call netcdf_err(error, 'DEFINING NY DIMENSION' )
93+
error = nf90_def_dim(ncid(tile), 'num_categories', num_categories, dim_z)
94+
call netcdf_err(error, 'DEFINING NZ DIMENSION' )
95+
error = nf90_def_dim(ncid(tile), 'time', num_time_recs, dim_time)
96+
call netcdf_err(error, 'DEFINING TIME DIMENSION' )
97+
error = nf90_def_var(ncid(tile), 'time', NF90_FLOAT, dim_time, id_times)
98+
call netcdf_err(error, 'DEFINING TIME VARIABLE' )
99+
error = nf90_put_att(ncid(tile), id_times, "units", "days since 2015-1-1")
100+
call netcdf_err(error, 'DEFINING TIME ATTRIBUTE' )
101+
if (len_trim(source) > 0) then
102+
error = nf90_put_att(ncid(tile), nf90_global, 'source', source)
103+
call netcdf_err(error, 'DEFINING GLOBAL SOURCE ATTRIBUTE' )
104+
endif
157105

158-
error = nf90_put_var( ncid(tile), id_lat, lat_one_tile(i_start:i_end,j_start:j_end), &
106+
error = nf90_def_var(ncid(tile), 'geolat', NF90_FLOAT, (/dim_x,dim_y/), id_lat)
107+
call netcdf_err(error, 'DEFINING GEOLAT FIELD' )
108+
error = nf90_put_att(ncid(tile), id_lat, "long_name", "Latitude")
109+
call netcdf_err(error, 'DEFINING GEOLAT NAME ATTRIBUTE' )
110+
error = nf90_put_att(ncid(tile), id_lat, "units", "degrees_north")
111+
call netcdf_err(error, 'DEFINING GEOLAT UNIT ATTRIBUTE' )
112+
error = nf90_def_var(ncid(tile), 'geolon', NF90_FLOAT, (/dim_x,dim_y/), id_lon)
113+
call netcdf_err(error, 'DEFINING GEOLON FIELD' )
114+
error = nf90_put_att(ncid(tile), id_lon, "long_name", "Longitude")
115+
call netcdf_err(error, 'DEFINING GEOLON NAME ATTRIBUTE' )
116+
error = nf90_put_att(ncid(tile), id_lon, "units", "degrees_east")
117+
call netcdf_err(error, 'DEFINING GEOLON UNIT ATTRIBUTE' )
118+
119+
error = nf90_def_var(ncid(tile), trim(field_names(1)), NF90_FLOAT, (/dim_x,dim_y,dim_z,dim_time/), id_data)
120+
call netcdf_err(error, 'DEFINING FIELD' )
121+
error = nf90_put_att(ncid(tile), id_data, "units", "percent coverage each category")
122+
call netcdf_err(error, 'DEFINING FIELD ATTRIBUTE' )
123+
error = nf90_put_att(ncid(tile), id_data, "missing_value", missing)
124+
call netcdf_err(error, 'DEFINING FIELD ATTRIBUTE' )
125+
error = nf90_put_att(ncid(tile), id_data, "coordinates", "geolon geolat")
126+
call netcdf_err(error, 'DEFINING COORD ATTRIBUTE' )
127+
error = nf90_def_var(ncid(tile), 'sum', NF90_FLOAT, (/dim_x,dim_y,dim_time/), id_sum)
128+
call netcdf_err(error, 'DEFINING FIELD' )
129+
130+
error = nf90_enddef(ncid(tile), header_buffer_val,4,0,4)
131+
call netcdf_err(error, 'IN NF90_ENDDEF' )
132+
133+
error = nf90_put_var( ncid(tile), id_times, day_of_rec)
134+
call netcdf_err(error, 'WRITING TIME FIELD' )
135+
136+
error = nf90_put_var( ncid(tile), id_lat, lat_one_tile(i_start:i_end,j_start:j_end), &
159137
start=(/1,1/), count=(/i_out,j_out/))
160-
call netcdf_err(error, 'IN NF90_PUT_VAR FOR GEOLAT' )
138+
call netcdf_err(error, 'IN NF90_PUT_VAR FOR GEOLAT' )
161139

162-
error = nf90_put_var( ncid(tile), id_lon, lon_one_tile(i_start:i_end,j_start:j_end), &
140+
error = nf90_put_var( ncid(tile), id_lon, lon_one_tile(i_start:i_end,j_start:j_end), &
163141
start=(/1,1/), count=(/i_out,j_out/))
164-
call netcdf_err(error, 'IN NF90_PUT_VAR FOR GEOLON' )
165-
166-
endif
142+
call netcdf_err(error, 'IN NF90_PUT_VAR FOR GEOLON' )
167143

168144
print*,'- WRITE DATA FOR RECORD: ',record
169145
error = nf90_inq_varid( ncid(tile), field_names(field_idx), id_data)
170146
call netcdf_err(error, 'IN NF90_INQ_VARID' )
171147
error = nf90_put_var( ncid(tile), id_data, data_one_tile(i_start:i_end,j_start:j_end,:), &
172-
start=(/1,1,1,time/), count=(/i_out,j_out,20,1/))
148+
start=(/1,1,1,time/), count=(/i_out,j_out,num_categories,1/))
173149
call netcdf_err(error, 'IN NF90_PUT_VAR' )
174150

175-
! reuse lat
151+
! Temporary output of sum of %.
152+
! reuse latitude array.
176153
lat_one_tile = sum(data_one_tile, dim=3)
177154
error = nf90_put_var( ncid(tile), id_sum, lat_one_tile(i_start:i_end,j_start:j_end), &
178155
start=(/1,1,time/), count=(/i_out,j_out,1/))
179156

180-
if (record == num_records) then
181-
error = nf90_close(ncid(tile))
182-
endif
157+
error = nf90_close(ncid(tile))
183158

184159
print*,'after write'
185160
stop
@@ -194,10 +169,7 @@ subroutine output2(data_one_tile, lat_one_tile, lon_one_tile, i_mdl, j_mdl, &
194169

195170
if (record == 1) then
196171

197-
initialsiz = 0
198-
fsize = 65536
199-
error = nf90_create(out_file_with_halo, IOR(NF90_NETCDF4,NF90_CLASSIC_MODEL), &
200-
ncid_with_halo, initialsize=initialsiz, chunksize=fsize)
172+
error = nf90_create(out_file_with_halo, NF90_NETCDF4, ncid_with_halo)
201173
call netcdf_err(error, 'IN NF90_CREATE' )
202174
error = nf90_def_dim(ncid_with_halo, 'nx', i_mdl, dim_x)
203175
call netcdf_err(error, 'DEFINING NX DIMENSION' )

0 commit comments

Comments
 (0)