NCEPLIBS-bacio  2.5.0
baciof.f90
Go to the documentation of this file.
1 
4 
11 
13  INTEGER,PARAMETER :: fddim = 9999
14 
16  INTEGER,DIMENSION(FDDIM),SAVE:: fd = fddim*0
17 
20  INTEGER,DIMENSION(20),SAVE:: baopts = 0
21 
22  INTEGER,PARAMETER:: bacio_openr = 1
23  INTEGER,PARAMETER:: bacio_openw = 2
24  INTEGER,PARAMETER:: bacio_openrw = 4
25  INTEGER,PARAMETER:: bacio_close = 8
26  INTEGER,PARAMETER:: bacio_read = 16
27  INTEGER,PARAMETER:: bacio_write = 32
28  INTEGER,PARAMETER:: bacio_noseek = 64
29  INTEGER,PARAMETER:: bacio_openwt = 128
30  INTEGER,PARAMETER:: bacio_openwa = 256
31 
32  interface
33 
46  integer function baciol(mode, start, size, no, nactual, &
47  fdes, fname, datary) bind(C)
48  use, intrinsic :: iso_c_binding
49  integer(c_int), value, intent(in) :: mode
50  integer(c_long), value, intent(in) :: start
51  integer(c_int), value, intent(in) :: size
52  integer(c_long), value, intent(in) :: no
53  integer(c_long), intent(inout) :: nactual
54  integer(c_int), intent(inout) :: fdes
55  character(kind=C_char), intent(in) :: fname(*)
56  character(kind=C_char), intent(in) :: datary(*)
57  end function baciol
58  end interface
59 END MODULE bacio_module
60 
77 SUBROUTINE baseto(NOPT, VOPT)
78  USE bacio_module
79  IMPLICIT NONE
80  INTEGER NOPT, VOPT
81 
82  IF (nopt .GE. 1 .AND. nopt .LE. 20) baopts(nopt) = vopt
83 END SUBROUTINE baseto
84 
93 SUBROUTINE baopen(LU, CFN, IRET)
94  USE bacio_module
95  use iso_c_binding, only: c_null_char
96  IMPLICIT NONE
97  INTEGER, intent(in) :: LU
98  CHARACTER, intent(in) :: CFN*(*)
99  INTEGER, intent(out) :: IRET
100  integer(kind=8) IB, NB, KA
101  CHARACTER :: A(1)
102 
103  IF (lu .LT. 001 .OR. lu .GT. fddim) THEN
104  iret = 6
105  RETURN
106  ENDIF
107 
108  iret = baciol(bacio_openrw, ib, 1, nb, ka, fd(lu), &
109  trim(cfn)//c_null_char, a)
110 END SUBROUTINE baopen
111 
120 SUBROUTINE baopenr(LU, CFN, IRET)
121  USE bacio_module
122  use iso_c_binding, only: c_null_char
123  IMPLICIT NONE
124  INTEGER, intent(in) :: LU
125  CHARACTER, intent(in) :: CFN*(*)
126  INTEGER, intent(out) :: IRET
127  integer(kind=8) IB, NB, KA
128  CHARACTER :: A(1)
129 
130  IF (lu .LT. 001 .OR. lu .GT. fddim) THEN
131  iret = 6
132  RETURN
133  ENDIF
134 
135  iret = baciol(bacio_openr, ib, 1, nb, ka, fd(lu), &
136  trim(cfn)//c_null_char, a)
137 END SUBROUTINE baopenr
138 
147 SUBROUTINE baopenw(LU, CFN, IRET)
148  USE bacio_module
149  use iso_c_binding, only: c_null_char
150  IMPLICIT NONE
151  INTEGER, intent(in) :: LU
152  CHARACTER, intent(in) :: CFN*(*)
153  INTEGER, intent(out) :: IRET
154  integer(kind=8) IB, NB, KA
155  CHARACTER :: A(1)
156 
157  IF (lu .LT. 001 .OR. lu .GT. fddim) THEN
158  iret = 6
159  RETURN
160  ENDIF
161 
162  iret = baciol(bacio_openw, ib, 1, nb, ka, fd(lu), &
163  trim(cfn)//c_null_char, a)
164 END SUBROUTINE baopenw
165 
174 SUBROUTINE baopenwt(LU, CFN, IRET)
175  USE bacio_module
176  use iso_c_binding, only: c_null_char
177  IMPLICIT NONE
178  INTEGER, intent(in) :: LU
179  CHARACTER, intent(in) :: CFN*(*)
180  INTEGER, intent(out) :: IRET
181  integer(kind=8) IB, NB, KA
182  CHARACTER :: A(1)
183 
184  IF (lu .LT. 001 .OR. lu .GT. fddim) THEN
185  iret = 6
186  RETURN
187  ENDIF
188 
189  iret = baciol(bacio_openwt, ib, 1, nb, ka, fd(lu), &
190  trim(cfn)//c_null_char, a)
191 END SUBROUTINE baopenwt
192 
201 SUBROUTINE baopenwa(LU, CFN, IRET)
202  USE bacio_module
203  use iso_c_binding, only: c_null_char
204  IMPLICIT NONE
205  INTEGER, intent(in) :: LU
206  CHARACTER, intent(in) :: CFN*(*)
207  INTEGER, intent(out) :: IRET
208  integer(kind=8) IB,JB,NB,KA
209  CHARACTER :: A(1)
210 
211  IF (lu .LT. 001 .OR. lu .GT. fddim) THEN
212  iret = 6
213  RETURN
214  ENDIF
215 
216  iret = baciol(bacio_openwa, ib, 1, nb, ka, fd(lu), &
217  trim(cfn)//c_null_char, a)
218 END SUBROUTINE baopenwa
219 
226 SUBROUTINE baclose(LU, IRET)
227  USE bacio_module
228  IMPLICIT NONE
229  INTEGER, intent(in) :: LU
230  INTEGER, intent(out) :: IRET
231  integer(kind=8) IB, NB, KA
232  CHARACTER :: A(1)
233 
234  IF (lu .LT. 001 .OR. lu .GT. fddim) THEN
235  iret = 6
236  RETURN
237  ENDIF
238 
239  iret = baciol(bacio_close, ib, 1, nb, ka, fd(lu), char(0), a)
240  IF (iret .EQ. 0) fd(lu) = 0
241 END SUBROUTINE baclose
242 
272 SUBROUTINE baread(LU, IB, NB, KA, A)
273  IMPLICIT NONE
274  INTEGER,INTENT(IN) :: LU, IB, NB
275  INTEGER,INTENT(OUT) :: KA
276  CHARACTER,INTENT(OUT) :: A(NB)
277  INTEGER(KIND=8) :: LONG_IB, LONG_NB, LONG_KA
278 
279  if (nb < 0) THEN
280  print *,'WRONG: in BAREAD read data size NB < 0, STOP! '//&
281  'Consider using BAREADL and long integer'
282  ka = 0
283  return
284  ENDIF
285  long_ib = ib
286  long_nb = nb
287  CALL bareadl(lu, long_ib, long_nb, long_ka, a)
288  ka = int(long_ka)
289 END SUBROUTINE baread
290 
321 SUBROUTINE bareadl(LU, IB, NB, KA, A)
322  USE bacio_module
323  IMPLICIT NONE
324  INTEGER, intent(in) :: LU
325  INTEGER(kind=8), intent(in) :: IB,NB
326  INTEGER(kind=8), intent(out) :: KA
327  CHARACTER, intent(out) :: A(NB)
328  integer(kind=8), PARAMETER :: NY=4096, my=4
329  INTEGER(KIND=8) NS(MY), NN(MY)
330  INTEGER(kind=8) LONG_0, KY, I, K, IY, JY, LUX
331  INTEGER IRET
332  CHARACTER Y(NY, MY)
333  DATA lux/0/
334  SAVE jy, ns, nn, y, lux
335 
336  IF (lu .LT. 001 .OR. lu .GT. fddim) THEN
337  ka = 0
338  RETURN
339  ENDIF
340  IF (fd(lu) .LE. 0) THEN
341  ka = 0
342  RETURN
343  ENDIF
344  IF (ib .LT. 0 .AND. baopts(1) .EQ. 1) THEN
345  ka = 0
346  RETURN
347  ENDIF
348  IF (nb .LE. 0) THEN
349  ka = 0
350  RETURN
351  ENDIF
352 
353  long_0 = 0
354 
355  ! UNBUFFERED I/O
356  IF (baopts(1) .NE. 1) THEN
357  ka = 0
358  IF (ib .GE. 0) THEN
359  iret = baciol(bacio_read, ib, 1, nb, ka, fd(lu), char(0), a)
360  ELSE
361  iret = baciol(bacio_read + bacio_noseek, long_0, 1, nb, ka,&
362  fd(lu), char(0), a)
363  ENDIF
364 
365  ! BUFFERED I/O
366  ! GET DATA FROM PREVIOUS CALL IF POSSIBLE
367  ELSE
368  ka = 0
369  IF (lux .NE. lu) THEN
370  jy = 0
371  ns = 0
372  nn = 0
373  ELSE
374  DO i = 1, my
375  iy = mod(jy + i - 1, my) + 1
376  ky = ib + ka - ns(iy)
377  IF (ka .LT. nb .AND. ky .GE. long_0 .AND. ky .LT. nn(iy)) THEN
378  k = min(nb - ka, nn(iy) - ky)
379  a(ka + 1:ka + k) = y(ky + 1:ky + k, iy)
380  ka = ka + k
381  ENDIF
382  ENDDO
383  ENDIF
384 
385  ! SET POSITION AND READ BUFFER AND GET DATA
386  IF (ka .LT. nb) THEN
387  lux = abs(lu)
388  jy = mod(jy, my)+1
389  ns(jy) = ib+ka
390  iret = baciol(bacio_read, ns(jy), 1, ny, nn(jy), &
391  fd(lux), char(0), y(1, jy))
392  IF (nn(jy).GT.0) THEN
393  k = min(nb-ka, nn(jy))
394  a(ka+1:ka+k) = y(1:k, jy)
395  ka = ka+k
396  ENDIF
397 
398  ! CONTINUE TO READ BUFFER AND GET DATA
399  DO WHILE(nn(jy).EQ.ny.AND.ka.LT.nb)
400  jy = mod(jy, my)+1
401  ns(jy) = ns(jy)+nn(jy)
402  iret = baciol(bacio_read+bacio_noseek, ns(jy), 1, ny, nn(jy), &
403  fd(lux), char(0), y(1, jy))
404  IF (nn(jy).GT.0) THEN
405  k = min(nb-ka, nn(jy))
406  a(ka+1:ka+k) = y(1:k, jy)
407  ka = ka+k
408  ENDIF
409  ENDDO
410  ENDIF
411  ENDIF
412 END SUBROUTINE bareadl
413 
426 SUBROUTINE bawrite(LU, IB, NB, KA, A)
427  IMPLICIT NONE
428  INTEGER, INTENT(IN) :: LU, IB, NB
429  INTEGER, INTENT(OUT) :: KA
430  CHARACTER, INTENT(IN) :: A(NB)
431  INTEGER(KIND = 8) :: LONG_IB, LONG_NB, LONG_KA
432 
433  if (nb < 0) THEN
434  print *, 'WRONG: in BAWRITE read data size NB <0, STOP! '//&
435  'Consider using BAWRITEL and long integer'
436  ka = 0
437  return
438  ENDIF
439 
440  long_ib = ib
441  long_nb = nb
442  CALL bawritel(lu, long_ib, long_nb, long_ka, a)
443  ka = int(long_ka)
444 END SUBROUTINE bawrite
445 
456 SUBROUTINE bawritel(LU, IB, NB, KA, A)
457  USE bacio_module
458  IMPLICIT NONE
459  INTEGER, intent(in) :: LU
460  INTEGER(kind = 8), intent(in) :: IB, NB
461  INTEGER(kind = 8), intent(out):: KA
462  CHARACTER, intent(in) :: A(NB)
463  INTEGER(kind = 8) :: LONG_0
464  INTEGER :: IRET
465 
466  IF (lu .LT. 001 .OR. lu .GT. fddim) THEN
467  ka = 0
468  RETURN
469  ENDIF
470  IF (fd(lu) .LE. 0) THEN
471  ka = 0
472  RETURN
473  ENDIF
474  IF (nb .LE. 0) THEN
475  ka = 0
476  RETURN
477  ENDIF
478 
479  long_0 = 0
480 
481  IF (ib .GE. 0) THEN
482  ka = 0
483  iret = baciol(bacio_write, ib, 1, nb, ka, fd(lu), char(0), a)
484  ELSE
485  ka = 0
486  iret = baciol(bacio_write+bacio_noseek, long_0, 1, nb, ka, &
487  fd(lu), char(0), a)
488  ENDIF
489 END SUBROUTINE bawritel
490 
506 
508 SUBROUTINE wryte(LU, NB, A)
509  USE bacio_module
510  IMPLICIT NONE
511 
512  INTEGER, intent(in) :: LU
513  INTEGER, intent(in) :: NB
514  CHARACTER, intent(in) :: A(NB)
515  INTEGER(kind = 8) :: LONG_NB
516 
517  IF (nb < 0) THEN
518  print *, 'WRONG: NB: the number of bytes to write <0, STOP!'
519  RETURN
520  ENDIF
521  long_nb = nb
522  CALL wrytel(lu, long_nb, a)
523 END SUBROUTINE wryte
524 
539 
541 SUBROUTINE wrytel(LU, NB, A)
542  USE bacio_module
543  IMPLICIT NONE
544  INTEGER, intent(in) :: LU
545  INTEGER(kind = 8), intent(in) :: NB
546  CHARACTER, INTENT(in) :: A(NB)
547  INTEGER(kind = 8) :: LONG_0, KA
548  INTEGER :: IRET
549 
550  IF (lu .LT. 001 .OR. lu .GT. fddim) THEN
551  ka = 0
552  RETURN
553  ENDIF
554  IF (fd(lu) .LE. 0) THEN
555  RETURN
556  ENDIF
557  IF (nb .LE. 0) THEN
558  RETURN
559  ENDIF
560 
561  long_0 = 0
562  ka = 0
563  iret = baciol(bacio_write + bacio_noseek, long_0, 1, nb, ka, &
564  fd(lu), char(0), a)
565  RETURN
566 END SUBROUTINE wrytel
subroutine baopen(LU, CFN, IRET)
Open a byte-addressable file.
Definition: baciof.f90:94
subroutine wrytel(LU, NB, A)
Write a given number of bytes to an unblocked file.
Definition: baciof.f90:542
subroutine baseto(NOPT, VOPT)
Set options for byte-addressable I/O.
Definition: baciof.f90:78
subroutine bawritel(LU, IB, NB, KA, A)
This subrouytine writes a given number of bytes to an unblocked file, skipping a given number of byte...
Definition: baciof.f90:457
subroutine baread(LU, IB, NB, KA, A)
This subroutine calls bareadl() to read a given number of bytes from an unblocked file,...
Definition: baciof.f90:273
subroutine baopenwt(LU, CFN, IRET)
Open a byte-addressable file for write only with truncation.
Definition: baciof.f90:175
subroutine bareadl(LU, IB, NB, KA, A)
This subrouytine is using updated baciol() I/O package to read a given number of bytes from an unbloc...
Definition: baciof.f90:322
subroutine wryte(LU, NB, A)
This subroutine is calling wrytel() to write a given number of bytes to an unblocked file.
Definition: baciof.f90:509
subroutine baopenwa(LU, CFN, IRET)
Open a byte-addressable file for write only with append.
Definition: baciof.f90:202
subroutine baopenw(LU, CFN, IRET)
Open a byte-addressable file for write only.
Definition: baciof.f90:148
subroutine baclose(LU, IRET)
Close a byte-addressable file.
Definition: baciof.f90:227
subroutine bawrite(LU, IB, NB, KA, A)
This program is calling bawritel() to write a given number of bytes to an unblocked file,...
Definition: baciof.f90:427
subroutine baopenr(LU, CFN, IRET)
Open a byte-addressable file for read only.
Definition: baciof.f90:121
Contains subroutines to read/write binary files.
Definition: baciof.f90:10
integer, parameter bacio_openwt
Open for write only with truncation.
Definition: baciof.f90:29
integer, parameter bacio_openwa
Open for write only with append.
Definition: baciof.f90:30
integer, parameter bacio_read
Read from the file.
Definition: baciof.f90:26
integer, parameter bacio_write
Write to the file.
Definition: baciof.f90:27
integer, parameter bacio_openr
Open file for read only.
Definition: baciof.f90:22
integer, dimension(fddim), save fd
Array IDs of currently open files.
Definition: baciof.f90:16
integer, parameter bacio_close
Close file.
Definition: baciof.f90:25
integer, parameter fddim
Maximum number of open files in bacio library.
Definition: baciof.f90:13
integer, parameter bacio_noseek
Start I/O from previous spot.
Definition: baciof.f90:28
integer, parameter bacio_openw
Open file for write only.
Definition: baciof.f90:23
integer, parameter bacio_openrw
Open file for read or write.
Definition: baciof.f90:24
integer, dimension(20), save baopts
Array of option settings.
Definition: baciof.f90:20