little-endian and big-endian

Eric DeWeaver deweaver at AOS.WISC.EDU
Tue Jul 12 11:22:56 EDT 2005


Hi Siji,

One way is to open the file in grads using the"byteswap" option
in the control file.  Then you can use "set gxout fwrite" and
possibly "set fwrite <file name>" to make a binary file with
reverse word order.

One thing to keep in mind when writing binary data with grads is
that grads will put a wraparound point in the output file.   In
other words, if you open a file with 144 longitudes starting
at 0E (2.5 degree grid spacing) grads will add a 145th point at
which the longitude is 360 degrees.  So before you use the
display command to write binary data you should put (in this case)
'set x 1 144'.

Another way to byteswap the data is the attached fortran code, which
reverses the word ordering by treating each byte as a character.
swap32(a,n) is a subroutine to swap 32-bit numbers (real or integer),
where a is an array with n numbers in it.  It's an old code, I'm not
sure where I got it.

Best Wishes,

Eric



in the control file

On Tue, 12 Jul 2005, Sijikumar S wrote:

> hello all,
> I have created a binary data file with grads 32-bit little-endian. how
> can i convert this to  big-endian format, since all other data are in
> big-endian. Is there any fortran code to read little-endian and write
> in big-endian.
> thanks
> siji
>
-------------- next part --------------
C234567
      SUBROUTINE SWAP16(A,N)
C
C     REVERSE ORDER OF TWO BYTES IN INTEGER*2
C
      INTEGER * 2  A(N)
      INTEGER * 2  ITEMP
      CHARACTER*1  JTEMP(2)
      CHARACTER*1  KTEMP
C
      EQUIVALENCE  (ITEMP,JTEMP(1))
C
      DO 10 I = 1,N
        ITEMP    = A(I)
        KTEMP    = JTEMP(1)
        JTEMP(1) = JTEMP(2)
        JTEMP(2) = KTEMP
        A(I)     = ITEMP
  10  CONTINUE
      RETURN
      END            
C
      SUBROUTINE SWAP32(A,N)
C
C     REVERSE ORDER OF BYTES IN INTEGER*4 WORD, or REAL*4
C
      INTEGER*4   A(N)
      INTEGER*4   ITEMP
C
      CHARACTER*1 JTEMP(4)
      CHARACTER*1 KTEMP
C
      EQUIVALENCE (JTEMP(1),ITEMP)
C
      DO 10 I = 1,N
        ITEMP    = A(I)
        KTEMP    = JTEMP(4)
        JTEMP(4) = JTEMP(1)
        JTEMP(1) = KTEMP
        KTEMP    = JTEMP(3)
        JTEMP(3) = JTEMP(2)
        JTEMP(2) = KTEMP
        A(I)     = ITEMP
   10 CONTINUE
      RETURN
      END  

      SUBROUTINE SWAP64(A,N)
C
C     REVERSE ORDER OF EIGHT BYTES IN REAL*8 WORD
C
      REAL*8      A(N)
      REAL*8      ITEMP
C
      CHARACTER*1 JTEMP(8)
      CHARACTER*1 KTEMP
C
      EQUIVALENCE (JTEMP(1),ITEMP)
C
      DO 10 I = 1,N
        ITEMP    = A(I)
        KTEMP    = JTEMP(8)
        JTEMP(8) = JTEMP(1)
        JTEMP(1) = KTEMP
        KTEMP    = JTEMP(7)
        JTEMP(7) = JTEMP(2)
        JTEMP(2) = KTEMP
        KTEMP    = JTEMP(6)
        JTEMP(6) = JTEMP(3)
        JTEMP(3) = KTEMP
        KTEMP    = JTEMP(5)
        JTEMP(5) = JTEMP(4)
        JTEMP(4) = KTEMP
        A(I)     = ITEMP
   10 CONTINUE
      RETURN
      END           


More information about the gradsusr mailing list