SCALE-RM
scale_history.F90
Go to the documentation of this file.
1 !-------------------------------------------------------------------------------
15 !-------------------------------------------------------------------------------
17  !-----------------------------------------------------------------------------
18  !
19  !++ Used modules
20  !
21  use scale_precision
22  use scale_stdio
23  use scale_prof
27  !-----------------------------------------------------------------------------
28  implicit none
29  private
30  !-----------------------------------------------------------------------------
31  !
32  !++ Public procedures
33  !
34  public :: hist_setup
35  public :: hist_switch
36  public :: hist_setpres
37  public :: hist_reg
38  public :: hist_query
39  public :: hist_put
40  public :: hist_in
41  public :: hist_get
42  public :: hist_write
43 
44  interface hist_put
45  module procedure hist_put_0d
46  module procedure hist_put_1d
47  module procedure hist_put_2d
48  module procedure hist_put_3d
49  end interface hist_put
50 
51  interface hist_in
52  module procedure hist_in_0d
53  module procedure hist_in_1d
54  module procedure hist_in_2d
55  module procedure hist_in_3d
56  end interface hist_in
57 
58  interface hist_get
59  module procedure hist_get_1d
60  module procedure hist_get_2d
61  module procedure hist_get_3d
62  end interface hist_get
63 
64  !-----------------------------------------------------------------------------
65  !
66  !++ Public parameters & variables
67  !
68  !-----------------------------------------------------------------------------
69  !
70  !++ Private procedures
71  !
72  private :: hist_put_axes
73 
74  !-----------------------------------------------------------------------------
75  !
76  !++ Private parameters & variables
77  !
78  integer, private, parameter :: I_MODEL = 0
79  integer, private, parameter :: I_Z = 1
80  integer, private, parameter :: I_PRES = 2
81 
82  integer, private :: HIST_item_limit
83  integer, private :: HIST_item_count
84  character(len=H_SHORT), private, allocatable :: HIST_item (:)
85  integer, private, allocatable :: HIST_variant(:)
86  integer, private, allocatable :: HIST_zcoord (:,:)
87 
88  integer, private, parameter :: HIST_PRES_nlim = 300
89  integer, private :: HIST_PRES_nlayer = -1
90  real(RP), private, allocatable :: HIST_PRES_val(:)
91 
92  logical, private :: enabled
93  integer, private :: im, jm, km
94  integer, private :: ims, ime
95  integer, private :: jms, jme
96 
97  logical, private :: HIST_BND = .false.
98  !-----------------------------------------------------------------------------
99 contains
100  !-----------------------------------------------------------------------------
102  subroutine hist_setup
103  use gtool_history, only: &
105  use scale_process, only: &
106  prc_mpistop, &
107  prc_masterrank, &
108  prc_myrank
109  use scale_rm_process, only: &
110  prc_2drank, &
111  prc_num_x, &
112  prc_num_y
113  use scale_time, only: &
114  time_nowdate, &
115  time_nowms, &
116  time_dtsec, &
118  use scale_interpolation, only: &
120  implicit none
121 
122  character(len=H_MID) :: history_h_title = 'SCALE-RM HISTORY OUTPUT'
123  character(len=H_MID) :: history_t_since
124 
125  real(RP) :: hist_pres(hist_pres_nlim) = 0.0_rp
126 
127  namelist / param_hist / &
128  hist_pres_nlayer, &
129  hist_pres, &
130  hist_bnd
131 
132  integer :: hist_variant_limit
133 
134  real(DP) :: start_daysec
135  integer :: rankidx(2)
136  integer :: ierr
137  integer :: k
138  !---------------------------------------------------------------------------
139 
140  if( io_l ) write(io_fid_log,*)
141  if( io_l ) write(io_fid_log,*) '++++++ Module[HISTORY] / Categ[ATMOS-RM IO] / Origin[SCALElib]'
142 
143  !--- read namelist
144  rewind(io_fid_conf)
145  read(io_fid_conf,nml=param_hist,iostat=ierr)
146  if( ierr < 0 ) then !--- missing
147  if( io_l ) write(io_fid_log,*) '*** Not found namelist. Default used.'
148  elseif( ierr > 0 ) then !--- fatal error
149  write(*,*) 'xxx Not appropriate names in namelist PARAM_HIST. Check!'
150  call prc_mpistop
151  endif
152  if( io_nml ) write(io_fid_nml,nml=param_hist)
153 
154  ! check pressure coordinate
155  if ( hist_pres_nlayer > 0 ) then
156  if ( hist_pres_nlayer > 100 ) then
157  write(*,*) 'xxx number of layers of pressure is larger the KMAX'
158  call prc_mpistop
159  end if
160 
161  do k = 1, hist_pres_nlayer
162  if ( hist_pres(k) <= 0.0_rp ) then
163  write(*,*) 'xxx Invalid value found in pressure coordinate! (k,value)=', k, hist_pres(k)
164  call prc_mpistop
165  elseif ( hist_pres(k+1) >= hist_pres(k) ) then
166  write(*,*) 'xxx The value of pressure coordinate must be descending order! ', &
167  '(k,value[k],value[k+1])=', k, hist_pres(k), hist_pres(k+1)
168  call prc_mpistop
169  endif
170  enddo
171  else
172  if( io_l ) write(io_fid_log,*)
173  if( io_l ) write(io_fid_log,*) '*** HIST_PRES_nlayer is not set.'
174  if( io_l ) write(io_fid_log,*) '*** Output with pressure coordinate is disabled'
175  endif
176 
177  call prof_rapstart('FILE_O_NetCDF', 2)
178 
179  rankidx(1) = prc_2drank(prc_myrank,1)
180  rankidx(2) = prc_2drank(prc_myrank,2)
181 
182  start_daysec = time_startdaysec
183  if ( time_nowdate(1) > 0 ) then
184  write(history_t_since,'(I4.4,5(A1,I2.2))') time_nowdate(1), &
185  '-', time_nowdate(2), &
186  '-', time_nowdate(3), &
187  ' ', time_nowdate(4), &
188  ':', time_nowdate(5), &
189  ':', time_nowdate(6)
190  start_daysec = time_nowms
191  else
192  history_t_since = ''
193  endif
194 
195  if ( hist_bnd ) then
196  im = imaxb
197  jm = jmaxb
198  ims = isb
199  ime = ieb
200  jms = jsb
201  jme = jeb
202  else
203  im = imax
204  jm = jmax
205  ims = is
206  ime = ie
207  jms = js
208  jme = je
209  end if
210 
211  km = max( lkmax, ukmax, kmax )
212 
213  call historyinit( hist_item_limit, & ! [OUT]
214  hist_variant_limit, & ! [OUT]
215  im, jm, km, & ! [IN]
216  prc_masterrank, & ! [IN]
217  prc_myrank, & ! [IN]
218  rankidx(:), & ! [IN]
219  history_h_title, & ! [IN]
220  h_source, & ! [IN]
221  h_institute, & ! [IN]
222  time_start = start_daysec, & ! [IN]
223  time_interval = time_dtsec, & ! [IN]
224  time_since = history_t_since, & ! [IN]
225  default_zcoord = 'model', & ! [IN]
226  namelist_fid = io_fid_conf ) ! [IN]
227 
228  hist_item_count = 0
229  if ( hist_item_limit > 0 ) then
230  allocate( hist_item(hist_item_limit) )
231  allocate( hist_variant(hist_item_limit) )
232  allocate( hist_zcoord(hist_item_limit,hist_variant_limit) )
233  hist_item(:) = ''
234  hist_variant(:) = 0
235  hist_zcoord(:,:) = 0
236  endif
237 
238  if ( hist_pres_nlayer > 0 ) then
239  allocate( hist_pres_val(hist_pres_nlayer) )
240 
241  do k = 1, hist_pres_nlayer
242  hist_pres_val(k) = hist_pres(k) * 100.0_rp ! [hPa->Pa]
243  enddo
244 
245  call interp_setup_pres( hist_pres_nlayer ) ! [IN]
246  endif
247 
248  call hist_put_axes
249 
250  enabled = .true.
251 
252  call prof_rapend ('FILE_O_NetCDF', 2)
253 
254  return
255  end subroutine hist_setup
256 
257  !-----------------------------------------------------------------------------
259  ! only register the axis and coordinate variables into internal buffers
260  ! The actual write happens later when calling HIST_write
261  subroutine hist_put_axes
262  use gtool_history, only: &
263  historyputaxis, &
264  historyputassociatedcoordinates
265  use scale_const, only: &
266  d2r => const_d2r
267  use scale_grid, only: &
268  grid_cz, &
269  grid_cx, &
270  grid_cy, &
271  grid_fz, &
272  grid_fx, &
273  grid_fy, &
274  grid_cdz, &
275  grid_cdx, &
276  grid_cdy, &
277  grid_fdz, &
278  grid_fdx, &
279  grid_fdy, &
280  grid_cbfz, &
281  grid_cbfx, &
282  grid_cbfy, &
283  grid_fbfz, &
284  grid_fbfx, &
285  grid_fbfy, &
286  grid_cxg, &
287  grid_cyg, &
288  grid_fxg, &
289  grid_fyg, &
290  grid_cdxg, &
291  grid_cdyg, &
292  grid_fdxg, &
293  grid_fdyg, &
294  grid_cbfxg, &
295  grid_cbfyg, &
296  grid_fbfxg, &
297  grid_fbfyg
298  use scale_land_grid, only: &
299  grid_lcz, &
300  grid_lfz, &
301  grid_lcdz
302  use scale_urban_grid, only: &
303  grid_ucz, &
304  grid_ufz, &
305  grid_ucdz
306  use scale_grid_real, only: &
307  real_cz, &
308  real_fz, &
309  real_lon, &
310  real_lonx, &
311  real_lony, &
312  real_lonxy, &
313  real_lat, &
314  real_latx, &
315  real_laty, &
316  real_latxy
317  use scale_topography, only: &
318  topo_zsfc
319  use scale_landuse, only: &
321  use scale_process, only: &
322  prc_myrank
323  use scale_rm_process, only: &
324  prc_2drank, &
325  prc_num_x, &
326  prc_num_y
327  implicit none
328 
329  real(RP) :: axis (im,jm,kmax)
330  character(len=2) :: axis_name(3)
331 
332  integer :: k, i, j
333  integer :: rankidx(2)
334  integer :: start(3)
335  integer :: startx, starty, startz
336  integer :: xag, yag
337  !---------------------------------------------------------------------------
338 
339  rankidx(1) = prc_2drank(prc_myrank,1)
340  rankidx(2) = prc_2drank(prc_myrank,2)
341 
342  ! For parallel I/O, some variables are written by a subset of processes.
343  ! 1. Only PRC_myrank 0 writes all z axes
344  ! 2. Only south-most processes (rankidx(2) == 0) write x axes
345  ! rankidx(1) == 0 writes west HALO
346  ! rankidx(1) == PRC_NUM_X-1 writes east HALO
347  ! others writes without HALO
348  ! 3. Only west-most processes (rankidx(1) == 0) write y axes
349  ! rankidx(1) == 0 writes south HALO
350  ! rankidx(1) == PRC_NUM_Y-1 writes north HALO
351  ! others writes without HALO
352 
353  if ( io_aggregate ) then
354  if ( hist_bnd ) then
355  startx = isgb ! global subarray starting index
356  starty = jsgb ! global subarray starting index
357  xag = iagb
358  yag = jagb
359  else
360  startx = 1 + prc_2drank(prc_myrank,1) * imax ! no IHALO
361  starty = 1 + prc_2drank(prc_myrank,2) * jmax ! no JHALO
362  xag = imaxg
363  yag = jmaxg
364  end if
365  startz = 1
366  if ( rankidx(2) .GT. 0 ) startx = -1 ! only south-most processes write
367  if ( rankidx(1) .GT. 0 ) starty = -1 ! only west-most processes write
368  if ( prc_myrank .GT. 0 ) startz = -1 ! only rank 0 writes Z axes
369  else
370  xag = im
371  yag = jm
372  startx = 1
373  starty = 1
374  startz = 1
375  end if
376 
377  ! for the shared-file I/O method, the axes are global (gsize)
378  ! for one-file-per-process I/O method, the axes size is equal to the local buffer size
379  call historyputaxis( 'x', 'X', 'm', 'x', grid_cx(ims:ime), gsize=xag, start=startx )
380  call historyputaxis( 'y', 'Y', 'm', 'y', grid_cy(jms:jme), gsize=yag, start=starty )
381  call historyputaxis( 'z', 'Z', 'm', 'z', grid_cz(ks:ke), gsize=kmax, start=startz )
382  call historyputaxis( 'xh', 'X (half level)', 'm', 'xh', grid_fx(ims:ime), gsize=xag, start=startx )
383  call historyputaxis( 'yh', 'Y (half level)', 'm', 'yh', grid_fy(jms:jme), gsize=yag, start=starty )
384  call historyputaxis( 'zh', 'Z (half level)', 'm', 'zh', grid_fz(ks:ke), gsize=kmax, start=startz )
385 
386  if ( hist_pres_nlayer > 0 ) then
387  call historyputaxis( 'pressure', 'Pressure', 'hPa', 'pressure', &
388  hist_pres_val(:)/100.0_rp, down=.true., gsize=hist_pres_nlayer, start=startz )
389  endif
390 
391  call historyputaxis( 'lz', 'LZ', 'm', 'lz', grid_lcz(lks:lke), down=.true., gsize=lkmax, start=startz )
392  call historyputaxis( 'lzh', 'LZ (half level)', 'm', 'lzh', grid_lfz(lks:lke), down=.true., gsize=lkmax, start=startz )
393 
394  call historyputaxis( 'uz', 'UZ', 'm', 'uz', grid_ucz(uks:uke), down=.true., gsize=ukmax, start=startz )
395  call historyputaxis( 'uzh', 'UZ (half level)', 'm', 'uzh', grid_ufz(uks:uke), down=.true., gsize=ukmax, start=startz )
396 
397  ! axes below always include halos when written to file regardless of PRC_PERIODIC_X/PRC_PERIODIC_Y
398  call historyputaxis( 'CZ', 'Atmos Grid Center Position Z', 'm', 'CZ', grid_cz, gsize=ka, start=startz )
399  call historyputaxis( 'FZ', 'Atmos Grid Face Position Z', 'm', 'FZ', grid_fz, gsize=ka+1, start=startz )
400  call historyputaxis( 'CDZ', 'Grid Cell length Z', 'm', 'CZ', grid_cdz, gsize=ka, start=startz )
401  call historyputaxis( 'FDZ', 'Grid distance Z', 'm', 'FDZ', grid_fdz, gsize=ka-1, start=startz )
402  if ( io_aggregate ) then
403  call historyputaxis( 'CX', 'Atmos Grid Center Position X', 'm', 'CX', grid_cxg, gsize=iag, start=startz )
404  call historyputaxis( 'CY', 'Atmos Grid Center Position Y', 'm', 'CY', grid_cyg, gsize=jag, start=startz )
405  call historyputaxis( 'FX', 'Atmos Grid Face Position X', 'm', 'FX', grid_fxg, gsize=iag+1, start=startz )
406  call historyputaxis( 'FY', 'Atmos Grid Face Position Y', 'm', 'FY', grid_fyg, gsize=jag+1, start=startz )
407  call historyputaxis( 'CDX', 'Grid Cell length X', 'm', 'CX', grid_cdxg, gsize=iag, start=startz )
408  call historyputaxis( 'CDY', 'Grid Cell length Y', 'm', 'CY', grid_cdyg, gsize=jag, start=startz )
409  call historyputaxis( 'FDX', 'Grid distance X', 'm', 'FDX', grid_fdxg, gsize=iag-1, start=startz )
410  call historyputaxis( 'FDY', 'Grid distance Y', 'm', 'FDY', grid_fdyg, gsize=jag-1, start=startz )
411  else
412  call historyputaxis( 'CX', 'Atmos Grid Center Position X', 'm', 'CX', grid_cx )
413  call historyputaxis( 'CY', 'Atmos Grid Center Position Y', 'm', 'CY', grid_cy )
414  call historyputaxis( 'FX', 'Atmos Grid Face Position X', 'm', 'FX', grid_fx )
415  call historyputaxis( 'FY', 'Atmos Grid Face Position Y', 'm', 'FY', grid_fy )
416  call historyputaxis( 'CDX', 'Grid Cell length X', 'm', 'CX', grid_cdx )
417  call historyputaxis( 'CDY', 'Grid Cell length Y', 'm', 'CY', grid_cdy )
418  call historyputaxis( 'FDX', 'Grid distance X', 'm', 'FDX', grid_fdx )
419  call historyputaxis( 'FDY', 'Grid distance Y', 'm', 'FDY', grid_fdy )
420  end if
421 
422  call historyputaxis( 'LCZ', 'Land Grid Center Position Z', 'm', 'LCZ', grid_lcz, down=.true., gsize=lkmax, start=startz )
423  call historyputaxis( 'LFZ', 'Land Grid Face Position Z', 'm', 'LFZ', grid_lfz, down=.true., gsize=lkmax+1, start=startz )
424  call historyputaxis( 'LCDZ', 'Land Grid Cell length Z', 'm', 'LCZ', grid_lcdz, gsize=lkmax, start=startz )
425 
426  call historyputaxis( 'UCZ', 'Urban Grid Center Position Z', 'm', 'UCZ', grid_ucz, down=.true., gsize=ukmax, start=startz )
427  call historyputaxis( 'UFZ', 'Urban Grid Face Position Z', 'm', 'UFZ', grid_ufz, down=.true., gsize=ukmax+1, start=startz )
428  call historyputaxis( 'UCDZ', 'Urban Grid Cell length Z', 'm', 'UCZ', grid_ucdz, gsize=ukmax, start=startz )
429 
430  call historyputaxis('CBFZ', 'Boundary factor Center Z', '1', 'CZ', grid_cbfz, gsize=ka, start=startz )
431  call historyputaxis('FBFZ', 'Boundary factor Face Z', '1', 'CZ', grid_fbfz, gsize=ka, start=startz )
432  if ( io_aggregate ) then
433  call historyputaxis('CBFX', 'Boundary factor Center X', '1', 'CX', grid_cbfxg, gsize=iag, start=startz )
434  call historyputaxis('CBFY', 'Boundary factor Center Y', '1', 'CY', grid_cbfyg, gsize=jag, start=startz )
435  call historyputaxis('FBFX', 'Boundary factor Face X', '1', 'CX', grid_fbfxg, gsize=iag, start=startz )
436  call historyputaxis('FBFY', 'Boundary factor Face Y', '1', 'CY', grid_fbfyg, gsize=jag, start=startz )
437  else
438  call historyputaxis('CBFX', 'Boundary factor Center X', '1', 'CX', grid_cbfx )
439  call historyputaxis('CBFY', 'Boundary factor Center Y', '1', 'CY', grid_cbfy )
440  call historyputaxis('FBFX', 'Boundary factor Face X', '1', 'CX', grid_fbfx )
441  call historyputaxis('FBFY', 'Boundary factor Face Y', '1', 'CY', grid_fbfy )
442  end if
443 
444  ! TODO: skip 8 axes below when IO_AGGREGATE is true, as all axes are now global
445  call historyputaxis('CXG', 'Grid Center Position X (global)', 'm', 'CXG', grid_cxg, gsize=iag, start=startz )
446  call historyputaxis('CYG', 'Grid Center Position Y (global)', 'm', 'CYG', grid_cyg, gsize=jag, start=startz )
447  call historyputaxis('FXG', 'Grid Face Position X (global)', 'm', 'FXG', grid_fxg, gsize=iag+1, start=startz )
448  call historyputaxis('FYG', 'Grid Face Position Y (global)', 'm', 'FYG', grid_fyg, gsize=jag+1, start=startz )
449 
450  call historyputaxis('CBFXG', 'Boundary factor Center X (global)', '1', 'CXG', grid_cbfxg, gsize=iag, start=startz )
451  call historyputaxis('CBFYG', 'Boundary factor Center Y (global)', '1', 'CYG', grid_cbfyg, gsize=jag, start=startz )
452  call historyputaxis('FBFXG', 'Boundary factor Face X (global)', '1', 'CXG', grid_fbfxg, gsize=iag, start=startz )
453  call historyputaxis('FBFYG', 'Boundary factor Face Y (global)', '1', 'CYG', grid_fbfyg, gsize=jag, start=startz )
454 
455  ! associate coordinates
456  if ( io_aggregate ) then
457  if ( hist_bnd ) then
458  start(1) = isgb ! global subarray starting index
459  start(2) = jsgb ! global subarray starting index
460  else
461  start(1) = 1 + prc_2drank(prc_myrank,1) * imax ! no IHALO
462  start(2) = 1 + prc_2drank(prc_myrank,2) * jmax ! no JHALO
463  end if
464  start(3) = 1
465  else
466  start(:) = 1
467  end if
468 
469  do k = 1, kmax
470  axis(1:im,1:jm,k) = real_cz(k+ks-1,ims:ime,jms:jme)
471  enddo
472  axis_name(1:3) = (/'x ', 'y ', 'z '/)
473  call historyputassociatedcoordinates( 'height', 'height above ground level', &
474  'm', axis_name(1:3), axis(:,:,:), start=start )
475 
476  do k = 1, kmax
477  axis(1:im,1:jm,k) = real_fz(k+ks-1,ims:ime,jms:jme)
478  enddo
479  axis_name(1:3) = (/'x ', 'y ', 'zh'/)
480  call historyputassociatedcoordinates( 'height_xyw', 'height above ground level (half level xyw)', &
481  'm' , axis_name(1:3), axis(:,:,:), start=start )
482 
483  do k = 1, kmax
484  do j = 1, jm
485  do i = 1, min(im,ia-ims)
486  axis(i,j,k) = ( real_cz(k+ks-1,ims+i-1,jms+j-1) + real_cz(k+ks-1,ims+i,jms+j-1) ) * 0.5_rp
487  enddo
488  enddo
489  enddo
490  if ( im == ia-ims+1 ) then
491  do k = 1, kmax
492  do j = 1, jm
493  axis(im,j,k) = real_cz(k+ks-1,ims+im-1,jms+j-1)
494  enddo
495  enddo
496  end if
497  axis_name(1:3) = (/'xh', 'y ', 'z '/)
498  call historyputassociatedcoordinates( 'height_uyz', 'height above ground level (half level uyz)', &
499  'm', axis_name(1:3), axis(:,:,:), start=start )
500 
501  do k = 1, kmax
502  do j = 1, min(jm,ja-jms)
503  do i = 1, im
504  axis(i,j,k) = ( real_cz(k+ks-1,ims+i-1,jms+j-1) + real_cz(k+ks-1,ims+i-1,jms+j) ) * 0.5_rp
505  enddo
506  enddo
507  enddo
508  if ( jm == ja-jms+1 ) then
509  do k = 1, kmax
510  do i = 1, im
511  axis(i,jm,k) = real_cz(k+ks-1,ims+i-1,jms+jm-1)
512  enddo
513  enddo
514  end if
515  axis_name(1:3) = (/'x ', 'yh', 'z '/)
516  call historyputassociatedcoordinates( 'height_xvz', 'height above ground level (half level xvz)', &
517  'm', axis_name(1:3), axis(:,:,:), start=start )
518 
519  do k = 1, kmax
520  do j = 1, min(jm,ja-jms)
521  do i = 1, min(im,ia-ims)
522  axis(i,j,k) = ( real_cz(k+ks-1,ims+i-1,jms+j-1) + real_cz(k+ks-1,ims+i ,jms+j-1) &
523  + real_cz(k+ks-1,ims+i-1,jms+j ) + real_cz(k+ks-1,ims+i ,jms+j ) ) * 0.25_rp
524  enddo
525  enddo
526  enddo
527  if ( jm == ja-jms+1 ) then
528  do k = 1, kmax
529  do i = 1, min(im,ia-ims)
530  axis(i,jm,k) = ( real_cz(k+ks-1,ims+i-1,jms+jm-1) + real_cz(k+ks-1,ims+i,jms+jm-1) ) * 0.5_rp
531  enddo
532  enddo
533  end if
534  if ( im == ia-ims+1 ) then
535  do k = 1, kmax
536  do j = 1, min(jm,ja-jms)
537  axis(im,j,k) = ( real_cz(k+ks-1,ims+im-1,jms+j-1) + real_cz(k+ks-1,ims+im-1,jms+j) ) * 0.5_rp
538  enddo
539  enddo
540  end if
541  if ( im == ia-ims+1 .and. jm == ja-jms+1 ) then
542  do k = 1, kmax
543  axis(im,jm,k) = real_cz(k+ks-1,ims+im-1,jms+jm-1)
544  enddo
545  end if
546  axis_name(1:3) = (/'xh', 'yh', 'z '/)
547  call historyputassociatedcoordinates( 'height_uvz', 'height above ground level (half level uvz)', &
548  'm', axis_name(1:3), axis(:,:,:), start=start )
549 
550  do k = 1, kmax
551  do j = 1, jm
552  do i = 1, min(im,ia-ims)
553  axis(i,j,k) = ( real_fz(k+ks-1,ims+i-1,jms+j-1) + real_fz(k+ks-1,ims+i,jms+j-1) ) * 0.5_rp
554  enddo
555  enddo
556  enddo
557  if ( im == ia-ims+1 ) then
558  do k = 1, kmax
559  do j = 1, jm
560  axis(im,j,k) = real_fz(k+ks-1,ims+im-1,jms+j-1)
561  enddo
562  enddo
563  end if
564  axis_name(1:3) = (/'xh', 'y ', 'zh'/)
565  call historyputassociatedcoordinates( 'height_uyw', 'height above ground level (half level uyw)', &
566  'm', axis_name(1:3), axis(:,:,:), start=start )
567 
568  do k = 1, kmax
569  do j = 1, min(jm,ja-jms)
570  do i = 1, im
571  axis(i,j,k) = ( real_fz(k+ks-1,ims+i-1,jms+j-1) + real_fz(k+ks-1,ims+i-1,jms+j) ) * 0.5_rp
572  enddo
573  enddo
574  enddo
575  if ( jm == ja-jms+1 ) then
576  do k = 1, kmax
577  do i = 1, im
578  axis(i,jm,k) = real_fz(k+ks-1,ims+i-1,jms+jm-1)
579  enddo
580  enddo
581  end if
582  axis_name(1:3) = (/'x ', 'yh', 'zh'/)
583  call historyputassociatedcoordinates( 'height_xvw', 'height above ground level (half level xvw)', &
584  'm', axis_name(1:3), axis(:,:,:), start=start )
585 
586  do k = 1, kmax
587  do j = 1, min(jm,ja-jms)
588  do i = 1, min(im,ia-ims)
589  axis(i,j,k) = ( real_fz(k+ks-1,ims+i-1,jms+j-1) + real_fz(k+ks-1,ims+i ,jms+j-1) &
590  + real_fz(k+ks-1,ims+i-1,jms+j ) + real_fz(k+ks-1,ims+i ,jms+j ) ) * 0.25_rp
591  enddo
592  enddo
593  enddo
594  if ( jm == ja-jms+1 ) then
595  do k = 1, kmax
596  do i = 1, min(im,ia-ims)
597  axis(i,jm,k) = ( real_fz(k+ks-1,ims+i-1,jms+jm-1) + real_fz(k+ks-1,ims+i,jms+jm-1) ) * 0.5_rp
598  enddo
599  enddo
600  end if
601  if ( im == ia-ims+1 ) then
602  do k = 1, kmax
603  do j = 1, min(jm,ja-jms)
604  axis(im,j,k) = ( real_fz(k+ks-1,ims+im-1,jms+j-1) + real_fz(k+ks-1,ims+im-1,jms+j) ) * 0.5_rp
605  enddo
606  enddo
607  end if
608  if ( im == ia-ims+1 .and. jm == ja-jms+1 ) then
609  do k = 1, kmax
610  axis(im,jm,k) = real_fz(k+ks-1,ims+im-1,jms+jm-1)
611  enddo
612  end if
613  axis_name(1:3) = (/'xh', 'yh', 'zh'/)
614  call historyputassociatedcoordinates( 'height_uvw', 'height above ground level (half level uvw)', &
615  'm', axis_name(1:3), axis(:,:,:), start=start )
616 
617  axis(1:im,1:jm,1) = real_lon(ims:ime,jms:jme) / d2r
618  axis_name(1:2) = (/'x ', 'y '/)
619  call historyputassociatedcoordinates( 'lon', 'longitude', &
620  'degrees_east', axis_name(1:2), axis(:,:,1), start=start )
621 
622  axis(1:im,1:jm,1) = real_lonx(ims:ime,jms:jme) / d2r
623  axis_name(1:2) = (/'xh', 'y '/)
624  call historyputassociatedcoordinates( 'lon_uy', 'longitude (half level uy)', &
625  'degrees_east', axis_name(1:2), axis(:,:,1), start=start )
626 
627  axis(1:im,1:jm,1) = real_lony(ims:ime,jms:jme) / d2r
628  axis_name(1:2) = (/'x ', 'yh'/)
629  call historyputassociatedcoordinates( 'lon_xv', 'longitude (half level xv)', &
630  'degrees_east', axis_name(1:2), axis(:,:,1), start=start )
631 
632  axis(1:im,1:jm,1) = real_lonxy(ims:ime,jms:jme) / d2r
633  axis_name(1:2) = (/'xh', 'yh'/)
634  call historyputassociatedcoordinates( 'lon_uv', 'longitude (half level uv)', &
635  'degrees_east', axis_name(1:2), axis(:,:,1), start=start )
636 
637  axis(1:im,1:jm,1) = real_lat(ims:ime,jms:jme) / d2r
638  axis_name(1:2) = (/'x ', 'y '/)
639  call historyputassociatedcoordinates( 'lat', 'latitude', &
640  'degrees_north', axis_name(1:2), axis(:,:,1), start=start )
641 
642  axis(1:im,1:jm,1) = real_latx(ims:ime,jms:jme) / d2r
643  axis_name(1:2) = (/'xh', 'y '/)
644  call historyputassociatedcoordinates( 'lat_uy', 'latitude (half level uy)', &
645  'degrees_north', axis_name(1:2), axis(:,:,1), start=start )
646 
647  axis(1:im,1:jm,1) = real_laty(ims:ime,jms:jme) / d2r
648  axis_name(1:2) = (/'x ', 'yh'/)
649  call historyputassociatedcoordinates( 'lat_xv', 'latitude (half level xv)', &
650  'degrees_north', axis_name(1:2), axis(:,:,1), start=start )
651 
652  axis(1:im,1:jm,1) = real_latxy(ims:ime,jms:jme) / d2r
653  axis_name(1:2) = (/'xh', 'yh'/)
654  call historyputassociatedcoordinates( 'lat_uv', 'latitude (half level uv)', &
655  'degrees_north', axis_name(1:2), axis(:,:,1), start=start )
656 
657  axis(1:im,1:jm,1) = topo_zsfc(ims:ime,jms:jme)
658  axis_name(1:2) = (/'x ', 'y '/)
659  call historyputassociatedcoordinates( 'topo', 'topography', &
660  'm', axis_name(1:2), axis(:,:,1), start=start )
661 
662  axis(1:im,1:jm,1) = landuse_frac_land(ims:ime,jms:jme)
663  axis_name(1:2) = (/'x ', 'y '/)
664  call historyputassociatedcoordinates( 'lsmask', 'fraction for land-sea mask', &
665  '1', axis_name(1:2), axis(:,:,1), start=start )
666 
667  return
668  end subroutine hist_put_axes
669 
670  !-----------------------------------------------------------------------------
672  subroutine hist_switch( switch )
673  implicit none
674 
675  logical, intent(in) :: switch
676  !---------------------------------------------------------------------------
677 
678  enabled = switch
679 
680  return
681  end subroutine hist_switch
682 
683  !-----------------------------------------------------------------------------
685  subroutine hist_setpres( &
686  PRES, &
687  SFC_PRES )
688  use scale_interpolation, only: &
690  implicit none
691 
692  real(RP), intent(in) :: pres (ka,ia,ja) ! pressure in Xi coordinate [Pa]
693  real(RP), intent(in) :: sfc_pres( ia,ja) ! surface pressure [Pa]
694  !---------------------------------------------------------------------------
695 
696  if ( hist_pres_nlayer > 0 ) then
697  call interp_update_pres( hist_pres_nlayer, & ! [IN]
698  pres(:,:,:), & ! [IN]
699  sfc_pres(:,:) , & ! [IN]
700  hist_pres_val(:) ) ! [IN]
701  endif
702 
703  return
704  end subroutine hist_setpres
705 
706  !-----------------------------------------------------------------------------
708  subroutine hist_reg( &
709  itemid, &
710  item, &
711  desc, &
712  unit, &
713  ndim, &
714  xdim, &
715  ydim, &
716  zdim )
717  use mpi, only: &
718  mpi_comm_null
719  use gtool_history, only: &
722  use scale_time, only: &
723  nowstep => time_nowstep
724  use scale_process, only: &
725  prc_mpistop, &
726  prc_myrank, &
728  use scale_rm_process, only: &
729  prc_2drank, &
730  prc_num_x, &
731  prc_num_y
732  implicit none
733 
734  integer, intent(out) :: itemid
735  character(len=*), intent(in) :: item
736  character(len=*), intent(in) :: desc
737  character(len=*), intent(in) :: unit
738  integer, intent(in) :: ndim
739  character(len=*), intent(in), optional :: xdim
740  character(len=*), intent(in), optional :: ydim
741  character(len=*), intent(in), optional :: zdim
742 
743  logical :: flag_half_x
744  logical :: flag_half_y
745  logical :: flag_half_z
746 
747  character(len=H_SHORT) :: dims(3)
748 
749  integer :: nvariant1, nvariant2, nvariant3
750  integer :: v, id
751  logical :: atom
752 
753  integer :: rankidx(2)
754  integer :: start(4), count(4)
755  integer :: comm
756  !---------------------------------------------------------------------------
757 
758  itemid = -1
759 
760  if( .NOT. enabled ) return
761 
762  if( hist_item_limit == 0 ) return
763 
764  do id = 1, hist_item_count
765  if ( item == hist_item(id) ) then ! item exists
766  itemid = id
767  return
768  endif
769  enddo
770 
771  call prof_rapstart('FILE_O_NetCDF', 2)
772 
773  ! Try to add new item
774 
775  if ( len_trim(item) >= h_short ) then
776  write(*,'(1x,A,I2,A,A)') 'xxx Length of history name should be <= ', h_short-1 ,' chars. STOP', trim(item)
777  call prc_mpistop
778  endif
779 
780  atom = .true.
781 
782  rankidx(1) = prc_2drank(prc_myrank,1)
783  rankidx(2) = prc_2drank(prc_myrank,2)
784 
785  start = 0
786  count = 0
787 
788  if ( ndim == 1 ) then
789 
790  ! check half/full level for vertical
791  dims(1) = "z"
792  if ( present(zdim) ) then
793  if ( zdim == 'half' ) then
794  dims(1) = "zh"
795  endif
796  endif
797 
798  ! for shared-file parallel I/O, only rank 0 writes variables with only Z dimension
799  start(1) = 1
800  count(1) = kmax
801  if ( prc_myrank .GT. 0 ) count(1) = 0
802 
803  elseif ( ndim == 2 ) then
804 
805  ! check half/full level for horizontal
806  flag_half_x = .false.
807  if ( present(xdim) ) then
808  if( xdim == 'half' ) flag_half_x = .true.
809  endif
810 
811  flag_half_y = .false.
812  if ( present(ydim) ) then
813  if( ydim == 'half' ) flag_half_y = .true.
814  endif
815 
816  if ( flag_half_x .AND. flag_half_y ) then
817  dims(1) = 'lon_uv'
818  dims(2) = 'lat_uv'
819  elseif( flag_half_x ) then
820  dims(1) = 'lon_uy'
821  dims(2) = 'lat_uy'
822  elseif( flag_half_y ) then
823  dims(1) = 'lon_xv'
824  dims(2) = 'lat_xv'
825  else
826  dims(1) = 'lon'
827  dims(2) = 'lat'
828  endif
829 
830  elseif ( ndim == 3 ) then
831 
832  ! check half/full level for vertical/horizontal
833  flag_half_x = .false.
834 
835  if ( present(xdim) ) then
836  if( xdim == 'half' ) flag_half_x = .true.
837  endif
838 
839  flag_half_y = .false.
840  if ( present(ydim) ) then
841  if( ydim == 'half' ) flag_half_y = .true.
842  endif
843 
844  flag_half_z = .false.
845  if ( present(zdim) ) then
846  if( zdim == 'half' ) flag_half_z = .true.
847  endif
848 
849  if ( flag_half_x .AND. flag_half_y ) then
850  dims(1) = 'lon_uv'
851  dims(2) = 'lat_uv'
852  if ( flag_half_z ) then
853  dims(3) = 'height_uvw'
854  else
855  dims(3) = 'height_uvz'
856  endif
857  elseif( flag_half_x ) then
858  dims(1) = 'lon_uy'
859  dims(2) = 'lat_uy'
860  if ( flag_half_z ) then
861  dims(3) = 'height_uyw'
862  else
863  dims(3) = 'height_uyz'
864  endif
865  elseif( flag_half_y ) then
866  dims(1) = 'lon_xv'
867  dims(2) = 'lat_xv'
868  if ( flag_half_z ) then
869  dims(3) = 'height_xvw'
870  else
871  dims(3) = 'height_xvz'
872  endif
873  else
874  dims(1) = 'lon'
875  dims(2) = 'lat'
876  if ( flag_half_z ) then
877  dims(3) = 'height_xyw'
878  else
879  dims(3) = 'height'
880  endif
881  endif
882 
883  ! start and count will be used by PnetCDF I/O
884  start(3) = 1
885  count(3) = kmax
886 
887  if ( present(zdim) ) then
888  if ( zdim == 'land' ) then
889  dims(3) = 'lz'
890  count(3) = lkmax
891  atom = .false.
892  elseif( zdim == 'landhalf' ) then
893  dims(3) = 'lzh'
894  count(3) = lkmax
895  atom = .false.
896  elseif( zdim == 'urban' ) then
897  dims(3) = 'uz'
898  count(3) = ukmax
899  atom = .false.
900  elseif( zdim == 'urbanhalf' ) then
901  dims(3) = 'uzh'
902  count(3) = ukmax
903  atom = .false.
904  endif
905 
906  endif
907 
908  endif
909 
910  if ( ndim >= 2 ) then
911  ! start and count will be used by PnetCDF I/O
912  if ( hist_bnd ) then
913  start(1) = isgb
914  start(2) = jsgb
915  count(1) = imaxb
916  count(2) = jmaxb
917  else
918  ! for the case the shared-file contains no halos
919  start(1) = 1 + prc_2drank(prc_myrank,1) * imax ! no IHALO
920  start(2) = 1 + prc_2drank(prc_myrank,2) * jmax ! no JHALO
921  count(1) = imax
922  count(2) = jmax
923  end if
924  end if
925 
926 
927  if ( io_aggregate ) then ! user input parameter indicates to do PnetCDF I/O
928  comm = prc_local_comm_world
929  else
930  comm = mpi_comm_null
931  end if
932 
933  if ( atom ) then
934 
935  ! model coordinate (terrain following coordinate)
936  call historyaddvariable( nvariant1, & ! [OUT]
937  item, & ! [IN]
938  dims(1:ndim), & ! [IN]
939  desc, & ! [IN]
940  unit, & ! [IN]
941  nowstep, & ! [IN]
942  'model', & ! [IN]
943  start=start, & ! [IN]
944  count=count, & ! [IN]
945  comm=comm ) ! [IN]
946 
947  ! absolute height coordinate
948  dims(3) = 'z'
949  call historyaddvariable( nvariant2, & ! [OUT]
950  item, & ! [IN]
951  dims(1:ndim), & ! [IN]
952  desc, & ! [IN]
953  unit, & ! [IN]
954  nowstep, & ! [IN]
955  'z', & ! [IN]
956  start=start, & ! [IN]
957  count=count, & ! [IN]
958  comm=comm ) ! [IN]
959 
960  ! pressure coordinate
961  if ( hist_pres_nlayer > 0 ) then
962 
963  dims(3) = 'pressure'
964  call historyaddvariable( nvariant3, & ! [OUT]
965  item, & ! [IN]
966  dims(1:ndim), & ! [IN]
967  desc, & ! [IN]
968  unit, & ! [IN]
969  nowstep, & ! [IN]
970  'pressure', & ! [IN]
971  start=start, & ! [IN]
972  count=count, & ! [IN]
973  comm=comm ) ! [IN]
974 
975  else
976  nvariant3 = 0
977  endif
978 
979  else
980 
981  call historyaddvariable( nvariant1, & ! [OUT]
982  item, & ! [IN]
983  dims(1:ndim), & ! [IN]
984  desc, & ! [IN]
985  unit, & ! [IN]
986  nowstep, & ! [IN]
987  start=start, & ! [IN]
988  count=count, & ! [IN]
989  comm=comm ) ! [IN]
990 
991  nvariant2 = 0
992  nvariant3 = 0
993  endif
994 
995  if ( nvariant1 + nvariant2 + nvariant3 > 0 ) then
996  hist_item_count = hist_item_count + 1
997  itemid = hist_item_count
998  hist_item(itemid) = item
999 
1000  do v = 1, nvariant1
1001  hist_variant(itemid) = hist_variant(itemid) + 1
1002  hist_zcoord(itemid,hist_variant(itemid)) = i_model
1003  enddo
1004 
1005  do v = 1, nvariant2
1006  hist_variant(itemid) = hist_variant(itemid) + 1
1007  hist_zcoord(itemid,hist_variant(itemid)) = i_z
1008  enddo
1009 
1010  do v = 1, nvariant3
1011  hist_variant(itemid) = hist_variant(itemid) + 1
1012  hist_zcoord(itemid,hist_variant(itemid)) = i_pres
1013  enddo
1014  endif
1015 
1016  call prof_rapend ('FILE_O_NetCDF', 2)
1017 
1018  return
1019  end subroutine hist_reg
1020 
1021  !-----------------------------------------------------------------------------
1023  subroutine hist_query( &
1024  itemid, &
1025  answer )
1026  use gtool_history, only: &
1027  historyquery
1028  use scale_time, only: &
1029  time_nowstep
1030  implicit none
1031 
1032  integer, intent(in) :: itemid
1033  logical, intent(out) :: answer
1034  !---------------------------------------------------------------------------
1035 
1036  answer = .false.
1037 
1038  if( .NOT. enabled ) return
1039 
1040  if( itemid < 0 ) return
1041 
1042  call prof_rapstart('FILE_O_NetCDF', 2)
1043 
1044  call historyquery( hist_item(itemid), & ! [IN]
1045  time_nowstep, & ! [IN]
1046  answer ) ! [OUT]
1047 
1048  call prof_rapend ('FILE_O_NetCDF', 2)
1049 
1050  return
1051  end subroutine hist_query
1052 
1053  !-----------------------------------------------------------------------------
1055  subroutine hist_put_0d( &
1056  itemid, &
1057  var )
1058  use gtool_history, only: &
1059  historyput
1060  use scale_time, only: &
1061  time_nowstep
1062  implicit none
1063 
1064  integer, intent(in) :: itemid
1065  real(RP), intent(in) :: var
1066 
1067  integer :: n, v, id
1068  !---------------------------------------------------------------------------
1069 
1070  if( .NOT. enabled ) return
1071 
1072  if( itemid < 0 ) return
1073 
1074  call prof_rapstart('FILE_O_NetCDF', 2)
1075 
1076  id = 0
1077  do n = 1, itemid-1
1078  id = id + hist_variant(n)
1079  enddo
1080 
1081  do v = 1, hist_variant(itemid)
1082  id = id + 1
1083 
1084  call historyput( id, & ! [IN]
1085  time_nowstep, & ! [IN]
1086  var ) ! [IN]
1087  enddo
1088 
1089  call prof_rapend ('FILE_O_NetCDF', 2)
1090 
1091  return
1092  end subroutine hist_put_0d
1093 
1094  !-----------------------------------------------------------------------------
1096  subroutine hist_put_1d( &
1097  itemid, &
1098  var )
1099  use gtool_history, only: &
1100  historyput
1101  use scale_time, only: &
1102  time_nowstep
1103  implicit none
1104 
1105  integer, intent(in) :: itemid
1106  real(RP), intent(in) :: var(:)
1107 
1108  real(RP) :: var_trim(KMAX)
1109 
1110  integer :: n, v, id
1111  integer :: k
1112  !---------------------------------------------------------------------------
1113 
1114  if( .NOT. enabled ) return
1115 
1116  if( itemid < 0 ) return
1117 
1118  call prof_rapstart('FILE_O_NetCDF', 2)
1119 
1120  do k = 1, kmax
1121  var_trim(k) = var(ks+k-1)
1122  enddo
1123 
1124  id = 0
1125  do n = 1, itemid-1
1126  id = id + hist_variant(n)
1127  enddo
1128 
1129  do v = 1, hist_variant(itemid)
1130  id = id + 1
1131 
1132  call historyput( id, & ! [IN]
1133  time_nowstep, & ! [IN]
1134  var_trim(:) ) ! [IN]
1135  enddo
1136 
1137  call prof_rapend ('FILE_O_NetCDF', 2)
1138 
1139  return
1140  end subroutine hist_put_1d
1141 
1142  !-----------------------------------------------------------------------------
1144  subroutine hist_put_2d( &
1145  itemid, &
1146  var, &
1147  nohalo )
1148  use gtool_file, only: &
1149  rmiss
1150  use gtool_history, only: &
1151  historyput
1152  use scale_time, only: &
1153  time_nowstep
1154  implicit none
1155 
1156  integer, intent(in) :: itemid
1157  real(RP), intent(in) :: var(:,:)
1158  logical, intent(in), optional :: nohalo
1159 
1160  real(RP) :: var_trim(im*jm)
1161  logical :: nohalo_
1162 
1163  integer :: n, v, id
1164  integer :: i, j
1165  !---------------------------------------------------------------------------
1166 
1167  if( .NOT. enabled ) return
1168 
1169  if( itemid < 0 ) return
1170 
1171  call prof_rapstart('FILE_O_NetCDF', 2)
1172 
1173  do j = 1, jm
1174  do i = 1, im
1175  var_trim((j-1)*im+i) = var(ims+i-1,jms+j-1)
1176  enddo
1177  enddo
1178 
1179  nohalo_ = .false.
1180  if( present(nohalo) ) nohalo_ = nohalo
1181 
1182  if ( nohalo_ ) then
1183  ! W halo
1184  do j = 1, jm
1185  do i = 1, is-ims
1186  var_trim((j-1)*im+i) = rmiss
1187  enddo
1188  enddo
1189  ! E halo
1190  do j = 1, jm
1191  do i = ie-ims+2, ime-ims+1
1192  var_trim((j-1)*im+i) = rmiss
1193  enddo
1194  enddo
1195  ! S halo
1196  do j = 1, js-jms
1197  do i = 1, im
1198  var_trim((j-1)*im+i) = rmiss
1199  enddo
1200  enddo
1201  ! N halo
1202  do j = je-jms+2, jme-jms+1
1203  do i = 1, im
1204  var_trim((j-1)*im+i) = rmiss
1205  enddo
1206  enddo
1207  endif
1208 
1209  id = 0
1210  do n = 1, itemid-1
1211  id = id + hist_variant(n)
1212  enddo
1213 
1214  do v = 1, hist_variant(itemid)
1215  id = id + 1
1216 
1217  call historyput( id, & ! [IN]
1218  time_nowstep, & ! [IN]
1219  var_trim(:) ) ! [IN]
1220  enddo
1221 
1222  call prof_rapend ('FILE_O_NetCDF', 2)
1223 
1224  return
1225  end subroutine hist_put_2d
1226 
1227  !-----------------------------------------------------------------------------
1229  subroutine hist_put_3d( &
1230  itemid, &
1231  var, &
1232  xdim, &
1233  ydim, &
1234  zdim, &
1235  nohalo )
1236  use gtool_file, only: &
1237  rmiss
1238  use gtool_history, only: &
1239  historyput
1240  use scale_time, only: &
1241  time_nowstep
1242  use scale_interpolation, only: &
1246  implicit none
1247 
1248  integer, intent(in) :: itemid
1249  real(RP), intent(in) :: var(:,:,:)
1250  character(len=*), intent(in), optional :: xdim
1251  character(len=*), intent(in), optional :: ydim
1252  character(len=*), intent(in), optional :: zdim
1253  logical, intent(in), optional :: nohalo
1254 
1255  character(len=H_SHORT) :: xd, yd, zd
1256  integer :: isize, jsize, ksize
1257  integer :: istart, jstart, kstart
1258 
1259  real(RP) :: var_Z(KA ,IA,JA)
1260  real(RP) :: var_P(HIST_PRES_nlayer,IA,JA)
1261 
1262  real(RP) :: var_trim(km*im*jm)
1263  logical :: nohalo_
1264  integer :: s(3)
1265 
1266  integer :: n, v, id
1267  integer :: i, j, k
1268 
1269  intrinsic shape
1270  !---------------------------------------------------------------------------
1271 
1272  if( .NOT. enabled ) return
1273 
1274  if( itemid < 0 ) return
1275 
1276  call prof_rapstart('FILE_O_NetCDF', 2)
1277 
1278  xd = ''
1279  yd = ''
1280  zd = ''
1281  if( present(xdim) ) xd = xdim
1282  if( present(ydim) ) yd = ydim
1283  if( present(zdim) ) zd = zdim
1284 
1285  nohalo_ = .false.
1286  if( present(nohalo) ) nohalo_ = nohalo
1287 
1288  ! select dimension
1289  select case( xd )
1290  case('half')
1291  isize = im
1292  istart = ims
1293  case default
1294  isize = im
1295  istart = ims
1296  end select
1297 
1298  select case( yd )
1299  case('half')
1300  jsize = jm
1301  jstart = jms
1302  case default
1303  jsize = jm
1304  jstart = jms
1305  end select
1306 
1307  select case( zd )
1308  case('land')
1309  ksize = lkmax
1310  kstart = lks
1311  case('landhalf')
1312  ksize = lkmax
1313  kstart = lks
1314  case('urban')
1315  ksize = ukmax
1316  kstart = uks
1317  case('urbanhalf')
1318  ksize = ukmax
1319  kstart = uks
1320  case('half')
1321  ksize = kmax
1322  kstart = ks
1323  case default
1324  ksize = kmax
1325  kstart = ks
1326  end select
1327 
1328  s(:) = shape(var)
1329 
1330  id = 0
1331  do n = 1, itemid-1
1332  id = id + hist_variant(n)
1333  enddo
1334 
1335  do v = 1, hist_variant(itemid)
1336 
1337  if ( s(1) == ka &
1338  .AND. ksize == kmax &
1339  .AND. hist_zcoord(itemid,v) == i_z &
1340  .AND. interp_available ) then ! z*->z interpolation
1341 
1342  call prof_rapstart('FILE_O_interp', 2)
1343  call interp_vertical_xi2z( var(:,:,:), & ! [IN]
1344  var_z(:,:,:) ) ! [OUT]
1345  call prof_rapend ('FILE_O_interp', 2)
1346 
1347  do k = 1, ksize
1348  do j = 1, jsize
1349  do i = 1, isize
1350  var_trim((k-1)*jsize*isize+(j-1)*isize+i) = var_z(kstart+k-1,istart+i-1,jstart+j-1)
1351  enddo
1352  enddo
1353  enddo
1354 
1355  elseif( s(1) == ka &
1356  .AND. ksize == kmax &
1357  .AND. hist_zcoord(itemid,v) == i_pres ) then ! z*->p interpolation
1358 
1359  ksize = hist_pres_nlayer
1360 
1361  call prof_rapstart('FILE_O_interp', 2)
1362  call interp_vertical_xi2p( hist_pres_nlayer, & ! [IN]
1363  var(:,:,:), & ! [IN]
1364  var_p(:,:,:) ) ! [OUT]
1365  call prof_rapend ('FILE_O_interp', 2)
1366 
1367  do k = 1, ksize
1368  do j = 1, jsize
1369  do i = 1, isize
1370  var_trim((k-1)*jsize*isize+(j-1)*isize+i) = var_p(k,istart+i-1,jstart+j-1)
1371  enddo
1372  enddo
1373  enddo
1374 
1375  else ! no interpolation
1376 
1377  do k = 1, ksize
1378  do j = 1, jsize
1379  do i = 1, isize
1380  var_trim((k-1)*jsize*isize+(j-1)*isize+i) = var(kstart+k-1,istart+i-1,jstart+j-1)
1381  enddo
1382  enddo
1383  enddo
1384 
1385  endif
1386 
1387  if ( nohalo_ ) then
1388  ! W halo
1389  do k = 1, ksize
1390  do j = 1, jsize
1391  do i = 1, is-istart
1392  var_trim((k-1)*jsize*isize+(j-1)*isize+i) = rmiss
1393  enddo
1394  enddo
1395  enddo
1396  ! E halo
1397  do k = 1, ksize
1398  do j = 1, jsize
1399  do i = ie-istart+2, ime-istart+1
1400  var_trim((k-1)*jsize*isize+(j-1)*isize+i) = rmiss
1401  enddo
1402  enddo
1403  enddo
1404  ! S halo
1405  do k = 1, ksize
1406  do j = 1, js-jstart
1407  do i = 1, isize
1408  var_trim((k-1)*jsize*isize+(j-1)*isize+i) = rmiss
1409  enddo
1410  enddo
1411  enddo
1412  ! N halo
1413  do k = 1, ksize
1414  do j = je-jstart+2, jme-jstart+1
1415  do i = 1, isize
1416  var_trim((k-1)*jsize*isize+(j-1)*isize+i) = rmiss
1417  enddo
1418  enddo
1419  enddo
1420  endif
1421 
1422  id = id + 1
1423 
1424  call historyput( id, & ! [IN]
1425  time_nowstep, & ! [IN]
1426  var_trim(1:isize*jsize*ksize) ) ! [IN]
1427  enddo
1428 
1429  call prof_rapend ('FILE_O_NetCDF', 2)
1430 
1431  return
1432  end subroutine hist_put_3d
1433 
1434  !-----------------------------------------------------------------------------
1436  subroutine hist_in_0d( &
1437  var, &
1438  item, &
1439  desc, &
1440  unit )
1441  implicit none
1442 
1443  real(RP), intent(in) :: var
1444  character(len=*), intent(in) :: item
1445  character(len=*), intent(in) :: desc
1446  character(len=*), intent(in) :: unit
1447 
1448  integer :: itemid
1449  logical :: do_put
1450  !---------------------------------------------------------------------------
1451 
1452  if( .NOT. enabled ) return
1453 
1454  ! Check whether the item has been already registered
1455  call hist_reg ( itemid, & ! [OUT]
1456  item, & ! [IN]
1457  desc, & ! [IN]
1458  unit, & ! [IN]
1459  0 ) ! [IN]
1460 
1461  ! Check whether it is time to input the item
1462  call hist_query( itemid, & ! [IN]
1463  do_put ) ! [OUT]
1464 
1465  if ( do_put ) then
1466  call hist_put( itemid, & ! [IN]
1467  var ) ! [IN]
1468  endif
1469 
1470  return
1471  end subroutine hist_in_0d
1472 
1473  !-----------------------------------------------------------------------------
1475  subroutine hist_in_1d( &
1476  var, &
1477  item, &
1478  desc, &
1479  unit, &
1480  zdim )
1481  implicit none
1482 
1483  real(RP), intent(in) :: var(:)
1484  character(len=*), intent(in) :: item
1485  character(len=*), intent(in) :: desc
1486  character(len=*), intent(in) :: unit
1487  character(len=*), intent(in), optional :: zdim
1488 
1489  character(len=H_SHORT) :: zd
1490 
1491  integer :: itemid
1492  logical :: do_put
1493  !---------------------------------------------------------------------------
1494 
1495  if( .NOT. enabled ) return
1496 
1497  zd = ''
1498  if( present(zdim) ) zd = zdim
1499 
1500  ! Check whether the item has been already registered
1501  call hist_reg ( itemid, & ! [OUT]
1502  item, & ! [IN]
1503  desc, & ! [IN]
1504  unit, & ! [IN]
1505  1, & ! [IN]
1506  zdim=zd ) ! [IN]
1507 
1508  ! Check whether it is time to input the item
1509  call hist_query( itemid, & ! [IN]
1510  do_put ) ! [OUT]
1511 
1512  if ( do_put ) then
1513  call hist_put( itemid, & ! [IN]
1514  var ) ! [IN]
1515  endif
1516 
1517  return
1518  end subroutine hist_in_1d
1519 
1520  !-----------------------------------------------------------------------------
1522  subroutine hist_in_2d( &
1523  var, &
1524  item, &
1525  desc, &
1526  unit, &
1527  xdim, &
1528  ydim, &
1529  nohalo )
1530  implicit none
1531 
1532  real(RP), intent(in) :: var(:,:)
1533  character(len=*), intent(in) :: item
1534  character(len=*), intent(in) :: desc
1535  character(len=*), intent(in) :: unit
1536  character(len=*), intent(in), optional :: xdim
1537  character(len=*), intent(in), optional :: ydim
1538  logical, intent(in), optional :: nohalo
1539 
1540  character(len=H_SHORT) :: xd, yd
1541 
1542  integer :: itemid
1543  logical :: do_put
1544  !---------------------------------------------------------------------------
1545 
1546  if( .NOT. enabled ) return
1547 
1548  xd = ''
1549  yd = ''
1550  if( present(xdim) ) xd = xdim
1551  if( present(ydim) ) yd = ydim
1552 
1553  ! Check whether the item has been already registered
1554  call hist_reg ( itemid, & ! [OUT]
1555  item, & ! [IN]
1556  desc, & ! [IN]
1557  unit, & ! [IN]
1558  2, & ! [IN]
1559  xdim=xd, & ! [IN]
1560  ydim=yd ) ! [IN]
1561 
1562  ! Check whether it is time to input the item
1563  call hist_query( itemid, & ! [IN]
1564  do_put ) ! [OUT]
1565 
1566  if ( do_put ) then
1567  call hist_put( itemid, & ! [IN]
1568  var, & ! [IN]
1569  nohalo=nohalo ) ! [IN]
1570  endif
1571 
1572  return
1573  end subroutine hist_in_2d
1574 
1575  !-----------------------------------------------------------------------------
1577  subroutine hist_in_3d( &
1578  var, &
1579  item, &
1580  desc, &
1581  unit, &
1582  xdim, &
1583  ydim, &
1584  zdim, &
1585  nohalo )
1586  implicit none
1587 
1588  real(RP), intent(in) :: var(:,:,:)
1589  character(len=*), intent(in) :: item
1590  character(len=*), intent(in) :: desc
1591  character(len=*), intent(in) :: unit
1592  character(len=*), intent(in), optional :: xdim
1593  character(len=*), intent(in), optional :: ydim
1594  character(len=*), intent(in), optional :: zdim
1595  logical, intent(in), optional :: nohalo
1596 
1597  character(len=H_SHORT) :: xd, yd, zd
1598 
1599  integer :: itemid
1600  logical :: do_put
1601  !---------------------------------------------------------------------------
1602 
1603  if( .NOT. enabled ) return
1604 
1605  xd = ''
1606  yd = ''
1607  zd = ''
1608  if( present(xdim) ) xd = xdim
1609  if( present(ydim) ) yd = ydim
1610  if( present(zdim) ) zd = zdim
1611 
1612  ! Check whether the item has been already registered
1613  call hist_reg ( itemid, & ! [OUT]
1614  item, & ! [IN]
1615  desc, & ! [IN]
1616  unit, & ! [IN]
1617  3, & ! [IN]
1618  xdim=xd, & ! [IN]
1619  ydim=yd, & ! [IN]
1620  zdim=zd ) ! [IN]
1621 
1622  ! Check whether it is time to input the item
1623  call hist_query( itemid, & ! [IN]
1624  do_put ) ! [OUT]
1625 
1626  if ( do_put ) then
1627  call hist_put( itemid, & ! [IN]
1628  var, & ! [IN]
1629  xdim=xd, & ! [IN]
1630  ydim=yd, & ! [IN]
1631  zdim=zd, & ! [IN]
1632  nohalo=nohalo ) ! [IN]
1633  endif
1634 
1635  return
1636  end subroutine hist_in_3d
1637 
1638  !-----------------------------------------------------------------------------
1640  subroutine hist_get_1d( &
1641  var, &
1642  basename, &
1643  varname, &
1644  step, &
1645  allow_missing )
1646  use gtool_history, only: &
1647  historyget
1648  implicit none
1649 
1650  real(RP), intent(out) :: var(:)
1651  character(len=*), intent(in) :: basename
1652  character(len=*), intent(in) :: varname
1653  integer, intent(in) :: step
1654  logical, intent(in), optional :: allow_missing
1655 
1656  logical :: am
1657  !---------------------------------------------------------------------------
1658 
1659  call prof_rapstart('FILE_I_NetCDF', 2)
1660 
1661  am = .false.
1662  if( present(allow_missing) ) am = allow_missing
1663 
1664  call historyget( var(:), & ! [OUT]
1665  basename, & ! [IN]
1666  varname, & ! [IN]
1667  step, & ! [IN]
1668  allow_missing=am ) ! [IN]
1669 
1670  call prof_rapend ('FILE_I_NetCDF', 2)
1671 
1672  return
1673  end subroutine hist_get_1d
1674 
1675  !-----------------------------------------------------------------------------
1677  subroutine hist_get_2d( &
1678  var, &
1679  basename, &
1680  varname, &
1681  step, &
1682  allow_missing )
1683  use gtool_history, only: &
1684  historyget
1685  implicit none
1686 
1687  real(RP), intent(out) :: var(:,:)
1688  character(len=*), intent(in) :: basename
1689  character(len=*), intent(in) :: varname
1690  integer, intent(in) :: step
1691  logical, intent(in), optional :: allow_missing
1692 
1693  logical :: am
1694  !---------------------------------------------------------------------------
1695 
1696  call prof_rapstart('FILE_I_NetCDF', 2)
1697 
1698  am = .false.
1699  if( present(allow_missing) ) am = allow_missing
1700 
1701  call historyget( var(:,:), & ! [OUT]
1702  basename, & ! [IN]
1703  varname, & ! [IN]
1704  step, & ! [IN]
1705  allow_missing=am ) ! [IN]
1706 
1707  call prof_rapend ('FILE_I_NetCDF', 2)
1708 
1709  return
1710  end subroutine hist_get_2d
1711 
1712  !-----------------------------------------------------------------------------
1714  subroutine hist_get_3d( &
1715  var, &
1716  basename, &
1717  varname, &
1718  step, &
1719  allow_missing )
1720  use gtool_history, only: &
1721  historyget
1722  implicit none
1723 
1724  real(RP), intent(out) :: var(:,:,:)
1725  character(len=*), intent(in) :: basename
1726  character(len=*), intent(in) :: varname
1727  integer, intent(in) :: step
1728  logical, intent(in), optional :: allow_missing
1729 
1730  logical :: am
1731  !---------------------------------------------------------------------------
1732 
1733  call prof_rapstart('FILE_I_NetCDF', 2)
1734 
1735  am = .false.
1736  if( present(allow_missing) ) am = allow_missing
1737 
1738  call historyget( var(:,:,:), & ! [OUT]
1739  basename, & ! [IN]
1740  varname, & ! [IN]
1741  step, & ! [IN]
1742  allow_missing=am ) ! [IN]
1743 
1744  call prof_rapend ('FILE_I_NetCDF', 2)
1745 
1746  return
1747  end subroutine hist_get_3d
1748 
1749  !-----------------------------------------------------------------------------
1751  subroutine hist_write
1752  use gtool_history, only: &
1754  use scale_time, only: &
1755  time_nowstep
1756  implicit none
1757  !---------------------------------------------------------------------------
1758 
1759  call prof_rapstart('FILE_O_NetCDF', 2)
1760 
1761  call historywriteall( time_nowstep ) ![IN]
1762 
1763  call prof_rapend ('FILE_O_NetCDF', 2)
1764 
1765  return
1766  end subroutine hist_write
1767 
1768 end module scale_history
integer, public imax
of computational cells: x, local
integer, public time_nowstep
current step [number]
Definition: scale_time.F90:70
integer, public iagb
of computational grids
integer, public prc_num_x
x length of 2D processor topology
integer, public is
start point of inner domain: x, local
module GTOOL_FILE
Definition: gtool_file.f90:17
integer, public je
end point of inner domain: y, local
real(rp), dimension(:), allocatable, public grid_cyg
center coordinate [m]: y, global
integer, public prc_local_comm_world
local communicator
subroutine, public interp_vertical_xi2p(Kpres, var, var_P)
subroutine, public prc_mpistop
Abort MPI.
real(rp), dimension(:), allocatable, public grid_cbfyg
center buffer factor (0-1): y, global
integer, public jeb
real(dp), public time_nowms
subsecond part of current time [millisec]
Definition: scale_time.F90:66
real(dp), parameter, public rmiss
Definition: gtool_file.f90:150
real(rp), dimension(:), allocatable, public grid_cxg
center coordinate [m]: x, global
subroutine, public historyaddvariable(nregist, item, dims, desc, units, now_step, zcoord, options, start, count, comm)
real(rp), dimension(:), allocatable, public grid_fdy
y-length of grid(j+1) to grid(j) [m]
logical, public io_l
output log or not? (this process)
Definition: scale_stdio.F90:61
subroutine, public hist_switch(switch)
set switch
subroutine, public historycheck(existed, item, zcoord)
real(rp), dimension(:), allocatable, public grid_cdyg
center coordinate [m]: y, global
real(rp), dimension(:), allocatable, public grid_cz
center coordinate [m]: z, local=global
integer, public jsgb
start point of the inner domain: y, global
real(rp), dimension(:), allocatable, public grid_fxg
face coordinate [m]: x, global
real(rp), dimension(:), allocatable, public grid_fbfy
face buffer factor (0-1): y
module STDIO
Definition: scale_stdio.F90:12
real(dp), public time_startdaysec
second of start time [sec]
Definition: scale_time.F90:74
integer, public ke
end point of inner domain: z, local
real(rp), dimension(:,:,:), allocatable, public real_fz
geopotential height [m] (cell face )
integer, public imaxb
subroutine, public interp_vertical_xi2z(var, var_Z)
real(rp), dimension(:,:,:), allocatable, public real_cz
geopotential height [m] (cell center)
real(rp), public const_d2r
degree to radian
Definition: scale_const.F90:35
module GRID (cartesian) for land
real(rp), dimension(:), allocatable, public grid_cdxg
center coordinate [m]: x, global
subroutine hist_put_1d(itemid, var)
Put 1D data to history buffer.
integer, public jmaxb
module Gtool_History
real(rp), dimension(:), allocatable, public grid_fx
face coordinate [m]: x, local
real(rp), dimension(:), allocatable, public grid_lfz
face coordinate [m]: z, local=global
integer, public ieb
subroutine hist_in_0d(var, item, desc, unit)
Wrapper routine of HIST_reg+HIST_put 0D.
integer, public prc_num_y
y length of 2D processor topology
module grid index
real(rp), dimension(:), allocatable, public grid_ucdz
z-length of control volume [m]
logical, public io_nml
output log or not? (for namelist, this process)
Definition: scale_stdio.F90:62
integer, public isgb
start point of the inner domain: x, global
subroutine, public hist_query(itemid, answer)
Check time to putting data.
integer, public ia
of whole cells: x, local, with HALO
character(len=h_mid), public h_source
for file header
Definition: scale_stdio.F90:50
real(rp), dimension(:,:), allocatable, public real_latx
latitude at staggered point (uy) [rad,-pi,pi]
integer, public jag
of computational grids
module GRID (cartesian) for urban
real(rp), dimension(:), allocatable, public grid_fbfx
face buffer factor (0-1): x
real(rp), dimension(:), allocatable, public grid_fdz
z-length of grid(k+1) to grid(k) [m]
subroutine, public interp_update_pres(Kpres, PRES, SFC_PRES, Paxis)
module GRID (real space)
module LANDUSE
real(rp), dimension(:), allocatable, public grid_ufz
face coordinate [m]: z, local=global
real(dp), public time_dtsec
time interval of model [sec]
Definition: scale_time.F90:36
integer, public ka
of whole cells: z, local, with HALO
subroutine, public hist_setpres(PRES, SFC_PRES)
set interpolation factor for pressure coordinate
real(rp), dimension(:), allocatable, public grid_fbfxg
face buffer factor (0-1): x, global
subroutine, public hist_write
Flush history buffer to file.
real(rp), dimension(:), allocatable, public grid_fdyg
center coordinate [m]: y, global
subroutine, public interp_setup_pres(Kpres)
Reset random seed.
integer, public kmax
of computational cells: z, local
real(rp), dimension(:), allocatable, public grid_fz
face coordinate [m]: z, local=global
real(rp), dimension(:,:), allocatable, public real_lonxy
longitude at staggered point (uv) [rad,0-2pi]
subroutine hist_get_3d(var, basename, varname, step, allow_missing)
Get 3D data from file.
real(rp), dimension(:), allocatable, public grid_fbfz
face buffer factor (0-1): z
subroutine hist_get_2d(var, basename, varname, step, allow_missing)
Get 2D data from file.
integer, public js
start point of inner domain: y, local
integer, public iag
of computational grids
real(rp), dimension(:), allocatable, public grid_cbfx
center buffer factor (0-1): x
module TIME
Definition: scale_time.F90:15
module PROCESS
real(rp), dimension(:), allocatable, public grid_fbfyg
face buffer factor (0-1): y, global
real(rp), dimension(:), allocatable, public grid_ucz
center coordinate [m]: z, local=global
integer, public jagb
of computational grids
module CONSTANT
Definition: scale_const.F90:14
integer, parameter, public prc_masterrank
master process in each communicator
integer, public ks
start point of inner domain: z, local
real(rp), dimension(:), allocatable, public grid_cbfz
center buffer factor (0-1): z
integer, public prc_myrank
process num in local communicator
real(rp), dimension(:), allocatable, public grid_cx
center coordinate [m]: x, local
module GRID (cartesian)
subroutine, public prof_rapstart(rapname_base, level)
Start raptime.
Definition: scale_prof.F90:156
module RM PROCESS
subroutine, public hist_setup
Setup.
subroutine hist_put_0d(itemid, var)
Put 1D data to history buffer.
module profiler
Definition: scale_prof.F90:10
integer, public ie
end point of inner domain: x, local
integer, parameter, public h_short
Character length (short=16)
Definition: scale_stdio.F90:44
integer, public imaxg
of computational cells: x, global
real(rp), dimension(:), allocatable, public grid_lcdz
z-length of control volume [m]
character(len=h_mid), public h_institute
for file header
Definition: scale_stdio.F90:51
real(rp), dimension(:,:), allocatable, public real_lon
longitude [rad,0-2pi]
logical, public interp_available
topography exists & vertical interpolation has meaning?
logical, public io_aggregate
do parallel I/O through PnetCDF
Definition: scale_stdio.F90:66
real(rp), dimension(:), allocatable, public grid_fdxg
center coordinate [m]: x, global
module PRECISION
integer, dimension(:,:), allocatable, public prc_2drank
node index in 2D topology
real(rp), dimension(:,:), allocatable, public topo_zsfc
absolute ground height [m]
real(rp), dimension(:), allocatable, public grid_cdz
z-length of control volume [m]
subroutine, public historywriteall(step_now)
real(rp), dimension(:), allocatable, public grid_fdx
x-length of grid(i+1) to grid(i) [m]
real(rp), dimension(:), allocatable, public grid_lcz
center coordinate [m]: z, local=global
real(rp), dimension(:), allocatable, public grid_cdy
y-length of control volume [m]
real(rp), dimension(:), allocatable, public grid_cbfy
center buffer factor (0-1): y
module HISTORY
module TOPOGRAPHY
integer, public isb
integer, dimension(6), public time_nowdate
current time [YYYY MM DD HH MM SS]
Definition: scale_time.F90:65
module land grid index
integer, public io_fid_conf
Config file ID.
Definition: scale_stdio.F90:55
real(rp), dimension(:,:), allocatable, public real_lat
latitude [rad,-pi,pi]
real(rp), dimension(:,:), allocatable, public real_lony
longitude at staggered point (xv) [rad,0-2pi]
subroutine, public historyquery(item, step_now, answer)
subroutine, public historyinit(item_count, variant_count, isize, jsize, ksize, master, myrank, rankidx, title, source, institution, time_start, time_interval, time_units, time_since, default_basename, default_zcoord, default_tinterval, default_tunit, default_taverage, default_datatype, namelist_filename, namelist_fid)
integer, public io_fid_log
Log file ID.
Definition: scale_stdio.F90:56
subroutine, public hist_reg(itemid, item, desc, unit, ndim, xdim, ydim, zdim)
Register/Append variable to history file.
integer, public jsb
integer, public jmaxg
of computational cells: y, global
subroutine, public prof_rapend(rapname_base, level)
Save raptime.
Definition: scale_prof.F90:204
real(rp), dimension(:,:), allocatable, public real_latxy
latitude at staggered point (uv) [rad,-pi,pi]
real(rp), dimension(:,:), allocatable, public real_lonx
longitude at staggered point (uy) [rad,0-2pi]
real(rp), dimension(:), allocatable, public grid_cdx
x-length of control volume [m]
real(rp), dimension(:,:), allocatable, public landuse_frac_land
land fraction
integer, public jmax
of computational cells: y, local
integer, public io_fid_nml
Log file ID (only for output namelist)
Definition: scale_stdio.F90:57
real(rp), dimension(:), allocatable, public grid_fyg
face coordinate [m]: y, global
real(rp), dimension(:), allocatable, public grid_cy
center coordinate [m]: y, local
subroutine hist_put_2d(itemid, var, nohalo)
Put 2D data to history buffer.
module INTERPOLATION
real(rp), dimension(:), allocatable, public grid_cbfxg
center buffer factor (0-1): x, global
subroutine hist_put_3d(itemid, var, xdim, ydim, zdim, nohalo)
Put 3D data to history buffer.
module urban grid index
real(rp), dimension(:,:), allocatable, public real_laty
latitude at staggered point (xv) [rad,-pi,pi]
integer, public ja
of whole cells: y, local, with HALO
real(rp), dimension(:), allocatable, public grid_fy
face coordinate [m]: y, local