-
Notifications
You must be signed in to change notification settings - Fork 307
/
mpas_io.F
6482 lines (5579 loc) · 261 KB
/
mpas_io.F
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
! Copyright (c) 2013, Los Alamos National Security, LLC (LANS)
! and the University Corporation for Atmospheric Research (UCAR).
!
! Unless noted otherwise source code is licensed under the BSD license.
! Additional copyright and license information can be found in the LICENSE file
! distributed with this code, or at http://mpas-dev.github.com/license.html
!
module mpas_io
use mpas_derived_types
use mpas_attlist
use mpas_dmpar
use mpas_log
#ifdef MPAS_PIO_SUPPORT
use pio
use piolib_mod
use pionfatt_mod
use pio_types
#ifdef SINGLE_PRECISION
integer, parameter :: PIO_REALKIND = PIO_REAL
#else
integer, parameter :: PIO_REALKIND = PIO_DOUBLE
#endif
#endif
#ifdef MPAS_SMIOL_SUPPORT
use SMIOLf
#include "smiol_codes.inc"
#endif
#ifdef MPAS_PIO_SUPPORT
!
! PIO-based fill values
!
#ifdef USE_PIO2
integer, parameter :: MPAS_INT_FILLVAL = PIO_FILL_INT
character, parameter :: MPAS_CHAR_FILLVAL = achar(0) ! TODO: To be replaced with PIO_FILL_CHAR once PIO2 provides this variable
#else
integer, parameter :: MPAS_INT_FILLVAL = NF_FILL_INT
character, parameter :: MPAS_CHAR_FILLVAL = achar(NF_FILL_CHAR)
#endif
#ifdef USE_PIO2
#ifdef SINGLE_PRECISION
real (kind=RKIND), parameter :: MPAS_REAL_FILLVAL = PIO_FILL_FLOAT
#else
real (kind=RKIND), parameter :: MPAS_REAL_FILLVAL = PIO_FILL_DOUBLE
#endif
#else
#ifdef SINGLE_PRECISION
real (kind=RKIND), parameter :: MPAS_REAL_FILLVAL = NF_FILL_FLOAT
#else
real (kind=RKIND), parameter :: MPAS_REAL_FILLVAL = NF_FILL_DOUBLE
#endif
#endif
#else
#ifdef MPAS_SMIOL_SUPPORT
!
! SMIOL-based fill values
!
integer, parameter :: MPAS_INT_FILLVAL = huge(0)
character, parameter :: MPAS_CHAR_FILLVAL = achar(0)
real (kind=RKIND), parameter :: MPAS_REAL_FILLVAL = huge(0.0_RKIND)
#else
!
! Default fill values
!
integer, parameter :: MPAS_INT_FILLVAL = huge(1)
character, parameter :: MPAS_CHAR_FILLVAL = achar(0)
real (kind=RKIND), parameter :: MPAS_REAL_FILLVAL = huge(1.0_RKIND)
#endif
#endif
#ifdef MPAS_PIO_SUPPORT
integer, private :: io_global_err = PIO_noerr
#endif
#ifdef MPAS_SMIOL_SUPPORT
integer, private :: io_global_err = SMIOL_SUCCESS
#endif
interface MPAS_io_get_var
module procedure MPAS_io_get_var_int0d
module procedure MPAS_io_get_var_int1d
module procedure MPAS_io_get_var_int2d
module procedure MPAS_io_get_var_int3d
module procedure MPAS_io_get_var_int4d
module procedure MPAS_io_get_var_real0d
module procedure MPAS_io_get_var_real1d
module procedure MPAS_io_get_var_real2d
module procedure MPAS_io_get_var_real3d
module procedure MPAS_io_get_var_real4d
module procedure MPAS_io_get_var_real5d
module procedure MPAS_io_get_var_char0d
module procedure MPAS_io_get_var_char1d
end interface MPAS_io_get_var
interface MPAS_io_put_var
module procedure MPAS_io_put_var_int0d
module procedure MPAS_io_put_var_int1d
module procedure MPAS_io_put_var_int2d
module procedure MPAS_io_put_var_int3d
module procedure MPAS_io_put_var_int4d
module procedure MPAS_io_put_var_real0d
module procedure MPAS_io_put_var_real1d
module procedure MPAS_io_put_var_real2d
module procedure MPAS_io_put_var_real3d
module procedure MPAS_io_put_var_real4d
module procedure MPAS_io_put_var_real5d
module procedure MPAS_io_put_var_char0d
module procedure MPAS_io_put_var_char1d
end interface MPAS_io_put_var
interface MPAS_io_get_att
module procedure MPAS_io_get_att_int0d
module procedure MPAS_io_get_att_int1d
module procedure MPAS_io_get_att_real0d
module procedure MPAS_io_get_att_real1d
module procedure MPAS_io_get_att_text
end interface MPAS_io_get_att
interface MPAS_io_put_att
module procedure MPAS_io_put_att_int0d
module procedure MPAS_io_put_att_int1d
module procedure MPAS_io_put_att_real0d
module procedure MPAS_io_put_att_real1d
module procedure MPAS_io_put_att_text
end interface MPAS_io_put_att
contains
subroutine MPAS_io_init(ioContext, io_task_count, io_task_stride, io_system, ierr)
implicit none
type (mpas_io_context_type), intent(inout) :: ioContext
integer, intent(in) :: io_task_count
integer, intent(in) :: io_task_stride
#ifdef MPAS_PIO_SUPPORT
type (iosystem_desc_t), optional, pointer :: io_system
#else
integer, optional, pointer :: io_system
#endif
integer, intent(out), optional :: ierr
integer :: local_ierr
local_ierr = 0
! call mpas_log_write('Called MPAS_io_init()')
if (present(ierr)) ierr = MPAS_IO_NOERR
if (present(io_system)) then
#ifdef MPAS_PIO_SUPPORT
ioContext % pio_iosystem => io_system
#endif
else
!call mpas_log_write('MGD PIO_init')
if ( io_task_count < 0 .or. io_task_count > ioContext % dminfo % nprocs ) then
call mpas_log_write('PIO io_task_count has a value of $i.', MPAS_LOG_ERR, intArgs=(/io_task_count/))
call mpas_log_write(' It must be between 1 and $i.', MPAS_LOG_ERR, intArgs=(/ioContext % dminfo % nprocs/) )
local_ierr = 1
end if
if ( io_task_stride <= 0 .or. io_task_stride > ioContext % dminfo % nprocs ) then
call mpas_log_write('PIO io_task_stride has a value of $i.', MPAS_LOG_ERR, intArgs=(/io_task_stride/) )
call mpas_log_write(' It must be between 1 and $i.', MPAS_LOG_ERR, intArgs=(/ioContext % dminfo % nprocs/) )
local_ierr = 1
end if
if ( io_task_stride * io_task_count > ioContext % dminfo % nprocs ) then
call mpas_log_write('PIO io_task_stride * io_task_count has a value of $i.', MPAS_LOG_ERR, &
intArgs=(/io_task_stride * io_task_count/) )
call mpas_log_write(' It must be between 1 and $i.', MPAS_LOG_ERR, intArgs=(/ioContext % dminfo % nprocs/) )
local_ierr = 1
end if
if ( local_ierr /= 0 ) then
call mpas_log_write('Invalid PIO configuration.', MPAS_LOG_CRIT)
end if
#ifdef MPAS_PIO_SUPPORT
allocate(ioContext % pio_iosystem)
call PIO_init(ioContext % dminfo % my_proc_id, & ! comp_rank
#ifdef MPAS_USE_MPI_F08
ioContext % dminfo % comm % mpi_val, & ! comp_comm
#else
ioContext % dminfo % comm, & ! comp_comm
#endif
io_task_count, & ! num_iotasks
0, & ! num_aggregator
io_task_stride, & ! stride
PIO_rearr_box, & ! rearr
ioContext % pio_iosystem) ! iosystem
#endif
#ifdef MPAS_SMIOL_SUPPORT
allocate(ioContext % smiol_context)
#ifdef MPAS_USE_MPI_F08
local_ierr = SMIOLf_init(ioContext % dminfo % comm % mpi_val, &
#else
local_ierr = SMIOLf_init(ioContext % dminfo % comm, &
#endif
io_task_count, &
io_task_stride, &
iocontext % smiol_context)
if (local_ierr /= SMIOL_SUCCESS) then
call mpas_log_write('SMIOLf_init failed with error $i', intArgs=[local_ierr], messageType=MPAS_LOG_ERR)
if (local_ierr == SMIOL_LIBRARY_ERROR) then
call mpas_log_write(trim(SMIOLf_lib_error_string(ioContext % smiol_context)), messageType=MPAS_LOG_CRIT)
else
call mpas_log_write(trim(SMIOLf_error_string(local_ierr)), messageType=MPAS_LOG_CRIT)
end if
end if
#endif
end if
#ifdef MPAS_PIO_SUPPORT
call pio_seterrorhandling(ioContext % pio_iosystem, PIO_BCAST_ERROR)
#endif
end subroutine MPAS_io_init
!***********************************************************************
!
! routine MPAS_io_set_iotype
!
!> \brief Set master PIO io type
!> \author Doug Jacobsen
!> \date 10/18/2013
!> \details
!> This routine sets the master io type for use with PIO.
!
!-----------------------------------------------------------------------
subroutine MPAS_io_set_iotype(ioContext, io_type_in, ierr)
implicit none
type (mpas_io_context_type), intent(inout) :: ioContext
integer, intent(in) :: io_type_in
integer, intent(out), optional :: ierr
if (present(ierr)) then
ierr = MPAS_IO_NOERR
end if
ioContext % master_pio_iotype = io_type_in
end subroutine MPAS_io_set_iotype
!***********************************************************************
!
! routine MPAS_io_unset_iotype
!
!> \brief Unset master PIO io type
!> \author Doug Jacobsen
!> \date 10/18/2013
!> \details
!> This routine sets the master io type for use with PIO to it's default
!> "unset" value.
!
!-----------------------------------------------------------------------
subroutine MPAS_io_unset_iotype(ioContext, ierr)
implicit none
type (mpas_io_context_type), intent(inout) :: ioContext
integer, intent(out), optional :: ierr
if (present(ierr)) then
ierr = MPAS_IO_NOERR
end if
ioContext % master_pio_iotype = -999
end subroutine MPAS_io_unset_iotype
type (MPAS_IO_Handle_type) function MPAS_io_open(filename, mode, ioformat, ioContext, &
clobber_file, truncate_file, pio_file_desc, ierr)
implicit none
character (len=*), intent(in) :: filename
integer, intent(in) :: mode
integer, intent(in) :: ioformat
type (mpas_io_context_type), pointer :: ioContext
logical, intent(in), optional :: clobber_file
logical, intent(in), optional :: truncate_file
#ifdef MPAS_PIO_SUPPORT
type (file_desc_t), intent(inout), optional :: pio_file_desc
#else
integer, optional :: pio_file_desc
#endif
integer, intent(out), optional :: ierr
integer :: pio_ierr, pio_iotype, pio_mode
integer :: local_ierr
logical :: local_clobber, local_truncate
logical :: exists
#ifdef MPAS_SMIOL_SUPPORT
integer(kind=SMIOL_offset_kind) :: preexisting_records
#endif
! call mpas_log_write('Called MPAS_io_open()')
if (present(ierr)) ierr = MPAS_IO_NOERR
if (present(clobber_file)) then
local_clobber = clobber_file
else
local_clobber = .false.
end if
if (present(truncate_file)) then
local_truncate = truncate_file
else
local_truncate = .false.
end if
! Sanity checks
if (mode /= MPAS_IO_READ .and. &
mode /= MPAS_IO_WRITE) then
if (present(ierr)) ierr = MPAS_IO_ERR_INVALID_MODE
return
end if
if (ioformat /= MPAS_IO_NETCDF .and. &
ioformat /= MPAS_IO_NETCDF4 .and. &
ioformat /= MPAS_IO_PNETCDF .and. &
ioformat /= MPAS_IO_PNETCDF5) then
if (present(ierr)) ierr = MPAS_IO_ERR_INVALID_FORMAT
return
end if
if (len(filename) > 1024) then
if (present(ierr)) ierr = MPAS_IO_ERR_LONG_FILENAME
return
end if
MPAS_io_open % filename = filename
MPAS_io_open % iomode = mode
MPAS_io_open % ioformat = ioformat
MPAS_io_open % ioContext => ioContext
#ifdef MPAS_PIO_SUPPORT
if (ioContext % master_pio_iotype /= -999) then
pio_iotype = ioContext % master_pio_iotype
pio_mode = PIO_64BIT_OFFSET
else
if (ioformat == MPAS_IO_PNETCDF) then
pio_iotype = PIO_iotype_pnetcdf
pio_mode = PIO_64BIT_OFFSET
else if (ioformat == MPAS_IO_PNETCDF5) then
pio_iotype = PIO_iotype_pnetcdf
pio_mode = PIO_64BIT_DATA
else if (ioformat == MPAS_IO_NETCDF) then
pio_iotype = PIO_iotype_netcdf
pio_mode = PIO_64BIT_OFFSET
else if (ioformat == MPAS_IO_NETCDF4) then
pio_iotype = PIO_iotype_netcdf4p
#ifdef USE_PIO2
pio_mode = 0
#else
pio_mode = PIO_64BIT_OFFSET
#endif
end if
end if
#endif
if (present(pio_file_desc)) then
#ifdef MPAS_PIO_SUPPORT
MPAS_io_open % pio_file = pio_file_desc
#endif
MPAS_io_open % external_file_desc = .true.
else
if (mode == MPAS_IO_WRITE) then
!call mpas_log_write('MGD PIO_createfile')
if (ioContext % dminfo % my_proc_id == 0) then
inquire(file=trim(filename), exist=exists)
end if
call mpas_dmpar_bcast_logical(ioContext % dminfo, exists)
! If the file exists and we are not allowed to clobber it, return an
! appropriate error code
if (exists .and. (.not. local_clobber)) then
if (present(ierr)) ierr = MPAS_IO_ERR_WOULD_CLOBBER
return
end if
if (exists .and. (.not. local_truncate)) then
#ifdef MPAS_PIO_SUPPORT
pio_ierr = PIO_openfile(ioContext % pio_iosystem, MPAS_io_open % pio_file, pio_iotype, trim(filename), PIO_write)
#endif
#ifdef MPAS_SMIOL_SUPPORT
local_ierr = SMIOLf_open_file(ioContext % smiol_context, trim(filename), &
SMIOL_FILE_WRITE, MPAS_io_open % smiol_file)
#endif
MPAS_io_open % preexisting_file = .true.
else
#ifdef MPAS_PIO_SUPPORT
pio_ierr = PIO_createfile(ioContext % pio_iosystem, MPAS_io_open % pio_file, pio_iotype, trim(filename), pio_mode)
#endif
#ifdef MPAS_SMIOL_SUPPORT
local_ierr = SMIOLf_open_file(ioContext % smiol_context, trim(filename), &
SMIOL_FILE_CREATE, MPAS_io_open % smiol_file)
#endif
#ifdef MPAS_DEBUG
if (exists) then
call mpas_log_write('MPAS I/O: Truncating existing data in output file '//trim(filename), MPAS_LOG_WARN)
end if
#endif
end if
else
inquire(file=trim(filename), exist=exists)
if (.not. exists) then
if (present(ierr)) ierr = MPAS_IO_ERR_NOEXIST_READ
return
end if
#ifdef MPAS_PIO_SUPPORT
!call mpas_log_write('MGD PIO_openfile')
pio_ierr = PIO_openfile(ioContext % pio_iosystem, MPAS_io_open % pio_file, pio_iotype, trim(filename), PIO_nowrite)
#endif
#ifdef MPAS_SMIOL_SUPPORT
local_ierr = SMIOLf_open_file(ioContext % smiol_context, trim(filename), &
SMIOL_FILE_READ, MPAS_io_open % smiol_file)
#endif
endif
#ifdef MPAS_PIO_SUPPORT
if (pio_ierr /= PIO_noerr) then
io_global_err = pio_ierr
if (present(ierr)) ierr = MPAS_IO_ERR_BACKEND
return
end if
#endif
#ifdef MPAS_SMIOL_SUPPORT
if (local_ierr /= SMIOL_SUCCESS) then
call mpas_log_write('SMIOLf_open_file failed with error $i', intArgs=[local_ierr], messageType=MPAS_LOG_ERR)
if (local_ierr == SMIOL_LIBRARY_ERROR) then
call mpas_log_write(trim(SMIOLf_lib_error_string(ioContext % smiol_context)), messageType=MPAS_LOG_ERR)
else
call mpas_log_write(trim(SMIOLf_error_string(local_ierr)), messageType=MPAS_LOG_ERR)
end if
io_global_err = local_ierr
if (present(ierr)) ierr = MPAS_IO_ERR_BACKEND
return
end if
#endif
MPAS_io_open % external_file_desc = .false.
end if
if (mode == MPAS_IO_READ .or. MPAS_io_open % preexisting_file) then
#ifdef MPAS_PIO_SUPPORT
!MPAS_io_open % pio_unlimited_dimid = 44
pio_ierr = PIO_inquire(MPAS_io_open % pio_file, unlimitedDimID=MPAS_io_open % pio_unlimited_dimid)
!call mpas_log_write('Found unlimited dim $i', intArgs=(/MPAS_io_open % pio_unlimited_dimid/) )
if (pio_ierr /= PIO_noerr) then
io_global_err = pio_ierr
if (present(ierr)) ierr = MPAS_IO_ERR_BACKEND
return
end if
#endif
#ifdef MPAS_PIO_SUPPORT
! Here we're depending on the undocumented behavior of PIO to return a
! negative dimension ID when an unlimited dimension is not found. This
! might change in the future, causing this code to break, though it
! shouldn't break for files with unlimited dimensions.
if ( MPAS_io_open % pio_unlimited_dimid >= 0 ) then
pio_ierr = PIO_inq_dimlen(MPAS_io_open % pio_file, MPAS_io_open % pio_unlimited_dimid, MPAS_io_open % preexisting_records)
if (pio_ierr /= PIO_noerr) then
io_global_err = pio_ierr
if (present(ierr)) ierr = MPAS_IO_ERR_BACKEND
return
end if
else
MPAS_io_open % preexisting_records = -1
end if
#endif
#ifdef MPAS_SMIOL_SUPPORT
!MGD TODO: need a way to determine which dimension is the unlimited dimension
local_ierr = SMIOLf_inquire_dim(MPAS_io_open % smiol_file, 'Time', dimsize=preexisting_records)
MPAS_io_open % preexisting_records = preexisting_records
#endif
end if
MPAS_io_open % initialized = .true.
return
end function MPAS_io_open
subroutine MPAS_io_inq_unlimited_dim(handle, dimname, ierr)
implicit none
type (MPAS_IO_Handle_type), intent(inout) :: handle
character (len=*), intent(out) :: dimname
integer, intent(out), optional :: ierr
integer :: pio_ierr
! call mpas_log_write('Called MPAS_io_inq_unlimited_dim()')
if (present(ierr)) ierr = MPAS_IO_NOERR
! Sanity checks
if (.not. handle % initialized) then
if (present(ierr)) ierr = MPAS_IO_ERR_UNINIT_HANDLE
return
end if
if (handle % iomode /= MPAS_IO_READ) then ! We could eventually handle this for write mode, too...
if (present(ierr)) ierr = MPAS_IO_ERR_WRONG_MODE
return
end if
#ifdef MPAS_PIO_SUPPORT
pio_ierr = PIO_inq_dimname(handle % pio_file, handle % pio_unlimited_dimid, dimname)
if (pio_ierr /= PIO_noerr) then
if (present(ierr)) ierr = MPAS_IO_ERR_NO_UNLIMITED_DIM
dimname = ' '
return
end if
#endif
#ifdef MPAS_SMIOL_SUPPORT
!MGD TODO: Do this for SMIOL once we have a way to inquire about unlimited dim by name
dimname = 'Time'
#endif
end subroutine MPAS_io_inq_unlimited_dim
subroutine MPAS_io_inq_dim(handle, dimname, dimsize, ierr)
implicit none
type (MPAS_IO_Handle_type), intent(inout) :: handle
character (len=*), intent(in) :: dimname
integer, intent(out) :: dimsize
integer, intent(out), optional :: ierr
type (dimlist_type), pointer :: new_dimlist_node
type (dimlist_type), pointer :: dim_cursor
integer :: pio_ierr
integer :: local_ierr
#ifdef MPAS_SMIOL_SUPPORT
integer(kind=SMIOL_offset_kind) :: local_dimsize
#endif
! call mpas_log_write('Called MPAS_io_inq_dim()')
if (present(ierr)) ierr = MPAS_IO_NOERR
! Sanity checks
if (.not. handle % initialized) then
if (present(ierr)) ierr = MPAS_IO_ERR_UNINIT_HANDLE
return
end if
!
! First see if we already have this dimension in our list
!
dim_cursor => handle % dimlist_head
do while (associated(dim_cursor))
if (trim(dimname) == trim(dim_cursor % dimhandle % dimname)) then
dimsize = dim_cursor % dimhandle % dimsize
return
end if
dim_cursor => dim_cursor % next
end do
!
! Otherwise, query the file-level API for information about the dim
!
allocate(new_dimlist_node)
nullify(new_dimlist_node % next)
allocate(new_dimlist_node % dimhandle)
new_dimlist_node % dimhandle % dimname = dimname
#ifdef MPAS_PIO_SUPPORT
pio_ierr = PIO_inq_dimid(handle % pio_file, trim(dimname), new_dimlist_node % dimhandle % dimid)
if (pio_ierr /= PIO_noerr) then
if (present(ierr)) ierr = MPAS_IO_ERR_MISSING_DIM
deallocate(new_dimlist_node % dimhandle)
deallocate(new_dimlist_node)
dimsize = -1
return
end if
if (new_dimlist_node % dimhandle % dimid == handle % pio_unlimited_dimid) new_dimlist_node % dimhandle % is_unlimited_dim = .true.
pio_ierr = PIO_inq_dimlen(handle % pio_file, new_dimlist_node % dimhandle % dimid, new_dimlist_node % dimhandle % dimsize)
if (pio_ierr /= PIO_noerr) then
io_global_err = pio_ierr
if (present(ierr)) ierr = MPAS_IO_ERR_BACKEND
deallocate(new_dimlist_node % dimhandle)
deallocate(new_dimlist_node)
dimsize = -1
return
end if
#endif
#ifdef MPAS_SMIOL_SUPPORT
local_ierr = SMIOLf_inquire_dim(handle % smiol_file, trim(dimname), &
dimsize=local_dimsize, &
is_unlimited=new_dimlist_node % dimhandle % is_unlimited_dim)
if (local_ierr /= SMIOL_SUCCESS) then
if (present(ierr)) ierr = MPAS_IO_ERR_MISSING_DIM
deallocate(new_dimlist_node % dimhandle)
deallocate(new_dimlist_node)
dimsize = -1
return
end if
new_dimlist_node % dimhandle % dimsize = local_dimsize
#endif
! Keep dimension information for future reference
if (.not. associated(handle % dimlist_head)) then
handle % dimlist_head => new_dimlist_node
end if
if (.not. associated(handle % dimlist_tail)) then
handle % dimlist_tail => new_dimlist_node
else
handle % dimlist_tail % next => new_dimlist_node
handle % dimlist_tail => handle % dimlist_tail % next
end if
dimsize = new_dimlist_node % dimhandle % dimsize
end subroutine MPAS_io_inq_dim
subroutine MPAS_io_def_dim(handle, dimname, dimsize, ierr)
implicit none
type (MPAS_IO_Handle_type), intent(inout) :: handle
character (len=*), intent(in) :: dimname
integer, intent(in) :: dimsize
integer, intent(out), optional :: ierr
integer :: pio_ierr
integer :: local_ierr
integer :: inq_dimsize
#ifdef MPAS_SMIOL_SUPPORT
integer(kind=SMIOL_offset_kind) :: local_dimsize
#endif
type (dimlist_type), pointer :: new_dimlist_node
type (dimlist_type), pointer :: dim_cursor
! call mpas_log_write('Called MPAS_io_def_dim()')
if (present(ierr)) ierr = MPAS_IO_NOERR
! Sanity checks
if (.not. handle % initialized) then
if (present(ierr)) ierr = MPAS_IO_ERR_UNINIT_HANDLE
return
end if
if (handle % data_mode) then
if (present(ierr)) ierr = MPAS_IO_ERR_DATA_MODE
return
end if
if (handle % iomode /= MPAS_IO_WRITE) then
if (present(ierr)) ierr = MPAS_IO_ERR_NOWRITE
return
end if
!
! Check that this dimension hasn't already been defined
!
dim_cursor => handle % dimlist_head
do while (associated(dim_cursor))
if (trim(dimname) == trim(dim_cursor % dimhandle % dimname)) then
! The second half of the test below avoids raising errors in the case where
! we are writing to an already existing file, in which case the dimlen for the
! unlimited dimension in the file will generally not be MPAS_IO_UNLIMITED_DIM
if ((dimsize /= dim_cursor % dimhandle % dimsize) .and. &
.not. (dimsize == MPAS_IO_UNLIMITED_DIM .and. dim_cursor % dimhandle % is_unlimited_dim)) then
if (present(ierr)) ierr = MPAS_IO_ERR_REDEF_DIM
end if
return
end if
! Also, check that the user is not trying to define more than one record dimension
if (dimsize == MPAS_IO_UNLIMITED_DIM .and. dim_cursor % dimhandle % is_unlimited_dim) then
if (present(ierr)) ierr = MPAS_IO_ERR_TWO_UNLIMITED_DIMS
return
end if
dim_cursor => dim_cursor % next
end do
!
! If we're working with a preexisting file, see whether this dimension isn't already
! defined in the file; if it is, the dimension should match the dimsize specified in
! this call to define the dimension, at which point we can add it to our local cache
!
if (handle % preexisting_file) then
call MPAS_io_inq_dim(handle, dimname, inq_dimsize, ierr=pio_ierr)
if (pio_ierr /= MPAS_IO_ERR_BACKEND) then
! Verify that the dimsize matches...
if (dimsize /= inq_dimsize .and. dimsize /= MPAS_IO_UNLIMITED_DIM) then
if (present(ierr)) ierr = MPAS_IO_ERR_REDEF_DIM
return
end if
end if
return
end if
!
! Otherwise, define it
!
allocate(new_dimlist_node)
nullify(new_dimlist_node % next)
allocate(new_dimlist_node % dimhandle)
new_dimlist_node % dimhandle % dimname = dimname
new_dimlist_node % dimhandle % dimsize = dimsize
if (dimsize == MPAS_IO_UNLIMITED_DIM) then
new_dimlist_node % dimhandle % is_unlimited_dim = .true.
#ifdef MPAS_PIO_SUPPORT
pio_ierr = PIO_def_dim(handle % pio_file, trim(dimname), PIO_unlimited, new_dimlist_node % dimhandle % dimid)
#endif
#ifdef MPAS_SMIOL_SUPPORT
local_dimsize = -1_SMIOL_offset_kind
#endif
else
#ifdef MPAS_PIO_SUPPORT
pio_ierr = PIO_def_dim(handle % pio_file, trim(dimname), dimsize, new_dimlist_node % dimhandle % dimid)
#endif
#ifdef MPAS_SMIOL_SUPPORT
local_dimsize = int(dimsize,kind=SMIOL_offset_kind)
#endif
end if
#ifdef MPAS_SMIOL_SUPPORT
local_ierr = SMIOLf_define_dim(handle % smiol_file, trim(dimname), local_dimsize)
if (local_ierr /= SMIOL_SUCCESS) then
call mpas_log_write('SMIOLf_define_dim failed with error $i', intArgs=[local_ierr], messageType=MPAS_LOG_ERR)
if (local_ierr == SMIOL_LIBRARY_ERROR) then
call mpas_log_write(trim(SMIOLf_lib_error_string(handle % ioContext % smiol_context)), messageType=MPAS_LOG_ERR)
else
call mpas_log_write(trim(SMIOLf_error_string(local_ierr)), messageType=MPAS_LOG_ERR)
end if
io_global_err = local_ierr
if (present(ierr)) ierr = MPAS_IO_ERR_BACKEND
return
end if
#endif
#ifdef MPAS_PIO_SUPPORT
if (pio_ierr /= PIO_noerr) then
io_global_err = pio_ierr
if (present(ierr)) ierr = MPAS_IO_ERR_BACKEND
deallocate(new_dimlist_node % dimhandle)
deallocate(new_dimlist_node)
return
end if
#endif
! Keep dimension information
if (.not. associated(handle % dimlist_head)) then
handle % dimlist_head => new_dimlist_node
!call mpas_log_write('Assigning head for '//trim(dimname))
end if
if (.not. associated(handle % dimlist_tail)) then
handle % dimlist_tail => new_dimlist_node
!call mpas_log_write('Assigning tail for '//trim(dimname))
else
handle % dimlist_tail % next => new_dimlist_node
handle % dimlist_tail => handle % dimlist_tail % next
!call mpas_log_write('Extending tail for '//trim(dimname))
end if
end subroutine MPAS_io_def_dim
subroutine MPAS_io_inq_var(handle, fieldname, fieldtype, ndims, dimnames, dimsizes, ierr)
implicit none
type (MPAS_IO_Handle_type), intent(inout) :: handle
character (len=*), intent(in) :: fieldname
integer, intent(out), optional :: fieldtype
integer, intent(out), optional :: ndims
character (len=StrKIND), dimension(:), pointer, optional :: dimnames
integer, dimension(:), pointer, optional :: dimsizes
integer, intent(out), optional :: ierr
integer :: i
type (fieldlist_type), pointer :: new_fieldlist_node
type (fieldlist_type), pointer :: field_cursor
type (dimlist_type), pointer :: new_dimlist_node
type (dimlist_type), pointer :: dim_cursor
integer, dimension(:), pointer :: dimids
logical :: found
integer :: pio_ierr
integer :: local_ierr
integer :: smiol_type
integer :: smiol_ndims
character(len=StrKind), dimension(:), allocatable :: smiol_dimnames
#ifdef MPAS_SMIOL_SUPPORT
integer(kind=SMIOL_offset_kind) :: smiol_dimlen
#endif
! call mpas_log_write('Called MPAS_io_inq_var()')
if (present(ierr)) ierr = MPAS_IO_NOERR
! Sanity checks
if (.not. handle % initialized) then
if (present(ierr)) ierr = MPAS_IO_ERR_UNINIT_HANDLE
return
end if
!
! See if we already have this variable in our list
!
found = .false.
field_cursor => handle % fieldlist_head
do while (associated(field_cursor))
if (trim(fieldname) == trim(field_cursor % fieldhandle % fieldname)) then
!call mpas_log_write('Already found variable in fieldlist')
found = .true.
exit
end if
field_cursor => field_cursor % next
end do
!
! Otherwise, inquire through the file-level API and add it to the list
!
if (.not. found) then
allocate(new_fieldlist_node)
nullify(new_fieldlist_node % next)
allocate(new_fieldlist_node % fieldhandle)
new_fieldlist_node % fieldhandle % fieldname = fieldname
! Get variable ID
#ifdef MPAS_PIO_SUPPORT
pio_ierr = PIO_inq_varid(handle % pio_file, trim(fieldname), new_fieldlist_node % fieldhandle % fieldid)
pio_ierr = PIO_inq_varid(handle % pio_file, trim(fieldname), new_fieldlist_node % fieldhandle % field_desc)
if (pio_ierr /= PIO_noerr) then
io_global_err = pio_ierr
if (present(ierr)) ierr = MPAS_IO_ERR_BACKEND
deallocate(new_fieldlist_node % fieldhandle)
deallocate(new_fieldlist_node)
call mpas_log_write('Variable ' // trim(fieldname) // ' not in input file.', MPAS_LOG_WARN)
return
end if
!call mpas_log_write('Inquired about variable ID $i.', intArgs=(/new_fieldlist_node % fieldhandle % fieldid/) )
! Get field type
pio_ierr = PIO_inq_vartype(handle % pio_file, new_fieldlist_node % fieldhandle % fieldid, new_fieldlist_node % fieldhandle % field_type)
if (pio_ierr /= PIO_noerr) then
io_global_err = pio_ierr
if (present(ierr)) ierr = MPAS_IO_ERR_BACKEND
deallocate(new_fieldlist_node % fieldhandle)
deallocate(new_fieldlist_node)
return
end if
!call mpas_log_write('Inquired about variable type $i.', intArgs=(/new_fieldlist_node % fieldhandle % field_type/) )
! Convert to MPAS type
new_fieldlist_node % fieldhandle % precision = MPAS_IO_NATIVE_PRECISION
if (new_fieldlist_node % fieldhandle % field_type == PIO_double) then
new_fieldlist_node % fieldhandle % field_type = MPAS_IO_DOUBLE
new_fieldlist_node % fieldhandle % precision = MPAS_IO_DOUBLE_PRECISION
else if (new_fieldlist_node % fieldhandle % field_type == PIO_real) then
new_fieldlist_node % fieldhandle % field_type = MPAS_IO_REAL
new_fieldlist_node % fieldhandle % precision = MPAS_IO_SINGLE_PRECISION
else if (new_fieldlist_node % fieldhandle % field_type == PIO_int) then
new_fieldlist_node % fieldhandle % field_type = MPAS_IO_INT
else if (new_fieldlist_node % fieldhandle % field_type == PIO_char) then
new_fieldlist_node % fieldhandle % field_type = MPAS_IO_CHAR
!!!!!!!! PIO DOES NOT SUPPORT LOGICAL !!!!!!!!
end if
#endif
#ifdef MPAS_SMIOL_SUPPORT
local_ierr = SMIOLf_inquire_var(handle % smiol_file, trim(fieldname), vartype=smiol_type)
if (local_ierr /= SMIOL_SUCCESS) then
io_global_err = local_ierr
if (present(ierr)) ierr = MPAS_IO_ERR_BACKEND
deallocate(new_fieldlist_node % fieldhandle)
deallocate(new_fieldlist_node)
call mpas_log_write('Variable ' // trim(fieldname) // ' not in input file.', MPAS_LOG_WARN)
return
end if
new_fieldlist_node % fieldhandle % field_type = smiol_type
! Convert to MPAS type
new_fieldlist_node % fieldhandle % precision = MPAS_IO_NATIVE_PRECISION
if (new_fieldlist_node % fieldhandle % field_type == SMIOL_REAL64) then
new_fieldlist_node % fieldhandle % field_type = MPAS_IO_DOUBLE
new_fieldlist_node % fieldhandle % precision = MPAS_IO_DOUBLE_PRECISION
else if (new_fieldlist_node % fieldhandle % field_type == SMIOL_REAL32) then
new_fieldlist_node % fieldhandle % field_type = MPAS_IO_REAL
new_fieldlist_node % fieldhandle % precision = MPAS_IO_SINGLE_PRECISION
else if (new_fieldlist_node % fieldhandle % field_type == SMIOL_INT32) then
new_fieldlist_node % fieldhandle % field_type = MPAS_IO_INT
else if (new_fieldlist_node % fieldhandle % field_type == SMIOL_CHAR) then
new_fieldlist_node % fieldhandle % field_type = MPAS_IO_CHAR
end if
#endif
! Get number of dimensions
#ifdef MPAS_PIO_SUPPORT
pio_ierr = PIO_inq_varndims(handle % pio_file, new_fieldlist_node % fieldhandle % fieldid, new_fieldlist_node % fieldhandle % ndims)
if (pio_ierr /= PIO_noerr) then
io_global_err = pio_ierr
if (present(ierr)) ierr = MPAS_IO_ERR_BACKEND
deallocate(new_fieldlist_node % fieldhandle)
deallocate(new_fieldlist_node)
return
end if
!call mpas_log_write('Inquired about number of dimensions $i', intArgs=(/new_fieldlist_node % fieldhandle % ndims/) )
#endif
#ifdef MPAS_SMIOL_SUPPORT
local_ierr = SMIOLf_inquire_var(handle % smiol_file, trim(fieldname), ndims=smiol_ndims)
if (local_ierr /= SMIOL_SUCCESS) then
call mpas_log_write('SMIOLf_inquire_var failed with error $i', intArgs=[local_ierr], messageType=MPAS_LOG_ERR)
if (local_ierr == SMIOL_LIBRARY_ERROR) then
call mpas_log_write(trim(SMIOLf_lib_error_string(handle % ioContext % smiol_context)), messageType=MPAS_LOG_ERR)
else
call mpas_log_write(trim(SMIOLf_error_string(local_ierr)), messageType=MPAS_LOG_ERR)
end if
io_global_err = local_ierr
if (present(ierr)) ierr = MPAS_IO_ERR_BACKEND
deallocate(new_fieldlist_node % fieldhandle)
deallocate(new_fieldlist_node)
call mpas_log_write('Variable ' // trim(fieldname) // ' not in input file.', MPAS_LOG_WARN)
return
end if
new_fieldlist_node% fieldhandle % ndims = smiol_ndims
#endif
allocate(dimids(new_fieldlist_node % fieldhandle % ndims))
! Get dimension IDs
#ifdef MPAS_PIO_SUPPORT
if (new_fieldlist_node % fieldhandle % ndims > 0) then
pio_ierr = PIO_inq_vardimid(handle % pio_file, new_fieldlist_node % fieldhandle % fieldid, dimids)
if (pio_ierr /= PIO_noerr) then
io_global_err = pio_ierr
if (present(ierr)) ierr = MPAS_IO_ERR_BACKEND
deallocate(new_fieldlist_node % fieldhandle)
deallocate(new_fieldlist_node)
deallocate(dimids)
return
end if
!call mpas_log_write('Inquired about dimension IDs $i.', intArgs=(/dimids/) )
end if
#endif
allocate(new_fieldlist_node % fieldhandle % dims(new_fieldlist_node % fieldhandle % ndims))
#ifdef MPAS_PIO_SUPPORT
! Get information about dimensions
do i=1,new_fieldlist_node % fieldhandle % ndims
new_fieldlist_node % fieldhandle % dims(i) % dimid = dimids(i)
if (dimids(i) == handle % pio_unlimited_dimid) then
new_fieldlist_node % fieldhandle % dims(i) % is_unlimited_dim = .true.
new_fieldlist_node % fieldhandle % has_unlimited_dim = .true.
end if
pio_ierr = PIO_inq_dimlen(handle % pio_file, dimids(i), new_fieldlist_node % fieldhandle % dims(i) % dimsize)
if (pio_ierr /= PIO_noerr) then
io_global_err = pio_ierr
if (present(ierr)) ierr = MPAS_IO_ERR_BACKEND
deallocate(new_fieldlist_node % fieldhandle)
deallocate(new_fieldlist_node)
deallocate(dimids)
return