-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathb2co_time_cdf.F
More file actions
360 lines (352 loc) · 12.6 KB
/
b2co_time_cdf.F
File metadata and controls
360 lines (352 loc) · 12.6 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
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
subroutine b2co_time_cdf(nx, ny, ns, nnatmi, nnmoli, tim,
. batch_only)
use b2mod_types
use b2mod_geo
use b2mod_mwti
use b2mod_indirect
use b2mod_numerics_namelist
use b2mod_subsys
implicit none
#ifndef NO_CDF
# include <netcdf.inc>
#endif
integer :: nx, ny, ns, nnatmi, nnmoli
real (kind=R8) :: tim
logical :: batch_only
integer :: jxi, jxa, jsep
integer :: iylstrt, iyistrt, iyastrt, iyrstrt,
1 iylend, iyiend, iyaend, iyrend
integer, save :: write_2d = 0, target_offset = 1, ntim_batch = 500
#ifndef NO_CDF
#ifndef NO_GETENV
integer lenval, ierror
#ifndef USE_PXFGETENV
intrinsic get_environment_variable
#endif
#endif
integer :: status
integer :: nc, batchdim, nastep, nbatch, ntstep, timedim,
1 ixtl, ixtr, nybl, nybr, nytl, nytr, nya, nyi,
2 iytlend, iytlstrt, iytrend, iytrstrt
integer :: idim, ivar, iatt, iret, ib, it, ih,
1 itim, ibatch, ncid, ncid2, ndims, nvars, natts, unlimid, xtype,
2 dimids(NF_MAX_VAR_DIMS), len, dimsize(NF_MAX_VAR_DIMS),
3 start(NF_MAX_VAR_DIMS), count(NF_MAX_VAR_DIMS)
real (kind=R8) :: scale
real (kind=R8), allocatable :: data1(:), data2(:,:), data3(:,:,:)
real (kind=R8) :: dsl(-1:ny), dsi(-1:ny), dsa(-1:ny),
1 dsr(-1:ny), dstl(-1:ny), dstr(-1:ny)
character*(NF_MAX_NAME) :: varname, dimname, attname
character :: units*(24)
character*4 :: extension
character*32 hlp_frm
character*256 filename, plasmastate, message
logical :: exists
#endif
external ipgeti, ipgetr, xerrab, xertst
call subini ('b2co_time_cdf')
write(*,*) 'b2co_time_cdf ...'
call get_jsep(nx,ny,jxi,jxa,jsep)
call ipgeti ('b2mwti_2dwrite', write_2d)
call ipgeti ('b2mndt_av_ntim_batch', ntim_batch)
call ipgeti ('b2mwti_target_offset', target_offset)
call output_ds(ny, -1,+target_offset,jsep,iylstrt,iylend,
. 'dsl',dsl)
call output_ds(ny,jxi,0,jsep,iyistrt,iyiend,'dsi',dsi)
call output_ds(ny,jxa,0,jsep,iyastrt,iyaend,'dsa',dsa)
call output_ds(ny, nx,-target_offset,jsep,iyrstrt,iyrend,
. 'dsr',dsr)
call xertst (0.lt.ns, 'faulty argument ns')
#ifndef NO_CDF
if (nnreg(0).ge.7) then
ixtl = 0
do while (rightix(ixtl,max(topcut(1),topcut(2))).ne.nx+1.and.
. ixtl.lt.nx)
ixtl=ixtl+1
enddo
ixtr = ixtl
do while (leftix(ixtr,max(topcut(1),topcut(2))).ne.-2 .and.
. ixtr.lt.nx)
ixtr=ixtr+1
enddo
call output_ds(ny,ixtl,-target_offset,jsep,
. iytlstrt,iytlend,'dstl',dstl)
call output_ds(ny,ixtr,+target_offset,jsep,
. iytrstrt,iytrend,'dstr',dstr)
nytl = iytlend - iytlstrt + 1
nytr = iytrend - iytrstrt + 1
else
nytl = 0
nytr = 0
endif
nybl = iylend - iylstrt + 1
nybr = iyrend - iyrstrt + 1
nya = iyaend - iyastrt + 1
nyi = iyiend - iyistrt + 1
nc = max(nncut,1)
nbatch = int((tim/dteff)/ntim_batch) + 1
c access the b2time.nc or b2batch.nc file
filename='b2time.nc'
if (batch_only) filename='b2batch.nc'
call find_file(filename,exists)
status=nf_open(filename,NF_NOWRITE,ncid)
call check_cdf_status(status)
status=nf_inq(ncid,ndims,nvars,natts,unlimid)
call check_cdf_status(status)
itim=0
ibatch=0
nastep=0
ntstep=0
timedim=-1
batchdim=-1
c loop over all variables in the file
do ivar=1,nvars
status=nf_inq_var(ncid,ivar,varname,xtype,
1 ndims,dimids,natts)
call check_cdf_status(status)
do idim=1,ndims
status=nf_inq_dim(ncid,dimids(idim),dimname,len)
call check_cdf_status(status)
dimsize(idim)=len
enddo
if(varname.eq.'ntstep') then
allocate(data1(1))
status=nf_get_var_double(ncid,ivar,data1)
call check_cdf_status(status)
ntstep = nint(data1(1))
deallocate(data1)
else if(varname.eq.'nastep') then
allocate(data1(1))
status=nf_get_var_double(ncid,ivar,data1)
call check_cdf_status(status)
nastep = nint(data1(1))
deallocate(data1)
endif
units=''
scale=1
do iatt=1,natts
status=nf_inq_attname(ncid,ivar,iatt,attname)
call check_cdf_status(status)
status=nf_inq_att(ncid,ivar,attname,xtype,len)
call check_cdf_status(status)
if(attname.eq.'units') then
if(len.le.24) then
status=nf_get_att_text(ncid,ivar,attname,units)
call check_cdf_status(status)
else
call xerrab ('increase size of units')
endif
endif
if(attname.eq.'scale') then
if(len.le.1) then
status=nf_get_att_double(ncid,ivar,attname,scale)
call check_cdf_status(status)
else
call xerrab ('increase size of scale')
endif
endif
enddo
enddo
c find the time and batch vector sizes
do ivar=1,nvars
status=nf_inq_var(ncid,ivar,varname,xtype,
1 ndims,dimids,natts)
call check_cdf_status(status)
do idim=1,ndims
status=nf_inq_dim(ncid,dimids(idim),dimname,len)
call check_cdf_status(status)
dimsize(idim)=len
enddo
if(varname.eq.'timesa'.and.ntstep.gt.0) then
allocate(data1(dimsize(1)))
status=nf_get_var_double(ncid,ivar,data1)
call check_cdf_status(status)
timedim=dimsize(1)
ib=1
it=timedim
ih=(ib+it)/2
do while (it-ib.gt.1)
ih=(ib+it)/2
if(tim.lt.data1(ih)) then
it=ih
else if(tim.gt.data1(ih)) then
ib=ih
else
exit
endif
end do
itim=ih
deallocate(data1)
else if(varname.eq.'batchsa'.and.nastep.gt.0) then
allocate(data1(dimsize(1)))
status=nf_get_var_double(ncid,ivar,data1)
call check_cdf_status(status)
batchdim=dimsize(1)
ib=1
it=batchdim
ih=(ib+it)/2
do while (it-ib.gt.1)
ih=(ib+it)/2
if(tim.lt.data1(ih)) then
it=ih
else if(tim.gt.data1(ih)) then
ib=ih
else
exit
endif
end do
ibatch=ih
deallocate(data1)
end if
end do
c prepare the b2time.nc.xxxx or b2batch.nc.xxxx file
extension='xxxx'
#ifndef NO_GETENV
#ifdef USE_PXFGETENV
CALL PXFGETENV ('PLASMASTATE', 0, plasmastate, lenval, ierror)
if (ierror.eq.0) extension=plasmastate(lenval-3:lenval)
#else
call get_environment_variable('PLASMASTATE',
. status=ierror, length=lenval)
if (ierror.eq.0) then
call get_environment_variable('PLASMASTATE', value=plasmastate)
extension=plasmastate(lenval-3:lenval)
endif
#endif
#endif
filename=trim(filename)//'.'//extension
message = 'File '//trim(filename)//' already present! '//
. 'Please remove first.'
call find_file(filename,exists)
call xertst(.not.exists,message)
status=nf_create(trim(filename),NF_NOCLOBBER,ncid2)
call check_cdf_status(status)
call b2crtimecdf(filename, nx, ny, nybl, nytl, nytr, nybr,
. nya, nyi, nc, ns, nnatmi, nnmoli, write_2d,
. ncid2, batch_only, iret)
status=nf_open(trim(filename),NF_WRITE,ncid2)
call check_cdf_status(status)
c read and rewrite data
do ivar=1,nvars
status=nf_inq_var(ncid,ivar,varname,xtype,
1 ndims,dimids,natts)
call check_cdf_status(status)
count=0
do idim=1,ndims
status=nf_inq_dim(ncid,dimids(idim),dimname,len)
call check_cdf_status(status)
dimsize(idim)=len
count(idim)=len
start(idim)=1
enddo
if (ndims.eq.0) then
write(message,'(a)') 'Processing '//trim(varname)//' ...'
elseif (ndims.eq.1) then
write(message,'(a,i6,a)') 'Processing '//trim(varname)//
. ' with size ',len,' ...'
else
write(hlp_frm,'(a,i1,a)') '(3a,',ndims-1,'(i6,'',''),i6,a1)'
write(message,hlp_frm) 'Processing ',trim(varname),
. ' with dimensions (',(count(idim),idim=1,ndims),')'
end if
write(*,'(a)') trim(message)
if(varname.eq.'timesa') then
if(itim.gt.0) then
allocate(data1(dimsize(1)))
status=nf_get_vara_double(ncid,ivar,start,count,data1)
call check_cdf_status(status)
count(1)=itim
status=nf_put_vara_double(ncid2,ivar,start,count,data1)
deallocate(data1)
end if
else if(varname.eq.'batchsa') then
if(ibatch.gt.0) then
allocate(data1(dimsize(1)))
status=nf_get_vara_double(ncid,ivar,start,count,data1)
call check_cdf_status(status)
count(1)=ibatch
status=nf_put_vara_double(ncid2,ivar,start,count,data1)
deallocate(data1)
endif
else
if(ndims.eq.3) then
allocate(data3(dimsize(1),dimsize(2),dimsize(3)))
status=nf_get_vara_double(ncid,ivar,start,count,data3)
call check_cdf_status(status)
if (dimsize(3).eq.timedim) then
count(3)=itim
if (itim.gt.0) status=nf_put_vara_double(ncid2,ivar,
. start,count,data3)
else if (dimsize(3).eq.batchdim) then
count(3)=ibatch
if (ibatch.gt.0) status=nf_put_vara_double(ncid2,ivar,
. start,count,data3)
else
count(3)=dimsize(3)
status=nf_put_vara_double(ncid2,ivar,start,count,data3)
end if
call check_cdf_status(status)
deallocate(data3)
elseif(ndims.eq.2) then
allocate(data2(dimsize(1),dimsize(2)))
status=nf_get_vara_double(ncid,ivar,start,count,data2)
call check_cdf_status(status)
if (dimsize(2).eq.timedim) then
count(2)=itim
if (itim.gt.0) status=nf_put_vara_double(ncid2,ivar,
. start,count,data2)
else if (dimsize(2).eq.batchdim) then
count(2)=ibatch
if (ibatch.gt.0) status=nf_put_vara_double(ncid2,ivar,
. start,count,data2)
else
status=nf_put_vara_double(ncid2,ivar,start,count,data2)
end if
call check_cdf_status(status)
deallocate(data2)
elseif(ndims.eq.1) then
allocate(data1(dimsize(1)))
status=nf_get_vara_double(ncid,ivar,start,count,data1)
call check_cdf_status(status)
if (dimsize(1).eq.timedim) then
count(1)=itim
if (itim.gt.0) status=nf_put_vara_double(ncid2,ivar,
. start,count,data1)
else if (dimsize(1).eq.batchdim) then
count(1)=ibatch
if (ibatch.gt.0) status=nf_put_vara_double(ncid2,ivar,
. start,count,data1)
else
status=nf_put_vara_double(ncid2,ivar,start,count,data1)
end if
call check_cdf_status(status)
deallocate(data1)
elseif(ndims.eq.0) then
allocate(data1(1))
status=nf_get_var_double(ncid,ivar,data1)
call check_cdf_status(status)
if (nint(data1(1)).eq.timedim) then
data1(1) = real(itim)
else if (nint(data1(1)).eq.batchdim) then
data1(1) = real(ibatch)
end if
status=nf_put_var_double(ncid2,ivar,data1)
call check_cdf_status(status)
deallocate(data1)
else
write(*,'(a,i2,a)')
* 'Case for ',ndims,' dimensions not yet coded'
endif
endif
enddo
status=nf_close(ncid)
call check_cdf_status(status)
status=nf_close(ncid2)
call check_cdf_status(status)
#else
write(*,*)
1 'b2co_time_cdf failed because compiled with NO_CDF option'
#endif
call subend ()
return
end subroutine b2co_time_cdf