Merge pull request #22 from wirc-sjsu/develop-w21
[WRF-SFIRE.git] / external / io_grib1 / WGRIB / wrtieee.c
blob4e2decfba8c786c6ffa59632584dbc25e69336b2
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stddef.h>
4 #include "grib.h"
7 /* wesley ebisuzaki v1.3
9 * write ieee file -- big endian format
11 * input float *array data to be written
12 * int n size of array
13 * int header 1 for f77 style header 0 for none
14 * (header is 4 byte header
15 * FILE *output output file
17 * v1.2 7/97 buffered, faster
18 * v1.3 2/99 fixed (typo) error in wrtieee_header found by
19 * Bob Farquhar
22 #define BSIZ 1024*4
24 int wrtieee(float *array, int n, int header, FILE *output) {
26 unsigned long int l;
27 int i, nbuf;
28 unsigned char buff[BSIZ];
29 unsigned char h4[4];
31 nbuf = 0;
32 if (header) {
33 l = n * 4;
34 for (i = 0; i < 4; i++) {
35 h4[i] = l & 255;
36 l >>= 8;
38 buff[nbuf++] = h4[3];
39 buff[nbuf++] = h4[2];
40 buff[nbuf++] = h4[1];
41 buff[nbuf++] = h4[0];
43 for (i = 0; i < n; i++) {
44 if (nbuf >= BSIZ) {
45 fwrite(buff, 1, BSIZ, output);
46 nbuf = 0;
48 flt2ieee(array[i], buff + nbuf);
49 nbuf += 4;
51 if (header) {
52 if (nbuf == BSIZ) {
53 fwrite(buff, 1, BSIZ, output);
54 nbuf = 0;
56 buff[nbuf++] = h4[3];
57 buff[nbuf++] = h4[2];
58 buff[nbuf++] = h4[1];
59 buff[nbuf++] = h4[0];
61 if (nbuf) fwrite(buff, 1, nbuf, output);
62 return 0;
65 /* write a big-endian 4 byte integer .. f77 IEEE header */
67 int wrtieee_header(unsigned int n, FILE *output) {
68 unsigned h4[4];
70 h4[0] = n & 255;
71 h4[1] = (n >> 8) & 255;
72 h4[2] = (n >> 16) & 255;
73 h4[3] = (n >> 24) & 255;
75 putc(h4[3],output);
76 putc(h4[2],output);
77 putc(h4[1],output);
78 putc(h4[0],output);
80 return 0;