-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnetcdf_file.f90
1978 lines (1727 loc) · 60.9 KB
/
netcdf_file.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
! Copyright (C) 2013 David Dickinson
! This program is free software: you can redistribute it and/or modify
! it under the terms of the GNU General Public License as published by
! the Free Software Foundation, either version 3 of the License, or
! (at your option) any later version.
! This program is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! GNU General Public License for more details.
! You should have received a copy of the GNU General Public License
! along with this program. If not, see <http://www.gnu.org/licenses/>.
!TODO:
! Error checking
! Preprocessor directives
module netcdf_file
use netcdf_dim, only: ncdf_dim_type
use netcdf_var, only: ncdf_var_type
use netcdf, only: nf90_put_var
implicit none
public :: ncdf_file_type, file_max_name_len
private
integer, parameter :: file_max_name_len=200 !The maximum allowed filename length
integer :: ndim_expand=5 !How much we increase self%dims when required
integer :: nvar_expand=5 !How much we increase self%vars when required
!<DD> A module containing the netcdf file type definition
!and associated routines.
!</DD>
type :: ncdf_file_type
type(ncdf_dim_type), dimension(:), allocatable :: dims !The dimensions
type(ncdf_var_type), dimension(:), allocatable :: vars !The variables
integer :: id !The file id to use in calls to netcdf routines
character(len=file_max_name_len) :: name
!Meta data
integer :: unlim_dim=1
integer :: ndim, nvar
logical :: def_mode, opened=.false., writable=.false.
contains
private
procedure,public :: init !Basic initialisation
procedure,public :: free !Free up memory
procedure,public :: set_name !Set the filename
procedure,public :: create_and_define !Make the file and define variables/dims
procedure,public :: enddef !Exit definition mode
procedure,public :: close_file !Close the file
procedure,public :: create_dim !Creates and adds a dimension
procedure,public :: create_var !Creates and adds a variable
procedure,public :: add_dim !Attach a premade dimension
procedure,public :: add_var !Attach a premade variable
procedure :: write_comp_s,write_comp_1,write_comp_2,write_comp_3,write_comp_4,write_comp_5,write_comp_6,write_real_s,write_real_1,write_real_2,write_real_3,write_real_4,write_real_5,write_real_6,write_real_7,write_int_s,write_int_1,write_int_2,write_int_3,write_int_4,write_int_5,write_int_6,write_int_7,write_char_s,write_char_1,write_char_2,write_char_3,write_char_4,write_char_5,write_char_6,write_char_7,write_logi_s,write_logi_1,write_logi_2,write_logi_3,write_logi_4,write_logi_5,write_logi_6,write_logi_7
generic,public::write_var=>write_comp_s,write_comp_1,write_comp_2,write_comp_3,write_comp_4,write_comp_5,write_comp_6,write_real_s,write_real_1,write_real_2,write_real_3,write_real_4,write_real_5,write_real_6,write_real_7,write_int_s,write_int_1,write_int_2,write_int_3,write_int_4,write_int_5,write_int_6,write_int_7,write_char_s,write_char_1,write_char_2,write_char_3,write_char_4,write_char_5,write_char_6,write_char_7,write_logi_s,write_logi_1,write_logi_2,write_logi_3,write_logi_4,write_logi_5,write_logi_6,write_logi_7
procedure :: define_dim !Defines a passed dimension in the file
procedure :: define_var !Defines a passed variable in the file
procedure :: has_dim !Check if we have a named dim
procedure :: has_var !Check if we have a named var
procedure :: get_dim_id !Get the dimension id corresponding to a named dim
procedure :: get_dim_name !Get the dimension name corresponding to a dim id
procedure :: get_dim_index !Get the index corresponding to a named dimension
procedure :: get_var_index !Get the index corresponding to a named variable
procedure :: enable_complex !Creates the complex dimension
procedure,public :: flush_file !Flush the output
procedure,public :: increment_unlim !Increments the size of the unlimited dimension
procedure,public :: set_unlim !Sets the current index of the unlimited dimension
procedure :: create_file !Creates and opens the file
procedure,public :: open_file !Opens an existing file in read-only or read-write mode
procedure,public :: populate_from_file !Populates dimensions and variables from open file
end type ncdf_file_type
contains
!>Initialise the netcdf file object
subroutine init(self,ndim,nvar,name)
implicit none
class(ncdf_file_type), intent(inout) :: self
integer, intent(in), optional :: ndim,nvar
character(len=*),intent(in),optional:: name
!If we've passed a dim/var number then preallocate
!else we need either a linked list or temporaries.
if(present(ndim)) allocate(self%dims(ndim))
if(present(nvar)) allocate(self%vars(nvar))
if(present(name)) call self%set_name(name)
!Initialise variables
self%ndim=0
self%nvar=0
end subroutine init
!Free objects
subroutine free(self)
implicit none
class(ncdf_file_type), intent(inout) :: self
integer :: i
if(allocated(self%dims))then
do i=1,self%ndim
call self%dims(i)%free
enddo
deallocate(self%dims)
endif
if(allocated(self%vars))then
do i=1,self%nvar
call self%vars(i)%free
enddo
deallocate(self%vars)
endif
end subroutine free
!Make the netcdf file, define the dimensions and variables
subroutine create_and_define(self)
use netcdf, only: NF90_CLOBBER, nf90_create
implicit none
class(ncdf_file_type), intent(inout) :: self
integer :: status, i
!Open the file
call self%create_file
!Now define the dimensions
do i=1,self%ndim
status=self%define_dim(self%dims(i))
enddo
!Now define the variables
do i=1,self%nvar
status=self%define_var(self%vars(i))
enddo
end subroutine create_and_define
!Create and open the file (overwrites existing file)
subroutine create_file(self)
use netcdf, only: nf90_create,NF90_CLOBBER
implicit none
class(ncdf_file_type), intent(inout) :: self
integer :: status
if(self%opened)return
!Open the file
status=nf90_create(trim(self%name),NF90_CLOBBER,self%id)
!Set the def mod
self%def_mode=.true.
!Set opened state
self%opened=.true.
self%writable=.true.
end subroutine create_file
!Open the file (Read-only/Read-write)
subroutine open_file(self,rw)
use netcdf, only: nf90_open,NF90_NOWRITE,NF90_WRITE
implicit none
class(ncdf_file_type), intent(inout) :: self
logical, intent(in),optional :: rw
logical :: do_rw
integer :: status
if(self%opened)return
do_rw=.false.
if(present(rw))then
if(rw) do_rw=.true.
endif
if(rw)then
status=nf90_open(self%name,NF90_WRITE,self%id)
self%writable=.true.
else
status=nf90_open(self%name,NF90_NOWRITE,self%id)
self%writable=.false.
endif
self%def_mode=.false.
self%opened=.true.
end subroutine open_file
!Close the file
subroutine close_file(self)
use netcdf, only: nf90_close
implicit none
class(ncdf_file_type), intent(inout) :: self
integer :: status
if(.not.self%opened)return
status=nf90_close(self%id)
self%opened=.false.
end subroutine close_file
!Set the filename
subroutine set_name(self,name)
implicit none
class(ncdf_file_type), intent(inout) :: self
character(len=*), intent(in) :: name
self%name=trim(name)
self%opened=.false.
self%def_mode=.false.
end subroutine set_name
!Exit def mode
subroutine enddef(self)
use netcdf, only: nf90_enddef
implicit none
class(ncdf_file_type), intent(inout) :: self
integer :: status
if(.not.self%def_mode) return
status=nf90_enddef(self%id)
self%def_mode=.false.
end subroutine enddef
!Make the special dimension used for complex variables
subroutine enable_complex(self)
use netcdf_dim, only: dim_complex_name
implicit none
class(ncdf_file_type), intent(inout) :: self
if(self%has_dim(dim_complex_name)) return
call self%create_dim(name=dim_complex_name,size=2)
end subroutine enable_complex
subroutine increment_unlim(self,inc)
implicit none
class(ncdf_file_type), intent(inout) :: self
integer, intent(in), optional :: inc
if(present(inc))then
self%unlim_dim=self%unlim_dim+inc
else
self%unlim_dim=self%unlim_dim+1
endif
end subroutine increment_unlim
subroutine set_unlim(self,inc)
implicit none
class(ncdf_file_type), intent(inout) :: self
integer, intent(in) :: inc
self%unlim_dim=inc
end subroutine set_unlim
!Flush the output
subroutine flush_file(self)
use netcdf, only: nf90_sync
implicit none
class(ncdf_file_type), intent(in) :: self
integer :: status
status=nf90_sync(self%id)
end subroutine flush_file
!Populate from open file
subroutine populate_from_file(self)
use netcdf, only: nf90_inquire, nf90_inquire_dimension, nf90_inquire_variable
use netcdf_dim, only: dim_max_name_len, dim_complex_name
use netcdf_var, only: var_max_name_len
implicit none
class(ncdf_file_type), intent(inout) :: self
integer :: status, ndim_f,nvar_f,natt_f, unlim_id,form, idim, ivar
integer :: dlen, vtype,vndim,vnatts
logical :: complx
integer, dimension(:), allocatable :: vdim_ids
character(len=dim_max_name_len),dimension(:),allocatable :: vdim_names,dim_names
character(len=dim_max_name_len) :: dname
character(len=var_max_name_len) :: vname
!Make sure file is opened
if(.not.self%opened)then
call self%open_file
endif
!Make sure we're free
call self%free
!Now find out about the file
status=nf90_inquire(self%id,ndimensions=ndim_f,nvariables=nvar_f,&
nattributes=natt_f,unlimiteddimid=unlim_id,formatnum=form)
!Init the object
call self%init(ndim=ndim_f,nvar=nvar_f)
allocate(dim_names(ndim_f))
!Now we want to create all the dimensions
do idim=1,ndim_f
!Get dimension name and length
status=nf90_inquire_dimension(self%id,idim,dname,dlen)
dim_names(idim)=trim(dname)
!Now make dimension
if(idim.eq.unlim_id)then
call self%create_dim(dname,unlimited=.true.)
self%unlim_dim=dlen
else
call self%create_dim(dname,dlen)
endif
enddo
!Now we want to create all the variables
do ivar=1,nvar_f
!Get var name, type, ndims, natts
status=nf90_inquire_variable(self%id,ivar,name=vname,xtype=vtype,&
ndims=vndim,natts=vnatts)
!Create variable
if(vndim.gt.0)then
!Get dimension ids and names
allocate(vdim_ids(vndim),vdim_names(vndim))
status=nf90_inquire_variable(self%id,ivar,dimids=vdim_ids)
complx=.false.
!Now convert to names
do idim=1,vndim
vdim_names(idim)=trim(dim_names(vdim_ids(idim)))
if(trim(vdim_names(idim)).eq.trim(dim_complex_name)) complx=.true.
enddo
call self%create_var(vname,dims=vdim_names,type_int=vtype,complx=complx)
deallocate(vdim_ids,vdim_names)
else
call self%create_var(vname,type_int=vtype)
endif
enddo
deallocate(dim_names)
end subroutine populate_from_file
!/////////////////////
!// DIMENSION RELATED
!/////////////////////
!Define a dimension
function define_dim(self,dim)
use netcdf, only: nf90_def_dim
implicit none
class(ncdf_file_type), intent(inout) :: self
type(ncdf_dim_type), intent(inout) :: dim
integer :: define_dim
!Note dim%size could be NF90_UNLIMITED
define_dim=nf90_def_dim(self%id,trim(dim%name),dim%size,dim%id)
end function define_dim
!>Attach a dimension
subroutine add_dim(self,dim)
implicit none
class(ncdf_file_type), intent(inout) :: self
type(ncdf_dim_type), intent(in) :: dim
type(ncdf_dim_type), dimension(:), allocatable :: dim_tmp
integer :: sz
!First check if we already have this dim, if so exit
if(self%has_dim(dim%name)) return
!Now check if self%dim is allocated, if not allocate it
if(.not.allocated(self%dims)) allocate(self%dims(ndim_expand))
!Increment dimension count
self%ndim=self%ndim+1
!Get size of self%dims and see if we need to make it
!bigger, if we used a linked list we could avoid this
sz=size(self%dims)
!If we have more dims than available space we need to expand
if(self%ndim>sz)then
!Make temp storage
allocate(dim_tmp(self%ndim-1))
dim_tmp=self%dims
!Expand internal storage
deallocate(self%dims)
allocate(self%dims(self%ndim+ndim_expand))
!Replace data
self%dims(1:self%ndim-1)=dim_tmp
endif
!Store dim
self%dims(self%ndim)=dim
end subroutine add_dim
!>See if we have the current named dimension
function has_dim(self,name)
implicit none
class(ncdf_file_type), intent(in) :: self
character(len=*), intent(in) :: name
logical :: has_dim
integer :: i
has_dim=.false.
!loop until we find a match. This is probably not the
!most efficient way to do this
do i=1,self%ndim
if(trim(self%dims(i)%name).eq.trim(name)) then
has_dim=.true.
exit
endif
enddo
end function has_dim
!Find the dimension id corresponding to dimension name
function get_dim_id(self,name)
implicit none
class(ncdf_file_type), intent(in) :: self
character(len=*),intent(in) :: name
integer :: get_dim_id, i
get_dim_id=-100
do i=1,self%ndim
if(trim(self%dims(i)%name).eq.trim(name)) then
get_dim_id=self%dims(i)%id
exit
endif
enddo
if(get_dim_id.eq.-100) then
print*,"ERROR: No id found for dimension '",trim(name),"' -- Probably not added yet --> Have to halt."
stop
endif
end function get_dim_id
!Find the dimension name corresponding to dimension id
function get_dim_name(self,id)
use netcdf_dim, only: dim_max_name_len
implicit none
class(ncdf_file_type), intent(in) :: self
integer,intent(in) :: id
integer :: i
character(len=dim_max_name_len) :: get_dim_name
get_dim_name=''
do i=1,self%ndim
print*,self%dims(i)%name
if(self%dims(i)%id.eq.id) then
get_dim_name=self%dims(i)%name
exit
endif
enddo
if(get_dim_name.eq.'') then
print*,"ERROR: No name found for dimension id '",id,"' -- Probably not added yet --> Have to halt."
stop
endif
end function get_dim_name
!Make a dimension and attach it to the file object
subroutine create_dim(self,name,size,unlimited)
implicit none
class(ncdf_file_type), intent(inout) :: self
character(len=*), intent(in) :: name
integer,intent(in),optional :: size
logical,intent(in),optional :: unlimited
type(ncdf_dim_type) :: tmp
!Initialise dimension
!<DD>
!NOTE: Size and unlimited are optional, I think the following statement is ok
!in cases where these variables are provided AND where they are not but this may
!be something which needs testing/changing
!</DD>
call tmp%init(name=name,size=size,unlimited=unlimited)
!Now add it to the file
call self%add_dim(dim=tmp)
!Now free the tmp dim
call tmp%free
end subroutine create_dim
!Find the index of the dim corresponding to dimension name
function get_dim_index(self,name)
implicit none
class(ncdf_file_type), intent(in) :: self
character(len=*),intent(in) :: name
integer :: get_dim_index, i
get_dim_index=-100
do i=1,self%ndim
if(trim(self%dims(i)%name).eq.trim(name)) then
get_dim_index=i
exit
endif
enddo
if(get_dim_index.eq.-100) then
print*,"ERROR: No dimension found with name '",trim(name),"' --> Have to halt."
stop
endif
end function get_dim_index
!/////////////////////
!// VARIABLE RELATED
!/////////////////////
!Define a variable
function define_var(self,var)
use netcdf, only: nf90_def_var
implicit none
class(ncdf_file_type), intent(inout) :: self
type(ncdf_var_type), intent(inout) :: var
integer :: define_var,i
integer,dimension(:),allocatable :: dim_id
!Not all variables have dimensions so we need to check
!if this variable has dims or not.
if(var%ndim.gt.0)then
!Make array containing dim ids
allocate(dim_id(var%ndim))
do i=1,var%ndim
dim_id(i)=self%get_dim_id(var%dim_names(i))
enddo
define_var=nf90_def_var(self%id,var%name,var%type,dim_id,var%id)
deallocate(dim_id)
else
define_var=nf90_def_var(self%id,var%name,var%type,var%id)
endif
!Note we should loop over var attributes and attach them here
end function define_var
!>Attach a variable
subroutine add_var(self,var)
implicit none
class(ncdf_file_type), intent(inout) :: self
type(ncdf_var_type), intent(in) :: var
type(ncdf_var_type), dimension(:), allocatable :: var_tmp
integer :: sz
!First check if we already have this var, if so exit
if(self%has_var(var%name)) return
!Now check if self%dim is allocated, if not allocate it
if(.not.allocated(self%vars)) allocate(self%vars(nvar_expand))
!Increment dimension count
self%nvar=self%nvar+1
!Get size of self%dims and see if we need to make it
!bigger, if we used a linked list we could avoid this
sz=size(self%vars)
!If we have more dims than available space we need to expand
if(self%nvar>sz)then
!Make temp storage
allocate(var_tmp(self%nvar-1))
var_tmp=self%vars
!Expand internal storage
deallocate(self%vars)
allocate(self%vars(self%nvar+nvar_expand))
!Replace data
self%vars(1:self%nvar-1)=var_tmp
endif
!Store var
self%vars(self%nvar)=var
end subroutine add_var
!>See if we have the current named variable
function has_var(self,name)
implicit none
class(ncdf_file_type), intent(in) :: self
character(len=*), intent(in) :: name
logical :: has_var
integer :: i
has_var=.false.
!loop until we find a match. This is probably not the
!most efficient way to do this
do i=1,self%nvar
if(trim(self%vars(i)%name).eq.trim(name)) then
has_var=.true.
exit
endif
enddo
end function has_var
!Find the index of the var corresponding to variable name
function get_var_index(self,name)
implicit none
class(ncdf_file_type), intent(in) :: self
character(len=*),intent(in) :: name
integer :: get_var_index, i
get_var_index=-100
do i=1,self%nvar
if(trim(self%vars(i)%name).eq.trim(name)) then
get_var_index=i
exit
endif
enddo
if(get_var_index.eq.-100) then
print*,"ERROR: No variable found with name '",trim(name),"' --> Have to halt."
stop
endif
end function get_var_index
!Make a variable and attach it to the file object
subroutine create_var(self,name,dims,type_int,type_str,complx)
use netcdf_dim, only: dim_max_name_len
implicit none
class(ncdf_file_type), intent(inout) :: self
type(ncdf_var_type) :: tmp
character(len=*), intent(in) :: name
character(len=*),dimension(:),intent(in),optional :: dims
integer,intent(in),optional :: type_int
character(len=*),intent(in),optional :: type_str
logical, intent(in), optional :: complx
character(len=dim_max_name_len),dimension(:),allocatable::loc_dim
!Handle the case where we don't have dimensions
if(present(dims)) then
allocate(loc_dim(size(dims)))
loc_dim=dims
else
allocate(loc_dim(1))
loc_dim(1)=''
endif
!Initialise variable
!<DD>
!NOTE: Several args are optional, I think the following statement is ok
!in cases where these variables are provided AND where they are not but this may
!be something which needs testing/changing
!</DD>
call tmp%init(name=name,dims=loc_dim,type_int=type_int,&
type_str=type_str,complx=complx)
!Deallocate
deallocate(loc_dim)
!Now add it to the file
call self%add_var(var=tmp)
!Enable complex if variable is complex
if(tmp%is_complex) call self%enable_complex
!Now free the tmp var
call tmp%free
end subroutine create_var
!/////////////////////
!// WRITE VAR RELATED
!/////////////////////
!Tedious duplicatation of write routines for different types and ranks
!##########
!##COMPLEX
!##########
subroutine write_comp_s(self,name,dat)
implicit none
class(ncdf_file_type), intent(in) :: self
character(len=*), intent(in) :: name
complex, intent(in) :: dat
real,dimension(2) :: rdat
!Convert dat to rdat
rdat(1)=dble(dat) ; rdat(2)=aimag(dat)
!Pass data onto variable write routine
call self%write_var(name,rdat)
end subroutine write_comp_s
subroutine write_comp_1(self,name,dat)
implicit none
class(ncdf_file_type), intent(in) :: self
character(len=*), intent(in) :: name
complex,dimension(:), intent(in) :: dat
real,dimension(:,:),allocatable :: rdat
!Convert dat to rdat
allocate(rdat(2,size(dat,1)))
rdat(1,:)=dble(dat) ; rdat(2,:)=aimag(dat)
!Pass data onto variable write routine
call self%write_var(name,rdat)
deallocate(rdat)
end subroutine write_comp_1
subroutine write_comp_2(self,name,dat)
implicit none
class(ncdf_file_type), intent(in) :: self
character(len=*), intent(in) :: name
complex,dimension(:,:), intent(in) :: dat
real,dimension(:,:,:),allocatable :: rdat
!Convert dat to rdat
allocate(rdat(2,size(dat,1),size(dat,2)))
rdat(1,:,:)=dble(dat) ; rdat(2,:,:)=aimag(dat)
!Pass data onto variable write routine
call self%write_var(name,rdat)
deallocate(rdat)
end subroutine write_comp_2
subroutine write_comp_3(self,name,dat)
implicit none
class(ncdf_file_type), intent(in) :: self
character(len=*), intent(in) :: name
complex,dimension(:,:,:), intent(in) :: dat
real,dimension(:,:,:,:),allocatable :: rdat
!Convert dat to rdat
allocate(rdat(2,size(dat,1),size(dat,2),size(dat,3)))
rdat(1,:,:,:)=dble(dat) ; rdat(2,:,:,:)=aimag(dat)
!Pass data onto variable write routine
call self%write_var(name,rdat)
deallocate(rdat)
end subroutine write_comp_3
subroutine write_comp_4(self,name,dat)
implicit none
class(ncdf_file_type), intent(in) :: self
character(len=*), intent(in) :: name
complex,dimension(:,:,:,:), intent(in) :: dat
real,dimension(:,:,:,:,:),allocatable :: rdat
integer :: v_index, status
!Convert dat to rdat
allocate(rdat(2,size(dat,1),size(dat,2),size(dat,3),size(dat,4)))
rdat(1,:,:,:,:)=dble(dat) ; rdat(2,:,:,:,:)=aimag(dat)
!Pass data onto variable write routine
call self%write_var(name,rdat)
deallocate(rdat)
end subroutine write_comp_4
subroutine write_comp_5(self,name,dat)
implicit none
class(ncdf_file_type), intent(in) :: self
character(len=*), intent(in) :: name
complex,dimension(:,:,:,:,:), intent(in) :: dat
real,dimension(:,:,:,:,:,:),allocatable :: rdat
!Convert dat to rdat
allocate(rdat(2,size(dat,1),size(dat,2),size(dat,3),size(dat,4),size(dat,5)))
rdat(1,:,:,:,:,:)=dble(dat) ; rdat(2,:,:,:,:,:)=aimag(dat)
!Pass data onto variable write routine
call self%write_var(name,rdat)
deallocate(rdat)
end subroutine write_comp_5
subroutine write_comp_6(self,name,dat)
implicit none
class(ncdf_file_type), intent(in) :: self
character(len=*), intent(in) :: name
complex,dimension(:,:,:,:,:,:), intent(in) :: dat
real,dimension(:,:,:,:,:,:,:),allocatable :: rdat
!Convert dat to rdat
allocate(rdat(2,size(dat,1),size(dat,2),size(dat,3),size(dat,4),size(dat,5),size(dat,6)))
rdat(1,:,:,:,:,:,:)=dble(dat) ; rdat(2,:,:,:,:,:,:)=aimag(dat)
!Pass data onto variable write routine
call self%write_var(name,rdat)
deallocate(rdat)
end subroutine write_comp_6
!##########
!##REAL
!##########
subroutine write_real_s(self,name,dat)
implicit none
class(ncdf_file_type), intent(in) :: self
character(len=*), intent(in) :: name
real, intent(in) :: dat
integer :: v_index,status
integer :: dim_in=size(shape(dat)),nd
logical :: with_start
!Get variable index
v_index=self%get_var_index(name)
!Validate data (note this is only valid for scalars)
nd=self%vars(v_index)%ndim
!Work out if we want start or not
with_start=.false.
if(nd.ne.dim_in)then
if(self%dims(self%get_dim_id(self%vars(v_index)%dim_names(1)))%is_unlimited())then
with_start=.true.
endif
endif
!Put variable
if(with_start)then
status=nf90_put_var(self%id,self%vars(v_index)%id,(/dat/),start=(/self%unlim_dim/),count=(/1/))
else
status=nf90_put_var(self%id,self%vars(v_index)%id,dat)
endif
end subroutine write_real_s
subroutine write_real_1(self,name,dat)
implicit none
class(ncdf_file_type), intent(in) :: self
character(len=*), intent(in) :: name
real,dimension(:), intent(in) :: dat
real :: tdat
integer :: v_index,d_index,status,nd,i,ct
integer :: dim_in=size(shape(dat))
integer, dimension(:),allocatable :: starts,counts,shp
!Get variable index
v_index=self%get_var_index(name)
!Initialise starts and counts array
nd=self%vars(v_index)%ndim
allocate(starts(nd),counts(nd),shp(dim_in))
starts=1
shp=shape(dat)
ct=0
!Set the counts and handle the unlimited dimension
do i=1,nd
d_index=self%get_dim_index(self%vars(v_index)%dim_names(i))
!If this is an unlimited dimension then we need to change start and counts
if(self%dims(d_index)%is_unlimited())then
counts(i)=1
starts(i)=self%unlim_dim
else
ct=ct+1
counts(i)=shp(ct)
endif
enddo
!Put variable
status=nf90_put_var(self%id,self%vars(v_index)%id,dat,start=starts,count=counts)
!Tidy
deallocate(starts,counts)
end subroutine write_real_1
subroutine write_real_2(self,name,dat)
implicit none
class(ncdf_file_type), intent(in) :: self
character(len=*), intent(in) :: name
real,dimension(:,:), intent(in) :: dat
integer :: v_index,d_index,status,nd,i,ct
integer :: dim_in=size(shape(dat))
integer, dimension(:),allocatable :: starts,counts,shp
!Get variable index
v_index=self%get_var_index(name)
!Initialise starts and counts array
nd=self%vars(v_index)%ndim
allocate(starts(nd),counts(nd),shp(dim_in))
starts=1
shp=shape(dat)
ct=0
!Set the counts and handle the unlimited dimension
do i=1,nd
d_index=self%get_dim_index(self%vars(v_index)%dim_names(i))
!If this is an unlimited dimension then we need to change start and counts
if(self%dims(d_index)%is_unlimited())then
counts(i)=1
starts(i)=self%unlim_dim
else
ct=ct+1
counts(i)=shp(ct)
endif
enddo
!Put variable
status=nf90_put_var(self%id,self%vars(v_index)%id,dat,start=starts,count=counts)
!Tidy
deallocate(starts,counts)
end subroutine write_real_2
subroutine write_real_3(self,name,dat)
implicit none
class(ncdf_file_type), intent(in) :: self
character(len=*), intent(in) :: name
real,dimension(:,:,:), intent(in) :: dat
integer :: v_index,d_index,status,nd,i,ct
integer :: dim_in=size(shape(dat))
integer, dimension(:),allocatable :: starts,counts,shp
!Get variable index
v_index=self%get_var_index(name)
!Initialise starts and counts array
nd=self%vars(v_index)%ndim
allocate(starts(nd),counts(nd),shp(dim_in))
starts=1
shp=shape(dat)
ct=0
!Set the counts and handle the unlimited dimension
do i=1,nd
d_index=self%get_dim_index(self%vars(v_index)%dim_names(i))
!If this is an unlimited dimension then we need to change start and counts
if(self%dims(d_index)%is_unlimited())then
counts(i)=1
starts(i)=self%unlim_dim !<DD>TO FIX
else
ct=ct+1
counts(i)=shp(ct)
endif
enddo
!Put variable
status=nf90_put_var(self%id,self%vars(v_index)%id,dat,start=starts,count=counts)
!Tidy
deallocate(starts,counts)
end subroutine write_real_3
subroutine write_real_4(self,name,dat)
implicit none
class(ncdf_file_type), intent(in) :: self
character(len=*), intent(in) :: name
real,dimension(:,:,:,:), intent(in) :: dat
integer :: v_index,d_index,status,nd,i,ct
integer :: dim_in=size(shape(dat))
integer, dimension(:),allocatable :: starts,counts,shp
!Get variable index
v_index=self%get_var_index(name)
!Initialise starts and counts array
nd=self%vars(v_index)%ndim
allocate(starts(nd),counts(nd),shp(dim_in))
starts=1
shp=shape(dat)
ct=0
!Set the counts and handle the unlimited dimension
do i=1,nd
d_index=self%get_dim_index(self%vars(v_index)%dim_names(i))
!If this is an unlimited dimension then we need to change start and counts
if(self%dims(d_index)%is_unlimited())then
counts(i)=1
starts(i)=self%unlim_dim !<DD>TO FIX
else
ct=ct+1
counts(i)=shp(ct)
endif
enddo
!Put variable
status=nf90_put_var(self%id,self%vars(v_index)%id,dat,start=starts,count=counts)
!Tidy
deallocate(starts,counts)
end subroutine write_real_4
subroutine write_real_5(self,name,dat)
implicit none
class(ncdf_file_type), intent(in) :: self
character(len=*), intent(in) :: name
real,dimension(:,:,:,:,:), intent(in) :: dat
integer :: v_index,d_index,status,nd,i,ct
integer :: dim_in=size(shape(dat))
integer, dimension(:),allocatable :: starts,counts,shp
!Get variable index
v_index=self%get_var_index(name)
!Initialise starts and counts array
nd=self%vars(v_index)%ndim
allocate(starts(nd),counts(nd),shp(dim_in))
starts=1
shp=shape(dat)
ct=0
!Set the counts and handle the unlimited dimension
do i=1,nd
d_index=self%get_dim_index(self%vars(v_index)%dim_names(i))
!If this is an unlimited dimension then we need to change start and counts
if(self%dims(d_index)%is_unlimited())then
counts(i)=1
starts(i)=self%unlim_dim !<DD>TO FIX
else
ct=ct+1
counts(i)=shp(ct)
endif
enddo
!Put variable
status=nf90_put_var(self%id,self%vars(v_index)%id,dat,start=starts,count=counts)
!Tidy
deallocate(starts,counts)
end subroutine write_real_5
subroutine write_real_6(self,name,dat)
implicit none
class(ncdf_file_type), intent(in) :: self
character(len=*), intent(in) :: name
real,dimension(:,:,:,:,:,:), intent(in) :: dat
integer :: v_index,d_index,status,nd,i,ct
integer :: dim_in=size(shape(dat))
integer, dimension(:),allocatable :: starts,counts,shp
!Get variable index
v_index=self%get_var_index(name)
!Initialise starts and counts array
nd=self%vars(v_index)%ndim
allocate(starts(nd),counts(nd),shp(dim_in))
starts=1
shp=shape(dat)
ct=0
!Set the counts and handle the unlimited dimension
do i=1,nd
d_index=self%get_dim_index(self%vars(v_index)%dim_names(i))
!If this is an unlimited dimension then we need to change start and counts
if(self%dims(d_index)%is_unlimited())then
counts(i)=1
starts(i)=self%unlim_dim !<DD>TO FIX
else
ct=ct+1
counts(i)=shp(ct)
endif
enddo
!Put variable
status=nf90_put_var(self%id,self%vars(v_index)%id,dat,start=starts,count=counts)
!Tidy
deallocate(starts,counts)
end subroutine write_real_6
subroutine write_real_7(self,name,dat)
implicit none
class(ncdf_file_type), intent(in) :: self
character(len=*), intent(in) :: name
real,dimension(:,:,:,:,:,:,:), intent(in) :: dat
integer :: v_index,d_index,status,nd,i,ct
integer :: dim_in=size(shape(dat))
integer, dimension(:),allocatable :: starts,counts,shp
!Get variable index