1 /* File: write_geogrid.c
3 Sample subroutine to write an array into the geogrid binary format.
5 Side effects: Upon completion, a file named 00001-<NX>.00001-<NY> is
6 created, where <NX> is the argument nx and <NY> is the argument ny,
9 Notes: Depending on the compiler and compiler flags, the name of
10 the write_geogrid() routine may need to be adjusted with respect
11 to the number of trailing underscores when calling from Fortran.
13 Michael G. Duda, NCAR/MMM
21 #define write_geogrid write_geogrid_
23 #ifdef _DOUBLEUNDERSCORE
24 #define write_geogrid write_geogrid__
28 #define LITTLE_ENDIAN 1
31 float * rarray
, /* The array to be written */
32 int * nx
, /* x-dimension of the array */
33 int * ny
, /* y-dimension of the array */
34 int * nz
, /* z-dimension of the array */
35 int * isigned
, /* 0=unsigned data, 1=signed data */
36 int * endian
, /* 0=big endian, 1=little endian */
37 float * scalefactor
, /* value to divide array elements by before truncation to integers */
38 int * wordsize
) /* number of bytes to use for each array element */
44 unsigned int * iarray
;
45 unsigned char * barray
;
49 narray
= (*nx
) * (*ny
) * (*nz
);
51 iarray
= (unsigned int *)malloc(sizeof(int) * narray
);
52 barray
= (unsigned char *)malloc(sizeof(unsigned char) * narray
* (*wordsize
));
54 /* Scale real-valued array by scalefactor and convert to integers */
55 for (i
=0; i
<narray
; i
++)
56 iarray
[i
] = (unsigned int)(rarray
[i
] / (*scalefactor
));
59 Set up byte offsets for each wordsize depending on byte order.
60 A, B, C, D give the offsets of the LSB through MSB (i.e., for
61 word ABCD, A=MSB, D=LSB) in the array from the beginning of a word
63 if (*endian
== BIG_ENDIAN
) {
65 A3
= 0; B3
= 1; C3
= 2;
66 A4
= 0; B4
= 1; C4
= 2; D4
= 3;
70 C3
= 0; B3
= 1; A3
= 2;
71 D4
= 0; C4
= 1; B4
= 2; A4
= 3;
74 /* Place words into storage byte order */
77 for(i
=0; i
<narray
; i
++) {
78 if (iarray
[i
] < 0 && *isigned
) iarray
[i
] += (1 << 8);
79 barray
[(*wordsize
)*i
] = (unsigned char)(iarray
[i
] & 0xff);
84 for(i
=0; i
<narray
; i
++) {
85 if (iarray
[i
] < 0 && *isigned
) iarray
[i
] += (1 << 16);
86 barray
[(*wordsize
)*i
+A2
] = (unsigned char)((iarray
[i
] >> 8) & 0xff);
87 barray
[(*wordsize
)*i
+B2
] = (unsigned char)( iarray
[i
] & 0xff);
92 for(i
=0; i
<narray
; i
++) {
93 if (iarray
[i
] < 0 && *isigned
) iarray
[i
] += (1 << 24);
94 barray
[(*wordsize
)*i
+A3
] = (unsigned char)((iarray
[i
] >> 16) & 0xff);
95 barray
[(*wordsize
)*i
+B3
] = (unsigned char)((iarray
[i
] >> 8) & 0xff);
96 barray
[(*wordsize
)*i
+C3
] = (unsigned char)( iarray
[i
] & 0xff);
101 for(i
=0; i
<narray
; i
++) {
102 if (iarray
[i
] < 0 && *isigned
) iarray
[i
] += (1 << 32);
103 barray
[(*wordsize
)*i
+A4
] = (unsigned char)((iarray
[i
] >> 24) & 0xff);
104 barray
[(*wordsize
)*i
+B4
] = (unsigned char)((iarray
[i
] >> 16) & 0xff);
105 barray
[(*wordsize
)*i
+C4
] = (unsigned char)((iarray
[i
] >> 8) & 0xff);
106 barray
[(*wordsize
)*i
+D4
] = (unsigned char)( iarray
[i
] & 0xff);
111 sprintf(fname
,"%5.5i-%5.5i.%5.5i-%5.5i",1,*nx
,1,*ny
);
113 /* Write array to file */
114 bfile
= fopen(fname
,"wb");
115 fwrite(barray
,sizeof(unsigned char),narray
*(*wordsize
),bfile
);