File: C:\NOAA\NEMS_11731\src\chem\gocart\src\GMAO_Shared\GMAO_mpeu\m_ioutil.F90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 module m_ioutil
16 implicit none
17 private
18
19 public :: opntext,clstext
20 public :: opnieee,clsieee
21 public :: luavail
22 public :: luflush
23 public :: byteswap
24
25
26 interface byteswap; module procedure &
27 swapI4_, &
28 swapI8_; end interface
29
30
31
32
33
34
35
36
37
38
39
40 character(len=*),parameter :: myname="m_ioutil"
41 integer,parameter :: MX_LU=255
42
43 contains
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 subroutine opnieee(lu,fname,status,ier,recl)
69 use m_stdio,only : stderr
70 implicit none
71
72 integer, intent(in) :: lu
73 character(len=*),intent(in) :: fname
74 character(len=*),intent(in) :: status
75 integer, intent(out):: ier
76 integer,optional,intent(in) :: recl
77
78
79
80
81
82
83
84
85
86
87
88
89 character(len=*),parameter :: myname_=myname//'::opnieee'
90
91 integer,parameter :: iA=ichar('a')
92 integer,parameter :: mA=ichar('A')
93 integer,parameter :: iZ=ichar('z')
94
95 logical :: direct
96 character(len=16) :: clen
97 character(len=len(status)) :: Ustat
98 integer :: i,ic
99
100 direct=.false.
101 if(present(recl)) then
102 if(recl<0) then
103 clen='****************'
104 write(clen,'(i16)',iostat=ier) recl
105 write(stderr,'(3a)') myname_, &
106 ': invalid recl, ',trim(adjustl(clen))
107 ier=-1
108 return
109 endif
110 direct = recl>0
111 endif
112
113 #ifdef _UNICOS
114 character(len=128) :: attr
115
116 call asnqunit(lu,attr,ier)
117
118 if(ier.eq.-1) then
119 if(direct) then
120 call asnunit(lu,'-N ieee -F null',ier)
121 else
122 call asnunit(lu,'-N ieee -F f77',ier)
123 endif
124 ier=0
125
126 elseif(ier.ge.0) then
127 ier=-1
128 endif
129 if(ier.ne.0) return
130 #endif
131
132 do i=1,len(status)
133 ic=ichar(status(i:i))
134 if(ic >= iA .and. ic <= iZ) ic=ic+(mA-iA)
135 Ustat(i:i)=char(ic)
136 end do
137
138 select case(Ustat)
139
140 case ('APPEND')
141
142 if(direct) then
143 write(stderr,'(2a)') myname_, &
144 ': invalid arguments, (status=="APPEND",recl>0)'
145 ier=1
146 return
147 endif
148
149 open( &
150 unit =lu, &
151 file =fname, &
152 form ='unformatted', &
153 access ='sequential', &
154 status ='unknown', &
155 position ='append', &
156 iostat =ier )
157
158 case default
159
160 if(direct) then
161 open( &
162 unit =lu, &
163 file =fname, &
164 form ='unformatted', &
165 access ='direct', &
166 status =status, &
167 recl =recl, &
168 iostat =ier )
169
170 else
171 open( &
172 unit =lu, &
173 file =fname, &
174 form ='unformatted', &
175 access ='sequential', &
176 status =status, &
177 position ='asis', &
178 iostat =ier )
179 endif
180
181 end select
182
183 end subroutine opnieee
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202 subroutine clsieee(lu,ier,status)
203 implicit none
204 integer, intent(in) :: lu
205 integer, intent(out) :: ier
206 Character(len=*), optional, intent(In) :: status
207
208
209
210
211
212 character(len=*), parameter :: myname_ = myname//'::clsieee'
213 Character(Len=6) :: status_
214
215 status_ = 'KEEP'
216 If (Present(status)) Then
217 Select Case (Trim(status))
218 Case ('DELETE','delete')
219 status_ = 'DELETE'
220 Case ('KEEP','keep')
221 status_ = 'KEEP'
222 Case Default
223 ier = -997
224 return
225 End Select
226 End If
227
228 close(lu,iostat=ier,status=status_)
229 #ifdef _UNICOS
230 if(ier==0) call asnunit(lu,'-R',ier)
231 #endif
232
233 end subroutine clsieee
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249 subroutine opntext(lu,fname,status,ier)
250 implicit none
251
252 integer, intent(in) :: lu
253 character(len=*),intent(in) :: fname
254 character(len=*),intent(in) :: status
255 integer, intent(out):: ier
256
257
258
259
260
261
262
263
264
265
266
267 character(len=*),parameter :: myname_=myname//'::opntext'
268
269 integer,parameter :: iA=ichar('a')
270 integer,parameter :: mA=ichar('A')
271 integer,parameter :: iZ=ichar('z')
272
273 character(len=len(status)) :: Ustat
274 integer :: i,ic
275
276 #ifdef _UNICOS
277 call asnunit(lu,'-R',ier)
278 if(ier.ne.0) return
279 #endif
280
281 do i=1,len(status)
282 ic=ichar(status(i:i))
283 if(ic >= iA .and. ic <= iZ) ic=ic+(mA-iA)
284 Ustat(i:i)=char(ic)
285 end do
286
287 select case(Ustat)
288
289 case ('APPEND')
290
291 open( &
292 unit =lu, &
293 file =fname, &
294 form ='formatted', &
295 access ='sequential', &
296 status ='unknown', &
297 position ='append', &
298 iostat =ier )
299
300 case default
301
302 open( &
303 unit =lu, &
304 file =fname, &
305 form ='formatted', &
306 access ='sequential', &
307 status =status, &
308 position ='asis', &
309 iostat =ier )
310
311 end select
312
313 end subroutine opntext
314
315
316
317
318
319
320
321
322
323
324
325
326 subroutine clstext(lu,ier,status)
327 implicit none
328
329 integer, intent(in) :: lu
330 integer, intent(out) :: ier
331 Character(len=*), optional, intent(In) :: status
332
333
334
335
336
337 character(len=*), parameter :: myname_ = myname//'::clsitext'
338 Character(Len=6) :: status_
339
340 status_ = 'KEEP'
341 If (Present(status)) Then
342 Select Case (Trim(status))
343 Case ('DELETE','delete')
344 status_ = 'DELETE'
345 Case ('KEEP','keep')
346 status_ = 'KEEP'
347 Case Default
348 ier = -997
349 return
350 End Select
351 End If
352
353 close(lu,iostat=ier,status=status_)
354 #ifdef _UNICOS
355 if(ier == 0) call asnunit(lu,'-R',ier)
356 #endif
357
358 end subroutine clstext
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373 function luavail()
374 use m_stdio
375 implicit none
376 integer :: luavail
377
378
379
380
381
382
383
384
385
386
387
388
389
390 character(len=*),parameter :: myname_=myname//'::luavail'
391
392 integer lu,ios
393 logical inuse
394 character*8 attr
395
396 lu=-1
397 ios=0
398 inuse=.true.
399
400 do while(ios.eq.0.and.inuse)
401 lu=lu+1
402
403
404
405 = lu.eq.stdout .or. lu.eq.stdin .or. lu.eq.stderr
406
407 #ifdef sysSunOS
408
409 inuse = lu.eq.100 .or. lu.eq.101 .or. lu.eq.102
410 #endif
411
412
413
414 if(.not.inuse) inquire(unit=lu,opened=inuse,iostat=ios)
415
416 #ifdef _UNICOS
417
418
419
420 if(ios.eq.0 .and. .not.inuse) then
421 call asnqunit(lu,attr,ios)
422
423
424
425
426
427
428
429 inuse=ios.ne.-1
430 if(ios >= -1) ios=0
431 endif
432 #endif
433
434 if(lu >= MX_LU) ios=-1
435 end do
436
437 if(ios.ne.0) lu=-1
438 luavail=lu
439 end function luavail
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456 subroutine luflush(unit)
457 use m_stdio, only : stdout
458 implicit none
459 integer,optional,intent(in) :: unit
460
461
462
463
464
465 character(len=*),parameter :: myname_=myname//'::luflush'
466
467 integer :: ier
468 integer :: lu
469
470
471
472 =stdout
473 if(present(unit)) lu=unit
474 if(lu < 0) return
475
476
477
478 #ifdef sysIRIX64
479 call flush(lu,ier)
480 #else
481 #if sysAIX
482 call flush_(lu)
483 #else
484 call flush(lu)
485 #endif
486 #endif
487
488 end subroutine luflush
489
490
491
492
493
494
495
496
497
498
499
500 function swapI4_(ibuf)
501 use m_intkinds,only : I4 => kind_i4
502 implicit none
503 integer(I4),dimension(:),intent(in) :: ibuf
504 integer(I4),dimension(size(ibuf)) :: swapI4_
505
506
507
508
509
510
511 character(len=*),parameter :: myname_=myname//'::swapI4_'
512
513
514
515
516 call ioutil_byteswap_(size(ibuf),4,ibuf,swapI4_)
517
518 end function swapI4_
519
520
521
522
523
524
525
526
527
528
529
530 function swapI8_(ibuf)
531 use m_intkinds,only : I8 => kind_i8
532 implicit none
533 integer(I8),dimension(:),intent(in) :: ibuf
534 integer(I8),dimension(size(ibuf)) :: swapI8_
535
536
537
538
539
540
541 character(len=*),parameter :: myname_=myname//'::swapI8_'
542
543
544
545
546 call ioutil_byteswap_(size(ibuf),8,ibuf,swapI8_)
547
548 end function swapI8_
549 end module m_ioutil
550
551
552
553
554
555
556
557
558
559
560
561 subroutine ioutil_byteswap_(nword,nbyte,ibuf,obuf)
562 implicit none
563 integer,intent(in) :: nword
564 integer,intent(in) :: nbyte
565 character(len=1),dimension(0:nbyte-1,nword),intent(in ) :: ibuf
566 character(len=1),dimension(0:nbyte-1,nword),intent(out) :: obuf
567
568
569
570
571
572
573 character(len=*),parameter :: myname_='ioutil_byteswap_'
574 integer :: mbyte,ibyte,jbyte
575
576 mbyte=nbyte-1
577 do ibyte=0,mbyte
578 jbyte=mbyte-ibyte
579 obuf(jbyte,:)=ibuf(ibyte,:)
580 end do
581
582 end subroutine ioutil_byteswap_
583
584