-
Notifications
You must be signed in to change notification settings - Fork 2
/
nc_test_prog.f90
108 lines (95 loc) · 3.96 KB
/
nc_test_prog.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
! 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/>.
program tester
use netcdf_file, only: ncdf_file_type
implicit none
type(ncdf_file_type) :: My_Output
real,dimension(:,:),allocatable :: phi
complex,dimension(:,:),allocatable :: cphi
real,dimension(:),allocatable :: xx,yy,t
integer :: nx=9,ny=26, ix,iy,i,nl=15
character(len=200),dimension(2) :: mesg
logical,dimension(:),allocatable :: logi
!Now initialise the file
call My_Output%init(ndim=2,nvar=5)
call My_Output%set_name(name="test.nc")
!Now define the dimensions
call My_Output%create_dim(name='x',size=nx) !Note the dimension name can be longer than 1 char
call My_Output%create_dim(name='y',size=ny) !but makes it tricky to use array constructors as
call My_Output%create_dim(name='c',size=200)!it's not possible to use strings of different length
call My_Output%create_dim(name='j ',size=2) !i.e. (/'c200','j'/) is not valid, though (/'c200','j '/)
call My_Output%create_dim(name='l',size=nl) !should be and the library should ignore the blanks.
call My_Output%create_dim(name='t',unlimited=.true.)
!Now define the variables
call My_Output%create_var(name='phi',dims=(/'x','y'/),type_str='real')
call My_Output%create_var(name='cphi',dims=(/'x','y'/),type_str='complex')
call My_Output%create_var(name='phit',dims=(/'t'/),type_str='real')
call My_Output%create_var(name='phit_x',dims=(/'x','t'/),type_str='real')
call My_Output%create_var(name='x',dims=(/'x'/),type_str='real')
call My_Output%create_var(name='y',dims=(/'y'/),type_str='real')
call My_Output%create_var(name='logi',dims=(/'l'/),type_str='logical')
call My_Output%create_var(name='nproc',type_str='int')
call My_Output%create_var(name='mesg',dims=(/'c','j'/),type_str='char')
call My_Output%create_var(name='mesg_short',dims=(/'c'/),type_str='char')
!Create and define file
call My_Output%create_and_define
!Exit def mode
call My_Output%enddef
!Make data
allocate(xx(nx),yy(ny),phi(nx,ny),logi(nl))
do ix=1,nx
xx(ix)=0.1*ix
enddo
do iy=1,ny
yy(iy)=0.5*iy
enddo
do iy=1,nl
logi(iy)=(1.eq.mod(iy,2))
enddo
do iy=1,ny
do ix=1,nx
phi(ix,iy)=sqrt(xx(ix)*yy(iy))
enddo
enddo
cphi=cmplx(0,1.0)*phi
!Now write the fixed data
call My_Output%write_var('phi',phi)
call My_Output%write_var('cphi',cphi)
call My_Output%write_var('x',xx)
call My_Output%write_var('logi',logi)
call My_Output%write_var('y',yy)
call My_Output%write_var('nproc',26)
mesg(1)="Hello this is a message!"
mesg(2)="Line 2"
call My_Output%write_var('mesg',mesg)
call My_Output%write_var('mesg_short',"Testing 2")
!Demonstrate incremental writing of unlimited dimension
do i=1,nx
call My_Output%write_var('phit',i*1.001)
call My_Output%write_var('phit_x',xx+i*1.0)
call My_Output%increment_unlim !This advances the unlimited dimension
if(mod(i,10).eq.0) call My_Output%flush_file() !Flush the output
enddo
!Finish up
call My_Output%close_file
call My_Output%free
!Now open the file up again, read it in and save it as a different file (without data)
call My_Output%open_file(rw=.false.)
call My_Output%populate_from_file
call My_Output%close_file
call My_Output%set_name("copy.nc")
call My_Output%create_and_define
call My_Output%enddef
call My_Output%write_var('y',yy)
call My_Output%close_file
call My_Output%free
end program tester