3 #include "dprints.h" /* for dprints */
4 #include "gribfuncs.h" /* prototypes */
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;
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;
29 * 0> no errors, GRIB file successfully created;
30 * 1> error; errmsg is filled;
31 ************************************************************************
34 int gribhdr2file ( GRIB_HDR
*gh
, FILE *stream
, char *errmsg
)
36 int gribhdr2file ( gh
, stream
, errmsg
)
44 char *func
= "gribhdr2file";
49 DPRINT1 ("%s: Invalid file stream encountered.\n", func
);
54 stat
= gribhdr2filed ( gh
, fd
, errmsg
);
62 ************************************************************************
63 * A. FUNCTION gribhdr2file
64 * write out the Grib message stored in GRIB_HDR struct to file
66 * if the 'shuffle' flag is set, write each individual section out, else
67 * write 'entire_msg' all at once;
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;
78 * 0> no errors, GRIB file successfully created;
79 * 1> error; errmsg is filled;
80 ************************************************************************
83 int gribhdr2filed ( GRIB_HDR
*gh
, int f1
, char *errmsg
)
85 int gribhdr2filed ( gh
, f1
, errmsg
)
93 * A.0 DEFAULT to error status of 1
95 char *func
= "gribhdr2file";
102 * A.1 IF (entire msg array is null or msg length is 0)
104 * RETURN error stat !errmsg filled
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",
117 * A.2 IF (in Shuffle mode)
119 * IF (length of EDS/PDS/BDS/EDS is 0) THEN
120 * RETURN error stat !errmsg filled
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
);
128 "%s: Shuffle mode: Zero length encountered, quit\n", func
);
130 DPRINT1 ("%s: this mesg is in shuffled mode;\n", func
);
135 * A.4 IF (in shuffled mode)
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)
145 * RETURN error stat !errmsg filled
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
);
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
);
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
);
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
);
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
);
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
);
186 DPRINT0 ("ALL Sections to written to file successfully\n");
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)
196 * RETURN error stat !errmsg filled
199 if ((check
= write (f1
, gh
->entire_msg
, gh
->msg_length
)) !=
201 DPRINT1( "%s: failed to write GH's entire Msg to file\n",func
);
203 "%s: failed to write GH's entire Msg to file %d\n",func
,check
);
206 DPRINT0 ("write GH's entire_msg to file successful\n");
213 * A.5 DONE, set status to 0 !no errors
221 * A.7 RETURN with stat
223 DPRINT2 ("Leaving %s, stat=%d;\n", func
, stat
);