-
Notifications
You must be signed in to change notification settings - Fork 6
/
mo_var2nc.f90
3071 lines (3011 loc) · 135 KB
/
mo_var2nc.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_var2nc
! This module provides a routines for writing netcdf files.
! License
! -------
! This file is part of the JAMS Fortran package, distributed under the MIT License.
!
! Copyright (c) 2014-2016 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: i4, sp, dp
use mo_utils, only: ne
! functions and constants of netcdf4 library
use netcdf, only: &
NF90_INT, NF90_FLOAT, NF90_DOUBLE, NF90_UNLIMITED, NF90_WRITE, NF90_NOERR, &
nf90_create, nf90_def_dim, nf90_def_var, nf90_put_att, &
nf90_enddef, nf90_put_var, nf90_close, nf90_strerror, &
nf90_inq_varid, nf90_inquire_variable, nf90_inquire_dimension, nf90_open, &
nf90_inq_varid, nf90_inq_dimid, nf90_inquire, nf90_get_var, nf90_fill_float, &
nf90_fill_double, nf90_fill_int, nf90_redef
#ifndef __NETCDF3__
use netcdf, only: NF90_NETCDF4
#else
use netcdf, only: NF90_64BIT_OFFSET
#endif
! public routines -------------------------------------------------------------------
public :: var2nc ! simple dump of multiple variables with attributes into one netcdf file
! ------------------------------------------------------------------
!
! NAME
! var2nc
!
! PURPOSE
! Provide an intermediate way to write netcdf files
! between dump_netcdf and create_netcdf
!
!> \brief Extended dump_netcdf for multiple variables
!
!> \details Write different variables including attributes to netcdf
!> file. The attributes are restricted to long_name, units,
!> and missing_value. It is also possible to append variables
!> when an unlimited dimension is specified.
!
! CALLING SEQUENCE
!> call var2nc(f_name, arr, dnames, v_name, dim_unlimited,
!> long_name, units, missing_value, create)
!
! INTENT(IN)
!> \param[in] "character(*) :: f_name" filename
!> \param[in] "integer(i4)/real(sp,dp) :: arr(:[,:[,:[,:[,:]]]])" array to write
!> \param[in] "character(*) :: dnames(:)" dimension names
!> \param[in] "character(*) :: v_name" variable name
!
! INTENT(IN), OPTIONAL
!> \param[in] "integer(i4), optional :: dim_unlimited" index of unlimited dimension
!> \param[in] "character(*), optional :: long_name" attribute
!> \param[in] "character(*), optional :: units" attribute
!> \param[in] "integer(i4)/real(sp,dp), optional :: missing_value" attribute
!> \param[in] "character(256), dimension(:,:), optional :: attributes" two dimensional array of attributes
!> size of first dimension equals number of attributes
!> first entry of second dimension equals attribute name (e.g. long_name)
!> second entry of second dimension equals attribute value (e.g. precipitation)
!> every attribute is written as string with the exception of missing_value
!> \param[in] "logical, optional :: create" flag - specify whether a
!> output file should be
!> created, default
!> \param[inout] "integer(i4)/real(sp,dp), optional :: ncid" if not given filename will be opened and closed
!> if given and <0 then file will be opened
!> and ncid will return the file unit.
!> if given and >0 then file is assumed open and
!> ncid is used as file unit.
!> \param[in] "integer(i4), optional :: nrec" if given: start point on unlimited dimension.
!
! RESTRICTIONS
!> \note It is not allowed to write the folloing numbers for the indicated type\n
!> number | kind\n
!> -2.1474836E+09 | integer(i4)\n
!> 9.9692100E+36 | real(sp)\n
!> 9.9692099683868690E+36 | real(dp)\n
!> These numbers are netcdf fortran 90 constants! They are used to determine the
!> chunksize of the already written variable. Hence, this routine cannot append
!> correctly to variables when these numbers are used. Only five dimensional
!> variables can be written, only one unlimited dimension can be defined.
!>
!> The unlimited dimension can be any dimension in netcdf4 files but must be the
!> last dimension when NETCDF3 is used (compiled with -DNETCDF3).
!
! EXAMPLE
! Let <field> be some three dimensional array
! dnames(1) = 'x'
! dnames(2) = 'y'
! dnames(3) = 'time'
!
! The simplest call to write <field> to a file is
! call var2nc('test.nc', field, dnames, 'h')
!
! With attributes it looks like
! call var2nc('test.nc', field, dnames, 'h', &
! long_name = 'height', units = '[m]', missing_value = -9999)
! or alternatively
! character(256), dimension(3,2) :: attributes
! attributes(1,1) = 'long_name'
! attributes(1,2) = 'precipitation'
! attributes(2,1) = 'units'
! attributes(2,2) = '[mm/d]'
! attributes(3,1) = 'missing_value'
! attributes(3,2) = '-9999.'
! call var2nc('test.nc', field, dnames, 'h', attributes = attributes, create = .true. )
! To be able to dynamically write <field>, an unlimited dimension
! needs to be specified (in this example field could also be only two
! dimensional)
! call var2nc('test.nc', field(:,:,1), dnames, 'h', dim_unlimited=3)
! Now one can append an arbitrary number of time steps, e.g., the next 9
! and the time has to be added again before
! call var2nc('test.nc', (/20,...,100/), dnames(3:3), 'time',
! dim_unlimited = 1 )
! call var2nc('test.nc', field(:,:,2:10, dnames, 'h', dim_unlimited=3)
!
! You can also write another variable sharing the same dimensions
! call var2nc('test.nc', field_2, dnames(1:2), 'h_2')
!
! The netcdf file can stay open after the first call and subsequent calls can use the file unit
! ncid = -1_i4
! call var2nc('test.nc', field_1, dnames(1:1), 'h_1', ncid=ncid) ! opens file
! call var2nc('test.nc', field_2, dnames(1:2), 'h_2', ncid=ncid) ! uses ncid from last call
! call close_netcdf(ncid)
!
! One can also give the start record number (on the unlimited dimension)
! ncid = -1_i4
! call var2nc('test.nc', time1, dnames(3:3), 'time', dim_unlimited=1_i4, ncid=ncid, create=.true.)
! do i=1, n
! call var2nc('test.nc', field_2(:,:,i), dnames(1:3), 'h_2', dim_unlimited=3_i4, ncid=ncid, nrec=i)
! end do
! call close_netcdf(ncid)
!
! That's it, enjoy!
! -> see also example in test program
!
! LITERATURE
! The manual of the used netcdf fortran library can be found in
! Robert Pincus & Ross Rew, The netcdf Fortran 90 Interface Guide
!
! HISTORY
!> \author Stephan Thober & Matthias Cuntz
!> \date May 2014
! Modified Stephan Thober - Jun 2014 added deflate, shuffle, and chunksizes
! Stephan Thober - Jun 2014 automatically append variable at the end,
! renamed _FillValue to missing_value
! Stephan Thober - Jul 2014 add attributes array, introduced unlimited dimension
! that is added to the dimensions of the given array
! Stephan Thober - Jan 2015 changed chunk_size convention to one chunk per unit in
! unlimited dimension (typically time)
! Matthias Cuntz - Feb 2015 d_unlimit was not set in 5d cases
! use ne from mo_utils for fill value comparisons
! dummy(1) was sp instead of i4 in var2nc_1d_i4
! Matthias Cuntz - May 2015 ncid for opening the file only once
! nrec for writing a specific record
! Matthias Cuntz - Nov 2016 reopen definition mode when not create and file exists
! netCDF3: unlimited dimension must be last dimension when compiled with -DNETCDF3
interface var2nc
module procedure var2nc_1d_i4, var2nc_1d_sp, var2nc_1d_dp, &
var2nc_2d_i4, var2nc_2d_sp, var2nc_2d_dp, &
var2nc_3d_i4, var2nc_3d_sp, var2nc_3d_dp, &
var2nc_4d_i4, var2nc_4d_sp, var2nc_4d_dp, &
var2nc_5d_i4, var2nc_5d_sp, var2nc_5d_dp
end interface var2nc
! ----------------------------------------------------------------------------
private
#ifdef __NETCDF3__
INTEGER, PARAMETER :: NF90_NETCDF4 = NF90_64BIT_OFFSET ! to be available for compilation
#endif
! ----------------------------------------------------------------------------
contains
! ------------------------------------------------------------------
subroutine var2nc_1d_i4( f_name, arr, dnames, v_name, dim_unlimited, &
long_name, units, missing_value, attributes, create, ncid, nrec )
!
implicit none
!
integer(i4), parameter :: ndim_const = 1
integer(i4) :: ndim
! input variables
character(len=*), intent(in) :: f_name
integer(i4), dimension(:), intent(in) :: arr
character(len=*), intent(in) :: v_name
character(len=*), dimension(:), intent(in) :: dnames
! attributes
integer(i4), optional, intent(in) :: dim_unlimited
character(len=*), optional, intent(in) :: long_name
character(len=*), optional, intent(in) :: units
character(256), dimension(:,:), optional, intent(in) :: attributes
integer(i4), optional, intent(in) :: missing_value
logical, optional, intent(in) :: create
integer(i4), optional, intent(inout) :: ncid
integer(i4), optional, intent(in) :: nrec
! local variables
logical :: create_loc
character(256) :: dummy_name
integer(i4) :: deflate
integer(i4), dimension(:), allocatable :: chunksizes
integer(i4), dimension(:), allocatable :: start ! start array for write
integer(i4), dimension(:), allocatable :: counter ! length array for write
integer(i4) :: idim ! read dimension on append
integer(i4) :: d_unlimit ! index of unlimited dimension
integer(i4) :: u_dimid
integer(i4) :: u_len ! length of unlimited dimension
integer(i4) :: f_handle
integer(i4), dimension(:), allocatable :: dims
integer(i4), dimension(:), allocatable :: dimid ! netcdf IDs of each dimension
integer(i4), dimension(:), allocatable :: varid ! dimension variables and var id
integer(i4) :: i ! loop indices
integer(i4), dimension(:), allocatable :: dummy_count
integer(i4), dimension(1) :: dummy ! dummy read
logical :: openfile ! tmp logical
!
ndim = size(dnames, 1)
! consistency checks
d_unlimit = 0_i4
if ( present( dim_unlimited ) ) d_unlimit = dim_unlimited
if ( ( size( dnames, 1) .eq. ndim_const + 1 ) .and. ( d_unlimit .ne. ndim_const+1 ) ) then
print *, '***ERROR one more dimension name specified than dimension of array, but the last one is not unlimited'
stop '***ERROR see StdOut'
end if
if ( size( dnames, 1) .gt. ndim_const + 1 ) then
print *, '***ERROR too many dimension name specified, should be atmost ndim_const + 1'
stop '***ERROR see StdOut'
end if
allocate( chunksizes( ndim ) )
allocate( start( ndim ) )
allocate( counter( ndim ) )
allocate( dims( ndim ) )
allocate( dimid( ndim ) )
allocate( varid( 1 + ndim ) )
allocate( dummy_count( ndim ) )
! initialize
deflate = 1
if ( ndim .gt. ndim_const ) then
chunksizes = (/ size( arr, 1), 1 /)
dims(1:ndim-1) = shape( arr )
dims(ndim) = 1
else
chunksizes = (/ size( arr, 1) /)
dims(1:ndim_const) = shape( arr )
end if
start(:) = 1
counter(:) = dims
dummy = nf90_fill_int
dummy_count = 1
! open the netcdf file
if (present(ncid)) then
if (ncid < 0_i4) then
openfile = .true.
else
openfile = .false.
f_handle = ncid
endif
else
openfile = .true.
endif
if (openfile) then
create_loc = .false.
if (present(create)) create_loc = create
f_handle = open_netcdf(f_name, create=create_loc)
else
create_loc = .false.
endif
! check whether variable exists
if ( NF90_NOERR .eq. nf90_inq_varid( f_handle, v_name, varid(ndim+1)) ) then
! append
call check(nf90_inquire_variable(f_handle, varid(ndim+1), ndims=idim, dimids=dimid))
if (idim .ne. ndim) stop "var2nc_1d_i4: number of variable dimensions /= number of file variable dimensions."
! check unlimited dimension
call check(nf90_inquire( f_handle, unlimitedDimId = u_dimid ))
if ( u_dimid .eq. -1 ) stop 'var2nc_1d_i4: cannot append, no unlimited dimension defined'
! check for unlimited dimension
if ( dimid( d_unlimit ) .ne. u_dimid ) stop 'var2nc_1d_i4: unlimited dimension not specified correctly'
if (present(nrec)) then
start(d_unlimit) = nrec
else
! get length of unlimited dimension
call check(nf90_inquire_dimension( f_handle, u_dimid, len = u_len ) )
! adapt start, that is find last written chunk
do i = u_len, 1, -1
if ( dummy(1) /= nf90_fill_int ) exit
start(d_unlimit) = i
call check( nf90_get_var( f_handle, varid(ndim+1), dummy, start, dummy_count ) )
end do
start(d_unlimit) = start(d_unlimit) + 1
endif
else
! reopen definition
if (.not. create_loc) call check(nf90_redef(f_handle))
! define dimension
do i = 1, ndim
! check whether dimension exists
if ( NF90_NOERR .ne. nf90_inq_dimid( f_handle, dnames(i), dimid(i)) ) then
! create dimension
if ( i .eq. d_unlimit ) then
! define unlimited dimension
call check(nf90_def_dim(f_handle, trim(dnames(i)), NF90_UNLIMITED, dimid(i) ))
else
! define limited dimension
call check(nf90_def_dim(f_handle, trim(dnames(i)), dims(i), dimid(i) ))
end if
end if
end do
! define variable
call check(nf90_def_var(f_handle, v_name, NF90_INT, dimid, varid(ndim+1) &
#ifndef __NETCDF3__
, chunksizes=chunksizes(:), shuffle=.true., deflate_level=deflate))
#else
))
#endif
!
if ( present( long_name ) ) call check(nf90_put_att(f_handle, varid(ndim+1), 'long_name', long_name ) )
if ( present( units ) ) call check(nf90_put_att(f_handle, varid(ndim+1), 'units', units ) )
if ( present( missing_value ) ) call check(nf90_put_att(f_handle, varid(ndim+1), 'missing_value', missing_value ) )
if ( present( attributes ) ) then
do i = 1, size( attributes, dim = 1 )
if ( trim( attributes(i,1)) .eq. 'missing_value' ) then
! write number
read(attributes(i,2),'(I6)') dummy(1)
call check( nf90_put_att( f_handle, varid(ndim+1), &
trim(attributes(i,1)), dummy(1) ) )
else
! write string
call check( nf90_put_att( f_handle, varid(ndim+1), &
trim(attributes(i,1)), trim(attributes(i,2)) ) )
end if
end do
end if
! end definition
call check(nf90_enddef(f_handle))
end if
! inquire dimensions
do i=1, ndim_const
call check(nf90_inquire_dimension(f_handle, dimid(i), dummy_name, dims(i)))
if (trim(dummy_name) .ne. dnames(i)) &
stop "var2nc_1d_i4: dimension name problem."
if ( (dims(i) .ne. size(arr,i)) .and. ( d_unlimit .ne. i ) ) &
stop "var2nc_1d_i4: variable dimension /= file variable dimension."
enddo
! write time and variable
call check(nf90_put_var(f_handle, varid(ndim+1), arr, start, counter ) )
! close netcdf_dataset
if (present(ncid)) then
if (ncid < 0_i4) ncid = f_handle
else
call close_netcdf( f_handle )
endif
!
end subroutine var2nc_1d_i4
subroutine var2nc_1d_sp( f_name, arr, dnames, v_name, dim_unlimited, &
long_name, units, missing_value, attributes, create, ncid, nrec )
!
implicit none
!
integer(i4), parameter :: ndim_const = 1
integer(i4) :: ndim
! input variables
character(len=*), intent(in) :: f_name
real(sp), dimension(:), intent(in) :: arr
character(len=*), dimension(:), intent(in) :: dnames
character(len=*), intent(in) :: v_name
! attributes
integer(i4), optional, intent(in) :: dim_unlimited
character(len=*), optional, intent(in) :: long_name
character(len=*), optional, intent(in) :: units
character(256), dimension(:,:), optional, intent(in) :: attributes
real(sp), optional, intent(in) :: missing_value
logical, optional, intent(in) :: create
integer(i4), optional, intent(inout) :: ncid
integer(i4), optional, intent(in) :: nrec
! local variables
logical :: create_loc
character(256) :: dummy_name
integer(i4) :: deflate
integer(i4), dimension(:), allocatable :: chunksizes
integer(i4), dimension(:), allocatable :: start ! start array for write
integer(i4), dimension(:), allocatable :: counter ! length array for write
integer(i4) :: idim ! read dimension on append
integer(i4) :: d_unlimit ! index of unlimited dimension
integer(i4) :: u_dimid
integer(i4) :: u_len ! length of unlimited dimension
integer(i4) :: f_handle
integer(i4), dimension(:), allocatable :: dims
integer(i4), dimension(:), allocatable :: dimid ! netcdf IDs of each dimension
integer(i4), dimension(:), allocatable :: varid ! dimension variables and var id
integer(i4) :: i ! loop indices
integer(i4), dimension(:), allocatable :: dummy_count
real(sp), dimension(1) :: dummy ! dummy read
logical :: openfile ! tmp logical
!
ndim = size(dnames, 1)
! consistency checks
d_unlimit = 0_i4
if ( present( dim_unlimited ) ) d_unlimit = dim_unlimited
if ( ( size( dnames, 1) .eq. ndim_const + 1 ) .and. ( d_unlimit .ne. ndim_const+1 ) ) then
print *, '***ERROR one more dimension name specified than dimension of array, but the last one is not unlimited'
stop '***ERROR see StdOut'
end if
if ( size( dnames, 1) .gt. ndim_const + 1 ) then
print *, '***ERROR too many dimension name specified, should be atmost ndim_const + 1'
stop '***ERROR see StdOut'
end if
allocate( chunksizes( ndim ) )
allocate( start( ndim ) )
allocate( counter( ndim ) )
allocate( dims( ndim ) )
allocate( dimid( ndim ) )
allocate( varid( 1 + ndim ) )
allocate( dummy_count( ndim ) )
! initialize
deflate = 1
if ( ndim .gt. ndim_const ) then
chunksizes = (/ size( arr, 1), 1 /)
dims(1:ndim-1) = shape( arr )
dims(ndim) = 1
else
chunksizes = (/ size( arr, 1) /)
dims(1:ndim_const) = shape( arr )
end if
start(:) = 1
counter(:) = dims
dummy_count = 1
dummy = nf90_fill_float
! open the netcdf file
if (present(ncid)) then
if (ncid < 0_i4) then
openfile = .true.
else
openfile = .false.
f_handle = ncid
endif
else
openfile = .true.
endif
if (openfile) then
create_loc = .false.
if (present(create)) create_loc = create
f_handle = open_netcdf(f_name, create=create_loc)
else
create_loc = .false.
endif
! check whether variable exists
if ( NF90_NOERR .eq. nf90_inq_varid( f_handle, v_name, varid(ndim+1)) ) then
! append
call check(nf90_inquire_variable(f_handle, varid(ndim+1), ndims=idim, dimids=dimid))
if (idim .ne. ndim) stop "var2nc_1d_sp: number of variable dimensions /= number of file variable dimensions."
! check unlimited dimension
call check(nf90_inquire( f_handle, unlimitedDimId = u_dimid ))
if ( u_dimid .eq. -1 ) stop 'var2nc_1d_sp: cannot append, no unlimited dimension defined'
! check for unlimited dimension
if ( dimid( d_unlimit ) .ne. u_dimid ) stop 'var2nc_1d_sp: unlimited dimension not specified correctly'
if (present(nrec)) then
start(d_unlimit) = nrec
else
! get length of unlimited dimension
call check(nf90_inquire_dimension( f_handle, u_dimid, len = u_len ) )
! adapt start, that is find last written chunk
do i = u_len, 1, -1
if ( ne(dummy(1),nf90_fill_float) ) exit
start(d_unlimit) = i
call check( nf90_get_var( f_handle, varid(ndim+1), dummy, start, dummy_count ) )
end do
start(d_unlimit) = start(d_unlimit) + 1
endif
else
! reopen definition
if (.not. create_loc) call check(nf90_redef(f_handle))
! define dimension
do i = 1, ndim
! check whether dimension exists
if ( NF90_NOERR .ne. nf90_inq_dimid( f_handle, dnames(i), dimid(i)) ) then
! create dimension
if ( i .eq. d_unlimit ) then
! define unlimited dimension
call check(nf90_def_dim(f_handle, trim(dnames(i)), NF90_UNLIMITED, dimid(i) ))
else
! define limited dimension
call check(nf90_def_dim(f_handle, trim(dnames(i)), dims(i), dimid(i) ))
end if
end if
end do
! define variable
call check(nf90_def_var(f_handle, v_name, NF90_FLOAT, dimid, varid(ndim+1) &
#ifndef __NETCDF3__
, chunksizes=chunksizes(:), shuffle=.true., deflate_level=deflate))
#else
))
#endif
if ( present( long_name ) ) call check(nf90_put_att(f_handle, varid(ndim+1), 'long_name', long_name ) )
if ( present( units ) ) call check(nf90_put_att(f_handle, varid(ndim+1), 'units', units ) )
if ( present( missing_value ) ) call check(nf90_put_att(f_handle, varid(ndim+1), 'missing_value', missing_value ) )
if ( present( attributes ) ) then
do i = 1, size( attributes, dim = 1 )
if ( trim( attributes(i,1)) .eq. 'missing_value' ) then
! write number
read(attributes(i,2),'(F10.2)') dummy(1)
call check( nf90_put_att( f_handle, varid(ndim+1), &
trim(attributes(i,1)), dummy(1) ) )
else
! write string
call check( nf90_put_att( f_handle, varid(ndim+1), &
trim(attributes(i,1)), trim(attributes(i,2)) ) )
end if
end do
end if
! end definition
call check(nf90_enddef(f_handle))
end if
! inquire dimensions
do i=1, ndim_const
call check(nf90_inquire_dimension(f_handle, dimid(i), dummy_name, dims(i)))
if (trim(dummy_name) .ne. dnames(i)) &
stop "var2nc_1d_sp: dimension name problem."
if ( (dims(i) .ne. size(arr,i)) .and. ( d_unlimit .ne. i ) ) &
stop "var2nc_1d_sp: variable dimension /= file variable dimension."
enddo
! write time and variable
call check(nf90_put_var(f_handle, varid(ndim+1), arr, start, counter ) )
! close netcdf_dataset
if (present(ncid)) then
if (ncid < 0_i4) ncid = f_handle
else
call close_netcdf( f_handle )
endif
!
end subroutine var2nc_1d_sp
subroutine var2nc_1d_dp( f_name, arr, dnames, v_name, dim_unlimited, &
long_name, units, missing_value, attributes, create, ncid, nrec )
!
implicit none
!
integer(i4), parameter :: ndim_const = 1
integer(i4) :: ndim
! input variables
character(len=*), intent(in) :: f_name
real(dp), dimension(:), intent(in) :: arr
character(len=*), intent(in) :: v_name
character(len=*), dimension(:), intent(in) :: dnames
! attributes
integer(i4), optional, intent(in) :: dim_unlimited
character(len=*), optional, intent(in) :: long_name
character(len=*), optional, intent(in) :: units
character(256), dimension(:,:), optional, intent(in) :: attributes
real(dp), optional, intent(in) :: missing_value
logical, optional, intent(in) :: create
integer(i4), optional, intent(inout) :: ncid
integer(i4), optional, intent(in) :: nrec
! local variables
logical :: create_loc
character(256) :: dummy_name
integer(i4) :: deflate
integer(i4), dimension(:), allocatable :: chunksizes
integer(i4), dimension(:), allocatable :: start ! start array for write
integer(i4), dimension(:), allocatable :: counter ! length array for write
integer(i4) :: idim ! read dimension on append
integer(i4) :: d_unlimit ! index of unlimited dimension
integer(i4) :: u_dimid
integer(i4) :: u_len ! length of unlimited dimension
integer(i4) :: f_handle
integer(i4), dimension(:), allocatable :: dims
integer(i4), dimension(:), allocatable :: dimid ! netcdf IDs of each dimension
integer(i4), dimension(:), allocatable :: varid ! dimension variables and var id
integer(i4) :: i ! loop indices
integer(i4), dimension(:), allocatable :: dummy_count
real(dp), dimension(1) :: dummy ! dummy read
logical :: openfile ! tmp logical
!
ndim = size(dnames, 1)
! consistency checks
d_unlimit = 0_i4
if ( present( dim_unlimited ) ) d_unlimit = dim_unlimited
if ( ( size( dnames, 1) .eq. ndim_const + 1 ) .and. ( d_unlimit .ne. ndim_const+1 ) ) then
print *, '***ERROR one more dimension name specified than dimension of array, but the last one is not unlimited'
stop '***ERROR see StdOut'
end if
if ( size( dnames, 1) .gt. ndim_const + 1 ) then
print *, '***ERROR too many dimension name specified, should be atmost ndim_const + 1'
stop '***ERROR see StdOut'
end if
allocate( chunksizes( ndim ) )
allocate( start( ndim ) )
allocate( counter( ndim ) )
allocate( dims( ndim ) )
allocate( dimid( ndim ) )
allocate( varid( 1 + ndim ) )
allocate( dummy_count( ndim ) )
! initialize
deflate = 1
if ( ndim .gt. ndim_const ) then
chunksizes = (/ size( arr, 1), 1 /)
dims(1:ndim-1) = shape( arr )
dims(ndim) = 1
else
chunksizes = (/ size( arr, 1) /)
dims(1:ndim_const) = shape( arr )
end if
start(:) = 1
counter(:) = dims
dummy_count = 1
dummy = nf90_fill_double
! open the netcdf file
if (present(ncid)) then
if (ncid < 0_i4) then
openfile = .true.
else
openfile = .false.
f_handle = ncid
endif
else
openfile = .true.
endif
if (openfile) then
create_loc = .false.
if (present(create)) create_loc = create
f_handle = open_netcdf(f_name, create=create_loc)
else
create_loc = .false.
endif
! check whether variable exists
if ( NF90_NOERR .eq. nf90_inq_varid( f_handle, v_name, varid(ndim+1)) ) then
! append
call check(nf90_inquire_variable(f_handle, varid(ndim+1), ndims=idim, dimids=dimid))
if (idim .ne. ndim) stop "var2nc_1d_dp: number of variable dimensions /= number of file variable dimensions."
! check unlimited dimension
call check(nf90_inquire( f_handle, unlimitedDimId = u_dimid ))
if ( u_dimid .eq. -1 ) stop 'var2nc_1d_dp: cannot append, no unlimited dimension defined'
! check for unlimited dimension
if ( dimid( d_unlimit ) .ne. u_dimid ) stop 'var2nc_1d_dp: unlimited dimension not specified correctly'
if (present(nrec)) then
start(d_unlimit) = nrec
else
! get length of unlimited dimension
call check(nf90_inquire_dimension( f_handle, u_dimid, len = u_len ) )
! adapt start, that is find last written chunk
do i = u_len, 1, -1
if ( ne(dummy(1),nf90_fill_double) ) exit
start(d_unlimit) = i
call check( nf90_get_var( f_handle, varid(ndim+1), dummy, start, dummy_count ) )
end do
start(d_unlimit) = start(d_unlimit) + 1
endif
else
! reopen definition
if (.not. create_loc) call check(nf90_redef(f_handle))
! define dimension
do i = 1, ndim
! check whether dimension exists
if ( NF90_NOERR .ne. nf90_inq_dimid( f_handle, dnames(i), dimid(i)) ) then
! create dimension
if ( i .eq. d_unlimit ) then
! define unlimited dimension
call check(nf90_def_dim(f_handle, trim(dnames(i)), NF90_UNLIMITED, dimid(i) ))
else
! define limited dimension
call check(nf90_def_dim(f_handle, trim(dnames(i)), dims(i), dimid(i) ))
end if
end if
end do
! define variable
call check(nf90_def_var(f_handle, v_name, NF90_DOUBLE, dimid, varid(ndim+1) &
#ifndef __NETCDF3__
, chunksizes=chunksizes(:), shuffle=.true., deflate_level=deflate))
#else
))
#endif
if ( present( long_name ) ) call check(nf90_put_att(f_handle, varid(ndim+1), 'long_name', long_name ) )
if ( present( units ) ) call check(nf90_put_att(f_handle, varid(ndim+1), 'units', units ) )
if ( present( missing_value ) ) call check(nf90_put_att(f_handle, varid(ndim+1), 'missing_value', missing_value ) )
if ( present( attributes ) ) then
do i = 1, size( attributes, dim = 1 )
if ( trim( attributes(i,1)) .eq. 'missing_value' ) then
! write number
read(attributes(i,2),'(F10.2)') dummy(1)
call check( nf90_put_att( f_handle, varid(ndim+1), &
trim(attributes(i,1)), dummy(1) ) )
else
! write string
call check( nf90_put_att( f_handle, varid(ndim+1), &
trim(attributes(i,1)), trim(attributes(i,2)) ) )
end if
end do
end if
! end definition
call check(nf90_enddef(f_handle))
end if
! inquire dimensions
do i=1, ndim_const
call check(nf90_inquire_dimension(f_handle, dimid(i), dummy_name, dims(i)))
if (trim(dummy_name) .ne. dnames(i)) &
stop "var2nc_1d_dp: dimension name problem."
if ( (dims(i) .ne. size(arr,i)) .and. ( d_unlimit .ne. i ) ) &
stop "var2nc_1d_dp: variable dimension /= file variable dimension."
enddo
! write time and variable
call check(nf90_put_var(f_handle, varid(ndim+1), arr, start, counter ) )
! close netcdf_dataset
if (present(ncid)) then
if (ncid < 0_i4) ncid = f_handle
else
call close_netcdf( f_handle )
endif
!
end subroutine var2nc_1d_dp
subroutine var2nc_2d_i4( f_name, arr, dnames, v_name, dim_unlimited, &
long_name, units, missing_value, attributes, create, ncid, nrec )
!
implicit none
!
integer(i4), parameter :: ndim_const = 2
integer(i4) :: ndim
! input variables
character(len=*), intent(in) :: f_name
integer(i4), dimension(:,:), intent(in) :: arr
character(len=*), intent(in) :: v_name
character(len=*), dimension(:), intent(in) :: dnames
! optional
integer(i4), optional, intent(in) :: dim_unlimited
character(len=*), optional, intent(in) :: long_name
character(len=*), optional, intent(in) :: units
integer(i4), optional, intent(in) :: missing_value
character(256), dimension(:,:), optional, intent(in) :: attributes
logical, optional, intent(in) :: create
integer(i4), optional, intent(inout) :: ncid
integer(i4), optional, intent(in) :: nrec
! local variables
logical :: create_loc
character(256) :: dummy_name
integer(i4) :: deflate
integer(i4), dimension(:), allocatable :: chunksizes
integer(i4), dimension(:), allocatable :: start ! start array for write
integer(i4), dimension(:), allocatable :: counter ! length array for write
integer(i4) :: idim ! read dimension on append
integer(i4) :: f_handle
integer(i4) :: d_unlimit ! index of unlimited dimension
integer(i4) :: u_dimid ! dimid of unlimited dimension
integer(i4) :: u_len ! length of unlimited dimension
integer(i4), dimension(:), allocatable :: dims
integer(i4), dimension(:), allocatable :: dimid ! netcdf IDs of each dimension
integer(i4), dimension(:), allocatable :: varid ! dimension variables and var id
integer(i4) :: i ! loop indices
integer(i4), dimension(:), allocatable :: dummy_count
integer(i4), dimension(1) :: dummy ! dummy read
logical :: openfile ! tmp logical
!
ndim = size( dnames, 1 )
! consistency checks
d_unlimit = 0_i4
if ( present( dim_unlimited ) ) d_unlimit = dim_unlimited
if ( ( ndim .eq. ndim_const + 1 ) .and. ( d_unlimit .ne. ndim_const+1 ) ) then
print *, '***ERROR one more dimension name specified than dimension of array, but the last one is not unlimited'
stop '***ERROR see StdOut'
end if
if ( ndim .gt. ndim_const + 1 ) then
print *, '***ERROR too many dimension name specified, should be atmost ndim_const + 1'
stop '***ERROR see StdOut'
end if
if ( ( ( ndim .eq. ndim_const ) .and. ( d_unlimit .gt. ndim_const ) ) .or. &
( d_unlimit .lt. 0_i4 ) ) then
write(*,*) '***ERROR unlimited dimension out of bounds, must be positive but not greater than number of given dimensions'
write(*,*) '***Dims: ', ndim, ndim_const, d_unlimit, ndim_const, d_unlimit
stop '***ERROR see StdOut'
end if
!
allocate( chunksizes( ndim ) )
allocate( start( ndim ) )
allocate( counter( ndim ) )
allocate( dims( ndim ) )
allocate( dimid( ndim ) )
allocate( varid( 1 + ndim ) )
allocate( dummy_count( ndim ) )
! initialize
deflate = 1
if ( ndim .gt. ndim_const ) then
chunksizes = (/ size( arr, 1), size( arr, 2), 1 /)
dims(1:ndim-1) = shape( arr )
dims(ndim) = 1
else
chunksizes = (/ size( arr, 1), size( arr, 2) /)
if ( d_unlimit .gt. 0 ) chunksizes( d_unlimit ) = 1
dims(1:ndim_const) = shape( arr )
end if
start(:) = 1_i4
counter(:) = dims
dummy_count = 1_i4
dummy = nf90_fill_int
! open the netcdf file
if (present(ncid)) then
if (ncid < 0_i4) then
openfile = .true.
else
openfile = .false.
f_handle = ncid
endif
else
openfile = .true.
endif
if (openfile) then
create_loc = .false.
if (present(create)) create_loc = create
f_handle = open_netcdf(f_name, create=create_loc)
else
create_loc = .false.
endif
! check whether variable exists
if ( NF90_NOERR .eq. nf90_inq_varid( f_handle, v_name, varid(ndim+1)) ) then
! append
call check(nf90_inquire_variable(f_handle, varid(ndim+1), ndims=idim, dimids=dimid))
! consistency checks
if (idim .ne. ndim) stop "var2nc_2d_i4: number of variable dimensions /= number of file variable dimensions."
! check unlimited dimension
call check(nf90_inquire( f_handle, unlimitedDimId = u_dimid ))
if ( u_dimid .eq. -1 ) stop 'var2nc_2d_i4: cannot append, no unlimited dimension defined'
! check for unlimited dimension
if ( dimid( d_unlimit ) .ne. u_dimid ) stop 'var2nc_2d_i4: unlimited dimension not specified correctly'
if (present(nrec)) then
start(d_unlimit) = nrec
else
! get length of unlimited dimension
call check(nf90_inquire_dimension( f_handle, u_dimid, len = u_len ) )
! adapt start, that is find last written chunk
do i = u_len, 1, -1
if ( dummy(1) /= nf90_fill_int ) exit
start(d_unlimit) = i
call check( nf90_get_var( f_handle, varid(ndim+1), dummy, start, dummy_count ) )
end do
start(d_unlimit) = start(d_unlimit) + 1
endif
else
! reopen definition
if (.not. create_loc) call check(nf90_redef(f_handle))
! define dimensions
do i = 1, ndim
! check whether dimension exists
if ( NF90_NOERR .ne. nf90_inq_dimid( f_handle, dnames(i), dimid(i)) ) then
! create dimension
if ( i .eq. d_unlimit ) then
! define unlimited dimension
call check(nf90_def_dim(f_handle, trim(dnames(i)), NF90_UNLIMITED, dimid(i) ))
else
! define limited dimension
call check(nf90_def_dim(f_handle, trim(dnames(i)), dims(i), dimid(i) ))
end if
end if
end do
! define variable
call check(nf90_def_var(f_handle, v_name, NF90_INT, dimid, varid(ndim+1) &
#ifndef __NETCDF3__
, chunksizes=chunksizes(:), shuffle=.true., deflate_level=deflate))
#else
))
#endif
! add attributes
if ( present( long_name ) ) call check(nf90_put_att(f_handle, varid(ndim+1), 'long_name', long_name ) )
if ( present( units ) ) call check(nf90_put_att(f_handle, varid(ndim+1), 'units', units ) )
if ( present( missing_value ) ) call check(nf90_put_att(f_handle, varid(ndim+1), 'missing_value', missing_value ) )
if ( present( attributes ) ) then
do i = 1, size( attributes, dim = 1 )
if ( trim( attributes(i,1)) .eq. 'missing_value' ) then
! write number
read(attributes(i,2),'(I6)') dummy(1)
call check( nf90_put_att( f_handle, varid(ndim+1), &
trim(attributes(i,1)), dummy(1) ) )
else
! write string
call check( nf90_put_att( f_handle, varid(ndim+1), &
trim(attributes(i,1)), trim(attributes(i,2)) ) )
end if
end do
end if
! end definition
call check(nf90_enddef(f_handle))
end if
! check dimensions before writing
do i=1, ndim_const
call check(nf90_inquire_dimension(f_handle, dimid(i), dummy_name, dims(i)))
if (trim(dummy_name) .ne. dnames(i)) &
stop "var2nc_2d_i4: dimension name problem."
if ( (dims(i) .ne. size(arr,i)) .and. ( d_unlimit .ne. i ) ) &
stop "var2nc_2d_i4: variable dimension /= file variable dimension."
end do
! write variable
call check( nf90_put_var(f_handle, varid(ndim+1), arr, start, counter) )
! close netcdf_dataset
if (present(ncid)) then
if (ncid < 0_i4) ncid = f_handle
else
call close_netcdf( f_handle )
endif
!
end subroutine var2nc_2d_i4
subroutine var2nc_2d_sp( f_name, arr, dnames, v_name, dim_unlimited, &
long_name, units, missing_value, attributes, create, ncid, nrec )
!
implicit none
!
integer(i4), parameter :: ndim_const = 2
integer(i4) :: ndim
! input variables
character(len=*), intent(in) :: f_name
real(sp), dimension(:,:), intent(in) :: arr
character(len=*), intent(in) :: v_name
character(len=*), dimension(:), intent(in) :: dnames
! optional
integer(i4), optional, intent(in) :: dim_unlimited
character(len=*), optional, intent(in) :: long_name
character(len=*), optional, intent(in) :: units
real(sp), optional, intent(in) :: missing_value
character(256), dimension(:,:), optional, intent(in) :: attributes
logical, optional, intent(in) :: create
integer(i4), optional, intent(inout) :: ncid
integer(i4), optional, intent(in) :: nrec
! local variables
logical :: create_loc
character(256) :: dummy_name
integer(i4) :: deflate
integer(i4), dimension(:), allocatable :: chunksizes
integer(i4), dimension(:), allocatable :: start ! start array for write
integer(i4), dimension(:), allocatable :: counter ! length array for write
integer(i4) :: idim ! read dimension on append
integer(i4) :: f_handle
integer(i4) :: d_unlimit ! index of unlimited dimension
integer(i4) :: u_dimid ! dimid of unlimited dimension
integer(i4) :: u_len ! length of unlimited dimension
integer(i4), dimension(:), allocatable :: dims
integer(i4), dimension(:), allocatable :: dimid ! netcdf IDs of each dimension
integer(i4), dimension(:), allocatable :: varid ! dimension variables and var id
integer(i4) :: i ! loop indices
integer(i4), dimension(:), allocatable :: dummy_count
real(sp), dimension(1) :: dummy ! dummy read
logical :: openfile ! tmp logical
!
ndim = size( dnames, 1 )
! consistency checks
d_unlimit = 0_i4
if ( present( dim_unlimited ) ) d_unlimit = dim_unlimited
if ( ( ndim .eq. ndim_const + 1 ) .and. ( d_unlimit .ne. ndim_const+1 ) ) then
print *, '***ERROR one more dimension name specified than dimension of array, but the last one is not unlimited'
stop '***ERROR see StdOut'
end if
if ( ndim .gt. ndim_const + 1 ) then
print *, '***ERROR too many dimension name specified, should be atmost ndim_const + 1'
stop '***ERROR see StdOut'
end if
if ( ( ( ndim .eq. ndim_const ) .and. ( d_unlimit .gt. ndim_const ) ) .or. &
( d_unlimit .lt. 0_i4 ) ) then
write(*,*) '***ERROR unlimited dimension out of bounds, must be positive but not greater than number of given dimensions'
write(*,*) '***Dims: ', ndim, ndim_const, d_unlimit, ndim_const, d_unlimit
stop '***ERROR see StdOut'
end if
!
allocate( chunksizes( ndim ) )
allocate( start( ndim ) )
allocate( counter( ndim ) )
allocate( dims( ndim ) )
allocate( dimid( ndim ) )
allocate( varid( 1 + ndim ) )
allocate( dummy_count( ndim ) )
! initialize
deflate = 1
if ( ndim .gt. ndim_const ) then