Created a tag for the 2012 HWRF baseline tests.
[WPS-merge.git] / hwrf-baseline-20120103-1354 / geogrid / src / write_geogrid.c
blobf615682cb2f3d9cd5d6d11da68be0929a7ec6602
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,
7 both in i5.5 format.
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
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <string.h>
20 #ifdef _UNDERSCORE
21 #define write_geogrid write_geogrid_
22 #endif
23 #ifdef _DOUBLEUNDERSCORE
24 #define write_geogrid write_geogrid__
25 #endif
27 #define BIG_ENDIAN 0
28 #define LITTLE_ENDIAN 1
30 int write_geogrid(
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 */
40 int i, narray;
41 int A2, B2;
42 int A3, B3, C3;
43 int A4, B4, C4, D4;
44 unsigned int * iarray;
45 unsigned char * barray;
46 char fname[24];
47 FILE * bfile;
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));
58 /*
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) {
64 A2 = 0; B2 = 1;
65 A3 = 0; B3 = 1; C3 = 2;
66 A4 = 0; B4 = 1; C4 = 2; D4 = 3;
68 else {
69 B2 = 0; A2 = 1;
70 C3 = 0; B3 = 1; A3 = 2;
71 D4 = 0; C4 = 1; B4 = 2; A4 = 3;
74 /* Place words into storage byte order */
75 switch(*wordsize) {
76 case 1:
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);
81 break;
83 case 2:
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);
89 break;
91 case 3:
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);
98 break;
100 case 4:
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);
108 break;
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);
116 fclose(bfile);
118 free(iarray);
119 free(barray);
121 return 0;