-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpbpwrite.F90
404 lines (331 loc) · 13.9 KB
/
pbpwrite.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
subroutine create_pbp( npoints, year, month, numvars, totnumvars, &
latitude, longitude, dopbpsib, namepbpsib, listpbpsib, &
indxpbpsib, drvr_type, biome_source, soil_source, &
soref_source, ndvi_source, c4_source, d13cresp_source, &
outpath,pbptimeid, pbpcharid, pbpvarid, &
rank1 )
use netcdf
use typeSizes
#include "nc_util.h"
! parameters
integer, intent(in) :: npoints
integer, intent(in) :: year
integer, intent(in) :: month
integer, intent(in) :: numvars
integer, intent(in) :: totnumvars
real, dimension(npoints), intent(in) :: latitude
real, dimension(npoints), intent(in) :: longitude
logical, dimension(totnumvars), intent(in) :: dopbpsib
character(len=16), dimension(totnumvars), intent(in) :: namepbpsib
character(len=80), dimension(totnumvars), intent(in) :: listpbpsib
integer, dimension(totnumvars), intent(in) :: indxpbpsib
character(len=8), intent(in) :: drvr_type
character(len=100), intent(in) :: biome_source
character(len=100), intent(in) :: soil_source
character(len=100), intent(in) :: soref_source
character(len=100), intent(in) :: ndvi_source
character(len=100), intent(in) :: c4_source
character(len=100), intent(in) :: d13cresp_source
character(len=256), intent(in) :: outpath
integer, intent(out) :: pbptimeid
integer, intent(out) :: pbpcharid
integer, dimension(numvars), intent(out) :: pbpvarid
integer, intent(in) :: rank1
! netcdf variables
integer :: status
integer :: tid
integer :: clid
integer :: npid
integer :: npointsid
integer :: latid
integer :: lonid
integer :: pbpid
! local variables
integer :: n
character(len=40) :: units ! variable units
character(len=80) :: longname ! variable description
integer :: unit_len, long_len ! not used, returned by get_units()
integer, dimension(npoints) :: npoints_array
character(len =256) ::filename
! create file name
write( filename, '(a,i4.4,i2.2,a,i3.3,a)' ) trim(outpath)//'psib_', &
year, month, 'p', rank1, '.pbp1.nc'
! create file and define dimensions
CHECK( nf90_create( trim(filename), nf90_clobber, pbpid ) )
CHECK( nf90_def_dim( pbpid, 'time', nf90_unlimited, tid ) )
CHECK( nf90_def_dim( pbpid, 'char_len', 10, clid ) )
CHECK( nf90_def_dim( pbpid, 'npoints', npoints, npointsid ) )
! define global atts
call global_atts( pbpid, 'sib3', 'lat/lon', '1.0', drvr_type, &
biome_source, soil_source, soref_source, ndvi_source, c4_source, &
d13cresp_source, rank1 )
! define variables
CHECK( nf90_def_var( pbpid, 'time', nf90_double, (/tid/), pbptimeid ) )
CHECK( nf90_put_att( pbpid, pbptimeid, 'quantity', 'time' ) )
CHECK( nf90_put_att( pbpid, pbptimeid, 'units', 'days since 1-1-1' ) )
CHECK( nf90_put_att( pbpid, pbptimeid, 'calendar', 'noleap' ) )
CHECK( nf90_def_var( pbpid, 'char_time', nf90_char, (/clid,tid/), pbpcharid ) )
CHECK( nf90_put_att( pbpid, pbpcharid, 'format', 'mm/dd/yyyy' ) )
CHECK( nf90_def_var( pbpid, 'npoints', nf90_int, (/npointsid/), npid ) )
CHECK( nf90_def_var( pbpid, 'latitude', nf90_float, (/npointsid/), latid ) )
CHECK( nf90_put_att( pbpid, latid, 'units', 'degrees_north' ) )
CHECK( nf90_put_att( pbpid, latid, 'quantity', 'latitude' ) )
CHECK( nf90_def_var( pbpid, 'longitude', nf90_float, (/npointsid/), lonid ) )
CHECK( nf90_put_att( pbpid, lonid, 'units', 'degrees_east' ) )
CHECK( nf90_put_att( pbpid, lonid, 'quantity', 'longitude' ) )
do n = 1, totnumvars
if ( dopbpsib(n) ) then
status = nf90_def_var( pbpid, trim(namepbpsib(n)), nf90_float, &
(/npointsid,tid/), pbpvarid(indxpbpsib(n)) )
CHECK( status )
call get_units( listpbpsib(n), longname, long_len, units, unit_len )
status = nf90_put_att( pbpid, pbpvarid(indxpbpsib(n)), &
'long_name', trim(longname) )
CHECK( status )
status = nf90_put_att( pbpid, pbpvarid(indxpbpsib(n)), &
'title', trim(longname) )
CHECK( status )
status = nf90_put_att( pbpid, pbpvarid(indxpbpsib(n)), &
'units', trim(units) )
CHECK( status )
status = nf90_put_att( pbpid, pbpvarid(indxpbpsib(n)), &
'missing_value', 1.e36 )
CHECK( status )
endif
enddo
! switch from definition mode to data mode
CHECK( nf90_enddef( pbpid ) )
! assign values to variables not variant with time
CHECK( nf90_put_var( pbpid, latid, latitude ) )
CHECK( nf90_put_var( pbpid, lonid, longitude ) )
do n = 1, npoints
npoints_array(n) = n
enddo
CHECK( nf90_put_var( pbpid, npid, npoints_array(:) ) )
CHECK( nf90_close( pbpid ) )
end subroutine create_pbp
!-----------------------------------------------------------------
subroutine write_pbp( npoints, year, month, day, seconds, &
numvars, pbp, pbptid, pbpcid, &
pbpvid,outpath, rank1 )
use netcdf
use typeSizes
use kinds
! parameters
integer, intent(in) :: npoints
integer, intent(in) :: year
integer, intent(in) :: month
integer, intent(in) :: day
integer, intent(in) :: seconds
integer, intent(in) :: numvars
real(kind=dbl_kind), dimension(numvars+1,npoints), intent(in) :: pbp
integer, intent(in) :: pbptid
integer, intent(in) :: pbpcid
integer, intent(in), dimension(numvars) :: pbpvid
character(len=256), intent(in) :: outpath
integer, intent(in) :: rank1
! netcdf variables
integer :: status
integer :: dimid
! local variables
integer :: i
integer :: step
double precision :: dyear
character(len=10) :: char_time
character(len=256) :: filename
character(len=10) :: name
double precision :: secyear = 86400.
integer :: pbpid
!open file
write( filename, '(a,i4.4,i2.2,a,i3.3,a)' ) trim(outpath)//'psib_', &
year, month, 'p', rank1, '.pbp1.nc'
CHECK( nf90_open( trim(filename), nf90_write, pbpid ) )
! find next time step
CHECK( nf90_inq_dimid( pbpid, 'time', dimid ) )
CHECK( nf90_inquire_dimension( pbpid, dimid, name,step ) )
step = step + 1
! write out time variables
dyear = seconds/secyear
CHECK( nf90_put_var( pbpid, pbptid, dyear, (/step/) ) )
write( char_time, '(i2.2,a1,i2.2,a1,i4.4)' ) month, '/', day, '/', year
CHECK( nf90_put_var( pbpid, pbpcid, char_time, (/1,step/), (/10,1/) ) )
! write out data variables
do i = 1, numvars
status = nf90_put_var( pbpid, pbpvid(i), pbp(i,:), &
(/1,step/), (/npoints,1/) )
CHECK( status )
enddo
CHECK( nf90_close( pbpid ) )
end subroutine write_pbp
!-----------------------------------------------------------------
subroutine create_pbp2( npoints, levels, year, month, numvars, &
totnumvars, latitude, longitude, dopbp2sib, &
namepbp2sib, listpbp2sib, indxpbp2sib, drvr_type, &
biome_source, soil_source, soref_source, ndvi_source, &
c4_source, d13cresp_source, out_path, &
pbp2timeid, pbp2charid, pbp2varid,rank )
use netcdf
use typeSizes
! parameters
integer, intent(in) :: npoints
integer, intent(in) :: levels
integer, intent(in) :: year
integer, intent(in) :: month
integer, intent(in) :: numvars
integer, intent(in) :: totnumvars
real, dimension(npoints), intent(in) :: latitude
real, dimension(npoints), intent(in) :: longitude
logical, dimension(totnumvars), intent(in) :: dopbp2sib
character(len=16), dimension(totnumvars), intent(in) :: namepbp2sib
character(len=80), dimension(totnumvars), intent(in) :: listpbp2sib
integer, dimension(totnumvars), intent(in) :: indxpbp2sib
character(len=8), intent(in) :: drvr_type
character(len=100), intent(in) :: biome_source
character(len=100), intent(in) :: soil_source
character(len=100), intent(in) :: soref_source
character(len=100), intent(in) :: ndvi_source
character(len=100), intent(in) :: c4_source
character(len=100), intent(in) :: d13cresp_source
character(len=256), intent(in) :: out_path
integer, intent(out) :: pbp2timeid
integer, intent(out) :: pbp2charid
integer, dimension(numvars), intent(out) :: pbp2varid
integer, intent(in) :: rank
! netcdf variables
integer :: status
integer :: tid
integer :: charid
integer :: npointsid
integer :: levelid
integer :: npid
integer :: levid
integer :: latid
integer :: lonid
character(len=256) :: filename
integer :: pbp2id
! local variables
integer :: x
integer, dimension(npoints) :: npoints_array
integer, dimension(levels) :: levels_array
character(len=40) :: units ! variable units
character(len=80) :: longname ! variable description
integer :: unit_len, long_len ! not used, returned by get_units()
! create file name
write( filename, '(a,i4.4,i2.2,a,i3.3,a)' ) trim(out_path)//'psib_', &
year, month, 'p', rank, '.pbp2.nc'
! make sure pbp2id is not tied to any open file
status = nf90_close( pbp2id )
! create file and define dimensions
CHECK( nf90_create( trim(filename), nf90_clobber, pbp2id ) )
CHECK( nf90_def_dim( pbp2id, 'time', nf90_unlimited, tid ) )
CHECK( nf90_def_dim( pbp2id, 'char_len', 10, charid ) )
CHECK( nf90_def_dim( pbp2id, 'npoints', npoints, npointsid ) )
CHECK( nf90_def_dim( pbp2id, 'level', levels, levelid ) )
! define global atts
call global_atts( pbp2id, 'sib3', 'lat/lon', '1.0', drvr_type, &
biome_source, soil_source, soref_source, ndvi_source, c4_source, &
d13cresp_source, rank )
! define variables
CHECK( nf90_def_var( pbp2id, 'time', nf90_double, (/tid/), pbp2timeid ) )
CHECK( nf90_put_att( pbp2id, pbp2timeid, 'quantity', 'time' ) )
CHECK( nf90_put_att( pbp2id, pbp2timeid, 'units', 'days since 1-1-1' ) )
CHECK( nf90_put_att( pbp2id, pbp2timeid, 'calendar', 'noleap' ) )
CHECK( nf90_def_var( pbp2id, 'char_time', nf90_char, (/charid,tid/), pbp2charid ) )
CHECK( nf90_put_att( pbp2id, pbp2charid, 'format', 'mm/dd/yyyy' ) )
CHECK( nf90_def_var( pbp2id, 'npoints', nf90_int, (/npointsid/), npid ) )
CHECK( nf90_def_var( pbp2id, 'latitude', nf90_float, (/npointsid/), latid ) )
CHECK( nf90_put_att( pbp2id, latid, 'units', 'degrees_north' ) )
CHECK( nf90_put_att( pbp2id, latid, 'quantity', 'latitude' ) )
CHECK( nf90_def_var( pbp2id, 'longitude', nf90_float, (/npointsid/), lonid ) )
CHECK( nf90_put_att( pbp2id, lonid, 'units', 'degrees_east' ) )
CHECK( nf90_put_att( pbp2id, lonid, 'quantity', 'longitude' ) )
CHECK( nf90_def_var( pbp2id, 'level', nf90_int, (/levelid/), levid ) )
do x = 1, numvars
if ( dopbp2sib(x) ) then
status = nf90_def_var( pbp2id, trim(namepbp2sib(x)), nf90_float, &
(/npointsid,levelid,tid/), pbp2varid(indxpbp2sib(x)) )
CHECK( status )
call get_units( listpbp2sib(x), longname, long_len, units, unit_len )
status = nf90_put_att( pbp2id, pbp2varid(indxpbp2sib(x)), &
'long_name', trim(longname) )
CHECK( status )
status = nf90_put_att( pbp2id, pbp2varid(indxpbp2sib(x)), &
'title', trim(longname) )
CHECK( status )
status = nf90_put_att( pbp2id, pbp2varid(indxpbp2sib(x)), &
'units', trim(units) )
CHECK( status )
status = nf90_put_att( pbp2id, pbp2varid(indxpbp2sib(x)), &
'missing_value', 1.e36 )
CHECK( status )
endif
enddo
! switch from define mode to data mode
CHECK( nf90_enddef( pbp2id ) )
! assign values to variables not variant with time
CHECK( nf90_put_var( pbp2id, latid, latitude ) )
CHECK( nf90_put_var( pbp2id, lonid, longitude ) )
do x = 1, npoints
npoints_array(x) = x
enddo
CHECK( nf90_put_var( pbp2id, npid, npoints_array(:) ) )
do x = 1, levels
levels_array(x) = x
enddo
CHECK( nf90_put_var( pbp2id, levid, levels_array(:) ) )
CHECK( nf90_close( pbp2id ) )
end subroutine create_pbp2
!-----------------------------------------------------------------------
subroutine write_pbp2( npoints, levels, year, month, day, &
seconds, numvars, pbp2, pbp2timeid, &
pbp2charid, pbp2varid,out_path, rank )
use netcdf
use typeSizes
use kinds
! parameters
integer, intent(in) :: npoints
integer, intent(in) :: levels
integer, intent(in) :: year
integer, intent(in) :: month
integer, intent(in) :: day
integer, intent(in) :: seconds
integer, intent(in) :: numvars
real(kind=dbl_kind), dimension(levels,numvars+1,npoints), intent(in) :: pbp2
integer, intent(in) :: pbp2timeid
integer, intent(in) :: pbp2charid
integer, dimension(numvars), intent(in) :: pbp2varid
character(len=256), intent(in) :: out_path
integer, intent(in) :: rank
! netcdf variables
integer :: status
integer :: dimid
! local variables
integer :: i
integer :: step
double precision :: dyear
character(len=10) :: char_time
character(len=256) :: filename
double precision :: secyear = 86400.
character(len=10) :: name
integer :: pbp2id
! create file name
write( filename, '(a,i4.4,i2.2,a,i3.3,a)' ) trim(out_path)//'psib_', &
year, month, 'p', rank, '.pbp2.nc'
CHECK( nf90_open( trim(filename), nf90_write, pbp2id ) )
! find next time step
CHECK( nf90_inq_dimid( pbp2id, 'time', dimid ) )
CHECK( nf90_inquire_dimension( pbp2id, dimid, name,step ) )
step = step + 1
! write out time variables
dyear = seconds/secyear
CHECK( nf90_put_var( pbp2id, pbp2timeid, dyear, (/step/) ) )
write( char_time, '(i2.2,a1,i2.2,a1,i4.4)' ) month, '/', day, '/', year
CHECK( nf90_put_var( pbp2id, pbp2charid, char_time, (/1,step/), (/10,1/) ) )
! write out data variables
do i = 1, numvars
status = nf90_put_var( pbp2id, pbp2varid(i), pbp2(:,i,:), &
(/1,1,step/), (/npoints,levels,1/) )
CHECK( status )
enddo
CHECK( nf90_close( pbp2id ) )
end subroutine write_pbp2