Skip to content

Commit 86f97df

Browse files
Simplify output2 by adding subroutine to do file write.
Fixes ufs-community#709.
1 parent 33cefde commit 86f97df

File tree

1 file changed

+62
-123
lines changed

1 file changed

+62
-123
lines changed

sorc/sfc_climo_gen.fd/output2.f90

+62-123
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@ subroutine output2(data_one_tile, dom_cat_one_tile, lat_one_tile, lon_one_tile,
1818

1919
use mpi
2020
use esmf
21-
use netcdf
22-
use utils
23-
use source_grid, only : field_names, source, &
24-
num_time_recs, day_of_rec
25-
use model_grid, only : missing, grid_tiles
21+
use source_grid, only : field_names, &
22+
num_time_recs
23+
use model_grid, only : grid_tiles
2624
use program_setup, only : halo
2725

2826
implicit none
@@ -33,20 +31,14 @@ subroutine output2(data_one_tile, dom_cat_one_tile, lat_one_tile, lon_one_tile,
3331
real(esmf_kind_r4), intent(in) :: dom_cat_one_tile(i_mdl,j_mdl)
3432
real(esmf_kind_r4) :: lat_one_tile(i_mdl,j_mdl)
3533
real(esmf_kind_r4), intent(in) :: lon_one_tile(i_mdl,j_mdl)
36-
real(esmf_kind_r4) :: sum_one_tile(i_mdl,j_mdl)
3734

38-
character(len=200) :: field_names_pct
3935
character(len=200) :: out_file
4036
character(len=200) :: out_file_with_halo
4137

42-
integer :: error
4338
integer :: field_idx
44-
integer :: dim_x, dim_y, dim_z, id_data_pct, id_data_dom_cat
45-
integer :: dim_time, id_times, ierr
46-
integer :: header_buffer_val = 16384
47-
integer :: i_out, j_out, id_lat, id_lon, id_sum
39+
integer :: ierr
40+
integer :: i_out, j_out
4841
integer :: i_start, i_end, j_start, j_end
49-
integer :: ncid, ncid_with_halo
5042

5143
field_idx = 1
5244

@@ -68,32 +60,72 @@ subroutine output2(data_one_tile, dom_cat_one_tile, lat_one_tile, lon_one_tile,
6860
!----------------------------------------------------------------------
6961

7062
if (halo > 0) then
71-
print*,"- WILL REMOVE HALO REGION OF ", halo, " ROWS/COLS."
63+
print*,"- WILL WRITE WITHOUT HALO REGION OF ", halo, " ROWS/COLS."
7264
i_start = 1 + halo
7365
i_end = i_mdl - halo
7466
j_start = 1 + halo
7567
j_end = j_mdl - halo
68+
i_out = i_end - i_start + 1
69+
j_out = j_end - j_start + 1
70+
call writeit(out_file, i_out, j_out, num_categories, num_time_recs, &
71+
lat_one_tile(i_start:i_end,j_start:j_end), &
72+
lon_one_tile(i_start:i_end,j_start:j_end), &
73+
data_one_tile(i_start:i_end,j_start:j_end,:), &
74+
dom_cat_one_tile(i_start:i_end,j_start:j_end) )
75+
print*,"- WILL WRITE FULL DOMAIN INCLUDING HALO."
76+
call writeit(out_file_with_halo, i_mdl, j_mdl, num_categories, num_time_recs, &
77+
lat_one_tile, lon_one_tile, data_one_tile, dom_cat_one_tile)
7678
else
77-
i_start = 1
78-
i_end = i_mdl
79-
j_start = 1
80-
j_end = j_mdl
79+
print*,"- WILL WRITE DATA."
80+
call writeit(out_file, i_mdl, j_mdl, num_categories, num_time_recs, &
81+
lat_one_tile, lon_one_tile, data_one_tile, dom_cat_one_tile)
8182
endif
8283

83-
i_out = i_end - i_start + 1
84-
j_out = j_end - j_start + 1
84+
return
85+
86+
end subroutine output2
87+
88+
subroutine writeit(out_file, iout, jout, num_categories, num_time_recs, &
89+
latitude, longitude, data_pct, dominate_cat)
90+
91+
use esmf
92+
use netcdf
93+
use utils
94+
use source_grid, only : day_of_rec, source, field_names
95+
use model_grid, only : missing
96+
97+
implicit none
98+
99+
character(len=*), intent(in) :: out_file
100+
101+
integer, intent(in) :: iout, jout, num_categories, num_time_recs
102+
103+
real(esmf_kind_r4), intent(in) :: latitude(iout,jout)
104+
real(esmf_kind_r4), intent(in) :: longitude(iout,jout)
105+
real(esmf_kind_r4), intent(in) :: data_pct(iout,jout,num_categories)
106+
real(esmf_kind_r4), intent(in) :: dominate_cat(iout,jout)
107+
108+
character(len=200) :: field_names_pct
109+
integer :: header_buffer_val = 16384
110+
integer :: ncid, dim_x, dim_y, dim_z, dim_time
111+
integer :: id_times, id_lat, id_lon, id_data_pct
112+
integer :: id_data_dom_cat, id_sum
113+
integer :: error
85114

115+
real :: sum_all(iout,jout)
116+
117+
print*,"- OPEN AND WRITE: ",trim(out_file)
86118
error = nf90_create(out_file, NF90_NETCDF4, ncid)
87-
print*,'ncid is ',ncid
88119
call netcdf_err(error, 'ERROR IN NF90_CREATE' )
89-
error = nf90_def_dim(ncid, 'nx', i_out, dim_x)
120+
error = nf90_def_dim(ncid, 'nx', iout, dim_x)
90121
call netcdf_err(error, 'DEFINING NX DIMENSION' )
91-
error = nf90_def_dim(ncid, 'ny', j_out, dim_y)
122+
error = nf90_def_dim(ncid, 'ny', jout, dim_y)
92123
call netcdf_err(error, 'DEFINING NY DIMENSION' )
93124
error = nf90_def_dim(ncid, 'num_categories', num_categories, dim_z)
94125
call netcdf_err(error, 'DEFINING NZ DIMENSION' )
95126
error = nf90_def_dim(ncid, 'time', num_time_recs, dim_time)
96127
call netcdf_err(error, 'DEFINING TIME DIMENSION' )
128+
97129
error = nf90_def_var(ncid, 'time', NF90_FLOAT, dim_time, id_times)
98130
call netcdf_err(error, 'DEFINING TIME VARIABLE' )
99131
error = nf90_put_att(ncid, id_times, "units", "days since 2015-1-1")
@@ -139,119 +171,26 @@ subroutine output2(data_one_tile, dom_cat_one_tile, lat_one_tile, lon_one_tile,
139171
call netcdf_err(error, 'DEFINING FIELD' )
140172

141173
error = nf90_enddef(ncid, header_buffer_val,4,0,4)
142-
call netcdf_err(error, 'IN NF90_ENDDEF' )
143174

144175
error = nf90_put_var( ncid, id_times, day_of_rec)
145176
call netcdf_err(error, 'WRITING TIME FIELD' )
146177

147-
error = nf90_put_var( ncid, id_lat, lat_one_tile(i_start:i_end,j_start:j_end), &
148-
start=(/1,1/), count=(/i_out,j_out/))
178+
error = nf90_put_var( ncid, id_lat, latitude)
149179
call netcdf_err(error, 'IN NF90_PUT_VAR FOR GEOLAT' )
150180

151-
error = nf90_put_var( ncid, id_lon, lon_one_tile(i_start:i_end,j_start:j_end), &
152-
start=(/1,1/), count=(/i_out,j_out/))
181+
error = nf90_put_var( ncid, id_lon, longitude)
153182
call netcdf_err(error, 'IN NF90_PUT_VAR FOR GEOLON' )
154183

155-
print*,'- WRITE DATA '
156-
error = nf90_put_var( ncid, id_data_pct, data_one_tile(i_start:i_end,j_start:j_end,:), &
157-
start=(/1,1,1,1/), count=(/i_out,j_out,num_categories,1/))
184+
error = nf90_put_var( ncid, id_data_pct, data_pct)
158185
call netcdf_err(error, 'IN NF90_PUT_VAR' )
159186

160-
error = nf90_put_var( ncid, id_data_dom_cat, dom_cat_one_tile(i_start:i_end,j_start:j_end), &
161-
start=(/1,1,1/), count=(/i_out,j_out,1/))
187+
error = nf90_put_var( ncid, id_data_dom_cat, dominate_cat)
162188
call netcdf_err(error, 'IN NF90_PUT_VAR' )
163189

164190
! Temporary output of sum of %.
165-
sum_one_tile = sum(data_one_tile, dim=3)
166-
error = nf90_put_var( ncid, id_sum, sum_one_tile(i_start:i_end,j_start:j_end), &
167-
start=(/1,1,1/), count=(/i_out,j_out,1/))
191+
sum_all = sum(data_pct, dim=3)
192+
error = nf90_put_var( ncid, id_sum, sum_all)
168193

169194
error = nf90_close(ncid)
170195

171-
!----------------------------------------------------------------------
172-
! For regional nests, also output files including the halo
173-
!----------------------------------------------------------------------
174-
175-
if (halo == 0) return
176-
177-
print*,"- WRITE OUT FILES THAT INCLUDE HALO REGION."
178-
179-
error = nf90_create(out_file_with_halo, NF90_NETCDF4, ncid_with_halo)
180-
call netcdf_err(error, 'IN NF90_CREATE' )
181-
error = nf90_def_dim(ncid_with_halo, 'nx', i_mdl, dim_x)
182-
call netcdf_err(error, 'DEFINING NX DIMENSION' )
183-
error = nf90_def_dim(ncid_with_halo, 'ny', j_mdl, dim_y)
184-
call netcdf_err(error, 'DEFINING NY DIMENSION' )
185-
error = nf90_def_dim(ncid_with_halo, 'num_categories', num_categories, dim_z)
186-
call netcdf_err(error, 'DEFINING NZ DIMENSION' )
187-
error = nf90_def_dim(ncid_with_halo, 'time', num_time_recs, dim_time)
188-
call netcdf_err(error, 'DEFINING TIME DIMENSION' )
189-
error = nf90_def_var(ncid_with_halo, 'time', NF90_FLOAT, dim_time, id_times)
190-
call netcdf_err(error, 'DEFINING TIME VARIABLE' )
191-
error = nf90_put_att(ncid_with_halo, id_times, "units", "days since 2015-1-1")
192-
call netcdf_err(error, 'DEFINING TIME ATTRIBUTE' )
193-
if (len_trim(source) > 0) then
194-
error = nf90_put_att(ncid_with_halo, nf90_global, 'source', source)
195-
call netcdf_err(error, 'DEFINING GLOBAL SOURCE ATTRIBUTE' )
196-
endif
197-
198-
error = nf90_def_var(ncid_with_halo, 'geolat', NF90_FLOAT, (/dim_x,dim_y/), id_lat)
199-
call netcdf_err(error, 'DEFINING GEOLAT FIELD' )
200-
error = nf90_put_att(ncid_with_halo, id_lat, "long_name", "Latitude")
201-
call netcdf_err(error, 'DEFINING GEOLAT NAME ATTRIBUTE' )
202-
error = nf90_put_att(ncid_with_halo, id_lat, "units", "degrees_north")
203-
call netcdf_err(error, 'DEFINING GEOLAT UNIT ATTRIBUTE' )
204-
error = nf90_def_var(ncid_with_halo, 'geolon', NF90_FLOAT, (/dim_x,dim_y/), id_lon)
205-
call netcdf_err(error, 'DEFINING GEOLON FIELD' )
206-
error = nf90_put_att(ncid_with_halo, id_lon, "long_name", "Longitude")
207-
call netcdf_err(error, 'DEFINING GEOLON NAME ATTRIBUTE' )
208-
error = nf90_put_att(ncid_with_halo, id_lon, "units", "degrees_east")
209-
call netcdf_err(error, 'DEFINING GEOLON UNIT ATTRIBUTE' )
210-
211-
error = nf90_def_var(ncid_with_halo, field_names_pct, NF90_FLOAT, (/dim_x,dim_y,dim_z,dim_time/), id_data_pct)
212-
call netcdf_err(error, 'DEFINING FIELD VARIABLE' )
213-
error = nf90_put_att(ncid_with_halo, id_data_pct, "units", "percent coverage each category")
214-
call netcdf_err(error, 'DEFINING FIELD ATTRIBUTE' )
215-
error = nf90_put_att(ncid_with_halo, id_data_pct, "missing_value", missing)
216-
call netcdf_err(error, 'DEFINING FIELD ATTRIBUTE' )
217-
error = nf90_put_att(ncid_with_halo, id_data_pct, "coordinates", "geolon geolat")
218-
call netcdf_err(error, 'DEFINING COORD ATTRIBUTE' )
219-
220-
error = nf90_def_var(ncid_with_halo, trim(field_names(1)), NF90_FLOAT, (/dim_x,dim_y,dim_time/), id_data_dom_cat)
221-
call netcdf_err(error, 'DEFINING FIELD' )
222-
error = nf90_put_att(ncid_with_halo, id_data_dom_cat, "units", "dominate category")
223-
call netcdf_err(error, 'DEFINING FIELD ATTRIBUTE' )
224-
error = nf90_put_att(ncid_with_halo, id_data_dom_cat, "missing_value", missing)
225-
call netcdf_err(error, 'DEFINING FIELD ATTRIBUTE' )
226-
error = nf90_put_att(ncid_with_halo, id_data_dom_cat, "coordinates", "geolon geolat")
227-
call netcdf_err(error, 'DEFINING COORD ATTRIBUTE' )
228-
229-
error = nf90_enddef(ncid_with_halo, header_buffer_val,4,0,4)
230-
call netcdf_err(error, 'WRITING HEADER ENDDEF' )
231-
232-
error = nf90_put_var(ncid_with_halo, id_times, day_of_rec)
233-
call netcdf_err(error, 'WRITING TIME VARIABLE' )
234-
235-
error = nf90_put_var( ncid_with_halo, id_lat, lat_one_tile, &
236-
start=(/1,1/), count=(/i_mdl,j_mdl/))
237-
call netcdf_err(error, 'IN NF90_PUT_VAR FOR GEOLAT' )
238-
239-
error = nf90_put_var( ncid_with_halo, id_lon, lon_one_tile, &
240-
start=(/1,1/), count=(/i_mdl,j_mdl/))
241-
call netcdf_err(error, 'IN NF90_PUT_VAR FOR GEOLON' )
242-
243-
print*,'- WRITE DATA'
244-
245-
error = nf90_put_var(ncid_with_halo, id_data_pct, data_one_tile, &
246-
start=(/1,1,1,1/), count=(/i_mdl,j_mdl,num_categories,1/))
247-
call netcdf_err(error, 'IN NF90_PUT_VAR' )
248-
249-
error = nf90_put_var(ncid_with_halo, id_data_dom_cat, dom_cat_one_tile, &
250-
start=(/1,1,1/), count=(/i_mdl,j_mdl,1/))
251-
call netcdf_err(error, 'IN NF90_PUT_VAR' )
252-
253-
error = nf90_close(ncid_with_halo)
254-
255-
return
256-
257-
end subroutine output2
196+
end subroutine writeit

0 commit comments

Comments
 (0)