sfc_climo_gen  1.13.0
output_frac_cats.F90
Go to the documentation of this file.
1 
4 
11 
12  implicit none
13 
14  private
15 
16  public :: output_driver
17 
18  contains
19 
31  subroutine output_driver(data_one_tile, dom_cat_one_tile, lat_one_tile, lon_one_tile, &
32  i_mdl, j_mdl, num_categories, tile)
33 
34  use mpi
35  use esmf
36  use source_grid, only : field_names
37  use model_grid, only : grid_tiles
38  use program_setup, only : halo
39 
40  implicit none
41 
42  integer, intent(in) :: i_mdl, j_mdl, tile, num_categories
43 
44  real(esmf_kind_r4), intent(in) :: data_one_tile(i_mdl,j_mdl,num_categories)
45  real(esmf_kind_r4), intent(in) :: dom_cat_one_tile(i_mdl,j_mdl)
46  real(esmf_kind_r4), intent(in) :: lat_one_tile(i_mdl,j_mdl)
47  real(esmf_kind_r4), intent(in) :: lon_one_tile(i_mdl,j_mdl)
48 
49  character(len=200) :: out_file
50  character(len=200) :: out_file_with_halo
51 
52  integer :: field_idx
53  integer :: ierr
54  integer :: i_out, j_out
55  integer :: i_start, i_end, j_start, j_end
56 
57  field_idx = 1
58 
59  select case (field_names(field_idx))
60  case ('soil_type')
61  out_file = "./soil_type." // grid_tiles(tile) // ".nc"
62  out_file_with_halo = "./soil_type." // grid_tiles(tile) // ".halo.nc"
63  case ('vegetation_type')
64  out_file = "./vegetation_type." // grid_tiles(tile) // ".nc"
65  out_file_with_halo = "./vegetation_type." // grid_tiles(tile) // ".halo.nc"
66  case default
67  print*,'- FATAL ERROR IN ROUTINE OUTPUT. UNIDENTIFIED FIELD : ', field_names(field_idx)
68  call mpi_abort(mpi_comm_world, 67, ierr)
69  end select
70 
71 !----------------------------------------------------------------------
72 ! If user specified a halo (for running stand-alone regional grid),
73 ! remove it.
74 !----------------------------------------------------------------------
75 
76  if (halo > 0) then
77  print*,"- WILL WRITE WITHOUT HALO REGION OF ", halo, " ROWS/COLS."
78  i_start = 1 + halo
79  i_end = i_mdl - halo
80  j_start = 1 + halo
81  j_end = j_mdl - halo
82  i_out = i_end - i_start + 1
83  j_out = j_end - j_start + 1
84  call writeit(out_file, i_out, j_out, num_categories, &
85  lat_one_tile(i_start:i_end,j_start:j_end), &
86  lon_one_tile(i_start:i_end,j_start:j_end), &
87  data_one_tile(i_start:i_end,j_start:j_end,:), &
88  dom_cat_one_tile(i_start:i_end,j_start:j_end) )
89  print*,"- WILL WRITE FULL DOMAIN INCLUDING HALO."
90  call writeit(out_file_with_halo, i_mdl, j_mdl, num_categories, &
91  lat_one_tile, lon_one_tile, data_one_tile, dom_cat_one_tile)
92  else
93  print*,"- WILL WRITE DATA."
94  call writeit(out_file, i_mdl, j_mdl, num_categories, &
95  lat_one_tile, lon_one_tile, data_one_tile, dom_cat_one_tile)
96  endif
97 
98  return
99 
100  end subroutine output_driver
101 
112  subroutine writeit(out_file, iout, jout, num_categories, &
113  latitude, longitude, data_pct, dominant_cat)
115  use esmf
116  use netcdf
117  use utils
119 
120  implicit none
121 
122  character(len=*), intent(in) :: out_file
123 
124  integer, intent(in) :: iout, jout, num_categories
125 
126  real(esmf_kind_r4), intent(in) :: latitude(iout,jout)
127  real(esmf_kind_r4), intent(in) :: longitude(iout,jout)
128  real(esmf_kind_r4), intent(in) :: data_pct(iout,jout,num_categories)
129  real(esmf_kind_r4), intent(in) :: dominant_cat(iout,jout)
130 
131  character(len=200) :: field_names_pct
132  integer :: header_buffer_val = 16384
133  integer :: ncid, dim_x, dim_y, dim_z, dim_time
134  integer :: id_times, id_lat, id_lon, id_data_pct
135  integer :: id_data_dom_cat
136 !integer :: id_sum
137  integer :: error
138 
139 !real :: sum_all(iout,jout)
140 
141  print*,"- OPEN AND WRITE: ",trim(out_file)
142  error = nf90_create(out_file, nf90_netcdf4, ncid)
143  call netcdf_err(error, 'ERROR IN NF90_CREATE' )
144  error = nf90_def_dim(ncid, 'nx', iout, dim_x)
145  call netcdf_err(error, 'DEFINING NX DIMENSION' )
146  error = nf90_def_dim(ncid, 'ny', jout, dim_y)
147  call netcdf_err(error, 'DEFINING NY DIMENSION' )
148  error = nf90_def_dim(ncid, 'num_categories', num_categories, dim_z)
149  call netcdf_err(error, 'DEFINING NZ DIMENSION' )
150  error = nf90_def_dim(ncid, 'time', num_time_recs, dim_time)
151  call netcdf_err(error, 'DEFINING TIME DIMENSION' )
152 
153  error = nf90_def_var(ncid, 'time', nf90_float, dim_time, id_times)
154  call netcdf_err(error, 'DEFINING TIME VARIABLE' )
155  error = nf90_put_att(ncid, id_times, "units", "days since 2015-1-1")
156  call netcdf_err(error, 'DEFINING TIME ATTRIBUTE' )
157  if (len_trim(source) > 0) then
158  error = nf90_put_att(ncid, nf90_global, 'source', source)
159  call netcdf_err(error, 'DEFINING GLOBAL SOURCE ATTRIBUTE' )
160  endif
161 
162  error = nf90_def_var(ncid, 'geolat', nf90_float, (/dim_x,dim_y/), id_lat)
163  call netcdf_err(error, 'DEFINING GEOLAT FIELD' )
164  error = nf90_put_att(ncid, id_lat, "long_name", "Latitude")
165  call netcdf_err(error, 'DEFINING GEOLAT NAME ATTRIBUTE' )
166  error = nf90_put_att(ncid, id_lat, "units", "degrees_north")
167  call netcdf_err(error, 'DEFINING GEOLAT UNIT ATTRIBUTE' )
168  error = nf90_def_var(ncid, 'geolon', nf90_float, (/dim_x,dim_y/), id_lon)
169  call netcdf_err(error, 'DEFINING GEOLON FIELD' )
170  error = nf90_put_att(ncid, id_lon, "long_name", "Longitude")
171  call netcdf_err(error, 'DEFINING GEOLON NAME ATTRIBUTE' )
172  error = nf90_put_att(ncid, id_lon, "units", "degrees_east")
173  call netcdf_err(error, 'DEFINING GEOLON UNIT ATTRIBUTE' )
174 
175  field_names_pct = trim(field_names(1)) // "_pct"
176  error = nf90_def_var(ncid, trim(field_names_pct), nf90_float, (/dim_x,dim_y,dim_z,dim_time/), id_data_pct)
177  call netcdf_err(error, 'DEFINING FIELD' )
178  error = nf90_put_att(ncid, id_data_pct, "units", "percent coverage each category")
179  call netcdf_err(error, 'DEFINING FIELD ATTRIBUTE' )
180  error = nf90_put_att(ncid, id_data_pct, "coordinates", "geolon geolat")
181  call netcdf_err(error, 'DEFINING COORD ATTRIBUTE' )
182 
183  error = nf90_def_var(ncid, trim(field_names(1)), nf90_float, (/dim_x,dim_y,dim_time/), id_data_dom_cat)
184  call netcdf_err(error, 'DEFINING FIELD' )
185  error = nf90_put_att(ncid, id_data_dom_cat, "units", "dominant category")
186  call netcdf_err(error, 'DEFINING FIELD ATTRIBUTE' )
187  error = nf90_put_att(ncid, id_data_dom_cat, "coordinates", "geolon geolat")
188  call netcdf_err(error, 'DEFINING COORD ATTRIBUTE' )
189 
190 !error = nf90_def_var(ncid, 'sum', NF90_FLOAT, (/dim_x,dim_y,dim_time/), id_sum)
191 !call netcdf_err(error, 'DEFINING FIELD' )
192 
193  error = nf90_enddef(ncid, header_buffer_val,4,0,4)
194 
195  error = nf90_put_var( ncid, id_times, day_of_rec)
196  call netcdf_err(error, 'WRITING TIME FIELD' )
197 
198  error = nf90_put_var( ncid, id_lat, latitude)
199  call netcdf_err(error, 'IN NF90_PUT_VAR FOR GEOLAT' )
200 
201  error = nf90_put_var( ncid, id_lon, longitude)
202  call netcdf_err(error, 'IN NF90_PUT_VAR FOR GEOLON' )
203 
204  error = nf90_put_var( ncid, id_data_pct, data_pct)
205  call netcdf_err(error, 'IN NF90_PUT_VAR' )
206 
207  error = nf90_put_var( ncid, id_data_dom_cat, dominant_cat)
208  call netcdf_err(error, 'IN NF90_PUT_VAR' )
209 
210 ! Temporary output of sum of %.
211 !sum_all = sum(data_pct, dim=3)
212 !error = nf90_put_var( ncid, id_sum, sum_all)
213 
214  error = nf90_close(ncid)
215 
216  end subroutine writeit
217 
218  end module output_frac_cats
Set up program execution.
character(len=5), dimension(:), allocatable, public grid_tiles
Array of model grid tile names.
Definition: model_grid.F90:18
character(len=50), dimension(:), allocatable, public field_names
Names of fields to be processed.
Definition: source_grid.F90:21
This module defines the model grid.
Definition: model_grid.F90:10
Read grid specs, date information and land/sea mask for the source data that will be interpolated to ...
Definition: source_grid.F90:12
integer, public num_time_recs
Number of time records.
Definition: source_grid.F90:27
integer, public halo
Number of row/cols defining the lateral boundary halo.
integer, dimension(:), allocatable, public day_of_rec
Day of each time record with respect to Jan 1.
Definition: source_grid.F90:31
subroutine writeit(out_file, iout, jout, num_categories, latitude, longitude, data_pct, dominant_cat)
Write data to a netcdf file.
Output categorical data such as vegetation type.
subroutine, public output_driver(data_one_tile, dom_cat_one_tile, lat_one_tile, lon_one_tile, i_mdl, j_mdl, num_categories, tile)
Driver routine to output model categorical data.
subroutine, public netcdf_err(err, string)
Handle netCDF error codes.
Definition: utils.f90:23
Utilities.
Definition: utils.f90:8
character(len=75), public source
Original source of the data.
Definition: source_grid.F90:22