-
Notifications
You must be signed in to change notification settings - Fork 1
/
netcdfmod.f90
280 lines (188 loc) · 7.78 KB
/
netcdfmod.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
module netcdfmod
use netcdf
use parametersmod
implicit none
public :: handle_err
public :: genoutfile
contains
! -------------------------------------------------------
subroutine handle_err(status)
implicit none
! Internal subroutine - checks error ncstat 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
! -------------------------------------------------------
subroutine genoutfile(jobfile,outfile,xlen,ylen,ofid)
implicit none
character(*), intent(in) :: jobfile
character(*), intent(in) :: outfile
integer, intent(in) :: xlen
integer, intent(in) :: ylen
integer, intent(out) :: ofid
integer(i2), parameter :: missing = -32768
integer, dimension(4) :: dimids
integer, dimension(4) :: chunks
character(8) :: today
character(10) :: now
integer :: ncstat
integer :: dimid
integer :: varid
real(dp), dimension(2) :: xrange = [0.,0.]
real(dp), dimension(2) :: yrange = [0.,0.]
! ----------------------
call date_and_time(today,now)
ncstat = nf90_create(outfile,nf90_hdf5,ofid)
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_put_att(ofid,nf90_global,'title','BIOME4 netCDF output file')
if (ncstat/=nf90_noerr) call handle_err(ncstat)
call date_and_time(today,now)
ncstat = nf90_put_att(ofid,nf90_global,'timestamp',today//' '//now(1:4))
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_put_att(ofid,nf90_global,'Jobfile',jobfile)
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_put_att(ofid,nf90_global,'Conventions','COARDS')
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_put_att(ofid,nf90_global,'node_offset',1)
if (ncstat/=nf90_noerr) call handle_err(ncstat)
! ----
! coordinate lon
ncstat = nf90_def_dim(ofid,'lon',xlen,dimid)
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_def_var(ofid,'lon',nf90_double,dimid,varid)
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_put_att(ofid,varid,'long_name','longitude')
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_put_att(ofid,varid,'units','degrees_east')
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_put_att(ofid,varid,'actual_range',xrange)
if (ncstat/=nf90_noerr) call handle_err(ncstat)
dimids(1) = dimid
! ----
! coordinate lat
ncstat = nf90_def_dim(ofid,'lat',ylen,dimid)
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_def_var(ofid,'lat',nf90_double,dimid,varid)
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_put_att(ofid,varid,'long_name','latitude')
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_put_att(ofid,varid,'units','degrees_north')
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_put_att(ofid,varid,'actual_range',yrange)
if (ncstat/=nf90_noerr) call handle_err(ncstat)
dimids(2) = dimid
! ----
! coordinate PFT
ncstat = nf90_def_dim(ofid,'pft',13,dimid)
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_def_var(ofid,'pft',nf90_int,dimid,varid)
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_put_att(ofid,varid,'long_name','plant functional type')
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_put_att(ofid,varid,'units','type')
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_put_att(ofid,varid,'actual_range',[1,13])
if (ncstat/=nf90_noerr) call handle_err(ncstat)
dimids(3) = dimid
! ----
! coordinate month
ncstat = nf90_def_dim(ofid,'month',12,dimid)
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_def_var(ofid,'month',nf90_int,dimid,varid)
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_put_att(ofid,varid,'long_name','month')
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_put_att(ofid,varid,'units','month')
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_put_att(ofid,varid,'actual_range',[1,12])
if (ncstat/=nf90_noerr) call handle_err(ncstat)
dimids(4) = dimid
! ----
chunks(1) = min(xlen,200)
chunks(2) = min(ylen,200)
chunks(3) = 1
chunks(4) = 1
! ----
ncstat = nf90_def_var(ofid,'biome',nf90_short,dimids(1:2),varid,chunksizes=chunks(1:2),deflate_level=1,shuffle=.false.)
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_put_att(ofid,varid,'long_name','biome')
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_put_att(ofid,varid,'units','biome')
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_put_att(ofid,varid,'_FillValue',missing)
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_put_att(ofid,varid,'missing_value',missing)
if (ncstat/=nf90_noerr) call handle_err(ncstat)
! ----
ncstat = nf90_def_var(ofid,'wdom',nf90_short,dimids(1:2),varid,chunksizes=chunks(1:2),deflate_level=1,shuffle=.false.)
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_put_att(ofid,varid,'long_name','dominant tree pft')
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_put_att(ofid,varid,'units','PFT')
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_put_att(ofid,varid,'_FillValue',missing)
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_put_att(ofid,varid,'missing_value',missing)
if (ncstat/=nf90_noerr) call handle_err(ncstat)
! ----
ncstat = nf90_def_var(ofid,'gdom',nf90_short,dimids(1:2),varid,chunksizes=chunks(1:2),deflate_level=1,shuffle=.false.)
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_put_att(ofid,varid,'long_name','dominant non-tree pft')
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_put_att(ofid,varid,'units','PFT')
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_put_att(ofid,varid,'_FillValue',missing)
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_put_att(ofid,varid,'missing_value',missing)
if (ncstat/=nf90_noerr) call handle_err(ncstat)
! ----
ncstat = nf90_def_var(ofid,'npp',nf90_float,dimids,varid,chunksizes=chunks,deflate_level=1,shuffle=.false.)
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_put_att(ofid,varid,'long_name','net primary productivity')
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_put_att(ofid,varid,'units','g m-2')
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_put_att(ofid,varid,'_FillValue',-9999.)
if (ncstat/=nf90_noerr) call handle_err(ncstat)
ncstat = nf90_put_att(ofid,varid,'missing_value',-9999.)
if (ncstat/=nf90_noerr) call handle_err(ncstat)
! ----
! ncstat = nf90_def_var(ofid,'lai',nf90_float,dimids,varid,chunksizes=chunks,deflate_level=1,shuffle=.false.)
! if (ncstat/=nf90_noerr) call handle_err(ncstat)
!
! ncstat = nf90_put_att(ofid,varid,'long_name','LAI')
! if (ncstat/=nf90_noerr) call handle_err(ncstat)
!
! ncstat = nf90_put_att(ofid,varid,'units','m2 m-2')
! if (ncstat/=nf90_noerr) call handle_err(ncstat)
!
! ncstat = nf90_put_att(ofid,varid,'_FillValue',-9999.)
! if (ncstat/=nf90_noerr) call handle_err(ncstat)
!
! ncstat = nf90_put_att(ofid,varid,'missing_value',-9999.)
! if (ncstat/=nf90_noerr) call handle_err(ncstat)
! ----
! ncstat = nf90_def_var(ofid,'npp',nf90_float,dimids,varid,chunksizes=chunks,deflate_level=1,shuffle=.false.)
! if (ncstat/=nf90_noerr) call handle_err(ncstat)
!
! ncstat = nf90_put_att(ofid,varid,'long_name','NPP')
! if (ncstat/=nf90_noerr) call handle_err(ncstat)
!
! ncstat = nf90_put_att(ofid,varid,'units','gC m-2')
! if (ncstat/=nf90_noerr) call handle_err(ncstat)
!
! ncstat = nf90_put_att(ofid,varid,'_FillValue',-9999.)
! if (ncstat/=nf90_noerr) call handle_err(ncstat)
!
! ncstat = nf90_put_att(ofid,varid,'missing_value',-9999.)
! if (ncstat/=nf90_noerr) call handle_err(ncstat)
! ----
ncstat = nf90_enddef(ofid)
if (ncstat/=nf90_noerr) call handle_err(ncstat)
end subroutine genoutfile
! -------------------------------------------------------
end module netcdfmod