NCEPLIBS-bacio  2.6.0
bafrio.F90
Go to the documentation of this file.
1 
13 
32 SUBROUTINE bafrindex(LU,IB,LX,IX)
33  IMPLICIT NONE
34  INTEGER,INTENT(IN):: LU,IB
35  INTEGER,INTENT(INOUT):: LX
36  INTEGER,INTENT(OUT):: IX
37  integer(kind=8) :: LONG_IB,LONG_LX ,LONG_IX=0
38 
39  long_ib=ib
40  long_lx=lx
41  call bafrindexl(lu,long_ib,long_lx,long_ix)
42  lx=int(long_lx)
43  ix=int(long_ix)
44 
45  return
46 end SUBROUTINE bafrindex
47 
64 SUBROUTINE bafrindexl(LU,IB,LX,IX)
65  IMPLICIT NONE
66  INTEGER,INTENT(IN):: LU
67  INTEGER(KIND=8),INTENT(IN):: IB
68  INTEGER(KIND=8),INTENT(INOUT):: LX
69  INTEGER(KIND=8),INTENT(OUT):: IX
70  INTEGER(KIND=8),PARAMETER:: LBCW=4
71  INTEGER(KIND=LBCW):: BCW1,BCW2
72  INTEGER(KIND=8):: KR
73  CHARACTER(16) :: MACHINE_ENDIAN
74  LOGICAL :: DO_BYTESWAP = .true.
75  ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
76  ! COMPARE FIRST BLOCK CONTROL WORD AND TRAILING BLOCK CONTROL WORD
77  IF(lu.GT.0) THEN
78  !
79  !-- set do_byteswap from machine endianness and file endianness
80  CALL chk_endianc(machine_endian)
81  IF( lu<=999) THEN
82  IF( trim(machine_endian)=="big_endian") THEN
83  do_byteswap=.false.
84  ELSEIF( trim(machine_endian)=="little_endian") THEN
85  do_byteswap=.true.
86  ENDIF
87  ELSEIF(lu<=1999) THEN
88  IF( trim(machine_endian)=="big_endian") THEN
89  do_byteswap=.true.
90  ELSEIF( trim(machine_endian)=="little_endian") THEN
91  do_byteswap=.false.
92  ENDIF
93  ENDIF
94  !
95  !
96  !-- read out control word
97  CALL bareadl(lu,ib,lbcw,kr,bcw1)
98  IF(do_byteswap) CALL byteswap(bcw1,lbcw,1)
99  !
100  IF(kr.NE.lbcw) THEN
101  lx=-1
102  ELSE
103  CALL bareadl(lu,ib+lbcw+bcw1,lbcw,kr,bcw2)
104  IF(do_byteswap) CALL byteswap(bcw2,lbcw,1)
105  !
106  IF(kr.NE.lbcw.OR.bcw1.NE.bcw2) THEN
107  lx=-2
108  ELSE
109  lx=bcw1
110  ENDIF
111  ENDIF
112  !
113  !end luif
114  ENDIF
115 
116  ! COMPUTE START BYTE FOR THE NEXT FORTRAN RECORD
117  IF(lx.GE.0) ix=ib+lbcw+lx+lbcw
118 END SUBROUTINE bafrindexl
119 
136 SUBROUTINE bafrread(LU,IB,NB,KA,A)
137  IMPLICIT NONE
138  INTEGER,INTENT(IN):: LU,IB,NB
139  INTEGER,INTENT(OUT):: KA
140  CHARACTER,INTENT(OUT):: A(NB)
141  INTEGER(KIND=8) :: LONG_IB,LONG_NB,LONG_KA
142 
143  if((ib<0.and.ib/=-1) .or. nb<0 ) THEN
144  print *,'WRONG: in BAFRREAD starting postion IB or read '// &
145  'data size NB < 0, STOP! Consider use BAFREADL and long integer'
146  ka=0
147  return
148  ENDIF
149  long_ib=ib
150  long_nb=nb
151  CALL bafrreadl(lu,long_ib,long_nb,long_ka,a)
152  ka=int(long_ka)
153 END SUBROUTINE bafrread
154 
169 SUBROUTINE bafrreadl(LU,IB,NB,KA,A)
170  IMPLICIT NONE
171  INTEGER,INTENT(IN):: LU
172  INTEGER(kind=8),INTENT(IN):: IB,NB
173  INTEGER(kind=8),INTENT(OUT):: KA
174  CHARACTER,INTENT(OUT):: A(NB)
175  INTEGER(kind=8),PARAMETER:: LBCW=4
176  INTEGER(kind=8):: LX=0,ix
177  INTEGER(kind=8):: KR
178 
179  ! VALIDATE FORTRAN RECORD
180  CALL bafrindexl(lu,ib,lx,ix)
181 
182  ! READ IF VALID
183  IF(lx.LT.0) THEN
184  ka=lx
185  ELSEIF(lx.LT.nb) THEN
186  ka=-3
187  ELSE
188  CALL bareadl(lu,ib+lbcw,nb,kr,a)
189  IF(kr.NE.nb) THEN
190  ka=-1
191  ELSE
192  ka=lbcw+lx+lbcw
193  ENDIF
194  ENDIF
195 END SUBROUTINE bafrreadl
196 
211 SUBROUTINE bafrwrite(LU,IB,NB,KA,A)
212  IMPLICIT NONE
213  INTEGER,INTENT(IN):: LU,IB,NB
214  INTEGER,INTENT(OUT):: KA
215  CHARACTER,INTENT(IN):: A(NB)
216  INTEGER(KIND=8) :: LONG_IB,LONG_NB,LONG_KA
217 
218  if((ib<0.and.ib/=-1) .or. nb<0 ) THEN
219  print *,'WRONG: in BAFRWRITE starting postion IB or read '// &
220  'data size NB <0, STOP! ' // &
221  'Consider use BAFRRWRITEL and long integer'
222  ka=0
223  return
224  ENDIF
225  long_ib=ib
226  long_nb=nb
227  CALL bafrwritel(lu,long_ib,long_nb,long_ka,a)
228  ka=int(long_ka)
229 END SUBROUTINE bafrwrite
230 
243 SUBROUTINE bafrwritel(LU,IB,NB,KA,A)
244  IMPLICIT NONE
245  INTEGER,INTENT(IN):: LU
246  INTEGER(KIND=8),INTENT(IN):: IB,NB
247  INTEGER(kind=8),INTENT(OUT):: KA
248  CHARACTER,INTENT(IN):: A(NB)
249 
250  INTEGER(kind=8),PARAMETER:: LBCW=4
251  INTEGER(kind=LBCW):: BCW
252  INTEGER(kind=8):: KR
253  CHARACTER(16) :: MACHINE_ENDIAN
254  LOGICAL :: DO_BYTESWAP = .true.
255 
256  ! WRITE DATA BRACKETED BY BLOCK CONTROL WORDS
257 
258  !-- set do_byteswap from machine endianness and file endianness
259  CALL chk_endianc(machine_endian)
260  IF( lu<=999) THEN
261  IF( trim(machine_endian)=="big_endian") THEN
262  do_byteswap=.false.
263  ELSEIF( trim(machine_endian)=="little_endian") THEN
264  do_byteswap=.true.
265  ENDIF
266  ELSEIF(lu<=1999) THEN
267  IF( trim(machine_endian)=="big_endian") THEN
268  do_byteswap=.true.
269  ELSEIF( trim(machine_endian)=="little_endian") THEN
270  do_byteswap=.false.
271  ENDIF
272  ENDIF
273 
274  bcw=int(nb)
275  IF(do_byteswap) CALL byteswap(bcw,lbcw,1)
276  CALL bawritel(lu,ib,lbcw,kr,bcw)
277  IF(kr.NE.lbcw) THEN
278  ka=-1
279  ELSE
280  CALL bawritel(lu,ib+lbcw,nb,kr,a)
281  IF(kr.NE.nb) THEN
282  ka=-1
283  ELSE
284  CALL bawritel(lu,ib+lbcw+nb,lbcw,kr,bcw)
285  IF(kr.NE.lbcw) THEN
286  ka=-1
287  ELSE
288  ka=lbcw+nb+lbcw
289  ENDIF
290  ENDIF
291  ENDIF
292 END SUBROUTINE bafrwritel
bawritel
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:537
bafrwritel
subroutine bafrwritel(LU, IB, NB, KA, A)
This subprogram writes an unformatted fortran record.
Definition: bafrio.F90:244
bafrindex
subroutine bafrindex(LU, IB, LX, IX)
This subprogram calls bafrindexl() to either read an unformatted fortran record and return its length...
Definition: bafrio.F90:33
bafrindexl
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:65
chk_endianc
subroutine chk_endianc(mendian)
Obtain machine endianness.
Definition: chk_endianc.F90:11
bafrreadl
subroutine bafrreadl(LU, IB, NB, KA, A)
This subprogram reads an unformatted fortran record.
Definition: bafrio.F90:170
bafrwrite
subroutine bafrwrite(LU, IB, NB, KA, A)
This subprogram calls bafrwrite() to write an unformatted fortran record.
Definition: bafrio.F90:212
bafrread
subroutine bafrread(LU, IB, NB, KA, A)
This subprogram calls bafread() to read an unformatted fortran record.
Definition: bafrio.F90:137
bareadl
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:401