FV3 Bundle
fv3jedi_increment_io_mod.f90
Go to the documentation of this file.
2 
3 use config_mod
4 use iso_c_binding
5 use datetime_mod
6 use fckit_log_module, only : log
7 
11 
12 !For FMS like restarts
13 use mpp_domains_mod, only: east, north
16 
17 !For GEOS like restarts
18 use netcdf
19 
20 !fckit mpi
21 use fckit_mpi_module, only : fckit_mpi_comm
22 
23 implicit none
24 private
27 
28 contains
29 
30 ! ------------------------------------------------------------------------------
31 
32 subroutine read_fms_restart(geom, incr, c_conf, vdate)
33 
34 implicit none
35 
36 !Arguments
37 type(fv3jedi_geom), intent(inout) :: geom
38 type(fv3jedi_increment), intent(inout) :: incr !< incr
39 type(c_ptr), intent(in) :: c_conf !< Configuration
40 type(datetime), intent(inout) :: vdate !< DateTime
41 
42 
43 end subroutine read_fms_restart
44 
45 ! ------------------------------------------------------------------------------
46 
47 subroutine write_fms_restart(geom, incr, c_conf, vdate)
48 
49 use mpp_mod, only: mpp_pe, mpp_root_pe
50 
51 implicit none
52 
53 !Arguments
54 type(fv3jedi_geom), intent(inout) :: geom
55 type(fv3jedi_increment), intent(in) :: incr !< incr
56 type(c_ptr), intent(in) :: c_conf !< Configuration
57 type(datetime), intent(inout) :: vdate !< DateTime
58 
59 !Locals
60 type(restart_file_type) :: fv_restart
61 type(restart_file_type) :: tr_restart
62 
63 integer :: id_restart, iounit, io_status
64 integer :: date_init(6), date(6), calendar_type, layout(2)
65 integer(kind=c_int) :: idate, isecs
66 
67 character(len=64) :: filename_core
68 character(len=64) :: filename_trcr
69 character(len=64) :: filename_cplr
70 character(len=64) :: datefile
71 
72 character(len=20) :: sdate,validitydate
73 
74 character(len=255) :: datapath_out
75 
76 
77  ! Place to save restarts
78  ! ----------------------
79  datapath_out = "Data/"
80  if (config_element_exists(c_conf,"datapath_write")) then
81  datapath_out = config_get_string(c_conf,len(datapath_out),"datapath_write")
82  endif
83 
84 
85  ! Current date
86  ! ------------
87  call datetime_to_ifs(vdate, idate, isecs)
88  date(1) = idate/10000
89  date(2) = idate/100 - date(1)*100
90  date(3) = idate - (date(1)*10000 + date(2)*100)
91  date(4) = isecs/3600
92  date(5) = (isecs - date(4)*3600)/60
93  date(6) = isecs - (date(4)*3600 + date(5)*60)
94 
95 
96  ! Naming convection for the file
97  ! ------------------------------
98  filename_core = 'fv_core.res.nc'
99  if (config_element_exists(c_conf,"filename_core")) then
100  filename_core = config_get_string(c_conf,len(filename_core),"filename_core")
101  endif
102 
103  filename_trcr = 'fv_tracer.res.nc'
104  if (config_element_exists(c_conf,"filename_trcr")) then
105  filename_trcr = config_get_string(c_conf,len(filename_trcr),"filename_trcr")
106  endif
107 
108  filename_cplr = 'coupler.res'
109  if (config_element_exists(c_conf,"filename_cplr")) then
110  filename_cplr = config_get_string(c_conf,len(filename_cplr),"filename_cplr")
111  endif
112 
113  !Append with the date
114  write(datefile,'(I4,I0.2,I0.2,A1,I0.2,I0.2,I0.2,A1)') date(1),date(2),date(3),".",date(4),date(5),date(6),"."
115  filename_core = trim(datefile)//trim(filename_core)
116  filename_trcr = trim(datefile)//trim(filename_trcr)
117  filename_cplr = trim(datefile)//trim(filename_cplr)
118 
119 
120  ! Register the variables that should be written
121  ! ---------------------------------------------
122 
123  !A-Grid winds, increment
124  id_restart = register_restart_field(fv_restart, filename_core, 'ua', incr%ua, &
125  domain=geom%domain )
126  id_restart = register_restart_field(fv_restart, filename_core, 'va', incr%va, &
127  domain=geom%domain )
128 
129  !Temperature
130  id_restart = register_restart_field( fv_restart, filename_core, 'T', incr%t, &
131  domain=geom%domain )
132 
133  !Pressure thickness
134  id_restart = register_restart_field( fv_restart, filename_core, 'Ps', incr%ps, &
135  domain=geom%domain )
136 
137  !Nonhydrostatic state
138  if (.not. incr%hydrostatic) then
139  id_restart = register_restart_field( fv_restart, filename_core, 'W', incr%w, &
140  domain=geom%domain )
141  id_restart = register_restart_field( fv_restart, filename_core, 'DZ', incr%delz, &
142  domain=geom%domain )
143  endif
144 
145  !Cell center lat/lon
146  id_restart = register_restart_field( fv_restart, filename_core, 'grid_lat', geom%grid_lat, &
147  domain=geom%domain )
148  id_restart = register_restart_field( fv_restart, filename_core, 'grid_lon', geom%grid_lon, &
149  domain=geom%domain )
150 
151  ! Write variables to file
152  ! -----------------------`/
153  call save_restart(fv_restart, directory=trim(adjustl(datapath_out))//'RESTART')
154  call free_restart_type(fv_restart)
155 
156 
157  !Write tracers to file
158  !---------------------
159  id_restart = register_restart_field( tr_restart, filename_trcr, 'sphum' , incr%q, &
160  domain=geom%domain )
161  id_restart = register_restart_field( tr_restart, filename_trcr, 'ice_wat', incr%qi, &
162  domain=geom%domain )
163  id_restart = register_restart_field( tr_restart, filename_trcr, 'liq_wat', incr%ql, &
164  domain=geom%domain )
165  id_restart = register_restart_field( tr_restart, filename_trcr, 'o3mr' , incr%o3, &
166  domain=geom%domain )
167 
168  call save_restart(tr_restart, directory=trim(adjustl(datapath_out))//'RESTART')
169  call free_restart_type(tr_restart)
170 
171 
172  !Write date/time info in coupler.res
173  !-----------------------------------
174  iounit = 101
175  if (mpp_pe() == mpp_root_pe()) then
176  print *,'write_file: date model init = ',incr%date_init
177  print *,'write_file: date model now = ',incr%date
178  print *,'write_file: date vdate = ',date
179  open(iounit, file=trim(adjustl(datapath_out))//'RESTART/'//trim(adjustl(filename_cplr)), form='formatted')
180  write( iounit, '(i6,8x,a)' ) incr%calendar_type, &
181  '(Calendar: no_calendar=0, thirty_day_months=1, julian=2, gregorian=3, noleap=4)'
182  write( iounit, '(6i6,8x,a)' )date, &
183  'Model start time: year, month, day, hour, minute, second'
184  write( iounit, '(6i6,8x,a)' )date, &
185  'Current model time: year, month, day, hour, minute, second'
186  close(iounit)
187  endif
188 
189  return
190 
191 end subroutine write_fms_restart
192 
193 ! ------------------------------------------------------------------------------
194 
195 subroutine read_geos_restart(geom, incr, c_conf, vdate)
197 implicit none
198 
199 !Arguments
200 type(fv3jedi_geom), intent(inout) :: geom
201 type(fv3jedi_increment), intent(inout) :: incr !< incr
202 type(c_ptr), intent(in) :: c_conf !< Configuration
203 type(datetime), intent(inout) :: vdate !< DateTime
204 
205 
206 end subroutine read_geos_restart
207 
208 ! ------------------------------------------------------------------------------
209 
210 subroutine write_geos_restart(geom, incr, c_conf, vdate)
212 implicit none
213 
214 !Arguments
215 type(fv3jedi_geom), intent(inout) :: geom
216 type(fv3jedi_increment), intent(in) :: incr !< incr
217 type(c_ptr), intent(in) :: c_conf !< Configuration
218 type(datetime), intent(inout) :: vdate !< DateTime
219 
220 
221 character(len=255) :: datapath
222 character(len=255) :: filename
223 character(len=64) :: datefile
224 
225 
226 integer :: geostiledim = 0
227 
228 integer, parameter :: nvar = 10
229 integer :: ncid, varid(nvar)
230 
231 integer :: date(6)
232 integer(kind=c_int) :: idate, isecs
233 
234 integer :: isc,iec,jsc,jec,im,jm,km
235 integer :: x_dimid, y_dimid, z_dimid
236 integer :: t_dimid, tile_dimid
237 integer, allocatable :: dimids2(:), dimids3(:)
238 integer, allocatable :: istart2(:), icount2(:)
239 integer, allocatable :: istart3(:), icount3(:)
240 
241 integer :: writeprec
242 type(fckit_mpi_comm) :: f_comm
243 
244  !> Convenience
245  !> -----------
246  isc = incr%isc
247  iec = incr%iec
248  jsc = incr%jsc
249  jec = incr%jec
250 
251  f_comm = fckit_mpi_comm()
252 
253  ! Place to save restarts
254  ! ----------------------
255  datapath = "Data/"
256  if (config_element_exists(c_conf,"datapath")) then
257  datapath = config_get_string(c_conf,len(datapath),"datapath")
258  endif
259 
260 
261  ! Current date
262  ! ------------
263  call datetime_to_ifs(vdate, idate, isecs)
264 
265  date(1) = idate/10000
266  date(2) = idate/100 - date(1)*100
267  date(3) = idate - (date(1)*10000 + date(2)*100)
268  date(4) = isecs/3600
269  date(5) = (isecs - date(4)*3600)/60
270  date(6) = isecs - (date(4)*3600 + date(5)*60)
271 
272 
273  !Using tile as a dimension in the file?
274  if (config_element_exists(c_conf,"geos_tile_dim")) then
275  geostiledim = config_get_int(c_conf,"geos_tile_dim")
276  endif
277 
278  ! Naming convection for the file
279  ! ------------------------------
280  filename = 'GEOS.eta.'
281 
282  if (config_element_exists(c_conf,"filename")) then
283  filename = config_get_string(c_conf,len(filename),"filename")
284  endif
285 
286  !Append with the date
287  write(datefile,'(I4,I0.2,I0.2,A1,I0.2,I0.2,I0.2)') date(1),date(2),date(3),"_",date(4),date(5),date(6)
288  filename = trim(datapath)//trim(filename)//trim(datefile)//trim("z.nc4")
289 
290  writeprec = nf90_float
291 
292  varid = 0
293 
294  if (f_comm%rank() == 0) then
295 
296  im = geom%npx - 1
297  if (geostiledim == 0) then
298  jm = 6*(geom%npy - 1)
299  else
300  jm = geom%npy - 1
301  endif
302  km = geom%npz
303 
304  !Create file to write to
305  call nccheck ( nf90_create(trim(filename), nf90_clobber, ncid), "nf90_create" )
306 
307  !Create dimensions
308  call nccheck ( nf90_def_dim(ncid, "lon", im, x_dimid), "nf90_def_dim lon" )
309  call nccheck ( nf90_def_dim(ncid, "lat", jm, y_dimid), "nf90_def_dim lat" )
310  call nccheck ( nf90_def_dim(ncid, "lev", km, z_dimid), "nf90_def_dim lev" )
311  call nccheck ( nf90_def_dim(ncid, "time", 1, t_dimid), "nf90_def_dim time" )
312 
313  !Add further dimension for the tile number if requested and create dimids
314  if (geostiledim == 1) then
315 
316  call nccheck ( nf90_def_dim(ncid, "tile", geom%ntiles, tile_dimid), "nf90_def_dim tile" )
317 
318  allocate(dimids2(4))
319  dimids2 = (/ x_dimid, y_dimid, t_dimid, tile_dimid /)
320  allocate(dimids3(5))
321  dimids3 = (/ x_dimid, y_dimid, z_dimid, t_dimid, tile_dimid /)
322 
323  else
324 
325  allocate(dimids2(2))
326  dimids2 = (/ x_dimid, y_dimid /)
327  allocate(dimids3(4))
328  dimids3 = (/ x_dimid, y_dimid, z_dimid, t_dimid /)
329 
330  endif
331 
332  !Define variables to be written (geom)
333 print*, dimids2
334  call nccheck( nf90_def_var(ncid, "lon", nf90_double, dimids2, varid(1)), "nf90_def_var lon" )
335  call nccheck( nf90_def_var(ncid, "lat", nf90_double, dimids2, varid(2)), "nf90_def_var lat" )
336 
337  !Define variables to be written (increment)
338  call nccheck( nf90_def_var(ncid, "ua" , writeprec , dimids3, varid(3)), "nf90_def_var ua" )
339 
340  !Close for this proc
341  call nccheck( nf90_enddef(ncid), "nf90_enddef" )
342  call nccheck( nf90_close(ncid), "nf90_close" )
343 
344  deallocate(dimids3,dimids2)
345 
346  endif !Root process
347 
348 
349  !Broadcast the varids
350  call f_comm%broadcast(varid,0)
351 
352 
353  !Create local to this proc start/count
354  if (geostiledim == 1) then
355 
356  allocate(istart3(5),istart2(4))
357  allocate(icount3(5),icount2(4))
358 
359  istart3(1) = isc
360  istart3(2) = jsc
361  istart3(3) = geom%ntile
362  istart3(4) = 1
363  istart3(5) = 1
364  icount3(1) = iec-isc+1
365  icount3(2) = jec-jsc+1
366  icount3(3) = 1
367  icount3(4) = geom%npz
368  icount3(5) = 1
369 
370  istart2(1) = isc
371  istart2(2) = jsc
372  istart2(3) = geom%ntile
373  istart2(4) = 1
374  icount2(1) = iec-isc+1
375  icount2(2) = jec-jsc+1
376  icount2(3) = 1
377  icount2(4) = 1
378 
379  else
380 
381  allocate(istart3(4),istart2(2))
382  allocate(icount3(4),icount2(2))
383 
384  istart3(1) = isc
385  istart3(2) = (geom%ntile-1)*(jm/geom%ntiles) + jsc
386  istart3(3) = 1
387  istart3(4) = 1
388  icount3(1) = iec-isc+1
389  icount3(2) = jec-jsc+1
390  icount3(3) = geom%npz
391  icount3(4) = 1
392 
393  istart2(1) = isc
394  istart2(2) = (geom%ntile-1)*(jm/geom%ntiles) + jsc
395 ! istart2(3) = 1
396  icount2(1) = iec-isc+1
397  icount2(2) = jec-jsc+1
398 ! icount2(3) = 1
399 
400  endif
401 
402  call nccheck( nf90_open(trim(filename), nf90_write, ncid), "nf90_open" )
403 
404 print*, icount2
405  call nccheck( nf90_put_var(ncid, varid(1), geom%grid_lon(isc:iec,jsc:jec), istart2, icount2), "nf90_put_var lon" )
406 
407 
408 ! call nccheck( nf90_put_var(ncid, varid(2), geom%grid_lat(isc:iec,jsc:jec), istart2, icount2), "nf90_put_var lat" )
409 ! call nccheck( nf90_put_var(ncid, varid(3), incr%ua(isc:iec,jsc:jec,:) , istart3, icount3), "nf90_put_var ua" )
410 
411  call nccheck( nf90_close(ncid), "nf90_close" )
412 
413  deallocate(istart2,istart3)
414  deallocate(icount2,icount3)
415 
416  call abor1_ftn("done")
417 
418 end subroutine write_geos_restart
419 
420 ! ------------------------------------------------------------------------------
421 
422 subroutine nccheck(status,iam)
424 implicit none
425 integer, intent ( in) :: status
426 character(len=*), optional :: iam
427 
428 character(len=255) :: error_descr
429 
430  if(status /= nf90_noerr) then
431 
432  error_descr = "fv3jedi_increment_io_mod: NetCDF error, aborting"
433 
434  if (present(iam)) then
435  error_descr = trim(error_descr)//", "//trim(iam)
436  endif
437 
438  error_descr = trim(error_descr)//". Error code: "//trim(nf90_strerror(status))
439 
440  call abor1_ftn(trim(error_descr))
441 
442  end if
443 
444 end subroutine nccheck
445 
446 ! ------------------------------------------------------------------------------
447 
448 end module fv3jedi_increment_io_mod
Fortran derived type to hold FV3JEDI increment.
subroutine, public free_restart_type(fileObj)
Definition: fms_io.F90:1413
Fortran derived type to hold geometry data for the FV3JEDI model.
Definition: mpp.F90:39
subroutine, public write_fms_restart(geom, incr, c_conf, vdate)
Utilities for increment for the FV3JEDI model.
subroutine, public write_geos_restart(geom, incr, c_conf, vdate)
Fortran module handling geometry for the FV3 model.
integer, parameter, public kind_real
subroutine, public read_geos_restart(geom, incr, c_conf, vdate)
subroutine, public save_restart(fileObj, time_stamp, directory, append, time_level)
Definition: fms_io.F90:2467
subroutine, public read_fms_restart(geom, incr, c_conf, vdate)