NCEPLIBS-bacio  2.5.0
bafrio.f90
Go to the documentation of this file.
1 
19 
44 SUBROUTINE bafrindex(LU,IB,LX,IX)
45  IMPLICIT NONE
46  INTEGER,INTENT(IN):: LU,IB
47  INTEGER,INTENT(INOUT):: LX
48  INTEGER,INTENT(OUT):: IX
49  integer(kind=8) :: LONG_IB,LONG_LX ,LONG_IX=0
50 
51  long_ib=ib
52  long_lx=lx
53  call bafrindexl(lu,long_ib,long_lx,long_ix)
54  lx=int(long_lx)
55  ix=int(long_ix)
56 
57  return
58 end SUBROUTINE bafrindex
59 
83 SUBROUTINE bafrindexl(LU,IB,LX,IX)
84  IMPLICIT NONE
85  INTEGER,INTENT(IN):: LU
86  INTEGER(KIND=8),INTENT(IN):: IB
87  INTEGER(KIND=8),INTENT(INOUT):: LX
88  INTEGER(KIND=8),INTENT(OUT):: IX
89  INTEGER(KIND=8),PARAMETER:: LBCW=4
90  INTEGER(KIND=LBCW):: BCW1,BCW2
91  INTEGER(KIND=8):: KR
92  CHARACTER(16) :: MACHINE_ENDIAN
93  LOGICAL :: DO_BYTESWAP = .true.
94  ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
95  ! COMPARE FIRST BLOCK CONTROL WORD AND TRAILING BLOCK CONTROL WORD
96  IF(lu.GT.0) THEN
97  !
98  !-- set do_byteswap from machine endianness and file endianness
99  CALL chk_endianc(machine_endian)
100  IF( lu<=999) THEN
101  IF( trim(machine_endian)=="big_endian") THEN
102  do_byteswap=.false.
103  ELSEIF( trim(machine_endian)=="little_endian") THEN
104  do_byteswap=.true.
105  ENDIF
106  ELSEIF(lu<=1999) THEN
107  IF( trim(machine_endian)=="big_endian") THEN
108  do_byteswap=.true.
109  ELSEIF( trim(machine_endian)=="little_endian") THEN
110  do_byteswap=.false.
111  ENDIF
112  ENDIF
113  !
114  !
115  !-- read out control word
116  CALL bareadl(lu,ib,lbcw,kr,bcw1)
117  IF(do_byteswap) CALL byteswap(bcw1,lbcw,1)
118  !
119  IF(kr.NE.lbcw) THEN
120  lx=-1
121  ELSE
122  CALL bareadl(lu,ib+lbcw+bcw1,lbcw,kr,bcw2)
123  IF(do_byteswap) CALL byteswap(bcw2,lbcw,1)
124  !
125  IF(kr.NE.lbcw.OR.bcw1.NE.bcw2) THEN
126  lx=-2
127  ELSE
128  lx=bcw1
129  ENDIF
130  ENDIF
131  !
132  !end luif
133  ENDIF
134  ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
135  ! COMPUTE START BYTE FOR THE NEXT FORTRAN RECORD
136  IF(lx.GE.0) ix=ib+lbcw+lx+lbcw
137 END SUBROUTINE bafrindexl
138 
162 SUBROUTINE bafrread(LU,IB,NB,KA,A)
163  IMPLICIT NONE
164  INTEGER,INTENT(IN):: LU,IB,NB
165  INTEGER,INTENT(OUT):: KA
166  CHARACTER,INTENT(OUT):: A(NB)
167  INTEGER(KIND=8) :: LONG_IB,LONG_NB,LONG_KA
168 
169  if((ib<0.and.ib/=-1) .or. nb<0 ) THEN
170  print *,'WRONG: in BAFRREAD starting postion IB or read '// &
171  'data size NB < 0, STOP! Consider use BAFREADL and long integer'
172  ka=0
173  return
174  ENDIF
175  long_ib=ib
176  long_nb=nb
177  CALL bafrreadl(lu,long_ib,long_nb,long_ka,a)
178  ka=int(long_ka)
179 END SUBROUTINE bafrread
180 
202 SUBROUTINE bafrreadl(LU,IB,NB,KA,A)
203  IMPLICIT NONE
204  INTEGER,INTENT(IN):: LU
205  INTEGER(kind=8),INTENT(IN):: IB,NB
206  INTEGER(kind=8),INTENT(OUT):: KA
207  CHARACTER,INTENT(OUT):: A(NB)
208  INTEGER(kind=8),PARAMETER:: LBCW=4
209  INTEGER(kind=8):: LX=0,ix
210  INTEGER(kind=8):: KR
211 
212  ! VALIDATE FORTRAN RECORD
213  CALL bafrindexl(lu,ib,lx,ix)
214 
215  ! READ IF VALID
216  IF(lx.LT.0) THEN
217  ka=lx
218  ELSEIF(lx.LT.nb) THEN
219  ka=-3
220  ELSE
221  CALL bareadl(lu,ib+lbcw,nb,kr,a)
222  IF(kr.NE.nb) THEN
223  ka=-1
224  ELSE
225  ka=lbcw+lx+lbcw
226  ENDIF
227  ENDIF
228 END SUBROUTINE bafrreadl
229 
251 SUBROUTINE bafrwrite(LU,IB,NB,KA,A)
252  IMPLICIT NONE
253  INTEGER,INTENT(IN):: LU,IB,NB
254  INTEGER,INTENT(OUT):: KA
255  CHARACTER,INTENT(IN):: A(NB)
256  INTEGER(KIND=8) :: LONG_IB,LONG_NB,LONG_KA
257 
258  if((ib<0.and.ib/=-1) .or. nb<0 ) THEN
259  print *,'WRONG: in BAFRWRITE starting postion IB or read '// &
260  'data size NB <0, STOP! ' // &
261  'Consider use BAFRRWRITEL and long integer'
262  ka=0
263  return
264  ENDIF
265  long_ib=ib
266  long_nb=nb
267  CALL bafrwritel(lu,long_ib,long_nb,long_ka,a)
268  ka=int(long_ka)
269 END SUBROUTINE bafrwrite
270 
290 SUBROUTINE bafrwritel(LU,IB,NB,KA,A)
291  IMPLICIT NONE
292  INTEGER,INTENT(IN):: LU
293  INTEGER(KIND=8),INTENT(IN):: IB,NB
294  INTEGER(kind=8),INTENT(OUT):: KA
295  CHARACTER,INTENT(IN):: A(NB)
296 
297  INTEGER(kind=8),PARAMETER:: LBCW=4
298  INTEGER(kind=LBCW):: BCW
299  INTEGER(kind=8):: KR
300  CHARACTER(16) :: MACHINE_ENDIAN
301  LOGICAL :: DO_BYTESWAP = .true.
302 
303  ! WRITE DATA BRACKETED BY BLOCK CONTROL WORDS
304 
305  !-- set do_byteswap from machine endianness and file endianness
306  CALL chk_endianc(machine_endian)
307  IF( lu<=999) THEN
308  IF( trim(machine_endian)=="big_endian") THEN
309  do_byteswap=.false.
310  ELSEIF( trim(machine_endian)=="little_endian") THEN
311  do_byteswap=.true.
312  ENDIF
313  ELSEIF(lu<=1999) THEN
314  IF( trim(machine_endian)=="big_endian") THEN
315  do_byteswap=.true.
316  ELSEIF( trim(machine_endian)=="little_endian") THEN
317  do_byteswap=.false.
318  ENDIF
319  ENDIF
320 
321  bcw=int(nb)
322  IF(do_byteswap) CALL byteswap(bcw,lbcw,1)
323  CALL bawritel(lu,ib,lbcw,kr,bcw)
324  IF(kr.NE.lbcw) THEN
325  ka=-1
326  ELSE
327  CALL bawritel(lu,ib+lbcw,nb,kr,a)
328  IF(kr.NE.nb) THEN
329  ka=-1
330  ELSE
331  CALL bawritel(lu,ib+lbcw+nb,lbcw,kr,bcw)
332  IF(kr.NE.lbcw) THEN
333  ka=-1
334  ELSE
335  ka=lbcw+nb+lbcw
336  ENDIF
337  ENDIF
338  ENDIF
339 END SUBROUTINE bafrwritel
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 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 bafrindex(LU, IB, LX, IX)
This subprogram calls bafrindexl() to either read an unformatted fortran record and return its length...
Definition: bafrio.f90:45
subroutine bafrread(LU, IB, NB, KA, A)
This subprogram calls bafread() to read an unformatted fortran record.
Definition: bafrio.f90:163
subroutine bafrindexl(LU, IB, LX, IX)
This subprogram either reads an unformatted fortran record and return its length and start byte of th...
Definition: bafrio.f90:84
subroutine bafrwrite(LU, IB, NB, KA, A)
This subprogram calls bafrwrite() to write an unformatted fortran record.
Definition: bafrio.f90:252
subroutine bafrwritel(LU, IB, NB, KA, A)
This subprogram writes an unformatted fortran record.
Definition: bafrio.f90:291
subroutine bafrreadl(LU, IB, NB, KA, A)
This subprogram reads an unformatted fortran record.
Definition: bafrio.f90:203
subroutine chk_endianc(mendian)
Obtain machine endianness.
Definition: chk_endianc.f90:11