Update version info for release v4.6.1 (#2122)
[WRF.git] / external / io_grib1 / MEL_grib1 / gribhdr2file.c
blob649c46dc0e3fc5e01a68a34b44fb7c91dae54faf
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "dprints.h" /* for dprints */
4 #include "gribfuncs.h" /* prototypes */
6 #if defined(_WIN32)
7 #include <io.h>
8 #else
9 #include <unistd.h>
10 #endif
14 ************************************************************************
15 * A. FUNCTION gribhdr2file
16 * write out the Grib message stored in GRIB_HDR struct to stream;
17 * if the 'shuffle' flag is set, write each individual section out, else
18 * write 'entire_msg' all at once;
20 * INTERFACE:
21 * int gribhdr2file (gh, fn, errmsg)
23 * ARGUMENTS (I=input, O=output, I&O=input and output):
24 * (I) GRIB_HDR *gh holds the GRIB message to be written out
25 * (I) FILE *stream open strem to write to
26 * (O) char *errmsg array returned empty unless error occurred;
28 * RETURN CODE:
29 * 0> no errors, GRIB file successfully created;
30 * 1> error; errmsg is filled;
31 ************************************************************************
33 #if PROTOTYPE_NEEDED
34 int gribhdr2file ( GRIB_HDR *gh, FILE *stream, char *errmsg)
35 #else
36 int gribhdr2file ( gh, stream, errmsg)
37 GRIB_HDR *gh;
38 FILE *stream;
39 char *errmsg;
40 #endif
42 int fd;
43 int stat;
44 char *func= "gribhdr2file";
46 fd = fileno(stream);
47 if (fd == -1)
49 DPRINT1 ("%s: Invalid file stream encountered.\n", func);
50 return 1;
54 stat = gribhdr2filed ( gh, fd, errmsg);
55 return stat;
62 ************************************************************************
63 * A. FUNCTION gribhdr2file
64 * write out the Grib message stored in GRIB_HDR struct to file
65 * descriptor;
66 * if the 'shuffle' flag is set, write each individual section out, else
67 * write 'entire_msg' all at once;
69 * INTERFACE:
70 * int gribhdr2file (gh, fn, errmsg)
72 * ARGUMENTS (I=input, O=output, I&O=input and output):
73 * (I) GRIB_HDR *gh holds the GRIB message to be written out
74 * (I) int f1 open file descriptor to write to
75 * (O) char *errmsg array returned empty unless error occurred;
77 * RETURN CODE:
78 * 0> no errors, GRIB file successfully created;
79 * 1> error; errmsg is filled;
80 ************************************************************************
82 #if PROTOTYPE_NEEDED
83 int gribhdr2filed ( GRIB_HDR *gh, int f1, char *errmsg)
84 #else
85 int gribhdr2filed ( gh, f1, errmsg)
86 GRIB_HDR *gh;
87 int f1;
88 char *errmsg;
89 #endif
93 * A.0 DEFAULT to error status of 1
95 char *func= "gribhdr2file";
96 int stat=1;
97 char wrstring[4];
98 int check;
102 * A.1 IF (entire msg array is null or msg length is 0)
103 * THEN
104 * RETURN error stat !errmsg filled
105 * ENDIF
107 DPRINT1("Entering %s\n", func);
108 if (gh->entire_msg == NULL || gh->msg_length <= 0) {
109 DPRINT1 ("%s: GRIB_HDR message buffer is null, OR msg_length=0\n",func);
110 sprintf(errmsg,"%s: GRIB_HDR message buffer is null, OR msg_length=0\n",
111 func);
112 goto BYE;
117 * A.2 IF (in Shuffle mode)
118 * THEN
119 * IF (length of EDS/PDS/BDS/EDS is 0) THEN
120 * RETURN error stat !errmsg filled
121 * ENDIF
122 * ENDIF
124 if (gh->shuffled) {
125 if (!gh->ids_len|| !gh->pds_len || !gh->bds_len|| !gh->eds_len) {
126 DPRINT1("%s: Shuffle mode: Zero length encountered, quit\n", func);
127 sprintf(errmsg,
128 "%s: Shuffle mode: Zero length encountered, quit\n", func);
129 goto BYE; }
130 DPRINT1 ("%s: this mesg is in shuffled mode;\n", func);
135 * A.4 IF (in shuffled mode)
136 * A.4.a THEN
138 if (gh->shuffled) {
140 * A.4.a.1 IF (fails to write IDS OR fails to write PDS OR
141 * (GDS exists AND fails to write GDS) OR
142 * (BMS exists AND fails to write BMS) OR
143 * fails to write BDS or fails to write EDS)
144 * THEN
145 * RETURN error stat !errmsg filled
146 * ENDIF
148 if (write (f1, gh->ids_ptr , gh->ids_len) != gh->ids_len)
150 DPRINT1 ("%s: failed to write IDS to file\n", func);
151 sprintf(errmsg,"%s: failed to write IDS to file\n", func);
152 goto BYE;
154 if (write (f1, gh->pds_ptr , gh->pds_len) != gh->pds_len)
156 DPRINT1 ("%s: failed to write PDS to file\n", func);
157 sprintf(errmsg,"%s: failed to write PDS to file\n", func);
158 goto BYE;
160 if (gh->gds_len)
161 if (write (f1, gh->gds_ptr , gh->gds_len) != gh->gds_len)
163 DPRINT1 ("%s: failed to write GDS to file\n", func);
164 sprintf(errmsg,"%s: failed to write GDS to file\n", func);
165 goto BYE;
167 if (gh->bms_len)
168 if (write (f1, gh->bms_ptr , gh->bms_len) != gh->bms_len)
170 DPRINT1 ("%s: failed to write BMS to file\n", func);
171 sprintf(errmsg,"%s: failed to write BMS to file\n", func);
172 goto BYE;
174 if (write (f1, gh->bds_ptr , gh->bds_len) != gh->bds_len)
176 DPRINT1 ("%s: failed to write BDS to file\n", func);
177 sprintf(errmsg,"%s: failed to write BDS to file\n", func);
178 goto BYE;
180 if (write (f1, gh->eds_ptr , gh->eds_len) != gh->eds_len)
182 DPRINT1 ("%s: failed to write EDS to file\n", func);
183 sprintf(errmsg,"%s: failed to write EDS to file\n", func);
184 goto BYE;
186 DPRINT0 ("ALL Sections to written to file successfully\n");
189 * A.4.b ELSE
191 else {
192 DPRINT0 ("Writing gh->entire_msg (non-shuffled)\n");
194 * A.4.b.1 IF (fails to write msg_length byte straight from Entire_msg)
195 * THEN
196 * RETURN error stat !errmsg filled
197 * ENDIF
199 if ((check = write (f1, gh->entire_msg, gh->msg_length)) !=
200 gh->msg_length) {
201 DPRINT1( "%s: failed to write GH's entire Msg to file\n",func);
202 sprintf(errmsg,
203 "%s: failed to write GH's entire Msg to file %d\n",func,check);
204 /* goto BYE; */
206 DPRINT0 ("write GH's entire_msg to file successful\n");
208 * A.4 ENDIF
213 * A.5 DONE, set status to 0 !no errors
215 stat = 0;
217 BYE:
221 * A.7 RETURN with stat
223 DPRINT2 ("Leaving %s, stat=%d;\n", func, stat);
224 return stat;
227 * END OF FUNCTION