forked from ARVE-Research/makesoil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitsoil.f90
246 lines (160 loc) · 6.52 KB
/
initsoil.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
program initsoil
! gfortran -Wall -o initsoil initsoil.f90 -I/usr/local/include -L/usr/local/lib -lnetcdff
use iso_fortran_env
use netcdf
implicit none
integer, parameter :: sp = real32
integer, parameter :: dp = real64
integer, parameter :: i2 = int16
integer, parameter :: i1 = int8
character(200) :: infile
integer :: status
integer :: ncid
integer :: dimid
integer :: varid
integer :: xlen
integer :: ylen
integer :: zlen
real(sp) :: scale_factor
integer(i2) :: imissing
integer :: x
integer :: y
integer :: z
integer(i1), allocatable, dimension(:,:) :: soilclass
integer(i2), allocatable, dimension(:,:,:) :: sand
integer(i2), allocatable, dimension(:,:,:) :: silt
integer(i2), allocatable, dimension(:,:,:) :: clay
integer(i2), allocatable, dimension(:,:,:) :: soc
integer(i2), allocatable, dimension(:,:,:) :: cfvo
real(sp), allocatable, dimension(:) :: dz
real(sp) :: orgm
real(sp), parameter :: ps = 2650. ! particle density of mineral soil (typical) (kg m-3)
real(sp) :: varmin
real(sp) :: varmax
real(sp), dimension(2) :: actual_range = [0., 0.]
! ------------------------------------------------------------------------------------------------------------
call getarg(1,infile)
! -------------------------------------------------------
! read input file
! write(0,*)'reading'
status = nf90_open(infile,nf90_write,ncid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_inq_dimid(ncid,'lon',dimid)
if (status == nf90_ebaddim) status = nf90_inq_dimid(ncid,'x',dimid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_inquire_dimension(ncid,dimid,len=xlen)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_inq_dimid(ncid,'lat',dimid)
if (status == nf90_ebaddim) status = nf90_inq_dimid(ncid,'y',dimid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_inquire_dimension(ncid,dimid,len=ylen)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_inq_dimid(ncid,'depth',dimid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_inquire_dimension(ncid,dimid,len=zlen)
if (status /= nf90_noerr) call handle_err(status)
allocate(dz(zlen))
allocate(soilclass(xlen,ylen))
allocate(sand(xlen,ylen,zlen))
allocate(silt(xlen,ylen,zlen))
allocate(clay(xlen,ylen,zlen))
allocate(soc(xlen,ylen,zlen))
allocate(cfvo(xlen,ylen,zlen))
status = nf90_inq_varid(ncid,'dz',varid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_get_var(ncid,varid,dz)
if (status /= nf90_noerr) call handle_err(status)
! -------------------------------------------------------
! convert orgm density to soc
status = nf90_inq_varid(ncid,'soc',varid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_get_var(ncid,varid,soc)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_get_att(ncid,varid,'_FillValue',imissing)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_get_att(ncid,varid,'scale_factor',scale_factor)
if (status /= nf90_noerr) call handle_err(status)
do z = 1,zlen
do y = 1,ylen
do x = 1,xlen
if (soc(x,y,z) == imissing) cycle
orgm = real(soc(x,y,z)) * scale_factor / ps
soc(x,y,z) = nint(orgm / 0.001,i2)
end do
end do
end do
scale_factor = 0.001
varmin = real(minval(soc,mask=soc/=imissing)) * scale_factor
varmax = real(maxval(soc,mask=soc/=imissing)) * scale_factor
actual_range = [varmin,varmax]
status = nf90_put_att(ncid,varid,'scale_factor',scale_factor)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_put_att(ncid,varid,'actual_range',actual_range)
if (status /= nf90_noerr) call handle_err(status)
write(0,*)'soc',actual_range
status = nf90_put_var(ncid,varid,soc)
if (status /= nf90_noerr) call handle_err(status)
! -------------------------------------------------------
! set a value for cfvo (uniform everywhere)
cfvo = imissing
where (soc /= imissing) cfvo = 0.
status = nf90_inq_varid(ncid,'cfvo',varid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_put_var(ncid,varid,cfvo)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_get_att(ncid,varid,'actual_range',actual_range)
if (status /= nf90_noerr) call handle_err(status)
varmin = real(minval(cfvo,mask=cfvo/=imissing)) * scale_factor
varmax = real(maxval(cfvo,mask=cfvo/=imissing)) * scale_factor
actual_range = [min(varmin,actual_range(1)),max(varmax,actual_range(2))]
status = nf90_put_att(ncid,varid,'actual_range',actual_range)
if (status /= nf90_noerr) call handle_err(status)
write(0,*)'cfrag',actual_range
! -------------------------------------------------------
! calculate and add silt
silt = imissing
status = nf90_inq_varid(ncid,'sand',varid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_get_var(ncid,varid,sand)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_inq_varid(ncid,'clay',varid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_get_var(ncid,varid,clay)
if (status /= nf90_noerr) call handle_err(status)
where (sand /= imissing) silt = 1000_i2 - (sand + clay)
status = nf90_inq_varid(ncid,'silt',varid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_get_att(ncid,varid,'scale_factor',scale_factor)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_put_var(ncid,varid,silt)
if (status /= nf90_noerr) call handle_err(status)
varmin = real(minval(silt,mask=silt/=imissing)) * scale_factor
varmax = real(maxval(silt,mask=silt/=imissing)) * scale_factor
actual_range = [min(varmin,actual_range(1)),max(varmax,actual_range(2))]
status = nf90_put_att(ncid,varid,'actual_range',actual_range)
if (status /= nf90_noerr) call handle_err(status)
write(0,*)'silt',actual_range
! -------------------------------------------------------
! write USDA codes
soilclass = -1_i1
where (soc(:,:,1) /= imissing) soilclass = 69 ! Typical Mollisols
status = nf90_inq_varid(ncid,'USDA',varid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_put_var(ncid,varid,soilclass)
if (status /= nf90_noerr) call handle_err(status)
! -------------------------------------------------------
status = nf90_close(ncid)
if (status /= nf90_noerr) call handle_err(status)
! -------------------------------------------------------
contains
subroutine handle_err(status)
! Internal subroutine - checks error status after each netcdf call,
! prints out text message each time an error code is returned.
integer, intent (in) :: status
if(status /= nf90_noerr) then
print *, trim(nf90_strerror(status))
stop
end if
end subroutine handle_err
! -------------------------------------------------------
end program initsoil