-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinput_mod.F90
More file actions
executable file
·225 lines (195 loc) · 7.09 KB
/
input_mod.F90
File metadata and controls
executable file
·225 lines (195 loc) · 7.09 KB
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
!-----------------------------------------------------------------------
! SPBM 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 (https://www.gnu.org/licenses/gpl.html).
! It 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. A copy of the license is provided in
! the COPYING file at the root of the SPBM distribution.
!-----------------------------------------------------------------------
! Original author(s): Shamil Yakubov
!-----------------------------------------------------------------------
module input_mod
use list_mod
use types_mod
use netcdf
use fabm_driver
implicit none
private
public type_input
type:: netcdf_dimension
character(len=64):: name = ''
integer:: dim_id
integer:: dim_len
end type
type,extends(list_variables):: type_input
contains
private
procedure:: type_input_initialize
procedure:: add_input
end type
type,extends(list):: type_netcdf_dimension
contains
private
procedure:: add_netcdf_dimension
procedure:: get_netcdf_dimension
end type
interface type_input
module procedure type_input_constructor
end interface
contains
function type_input_constructor(infile)
character(len=*), intent(in):: infile
type(type_input):: type_input_constructor
call type_input_constructor%type_input_initialize(infile)
end function
subroutine type_input_initialize(self,infile)
class(type_input):: self
character(len=*),intent(in):: infile
type(netcdf_dimension):: alone_dim
type(type_netcdf_dimension):: list_dim
real(rk):: value
real(rk),dimension(:),allocatable:: value_1d
real(rk),dimension(:,:),allocatable:: value_2d
type(alone_variable):: alone_var
type(variable_1d):: var_1d
type(variable_2d):: var_2d
integer:: ncid !NetCDF ID
integer:: ndims !number of dimensions
integer:: nvars !number of variables
integer:: nglobalatts !number of global attributes
integer:: unlimdimid !ID of the unlimited dimension
integer:: i !Dimension ID, Variable ID
integer:: len_dim !Returned length of dimension
integer:: xtype !Returned variable type
integer:: varid
integer,dimension(2):: len_dim_2d !Returned length of dimensions
!dimids - Returned vector of *ndimsp dimension
!IDs corresponding to the variable dimensions
integer,dimension(NF90_MAX_VAR_DIMS):: dimids
character(len=NF90_MAX_NAME):: name !Returned dimension name
character(len=NF90_MAX_NAME):: vname !Returned variable name
character(len=NF90_MAX_NAME):: long_name
character(len=NF90_MAX_NAME):: units
call check(nf90_open(infile,nf90_nowrite,ncid))
call check(nf90_inquire(ncid,ndims,nvars,nglobalatts,unlimdimid))
!Dimension ID
do i=1,ndims
call check(nf90_inquire_dimension(ncid,i,name,len_dim))
alone_dim = netcdf_dimension(trim(name),i,len_dim)
call list_dim%add_netcdf_dimension(alone_dim)
end do
!Variable ID
do i=1,nvars
call check(nf90_inquire_variable(ncid,i,vname,xtype,ndims,dimids))
if (ndims==0) then
call check(nf90_get_var(ncid,i,value))
call get_att(ncid,i,"long_name",long_name)
call get_att(ncid,i,"units",units)
alone_var = alone_variable(vname,long_name,units,value)
call self%add_input(alone_var)
else if (ndims==1) then
len_dim_2d = list_dim%get_netcdf_dimension(dimids(1))
allocate(value_1d(len_dim_2d(1)))
call check(nf90_get_var(ncid,i,value_1d))
call get_att(ncid,i,"long_name",long_name)
call get_att(ncid,i,"units",units)
var_1d = variable_1d(vname,long_name,units,value_1d)
call self%add_input(var_1d)
deallocate(value_1d)
else if (ndims==2) then
len_dim_2d = list_dim%get_netcdf_dimension(dimids(1),dimids(2))
allocate(value_2d(len_dim_2d(1),len_dim_2d(2)))
call check(nf90_get_var(ncid,i,value_2d))
call get_att(ncid,i,"long_name",long_name)
call get_att(ncid,i,"units",units)
var_2d = variable_2d(vname,long_name,units,value_2d)
call self%add_input(var_2d)
deallocate(value_2d)
end if
end do
call list_dim%delete_list()
call check(nf90_close(ncid))
end subroutine
subroutine add_input(self, var)
class(type_input) :: self
class(variable) :: var
class(*),allocatable:: temp
allocate(temp,source=var)
call self%add_item(temp)
end subroutine
subroutine add_netcdf_dimension(self, var)
class(type_netcdf_dimension):: self
class(netcdf_dimension):: var
class(*),allocatable:: temp
allocate(temp,source=var)
call self%add_item(temp)
end subroutine
function get_netcdf_dimension(self, indim_id_1, indim_id_2)
class(type_netcdf_dimension):: self
integer,intent(in) :: indim_id_1
integer,optional,intent(in) :: indim_id_2
integer,dimension(2) :: get_netcdf_dimension
class(*),pointer :: curr
get_netcdf_dimension = -1
call self%reset()
do
curr => self%get_item()
select type(curr)
type is(netcdf_dimension)
if (curr%dim_id==indim_id_1) then
get_netcdf_dimension(1) = curr%dim_len
if (.not.present(indim_id_2)) return
end if
if (present(indim_id_2)) then
if (curr%dim_id==indim_id_2) then
get_netcdf_dimension(2) = curr%dim_len
end if
end if
end select
call self%next()
if (.not.self%moreitems()) exit
end do
if ((.not.present(indim_id_2).and.get_netcdf_dimension(1)==-1).or.&
(present(indim_id_2).and.get_netcdf_dimension(1)==-1 .or.&
get_netcdf_dimension(2)==-1)) then
call fatal_error("Getting NetCDF dimension lengths",&
"Can't get length of dimension")
end if
end function
subroutine check(status)
integer, intent(in):: status
if (status .ne. NF90_NOERR) then
call fatal_error("Netcdf internal",&
nf90_strerror(status))
end if
end subroutine
subroutine get_att(ncid,i,name_to_check,name)
integer,intent(in):: ncid
integer,intent(in):: i
character(len=*),intent(in) :: name_to_check
character(len=NF90_MAX_NAME),intent(out):: name
integer :: status
status = nf90_get_att(ncid,i,name_to_check,name)
if (status .ne. NF90_NOERR) then
name = ''
end if
end subroutine
subroutine get_ascii_array(infile,array)
character(len=*),intent(in):: infile
real(rk),dimension(:),intent(inout):: array
integer i,rdst
open(12,file=infile,status="old",action="read",position="rewind")
i = 1
do
! read line from input file
read(12, '(f5.1)', iostat=rdst) array(i)
! if end of file, exit loop
if (rdst >0) stop "ASCII read error"
if (rdst <0) exit
i = i + 1
end do
! close files
close(12)
end subroutine
end module