SCALE-RM
gtool_file.f90
Go to the documentation of this file.
1 !-------------------------------------------------------------------------------
13 !-------------------------------------------------------------------------------
14 ! Warning: This file was generated from gtool_file.f90.erb.
15 ! Do not edit this file.
16 !-------------------------------------------------------------------------------
17 module gtool_file
18  !-----------------------------------------------------------------------------
19  !
20  !++ Used modules
21  !
22  use gtool_file_h
23  use dc_log, only: &
24  log, &
25  log_lmsg
26  use dc_types, only: &
27  dp, &
28  sp
29  !-----------------------------------------------------------------------------
30  implicit none
31  private
32  !-----------------------------------------------------------------------------
33  !
34  !++ Public procedures
35  !
36  public :: fileopen
37  public :: filecreate
38  public :: filesetoption
39  public :: filedefaxis
40  public :: fileputaxis
41  public :: filewriteaxis
43  public :: fileputassociatedcoordinates
44  public :: filewriteassociatedcoordinates
45  public :: fileaddvariable
46  public :: filedefinevariable
47  public :: filesettattr
48  public :: filegetshape
49  public :: filegetdatainfo
50  public :: filegetalldatainfo
51  public :: fileread
52  public :: filewrite
53  public :: filegetglobalattribute
54  public :: filesetglobalattribute
55  public :: fileenddef
56  public :: fileflush
57  public :: fileclose
58  public :: filecloseall
59  public :: filemakefname
60  public :: fileattachbuffer
61  public :: filedetachbuffer
62 
63  interface filegetdatainfo
64  module procedure filegetdatainfofid
65  module procedure filegetdatainfofname
66  end interface filegetdatainfo
67  interface filegetalldatainfo
68  module procedure filegetalldatainfofid
69  module procedure filegetalldatainfofname
70  end interface filegetalldatainfo
71 
72  interface fileputaxis
73  module procedure fileputaxisrealsp
74  module procedure fileputaxisrealdp
75  end interface fileputaxis
76  interface filewriteaxis
77  module procedure filewriteaxisrealsp
78  module procedure filewriteaxisrealdp
79  end interface filewriteaxis
80  interface fileputassociatedcoordinates
81  module procedure fileput1dassociatedcoordinatesrealsp
82  module procedure fileput1dassociatedcoordinatesrealdp
83  module procedure fileput2dassociatedcoordinatesrealsp
84  module procedure fileput2dassociatedcoordinatesrealdp
85  module procedure fileput3dassociatedcoordinatesrealsp
86  module procedure fileput3dassociatedcoordinatesrealdp
87  module procedure fileput4dassociatedcoordinatesrealsp
88  module procedure fileput4dassociatedcoordinatesrealdp
89  end interface fileputassociatedcoordinates
90  interface filewriteassociatedcoordinates
91  module procedure filewrite1dassociatedcoordinatesrealsp
92  module procedure filewrite1dassociatedcoordinatesrealdp
93  module procedure filewrite2dassociatedcoordinatesrealsp
94  module procedure filewrite2dassociatedcoordinatesrealdp
95  module procedure filewrite3dassociatedcoordinatesrealsp
96  module procedure filewrite3dassociatedcoordinatesrealdp
97  module procedure filewrite4dassociatedcoordinatesrealsp
98  module procedure filewrite4dassociatedcoordinatesrealdp
99  end interface filewriteassociatedcoordinates
100  interface fileaddvariable
101  module procedure fileaddvariablenot
102  module procedure fileaddvariablerealsp
103  module procedure fileaddvariablerealdp
104  end interface fileaddvariable
105  interface fileread
106  module procedure fileread1drealsp
107  module procedure fileread1drealdp
108  module procedure fileread2drealsp
109  module procedure fileread2drealdp
110  module procedure fileread3drealsp
111  module procedure fileread3drealdp
112  module procedure fileread4drealsp
113  module procedure fileread4drealdp
114  module procedure filereadvar1drealsp
115  module procedure filereadvar1drealdp
116  module procedure filereadvar2drealsp
117  module procedure filereadvar2drealdp
118  module procedure filereadvar3drealsp
119  module procedure filereadvar3drealdp
120  module procedure filereadvar4drealsp
121  module procedure filereadvar4drealdp
122  end interface fileread
123  interface filewrite
124  module procedure filewrite1drealsp
125  module procedure filewrite1drealdp
126  module procedure filewrite2drealsp
127  module procedure filewrite2drealdp
128  module procedure filewrite3drealsp
129  module procedure filewrite3drealdp
130  module procedure filewrite4drealsp
131  module procedure filewrite4drealdp
132  end interface filewrite
133  interface filegetglobalattribute
134  module procedure filegetglobalattributetext
135  module procedure filegetglobalattributeint
136  module procedure filegetglobalattributefloat
137  module procedure filegetglobalattributedouble
138  end interface filegetglobalattribute
139  interface filesetglobalattribute
140  module procedure filesetglobalattributetext
141  module procedure filesetglobalattributeint
142  module procedure filesetglobalattributefloat
143  module procedure filesetglobalattributedouble
144  end interface filesetglobalattribute
145 
146  !-----------------------------------------------------------------------------
147  !
148  !++ Public parameters & variables
149  !
150  real(DP), parameter, public :: rmiss = -9.9999e+30
151  !-----------------------------------------------------------------------------
152  !
153  !++ Private procedures
154  !
155  !-----------------------------------------------------------------------------
156  !
157  !++ Private parameters & variables
158  !
159  integer, private, parameter :: file_nfile_max = 512 ! number limit of file
160  ! Keep consistency with "FILE_MAX" in gtool_netcdf.c
161  integer, private, parameter :: file_nvar_max = 40960 ! number limit of variables
162  ! Keep consistency with "VAR_MAX" in gtool_netcdf.c
163 
164  character(len=File_HLONG), private, save :: file_fname_list(file_nfile_max)
165  integer, private, save :: file_fid_list (file_nfile_max)
166  integer, private, save :: file_fid_count = 1
167  character(len=File_HLONG), private, save :: file_vname_list (file_nvar_max)
168  integer, private, save :: file_vid_fid_list(file_nvar_max)
169  integer, private, save :: file_vid_list (file_nvar_max)
170  integer, private, save :: file_vid_count = 1
171  integer, private, save :: mpi_myrank
172 
173  character(len=LOG_LMSG), private :: message
174 
175 contains
176  !-----------------------------------------------------------------------------
177  subroutine filecreate( &
178  fid, & ! (out)
179  existed, & ! (out)
180  basename, & ! (in)
181  title, & ! (in)
182  source, & ! (in)
183  institution, & ! (in)
184  master, & ! (in)
185  myrank, & ! (in)
186  rankidx, & ! (in)
187  single, & ! (in) optional
188  time_units, & ! (in) optional
189  append, & ! (in) optional
190  comm ) ! (in) optional
191  use mpi, only : mpi_comm_null
192  implicit none
193 
194  integer, intent(out) :: fid
195  logical, intent(out) :: existed
196  character(len=*), intent( in) :: basename
197  character(len=*), intent( in) :: title
198  character(len=*), intent( in) :: source
199  character(len=*), intent( in) :: institution
200  integer, intent( in) :: master
201  integer, intent( in) :: myrank
202  integer, intent( in) :: rankidx(:)
203  character(len=*), intent( in), optional :: time_units
204  logical, intent( in), optional :: single
205  logical, intent( in), optional :: append
206  integer, intent( in), optional :: comm ! MPI communicator
207 
208  character(len=File_HMID) :: time_units_
209  logical :: single_
210  integer :: mode
211  integer :: error
212 
213  intrinsic size
214 
215  if ( present(time_units) ) then
216  time_units_ = time_units
217  else
218  time_units_ = 'seconds'
219  end if
220 
221  mpi_myrank = myrank
222 
223  if ( present(single) ) then
224  if ( single .and. (myrank .ne. master) ) return
225  single_ = single
226  else
227  single_ = .false.
228  endif
229 
230  mode = file_fwrite
231  if ( present(append) ) then
232  if ( append ) mode = file_fappend
233  end if
234 
235  call filegetfid( &
236  fid, & ! (out)
237  existed, & ! (out)
238  basename, & ! (in)
239  mode, & ! (in)
240  single_, & ! (in)
241  comm & ! (in)
242  )
243 
244  if ( existed ) return
245 
246  !--- append package header to the file
247  call filesetglobalattribute( fid, & ! (in)
248  "title", title ) ! (in)
249  call filesetglobalattribute( fid, & ! (in)
250  "source", source ) ! (in)
251  call filesetglobalattribute( fid, & ! (in)
252  "institution", institution ) ! (in)
253 
254  if ( .NOT. present(comm) .OR. comm == mpi_comm_null ) then
255  ! for shared-file parallel I/O, skip attributes related to MPI processes
256  call filesetglobalattribute( fid, & ! (in)
257  "myrank", (/myrank/) ) ! (in)
258  call filesetglobalattribute( fid, & ! (in)
259  "rankidx", rankidx ) ! (in)
260  end if
261 
262  call file_set_tunits( fid, & ! (in)
263  time_units_, & ! (in)
264  error ) ! (out)
265  if ( error /= success_code ) then
266  call log('E', 'xxx failed to set time units')
267  end if
268 
269  return
270  end subroutine filecreate
271 
272  !-----------------------------------------------------------------------------
273  subroutine filegetglobalattributetext( &
274  fid, & ! (in)
275  key, & ! (in)
276  val & ! (out)
277  )
278  integer, intent(in) :: fid
279  character(len=*), intent(in) :: key
280  character(len=*), intent(out) :: val
281 
282  integer error
283 
284  intrinsic size
285 
286  call file_get_global_attribute_text( & ! (in)
287  fid, key, & ! (in)
288  val, error ) ! (out)
289  if ( error /= success_code ) then
290  call log('E', 'xxx failed to get text global attribute: '//trim(key))
291  end if
292 
293  return
294  end subroutine filegetglobalattributetext
295 
296  !-----------------------------------------------------------------------------
297  subroutine filegetglobalattributeint( &
298  fid, & ! (in)
299  key, & ! (in)
300  val & ! (out)
301  )
302  integer, intent(in) :: fid
303  character(len=*), intent(in) :: key
304  integer, intent(out) :: val(:)
305 
306  integer error
307 
308  intrinsic size
309 
310  call file_get_global_attribute_int( & ! (in)
311  fid, key, size(val), & ! (in)
312  val, error ) ! (out)
313  if ( error /= success_code ) then
314  call log('E', 'xxx failed to get integer global attribute: '//trim(key))
315  end if
316 
317  return
318  end subroutine filegetglobalattributeint
319 
320  !-----------------------------------------------------------------------------
321  subroutine filegetglobalattributefloat( &
322  fid, & ! (in)
323  key, & ! (in)
324  val & ! (out)
325  )
326  integer, intent(in) :: fid
327  character(len=*), intent(in) :: key
328  real(SP), intent(out) :: val(:)
329 
330  integer error
331 
332  intrinsic size
333 
334  call file_get_global_attribute_float( & ! (in)
335  fid, key, size(val), & ! (in)
336  val, error ) ! (out)
337  if ( error /= success_code ) then
338  call log('E', 'xxx failed to get float global attribute: '//trim(key))
339  end if
340 
341  return
342  end subroutine filegetglobalattributefloat
343 
344  !-----------------------------------------------------------------------------
345  subroutine filegetglobalattributedouble( &
346  fid, & ! (in)
347  key, & ! (in)
348  val & ! (out)
349  )
350  integer, intent(in) :: fid
351  character(len=*), intent(in) :: key
352  real(DP), intent(out) :: val(:)
353 
354  integer error
355 
356  intrinsic size
357 
358  call file_get_global_attribute_double( & ! (in)
359  fid, key, size(val), & ! (in)
360  val, error ) ! (out)
361  if ( error /= success_code ) then
362  call log('E', 'xxx failed to get double global attribute: '//trim(key))
363  end if
364 
365  return
366  end subroutine filegetglobalattributedouble
367 
368 
369  !-----------------------------------------------------------------------------
370  subroutine filesetglobalattributetext( &
371  fid, & ! (in)
372  key, & ! (in)
373  val & ! (in)
374  )
375  integer, intent(in) :: fid
376  character(len=*), intent(in) :: key
377  character(len=*), intent(in) :: val
378 
379  integer error
380 
381  call file_set_global_attribute_text( fid, & ! (in)
382  key, val, & ! (in)
383  error ) ! (out)
384  if ( error /= success_code ) then
385  call log('E', 'xxx failed to set text global attribute: '//trim(key))
386  end if
387 
388  return
389  end subroutine filesetglobalattributetext
390 
391  !-----------------------------------------------------------------------------
392  subroutine filesetglobalattributeint( &
393  fid, & ! (in)
394  key, & ! (in)
395  val & ! (in)
396  )
397  integer, intent(in) :: fid
398  character(len=*), intent(in) :: key
399  integer, intent(in) :: val(:)
400 
401  integer error
402 
403  intrinsic size
404 
405  call file_set_global_attribute_int( fid, & ! (in)
406  key, val, size(val), & ! (in)
407  error ) ! (out)
408  if ( error /= success_code ) then
409  call log('E', 'xxx failed to set integer global attribute: '//trim(key))
410  end if
411 
412  return
413  end subroutine filesetglobalattributeint
414 
415  !-----------------------------------------------------------------------------
416  subroutine filesetglobalattributefloat( &
417  fid, & ! (in)
418  key, & ! (in)
419  val & ! (in)
420  )
421  integer, intent(in) :: fid
422  character(len=*), intent(in) :: key
423  real(SP), intent(in) :: val(:)
424 
425  integer error
426 
427  intrinsic size
428 
429  call file_set_global_attribute_float( fid, & ! (in)
430  key, val, size(val), & ! (in)
431  error ) ! (out)
432  if ( error /= success_code ) then
433  call log('E', 'xxx failed to set float global attribute: '//trim(key))
434  end if
435 
436  return
437  end subroutine filesetglobalattributefloat
438 
439  !-----------------------------------------------------------------------------
440  subroutine filesetglobalattributedouble( &
441  fid, & ! (in)
442  key, & ! (in)
443  val & ! (in)
444  )
445  integer, intent(in) :: fid
446  character(len=*), intent(in) :: key
447  real(DP), intent(in) :: val(:)
448 
449  integer error
450 
451  intrinsic size
452 
453  call file_set_global_attribute_double( fid, & ! (in)
454  key, val, size(val), & ! (in)
455  error ) ! (out)
456  if ( error /= success_code ) then
457  call log('E', 'xxx failed to set double global attribute: '//trim(key))
458  end if
459 
460  return
461  end subroutine filesetglobalattributedouble
462 
463  !-----------------------------------------------------------------------------
464  subroutine filesetoption( &
465  fid, & ! (in)
466  filetype, & ! (in)
467  key, & ! (in)
468  val & ! (in)
469  )
470  integer, intent(in) :: fid
471  character(len=*), intent(in) :: filetype
472  character(len=*), intent(in) :: key
473  character(len=*), intent(in) :: val
474 
475  integer error
476 
477  call file_set_option( fid, filetype, key, val, & ! (in)
478  error ) ! (out)
479  if ( error /= success_code ) then
480  call log('E', 'xxx failed to set option')
481  end if
482 
483  return
484  end subroutine filesetoption
485 
486  !-----------------------------------------------------------------------------
487  subroutine fileopen( &
488  fid, & ! (out)
489  basename, & ! (in)
490  mode, & ! (in)
491  single, & ! (in) optional
492  comm, & ! (in) optional
493  myrank & ! (in) optional
494  )
495  implicit none
496 
497  integer, intent(out) :: fid
498  character(len=*), intent( in) :: basename
499  integer, intent( in) :: mode
500  logical, intent( in), optional :: single
501  integer, intent( in), optional :: comm
502  integer, intent( in), optional :: myrank
503 
504  logical :: existed
505  logical :: single_
506 
507  single_ = .false.
508 
509  if ( present(single) ) single_ = single
510  if ( present(myrank) ) mpi_myrank = myrank
511 
512  call filegetfid( fid, existed, & ! (out)
513  basename, mode, single_, comm ) ! (in)
514 
515  return
516  end subroutine fileopen
517 
518  !-----------------------------------------------------------------------------
519  ! interface FilePutAxis
520  !-----------------------------------------------------------------------------
521  subroutine fileputaxisrealsp( &
522  fid, & ! (in)
523  name, & ! (in)
524  desc, & ! (in)
525  units, & ! (in)
526  dim_name, & ! (in)
527  dtype, & ! (in)
528  val ) ! (in)
529  integer, intent(in) :: fid
530  character(len=*), intent(in) :: name
531  character(len=*), intent(in) :: desc
532  character(len=*), intent(in) :: units
533  character(len=*), intent(in) :: dim_name
534  integer, intent(in) :: dtype
535  real(SP), intent(in) :: val(:)
536 
537  integer error
538  intrinsic size
539 
540  call file_put_axis( fid, name, desc, units, dim_name, dtype, val, size(val), sp, & ! (in)
541  error ) ! (out)
542  if ( error /= success_code .and. error /= already_existed_code ) then
543  call log('E', 'xxx failed to put axis')
544  end if
545 
546  return
547  end subroutine fileputaxisrealsp
548  subroutine fileputaxisrealdp( &
549  fid, & ! (in)
550  name, & ! (in)
551  desc, & ! (in)
552  units, & ! (in)
553  dim_name, & ! (in)
554  dtype, & ! (in)
555  val ) ! (in)
556  integer, intent(in) :: fid
557  character(len=*), intent(in) :: name
558  character(len=*), intent(in) :: desc
559  character(len=*), intent(in) :: units
560  character(len=*), intent(in) :: dim_name
561  integer, intent(in) :: dtype
562  real(DP), intent(in) :: val(:)
563 
564  integer error
565  intrinsic size
566 
567  call file_put_axis( fid, name, desc, units, dim_name, dtype, val, size(val), dp, & ! (in)
568  error ) ! (out)
569  if ( error /= success_code .and. error /= already_existed_code ) then
570  call log('E', 'xxx failed to put axis')
571  end if
572 
573  return
574  end subroutine fileputaxisrealdp
575 
576  subroutine filedefaxis( &
577  fid, & ! (in)
578  name, & ! (in)
579  desc, & ! (in)
580  units, & ! (in)
581  dim_name, & ! (in)
582  dtype, & ! (in)
583  dim_size ) ! (in)
584  integer, intent(in) :: fid
585  character(len=*), intent(in) :: name
586  character(len=*), intent(in) :: desc
587  character(len=*), intent(in) :: units
588  character(len=*), intent(in) :: dim_name
589  integer, intent(in) :: dtype
590  integer, intent(in) :: dim_size
591 
592  integer error
593  intrinsic size
594 
595  call file_def_axis( fid, name, desc, units, dim_name, dtype, dim_size, & ! (in)
596  error ) ! (out)
597  if ( error /= success_code .and. error /= already_existed_code ) then
598  call log('E', 'xxx failed to define axis')
599  end if
600 
601  return
602  end subroutine filedefaxis
603 
604  !-----------------------------------------------------------------------------
605  ! interface FileWriteAxis
606  !-----------------------------------------------------------------------------
607  subroutine filewriteaxisrealsp( &
608  fid, & ! (in)
609  name, & ! (in)
610  val, & ! (in)
611  start ) ! (in)
612  integer, intent(in) :: fid
613  character(len=*), intent(in) :: name
614  real(SP), intent(in) :: val(:)
615  integer, intent(in), optional :: start(:)
616 
617  integer error
618  intrinsic shape
619 
620  if ( present(start) ) then
621  call file_write_axis( fid, name, val, sp, start, shape(val), & ! (in)
622  error ) ! (out)
623  else
624  call file_write_axis( fid, name, val, sp, (/1/), shape(val), & ! (in)
625  error ) ! (out)
626  end if
627  if ( error /= success_code ) then
628  call log('E', 'xxx failed to write axis')
629  end if
630 
631  return
632  end subroutine filewriteaxisrealsp
633  subroutine filewriteaxisrealdp( &
634  fid, & ! (in)
635  name, & ! (in)
636  val, & ! (in)
637  start ) ! (in)
638  integer, intent(in) :: fid
639  character(len=*), intent(in) :: name
640  real(DP), intent(in) :: val(:)
641  integer, intent(in), optional :: start(:)
642 
643  integer error
644  intrinsic shape
645 
646  if ( present(start) ) then
647  call file_write_axis( fid, name, val, dp, start, shape(val), & ! (in)
648  error ) ! (out)
649  else
650  call file_write_axis( fid, name, val, dp, (/1/), shape(val), & ! (in)
651  error ) ! (out)
652  end if
653  if ( error /= success_code ) then
654  call log('E', 'xxx failed to write axis')
655  end if
656 
657  return
658  end subroutine filewriteaxisrealdp
659 
660  !-----------------------------------------------------------------------------
661  ! interface FilePutAssociatedCoordinates
662  !-----------------------------------------------------------------------------
663  subroutine fileput1dassociatedcoordinatesrealsp( &
664  fid, & ! (in)
665  name, & ! (in)
666  desc, & ! (in)
667  units, & ! (in)
668  dim_names, & ! (in)
669  dtype, & ! (in)
670  val ) ! (in)
671  integer, intent(in) :: fid
672  character(len=*), intent(in) :: name
673  character(len=*), intent(in) :: desc
674  character(len=*), intent(in) :: units
675  character(len=*), intent(in) :: dim_names(:)
676  integer, intent(in) :: dtype
677  real(SP), intent(in) :: val(:)
678 
679  integer error
680  intrinsic size
681 
682  call file_put_associated_coordinates( fid, & ! (in)
683  name, desc, units, dim_names, size(dim_names), dtype, & ! (in)
684  val, sp, & ! (in)
685  error ) ! (out)
686  if ( error /= success_code .and. error /= already_existed_code ) then
687  call log('E', 'xxx failed to put associated coordinates')
688  end if
689 
690  return
691  end subroutine fileput1dassociatedcoordinatesrealsp
692  subroutine fileput1dassociatedcoordinatesrealdp( &
693  fid, & ! (in)
694  name, & ! (in)
695  desc, & ! (in)
696  units, & ! (in)
697  dim_names, & ! (in)
698  dtype, & ! (in)
699  val ) ! (in)
700  integer, intent(in) :: fid
701  character(len=*), intent(in) :: name
702  character(len=*), intent(in) :: desc
703  character(len=*), intent(in) :: units
704  character(len=*), intent(in) :: dim_names(:)
705  integer, intent(in) :: dtype
706  real(DP), intent(in) :: val(:)
707 
708  integer error
709  intrinsic size
710 
711  call file_put_associated_coordinates( fid, & ! (in)
712  name, desc, units, dim_names, size(dim_names), dtype, & ! (in)
713  val, dp, & ! (in)
714  error ) ! (out)
715  if ( error /= success_code .and. error /= already_existed_code ) then
716  call log('E', 'xxx failed to put associated coordinates')
717  end if
718 
719  return
720  end subroutine fileput1dassociatedcoordinatesrealdp
721  subroutine fileput2dassociatedcoordinatesrealsp( &
722  fid, & ! (in)
723  name, & ! (in)
724  desc, & ! (in)
725  units, & ! (in)
726  dim_names, & ! (in)
727  dtype, & ! (in)
728  val ) ! (in)
729  integer, intent(in) :: fid
730  character(len=*), intent(in) :: name
731  character(len=*), intent(in) :: desc
732  character(len=*), intent(in) :: units
733  character(len=*), intent(in) :: dim_names(:)
734  integer, intent(in) :: dtype
735  real(SP), intent(in) :: val(:,:)
736 
737  integer error
738  intrinsic size
739 
740  call file_put_associated_coordinates( fid, & ! (in)
741  name, desc, units, dim_names, size(dim_names), dtype, & ! (in)
742  val, sp, & ! (in)
743  error ) ! (out)
744  if ( error /= success_code .and. error /= already_existed_code ) then
745  call log('E', 'xxx failed to put associated coordinates')
746  end if
747 
748  return
749  end subroutine fileput2dassociatedcoordinatesrealsp
750  subroutine fileput2dassociatedcoordinatesrealdp( &
751  fid, & ! (in)
752  name, & ! (in)
753  desc, & ! (in)
754  units, & ! (in)
755  dim_names, & ! (in)
756  dtype, & ! (in)
757  val ) ! (in)
758  integer, intent(in) :: fid
759  character(len=*), intent(in) :: name
760  character(len=*), intent(in) :: desc
761  character(len=*), intent(in) :: units
762  character(len=*), intent(in) :: dim_names(:)
763  integer, intent(in) :: dtype
764  real(DP), intent(in) :: val(:,:)
765 
766  integer error
767  intrinsic size
768 
769  call file_put_associated_coordinates( fid, & ! (in)
770  name, desc, units, dim_names, size(dim_names), dtype, & ! (in)
771  val, dp, & ! (in)
772  error ) ! (out)
773  if ( error /= success_code .and. error /= already_existed_code ) then
774  call log('E', 'xxx failed to put associated coordinates')
775  end if
776 
777  return
778  end subroutine fileput2dassociatedcoordinatesrealdp
779  subroutine fileput3dassociatedcoordinatesrealsp( &
780  fid, & ! (in)
781  name, & ! (in)
782  desc, & ! (in)
783  units, & ! (in)
784  dim_names, & ! (in)
785  dtype, & ! (in)
786  val ) ! (in)
787  integer, intent(in) :: fid
788  character(len=*), intent(in) :: name
789  character(len=*), intent(in) :: desc
790  character(len=*), intent(in) :: units
791  character(len=*), intent(in) :: dim_names(:)
792  integer, intent(in) :: dtype
793  real(SP), intent(in) :: val(:,:,:)
794 
795  integer error
796  intrinsic size
797 
798  call file_put_associated_coordinates( fid, & ! (in)
799  name, desc, units, dim_names, size(dim_names), dtype, & ! (in)
800  val, sp, & ! (in)
801  error ) ! (out)
802  if ( error /= success_code .and. error /= already_existed_code ) then
803  call log('E', 'xxx failed to put associated coordinates')
804  end if
805 
806  return
807  end subroutine fileput3dassociatedcoordinatesrealsp
808  subroutine fileput3dassociatedcoordinatesrealdp( &
809  fid, & ! (in)
810  name, & ! (in)
811  desc, & ! (in)
812  units, & ! (in)
813  dim_names, & ! (in)
814  dtype, & ! (in)
815  val ) ! (in)
816  integer, intent(in) :: fid
817  character(len=*), intent(in) :: name
818  character(len=*), intent(in) :: desc
819  character(len=*), intent(in) :: units
820  character(len=*), intent(in) :: dim_names(:)
821  integer, intent(in) :: dtype
822  real(DP), intent(in) :: val(:,:,:)
823 
824  integer error
825  intrinsic size
826 
827  call file_put_associated_coordinates( fid, & ! (in)
828  name, desc, units, dim_names, size(dim_names), dtype, & ! (in)
829  val, dp, & ! (in)
830  error ) ! (out)
831  if ( error /= success_code .and. error /= already_existed_code ) then
832  call log('E', 'xxx failed to put associated coordinates')
833  end if
834 
835  return
836  end subroutine fileput3dassociatedcoordinatesrealdp
837  subroutine fileput4dassociatedcoordinatesrealsp( &
838  fid, & ! (in)
839  name, & ! (in)
840  desc, & ! (in)
841  units, & ! (in)
842  dim_names, & ! (in)
843  dtype, & ! (in)
844  val ) ! (in)
845  integer, intent(in) :: fid
846  character(len=*), intent(in) :: name
847  character(len=*), intent(in) :: desc
848  character(len=*), intent(in) :: units
849  character(len=*), intent(in) :: dim_names(:)
850  integer, intent(in) :: dtype
851  real(SP), intent(in) :: val(:,:,:,:)
852 
853  integer error
854  intrinsic size
855 
856  call file_put_associated_coordinates( fid, & ! (in)
857  name, desc, units, dim_names, size(dim_names), dtype, & ! (in)
858  val, sp, & ! (in)
859  error ) ! (out)
860  if ( error /= success_code .and. error /= already_existed_code ) then
861  call log('E', 'xxx failed to put associated coordinates')
862  end if
863 
864  return
865  end subroutine fileput4dassociatedcoordinatesrealsp
866  subroutine fileput4dassociatedcoordinatesrealdp( &
867  fid, & ! (in)
868  name, & ! (in)
869  desc, & ! (in)
870  units, & ! (in)
871  dim_names, & ! (in)
872  dtype, & ! (in)
873  val ) ! (in)
874  integer, intent(in) :: fid
875  character(len=*), intent(in) :: name
876  character(len=*), intent(in) :: desc
877  character(len=*), intent(in) :: units
878  character(len=*), intent(in) :: dim_names(:)
879  integer, intent(in) :: dtype
880  real(DP), intent(in) :: val(:,:,:,:)
881 
882  integer error
883  intrinsic size
884 
885  call file_put_associated_coordinates( fid, & ! (in)
886  name, desc, units, dim_names, size(dim_names), dtype, & ! (in)
887  val, dp, & ! (in)
888  error ) ! (out)
889  if ( error /= success_code .and. error /= already_existed_code ) then
890  call log('E', 'xxx failed to put associated coordinates')
891  end if
892 
893  return
894  end subroutine fileput4dassociatedcoordinatesrealdp
895 
896  subroutine filedefassociatedcoordinates( &
897  fid, & ! (in)
898  name, & ! (in)
899  desc, & ! (in)
900  units, & ! (in)
901  dim_names, & ! (in)
902  dtype ) ! (in)
903  integer, intent(in) :: fid
904  character(len=*), intent(in) :: name
905  character(len=*), intent(in) :: desc
906  character(len=*), intent(in) :: units
907  character(len=*), intent(in) :: dim_names(:)
908  integer, intent(in) :: dtype
909 
910  integer error
911  intrinsic size
912 
913  call file_def_associated_coordinates( fid, & ! (in)
914  name, desc, units, dim_names, size(dim_names), dtype, & ! (in)
915  error ) ! (out)
916  if ( error /= success_code .and. error /= already_existed_code ) then
917  call log('E', 'xxx failed to put associated coordinates')
918  end if
919 
920  return
921  end subroutine filedefassociatedcoordinates
922 
923  !-----------------------------------------------------------------------------
924  ! interface FileWriteAssociatedCoordinates
925  !-----------------------------------------------------------------------------
926  subroutine filewrite1dassociatedcoordinatesrealsp( &
927  fid, & ! (in)
928  name, & ! (in)
929  val, & ! (in)
930  start, & ! (in)
931  count, & ! (in)
932  ndims ) ! (in)
933  integer, intent(in) :: fid
934  character(len=*), intent(in) :: name
935  real(SP), intent(in) :: val(:)
936  integer, intent(in), optional :: start(:)
937  integer, intent(in), optional :: count(:) ! in case val has been reshaped
938  integer, intent(in), optional :: ndims ! in case val has been reshaped
939 
940  integer error
941  intrinsic size, shape
942 
943  if ( present(ndims) ) then
944  ! Note this is called for history coordinates which have been reshaped
945  ! from 2D/3D into 1D array. In this case, start and count must be also present
946  call file_write_associated_coordinates( fid, name, val, sp, & ! (in)
947  ndims, start, count, & ! (in)
948  error ) ! (out)
949  else if ( present(start) ) then
950  ! Note this is called for restart coordinates
951  call file_write_associated_coordinates( fid, name, val, sp, & ! (in)
952  1, start, shape(val), & ! (in)
953  error ) ! (out)
954  else
955  ! Note this is for the one-file-per-process I/O method
956  call file_write_associated_coordinates( fid, name, val, sp, & ! (in)
957  1, (/1/), shape(val), & ! (in)
958  error ) ! (out)
959  end if
960  if ( error /= success_code .and. error /= already_existed_code ) then
961  call log('E', 'xxx failed to put associated coordinates: '//trim(name))
962  end if
963 
964  return
965  end subroutine filewrite1dassociatedcoordinatesrealsp
966  subroutine filewrite1dassociatedcoordinatesrealdp( &
967  fid, & ! (in)
968  name, & ! (in)
969  val, & ! (in)
970  start, & ! (in)
971  count, & ! (in)
972  ndims ) ! (in)
973  integer, intent(in) :: fid
974  character(len=*), intent(in) :: name
975  real(DP), intent(in) :: val(:)
976  integer, intent(in), optional :: start(:)
977  integer, intent(in), optional :: count(:) ! in case val has been reshaped
978  integer, intent(in), optional :: ndims ! in case val has been reshaped
979 
980  integer error
981  intrinsic size, shape
982 
983  if ( present(ndims) ) then
984  ! Note this is called for history coordinates which have been reshaped
985  ! from 2D/3D into 1D array. In this case, start and count must be also present
986  call file_write_associated_coordinates( fid, name, val, dp, & ! (in)
987  ndims, start, count, & ! (in)
988  error ) ! (out)
989  else if ( present(start) ) then
990  ! Note this is called for restart coordinates
991  call file_write_associated_coordinates( fid, name, val, dp, & ! (in)
992  1, start, shape(val), & ! (in)
993  error ) ! (out)
994  else
995  ! Note this is for the one-file-per-process I/O method
996  call file_write_associated_coordinates( fid, name, val, dp, & ! (in)
997  1, (/1/), shape(val), & ! (in)
998  error ) ! (out)
999  end if
1000  if ( error /= success_code .and. error /= already_existed_code ) then
1001  call log('E', 'xxx failed to put associated coordinates: '//trim(name))
1002  end if
1003 
1004  return
1005  end subroutine filewrite1dassociatedcoordinatesrealdp
1006  subroutine filewrite2dassociatedcoordinatesrealsp( &
1007  fid, & ! (in)
1008  name, & ! (in)
1009  val, & ! (in)
1010  start, & ! (in)
1011  count, & ! (in)
1012  ndims ) ! (in)
1013  integer, intent(in) :: fid
1014  character(len=*), intent(in) :: name
1015  real(SP), intent(in) :: val(:,:)
1016  integer, intent(in), optional :: start(:)
1017  integer, intent(in), optional :: count(:) ! in case val has been reshaped
1018  integer, intent(in), optional :: ndims ! in case val has been reshaped
1019 
1020  integer error
1021  intrinsic size, shape
1022 
1023  if ( present(ndims) ) then
1024  ! Note this is called for history coordinates which have been reshaped
1025  ! from 2D/3D into 1D array. In this case, start and count must be also present
1026  call file_write_associated_coordinates( fid, name, val, sp, & ! (in)
1027  ndims, start, count, & ! (in)
1028  error ) ! (out)
1029  else if ( present(start) ) then
1030  ! Note this is called for restart coordinates
1031  call file_write_associated_coordinates( fid, name, val, sp, & ! (in)
1032  2, start, shape(val), & ! (in)
1033  error ) ! (out)
1034  else
1035  ! Note this is for the one-file-per-process I/O method
1036  call file_write_associated_coordinates( fid, name, val, sp, & ! (in)
1037  2, (/1,1/), shape(val), & ! (in)
1038  error ) ! (out)
1039  end if
1040  if ( error /= success_code .and. error /= already_existed_code ) then
1041  call log('E', 'xxx failed to put associated coordinates: '//trim(name))
1042  end if
1043 
1044  return
1045  end subroutine filewrite2dassociatedcoordinatesrealsp
1046  subroutine filewrite2dassociatedcoordinatesrealdp( &
1047  fid, & ! (in)
1048  name, & ! (in)
1049  val, & ! (in)
1050  start, & ! (in)
1051  count, & ! (in)
1052  ndims ) ! (in)
1053  integer, intent(in) :: fid
1054  character(len=*), intent(in) :: name
1055  real(DP), intent(in) :: val(:,:)
1056  integer, intent(in), optional :: start(:)
1057  integer, intent(in), optional :: count(:) ! in case val has been reshaped
1058  integer, intent(in), optional :: ndims ! in case val has been reshaped
1059 
1060  integer error
1061  intrinsic size, shape
1062 
1063  if ( present(ndims) ) then
1064  ! Note this is called for history coordinates which have been reshaped
1065  ! from 2D/3D into 1D array. In this case, start and count must be also present
1066  call file_write_associated_coordinates( fid, name, val, dp, & ! (in)
1067  ndims, start, count, & ! (in)
1068  error ) ! (out)
1069  else if ( present(start) ) then
1070  ! Note this is called for restart coordinates
1071  call file_write_associated_coordinates( fid, name, val, dp, & ! (in)
1072  2, start, shape(val), & ! (in)
1073  error ) ! (out)
1074  else
1075  ! Note this is for the one-file-per-process I/O method
1076  call file_write_associated_coordinates( fid, name, val, dp, & ! (in)
1077  2, (/1,1/), shape(val), & ! (in)
1078  error ) ! (out)
1079  end if
1080  if ( error /= success_code .and. error /= already_existed_code ) then
1081  call log('E', 'xxx failed to put associated coordinates: '//trim(name))
1082  end if
1083 
1084  return
1085  end subroutine filewrite2dassociatedcoordinatesrealdp
1086  subroutine filewrite3dassociatedcoordinatesrealsp( &
1087  fid, & ! (in)
1088  name, & ! (in)
1089  val, & ! (in)
1090  start, & ! (in)
1091  count, & ! (in)
1092  ndims ) ! (in)
1093  integer, intent(in) :: fid
1094  character(len=*), intent(in) :: name
1095  real(SP), intent(in) :: val(:,:,:)
1096  integer, intent(in), optional :: start(:)
1097  integer, intent(in), optional :: count(:) ! in case val has been reshaped
1098  integer, intent(in), optional :: ndims ! in case val has been reshaped
1099 
1100  integer error
1101  intrinsic size, shape
1102 
1103  if ( present(ndims) ) then
1104  ! Note this is called for history coordinates which have been reshaped
1105  ! from 2D/3D into 1D array. In this case, start and count must be also present
1106  call file_write_associated_coordinates( fid, name, val, sp, & ! (in)
1107  ndims, start, count, & ! (in)
1108  error ) ! (out)
1109  else if ( present(start) ) then
1110  ! Note this is called for restart coordinates
1111  call file_write_associated_coordinates( fid, name, val, sp, & ! (in)
1112  3, start, shape(val), & ! (in)
1113  error ) ! (out)
1114  else
1115  ! Note this is for the one-file-per-process I/O method
1116  call file_write_associated_coordinates( fid, name, val, sp, & ! (in)
1117  3, (/1,1,1/), shape(val), & ! (in)
1118  error ) ! (out)
1119  end if
1120  if ( error /= success_code .and. error /= already_existed_code ) then
1121  call log('E', 'xxx failed to put associated coordinates: '//trim(name))
1122  end if
1123 
1124  return
1125  end subroutine filewrite3dassociatedcoordinatesrealsp
1126  subroutine filewrite3dassociatedcoordinatesrealdp( &
1127  fid, & ! (in)
1128  name, & ! (in)
1129  val, & ! (in)
1130  start, & ! (in)
1131  count, & ! (in)
1132  ndims ) ! (in)
1133  integer, intent(in) :: fid
1134  character(len=*), intent(in) :: name
1135  real(DP), intent(in) :: val(:,:,:)
1136  integer, intent(in), optional :: start(:)
1137  integer, intent(in), optional :: count(:) ! in case val has been reshaped
1138  integer, intent(in), optional :: ndims ! in case val has been reshaped
1139 
1140  integer error
1141  intrinsic size, shape
1142 
1143  if ( present(ndims) ) then
1144  ! Note this is called for history coordinates which have been reshaped
1145  ! from 2D/3D into 1D array. In this case, start and count must be also present
1146  call file_write_associated_coordinates( fid, name, val, dp, & ! (in)
1147  ndims, start, count, & ! (in)
1148  error ) ! (out)
1149  else if ( present(start) ) then
1150  ! Note this is called for restart coordinates
1151  call file_write_associated_coordinates( fid, name, val, dp, & ! (in)
1152  3, start, shape(val), & ! (in)
1153  error ) ! (out)
1154  else
1155  ! Note this is for the one-file-per-process I/O method
1156  call file_write_associated_coordinates( fid, name, val, dp, & ! (in)
1157  3, (/1,1,1/), shape(val), & ! (in)
1158  error ) ! (out)
1159  end if
1160  if ( error /= success_code .and. error /= already_existed_code ) then
1161  call log('E', 'xxx failed to put associated coordinates: '//trim(name))
1162  end if
1163 
1164  return
1165  end subroutine filewrite3dassociatedcoordinatesrealdp
1166  subroutine filewrite4dassociatedcoordinatesrealsp( &
1167  fid, & ! (in)
1168  name, & ! (in)
1169  val, & ! (in)
1170  start, & ! (in)
1171  count, & ! (in)
1172  ndims ) ! (in)
1173  integer, intent(in) :: fid
1174  character(len=*), intent(in) :: name
1175  real(SP), intent(in) :: val(:,:,:,:)
1176  integer, intent(in), optional :: start(:)
1177  integer, intent(in), optional :: count(:) ! in case val has been reshaped
1178  integer, intent(in), optional :: ndims ! in case val has been reshaped
1179 
1180  integer error
1181  intrinsic size, shape
1182 
1183  if ( present(ndims) ) then
1184  ! Note this is called for history coordinates which have been reshaped
1185  ! from 2D/3D into 1D array. In this case, start and count must be also present
1186  call file_write_associated_coordinates( fid, name, val, sp, & ! (in)
1187  ndims, start, count, & ! (in)
1188  error ) ! (out)
1189  else if ( present(start) ) then
1190  ! Note this is called for restart coordinates
1191  call file_write_associated_coordinates( fid, name, val, sp, & ! (in)
1192  4, start, shape(val), & ! (in)
1193  error ) ! (out)
1194  else
1195  ! Note this is for the one-file-per-process I/O method
1196  call file_write_associated_coordinates( fid, name, val, sp, & ! (in)
1197  4, (/1,1,1,1/), shape(val), & ! (in)
1198  error ) ! (out)
1199  end if
1200  if ( error /= success_code .and. error /= already_existed_code ) then
1201  call log('E', 'xxx failed to put associated coordinates: '//trim(name))
1202  end if
1203 
1204  return
1205  end subroutine filewrite4dassociatedcoordinatesrealsp
1206  subroutine filewrite4dassociatedcoordinatesrealdp( &
1207  fid, & ! (in)
1208  name, & ! (in)
1209  val, & ! (in)
1210  start, & ! (in)
1211  count, & ! (in)
1212  ndims ) ! (in)
1213  integer, intent(in) :: fid
1214  character(len=*), intent(in) :: name
1215  real(DP), intent(in) :: val(:,:,:,:)
1216  integer, intent(in), optional :: start(:)
1217  integer, intent(in), optional :: count(:) ! in case val has been reshaped
1218  integer, intent(in), optional :: ndims ! in case val has been reshaped
1219 
1220  integer error
1221  intrinsic size, shape
1222 
1223  if ( present(ndims) ) then
1224  ! Note this is called for history coordinates which have been reshaped
1225  ! from 2D/3D into 1D array. In this case, start and count must be also present
1226  call file_write_associated_coordinates( fid, name, val, dp, & ! (in)
1227  ndims, start, count, & ! (in)
1228  error ) ! (out)
1229  else if ( present(start) ) then
1230  ! Note this is called for restart coordinates
1231  call file_write_associated_coordinates( fid, name, val, dp, & ! (in)
1232  4, start, shape(val), & ! (in)
1233  error ) ! (out)
1234  else
1235  ! Note this is for the one-file-per-process I/O method
1236  call file_write_associated_coordinates( fid, name, val, dp, & ! (in)
1237  4, (/1,1,1,1/), shape(val), & ! (in)
1238  error ) ! (out)
1239  end if
1240  if ( error /= success_code .and. error /= already_existed_code ) then
1241  call log('E', 'xxx failed to put associated coordinates: '//trim(name))
1242  end if
1243 
1244  return
1245  end subroutine filewrite4dassociatedcoordinatesrealdp
1246 
1247  !-----------------------------------------------------------------------------
1248  ! interface FileAddVariable
1249  !-----------------------------------------------------------------------------
1250  subroutine fileaddvariablenot( &
1251  vid, & ! (out)
1252  fid, & ! (in)
1253  varname, & ! (in)
1254  desc, & ! (in)
1255  units, & ! (in)
1256  dims, & ! (in)
1257  dtype, & ! (in)
1258  tavg & ! (in) optional
1259  )
1260  integer, intent(out) :: vid
1261  integer, intent( in) :: fid
1262  character(len=*), intent( in) :: varname
1263  character(len=*), intent( in) :: desc
1264  character(len=*), intent( in) :: units
1265  character(len=*), intent( in) :: dims(:)
1266  integer, intent( in) :: dtype
1267  logical, intent( in), optional :: tavg
1268 
1269  call fileaddvariablerealdp(vid, fid, varname, desc, units, dims, dtype, &
1270  -1.0_dp, tavg )
1271 
1272  return
1273  end subroutine fileaddvariablenot
1274  subroutine fileaddvariablerealsp( &
1275  vid, & ! (out)
1276  fid, & ! (in)
1277  varname, & ! (in)
1278  desc, & ! (in)
1279  units, & ! (in)
1280  dims, & ! (in)
1281  dtype, & ! (in)
1282  tint, & ! (in)
1283  tavg & ! (in) optional
1284  )
1285  integer, intent(out) :: vid
1286  integer, intent( in) :: fid
1287  character(len=*), intent( in) :: varname
1288  character(len=*), intent( in) :: desc
1289  character(len=*), intent( in) :: units
1290  character(len=*), intent( in) :: dims(:)
1291  integer, intent( in) :: dtype
1292  real(SP), intent( in) :: tint
1293  logical, intent( in), optional :: tavg
1294 
1295  real(DP) :: tint8
1296  integer :: itavg
1297  integer :: error
1298  integer :: n
1299 
1300  intrinsic size
1301  !---------------------------------------------------------------------------
1302 
1303  vid = -1
1304  do n = 1, file_vid_count
1305  if ( file_vid_fid_list(n) == fid .and. &
1306  varname == file_vname_list(n) ) then
1307  vid = file_vid_list(n)
1308  end if
1309  enddo
1310 
1311  if ( vid < 0 ) then ! variable registration
1312  !--- register new variable
1313  write(message,'(2A)') '###### Variable registration : name = ', trim(varname)
1314  call log("I",message)
1315 
1316  tint8 = real(tint,dp)
1317 
1318  if ( present(tavg) ) then
1319  if ( tavg ) then
1320  itavg = 1
1321  else
1322  itavg = 0
1323  end if
1324  else
1325  itavg = 0
1326  end if
1327 
1328  call file_add_variable( vid, & ! (out)
1329  fid, varname, desc, units, dims, size(dims), dtype, & ! (in)
1330  tint8, itavg, & ! (in)
1331  error ) ! (out)
1332  if ( error /= success_code ) then
1333  call log('E', 'xxx failed to add variable: '//trim(varname))
1334  end if
1335 
1336  file_vname_list(file_vid_count) = trim(varname)
1337  file_vid_list(file_vid_count) = vid
1338  file_vid_fid_list(file_vid_count) = fid
1339  file_vid_count = file_vid_count + 1
1340  endif
1341 
1342  return
1343  end subroutine fileaddvariablerealsp
1344  subroutine fileaddvariablerealdp( &
1345  vid, & ! (out)
1346  fid, & ! (in)
1347  varname, & ! (in)
1348  desc, & ! (in)
1349  units, & ! (in)
1350  dims, & ! (in)
1351  dtype, & ! (in)
1352  tint, & ! (in)
1353  tavg & ! (in) optional
1354  )
1355  integer, intent(out) :: vid
1356  integer, intent( in) :: fid
1357  character(len=*), intent( in) :: varname
1358  character(len=*), intent( in) :: desc
1359  character(len=*), intent( in) :: units
1360  character(len=*), intent( in) :: dims(:)
1361  integer, intent( in) :: dtype
1362  real(DP), intent( in) :: tint
1363  logical, intent( in), optional :: tavg
1364 
1365  real(DP) :: tint8
1366  integer :: itavg
1367  integer :: error
1368  integer :: n
1369 
1370  intrinsic size
1371  !---------------------------------------------------------------------------
1372 
1373  vid = -1
1374  do n = 1, file_vid_count
1375  if ( file_vid_fid_list(n) == fid .and. &
1376  varname == file_vname_list(n) ) then
1377  vid = file_vid_list(n)
1378  end if
1379  enddo
1380 
1381  if ( vid < 0 ) then ! variable registration
1382  !--- register new variable
1383  write(message,'(2A)') '###### Variable registration : name = ', trim(varname)
1384  call log("I",message)
1385 
1386  tint8 = real(tint,dp)
1387 
1388  if ( present(tavg) ) then
1389  if ( tavg ) then
1390  itavg = 1
1391  else
1392  itavg = 0
1393  end if
1394  else
1395  itavg = 0
1396  end if
1397 
1398  call file_add_variable( vid, & ! (out)
1399  fid, varname, desc, units, dims, size(dims), dtype, & ! (in)
1400  tint8, itavg, & ! (in)
1401  error ) ! (out)
1402  if ( error /= success_code ) then
1403  call log('E', 'xxx failed to add variable: '//trim(varname))
1404  end if
1405 
1406  file_vname_list(file_vid_count) = trim(varname)
1407  file_vid_list(file_vid_count) = vid
1408  file_vid_fid_list(file_vid_count) = fid
1409  file_vid_count = file_vid_count + 1
1410  endif
1411 
1412  return
1413  end subroutine fileaddvariablerealdp
1414 
1415  subroutine filedefinevariable( &
1416  fid, & ! (in)
1417  vid, & ! (out)
1418  varname, & ! (in)
1419  desc, & ! (in)
1420  units, & ! (in)
1421  ndims, & ! (in)
1422  dims, & ! (in)
1423  dtype, & ! (in)
1424  tint, & ! (in) optional
1425  tavg & ! (in) optional
1426  )
1427  integer, intent(out) :: vid
1428  integer, intent( in) :: fid
1429  character(len=*), intent( in) :: varname
1430  character(len=*), intent( in) :: desc
1431  character(len=*), intent( in) :: units
1432  integer, intent( in) :: ndims
1433  character(len=*), intent( in) :: dims(:)
1434  integer, intent( in) :: dtype
1435  real(DP), intent( in), optional :: tint
1436  logical, intent( in), optional :: tavg
1437 
1438  real(DP) :: tint_
1439  integer :: itavg
1440  integer :: error
1441  integer :: n
1442 
1443  intrinsic size
1444  !---------------------------------------------------------------------------
1445 
1446  vid = -1
1447  do n = 1, file_vid_count
1448  if ( file_vid_fid_list(n) == fid .and. &
1449  varname == file_vname_list(n) ) then
1450  vid = file_vid_list(n)
1451  end if
1452  enddo
1453 
1454  if ( vid < 0 ) then ! variable registration
1455  !--- register new variable
1456  write(message,'(2A)') '###### Variable registration : name = ', trim(varname)
1457  call log("I",message)
1458 
1459  if ( present(tint) ) then
1460  tint_ = tint
1461  else
1462  tint_ = -1.0_dp
1463  endif
1464 
1465  if ( present(tavg) ) then
1466  if ( tavg ) then
1467  itavg = 1
1468  else
1469  itavg = 0
1470  end if
1471  else
1472  itavg = 0
1473  end if
1474 
1475  call file_add_variable( vid, & ! (out)
1476  fid, varname, desc, units, dims, ndims, dtype, & ! (in)
1477  tint_, itavg, & ! (in)
1478  error ) ! (out)
1479  if ( error /= success_code ) then
1480  call log('E', 'xxx failed to add variable: '//trim(varname))
1481  end if
1482 
1483  file_vname_list(file_vid_count) = trim(varname)
1484  file_vid_list(file_vid_count) = vid
1485  file_vid_fid_list(file_vid_count) = fid
1486  file_vid_count = file_vid_count + 1
1487  endif
1488 
1489  return
1490  end subroutine filedefinevariable
1491 
1492  !-----------------------------------------------------------------------------
1493  ! FileSetTAttr
1494  !-----------------------------------------------------------------------------
1495  subroutine filesettattr( &
1496  fid, & ! (in)
1497  vname, & ! (in)
1498  key, & ! (in)
1499  val & ! (in)
1500  )
1501  integer, intent(in) :: fid
1502  character(len=*), intent(in) :: vname
1503  character(len=*), intent(in) :: key
1504  character(len=*), intent(in) :: val
1505 
1506  integer :: error
1507 
1508  call file_set_tattr( &
1509  fid, vname, & ! (in)
1510  key, val, & ! (in)
1511  error ) ! (out)
1512  if ( error /= success_code .and. error /= already_existed_code ) then
1513  call log('E', 'xxx failed to set attr for axis')
1514  end if
1515 
1516  return
1517  end subroutine filesettattr
1518 
1519  !-----------------------------------------------------------------------------
1520  ! FileGetShape
1521  !-----------------------------------------------------------------------------
1522  subroutine filegetshape( &
1523  dims, & ! (out)
1524  basename, & ! (in)
1525  varname, & ! (in)
1526  myrank, & ! (in)
1527  single, & ! (in) optional
1528  error ) ! (out) optional
1529  implicit none
1530 
1531  integer, intent(out) :: dims(:)
1532  character(len=*), intent( in) :: basename
1533  character(len=*), intent( in) :: varname
1534  integer, intent( in) :: myrank
1535  logical, intent( in), optional :: single
1536  logical, intent(out), optional :: error
1537 
1538  integer :: fid
1539  type(datainfo) :: dinfo
1540  integer :: ierror
1541  integer :: n
1542 
1543  logical :: single_
1544  logical :: suppress
1545 
1546  intrinsic size
1547  intrinsic shape
1548  !---------------------------------------------------------------------------
1549 
1550  mpi_myrank = myrank
1551 
1552  if ( present(single) ) then
1553  single_ = single
1554  else
1555  single_ = .false.
1556  end if
1557 
1558  if ( present(error) ) then
1559  suppress = .true.
1560  else
1561  suppress = .false.
1562  end if
1563 
1564  !--- search/register file
1565  call fileopen( fid, & ! (out)
1566  basename, file_fread, single_ ) ! (in)
1567 
1568  !--- get data information
1569  call file_get_datainfo( dinfo, & ! (out)
1570  fid, varname, 1, suppress, & ! (in)
1571  ierror ) ! (out)
1572 
1573  !--- verify
1574  if ( ierror /= success_code ) then
1575  if ( present(error) ) then
1576  error = .true.
1577  return
1578  else
1579  call log('E', 'xxx failed to get data information :'//trim(varname))
1580  end if
1581  end if
1582 
1583  if ( dinfo%rank /= size(dims) ) then
1584  write(message,*) 'xxx rank is different, ', size(dims), dinfo%rank
1585  call log('E', message)
1586  end if
1587  do n = 1, size(dims)
1588  dims(n) = dinfo%dim_size(n)
1589  end do
1590 
1591  if ( present(error) ) error = .false.
1592 
1593  return
1594  end subroutine filegetshape
1595 
1596  !-----------------------------------------------------------------------------
1597  ! FileGetData
1598  !-----------------------------------------------------------------------------
1599  subroutine filegetdatainfofname( &
1600  basename, &
1601  varname, &
1602  myrank, &
1603  istep, &
1604  single, &
1605  description, &
1606  units, &
1607  datatype, &
1608  dim_rank, &
1609  dim_name, &
1610  dim_size, &
1611  time_start, &
1612  time_end, &
1613  time_units )
1614  implicit none
1615  character(len=*), intent(in) :: basename
1616  character(len=*), intent(in) :: varname
1617  integer, intent(in) :: myrank
1618  integer, intent(in) :: istep
1619  logical, intent(in), optional :: single
1620 
1621  character(len=File_HMID), intent(out), optional :: description
1622  character(len=File_HSHORT), intent(out), optional :: units
1623  integer, intent(out), optional :: datatype
1624  integer, intent(out), optional :: dim_rank
1625  character(len=File_HSHORT), intent(out), optional :: dim_name(:)
1626  integer, intent(out), optional :: dim_size(:)
1627  real(DP), intent(out), optional :: time_start
1628  real(DP), intent(out), optional :: time_end
1629  character(len=File_HMID), intent(out), optional :: time_units
1630 
1631  logical :: single_
1632  integer :: fid
1633 
1634  if ( present(single) ) then
1635  single_ = single
1636  else
1637  single_ = .false.
1638  end if
1639 
1640  mpi_myrank = myrank
1641 
1642  !--- search/register file
1643  call fileopen( fid, & ! [OUT]
1644  basename, & ! [IN]
1645  file_fread, & ! [IN]
1646  single_ ) ! [IN]
1647 
1648  call filegetdatainfofid( &
1649  fid, &
1650  varname, &
1651  istep, &
1652  description, &
1653  units, &
1654  datatype, &
1655  dim_rank, &
1656  dim_name, &
1657  dim_size, &
1658  time_start, &
1659  time_end, &
1660  time_units )
1661 
1662  return
1663  end subroutine filegetdatainfofname
1664  subroutine filegetdatainfofid( &
1665  fid, &
1666  varname, &
1667  istep, &
1668  description, &
1669  units, &
1670  datatype, &
1671  dim_rank, &
1672  dim_name, &
1673  dim_size, &
1674  time_start, &
1675  time_end, &
1676  time_units )
1677  implicit none
1678  integer, intent(in) :: fid
1679  character(len=*), intent(in) :: varname
1680  integer, intent(in) :: istep
1681 
1682  character(len=File_HMID), intent(out), optional :: description
1683  character(len=File_HSHORT), intent(out), optional :: units
1684  integer, intent(out), optional :: datatype
1685  integer, intent(out), optional :: dim_rank
1686  character(len=File_HSHORT), intent(out), optional :: dim_name(:)
1687  integer, intent(out), optional :: dim_size(:)
1688  real(DP), intent(out), optional :: time_start
1689  real(DP), intent(out), optional :: time_end
1690  character(len=File_HMID), intent(out), optional :: time_units
1691 
1692  type(datainfo) :: dinfo
1693 
1694  integer :: ndim, idim
1695  real(DP):: time(1)
1696 
1697  integer :: error
1698 
1699  intrinsic size
1700  !---------------------------------------------------------------------------
1701 
1702  !--- get data information
1703  call file_get_datainfo( dinfo, & ! [OUT]
1704  fid, & ! [IN]
1705  varname, & ! [IN]
1706  istep, & ! [IN]
1707  .false., & ! [IN]
1708  error ) ! [OUT]
1709 
1710  !--- verify and exit
1711  if ( error /= success_code ) then
1712  call log('E', 'xxx data info not found')
1713  endif
1714 
1715  if ( present(description) ) description = dinfo%description
1716  if ( present(units) ) units = dinfo%units
1717  if ( present(datatype) ) datatype = dinfo%datatype
1718  if ( present(dim_rank) ) dim_rank = dinfo%rank
1719 
1720  if ( present(dim_name) ) then
1721  ndim = min( dinfo%rank, size(dim_name) ) ! limit dimension rank
1722  do idim = 1, ndim
1723  dim_name(idim) = dinfo%dim_name(idim)
1724  enddo
1725  end if
1726  if ( present(dim_size) ) then
1727  ndim = min( dinfo%rank, size(dim_size) ) ! limit dimension rank
1728  do idim = 1, ndim
1729  dim_size(idim) = dinfo%dim_size(idim)
1730  enddo
1731  end if
1732 
1733  if ( present(time_units) ) then
1734  if ( dinfo%time_units == "" ) then
1735  call filegetglobalattribute( fid, "time_units", time_units )
1736  else
1737  time_units = dinfo%time_units
1738  end if
1739  end if
1740  if ( present(time_start) ) then
1741  if ( dinfo%time_units == "" ) then
1742  call filegetglobalattribute( fid, "time", time )
1743  time_start = time(1)
1744  else
1745  time_start = dinfo%time_start
1746  end if
1747  end if
1748  if ( present(time_end) ) then
1749  if ( dinfo%time_units == "" ) then
1750  call filegetglobalattribute( fid, "time", time )
1751  time_end = time(1)
1752  else
1753  time_end = dinfo%time_end
1754  end if
1755  end if
1756 
1757  return
1758  end subroutine filegetdatainfofid
1759 
1760  !-----------------------------------------------------------------------------
1761  ! FileGetData
1762  !-----------------------------------------------------------------------------
1763  subroutine filegetalldatainfofname( &
1764  step_limit, &
1765  dim_limit, &
1766  basename, &
1767  varname, &
1768  myrank, &
1769  step_nmax, &
1770  description, &
1771  units, &
1772  datatype, &
1773  dim_rank, &
1774  dim_name, &
1775  dim_size, &
1776  time_start, &
1777  time_end, &
1778  time_units, &
1779  single )
1780  implicit none
1781 
1782  integer, intent(in) :: step_limit
1783  integer, intent(in) :: dim_limit
1784  character(len=*), intent(in) :: basename
1785  character(len=*), intent(in) :: varname
1786  integer, intent(in) :: myrank
1787  integer, intent(out) :: step_nmax
1788  character(len=File_HMID), intent(out) :: description
1789  character(len=File_HSHORT), intent(out) :: units
1790  integer, intent(out) :: datatype
1791  integer, intent(out) :: dim_rank
1792  character(len=File_HSHORT), intent(out) :: dim_name (dim_limit)
1793  integer, intent(out) :: dim_size (dim_limit)
1794  real(DP), intent(out) :: time_start(step_limit)
1795  real(DP), intent(out) :: time_end (step_limit)
1796  character(len=File_HMID), intent(out) :: time_units
1797 
1798  logical, intent(in), optional :: single
1799 
1800 
1801  integer :: fid
1802  logical :: single_
1803 
1804  mpi_myrank = myrank
1805 
1806  if ( present(single) ) then
1807  single_ = single
1808  else
1809  single_ = .false.
1810  end if
1811 
1812  !--- search/register file
1813  call fileopen( fid, & ! [OUT]
1814  basename, & ! [IN]
1815  file_fread, & ! [IN]
1816  single_ ) ! [IN]
1817 
1818  call filegetalldatainfofid( &
1819  step_limit, &
1820  dim_limit, &
1821  fid, &
1822  varname, &
1823  step_nmax, &
1824  description, &
1825  units, &
1826  datatype, &
1827  dim_rank, &
1828  dim_name, &
1829  dim_size, &
1830  time_start, &
1831  time_end, &
1832  time_units )
1833 
1834  return
1835  end subroutine filegetalldatainfofname
1836  subroutine filegetalldatainfofid( &
1837  step_limit, &
1838  dim_limit, &
1839  fid, &
1840  varname, &
1841  step_nmax, &
1842  description, &
1843  units, &
1844  datatype, &
1845  dim_rank, &
1846  dim_name, &
1847  dim_size, &
1848  time_start, &
1849  time_end, &
1850  time_units )
1851  implicit none
1852 
1853  integer, intent(in) :: step_limit
1854  integer, intent(in) :: dim_limit
1855  integer, intent(in) :: fid
1856  character(len=*), intent(in) :: varname
1857  integer, intent(out) :: step_nmax
1858  character(len=File_HMID), intent(out) :: description
1859  character(len=File_HSHORT), intent(out) :: units
1860  integer, intent(out) :: datatype
1861  integer, intent(out) :: dim_rank
1862  character(len=File_HSHORT), intent(out) :: dim_name (dim_limit)
1863  integer, intent(out) :: dim_size (dim_limit)
1864  real(DP), intent(out) :: time_start(step_limit)
1865  real(DP), intent(out) :: time_end (step_limit)
1866  character(len=File_HMID), intent(out) :: time_units
1867 
1868  type(datainfo) :: dinfo
1869 
1870  integer :: ndim
1871  integer :: istep, idim
1872 
1873  integer :: error
1874  !---------------------------------------------------------------------------
1875 
1876  ! initialize
1877  description = ""
1878  units = ""
1879  datatype = -1
1880  dim_rank = -1
1881  dim_name(:) = ""
1882  dim_size(:) = -1
1883  time_start(:) = rmiss
1884  time_end(:) = rmiss
1885 
1886  do istep = 1, step_limit
1887  !--- get data information
1888  call file_get_datainfo( dinfo, & ! [OUT]
1889  fid, & ! [IN]
1890  varname, & ! [IN]
1891  istep, & ! [IN]
1892  .true., & ! [IN]
1893  error ) ! [OUT]
1894 
1895  !--- verify and exit
1896  if ( error /= success_code ) then
1897  step_nmax = istep - 1
1898  exit
1899  endif
1900 
1901  if ( istep == 1 ) then
1902  description = dinfo%description
1903  units = dinfo%units
1904  datatype = dinfo%datatype
1905  dim_rank = dinfo%rank
1906 
1907  ndim = min( dinfo%rank, dim_limit ) ! limit dimension rank
1908  do idim = 1, ndim
1909  dim_name(idim) = dinfo%dim_name(idim)
1910  dim_size(idim) = dinfo%dim_size(idim)
1911  enddo
1912 
1913  time_units = dinfo%time_units
1914  endif
1915 
1916  time_start(istep) = dinfo%time_start
1917  time_end(istep) = dinfo%time_end
1918  enddo
1919 
1920  return
1921  end subroutine filegetalldatainfofid
1922 
1923  !-----------------------------------------------------------------------------
1924  ! interface File_read
1925  !-----------------------------------------------------------------------------
1926  subroutine fileread1drealsp( &
1927  var, & ! (out)
1928  basename, & ! (in)
1929  varname, & ! (in)
1930  step, & ! (in)
1931  myrank, & ! (in)
1932  allow_missing, & ! (in) optional
1933  single & ! (in) optional
1934  )
1935  implicit none
1936 
1937  real(SP), intent(out) :: var(:)
1938  character(len=*), intent( in) :: basename
1939  character(len=*), intent( in) :: varname
1940  integer, intent( in) :: step
1941  integer, intent( in) :: myrank
1942  logical, intent( in), optional :: allow_missing !--- if data is missing, set value to zero
1943  logical, intent( in), optional :: single
1944 
1945  integer :: fid
1946  type(datainfo) :: dinfo
1947  integer :: dim_size(1)
1948  integer :: error
1949  integer :: n
1950 
1951  logical :: single_
1952 
1953  intrinsic shape
1954  !---------------------------------------------------------------------------
1955 
1956  single_ = .false.
1957 
1958  mpi_myrank = myrank
1959 
1960  if ( present(single) ) single_ = single
1961 
1962  !--- search/register file
1963  call fileopen( fid, & ! (out)
1964  basename, file_fread, single_ ) ! (in)
1965 
1966  !--- get data information
1967  call file_get_datainfo( dinfo, & ! (out)
1968  fid, varname, step, .false., & ! (in)
1969  error ) ! (out)
1970 
1971  !--- verify
1972  if ( error /= success_code ) then
1973  if ( present(allow_missing) ) then
1974  if ( allow_missing ) then
1975  write(message,*) 'xxx [INPUT]/[File] data not found! : ', &
1976  'varname= ',trim(varname),', step=',step
1977  call log('I', message)
1978  call log('I', 'xxx [INPUT]/[File] Value is set to 0.')
1979  var(:) = 0.0_sp
1980  else
1981  call log('E', 'xxx failed to get data information :'//trim(varname))
1982  end if
1983  else
1984  call log('E', 'xxx failed to get data information :'//trim(varname))
1985  end if
1986  end if
1987 
1988  if ( dinfo%rank /= 1 ) then
1989  write(message,*) 'xxx rank is not 1', dinfo%rank
1990  call log('E', message)
1991  end if
1992  dim_size(:) = shape(var)
1993  do n = 1, 1
1994  if ( dinfo%dim_size(n) /= dim_size(n) ) then
1995  write(message,*) 'xxx shape is different: ', varname, n, dinfo%dim_size(n), dim_size(n)
1996  call log('E', message)
1997  end if
1998  end do
1999 
2000  call file_read_data( var(:), & ! (out)
2001  dinfo, sp, & ! (in)
2002  error ) ! (out)
2003  if ( error /= success_code ) then
2004  call log('E', 'xxx failed to get data value')
2005  end if
2006 
2007  return
2008  end subroutine fileread1drealsp
2009  subroutine fileread1drealdp( &
2010  var, & ! (out)
2011  basename, & ! (in)
2012  varname, & ! (in)
2013  step, & ! (in)
2014  myrank, & ! (in)
2015  allow_missing, & ! (in) optional
2016  single & ! (in) optional
2017  )
2018  implicit none
2019 
2020  real(DP), intent(out) :: var(:)
2021  character(len=*), intent( in) :: basename
2022  character(len=*), intent( in) :: varname
2023  integer, intent( in) :: step
2024  integer, intent( in) :: myrank
2025  logical, intent( in), optional :: allow_missing !--- if data is missing, set value to zero
2026  logical, intent( in), optional :: single
2027 
2028  integer :: fid
2029  type(datainfo) :: dinfo
2030  integer :: dim_size(1)
2031  integer :: error
2032  integer :: n
2033 
2034  logical :: single_
2035 
2036  intrinsic shape
2037  !---------------------------------------------------------------------------
2038 
2039  single_ = .false.
2040 
2041  mpi_myrank = myrank
2042 
2043  if ( present(single) ) single_ = single
2044 
2045  !--- search/register file
2046  call fileopen( fid, & ! (out)
2047  basename, file_fread, single_ ) ! (in)
2048 
2049  !--- get data information
2050  call file_get_datainfo( dinfo, & ! (out)
2051  fid, varname, step, .false., & ! (in)
2052  error ) ! (out)
2053 
2054  !--- verify
2055  if ( error /= success_code ) then
2056  if ( present(allow_missing) ) then
2057  if ( allow_missing ) then
2058  write(message,*) 'xxx [INPUT]/[File] data not found! : ', &
2059  'varname= ',trim(varname),', step=',step
2060  call log('I', message)
2061  call log('I', 'xxx [INPUT]/[File] Value is set to 0.')
2062  var(:) = 0.0_dp
2063  else
2064  call log('E', 'xxx failed to get data information :'//trim(varname))
2065  end if
2066  else
2067  call log('E', 'xxx failed to get data information :'//trim(varname))
2068  end if
2069  end if
2070 
2071  if ( dinfo%rank /= 1 ) then
2072  write(message,*) 'xxx rank is not 1', dinfo%rank
2073  call log('E', message)
2074  end if
2075  dim_size(:) = shape(var)
2076  do n = 1, 1
2077  if ( dinfo%dim_size(n) /= dim_size(n) ) then
2078  write(message,*) 'xxx shape is different: ', varname, n, dinfo%dim_size(n), dim_size(n)
2079  call log('E', message)
2080  end if
2081  end do
2082 
2083  call file_read_data( var(:), & ! (out)
2084  dinfo, dp, & ! (in)
2085  error ) ! (out)
2086  if ( error /= success_code ) then
2087  call log('E', 'xxx failed to get data value')
2088  end if
2089 
2090  return
2091  end subroutine fileread1drealdp
2092  subroutine fileread2drealsp( &
2093  var, & ! (out)
2094  basename, & ! (in)
2095  varname, & ! (in)
2096  step, & ! (in)
2097  myrank, & ! (in)
2098  allow_missing, & ! (in) optional
2099  single & ! (in) optional
2100  )
2101  implicit none
2102 
2103  real(SP), intent(out) :: var(:,:)
2104  character(len=*), intent( in) :: basename
2105  character(len=*), intent( in) :: varname
2106  integer, intent( in) :: step
2107  integer, intent( in) :: myrank
2108  logical, intent( in), optional :: allow_missing !--- if data is missing, set value to zero
2109  logical, intent( in), optional :: single
2110 
2111  integer :: fid
2112  type(datainfo) :: dinfo
2113  integer :: dim_size(2)
2114  integer :: error
2115  integer :: n
2116 
2117  logical :: single_
2118 
2119  intrinsic shape
2120  !---------------------------------------------------------------------------
2121 
2122  single_ = .false.
2123 
2124  mpi_myrank = myrank
2125 
2126  if ( present(single) ) single_ = single
2127 
2128  !--- search/register file
2129  call fileopen( fid, & ! (out)
2130  basename, file_fread, single_ ) ! (in)
2131 
2132  !--- get data information
2133  call file_get_datainfo( dinfo, & ! (out)
2134  fid, varname, step, .false., & ! (in)
2135  error ) ! (out)
2136 
2137  !--- verify
2138  if ( error /= success_code ) then
2139  if ( present(allow_missing) ) then
2140  if ( allow_missing ) then
2141  write(message,*) 'xxx [INPUT]/[File] data not found! : ', &
2142  'varname= ',trim(varname),', step=',step
2143  call log('I', message)
2144  call log('I', 'xxx [INPUT]/[File] Value is set to 0.')
2145  var(:,:) = 0.0_sp
2146  else
2147  call log('E', 'xxx failed to get data information :'//trim(varname))
2148  end if
2149  else
2150  call log('E', 'xxx failed to get data information :'//trim(varname))
2151  end if
2152  end if
2153 
2154  if ( dinfo%rank /= 2 ) then
2155  write(message,*) 'xxx rank is not 2', dinfo%rank
2156  call log('E', message)
2157  end if
2158  dim_size(:) = shape(var)
2159  do n = 1, 2
2160  if ( dinfo%dim_size(n) /= dim_size(n) ) then
2161  write(message,*) 'xxx shape is different: ', varname, n, dinfo%dim_size(n), dim_size(n)
2162  call log('E', message)
2163  end if
2164  end do
2165 
2166  call file_read_data( var(:,:), & ! (out)
2167  dinfo, sp, & ! (in)
2168  error ) ! (out)
2169  if ( error /= success_code ) then
2170  call log('E', 'xxx failed to get data value')
2171  end if
2172 
2173  return
2174  end subroutine fileread2drealsp
2175  subroutine fileread2drealdp( &
2176  var, & ! (out)
2177  basename, & ! (in)
2178  varname, & ! (in)
2179  step, & ! (in)
2180  myrank, & ! (in)
2181  allow_missing, & ! (in) optional
2182  single & ! (in) optional
2183  )
2184  implicit none
2185 
2186  real(DP), intent(out) :: var(:,:)
2187  character(len=*), intent( in) :: basename
2188  character(len=*), intent( in) :: varname
2189  integer, intent( in) :: step
2190  integer, intent( in) :: myrank
2191  logical, intent( in), optional :: allow_missing !--- if data is missing, set value to zero
2192  logical, intent( in), optional :: single
2193 
2194  integer :: fid
2195  type(datainfo) :: dinfo
2196  integer :: dim_size(2)
2197  integer :: error
2198  integer :: n
2199 
2200  logical :: single_
2201 
2202  intrinsic shape
2203  !---------------------------------------------------------------------------
2204 
2205  single_ = .false.
2206 
2207  mpi_myrank = myrank
2208 
2209  if ( present(single) ) single_ = single
2210 
2211  !--- search/register file
2212  call fileopen( fid, & ! (out)
2213  basename, file_fread, single_ ) ! (in)
2214 
2215  !--- get data information
2216  call file_get_datainfo( dinfo, & ! (out)
2217  fid, varname, step, .false., & ! (in)
2218  error ) ! (out)
2219 
2220  !--- verify
2221  if ( error /= success_code ) then
2222  if ( present(allow_missing) ) then
2223  if ( allow_missing ) then
2224  write(message,*) 'xxx [INPUT]/[File] data not found! : ', &
2225  'varname= ',trim(varname),', step=',step
2226  call log('I', message)
2227  call log('I', 'xxx [INPUT]/[File] Value is set to 0.')
2228  var(:,:) = 0.0_dp
2229  else
2230  call log('E', 'xxx failed to get data information :'//trim(varname))
2231  end if
2232  else
2233  call log('E', 'xxx failed to get data information :'//trim(varname))
2234  end if
2235  end if
2236 
2237  if ( dinfo%rank /= 2 ) then
2238  write(message,*) 'xxx rank is not 2', dinfo%rank
2239  call log('E', message)
2240  end if
2241  dim_size(:) = shape(var)
2242  do n = 1, 2
2243  if ( dinfo%dim_size(n) /= dim_size(n) ) then
2244  write(message,*) 'xxx shape is different: ', varname, n, dinfo%dim_size(n), dim_size(n)
2245  call log('E', message)
2246  end if
2247  end do
2248 
2249  call file_read_data( var(:,:), & ! (out)
2250  dinfo, dp, & ! (in)
2251  error ) ! (out)
2252  if ( error /= success_code ) then
2253  call log('E', 'xxx failed to get data value')
2254  end if
2255 
2256  return
2257  end subroutine fileread2drealdp
2258  subroutine fileread3drealsp( &
2259  var, & ! (out)
2260  basename, & ! (in)
2261  varname, & ! (in)
2262  step, & ! (in)
2263  myrank, & ! (in)
2264  allow_missing, & ! (in) optional
2265  single & ! (in) optional
2266  )
2267  implicit none
2268 
2269  real(SP), intent(out) :: var(:,:,:)
2270  character(len=*), intent( in) :: basename
2271  character(len=*), intent( in) :: varname
2272  integer, intent( in) :: step
2273  integer, intent( in) :: myrank
2274  logical, intent( in), optional :: allow_missing !--- if data is missing, set value to zero
2275  logical, intent( in), optional :: single
2276 
2277  integer :: fid
2278  type(datainfo) :: dinfo
2279  integer :: dim_size(3)
2280  integer :: error
2281  integer :: n
2282 
2283  logical :: single_
2284 
2285  intrinsic shape
2286  !---------------------------------------------------------------------------
2287 
2288  single_ = .false.
2289 
2290  mpi_myrank = myrank
2291 
2292  if ( present(single) ) single_ = single
2293 
2294  !--- search/register file
2295  call fileopen( fid, & ! (out)
2296  basename, file_fread, single_ ) ! (in)
2297 
2298  !--- get data information
2299  call file_get_datainfo( dinfo, & ! (out)
2300  fid, varname, step, .false., & ! (in)
2301  error ) ! (out)
2302 
2303  !--- verify
2304  if ( error /= success_code ) then
2305  if ( present(allow_missing) ) then
2306  if ( allow_missing ) then
2307  write(message,*) 'xxx [INPUT]/[File] data not found! : ', &
2308  'varname= ',trim(varname),', step=',step
2309  call log('I', message)
2310  call log('I', 'xxx [INPUT]/[File] Value is set to 0.')
2311  var(:,:,:) = 0.0_sp
2312  else
2313  call log('E', 'xxx failed to get data information :'//trim(varname))
2314  end if
2315  else
2316  call log('E', 'xxx failed to get data information :'//trim(varname))
2317  end if
2318  end if
2319 
2320  if ( dinfo%rank /= 3 ) then
2321  write(message,*) 'xxx rank is not 3', dinfo%rank
2322  call log('E', message)
2323  end if
2324  dim_size(:) = shape(var)
2325  do n = 1, 3
2326  if ( dinfo%dim_size(n) /= dim_size(n) ) then
2327  write(message,*) 'xxx shape is different: ', varname, n, dinfo%dim_size(n), dim_size(n)
2328  call log('E', message)
2329  end if
2330  end do
2331 
2332  call file_read_data( var(:,:,:), & ! (out)
2333  dinfo, sp, & ! (in)
2334  error ) ! (out)
2335  if ( error /= success_code ) then
2336  call log('E', 'xxx failed to get data value')
2337  end if
2338 
2339  return
2340  end subroutine fileread3drealsp
2341  subroutine fileread3drealdp( &
2342  var, & ! (out)
2343  basename, & ! (in)
2344  varname, & ! (in)
2345  step, & ! (in)
2346  myrank, & ! (in)
2347  allow_missing, & ! (in) optional
2348  single & ! (in) optional
2349  )
2350  implicit none
2351 
2352  real(DP), intent(out) :: var(:,:,:)
2353  character(len=*), intent( in) :: basename
2354  character(len=*), intent( in) :: varname
2355  integer, intent( in) :: step
2356  integer, intent( in) :: myrank
2357  logical, intent( in), optional :: allow_missing !--- if data is missing, set value to zero
2358  logical, intent( in), optional :: single
2359 
2360  integer :: fid
2361  type(datainfo) :: dinfo
2362  integer :: dim_size(3)
2363  integer :: error
2364  integer :: n
2365 
2366  logical :: single_
2367 
2368  intrinsic shape
2369  !---------------------------------------------------------------------------
2370 
2371  single_ = .false.
2372 
2373  mpi_myrank = myrank
2374 
2375  if ( present(single) ) single_ = single
2376 
2377  !--- search/register file
2378  call fileopen( fid, & ! (out)
2379  basename, file_fread, single_ ) ! (in)
2380 
2381  !--- get data information
2382  call file_get_datainfo( dinfo, & ! (out)
2383  fid, varname, step, .false., & ! (in)
2384  error ) ! (out)
2385 
2386  !--- verify
2387  if ( error /= success_code ) then
2388  if ( present(allow_missing) ) then
2389  if ( allow_missing ) then
2390  write(message,*) 'xxx [INPUT]/[File] data not found! : ', &
2391  'varname= ',trim(varname),', step=',step
2392  call log('I', message)
2393  call log('I', 'xxx [INPUT]/[File] Value is set to 0.')
2394  var(:,:,:) = 0.0_dp
2395  else
2396  call log('E', 'xxx failed to get data information :'//trim(varname))
2397  end if
2398  else
2399  call log('E', 'xxx failed to get data information :'//trim(varname))
2400  end if
2401  end if
2402 
2403  if ( dinfo%rank /= 3 ) then
2404  write(message,*) 'xxx rank is not 3', dinfo%rank
2405  call log('E', message)
2406  end if
2407  dim_size(:) = shape(var)
2408  do n = 1, 3
2409  if ( dinfo%dim_size(n) /= dim_size(n) ) then
2410  write(message,*) 'xxx shape is different: ', varname, n, dinfo%dim_size(n), dim_size(n)
2411  call log('E', message)
2412  end if
2413  end do
2414 
2415  call file_read_data( var(:,:,:), & ! (out)
2416  dinfo, dp, & ! (in)
2417  error ) ! (out)
2418  if ( error /= success_code ) then
2419  call log('E', 'xxx failed to get data value')
2420  end if
2421 
2422  return
2423  end subroutine fileread3drealdp
2424  subroutine fileread4drealsp( &
2425  var, & ! (out)
2426  basename, & ! (in)
2427  varname, & ! (in)
2428  step, & ! (in)
2429  myrank, & ! (in)
2430  allow_missing, & ! (in) optional
2431  single & ! (in) optional
2432  )
2433  implicit none
2434 
2435  real(SP), intent(out) :: var(:,:,:,:)
2436  character(len=*), intent( in) :: basename
2437  character(len=*), intent( in) :: varname
2438  integer, intent( in) :: step
2439  integer, intent( in) :: myrank
2440  logical, intent( in), optional :: allow_missing !--- if data is missing, set value to zero
2441  logical, intent( in), optional :: single
2442 
2443  integer :: fid
2444  type(datainfo) :: dinfo
2445  integer :: dim_size(4)
2446  integer :: error
2447  integer :: n
2448 
2449  logical :: single_
2450 
2451  intrinsic shape
2452  !---------------------------------------------------------------------------
2453 
2454  single_ = .false.
2455 
2456  mpi_myrank = myrank
2457 
2458  if ( present(single) ) single_ = single
2459 
2460  !--- search/register file
2461  call fileopen( fid, & ! (out)
2462  basename, file_fread, single_ ) ! (in)
2463 
2464  !--- get data information
2465  call file_get_datainfo( dinfo, & ! (out)
2466  fid, varname, step, .false., & ! (in)
2467  error ) ! (out)
2468 
2469  !--- verify
2470  if ( error /= success_code ) then
2471  if ( present(allow_missing) ) then
2472  if ( allow_missing ) then
2473  write(message,*) 'xxx [INPUT]/[File] data not found! : ', &
2474  'varname= ',trim(varname),', step=',step
2475  call log('I', message)
2476  call log('I', 'xxx [INPUT]/[File] Value is set to 0.')
2477  var(:,:,:,:) = 0.0_sp
2478  else
2479  call log('E', 'xxx failed to get data information :'//trim(varname))
2480  end if
2481  else
2482  call log('E', 'xxx failed to get data information :'//trim(varname))
2483  end if
2484  end if
2485 
2486  if ( dinfo%rank /= 4 ) then
2487  write(message,*) 'xxx rank is not 4', dinfo%rank
2488  call log('E', message)
2489  end if
2490  dim_size(:) = shape(var)
2491  do n = 1, 4
2492  if ( dinfo%dim_size(n) /= dim_size(n) ) then
2493  write(message,*) 'xxx shape is different: ', varname, n, dinfo%dim_size(n), dim_size(n)
2494  call log('E', message)
2495  end if
2496  end do
2497 
2498  call file_read_data( var(:,:,:,:), & ! (out)
2499  dinfo, sp, & ! (in)
2500  error ) ! (out)
2501  if ( error /= success_code ) then
2502  call log('E', 'xxx failed to get data value')
2503  end if
2504 
2505  return
2506  end subroutine fileread4drealsp
2507  subroutine fileread4drealdp( &
2508  var, & ! (out)
2509  basename, & ! (in)
2510  varname, & ! (in)
2511  step, & ! (in)
2512  myrank, & ! (in)
2513  allow_missing, & ! (in) optional
2514  single & ! (in) optional
2515  )
2516  implicit none
2517 
2518  real(DP), intent(out) :: var(:,:,:,:)
2519  character(len=*), intent( in) :: basename
2520  character(len=*), intent( in) :: varname
2521  integer, intent( in) :: step
2522  integer, intent( in) :: myrank
2523  logical, intent( in), optional :: allow_missing !--- if data is missing, set value to zero
2524  logical, intent( in), optional :: single
2525 
2526  integer :: fid
2527  type(datainfo) :: dinfo
2528  integer :: dim_size(4)
2529  integer :: error
2530  integer :: n
2531 
2532  logical :: single_
2533 
2534  intrinsic shape
2535  !---------------------------------------------------------------------------
2536 
2537  single_ = .false.
2538 
2539  mpi_myrank = myrank
2540 
2541  if ( present(single) ) single_ = single
2542 
2543  !--- search/register file
2544  call fileopen( fid, & ! (out)
2545  basename, file_fread, single_ ) ! (in)
2546 
2547  !--- get data information
2548  call file_get_datainfo( dinfo, & ! (out)
2549  fid, varname, step, .false., & ! (in)
2550  error ) ! (out)
2551 
2552  !--- verify
2553  if ( error /= success_code ) then
2554  if ( present(allow_missing) ) then
2555  if ( allow_missing ) then
2556  write(message,*) 'xxx [INPUT]/[File] data not found! : ', &
2557  'varname= ',trim(varname),', step=',step
2558  call log('I', message)
2559  call log('I', 'xxx [INPUT]/[File] Value is set to 0.')
2560  var(:,:,:,:) = 0.0_dp
2561  else
2562  call log('E', 'xxx failed to get data information :'//trim(varname))
2563  end if
2564  else
2565  call log('E', 'xxx failed to get data information :'//trim(varname))
2566  end if
2567  end if
2568 
2569  if ( dinfo%rank /= 4 ) then
2570  write(message,*) 'xxx rank is not 4', dinfo%rank
2571  call log('E', message)
2572  end if
2573  dim_size(:) = shape(var)
2574  do n = 1, 4
2575  if ( dinfo%dim_size(n) /= dim_size(n) ) then
2576  write(message,*) 'xxx shape is different: ', varname, n, dinfo%dim_size(n), dim_size(n)
2577  call log('E', message)
2578  end if
2579  end do
2580 
2581  call file_read_data( var(:,:,:,:), & ! (out)
2582  dinfo, dp, & ! (in)
2583  error ) ! (out)
2584  if ( error /= success_code ) then
2585  call log('E', 'xxx failed to get data value')
2586  end if
2587 
2588  return
2589  end subroutine fileread4drealdp
2590 
2591  subroutine filereadvar1drealsp( &
2592  var, & ! (out)
2593  fid, & ! (in)
2594  varname, & ! (in)
2595  step, & ! (in)
2596  allow_missing, & ! (in) optional
2597  single, & ! (in) optional
2598  ntypes, & ! (in)
2599  dtype, & ! (in)
2600  start, & ! (in)
2601  count & ! (in)
2602  )
2603  use mpi, only : mpi_comm_null
2604  implicit none
2605 
2606  real(SP), intent(out) :: var(:)
2607  integer, intent( in) :: fid
2608  character(len=*), intent( in) :: varname
2609  integer, intent( in) :: step
2610  logical, intent( in), optional :: allow_missing !--- if data is missing, set value to zero
2611  logical, intent( in), optional :: single
2612  integer, intent( in), optional :: ntypes ! number of dtypes
2613  integer, intent( in), optional :: dtype ! MPI derived datatype for read buffer
2614  integer, intent( in), optional :: start(:) ! request starts to global variable
2615  integer, intent( in), optional :: count(:) ! request sizes to global variable
2616 
2617  type(datainfo) :: dinfo
2618  integer :: error
2619 
2620  intrinsic size, shape
2621  !---------------------------------------------------------------------------
2622 
2623  !--- get data information
2624  call file_get_datainfo( dinfo, & ! (out)
2625  fid, varname, step, .false., & ! (in)
2626  error ) ! (out)
2627 
2628  !--- verify
2629  if ( error /= success_code ) then
2630  if ( present(allow_missing) ) then
2631  if ( allow_missing ) then
2632  write(message,*) 'xxx [INPUT]/[File] data not found! : ', &
2633  'varname= ',trim(varname),', step=',step
2634  call log('I', message)
2635  call log('I', 'xxx [INPUT]/[File] Value is set to 0.')
2636  var(:) = 0.0_sp
2637  else
2638  call log('E', 'xxx failed to get data information :'//trim(varname))
2639  end if
2640  else
2641  call log('E', 'xxx failed to get data information :'//trim(varname))
2642  end if
2643  end if
2644 
2645  if ( dinfo%rank /= 1 ) then
2646  write(message,*) 'xxx rank is not 1', dinfo%rank
2647  call log('E', message)
2648  end if
2649 
2650  if (present(ntypes) ) then
2651  call file_read_data_par( var(:), & ! (out)
2652  dinfo, size(shape(var)), ntypes, dtype, start, count, & ! (in)
2653  error ) ! (out)
2654  else
2655  call file_read_data( var(:), & ! (out)
2656  dinfo, sp, & ! (in)
2657  error ) ! (out)
2658  end if
2659  if ( error /= success_code ) then
2660  call log('E', 'xxx failed to get data value')
2661  end if
2662 
2663  return
2664  end subroutine filereadvar1drealsp
2665  subroutine filereadvar1drealdp( &
2666  var, & ! (out)
2667  fid, & ! (in)
2668  varname, & ! (in)
2669  step, & ! (in)
2670  allow_missing, & ! (in) optional
2671  single, & ! (in) optional
2672  ntypes, & ! (in)
2673  dtype, & ! (in)
2674  start, & ! (in)
2675  count & ! (in)
2676  )
2677  use mpi, only : mpi_comm_null
2678  implicit none
2679 
2680  real(DP), intent(out) :: var(:)
2681  integer, intent( in) :: fid
2682  character(len=*), intent( in) :: varname
2683  integer, intent( in) :: step
2684  logical, intent( in), optional :: allow_missing !--- if data is missing, set value to zero
2685  logical, intent( in), optional :: single
2686  integer, intent( in), optional :: ntypes ! number of dtypes
2687  integer, intent( in), optional :: dtype ! MPI derived datatype for read buffer
2688  integer, intent( in), optional :: start(:) ! request starts to global variable
2689  integer, intent( in), optional :: count(:) ! request sizes to global variable
2690 
2691  type(datainfo) :: dinfo
2692  integer :: error
2693 
2694  intrinsic size, shape
2695  !---------------------------------------------------------------------------
2696 
2697  !--- get data information
2698  call file_get_datainfo( dinfo, & ! (out)
2699  fid, varname, step, .false., & ! (in)
2700  error ) ! (out)
2701 
2702  !--- verify
2703  if ( error /= success_code ) then
2704  if ( present(allow_missing) ) then
2705  if ( allow_missing ) then
2706  write(message,*) 'xxx [INPUT]/[File] data not found! : ', &
2707  'varname= ',trim(varname),', step=',step
2708  call log('I', message)
2709  call log('I', 'xxx [INPUT]/[File] Value is set to 0.')
2710  var(:) = 0.0_dp
2711  else
2712  call log('E', 'xxx failed to get data information :'//trim(varname))
2713  end if
2714  else
2715  call log('E', 'xxx failed to get data information :'//trim(varname))
2716  end if
2717  end if
2718 
2719  if ( dinfo%rank /= 1 ) then
2720  write(message,*) 'xxx rank is not 1', dinfo%rank
2721  call log('E', message)
2722  end if
2723 
2724  if (present(ntypes) ) then
2725  call file_read_data_par( var(:), & ! (out)
2726  dinfo, size(shape(var)), ntypes, dtype, start, count, & ! (in)
2727  error ) ! (out)
2728  else
2729  call file_read_data( var(:), & ! (out)
2730  dinfo, dp, & ! (in)
2731  error ) ! (out)
2732  end if
2733  if ( error /= success_code ) then
2734  call log('E', 'xxx failed to get data value')
2735  end if
2736 
2737  return
2738  end subroutine filereadvar1drealdp
2739  subroutine filereadvar2drealsp( &
2740  var, & ! (out)
2741  fid, & ! (in)
2742  varname, & ! (in)
2743  step, & ! (in)
2744  allow_missing, & ! (in) optional
2745  single, & ! (in) optional
2746  ntypes, & ! (in)
2747  dtype, & ! (in)
2748  start, & ! (in)
2749  count & ! (in)
2750  )
2751  use mpi, only : mpi_comm_null
2752  implicit none
2753 
2754  real(SP), intent(out) :: var(:,:)
2755  integer, intent( in) :: fid
2756  character(len=*), intent( in) :: varname
2757  integer, intent( in) :: step
2758  logical, intent( in), optional :: allow_missing !--- if data is missing, set value to zero
2759  logical, intent( in), optional :: single
2760  integer, intent( in), optional :: ntypes ! number of dtypes
2761  integer, intent( in), optional :: dtype ! MPI derived datatype for read buffer
2762  integer, intent( in), optional :: start(:) ! request starts to global variable
2763  integer, intent( in), optional :: count(:) ! request sizes to global variable
2764 
2765  type(datainfo) :: dinfo
2766  integer :: error
2767 
2768  intrinsic size, shape
2769  !---------------------------------------------------------------------------
2770 
2771  !--- get data information
2772  call file_get_datainfo( dinfo, & ! (out)
2773  fid, varname, step, .false., & ! (in)
2774  error ) ! (out)
2775 
2776  !--- verify
2777  if ( error /= success_code ) then
2778  if ( present(allow_missing) ) then
2779  if ( allow_missing ) then
2780  write(message,*) 'xxx [INPUT]/[File] data not found! : ', &
2781  'varname= ',trim(varname),', step=',step
2782  call log('I', message)
2783  call log('I', 'xxx [INPUT]/[File] Value is set to 0.')
2784  var(:,:) = 0.0_sp
2785  else
2786  call log('E', 'xxx failed to get data information :'//trim(varname))
2787  end if
2788  else
2789  call log('E', 'xxx failed to get data information :'//trim(varname))
2790  end if
2791  end if
2792 
2793  if ( dinfo%rank /= 2 ) then
2794  write(message,*) 'xxx rank is not 2', dinfo%rank
2795  call log('E', message)
2796  end if
2797 
2798  if (present(ntypes) ) then
2799  call file_read_data_par( var(:,:), & ! (out)
2800  dinfo, size(shape(var)), ntypes, dtype, start, count, & ! (in)
2801  error ) ! (out)
2802  else
2803  call file_read_data( var(:,:), & ! (out)
2804  dinfo, sp, & ! (in)
2805  error ) ! (out)
2806  end if
2807  if ( error /= success_code ) then
2808  call log('E', 'xxx failed to get data value')
2809  end if
2810 
2811  return
2812  end subroutine filereadvar2drealsp
2813  subroutine filereadvar2drealdp( &
2814  var, & ! (out)
2815  fid, & ! (in)
2816  varname, & ! (in)
2817  step, & ! (in)
2818  allow_missing, & ! (in) optional
2819  single, & ! (in) optional
2820  ntypes, & ! (in)
2821  dtype, & ! (in)
2822  start, & ! (in)
2823  count & ! (in)
2824  )
2825  use mpi, only : mpi_comm_null
2826  implicit none
2827 
2828  real(DP), intent(out) :: var(:,:)
2829  integer, intent( in) :: fid
2830  character(len=*), intent( in) :: varname
2831  integer, intent( in) :: step
2832  logical, intent( in), optional :: allow_missing !--- if data is missing, set value to zero
2833  logical, intent( in), optional :: single
2834  integer, intent( in), optional :: ntypes ! number of dtypes
2835  integer, intent( in), optional :: dtype ! MPI derived datatype for read buffer
2836  integer, intent( in), optional :: start(:) ! request starts to global variable
2837  integer, intent( in), optional :: count(:) ! request sizes to global variable
2838 
2839  type(datainfo) :: dinfo
2840  integer :: error
2841 
2842  intrinsic size, shape
2843  !---------------------------------------------------------------------------
2844 
2845  !--- get data information
2846  call file_get_datainfo( dinfo, & ! (out)
2847  fid, varname, step, .false., & ! (in)
2848  error ) ! (out)
2849 
2850  !--- verify
2851  if ( error /= success_code ) then
2852  if ( present(allow_missing) ) then
2853  if ( allow_missing ) then
2854  write(message,*) 'xxx [INPUT]/[File] data not found! : ', &
2855  'varname= ',trim(varname),', step=',step
2856  call log('I', message)
2857  call log('I', 'xxx [INPUT]/[File] Value is set to 0.')
2858  var(:,:) = 0.0_dp
2859  else
2860  call log('E', 'xxx failed to get data information :'//trim(varname))
2861  end if
2862  else
2863  call log('E', 'xxx failed to get data information :'//trim(varname))
2864  end if
2865  end if
2866 
2867  if ( dinfo%rank /= 2 ) then
2868  write(message,*) 'xxx rank is not 2', dinfo%rank
2869  call log('E', message)
2870  end if
2871 
2872  if (present(ntypes) ) then
2873  call file_read_data_par( var(:,:), & ! (out)
2874  dinfo, size(shape(var)), ntypes, dtype, start, count, & ! (in)
2875  error ) ! (out)
2876  else
2877  call file_read_data( var(:,:), & ! (out)
2878  dinfo, dp, & ! (in)
2879  error ) ! (out)
2880  end if
2881  if ( error /= success_code ) then
2882  call log('E', 'xxx failed to get data value')
2883  end if
2884 
2885  return
2886  end subroutine filereadvar2drealdp
2887  subroutine filereadvar3drealsp( &
2888  var, & ! (out)
2889  fid, & ! (in)
2890  varname, & ! (in)
2891  step, & ! (in)
2892  allow_missing, & ! (in) optional
2893  single, & ! (in) optional
2894  ntypes, & ! (in)
2895  dtype, & ! (in)
2896  start, & ! (in)
2897  count & ! (in)
2898  )
2899  use mpi, only : mpi_comm_null
2900  implicit none
2901 
2902  real(SP), intent(out) :: var(:,:,:)
2903  integer, intent( in) :: fid
2904  character(len=*), intent( in) :: varname
2905  integer, intent( in) :: step
2906  logical, intent( in), optional :: allow_missing !--- if data is missing, set value to zero
2907  logical, intent( in), optional :: single
2908  integer, intent( in), optional :: ntypes ! number of dtypes
2909  integer, intent( in), optional :: dtype ! MPI derived datatype for read buffer
2910  integer, intent( in), optional :: start(:) ! request starts to global variable
2911  integer, intent( in), optional :: count(:) ! request sizes to global variable
2912 
2913  type(datainfo) :: dinfo
2914  integer :: error
2915 
2916  intrinsic size, shape
2917  !---------------------------------------------------------------------------
2918 
2919  !--- get data information
2920  call file_get_datainfo( dinfo, & ! (out)
2921  fid, varname, step, .false., & ! (in)
2922  error ) ! (out)
2923 
2924  !--- verify
2925  if ( error /= success_code ) then
2926  if ( present(allow_missing) ) then
2927  if ( allow_missing ) then
2928  write(message,*) 'xxx [INPUT]/[File] data not found! : ', &
2929  'varname= ',trim(varname),', step=',step
2930  call log('I', message)
2931  call log('I', 'xxx [INPUT]/[File] Value is set to 0.')
2932  var(:,:,:) = 0.0_sp
2933  else
2934  call log('E', 'xxx failed to get data information :'//trim(varname))
2935  end if
2936  else
2937  call log('E', 'xxx failed to get data information :'//trim(varname))
2938  end if
2939  end if
2940 
2941  if ( dinfo%rank /= 3 ) then
2942  write(message,*) 'xxx rank is not 3', dinfo%rank
2943  call log('E', message)
2944  end if
2945 
2946  if (present(ntypes) ) then
2947  call file_read_data_par( var(:,:,:), & ! (out)
2948  dinfo, size(shape(var)), ntypes, dtype, start, count, & ! (in)
2949  error ) ! (out)
2950  else
2951  call file_read_data( var(:,:,:), & ! (out)
2952  dinfo, sp, & ! (in)
2953  error ) ! (out)
2954  end if
2955  if ( error /= success_code ) then
2956  call log('E', 'xxx failed to get data value')
2957  end if
2958 
2959  return
2960  end subroutine filereadvar3drealsp
2961  subroutine filereadvar3drealdp( &
2962  var, & ! (out)
2963  fid, & ! (in)
2964  varname, & ! (in)
2965  step, & ! (in)
2966  allow_missing, & ! (in) optional
2967  single, & ! (in) optional
2968  ntypes, & ! (in)
2969  dtype, & ! (in)
2970  start, & ! (in)
2971  count & ! (in)
2972  )
2973  use mpi, only : mpi_comm_null
2974  implicit none
2975 
2976  real(DP), intent(out) :: var(:,:,:)
2977  integer, intent( in) :: fid
2978  character(len=*), intent( in) :: varname
2979  integer, intent( in) :: step
2980  logical, intent( in), optional :: allow_missing !--- if data is missing, set value to zero
2981  logical, intent( in), optional :: single
2982  integer, intent( in), optional :: ntypes ! number of dtypes
2983  integer, intent( in), optional :: dtype ! MPI derived datatype for read buffer
2984  integer, intent( in), optional :: start(:) ! request starts to global variable
2985  integer, intent( in), optional :: count(:) ! request sizes to global variable
2986 
2987  type(datainfo) :: dinfo
2988  integer :: error
2989 
2990  intrinsic size, shape
2991  !---------------------------------------------------------------------------
2992 
2993  !--- get data information
2994  call file_get_datainfo( dinfo, & ! (out)
2995  fid, varname, step, .false., & ! (in)
2996  error ) ! (out)
2997 
2998  !--- verify
2999  if ( error /= success_code ) then
3000  if ( present(allow_missing) ) then
3001  if ( allow_missing ) then
3002  write(message,*) 'xxx [INPUT]/[File] data not found! : ', &
3003  'varname= ',trim(varname),', step=',step
3004  call log('I', message)
3005  call log('I', 'xxx [INPUT]/[File] Value is set to 0.')
3006  var(:,:,:) = 0.0_dp
3007  else
3008  call log('E', 'xxx failed to get data information :'//trim(varname))
3009  end if
3010  else
3011  call log('E', 'xxx failed to get data information :'//trim(varname))
3012  end if
3013  end if
3014 
3015  if ( dinfo%rank /= 3 ) then
3016  write(message,*) 'xxx rank is not 3', dinfo%rank
3017  call log('E', message)
3018  end if
3019 
3020  if (present(ntypes) ) then
3021  call file_read_data_par( var(:,:,:), & ! (out)
3022  dinfo, size(shape(var)), ntypes, dtype, start, count, & ! (in)
3023  error ) ! (out)
3024  else
3025  call file_read_data( var(:,:,:), & ! (out)
3026  dinfo, dp, & ! (in)
3027  error ) ! (out)
3028  end if
3029  if ( error /= success_code ) then
3030  call log('E', 'xxx failed to get data value')
3031  end if
3032 
3033  return
3034  end subroutine filereadvar3drealdp
3035  subroutine filereadvar4drealsp( &
3036  var, & ! (out)
3037  fid, & ! (in)
3038  varname, & ! (in)
3039  step, & ! (in)
3040  allow_missing, & ! (in) optional
3041  single, & ! (in) optional
3042  ntypes, & ! (in)
3043  dtype, & ! (in)
3044  start, & ! (in)
3045  count & ! (in)
3046  )
3047  use mpi, only : mpi_comm_null
3048  implicit none
3049 
3050  real(SP), intent(out) :: var(:,:,:,:)
3051  integer, intent( in) :: fid
3052  character(len=*), intent( in) :: varname
3053  integer, intent( in) :: step
3054  logical, intent( in), optional :: allow_missing !--- if data is missing, set value to zero
3055  logical, intent( in), optional :: single
3056  integer, intent( in), optional :: ntypes ! number of dtypes
3057  integer, intent( in), optional :: dtype ! MPI derived datatype for read buffer
3058  integer, intent( in), optional :: start(:) ! request starts to global variable
3059  integer, intent( in), optional :: count(:) ! request sizes to global variable
3060 
3061  type(datainfo) :: dinfo
3062  integer :: error
3063 
3064  intrinsic size, shape
3065  !---------------------------------------------------------------------------
3066 
3067  !--- get data information
3068  call file_get_datainfo( dinfo, & ! (out)
3069  fid, varname, step, .false., & ! (in)
3070  error ) ! (out)
3071 
3072  !--- verify
3073  if ( error /= success_code ) then
3074  if ( present(allow_missing) ) then
3075  if ( allow_missing ) then
3076  write(message,*) 'xxx [INPUT]/[File] data not found! : ', &
3077  'varname= ',trim(varname),', step=',step
3078  call log('I', message)
3079  call log('I', 'xxx [INPUT]/[File] Value is set to 0.')
3080  var(:,:,:,:) = 0.0_sp
3081  else
3082  call log('E', 'xxx failed to get data information :'//trim(varname))
3083  end if
3084  else
3085  call log('E', 'xxx failed to get data information :'//trim(varname))
3086  end if
3087  end if
3088 
3089  if ( dinfo%rank /= 4 ) then
3090  write(message,*) 'xxx rank is not 4', dinfo%rank
3091  call log('E', message)
3092  end if
3093 
3094  if (present(ntypes) ) then
3095  call file_read_data_par( var(:,:,:,:), & ! (out)
3096  dinfo, size(shape(var)), ntypes, dtype, start, count, & ! (in)
3097  error ) ! (out)
3098  else
3099  call file_read_data( var(:,:,:,:), & ! (out)
3100  dinfo, sp, & ! (in)
3101  error ) ! (out)
3102  end if
3103  if ( error /= success_code ) then
3104  call log('E', 'xxx failed to get data value')
3105  end if
3106 
3107  return
3108  end subroutine filereadvar4drealsp
3109  subroutine filereadvar4drealdp( &
3110  var, & ! (out)
3111  fid, & ! (in)
3112  varname, & ! (in)
3113  step, & ! (in)
3114  allow_missing, & ! (in) optional
3115  single, & ! (in) optional
3116  ntypes, & ! (in)
3117  dtype, & ! (in)
3118  start, & ! (in)
3119  count & ! (in)
3120  )
3121  use mpi, only : mpi_comm_null
3122  implicit none
3123 
3124  real(DP), intent(out) :: var(:,:,:,:)
3125  integer, intent( in) :: fid
3126  character(len=*), intent( in) :: varname
3127  integer, intent( in) :: step
3128  logical, intent( in), optional :: allow_missing !--- if data is missing, set value to zero
3129  logical, intent( in), optional :: single
3130  integer, intent( in), optional :: ntypes ! number of dtypes
3131  integer, intent( in), optional :: dtype ! MPI derived datatype for read buffer
3132  integer, intent( in), optional :: start(:) ! request starts to global variable
3133  integer, intent( in), optional :: count(:) ! request sizes to global variable
3134 
3135  type(datainfo) :: dinfo
3136  integer :: error
3137 
3138  intrinsic size, shape
3139  !---------------------------------------------------------------------------
3140 
3141  !--- get data information
3142  call file_get_datainfo( dinfo, & ! (out)
3143  fid, varname, step, .false., & ! (in)
3144  error ) ! (out)
3145 
3146  !--- verify
3147  if ( error /= success_code ) then
3148  if ( present(allow_missing) ) then
3149  if ( allow_missing ) then
3150  write(message,*) 'xxx [INPUT]/[File] data not found! : ', &
3151  'varname= ',trim(varname),', step=',step
3152  call log('I', message)
3153  call log('I', 'xxx [INPUT]/[File] Value is set to 0.')
3154  var(:,:,:,:) = 0.0_dp
3155  else
3156  call log('E', 'xxx failed to get data information :'//trim(varname))
3157  end if
3158  else
3159  call log('E', 'xxx failed to get data information :'//trim(varname))
3160  end if
3161  end if
3162 
3163  if ( dinfo%rank /= 4 ) then
3164  write(message,*) 'xxx rank is not 4', dinfo%rank
3165  call log('E', message)
3166  end if
3167 
3168  if (present(ntypes) ) then
3169  call file_read_data_par( var(:,:,:,:), & ! (out)
3170  dinfo, size(shape(var)), ntypes, dtype, start, count, & ! (in)
3171  error ) ! (out)
3172  else
3173  call file_read_data( var(:,:,:,:), & ! (out)
3174  dinfo, dp, & ! (in)
3175  error ) ! (out)
3176  end if
3177  if ( error /= success_code ) then
3178  call log('E', 'xxx failed to get data value')
3179  end if
3180 
3181  return
3182  end subroutine filereadvar4drealdp
3183 
3184  !-----------------------------------------------------------------------------
3185  ! interface FileWrite
3186  !-----------------------------------------------------------------------------
3187  subroutine filewrite1drealsp( &
3188  fid, & ! (in)
3189  vid, & ! (in)
3190  var, & ! (in)
3191  t_start, & ! (in)
3192  t_end, & ! (in)
3193  start, & ! (in)
3194  count, & ! (in)
3195  ndims & ! (in)
3196  )
3197  implicit none
3198 
3199  real(SP), intent(in) :: var(:)
3200  integer, intent(in) :: fid
3201  integer, intent(in) :: vid
3202  real(DP), intent(in) :: t_start
3203  real(DP), intent(in) :: t_end
3204  integer, intent(in), optional :: start(:)
3205  integer, intent(in), optional :: count(:) ! when var has been reshaped to 1D
3206  integer, intent(in), optional :: ndims ! when var has been reshaped to 1D
3207 
3208  real(DP) :: ts, te
3209 
3210  integer :: start_(1)
3211 
3212  integer :: error, n
3213  character(len=100) :: str
3214 
3215  intrinsic shape
3216  !---------------------------------------------------------------------------
3217 
3218  ts = t_start
3219  te = t_end
3220 
3221  if ( present(ndims) ) then
3222  ! history variable has been reshaped to 1D
3223  ! In this case, start and count must be present
3224 
3225  if ( .not. present(start) ) then
3226  call log('E', 'start argument is neccessary when ndims is specified')
3227  end if
3228  if ( .not. present(count) ) then
3229  call log('E', 'count argument is neccessary when ndims is specified')
3230  end if
3231 
3232  call file_write_data( fid, vid, var(:), ts, te, sp, & ! (in)
3233  ndims, start, count, & ! (in)
3234  error ) ! (out)
3235  else
3236  ! this is for restart variable which keeps its original shape
3237  if ( present(start) ) then
3238  start_(:) = start(:)
3239  else
3240  start_(:) = 1
3241  end if
3242  call file_write_data( fid, vid, var(:), ts, te, sp, & ! (in)
3243  1, start_, shape(var), & ! (in)
3244  error ) ! (out)
3245  end if
3246  if ( error /= success_code ) then
3247  do n = 1, file_vid_count
3248  if ( file_vid_list(n) == vid ) then
3249  write(str,*) 'xxx failed to write data: ', trim(file_vname_list(n)), mpi_myrank
3250  exit
3251  end if
3252  enddo
3253  call log('E', trim(str))
3254  end if
3255 
3256  return
3257  end subroutine filewrite1drealsp
3258  subroutine filewrite1drealdp( &
3259  fid, & ! (in)
3260  vid, & ! (in)
3261  var, & ! (in)
3262  t_start, & ! (in)
3263  t_end, & ! (in)
3264  start, & ! (in)
3265  count, & ! (in)
3266  ndims & ! (in)
3267  )
3268  implicit none
3269 
3270  real(DP), intent(in) :: var(:)
3271  integer, intent(in) :: fid
3272  integer, intent(in) :: vid
3273  real(DP), intent(in) :: t_start
3274  real(DP), intent(in) :: t_end
3275  integer, intent(in), optional :: start(:)
3276  integer, intent(in), optional :: count(:) ! when var has been reshaped to 1D
3277  integer, intent(in), optional :: ndims ! when var has been reshaped to 1D
3278 
3279  real(DP) :: ts, te
3280 
3281  integer :: start_(1)
3282 
3283  integer :: error, n
3284  character(len=100) :: str
3285 
3286  intrinsic shape
3287  !---------------------------------------------------------------------------
3288 
3289  ts = t_start
3290  te = t_end
3291 
3292  if ( present(ndims) ) then
3293  ! history variable has been reshaped to 1D
3294  ! In this case, start and count must be present
3295 
3296  if ( .not. present(start) ) then
3297  call log('E', 'start argument is neccessary when ndims is specified')
3298  end if
3299  if ( .not. present(count) ) then
3300  call log('E', 'count argument is neccessary when ndims is specified')
3301  end if
3302 
3303  call file_write_data( fid, vid, var(:), ts, te, dp, & ! (in)
3304  ndims, start, count, & ! (in)
3305  error ) ! (out)
3306  else
3307  ! this is for restart variable which keeps its original shape
3308  if ( present(start) ) then
3309  start_(:) = start(:)
3310  else
3311  start_(:) = 1
3312  end if
3313  call file_write_data( fid, vid, var(:), ts, te, dp, & ! (in)
3314  1, start_, shape(var), & ! (in)
3315  error ) ! (out)
3316  end if
3317  if ( error /= success_code ) then
3318  do n = 1, file_vid_count
3319  if ( file_vid_list(n) == vid ) then
3320  write(str,*) 'xxx failed to write data: ', trim(file_vname_list(n)), mpi_myrank
3321  exit
3322  end if
3323  enddo
3324  call log('E', trim(str))
3325  end if
3326 
3327  return
3328  end subroutine filewrite1drealdp
3329  subroutine filewrite2drealsp( &
3330  fid, & ! (in)
3331  vid, & ! (in)
3332  var, & ! (in)
3333  t_start, & ! (in)
3334  t_end, & ! (in)
3335  start, & ! (in)
3336  count, & ! (in)
3337  ndims & ! (in)
3338  )
3339  implicit none
3340 
3341  real(SP), intent(in) :: var(:,:)
3342  integer, intent(in) :: fid
3343  integer, intent(in) :: vid
3344  real(DP), intent(in) :: t_start
3345  real(DP), intent(in) :: t_end
3346  integer, intent(in), optional :: start(:)
3347  integer, intent(in), optional :: count(:) ! when var has been reshaped to 1D
3348  integer, intent(in), optional :: ndims ! when var has been reshaped to 1D
3349 
3350  real(DP) :: ts, te
3351 
3352  integer :: start_(2)
3353 
3354  integer :: error, n
3355  character(len=100) :: str
3356 
3357  intrinsic shape
3358  !---------------------------------------------------------------------------
3359 
3360  ts = t_start
3361  te = t_end
3362 
3363  if ( present(ndims) ) then
3364  ! history variable has been reshaped to 1D
3365  ! In this case, start and count must be present
3366 
3367  if ( .not. present(start) ) then
3368  call log('E', 'start argument is neccessary when ndims is specified')
3369  end if
3370  if ( .not. present(count) ) then
3371  call log('E', 'count argument is neccessary when ndims is specified')
3372  end if
3373 
3374  call file_write_data( fid, vid, var(:,:), ts, te, sp, & ! (in)
3375  ndims, start, count, & ! (in)
3376  error ) ! (out)
3377  else
3378  ! this is for restart variable which keeps its original shape
3379  if ( present(start) ) then
3380  start_(:) = start(:)
3381  else
3382  start_(:) = 1
3383  end if
3384  call file_write_data( fid, vid, var(:,:), ts, te, sp, & ! (in)
3385  2, start_, shape(var), & ! (in)
3386  error ) ! (out)
3387  end if
3388  if ( error /= success_code ) then
3389  do n = 1, file_vid_count
3390  if ( file_vid_list(n) == vid ) then
3391  write(str,*) 'xxx failed to write data: ', trim(file_vname_list(n)), mpi_myrank
3392  exit
3393  end if
3394  enddo
3395  call log('E', trim(str))
3396  end if
3397 
3398  return
3399  end subroutine filewrite2drealsp
3400  subroutine filewrite2drealdp( &
3401  fid, & ! (in)
3402  vid, & ! (in)
3403  var, & ! (in)
3404  t_start, & ! (in)
3405  t_end, & ! (in)
3406  start, & ! (in)
3407  count, & ! (in)
3408  ndims & ! (in)
3409  )
3410  implicit none
3411 
3412  real(DP), intent(in) :: var(:,:)
3413  integer, intent(in) :: fid
3414  integer, intent(in) :: vid
3415  real(DP), intent(in) :: t_start
3416  real(DP), intent(in) :: t_end
3417  integer, intent(in), optional :: start(:)
3418  integer, intent(in), optional :: count(:) ! when var has been reshaped to 1D
3419  integer, intent(in), optional :: ndims ! when var has been reshaped to 1D
3420 
3421  real(DP) :: ts, te
3422 
3423  integer :: start_(2)
3424 
3425  integer :: error, n
3426  character(len=100) :: str
3427 
3428  intrinsic shape
3429  !---------------------------------------------------------------------------
3430 
3431  ts = t_start
3432  te = t_end
3433 
3434  if ( present(ndims) ) then
3435  ! history variable has been reshaped to 1D
3436  ! In this case, start and count must be present
3437 
3438  if ( .not. present(start) ) then
3439  call log('E', 'start argument is neccessary when ndims is specified')
3440  end if
3441  if ( .not. present(count) ) then
3442  call log('E', 'count argument is neccessary when ndims is specified')
3443  end if
3444 
3445  call file_write_data( fid, vid, var(:,:), ts, te, dp, & ! (in)
3446  ndims, start, count, & ! (in)
3447  error ) ! (out)
3448  else
3449  ! this is for restart variable which keeps its original shape
3450  if ( present(start) ) then
3451  start_(:) = start(:)
3452  else
3453  start_(:) = 1
3454  end if
3455  call file_write_data( fid, vid, var(:,:), ts, te, dp, & ! (in)
3456  2, start_, shape(var), & ! (in)
3457  error ) ! (out)
3458  end if
3459  if ( error /= success_code ) then
3460  do n = 1, file_vid_count
3461  if ( file_vid_list(n) == vid ) then
3462  write(str,*) 'xxx failed to write data: ', trim(file_vname_list(n)), mpi_myrank
3463  exit
3464  end if
3465  enddo
3466  call log('E', trim(str))
3467  end if
3468 
3469  return
3470  end subroutine filewrite2drealdp
3471  subroutine filewrite3drealsp( &
3472  fid, & ! (in)
3473  vid, & ! (in)
3474  var, & ! (in)
3475  t_start, & ! (in)
3476  t_end, & ! (in)
3477  start, & ! (in)
3478  count, & ! (in)
3479  ndims & ! (in)
3480  )
3481  implicit none
3482 
3483  real(SP), intent(in) :: var(:,:,:)
3484  integer, intent(in) :: fid
3485  integer, intent(in) :: vid
3486  real(DP), intent(in) :: t_start
3487  real(DP), intent(in) :: t_end
3488  integer, intent(in), optional :: start(:)
3489  integer, intent(in), optional :: count(:) ! when var has been reshaped to 1D
3490  integer, intent(in), optional :: ndims ! when var has been reshaped to 1D
3491 
3492  real(DP) :: ts, te
3493 
3494  integer :: start_(3)
3495 
3496  integer :: error, n
3497  character(len=100) :: str
3498 
3499  intrinsic shape
3500  !---------------------------------------------------------------------------
3501 
3502  ts = t_start
3503  te = t_end
3504 
3505  if ( present(ndims) ) then
3506  ! history variable has been reshaped to 1D
3507  ! In this case, start and count must be present
3508 
3509  if ( .not. present(start) ) then
3510  call log('E', 'start argument is neccessary when ndims is specified')
3511  end if
3512  if ( .not. present(count) ) then
3513  call log('E', 'count argument is neccessary when ndims is specified')
3514  end if
3515 
3516  call file_write_data( fid, vid, var(:,:,:), ts, te, sp, & ! (in)
3517  ndims, start, count, & ! (in)
3518  error ) ! (out)
3519  else
3520  ! this is for restart variable which keeps its original shape
3521  if ( present(start) ) then
3522  start_(:) = start(:)
3523  else
3524  start_(:) = 1
3525  end if
3526  call file_write_data( fid, vid, var(:,:,:), ts, te, sp, & ! (in)
3527  3, start_, shape(var), & ! (in)
3528  error ) ! (out)
3529  end if
3530  if ( error /= success_code ) then
3531  do n = 1, file_vid_count
3532  if ( file_vid_list(n) == vid ) then
3533  write(str,*) 'xxx failed to write data: ', trim(file_vname_list(n)), mpi_myrank
3534  exit
3535  end if
3536  enddo
3537  call log('E', trim(str))
3538  end if
3539 
3540  return
3541  end subroutine filewrite3drealsp
3542  subroutine filewrite3drealdp( &
3543  fid, & ! (in)
3544  vid, & ! (in)
3545  var, & ! (in)
3546  t_start, & ! (in)
3547  t_end, & ! (in)
3548  start, & ! (in)
3549  count, & ! (in)
3550  ndims & ! (in)
3551  )
3552  implicit none
3553 
3554  real(DP), intent(in) :: var(:,:,:)
3555  integer, intent(in) :: fid
3556  integer, intent(in) :: vid
3557  real(DP), intent(in) :: t_start
3558  real(DP), intent(in) :: t_end
3559  integer, intent(in), optional :: start(:)
3560  integer, intent(in), optional :: count(:) ! when var has been reshaped to 1D
3561  integer, intent(in), optional :: ndims ! when var has been reshaped to 1D
3562 
3563  real(DP) :: ts, te
3564 
3565  integer :: start_(3)
3566 
3567  integer :: error, n
3568  character(len=100) :: str
3569 
3570  intrinsic shape
3571  !---------------------------------------------------------------------------
3572 
3573  ts = t_start
3574  te = t_end
3575 
3576  if ( present(ndims) ) then
3577  ! history variable has been reshaped to 1D
3578  ! In this case, start and count must be present
3579 
3580  if ( .not. present(start) ) then
3581  call log('E', 'start argument is neccessary when ndims is specified')
3582  end if
3583  if ( .not. present(count) ) then
3584  call log('E', 'count argument is neccessary when ndims is specified')
3585  end if
3586 
3587  call file_write_data( fid, vid, var(:,:,:), ts, te, dp, & ! (in)
3588  ndims, start, count, & ! (in)
3589  error ) ! (out)
3590  else
3591  ! this is for restart variable which keeps its original shape
3592  if ( present(start) ) then
3593  start_(:) = start(:)
3594  else
3595  start_(:) = 1
3596  end if
3597  call file_write_data( fid, vid, var(:,:,:), ts, te, dp, & ! (in)
3598  3, start_, shape(var), & ! (in)
3599  error ) ! (out)
3600  end if
3601  if ( error /= success_code ) then
3602  do n = 1, file_vid_count
3603  if ( file_vid_list(n) == vid ) then
3604  write(str,*) 'xxx failed to write data: ', trim(file_vname_list(n)), mpi_myrank
3605  exit
3606  end if
3607  enddo
3608  call log('E', trim(str))
3609  end if
3610 
3611  return
3612  end subroutine filewrite3drealdp
3613  subroutine filewrite4drealsp( &
3614  fid, & ! (in)
3615  vid, & ! (in)
3616  var, & ! (in)
3617  t_start, & ! (in)
3618  t_end, & ! (in)
3619  start, & ! (in)
3620  count, & ! (in)
3621  ndims & ! (in)
3622  )
3623  implicit none
3624 
3625  real(SP), intent(in) :: var(:,:,:,:)
3626  integer, intent(in) :: fid
3627  integer, intent(in) :: vid
3628  real(DP), intent(in) :: t_start
3629  real(DP), intent(in) :: t_end
3630  integer, intent(in), optional :: start(:)
3631  integer, intent(in), optional :: count(:) ! when var has been reshaped to 1D
3632  integer, intent(in), optional :: ndims ! when var has been reshaped to 1D
3633 
3634  real(DP) :: ts, te
3635 
3636  integer :: start_(4)
3637 
3638  integer :: error, n
3639  character(len=100) :: str
3640 
3641  intrinsic shape
3642  !---------------------------------------------------------------------------
3643 
3644  ts = t_start
3645  te = t_end
3646 
3647  if ( present(ndims) ) then
3648  ! history variable has been reshaped to 1D
3649  ! In this case, start and count must be present
3650 
3651  if ( .not. present(start) ) then
3652  call log('E', 'start argument is neccessary when ndims is specified')
3653  end if
3654  if ( .not. present(count) ) then
3655  call log('E', 'count argument is neccessary when ndims is specified')
3656  end if
3657 
3658  call file_write_data( fid, vid, var(:,:,:,:), ts, te, sp, & ! (in)
3659  ndims, start, count, & ! (in)
3660  error ) ! (out)
3661  else
3662  ! this is for restart variable which keeps its original shape
3663  if ( present(start) ) then
3664  start_(:) = start(:)
3665  else
3666  start_(:) = 1
3667  end if
3668  call file_write_data( fid, vid, var(:,:,:,:), ts, te, sp, & ! (in)
3669  4, start_, shape(var), & ! (in)
3670  error ) ! (out)
3671  end if
3672  if ( error /= success_code ) then
3673  do n = 1, file_vid_count
3674  if ( file_vid_list(n) == vid ) then
3675  write(str,*) 'xxx failed to write data: ', trim(file_vname_list(n)), mpi_myrank
3676  exit
3677  end if
3678  enddo
3679  call log('E', trim(str))
3680  end if
3681 
3682  return
3683  end subroutine filewrite4drealsp
3684  subroutine filewrite4drealdp( &
3685  fid, & ! (in)
3686  vid, & ! (in)
3687  var, & ! (in)
3688  t_start, & ! (in)
3689  t_end, & ! (in)
3690  start, & ! (in)
3691  count, & ! (in)
3692  ndims & ! (in)
3693  )
3694  implicit none
3695 
3696  real(DP), intent(in) :: var(:,:,:,:)
3697  integer, intent(in) :: fid
3698  integer, intent(in) :: vid
3699  real(DP), intent(in) :: t_start
3700  real(DP), intent(in) :: t_end
3701  integer, intent(in), optional :: start(:)
3702  integer, intent(in), optional :: count(:) ! when var has been reshaped to 1D
3703  integer, intent(in), optional :: ndims ! when var has been reshaped to 1D
3704 
3705  real(DP) :: ts, te
3706 
3707  integer :: start_(4)
3708 
3709  integer :: error, n
3710  character(len=100) :: str
3711 
3712  intrinsic shape
3713  !---------------------------------------------------------------------------
3714 
3715  ts = t_start
3716  te = t_end
3717 
3718  if ( present(ndims) ) then
3719  ! history variable has been reshaped to 1D
3720  ! In this case, start and count must be present
3721 
3722  if ( .not. present(start) ) then
3723  call log('E', 'start argument is neccessary when ndims is specified')
3724  end if
3725  if ( .not. present(count) ) then
3726  call log('E', 'count argument is neccessary when ndims is specified')
3727  end if
3728 
3729  call file_write_data( fid, vid, var(:,:,:,:), ts, te, dp, & ! (in)
3730  ndims, start, count, & ! (in)
3731  error ) ! (out)
3732  else
3733  ! this is for restart variable which keeps its original shape
3734  if ( present(start) ) then
3735  start_(:) = start(:)
3736  else
3737  start_(:) = 1
3738  end if
3739  call file_write_data( fid, vid, var(:,:,:,:), ts, te, dp, & ! (in)
3740  4, start_, shape(var), & ! (in)
3741  error ) ! (out)
3742  end if
3743  if ( error /= success_code ) then
3744  do n = 1, file_vid_count
3745  if ( file_vid_list(n) == vid ) then
3746  write(str,*) 'xxx failed to write data: ', trim(file_vname_list(n)), mpi_myrank
3747  exit
3748  end if
3749  enddo
3750  call log('E', trim(str))
3751  end if
3752 
3753  return
3754  end subroutine filewrite4drealdp
3755 
3756  !-----------------------------------------------------------------------------
3757  ! exit netCDF define mode and enter data mode
3758  subroutine fileenddef( &
3759  fid & ! (in)
3760  )
3761  implicit none
3762 
3763  integer, intent(in) :: fid
3764 
3765  integer :: error, n
3766  !---------------------------------------------------------------------------
3767 
3768  if ( fid < 0 ) return
3769 
3770  do n = 1, file_fid_count-1
3771  if ( file_fid_list(n) == fid ) exit
3772  end do
3773  if ( fid .NE. file_fid_list(n) ) then
3774  write(message,*) 'xxx in FileEndDef invalid fid' , fid
3775  call log('E', message)
3776  end if
3777 
3778  call file_enddef( fid, error )
3779 
3780  if ( error == success_code ) then
3781 
3782  call log("I",'')
3783  write(message,'(A,I3.3,2A)') '###### File end define mode : No.', file_fid_list(n), &
3784  ', name = ', trim(file_fname_list(n))
3785  call log("I",message)
3786  call log("I",'')
3787 
3788  else
3789 
3790  call log('E', 'xxx failed to exit define mode')
3791 
3792  end if
3793 
3794  return
3795  end subroutine fileenddef
3796 
3797  !-----------------------------------------------------------------------------
3798  ! This subroutine is used when PnetCDF I/O method is enabled
3799  subroutine fileattachbuffer( &
3800  fid, & ! (in)
3801  buf_amount ) ! (in)
3802  implicit none
3803 
3804  integer, intent(in) :: fid
3805  integer, intent(in) :: buf_amount
3806 
3807  integer :: error, n
3808  !---------------------------------------------------------------------------
3809 
3810  if ( fid < 0 ) return
3811 
3812  do n = 1, file_fid_count-1
3813  if ( file_fid_list(n) == fid ) exit
3814  end do
3815  if ( fid .NE. file_fid_list(n) ) then
3816  write(message,*) 'xxx in FileAttachBuffer invalid fid' , fid
3817  call log('E', message)
3818  end if
3819 
3820  call file_attach_buffer( fid, buf_amount, error )
3821 
3822  if ( error == success_code ) then
3823 
3824  write(message,'(A,I3.3,3A,I10)') '###### File attach buffer : No.', file_fid_list(n), &
3825  ', name = ', trim(file_fname_list(n)), &
3826  ', size = ', buf_amount
3827  call log("I",message)
3828 
3829  else
3830 
3831  call log('E', 'xxx failed to attach buffer in PnetCDF')
3832 
3833  end if
3834 
3835  return
3836  end subroutine fileattachbuffer
3837 
3838  !-----------------------------------------------------------------------------
3839  ! This subroutine is used when PnetCDF I/O method is enabled
3840  subroutine filedetachbuffer( &
3841  fid ) ! (in)
3842  implicit none
3843 
3844  integer, intent(in) :: fid
3845 
3846  integer :: error, n
3847  !---------------------------------------------------------------------------
3848 
3849  if ( fid < 0 ) return
3850 
3851  do n = 1, file_fid_count-1
3852  if ( file_fid_list(n) == fid ) exit
3853  end do
3854  if ( n == file_fid_count ) return ! already closed
3855 
3856  if ( fid .NE. file_fid_list(n) ) then
3857  write(message,*) 'xxx in FileDetachBuffer invalid fid' , fid
3858  call log('E', message)
3859  end if
3860 
3861  call file_detach_buffer( fid, error )
3862 
3863  if ( error == success_code ) then
3864 
3865  write(message,'(A,I3.3,2A)') '###### File detach buffer : No.', file_fid_list(n), &
3866  ', name = ', trim(file_fname_list(n))
3867  call log("I",message)
3868 
3869  else
3870 
3871  call log('E', 'xxx failed to detach buffer in PnetCDF')
3872 
3873  end if
3874 
3875  return
3876  end subroutine filedetachbuffer
3877 
3878  !-----------------------------------------------------------------------------
3879  ! This subroutine is used when PnetCDF I/O method is enabled
3880  subroutine fileflush( &
3881  fid & ! (in)
3882  )
3883  implicit none
3884 
3885  integer, intent(in) :: fid
3886 
3887  integer :: error, n
3888  !---------------------------------------------------------------------------
3889 
3890  if ( fid < 0 ) return
3891  if ( file_fid_count == 1 ) return
3892 
3893  do n = 1, file_fid_count-1
3894  if ( file_fid_list(n) == fid ) exit
3895  end do
3896  if ( n == file_fid_count ) return ! already closed
3897 
3898  if ( fid .NE. file_fid_list(n) ) then
3899  write(message,*) 'xxx in FileFlush invalid fid' , fid
3900  call log('E', message)
3901  end if
3902 
3903  call file_flush( fid, error )
3904 
3905  if ( error == success_code ) then
3906 
3907  write(message,'(A,I3.3,2A)') '###### File flush : No.', file_fid_list(n), &
3908  ', name = ', trim(file_fname_list(n))
3909  call log("I",message)
3910  call log("I",'')
3911 
3912  else
3913 
3914  call log('E', 'xxx failed to flush PnetCDF pending requests')
3915 
3916  end if
3917 
3918  return
3919  end subroutine fileflush
3920 
3921  !-----------------------------------------------------------------------------
3922  subroutine fileclose( &
3923  fid & ! (in)
3924  )
3925  implicit none
3926 
3927  integer, intent(in) :: fid
3928 
3929  character(len=File_HLONG) :: fname
3930  integer :: error
3931  integer :: n
3932  !---------------------------------------------------------------------------
3933 
3934  if ( fid < 0 ) return
3935 
3936  do n = 1, file_fid_count-1
3937  if ( file_fid_list(n) == fid ) exit
3938  end do
3939  if ( n == file_fid_count ) return ! already closed
3940 
3941  if ( fid /= file_fid_list(n) ) then
3942  write(message,*) 'xxx in FileClose invalid fid ', fid
3943  call log('E', message)
3944  end if
3945 
3946  call file_close( fid, error )
3947 
3948  if ( error == success_code ) then
3949 
3950  write(message,'(A,I3.3,2A)') '###### File close : No.', file_fid_list(n), &
3951  ', name = ', trim(file_fname_list(n))
3952  call log("I",message)
3953  call log("I",'')
3954 
3955  elseif( error /= already_closed_code ) then
3956 
3957  call log('E', 'xxx failed to close file')
3958 
3959  end if
3960 
3961  do n = 1, file_fid_count-1
3962  if ( file_fid_list(n) == fid ) then
3963  file_fid_list(n) = -1
3964  file_fname_list(n) = ''
3965  end if
3966  end do
3967 
3968  return
3969  end subroutine fileclose
3970  !-----------------------------------------------------------------------------
3971  subroutine filecloseall
3972  implicit none
3973 
3974  integer n
3975  !---------------------------------------------------------------------------
3976 
3977  do n = 1, file_fid_count-1
3978  call fileclose( file_fid_list(n) )
3979  enddo
3980 
3981  return
3982  end subroutine filecloseall
3983 
3984  !-----------------------------------------------------------------------------
3985  ! private
3986  !-----------------------------------------------------------------------------
3987  subroutine filemakefname( &
3988  fname, & ! (out)
3989  basename, & ! (in)
3990  prefix, & ! (in)
3991  myrank, & ! (in)
3992  len ) ! (in)
3993  character(len=*), intent(out) :: fname
3994  character(len=*), intent( in) :: basename
3995  character(len=*), intent( in) :: prefix
3996  integer, intent( in) :: myrank
3997  integer, intent( in) :: len
3998 
3999  ! 12345678901234567
4000  character(len=17) :: fmt = "(A, '.', A, I*.*)"
4001  !---------------------------------------------------------------------------
4002 
4003  if ( len < 1 .or. len > 9 ) then
4004  call log('E', 'xxx len is invalid')
4005  end if
4006 
4007  write(fmt(14:14),'(I1)') len
4008  write(fmt(16:16),'(I1)') len
4009  write(fname, fmt) trim(basename), trim(prefix), myrank
4010 
4011  return
4012  end subroutine filemakefname
4013  !-----------------------------------------------------------------------------
4014  subroutine filegetfid( &
4015  fid, &
4016  existed, &
4017  basename, &
4018  mode, &
4019  single, &
4020  comm )
4021  use mpi, only : mpi_comm_null, mpi_comm_self
4022  implicit none
4023 
4024  integer, intent(out) :: fid
4025  logical, intent(out) :: existed
4026  character(len=*), intent( in) :: basename
4027  integer, intent( in) :: mode
4028  logical, intent( in) :: single
4029  integer, intent( in), optional :: comm
4030 
4031  character(len=File_HSHORT) :: rwname(0:2)
4032  data rwname / 'READ','WRITE','APPEND' /
4033 
4034  character(len=File_HLONG) :: fname
4035  integer :: n
4036 
4037  integer :: error
4038  integer :: comm_
4039  !---------------------------------------------------------------------------
4040 
4041  !--- register new file and open
4042  comm_ = mpi_comm_null
4043  if ( present(comm) ) comm_ = comm
4044  if ( comm_ .NE. mpi_comm_null ) then
4045  ! parallel I/O on a single shared netCDF file
4046  fname = basename
4047  comm_ = comm
4048  elseif ( single ) then
4049  fname = trim(basename)//'.peall'
4050  else
4051  call filemakefname(fname,trim(basename),'pe',mpi_myrank,6)
4052  endif
4053 
4054  !--- search existing file
4055  fid = -1
4056  do n = 1, file_fid_count-1
4057  if ( fname==file_fname_list(n) ) fid = file_fid_list(n)
4058  enddo
4059 
4060  if ( fid >= 0 ) then
4061  existed = .true.
4062  return
4063  end if
4064 
4065  call file_open( fid, & ! (out)
4066  fname, mode, comm_, & ! (in)
4067  error ) ! (out)
4068 
4069  if ( error /= success_code ) then
4070  call log('E', 'xxx failed to open file :'//trim(fname)//'.nc')
4071  end if
4072 
4073  call log("I",'')
4074  write(message,'(3A,I3.3,2A)') '###### File registration (', &
4075  trim(rwname(mode)), ') : No.', fid, ', name = ', trim(fname)
4076  call log("I",message)
4077 
4078  file_fname_list(file_fid_count) = trim(fname)
4079  file_fid_list(file_fid_count) = fid
4080  file_fid_count = file_fid_count + 1
4081 
4082  existed = .false.
4083 
4084  return
4085  end subroutine filegetfid
4086 
4087 end module gtool_file
4088 !-------------------------------------------------------------------------------
4089 
4090 
4091 !--
4092 ! vi:set readonly sw=4 ts=8
4093 !
4094 !Local Variables:
4095 !mode: f90
4096 !buffer-read-only: t
4097 !End:
4098 !
4099 !++
int32_t file_set_tunits(int32_t fid, char *time_units)
Definition: gtool_netcdf.c:647
module GTOOL_FILE
Definition: gtool_file.f90:17
integer, parameter, public file_fwrite
int32_t file_open(int32_t *fid, char *fname, int32_t mode, MPI_Comm comm)
Definition: gtool_netcdf.c:132
subroutine filewrite1drealsp(fid, vid, var, t_start, t_end, start, count, ndims)
module DC_Log
Definition: dc_log.f90:14
real(dp), parameter, public rmiss
Definition: gtool_file.f90:150
subroutine filereadvar2drealdp(var, fid, varname, step, allow_missing, single, ntypes, dtype, start, count)
int32_t file_set_tattr(int32_t fid, char *vname, char *key, char *val)
Definition: gtool_netcdf.c:655
subroutine filereadvar1drealdp(var, fid, varname, step, allow_missing, single, ntypes, dtype, start, count)
int32_t file_set_global_attribute_text(int32_t fid, char *key, char *value)
Definition: gtool_netcdf.c:540
int32_t file_attach_buffer(int32_t fid, int32_t buf_amount)
int32_t file_set_global_attribute_double(int32_t fid, char *key, double *value, size_t len)
Definition: gtool_netcdf.c:620
integer, parameter, public log_lmsg
Definition: dc_log.f90:48
integer, parameter, public file_fappend
subroutine, public filedefinevariable(fid, vid, varname, desc, units, ndims, dims, dtype, tint, tavg)
subroutine, public fileenddef(fid)
integer, parameter, public already_existed_code
int32_t file_set_global_attribute_float(int32_t fid, char *key, float *value, size_t len)
Definition: gtool_netcdf.c:593
int32_t file_get_global_attribute_int(int32_t fid, char *key, int32_t *value, size_t len)
int32_t file_get_global_attribute_float(int32_t fid, char *key, float *value, size_t len)
Definition: gtool_netcdf.c:504
subroutine, public fileattachbuffer(fid, buf_amount)
subroutine, public filecloseall
int32_t file_get_datainfo(datainfo_t *dinfo, int32_t fid, char *varname, int32_t step, int32_t suppress)
Definition: gtool_netcdf.c:218
int32_t file_enddef(int32_t fid)
subroutine filereadvar3drealdp(var, fid, varname, step, allow_missing, single, ntypes, dtype, start, count)
int32_t file_set_global_attribute_int(int32_t fid, char *key, int32_t *value, size_t len)
int32_t file_get_global_attribute_double(int32_t fid, char *key, double *value, size_t len)
Definition: gtool_netcdf.c:522
integer, parameter, public success_code
subroutine filereadvar3drealsp(var, fid, varname, step, allow_missing, single, ntypes, dtype, start, count)
subroutine, public fileclose(fid)
int32_t file_put_axis(int32_t fid, char *name, char *desc, char *units, char *dim_name, int32_t dtype, void *val, int32_t size, int32_t precision)
Definition: gtool_netcdf.c:694
subroutine, public filedefassociatedcoordinates(fid, name, desc, units, dim_names, dtype)
Definition: gtool_file.f90:903
integer, parameter, public dp
Definition: dc_types.f90:27
subroutine, public filesettattr(fid, vname, key, val)
subroutine, public filesetoption(fid, filetype, key, val)
Definition: gtool_file.f90:470
int32_t file_def_axis(int32_t fid, char *name, char *desc, char *units, char *dim_name, int32_t dtype, int32_t dim_size)
Definition: gtool_netcdf.c:748
subroutine, public filemakefname(fname, basename, prefix, myrank, len)
integer, parameter, public already_closed_code
subroutine filegetglobalattributetext(fid, key, val)
Definition: gtool_file.f90:278
subroutine filereadvar4drealdp(var, fid, varname, step, allow_missing, single, ntypes, dtype, start, count)
integer, parameter, public file_fread
int32_t file_read_data_par(void *var, datainfo_t *dinfo, MPI_Offset ntypes, MPI_Datatype dtype, MPI_Offset *start, MPI_Offset *count)
Definition: gtool_netcdf.c:439
subroutine, public log(type, message)
Definition: dc_log.f90:165
int32_t file_write_axis(int32_t fid, char *name, void *val, int32_t precision, MPI_Offset *start, MPI_Offset *count)
Definition: gtool_netcdf.c:797
subroutine filereadvar4drealsp(var, fid, varname, step, allow_missing, single, ntypes, dtype, start, count)
int32_t file_get_global_attribute_text(int32_t fid, char *key, char *value, int32_t len)
Definition: gtool_netcdf.c:466
subroutine filereadvar2drealsp(var, fid, varname, step, allow_missing, single, ntypes, dtype, start, count)
integer, parameter, public sp
Definition: dc_types.f90:30
int32_t file_def_associated_coordinates(int32_t fid, char *name, char *desc, char *units, char **dim_names, int32_t ndims, int32_t dtype)
Definition: gtool_netcdf.c:900
subroutine, public filedetachbuffer(fid)
int32_t file_write_data(int32_t fid, int32_t vid, void *var, real64_t t_start, real64_t t_end, int32_t precision, MPI_Offset *start, MPI_Offset *count)
int32_t file_put_associated_coordinates(int32_t fid, char *name, char *desc, char *units, char **dim_names, int32_t ndims, int32_t dtype, void *val, int32_t precision)
Definition: gtool_netcdf.c:842
subroutine, public fileflush(fid)
int32_t file_write_associated_coordinates(int32_t fid, char *name, void *val, int32_t precision, MPI_Offset *start, MPI_Offset *count)
Definition: gtool_netcdf.c:956
subroutine, public filecreate(fid, existed, basename, title, source, institution, master, myrank, rankidx, single, time_units, append, comm)
Definition: gtool_file.f90:191
module FILE I/O HEADER
subroutine, public fileopen(fid, basename, mode, single, comm, myrank)
Definition: gtool_file.f90:495
int32_t file_read_data(void *var, datainfo_t *dinfo, int32_t precision)
Definition: gtool_netcdf.c:390
int32_t file_flush(int32_t fid)
int32_t file_add_variable(int32_t *vid, int32_t fid, char *varname, char *desc, char *units, char **dims, int32_t ndims, int32_t dtype, real64_t tint, int32_t tavg)
int32_t file_detach_buffer(int32_t fid)
int32_t file_close(int32_t fid)
int32_t file_set_option(int32_t fid, char *filetype, char *key, char *val)
Definition: gtool_netcdf.c:202
subroutine, public filedefaxis(fid, name, desc, units, dim_name, dtype, dim_size)
Definition: gtool_file.f90:584
subroutine, public filegetshape(dims, basename, varname, myrank, single, error)