-
Notifications
You must be signed in to change notification settings - Fork 6
/
mo_ncread.f90
2243 lines (2136 loc) · 79.9 KB
/
mo_ncread.f90
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
module mo_NcRead
! This module provides subroutines for reading arrays from nc file using the netcdf4 library
! License
! -------
! This file is part of the JAMS Fortran package, distributed under the MIT License.
!
! Copyright (c) 2011-2014 Stephan Thober, Matthias Cuntz - mc (at) macu (dot) de
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
! in the Software without restriction, including without limitation the rights
! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
! copies of the Software, and to permit persons to whom the Software is
! furnished to do so, subject to the following conditions:
!
! The above copyright notice and this permission notice shall be included in all
! copies or substantial portions of the Software.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
! SOFTWARE.
use mo_kind, only: i1, i4, i8, sp, dp
! functions and constants of netcdf4 library
use netcdf, only: nf90_open, nf90_get_var, nf90_close, NF90_MAX_NAME , &
nf90_get_att, nf90_inq_varid, nf90_inquire_variable, &
nf90_inquire_dimension, NF90_NOWRITE, &
nf90_noerr, nf90_strerror, nf90_inquire_attribute
implicit none
public :: Get_NcDim ! get the dimensions of a Variable
#ifndef __ABSOFT__
public :: Get_NcDimAtt ! get the attributes of the dimensions
public :: Get_NcVarAtt ! get attributes of a variable
#endif
public :: Get_NcVar ! get the data of a Variable in a nc file
public :: NcOpen ! Open a file and get a handle back
public :: NcClose ! Close a file
! ------------------------------------------------------------------------------
! NAME
! Get_NcVar
! PURPOSE
! Reads a 2 - 5 dimensional array from a nc file given
! the variable name EXACTLY as specified in the file. If the
! array is not allocated when calling, Get_NcVar will
! allocate it internally. If the dimension of the actual data
! is less than the ones of the array, then the dimension
! lengths of the array will be filled with ones.
! CALLING SEQUENCE
! call Get_NcVar(Filename, VarName, Dat, start=jdate, a_count=Nvalues, fid=fid)
! INTENT(IN)
! character(len=*) :: Filename - Name of the nc file
! INTENT(IN)
! character(len=*) :: VarName - Name of the Variable in the nc file
! INTENT(INOUT)
! real(sp/dp), dimension(:,:[,:[,:[,:]]]), allocatable :: array - array where data will be read
! INTENT(IN), OPTIONAL
! integer(i4), dimension(:) :: jdate ! starting indeces of first value to read
! ! len is the number of dimensions of
! ! array, default is 1, see example
! integer, dimension(:) :: a_count ! same size as jdate, specifies how
! ! many values in each dimension
! ! is going to be read
! integer :: fid ! file handle of opened netcdf file
!
! RESTRICTIONS
! Output array is a floating point of 2-5 dimensions.
! NOT yet tested for different compilers than intel11.1.075
! CANNOT read packed data
! i1 indicates, that 1 byte integer is read [type is integer(1)]
! EXAMPLE
! see test program in directory test_mo_NcRead
! LITERATURE
! http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f90.html
! HISTORY
! Written, Stephan Thober, Nov 2011
! Modified, Stephan Thober, Nov 2011 - added comments
! Modified, Matthias Cuntz, Jan 2012 - unified routines for different dimensions and data types
! Modified, Stephan Thober, Mar 2012 - corrected dynamical read of data
! Modified, Stephan Thober, May 2012 - fid
! Modified, Stephan Thober, Nov 2012 - write out Varname, when vartype is incorrect
! Modified, Stephan Thober, Feb 2013 - added 1 byte integer version
! Modified, Stephan Thober, Mar 2014 - added subroutines for allocatable arrays
! Modified, Matthias Cuntz, Mar 2020 - integer(1) -> integer(i1)
interface Get_NcVar
module procedure Get_NcVar_0d_sp, Get_NcVar_0d_dp, Get_NcVar_1d_sp, &
Get_NcVar_1d_dp, Get_NcVar_2d_sp, Get_NcVar_2d_dp, &
Get_NcVar_3d_sp, Get_NcVar_3d_dp, Get_NcVar_4d_sp, &
Get_NcVar_4d_dp, Get_NcVar_5d_sp, Get_NcVar_5d_dp, &
Get_NcVar_0d_i4, Get_NcVar_1d_i4, Get_NcVar_2d_i4, &
Get_NcVar_3d_i4, Get_NcVar_4d_i4, Get_NcVar_5d_i4, &
Get_NcVar_0d_i1, Get_NcVar_1d_i1, Get_NcVar_2d_i1, &
Get_NcVar_3d_i1, Get_NcVar_4d_i1, Get_NcVar_5d_i1
end interface Get_NcVar
! ------------------------------------------------------------------------------
private
! ------------------------------------------------------------------------------
contains
! ------------------------------------------------------------------------------
!
! NAME
! Get_NcDim
!
! PURPOSE
! gets the dimensions of variable in a netcdf file
!
! CALLING SEQUENCE
! dim = Get_NcDim(Filename, Variable, PrintInfo=PrintInfo, ndims=ndims)
!
! INTENT(IN)
! character(len=*) :: Filename - Filename of netcdf file
!
! INTENT(IN)
! character(len=*) :: Variable - Variable name exactly as specified in the file
!
! INTENT(IN), OPTIONAL
! logical :: PrintInfo - if given and true, information about dimension
! and their lengths will be printed to standard output
!
! INTENT(OUT)
! integer, dimension(5) :: Get_NcDim - dimension length, -1 if dimension does not exist
!
! INTENT(OUT), OPTIONAL
! integer :: ndims - # of dimensions
!
! LITERATURE
! http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f90.html
!
! HISTORY
! Written, Stephan Thober, Dec 2011
! Modified, Matthias Cuntz, Jan 2012 - ndims
function Get_NcDim(Filename, Variable, PrintInfo, ndims)
!
implicit none
!
character(len=*), intent(in) :: Filename
character(len=*), intent(in) :: Variable
logical, optional, intent(in) :: PrintInfo
integer, optional, intent(out) :: ndims
integer, dimension(5) :: Get_NcDim
!
logical :: PrintFlag
integer :: ncid ! id of input stream
integer :: varid ! id of variable to be read
integer :: vartype ! type of variable
integer :: NumDims ! # of dimensions
!
! Open NetCDF filename
call check(nf90_open(Filename, NF90_NOWRITE, ncid))
!
PrintFlag = .false.
if (present(PrintInfo)) PrintFlag = PrintInfo
!
! Inquire file and check if VarName exists in the dataset,
! get number of dimensions and
! get the length of the dimensions
call Get_Info(Variable, ncid, varid, vartype, Get_NcDim, Info=PrintFlag, ndims=NumDims)
if (present(ndims)) ndims=NumDims
!
! close File
call check(nf90_close(ncid))
!
end function Get_NcDim
#ifndef __ABSOFT__
! ------------------------------------------------------------------
! NAME
! Get_NcDimAtt
! PURPOSE
! gets the name and size of the dimensions of a variable in a netcdf file
! CALLING SEQUENCE
! Get_NcDimAtt(Filename, Variable, DimName, DimLen)
! INTENT(IN)
! character(len=*), intent(in) :: Filename - Filename of netcdf file
! character(len=*), intent(in) :: Variable - Variable name exactly as specified in the file
! INTENT(INOUT)
! None
! INTENT(OUT)
! character(len=*), dimension(:), allocatable, intent(out) :: DimName - allocatable array with the
! names of the dimensions
! INTENT(IN), OPTIONAL
! None
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! integer, dimension(:), allocatable, optional, intent(out) :: DimLen - allocatable array with the size
! of the dimensions
! RESTRICTIONS
! DimName and DimLen are both allocated within the subroutine, so please just provide an 1 dimensional
! allocatable array to the subroutine
! EXAMPLE
! Filename = 'test.nc'
! Varname = 'data'
! call Get_NcDimAtt(Filename, Varname, DimNames, DimLen)
! -> see example in test directory
! LITERATURE
! http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f90.html
! HISTORY
! Written, Matthias Zink, Oct 2012
subroutine Get_NcDimAtt(Filename, Variable, DimName, DimLen)
!
implicit none
!
character(len=*), intent(in) :: Filename
character(len=*), intent(in) :: Variable
character(len=*), dimension(:), allocatable, &
intent(out) :: DimName
integer , dimension(:), allocatable, &
optional, intent(out) :: DimLen
!
integer, dimension(5) :: Get_NcDim
!
integer :: ncid ! id of input stream
integer :: varid ! id of variable to be read
integer :: vartype ! type of variable
integer :: NumDims ! # of dimensions
integer :: dimid
integer :: len
!
! Open NetCDF filename
call check(nf90_open(Filename, NF90_NOWRITE, ncid))
!
! Inquire file and check if VarName exists in the dataset,
! get number of dimensions and
! get the length of the dimensions
call Get_Info(Variable, ncid, varid, vartype, Get_NcDim, Info=.FALSE., ndims=NumDims)
!
allocate(DimName(NumDims))
if (present(DimLen)) allocate(DimLen(NumDims))
!
do dimid = 1, NumDims
call check(nf90_inquire_dimension(ncid, dimid, DimName(dimid), len))
if (present(DimLen)) DimLen(dimid) = len
end do
! close File
call check(nf90_close(ncid))
!
end subroutine Get_NcDimAtt
! ------------------------------------------------------------------
! NAME
! Get_NcVarAtt
! PURPOSE
! gets the values of an particular attribute of an variable
! CALLING SEQUENCE
! Get_NcVarAtt(FileName, VarName, AttName, AttValues, fid)
! INTENT(IN)
! character(len=*), intent(in) :: FileName - Filename of netcdf file
! character(len=*), intent(in) :: VarName - Variable name exactly as specified in the file
! character(len=*), intent(in) :: AttName - Attribute name exactly as specified for the Variable
! INTENT(INOUT)
! None
! INTENT(OUT)
! character(len=*), intent(out) :: AttValues - values of the Attribute
! INTENT(IN), OPTIONAL
! integer, optional, intent(in) :: fid ! file handle of opened netcdf file
! integer, optional, intent(in) :: dtype ! datatype (ineteger,float) see NetCDF convention (unidata.ucar)
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RESTRICTIONS
! AttValues are restricted to be of character type
! The name of the variable (VarName) has to be known beforehand
! The name of the attribute (AttName) has to be known beforehand
! EXAMPLE
! Filename = 'test.nc'
! VarName = 'data'
! AttName = 'units'
! call Get_NcDimAtt(Filename, Varname, AttName, AttValues)
! -> see example in test directory
! LITERATURE
! http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f90.html
! HISTORY
! Written, Matthias Zink, Oct 2012
! Modified, Matthias Cuntz & Juliane Mai, Nov 2014 - correct data type detection
subroutine Get_NcVarAtt(FileName, VarName, AttName, AttValues, fid, dtype)
!
implicit none
!
character(len=*), intent(in) :: FileName
character(len=*), intent(in) :: VarName
character(len=*), intent(in) :: AttName
character(len=*), intent(out) :: AttValues
integer, optional, intent(in) :: fid
integer, optional, intent(out) :: dtype
!
integer :: ncid
integer :: varid
!
integer :: type
integer(i8) :: avint
real(dp) :: avfloat
character(256) :: avchar
!
if (present(fid)) then
ncid = fid
else
call check(nf90_open(trim(Filename),NF90_NOWRITE, ncid))
end if
!
! Inquire file, check if VarName exists and get the id
call check(nf90_inq_varid(ncid, trim(VarName), varid))
! get type of the attribute
call check(nf90_inquire_attribute(ncid, varid, trim(AttName), type))
!
! read attribute by type
select case (type)
case (1) ! 1 = NF90_BYTE
call check(nf90_get_att(ncid, varid, trim(AttName), avint))
write(AttValues,'(i4)') avint
AttValues = adjustl(trim(AttValues))
if (present(dtype)) dtype=type
case (2) ! NF90_CHAR
call check(nf90_get_att(ncid, varid, trim(AttName), avchar))
AttValues = adjustl(trim(avchar))
if (present(dtype)) dtype=type
case (3) ! NF90_SHORT
call check(nf90_get_att(ncid, varid, trim(AttName), avint))
write(AttValues,'(i6)') avint
AttValues = adjustl(trim(AttValues))
if (present(dtype)) dtype=type
case (4) ! NF90_INT
call check(nf90_get_att(ncid, varid, trim(AttName), avint))
write(AttValues,'(i11)') avint
AttValues = adjustl(trim(AttValues))
if (present(dtype)) dtype=type
case (5) ! NF90_FLOAT
call check(nf90_get_att(ncid, varid, trim(AttName), avfloat))
write(AttValues,'(E15.7)') avfloat
AttValues = adjustl(trim(AttValues))
if (present(dtype)) dtype=type
case (6) ! NF90_DOUBLE
call check(nf90_get_att(ncid, varid, trim(AttName), avfloat))
write(AttValues,'(E24.15)') avfloat
AttValues = adjustl(trim(AttValues))
if (present(dtype)) dtype=type
case DEFAULT
print*, '***ERROR: mo_ncread: Mismatch in attribute datatype!'
end select
!
if (.not. present(fid)) call check(nf90_close(ncid))
!
end subroutine Get_NcVarAtt
#endif
! ------------------------------------------------------------------------------
subroutine Get_NcVar_0d_sp(Filename, VarName, Dat, fid)
!
implicit none
!
integer, parameter :: itype = 5 ! 5 = float, 6 = double
!
character(len=*), intent(in) :: Filename
character(len=*), intent(in) :: VarName ! Variable name
real(sp), intent(inout) :: Dat ! array where values should be stored
integer, optional, intent(in) :: fid
!
integer :: ncid ! id of input stream
integer :: varid ! id of variable to be read
integer :: vartype ! type of variable
!
! Open NetCDF filename
if (present(fid)) then
ncid = fid
else
call check(nf90_open(trim(Filename),NF90_NOWRITE, ncid))
end if
!
! Inquire file, check if VarName exists and get the id
call Get_Info(Varname,ncid,varid,vartype)
! check variable type ( 5 equals float type, 6 equals double )
if (vartype /= itype) then
print *, 'Variable name: ', trim(Varname)
print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
stop
end if
!
! get values by varid
call check(nf90_get_var(ncid, varid, Dat))
!
! close File
if (.not. present(fid)) call check(nf90_close(ncid))
!
end subroutine Get_NcVar_0d_sp
subroutine Get_NcVar_0d_dp(Filename, VarName, Dat, fid)
!
implicit none
!
integer, parameter :: itype = 6 ! 5 = float, 6 = double
!
character(len=*), intent(in) :: Filename
character(len=*), intent(in) :: VarName ! Variable name
real(dp), intent(inout) :: Dat ! array where values should be stored
integer, optional, intent(in) :: fid
!
integer :: ncid ! id of input stream
integer :: varid ! id of variable to be read
integer :: vartype ! type of variable
!
! Open NetCDF filename
if (present(fid)) then
ncid = fid
else
call check(nf90_open(trim(Filename),NF90_NOWRITE, ncid))
end if
!
! Inquire file, check if VarName exists and get the id
call Get_Info(Varname,ncid,varid,vartype)
! check variable type ( 5 equals float type, 6 equals double )
if (vartype /= itype) then
print *, 'Variable name: ', trim(Varname)
print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
stop
end if
!
! get values by varid
call check(nf90_get_var(ncid, varid, Dat))
!
! close File
if (.not. present(fid)) call check(nf90_close(ncid))
!
end subroutine Get_NcVar_0d_dp
subroutine Get_NcVar_1d_sp(Filename, VarName, Dat, start, a_count, fid)
!
implicit none
!
integer, parameter :: idims = 1
integer, parameter :: itype = 5 ! 5 = float, 6 = double
!
character(len=*), intent(in) :: Filename
character(len=*), intent(in) :: VarName ! Variable name
real(sp), dimension(:), allocatable, intent(inout) :: Dat ! array where values should be stored
integer, dimension(:), optional, intent(in) :: start
integer, dimension(:), optional, intent(in) :: a_count
integer, optional, intent(in) :: fid
!
integer, dimension(5) :: Rstart
integer, dimension(5) :: Rcount
integer :: ncid ! id of input stream
integer :: varid ! id of variable to be read
integer :: vartype ! type of variable
integer :: i
!
! Defaults for Options Start and Count
Rstart = 1
Rcount = 1
!
! allocate Dat
if ( .not. allocated( Dat ) ) then
if ( .not. present( a_count ) ) then
Rcount = Get_NcDim( Filename, Varname )
else
Rcount(1:idims) = a_count(1:idims)
end if
allocate( Dat( Rcount(1) ) )
else
Rcount(1:idims) = shape(Dat)
end if
!
! Assign options Start and Count if present
if (present(start)) then
if (size(start) < size(shape(dat))) stop 'ERROR*** start has less values than data has dimensions. GetNcVar'
if (size(start) > 5) stop 'ERROR*** start has dimension greater than 5. GetNcVar'
Rstart(1:size(start)) = start
end if
!
if (present(a_count)) then
if (size(a_count) < size(shape(dat))) stop 'ERROR*** count has less values than data has dimensions. GetNcVar'
if (size(a_count) > 5) stop 'ERROR*** count has dimension greater than 5. GetNcVar'
Rcount(1:size(a_count)) = a_count
do i=1, idims
if (size(Dat,i) < Rcount(i)) stop 'ERROR*** try to read more data in dimension than there is. Get_NcVar'
end do
end if
!
! Open NetCDF filename
if (present(fid)) then
ncid = fid
else
call check(nf90_open(trim(Filename),NF90_NOWRITE, ncid))
end if
!
! Inquire file, check if VarName exists and get the id
call Get_Info(Varname,ncid,varid,vartype)
! check variable type ( 5 equals float type, 6 equals double )
if (vartype /= itype) then
print *, 'Variable name: ', trim(Varname)
print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
stop
end if
!
! get values by varid
call check(nf90_get_var(ncid, varid, Dat, Rstart, Rcount))
!
! close File
if (.not. present(fid)) call check(nf90_close(ncid))
!
end subroutine Get_NcVar_1d_sp
subroutine Get_NcVar_1d_dp(Filename, VarName, Dat, start, a_count, fid)
!
implicit none
!
integer, parameter :: idims = 1
integer, parameter :: itype = 6 ! 5 = float, 6 = double
!
character(len=*), intent(in) :: Filename
character(len=*), intent(in) :: VarName ! Variable name
real(dp), dimension(:), allocatable, intent(inout) :: Dat ! array where values should be stored
integer, dimension(:), optional, intent(in) :: start
integer, dimension(:), optional, intent(in) :: a_count
integer, optional, intent(in) :: fid
!
integer, dimension(5) :: Rstart
integer, dimension(5) :: Rcount
integer :: ncid ! id of input stream
integer :: varid ! id of variable to be read
integer :: vartype ! type of variable
integer :: i
!
! Defaults for Options Start and Count
Rstart = 1
Rcount = 1
!
! allocate Dat
if ( .not. allocated( Dat ) ) then
if ( .not. present( a_count ) ) then
Rcount = Get_NcDim( Filename, Varname )
else
Rcount(1:idims) = a_count(1:idims)
end if
allocate( Dat( Rcount(1) ) )
else
Rcount(1:idims) = shape(Dat)
end if
!
! Assign options Start and Count if present
if (present(start)) then
if (size(start) < size(shape(dat))) stop 'ERROR*** start has less values than data has dimensions. GetNcVar'
if (size(start) > 5) stop 'ERROR*** start has dimension greater than 5. GetNcVar'
Rstart(1:size(start)) = start
end if
!
if (present(a_count)) then
if (size(a_count) < size(shape(dat))) stop 'ERROR*** count has less values than data has dimensions. GetNcVar'
if (size(a_count) > 5) stop 'ERROR*** count has dimension greater than 5. GetNcVar'
Rcount(1:size(a_count)) = a_count
do i=1, idims
if (size(Dat,i) < Rcount(i)) stop 'ERROR*** try to read more data in dimension than there is. Get_NcVar'
end do
end if
!
! Open NetCDF filename
if (present(fid)) then
ncid = fid
else
call check(nf90_open(trim(Filename),NF90_NOWRITE, ncid))
end if
!
! Inquire file, check if VarName exists and get the id
call Get_Info(Varname,ncid,varid,vartype)
! check variable type ( 5 equals float type, 6 equals double )
if (vartype /= itype) then
print *, 'Variable name: ', trim(Varname)
print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
stop
end if
!
! get values by varid
call check(nf90_get_var(ncid, varid, Dat, Rstart, Rcount))
!
! close File
if (.not. present(fid)) call check(nf90_close(ncid))
!
end subroutine Get_NcVar_1d_dp
subroutine Get_NcVar_2d_sp(Filename, VarName, Dat, start, a_count, fid)
!
implicit none
!
integer, parameter :: idims = 2
integer, parameter :: itype = 5 ! 5 = float, 6 = double
!
character(len=*), intent(in) :: Filename
character(len=*), intent(in) :: VarName ! Variable name
!real(sp), dimension(:,:), allocatable, intent(inout) :: Dat ! array where values should be stored
real(sp), dimension(:,:), allocatable, intent(inout) :: Dat ! array where values should be stored
integer, dimension(:), optional, intent(in) :: start
integer, dimension(:), optional, intent(in) :: a_count
integer, optional, intent(in) :: fid
!
integer, dimension(5) :: Rstart
integer, dimension(5) :: Rcount
integer :: ncid ! id of input stream
integer :: varid ! id of variable to be read
integer :: vartype ! type of variable
integer :: i
!
! Defaults for Options Start and Count
Rstart = 1
Rcount = 1
!
! allocate Dat
if ( .not. allocated( Dat ) ) then
if ( .not. present( a_count ) ) then
Rcount = Get_NcDim( Filename, Varname )
else
Rcount(1:idims) = a_count(1:idims)
end if
allocate( Dat( Rcount(1), Rcount(2) ) )
else
Rcount(1:idims) = shape(Dat)
end if
!
! Assign options Start and Count if present
if (present(start)) then
if (size(start) < size(shape(dat))) stop 'ERROR*** start has less values than data has dimensions. GetNcVar'
if (size(start) > 5) stop 'ERROR*** start has dimension greater than 5. GetNcVar'
Rstart(1:size(start)) = start
end if
!
if (present(a_count)) then
if (size(a_count) < size(shape(dat))) stop 'ERROR*** a_count has less values than data has dimensions. GetNcVar'
if (size(a_count) > 5) stop 'ERROR*** a_count has dimension greater than 5. GetNcVar'
Rcount(1:size(a_count)) = a_count
do i=1, idims
if (size(Dat,i) < Rcount(i)) stop 'ERROR*** try to read more data in dimension than there is. Get_NcVar'
end do
end if
!
! Open NetCDF filename
if (present(fid)) then
ncid = fid
else
call check(nf90_open(trim(Filename),NF90_NOWRITE, ncid))
end if
!
! Inquire file, check if VarName exists and get the id
call Get_Info(Varname,ncid,varid,vartype)
! check variable type ( 5 equals float type, 6 equals double )
if (vartype /= itype) then
print *, 'Variable name: ', trim(Varname)
print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
stop
end if
!
! get values by varid
call check(nf90_get_var(ncid, varid, Dat, Rstart, Rcount))
!
! close File
if (.not. present(fid)) call check(nf90_close(ncid))
!
end subroutine Get_NcVar_2d_sp
subroutine Get_NcVar_2d_dp(Filename, VarName, Dat, start, a_count, fid)
!
implicit none
!
integer, parameter :: idims = 2
integer, parameter :: itype = 6 ! 5 = float, 6 = double
!
character(len=*), intent(in) :: Filename
character(len=*), intent(in) :: VarName ! Variable name
real(dp), dimension(:,:), allocatable, intent(inout) :: Dat ! array where values should be stored
integer, dimension(:), optional, intent(in) :: start
integer, dimension(:), optional, intent(in) :: a_count
integer, optional, intent(in) :: fid
!
integer, dimension(5) :: Rstart
integer, dimension(5) :: Rcount
integer :: ncid ! id of input stream
integer :: varid ! id of variable to be read
integer :: vartype ! type of variable
integer :: i
!
! Defaults for Options Start and Count
Rstart = 1
Rcount = 1
!
! allocate Dat
if ( .not. allocated( Dat ) ) then
if ( .not. present( a_count ) ) then
Rcount = Get_NcDim( Filename, Varname )
else
Rcount(1:idims) = a_count(1:idims)
end if
allocate( Dat( Rcount(1), Rcount(2) ) )
else
Rcount(1:idims) = shape(Dat)
end if
!
! Assign options Start and Count if present
if (present(start)) then
if (size(start) < size(shape(dat))) stop 'ERROR*** start has less values than data has dimensions. GetNcVar'
if (size(start) > 5) stop 'ERROR*** start has dimension greater than 5. GetNcVar'
Rstart(1:size(start)) = start
end if
!
if (present(a_count)) then
if (size(a_count) < size(shape(dat))) stop 'ERROR*** a_count has less values than data has dimensions. GetNcVar'
if (size(a_count) > 5) stop 'ERROR*** a_count has dimension greater than 5. GetNcVar'
Rcount(1:size(a_count)) = a_count
do i=1, idims
if (size(Dat,i) < Rcount(i)) stop 'ERROR*** try to read more data in dimension than there is. Get_NcVar'
end do
end if
!
! Open NetCDF filename
if (present(fid)) then
ncid = fid
else
call check(nf90_open(trim(Filename),NF90_NOWRITE, ncid))
end if
!
! Inquire file, check if VarName exists and get the id
call Get_Info(Varname,ncid,varid,vartype)
! check variable type ( 5 equals float type, 6 equals double )
if (vartype /= itype) then
print *, 'Variable name: ', trim(Varname)
print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
stop
end if
!
! get values by varid
call check(nf90_get_var(ncid, varid, Dat, Rstart, Rcount))
!
! close File
if (.not. present(fid)) call check(nf90_close(ncid))
!
end subroutine Get_NcVar_2d_dp
subroutine Get_NcVar_3d_sp(Filename, VarName, Dat, start, a_count, fid)
!
implicit none
!
integer, parameter :: idims = 3
integer, parameter :: itype = 5 ! 5 = float, 6 = double
!
character(len=*), intent(in) :: Filename
character(len=*), intent(in) :: VarName ! Variable name
real(sp), dimension(:,:,:), allocatable, intent(inout) :: Dat ! array where values should be stored
integer, dimension(:), optional, intent(in) :: start
integer, dimension(:), optional, intent(in) :: a_count
integer, optional, intent(in) :: fid
!
integer, dimension(5) :: Rstart
integer, dimension(5) :: Rcount
integer :: ncid ! id of input stream
integer :: varid ! id of variable to be read
integer :: vartype ! type of variable
integer :: i
!
! Defaults for Options Start and Count
Rstart = 1
Rcount = 1
!
! allocate Dat
if ( .not. allocated( Dat ) ) then
if ( .not. present( a_count ) ) then
Rcount = Get_NcDim( Filename, Varname )
else
Rcount(1:idims) = a_count(1:idims)
end if
allocate( Dat( Rcount(1), Rcount(2), Rcount(3) ) )
else
Rcount(1:idims) = shape(Dat)
end if
!
! Assign options Start and Count if present
if (present(start)) then
if (size(start) < size(shape(dat))) stop 'ERROR*** start has less values than data has dimensions. GetNcVar'
if (size(start) > 5) stop 'ERROR*** start has dimension greater than 5. GetNcVar'
Rstart(1:size(start)) = start
end if
!
if (present(a_count)) then
if (size(a_count) < size(shape(dat))) stop 'ERROR*** a_count has less values than data has dimensions. GetNcVar'
if (size(a_count) > 5) stop 'ERROR*** a_count has dimension greater than 5. GetNcVar'
Rcount(1:size(a_count)) = a_count
do i=1, idims
if (size(Dat,i) < Rcount(i)) stop 'ERROR*** try to read more data in dimension than there is. Get_NcVar'
end do
end if
!
! Open NetCDF filename
if (present(fid)) then
ncid = fid
else
call check(nf90_open(trim(Filename),NF90_NOWRITE, ncid))
end if
!
! Inquire file, check if VarName exists and get the id
call Get_Info(Varname,ncid,varid,vartype)
! check variable type ( 5 equals float type, 6 equals double )
if (vartype /= itype) then
print *, 'Variable name: ', trim(Varname)
print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
stop
end if
!
! get values by varid
call check(nf90_get_var(ncid, varid, Dat, Rstart, Rcount))
!
! close File
if (.not. present(fid)) call check(nf90_close(ncid))
!
end subroutine Get_NcVar_3d_sp
subroutine Get_NcVar_3d_dp(Filename, VarName, Dat, start, a_count, fid)
!
implicit none
!
integer, parameter :: idims = 3
integer, parameter :: itype = 6 ! 5 = float, 6 = double
!
character(len=*), intent(in) :: Filename
character(len=*), intent(in) :: VarName ! Variable name
real(dp), dimension(:,:,:), allocatable, intent(inout) :: Dat ! array where values should be stored
integer, dimension(:), optional, intent(in) :: start
integer, dimension(:), optional, intent(in) :: a_count
integer, optional, intent(in) :: fid
!
integer, dimension(5) :: Rstart
integer, dimension(5) :: Rcount
integer :: ncid ! id of input stream
integer :: varid ! id of variable to be read
integer :: vartype ! type of variable
integer :: i
!
! Defaults for Options Start and Count
Rstart = 1
Rcount = 1
!
! allocate Dat
if ( .not. allocated( Dat ) ) then
if ( .not. present( a_count ) ) then
Rcount = Get_NcDim( Filename, Varname )
else
Rcount(1:idims) = a_count(1:idims)
end if
allocate( Dat( Rcount(1), Rcount(2), Rcount(3) ) )
else
Rcount(1:idims) = shape(Dat)
end if
!
! Assign options Start and Count if present
if (present(start)) then
if (size(start) < size(shape(dat))) stop 'ERROR*** start has less values than data has dimensions. GetNcVar'
if (size(start) > 5) stop 'ERROR*** start has dimension greater than 5. GetNcVar'
Rstart(1:size(start)) = start
end if
!
if (present(a_count)) then
if (size(a_count) < size(shape(dat))) stop 'ERROR*** a_count has less values than data has dimensions. GetNcVar'
if (size(a_count) > 5) stop 'ERROR*** a_count has dimension greater than 5. GetNcVar'
Rcount(1:size(a_count)) = a_count
do i=1, idims
if (size(Dat,i) < Rcount(i)) stop 'ERROR*** try to read more data in dimension than there is. Get_NcVar'
end do
end if
!
! Open NetCDF filename
if (present(fid)) then
ncid = fid
else
call check(nf90_open(trim(Filename),NF90_NOWRITE, ncid))
end if
!
! Inquire file, check if VarName exists and get the id
call Get_Info(Varname,ncid,varid,vartype)
! check variable type ( 5 equals float type, 6 equals double )
if (vartype /= itype) then
print *, 'Variable name: ', trim(Varname)
print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
stop
end if
!
! get values by varid
call check(nf90_get_var(ncid, varid, Dat, Rstart, Rcount))
!
! close File
if (.not. present(fid)) call check(nf90_close(ncid))
!
end subroutine Get_NcVar_3d_dp
subroutine Get_NcVar_4d_sp(Filename, VarName, Dat, start, a_count, fid)
!
implicit none
!
integer, parameter :: idims = 4
integer, parameter :: itype = 5 ! 5 = float, 6 = double
!
character(len=*), intent(in) :: Filename
character(len=*), intent(in) :: VarName ! Variable name
real(sp), dimension(:,:,:,:), allocatable, intent(inout) :: Dat ! array where values should be stored
integer, dimension(:), optional, intent(in) :: start
integer, dimension(:), optional, intent(in) :: a_count
integer, optional, intent(in) :: fid
!
integer, dimension(5) :: Rstart
integer, dimension(5) :: Rcount
integer :: ncid ! id of input stream
integer :: varid ! id of variable to be read
integer :: vartype ! type of variable
integer :: i
!
! Defaults for Options Start and Count
Rstart = 1
Rcount = 1
!
! allocate Dat
if ( .not. allocated( Dat ) ) then
if ( .not. present( a_count ) ) then
Rcount = Get_NcDim( Filename, Varname )
else
Rcount(1:idims) = a_count(1:idims)
end if
allocate( Dat( Rcount(1), Rcount(2), Rcount(3), Rcount(4) ) )
else
Rcount(1:idims) = shape(Dat)
end if
!
! Assign options Start and Count if present
if (present(start)) then
if (size(start) < size(shape(dat))) stop 'ERROR*** start has less values than data has dimensions. GetNcVar'
if (size(start) > 5) stop 'ERROR*** start has dimension greater than 5. GetNcVar'
Rstart(1:size(start)) = start
end if
!
if (present(a_count)) then
if (size(a_count) < size(shape(dat))) stop 'ERROR*** a_count has less values than data has dimensions. GetNcVar'
if (size(a_count) > 5) stop 'ERROR*** a_count has dimension greater than 5. GetNcVar'
Rcount(1:size(a_count)) = a_count
do i=1, idims
if (size(Dat,i) < Rcount(i)) stop 'ERROR*** try to read more data in dimension than there is. Get_NcVar'