-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_routines.f90
1351 lines (1180 loc) · 59 KB
/
io_routines.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
!>------------------------------------------------------------
!! Basic file input/output routines
!!
!! @details
!! Primary use is io_read2d/3d
!! io_write* routines are more used for debugging
!! model output is performed in the output module
!!
!! Generic interfaces are supplied for io_read and io_write
!! but most code still uses the explicit read/write 2d/3d/etc.
!! this keeps the code a little more obvious (at least 2D vs 3D)
!!
!! @author
!! Ethan Gutmann ([email protected])
!!
!!------------------------------------------------------------
module io_routines
use netcdf
implicit none
! maximum number of dimensions for a netCDF file
integer,parameter::io_maxDims=10
! maximum length of a dimension name not this could be in data_structures, but that simply adds
! one more dependency on data_structures and prevents this being compiled in parallel
integer,parameter::MAXDIMLENGTH=255
!>------------------------------------------------------------
!! Generic interface to the netcdf read routines
!!------------------------------------------------------------
interface io_read
module procedure io_read6d, io_read5d, io_read3d, io_read2d, io_read1d, io_read2di, io_read1dd, io_read_scalar_d
end interface
!>------------------------------------------------------------
!! Generic interface to the netcdf write routines
!!------------------------------------------------------------
interface io_write
module procedure io_write6d, io_write5d, io_write4d, io_write3d, io_write2d, io_write4di, io_write3di
end interface
!>------------------------------------------------------------
!! Generic interface to the netcdf read_attribute_TYPE routines
!!------------------------------------------------------------
interface io_read_attribute
module procedure io_read_attribute_r, io_read_attribute_i, io_read_attribute_c
end interface
! to be added as necessary
!, io_read_attribute_d
!>------------------------------------------------------------
!! Generic interface to the netcdf add_attribute_TYPE routines
!!------------------------------------------------------------
interface io_add_attribute
module procedure io_add_attribute_r, io_add_attribute_i, io_add_attribute_c
end interface
! to be added as necessary
!, io_add_attribute_d
! All routines are public
contains
!>------------------------------------------------------------
!! Tests to see if a file exists
!!
!! @param filename name of file to look for
!! @retval logical true if file exists, false if it doesn't
!!
!!------------------------------------------------------------
logical function file_exists(filename)
character(len=*), intent(in) :: filename
inquire(file=filename,exist=file_exists)
end function file_exists
!>------------------------------------------------------------
!! Tests to see if a variable is present in a netcdf file
!! returns true of it is, false if it isn't
!!
!! @param filename name of NetCDF file
!! @param variable_name name of variable to search for in filename
!! @retval logical True if variable_name is present in filename
!!
!!------------------------------------------------------------
logical function io_variable_is_present(filename,variable_name)
character(len=*), intent(in) :: filename
character(len=*), intent(in) :: variable_name
integer :: ncid,err,varid
call check(nf90_open(filename, NF90_NOWRITE, ncid))
err = nf90_inq_varid(ncid, variable_name, varid)
call check( nf90_close(ncid),filename )
io_variable_is_present = (err==NF90_NOERR)
end function io_variable_is_present
!>------------------------------------------------------------
!! Finds the nearest time step in a file to a given MJD
!! Uses the "time" variable from filename.
!!
!! @param filename Name of an ICAR NetCDF output file
!! @param mjd Modified Julian day to find.
!! If on a noleap calendar, it assumes MJD is days since 1900
!! @retval integer Index into the time dimension (last dim)
!!
!!------------------------------------------------------------
integer function io_nearest_time_step(filename, mjd)
character(len=*),intent(in) :: filename
double precision, intent(in) :: mjd
double precision, allocatable, dimension(:) :: time_data
integer :: ncid,varid,dims(1),ntimes,i
call check(nf90_open(filename, NF90_NOWRITE, ncid),filename)
! Get the varid of the data_in variable, based on its name.
call check(nf90_inq_varid(ncid, "time", varid), trim(filename)//" : time")
call check(nf90_inquire_variable(ncid, varid, dimids = dims), trim(filename)//" : time dims")
call check(nf90_inquire_dimension(ncid, dims(1), len = ntimes), trim(filename)//" : inq time dim")
allocate(time_data(ntimes))
call check(nf90_get_var(ncid, varid, time_data),trim(filename)//"reading time")
! Close the file, freeing all resources.
call check( nf90_close(ncid),filename)
io_nearest_time_step=1
do i=1,ntimes
! keep track of every time that occurs before the mjd we are looking for
! the last one will be the date we want to use.
if ((mjd - time_data(i)) > -1e-4) then
io_nearest_time_step=i
endif
end do
deallocate(time_data)
end function io_nearest_time_step
!>------------------------------------------------------------
!! Read the dimensions of a variable in a given netcdf file
!!
!! @param filename Name of NetCDF file to look at
!! @param varname Name of the NetCDF variable to find the dimensions of
!! @param[out] dims Allocated array to store output
!! @retval dims(:) dims[1]=ndims, dims[i+1]=length of dimension i for a given variable
!!
!!------------------------------------------------------------
subroutine io_getdims(filename,varname,dims)
implicit none
character(len=*), intent(in) :: filename,varname
integer,intent(out) :: dims(:)
! internal variables
integer :: ncid,varid,numDims,dimlen,i
integer,dimension(io_maxDims) :: dimIds
! open the netcdf file
call check(nf90_open(filename, NF90_NOWRITE, ncid),filename)
! Get the varid of the variable, based on its name.
call check(nf90_inq_varid(ncid, varname, varid),varname)
! find the number of dimensions
call check(nf90_inquire_variable(ncid, varid, ndims = numDims),varname)
! find the dimension IDs
call check(nf90_inquire_variable(ncid, varid, dimids = dimIds(:numDims)),varname)
dims(1)=numDims
! finally, find the length of each dimension
do i=1,numDims
call check(nf90_inquire_dimension(ncid, dimIds(i), len = dimlen))
dims(i+1)=dimlen
end do
! Close the file, freeing all resources.
call check( nf90_close(ncid),filename )
end subroutine io_getdims
!>------------------------------------------------------------
!! Reads in a variable from a netcdf file, allocating memory in data_in for it.
!!
!! if extradim is provided specifies this index for any extra dimensions (dims>6)
!! e.g. we may only want one time slice from a 6d variable
!!
!! @param filename Name of NetCDF file to look at
!! @param varname Name of the NetCDF variable to read
!! @param[out] data_in Allocatable 6-dimensional array to store output
!! @param extradim OPTIONAL: specify the position to read for any extra (e.g. time) dimension
!! @retval data_in Allocated 6-dimensional array with the netCDF data
!!
!!------------------------------------------------------------
subroutine io_read6d(filename, varname, data_in, extradim)
implicit none
! This is the name of the data_in file and variable we will read.
character(len=*), intent(in) :: filename, varname
real, allocatable, intent(out) :: data_in(:,:,:,:,:,:)
integer, optional, intent(in) :: extradim
integer, dimension(io_maxDims) :: diminfo !will hold dimension lengths
integer, dimension(io_maxDims) :: dimstart
! This will be the netCDF ID for the file and data_in variable.
integer :: ncid, varid,i
if (present(extradim)) then
dimstart=extradim
dimstart(1:6)=1
else
dimstart=1
endif
! Read the dimension lengths
call io_getdims(filename,varname,diminfo)
allocate(data_in(diminfo(2),diminfo(3),diminfo(4),diminfo(5),diminfo(6),diminfo(7)))
! Open the file. NF90_NOWRITE tells netCDF we want read-only access to the file.
call check(nf90_open(filename, NF90_NOWRITE, ncid),filename)
! Get the varid of the data_in variable, based on its name.
call check(nf90_inq_varid(ncid, varname, varid),trim(filename)//":"//trim(varname))
! Read the data_in. skip the slowest varying indices if there are more than 6 dimensions (typically this will be time)
! and good luck if you have more than 6 dimensions...
if (diminfo(1)>6) then
diminfo(8:diminfo(1)+1)=1 ! set count for extra dims to 1
call check(nf90_get_var(ncid, varid, data_in,&
dimstart(1:diminfo(1)), & ! start = 1 or extradim
[ (diminfo(i+1), i=1,diminfo(1)) ],& ! count=n or 1 created through an implied do loop
[ (1, i=1,diminfo(1)) ]),& ! for all dims, stride = 1 " implied do loop
trim(filename)//":"//trim(varname)) !pass file:var to check so it can give us more info
else
call check(nf90_get_var(ncid, varid, data_in),trim(filename)//":"//trim(varname))
endif
! Close the file, freeing all resources.
call check( nf90_close(ncid),filename)
end subroutine io_read6d
!>------------------------------------------------------------
!! Same as io_read6d but for 5-dimensional data
!!
!! Reads in a variable from a netcdf file, allocating memory in data_in for it.
!!
!! if extradim is provided specifies this index for any extra dimensions (dims>3)
!! e.g. we may only want one time slice from a 3d variable
!!
!! @param filename Name of NetCDF file to look at
!! @param varname Name of the NetCDF variable to read
!! @param[out] data_in Allocatable 3-dimensional array to store output
!! @param extradim OPTIONAL: specify the position to read for any extra (e.g. time) dimension
!! @retval data_in Allocated 3-dimensional array with the netCDF data
!!
!!------------------------------------------------------------
subroutine io_read5d(filename,varname,data_in,extradim)
implicit none
! This is the name of the data_in file and variable we will read.
character(len=*), intent(in) :: filename, varname
real,intent(out),allocatable :: data_in(:,:,:,:,:)
integer, intent(in),optional :: extradim
integer, dimension(io_maxDims) :: diminfo !will hold dimension lengths
integer, dimension(io_maxDims) :: dimstart
! This will be the netCDF ID for the file and data_in variable.
integer :: ncid, varid,i, err
real :: scale, offset
if (present(extradim)) then
dimstart=extradim
dimstart(1:5)=1
else
dimstart=1
endif
! Read the dimension lengths
call io_getdims(filename,varname,diminfo)
allocate(data_in(diminfo(2),diminfo(3),diminfo(4),diminfo(5),diminfo(6)))
! Open the file. NF90_NOWRITE tells netCDF we want read-only access to
! the file.
call check(nf90_open(filename, NF90_NOWRITE, ncid),filename)
! Get the varid of the data_in variable, based on its name.
call check(nf90_inq_varid(ncid, varname, varid),trim(filename)//":"//trim(varname))
err = nf90_get_att(ncid,varid,'scale_factor', scale)
if (err/=0) scale=1
err = nf90_get_att(ncid,varid,'add_offset', offset)
if (err/=0) offset=0
! Read the data_in. skip the slowest varying indices if there are more than 3 dimensions (typically this will be time)
if (diminfo(1)>5) then
diminfo(7:diminfo(1)+1)=1 ! set count for extra dims to 1
call check(nf90_get_var(ncid, varid, data_in,&
dimstart(1:diminfo(1)), & ! start = 1 or extradim
[ (diminfo(i+1), i=1,diminfo(1)) ],& ! count=n or 1 created through an implied do loop
[ (1, i=1,diminfo(1)) ]),& ! for all dims, stride = 1 " implied do loop
trim(filename)//":"//trim(varname)) !pass file:var to check so it can give us more info
else
call check(nf90_get_var(ncid, varid, data_in),trim(filename)//":"//trim(varname))
endif
data_in = data_in * scale + offset
! Close the file, freeing all resources.
call check( nf90_close(ncid),filename)
end subroutine io_read5d
!>------------------------------------------------------------
!! Same as io_read6d but for 3-dimensional data
!!
!! Reads in a variable from a netcdf file, allocating memory in data_in for it.
!!
!! if extradim is provided specifies this index for any extra dimensions (dims>3)
!! e.g. we may only want one time slice from a 3d variable
!!
!! @param filename Name of NetCDF file to look at
!! @param varname Name of the NetCDF variable to read
!! @param[out] data_in Allocatable 3-dimensional array to store output
!! @param extradim OPTIONAL: specify the position to read for any extra (e.g. time) dimension
!! @retval data_in Allocated 3-dimensional array with the netCDF data
!!
!!------------------------------------------------------------
subroutine io_read3d(filename,varname,data_in,extradim)
implicit none
! This is the name of the data_in file and variable we will read.
character(len=*), intent(in) :: filename, varname
real,intent(out),allocatable :: data_in(:,:,:)
integer, intent(in),optional :: extradim
integer, dimension(io_maxDims) :: diminfo !will hold dimension lengths
integer, dimension(io_maxDims) :: dimstart
! This will be the netCDF ID for the file and data_in variable.
integer :: ncid, varid,i, err
real :: scale, offset
if (present(extradim)) then
dimstart=extradim
dimstart(1:3)=1
else
dimstart=1
endif
! Read the dimension lengths
call io_getdims(filename,varname,diminfo)
allocate(data_in(diminfo(2),diminfo(3),diminfo(4)))
! Open the file. NF90_NOWRITE tells netCDF we want read-only access to
! the file.
call check(nf90_open(filename, NF90_NOWRITE, ncid),filename)
! Get the varid of the data_in variable, based on its name.
call check(nf90_inq_varid(ncid, varname, varid),trim(filename)//":"//trim(varname))
err = nf90_get_att(ncid,varid,'scale_factor', scale)
if (err/=0) scale=1
err = nf90_get_att(ncid,varid,'add_offset', offset)
if (err/=0) offset=0
! Read the data_in. skip the slowest varying indices if there are more than 3 dimensions (typically this will be time)
if (diminfo(1)>3) then
diminfo(5:diminfo(1)+1)=1 ! set count for extra dims to 1
call check(nf90_get_var(ncid, varid, data_in,&
dimstart(1:diminfo(1)), & ! start = 1 or extradim
[ (diminfo(i+1), i=1,diminfo(1)) ],& ! count=n or 1 created through an implied do loop
[ (1, i=1,diminfo(1)) ]),& ! for all dims, stride = 1 " implied do loop
trim(filename)//":"//trim(varname)) !pass file:var to check so it can give us more info
else
call check(nf90_get_var(ncid, varid, data_in),trim(filename)//":"//trim(varname))
endif
data_in = data_in * scale + offset
! Close the file, freeing all resources.
call check( nf90_close(ncid),filename)
end subroutine io_read3d
!>------------------------------------------------------------
!! Same as io_read3d but for 2-dimensional data
!!
!! Reads in a variable from a netcdf file, allocating memory in data_in for it.
!!
!! if extradim is provided specifies this index for any extra dimensions (dims>2)
!! e.g. we may only want one time slice from a 2d variable
!!
!! @param filename Name of NetCDF file to look at
!! @param varname Name of the NetCDF variable to read
!! @param[out] data_in Allocatable 2-dimensional array to store output
!! @param extradim OPTIONAL: specify the position to read for any extra (e.g. time) dimension
!! @retval data_in Allocated 2-dimensional array with the netCDF data
!!
!!------------------------------------------------------------
subroutine io_read2d(filename,varname,data_in,extradim)
implicit none
! This is the name of the data_in file and variable we will read.
character(len=*), intent(in) :: filename, varname
real,intent(out),allocatable :: data_in(:,:)
integer, intent(in),optional :: extradim
integer, dimension(io_maxDims) :: diminfo ! will hold dimension lengths
integer, dimension(io_maxDims) :: dimstart
! This will be the netCDF ID for the file and data_in variable.
integer :: ncid, varid,i, err
real :: scale, offset
if (present(extradim)) then
dimstart=extradim
dimstart(1:2)=1
else
dimstart=1
endif
diminfo = 1
! Read the dimension lengths
call io_getdims(filename,varname,diminfo)
allocate(data_in(diminfo(2),diminfo(3)))
! Open the file. NF90_NOWRITE tells netCDF we want read-only access to
! the file.
call check(nf90_open(filename, NF90_NOWRITE, ncid),filename)
! Get the varid of the data_in variable, based on its name.
call check(nf90_inq_varid(ncid, varname, varid),trim(filename)//":"//trim(varname))
err = nf90_get_att(ncid,varid,'scale_factor', scale)
if (err/=0) scale=1
err = nf90_get_att(ncid,varid,'add_offset', offset)
if (err/=0) offset=0
! Read the data_in. skip the slowest varying indices if there are more than 3 dimensions (typically this will be time)
if (diminfo(1)>2) then
diminfo(4:diminfo(1)+1)=1 ! set count for extra dims to 1
call check(nf90_get_var(ncid, varid, data_in,&
dimstart(1:diminfo(1)), & ! start = 1 or extradim
[ (diminfo(i+1), i=1,diminfo(1)) ],& ! count=n or 1 created through an implied do loop
[ (1, i=1,diminfo(1)) ] ), & ! for all dims, stride = 1 " implied do loop
trim(filename)//":"//trim(varname)) !pass varname to check so it can give us more info
elseif (diminfo(1)==1) then
call check(nf90_get_var(ncid, varid, data_in(:,1)),trim(filename)//":"//trim(varname))
else
call check(nf90_get_var(ncid, varid, data_in),trim(filename)//":"//trim(varname))
endif
data_in = data_in * scale + offset
! Close the file, freeing all resources.
call check( nf90_close(ncid),filename)
end subroutine io_read2d
!>------------------------------------------------------------
!! Same as io_read2d but for integer data
!!
!! Reads in a variable from a netcdf file, allocating memory in data_in for it.
!!
!! if extradim is provided specifies this index for any extra dimensions (dims>2)
!! e.g. we may only want one time slice from a 2d variable
!!
!! @param filename Name of NetCDF file to look at
!! @param varname Name of the NetCDF variable to read
!! @param[out] data_in Allocatable 2-dimensional array to store output
!! @param extradim OPTIONAL: specify the position to read for any extra (e.g. time) dimension
!! @retval data_in Allocated 2-dimensional array with the netCDF data
!!
!!------------------------------------------------------------
subroutine io_read2di(filename,varname,data_in,extradim)
implicit none
! This is the name of the data_in file and variable we will read.
character(len=*), intent(in) :: filename, varname
integer,intent(out),allocatable :: data_in(:,:)
integer, intent(in),optional :: extradim
integer, dimension(io_maxDims) :: diminfo ! will hold dimension lengths
integer, dimension(io_maxDims) :: dimstart
! This will be the netCDF ID for the file and data_in variable.
integer :: ncid, varid,i
if (present(extradim)) then
dimstart=extradim
dimstart(1:2)=1
else
dimstart=1
endif
! Read the dimension lengths
call io_getdims(filename,varname,diminfo)
allocate(data_in(diminfo(2),diminfo(3)))
! Open the file. NF90_NOWRITE tells netCDF we want read-only access to
! the file.
call check(nf90_open(filename, NF90_NOWRITE, ncid),filename)
! Get the varid of the data_in variable, based on its name.
call check(nf90_inq_varid(ncid, varname, varid),trim(filename)//":"//trim(varname))
! Read the data_in. skip the slowest varying indices if there are more than 3 dimensions (typically this will be time)
if (diminfo(1)>2) then
diminfo(4:diminfo(1)+1)=1 ! set count for extra dims to 1
call check(nf90_get_var(ncid, varid, data_in,&
dimstart(1:diminfo(1)), & ! start = 1 or extradim
[ (diminfo(i+1), i=1,diminfo(1)) ],& ! count=n or 1 created through an implied do loop
[ (1, i=1,diminfo(1)) ] ), & ! for all dims, stride = 1 " implied do loop
trim(filename)//":"//trim(varname)) !pass varname to check so it can give us more info
else
call check(nf90_get_var(ncid, varid, data_in),varname)
endif
! Close the file, freeing all resources.
call check( nf90_close(ncid),filename)
end subroutine io_read2di
!>------------------------------------------------------------
!! Same as io_read3d but for 1-dimensional data
!!
!! Reads in a variable from a netcdf file, allocating memory in data_in for it.
!!
!! if extradim is provided specifies this index for any extra dimensions (dims>1)
!! e.g. we may only want one time slice from a 1d variable
!!
!! @param filename Name of NetCDF file to look at
!! @param varname Name of the NetCDF variable to read
!! @param[out] data_in Allocatable 1-dimensional array to store output
!! @param extradim OPTIONAL: specify the position to read for any extra (e.g. time) dimension
!! @retval data_in Allocated 1-dimensional array with the netCDF data
!!
!!------------------------------------------------------------
subroutine io_read1d(filename,varname,data_in,extradim)
implicit none
! This is the name of the data_in file and variable we will read.
character(len=*), intent(in) :: filename, varname
real,intent(out),allocatable :: data_in(:)
integer, intent(in),optional :: extradim
integer, dimension(io_maxDims) :: diminfo ! will hold dimension lengths
integer, dimension(io_maxDims) :: dimstart
! This will be the netCDF ID for the file and data_in variable.
integer :: ncid, varid,i
if (present(extradim)) then
dimstart=extradim
dimstart(1)=1
else
dimstart=1
endif
! Read the dimension lengths
call io_getdims(filename,varname,diminfo)
allocate(data_in(diminfo(2)))
! Open the file. NF90_NOWRITE tells netCDF we want read-only access to
! the file.
call check(nf90_open(filename, NF90_NOWRITE, ncid),filename)
! Get the varid of the data_in variable, based on its name.
call check(nf90_inq_varid(ncid, varname, varid),trim(filename)//":"//trim(varname))
! Read the data_in. skip the slowest varying indices if there are more than 1 dimensions (typically this will be time)
if (diminfo(1)>1) then
diminfo(3:diminfo(1)+1)=1 ! set count for extra dims to 1
call check(nf90_get_var(ncid, varid, data_in,&
dimstart(1:diminfo(1)), & ! start = 1 or extradim
[ (diminfo(i+1), i=1,diminfo(1)) ],& ! count=n or 1 created through an implied do loop
[ (1, i=1,diminfo(1)) ] ), & ! for all dims, stride = 1 " implied do loop
trim(filename)//":"//trim(varname)) !pass varname to check so it can give us more info
else
call check(nf90_get_var(ncid, varid, data_in),trim(filename)//":"//trim(varname))
endif
! Close the file, freeing all resources.
call check( nf90_close(ncid),filename)
end subroutine io_read1d
!>------------------------------------------------------------
!! Same as io_read1d but for double precision data
!!
!! Reads in a variable from a netcdf file, allocating memory in data_in for it.
!!
!! if extradim is provided specifies this index for any extra dimensions (dims>1)
!! e.g. we may only want one time slice from a 1d variable
!!
!! @param filename Name of NetCDF file to look at
!! @param varname Name of the NetCDF variable to read
!! @param[out] data_in Allocatable 1-dimensional array to store output
!! @param extradim OPTIONAL: specify the position to read for any extra (e.g. time) dimension
!! @retval data_in Allocated 1-dimensional array with the netCDF data
!!
!!------------------------------------------------------------
subroutine io_read1dd(filename,varname,data_in,extradim)
implicit none
! This is the name of the data_in file and variable we will read.
character(len=*), intent(in) :: filename, varname
double precision,intent(out),allocatable :: data_in(:)
integer, intent(in),optional :: extradim
integer, dimension(io_maxDims) :: diminfo ! will hold dimension lengths
integer, dimension(io_maxDims) :: dimstart
! This will be the netCDF ID for the file and data_in variable.
integer :: ncid, varid,i
if (present(extradim)) then
dimstart=extradim
dimstart(1)=1
else
dimstart=1
endif
! Read the dimension lengths
diminfo = 1
call io_getdims(filename,varname,diminfo)
allocate(data_in(diminfo(2)))
! Open the file. NF90_NOWRITE tells netCDF we want read-only access to
! the file.
call check(nf90_open(filename, NF90_NOWRITE, ncid),filename)
! Get the varid of the data_in variable, based on its name.
call check(nf90_inq_varid(ncid, varname, varid),trim(filename)//":"//trim(varname))
! Read the data_in. skip the slowest varying indices if there are more than 1 dimensions (typically this will be time)
if (diminfo(1)>1) then
diminfo(3:diminfo(1)+1)=1 ! set count for extra dims to 1
call check(nf90_get_var(ncid, varid, data_in,&
dimstart(1:diminfo(1)), & ! start = 1 or extradim
[ (diminfo(i+1), i=1,diminfo(1)) ],& ! count=n or 1 created through an implied do loop
[ (1, i=1,diminfo(1)) ] ), & ! for all dims, stride = 1 " implied do loop
trim(filename)//":"//trim(varname)) !pass varname to check so it can give us more info
else
call check(nf90_get_var(ncid, varid, data_in),trim(filename)//":"//trim(varname))
endif
! Close the file, freeing all resources.
call check( nf90_close(ncid),filename)
end subroutine io_read1dd
subroutine io_read_scalar_d(filename, varname, result, step)
implicit none
! This is the name of the data_in file and variable we will read.
character(len=*), intent(in) :: filename, varname
double precision, intent(out) :: result
integer, intent(in) :: step
double precision, allocatable :: data_in(:)
! This will be the netCDF ID for the file and data_in variable.
integer :: ncid, varid,i
! Open the file. NF90_NOWRITE tells netCDF we want read-only access to
! the file.
call check(nf90_open(filename, NF90_NOWRITE, ncid),filename)
! Get the varid of the data_in variable, based on its name.
call check(nf90_inq_varid(ncid, varname, varid),trim(filename)//":"//trim(varname))
! Read the data_in. Just reads a scalar from the 1D array
call check(nf90_get_var(ncid, varid, data_in), &
trim(filename)//":"//trim(varname))
result = data_in(step)
deallocate(data_in)
! Close the file, freeing all resources.
call check( nf90_close(ncid),filename)
end subroutine io_read_scalar_d
!>------------------------------------------------------------
!! Write a 6-dimensional variable to a netcdf file
!!
!! Create a netcdf file:filename with a variable:varname and write data_out to it
!!
!! @param filename Name of NetCDF file to write/create
!! @param varname Name of the NetCDF variable to write
!! @param data_out 6-dimensional array to write to the file
!!
!!------------------------------------------------------------
subroutine io_write6d(filename,varname,data_out, dimnames)
implicit none
! This is the name of the file and variable we will write.
character(len=*), intent(in) :: filename, varname
real,intent(in) :: data_out(:,:,:,:,:,:)
character(len=*), optional, dimension(6), intent(in) :: dimnames
! We are writing 6D data, a nx, nz, ny, na, nb, nc grid.
integer :: nx,ny,nz, na,nb,nc
integer, parameter :: ndims = 6
! This will be the netCDF ID for the file and data variable.
integer :: ncid, varid,temp_dimid,dimids(ndims)
character(len=MAXDIMLENGTH), dimension(6) :: dims
if (present(dimnames)) then
dims = dimnames
else
dims = ["x","y","z","a","b","c"]
endif
nx=size(data_out,1)
nz=size(data_out,2)
ny=size(data_out,3)
na=size(data_out,4)
nb=size(data_out,5)
nc=size(data_out,6)
! Open the file. NF90_CLOBBER tells netCDF we want overwrite existing files
call check( nf90_create(filename, NF90_CLOBBER, ncid), filename)
! define the dimensions
call check( nf90_def_dim(ncid, dims(1), nx, temp_dimid) )
dimids(1)=temp_dimid
call check( nf90_def_dim(ncid, dims(2), nz, temp_dimid) )
dimids(2)=temp_dimid
call check( nf90_def_dim(ncid, dims(3), ny, temp_dimid) )
dimids(3)=temp_dimid
call check( nf90_def_dim(ncid, dims(4), na, temp_dimid) )
dimids(4)=temp_dimid
call check( nf90_def_dim(ncid, dims(5), nb, temp_dimid) )
dimids(5)=temp_dimid
call check( nf90_def_dim(ncid, dims(6), nc, temp_dimid) )
dimids(6)=temp_dimid
! Create the variable returns varid of the data variable
call check( nf90_def_var(ncid, varname, NF90_REAL, dimids, varid), trim(filename)//":"//trim(varname))
! End define mode. This tells netCDF we are done defining metadata.
call check( nf90_enddef(ncid) )
!write the actual data to the file
call check( nf90_put_var(ncid, varid, data_out), trim(filename)//":"//trim(varname))
! Close the file, freeing all resources.
call check( nf90_close(ncid), filename)
end subroutine io_write6d
!>------------------------------------------------------------
!! Same as io_write6d but for 5-dimensional data
!!
!! Write a 5-dimensional variable to a netcdf file
!!
!! Create a netcdf file:filename with a variable:varname and write data_out to it
!!
!! @param filename Name of NetCDF file to write/create
!! @param varname Name of the NetCDF variable to write
!! @param data_out 5-dimensional array to write to the file
!!
!!------------------------------------------------------------
subroutine io_write5d(filename,varname,data_out, dimnames)
implicit none
! This is the name of the file and variable we will write.
character(len=*), intent(in) :: filename, varname
real,intent(in) :: data_out(:,:,:,:,:)
character(len=*), optional, dimension(5), intent(in) :: dimnames
! We are writing 5D data, a nx, nz, ny, na, nb, nc grid.
integer :: nx,ny,nz, na,nb
integer, parameter :: ndims = 5
! This will be the netCDF ID for the file and data variable.
integer :: ncid, varid,temp_dimid,dimids(ndims)
character(len=MAXDIMLENGTH), dimension(5) :: dims
if (present(dimnames)) then
dims = dimnames
else
dims = ["x","y","z","a","b"]
endif
nx=size(data_out,1)
nz=size(data_out,2)
ny=size(data_out,3)
na=size(data_out,4)
nb=size(data_out,5)
! Open the file. NF90_CLOBBER tells netCDF we want overwrite existing files
call check( nf90_create(filename, NF90_CLOBBER, ncid), filename)
! define the dimensions
call check( nf90_def_dim(ncid, dims(1), nx, temp_dimid) )
dimids(1)=temp_dimid
call check( nf90_def_dim(ncid, dims(2), nz, temp_dimid) )
dimids(2)=temp_dimid
call check( nf90_def_dim(ncid, dims(3), ny, temp_dimid) )
dimids(3)=temp_dimid
call check( nf90_def_dim(ncid, dims(4), na, temp_dimid) )
dimids(4)=temp_dimid
call check( nf90_def_dim(ncid, dims(5), nb, temp_dimid) )
dimids(5)=temp_dimid
! Create the variable returns varid of the data variable
call check( nf90_def_var(ncid, varname, NF90_REAL, dimids, varid), trim(filename)//":"//trim(varname))
! End define mode. This tells netCDF we are done defining metadata.
call check( nf90_enddef(ncid) )
!write the actual data to the file
call check( nf90_put_var(ncid, varid, data_out), trim(filename)//":"//trim(varname))
! Close the file, freeing all resources.
call check( nf90_close(ncid), filename)
end subroutine io_write5d
!>------------------------------------------------------------
!! Same as io_write6d but for 4-dimensional data
!!
!! Write a 4-dimensional variable to a netcdf file
!!
!! Create a netcdf file:filename with a variable:varname and write data_out to it
!!
!! @param filename Name of NetCDF file to write/create
!! @param varname Name of the NetCDF variable to write
!! @param data_out 4-dimensional array to write to the file
!!
!!------------------------------------------------------------
subroutine io_write4d(filename,varname,data_out)
implicit none
! This is the name of the file and variable we will write.
character(len=*), intent(in) :: filename, varname
real,intent(in) :: data_out(:,:,:,:)
! We are writing 4D data, assume a nx x nz x ny x nr grid.
integer :: nx,ny,nz,nr
integer, parameter :: ndims = 4
! This will be the netCDF ID for the file and data variable.
integer :: ncid, varid,temp_dimid,dimids(ndims)
nx=size(data_out,1)
nz=size(data_out,2)
ny=size(data_out,3)
nr=size(data_out,4)
! Open the file. NF90_NOWRITE tells netCDF we want read-only access to
! the file.
call check( nf90_create(filename, NF90_CLOBBER, ncid), filename)
! define the dimensions
call check( nf90_def_dim(ncid, "x", nx, temp_dimid) )
dimids(1)=temp_dimid
call check( nf90_def_dim(ncid, "z", nz, temp_dimid) )
dimids(2)=temp_dimid
call check( nf90_def_dim(ncid, "y", ny, temp_dimid) )
dimids(3)=temp_dimid
call check( nf90_def_dim(ncid, "r", nr, temp_dimid) )
dimids(4)=temp_dimid
! Create the variable returns varid of the data variable
call check( nf90_def_var(ncid, varname, NF90_REAL, dimids, varid), trim(filename)//":"//trim(varname))
! End define mode. This tells netCDF we are done defining metadata.
call check( nf90_enddef(ncid) )
! write the actual data to the file
call check( nf90_put_var(ncid, varid, data_out), trim(filename)//":"//trim(varname))
! Close the file, freeing all resources.
call check( nf90_close(ncid), filename)
end subroutine io_write4d
!>------------------------------------------------------------
!! Same as io_write4d but for integer data
!!
!! Write a 4-dimensional variable to a netcdf file
!!
!! Create a netcdf file:filename with a variable:varname and write data_out to it
!!
!! @param filename Name of NetCDF file to write/create
!! @param varname Name of the NetCDF variable to write
!! @param data_out 4-dimensional array to write to the file
!!
!!------------------------------------------------------------
subroutine io_write4di(filename,varname,data_out)
implicit none
! This is the name of the file and variable we will write.
character(len=*), intent(in) :: filename, varname
integer,intent(in) :: data_out(:,:,:,:)
! We are writing 4D data, assume a nx x nz x ny x nr grid.
integer :: nx,ny,nz,nr
integer, parameter :: ndims = 4
! This will be the netCDF ID for the file and data variable.
integer :: ncid, varid,temp_dimid,dimids(ndims)
nx=size(data_out,1)
nz=size(data_out,2)
ny=size(data_out,3)
nr=size(data_out,4)
! Open the file. NF90_NOWRITE tells netCDF we want read-only access to
! the file.
call check( nf90_create(filename, NF90_CLOBBER, ncid), filename)
! define the dimensions
call check( nf90_def_dim(ncid, "x", nx, temp_dimid) )
dimids(1)=temp_dimid
call check( nf90_def_dim(ncid, "z", nz, temp_dimid) )
dimids(2)=temp_dimid
call check( nf90_def_dim(ncid, "y", ny, temp_dimid) )
dimids(3)=temp_dimid
call check( nf90_def_dim(ncid, "r", nr, temp_dimid) )
dimids(4)=temp_dimid
! Create the variable returns varid of the data variable
call check( nf90_def_var(ncid, varname, NF90_INT, dimids, varid), trim(filename)//":"//trim(varname))
! End define mode. This tells netCDF we are done defining metadata.
call check( nf90_enddef(ncid) )
! write the actual data to the file
call check( nf90_put_var(ncid, varid, data_out), trim(filename)//":"//trim(varname))
! Close the file, freeing all resources.
call check( nf90_close(ncid), filename)
end subroutine io_write4di
!>------------------------------------------------------------
!! Same as io_write6d but for 3-dimensional data
!!
!! Write a 3-dimensional variable to a netcdf file
!!
!! Create a netcdf file:filename with a variable:varname and write data_out to it
!!
!! @param filename Name of NetCDF file to write/create
!! @param varname Name of the NetCDF variable to write
!! @param data_out 3-dimensional array to write to the file
!!
!!------------------------------------------------------------
subroutine io_write3d(filename,varname,data_out)
implicit none
! This is the name of the file and variable we will write.
character(len=*), intent(in) :: filename, varname
real,intent(in) :: data_out(:,:,:)
! We are reading 2D data, a nx x ny grid.
integer :: nx,ny,nz
integer, parameter :: ndims = 3
! This will be the netCDF ID for the file and data variable.
integer :: ncid, varid,temp_dimid,dimids(ndims)
nx=size(data_out,1)
nz=size(data_out,2)
ny=size(data_out,3)
! Open the file. NF90_NOWRITE tells netCDF we want read-only access to
! the file.
call check( nf90_create(filename, NF90_CLOBBER, ncid), filename)
! define the dimensions
call check( nf90_def_dim(ncid, "x", nx, temp_dimid) )
dimids(1)=temp_dimid
call check( nf90_def_dim(ncid, "z", nz, temp_dimid) )
dimids(2)=temp_dimid
call check( nf90_def_dim(ncid, "y", ny, temp_dimid) )
dimids(3)=temp_dimid
! Create the variable returns varid of the data variable
call check( nf90_def_var(ncid, varname, NF90_REAL, dimids, varid), trim(filename)//":"//trim(varname))
! End define mode. This tells netCDF we are done defining metadata.
call check( nf90_enddef(ncid) )
! write the actual data to the file
call check( nf90_put_var(ncid, varid, data_out), trim(filename)//":"//trim(varname))
! Close the file, freeing all resources.
call check( nf90_close(ncid), filename)
end subroutine io_write3d
!>------------------------------------------------------------
!! Same as io_write3d but for integer arrays
!!
!! Write a 3-dimensional variable to a netcdf file
!!
!! Create a netcdf file:filename with a variable:varname and write data_out to it
!!
!! @param filename Name of NetCDF file to write/create
!! @param varname Name of the NetCDF variable to write
!! @param data_out 3-dimensional array to write to the file
!!
!!------------------------------------------------------------
subroutine io_write3di(filename,varname,data_out)
implicit none
! This is the name of the data file and variable we will read.
character(len=*), intent(in) :: filename, varname
integer,intent(in) :: data_out(:,:,:)
! We are reading 2D data, a nx x ny grid.
integer :: nx,ny,nz
integer, parameter :: ndims = 3
! This will be the netCDF ID for the file and data variable.
integer :: ncid, varid,temp_dimid,dimids(ndims)
nx=size(data_out,1)
nz=size(data_out,2)
ny=size(data_out,3)
! Open the file. NF90_NOWRITE tells netCDF we want read-only access to
! the file.
call check( nf90_create(filename, NF90_CLOBBER, ncid) )
! define the dimensions
call check( nf90_def_dim(ncid, "x", nx, temp_dimid) )
dimids(1)=temp_dimid
call check( nf90_def_dim(ncid, "z", nz, temp_dimid) )
dimids(2)=temp_dimid
call check( nf90_def_dim(ncid, "y", ny, temp_dimid) )
dimids(3)=temp_dimid
! Create the variable returns varid of the data variable
call check( nf90_def_var(ncid, varname, NF90_INT, dimids, varid), trim(filename)//":"//trim(varname) )
! End define mode. This tells netCDF we are done defining metadata.
call check( nf90_enddef(ncid) )
call check( nf90_put_var(ncid, varid, data_out),trim(filename)//":"//trim(varname) )
! Close the file, freeing all resources.
call check( nf90_close(ncid) )
end subroutine io_write3di
!>------------------------------------------------------------
!! Same as io_write3d but for 2-dimensional arrays
!!
!! Write a 2-dimensional variable to a netcdf file
!!
!! Create a netcdf file:filename with a variable:varname and write data_out to it
!!
!! @param filename Name of NetCDF file to write/create
!! @param varname Name of the NetCDF variable to write
!! @param data_out 2-dimensional array to write to the file
!!
!!------------------------------------------------------------
subroutine io_write2d(filename,varname,data_out)
implicit none
! This is the name of the data file and variable we will read.
character(len=*), intent(in) :: filename, varname
real,intent(in) :: data_out(:,:)
! We are reading 2D data, a nx x ny grid.
integer :: nx,ny
integer, parameter :: ndims = 2
! This will be the netCDF ID for the file and data variable.
integer :: ncid, varid,temp_dimid,dimids(ndims)