-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrads_module.f90
233 lines (180 loc) · 5.26 KB
/
grads_module.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
module grads_module
use kind_module, only : i4b, sp
use string_module, only : string_tolower
implicit none
private
type, public :: grads_ctl
character(len=256) :: dset, title
real(kind=sp) :: undef
integer(kind=i4b) :: xdef, ydef, zdef, tdef
integer(kind=i4b), dimension(6) :: t
character(len=2) :: tunit
real(kind=sp), dimension(:), allocatable :: xlevs, ylevs, zlevs
integer(kind=i4b) :: vars
integer(kind=i4b), dimension(:), allocatable :: levs
character(len=15), dimension(:), allocatable :: varnames
end type grads_ctl
public :: grads_parse, grads_clean
private :: read_dset, read_title, read_undef, read_options, &
read_axis, read_tdef, read_vars, read_var
contains
function grads_parse(u, ctl) result(f)
implicit none
character(len=*), intent(in) :: ctl
integer(kind=i4b), intent(in) :: u
type(grads_ctl) :: f
integer(kind=i4b) :: i, n
character(len=16) :: a
open(unit=u, file=ctl, status="old", action="read")
do
read(unit=u, fmt=*, end=100) a
a = string_tolower(adjustl(a))
select case(trim(a))
case("dset")
f % dset = read_dset(u)
case("title")
f % title = read_title(u)
case("undef")
f % undef = read_undef(u)
case("xdef")
f % xdef = read_axis(u, f % xlevs)
case("ydef")
f % ydef = read_axis(u, f % ylevs)
case("zdef")
f % zdef = read_axis(u, f % zlevs)
case("vars")
n = read_vars(u)
f % vars = n
allocate(f % levs(n), f % varnames(n))
do i=1, n
f % levs(i) = read_var(u, f % varnames(i))
end do
case default
end select
end do
100 continue
close(unit=u)
end function grads_parse
subroutine grads_clean(f)
implicit none
type(grads_ctl), intent(inout) :: f
deallocate(f % xlevs, f % ylevs, f % zlevs, f % levs, f % varnames)
end subroutine grads_clean
function read_dset(u) result(dset)
implicit none
integer(kind=i4b), intent(in) :: u
character(len=256) :: dset
character(len=4) :: a
backspace(unit=u)
read(unit=u, fmt=*) a, dset
if (dset(1:1)=="^") then
dset(1:1) = " "
end if
dset = adjustl(dset)
end function read_dset
function read_title(u) result(title)
implicit none
integer(kind=i4b), intent(in) :: u
character(len=256) :: title
character(len=5) :: a
backspace(unit=u)
read(unit=u, fmt=*) a, title
title = adjustl(title)
end function read_title
function read_undef(u) result(undef)
implicit none
integer(kind=i4b), intent(in) :: u
real(kind=sp) :: undef
character(len=5) :: a
backspace(unit=u)
read(unit=u, fmt=*) a, undef
end function read_undef
function read_options(u,n) result(s)
implicit none
integer(kind=i4b), intent(in) :: u, n
character(len=16), dimension(:), allocatable :: s
character(len=6) :: a
backspace(unit=u)
allocate(s(n))
read(unit=u, fmt=*) a, s
end function read_options
function read_axis(u,axis) result(n)
implicit none
integer(kind=i4b), intent(in) :: u
real(kind=sp), dimension(:), allocatable, intent(out) :: axis
integer(kind=i4b) :: n
character(len=4) :: a
character(len=6) :: b
integer(kind=i4b) :: i
real(kind=sp) :: x0, dx
backspace(unit=u)
read(unit=u, fmt=*) a, n, b
b=string_tolower(b)
allocate(axis(n))
backspace(unit=u)
if (b=="linear") then
read(unit=u, fmt=*) a, n, b, x0, dx
do i=1, n
axis(i) = x0 + dx*(i-1)
end do
else
read(unit=u, fmt=*) a, n, b, axis
end if
end function read_axis
function read_tdef(u,t,tu) result(n)
implicit none
integer(kind=i4b), intent(in) :: u
integer(kind=i4b), dimension(6), intent(out) :: t
character(len=2), intent(out) :: tu
integer(kind=i4b) :: n
character(len=*), parameter :: &
monthname = "jan feb mar apr may jun jul aug sep oct nov dec", &
tunits = "mnhrdymoyr"
character(len=4) :: a
character(len=6) :: b
character(len=15) :: c
character(len=4) :: d
integer(kind=i4b) :: ic, iz, im, ik
backspace(unit=u)
read(unit=u, fmt=*) a, n, b, c, d
c = adjustl(string_tolower(c))
ic = index(c,":")
iz = index(c,"z")
im = iz+scan(c(iz+1:),monthname)
read(c(im+3:),fmt=*) t(1)
if (t(1)<100) then
if (t(1)>=50) then
t(1) = t(1) + 1900
else
t(1) = t(1) + 2000
end if
end if
t(2) = (index(monthname,c(im:im+2))+3)/4
read(c(iz+1:im-1),fmt=*) t(3)
if (ic/= 0) then
read(c(1:ic-1),fmt=*) t(4)
read(c(ic+1:iz-1),fmt=*) t(5)
else
t(4:5) = 0
end if
d = adjustl(d)
ik = scan(d,tunits)
read(d(1:ik-1),fmt=*) t(6)
tu = d(ik:ik+1)
end function read_tdef
function read_vars(u) result(n)
implicit none
integer(kind=i4b), intent(in) :: u
integer(kind=i4b) :: n
character(len=4) :: a
backspace(unit=u)
read(unit=u, fmt=*) a, n
end function read_vars
function read_var(u,s) result(n)
implicit none
integer(kind=i4b), intent(in) :: u
character(len=15), intent(out) :: s
integer(kind=i4b) :: n
read(unit=u, fmt=*) s, n
end function read_var
end module grads_module