source: trunk/WRF.COMMON/WRFV3/external/io_grib1/MEL_grib1/gribhdr2file.c

Last change on this file was 2759, checked in by aslmd, 2 years ago

adding unmodified code from WRFV3.0.1.1, expurged from useless data +1M size

File size: 6.1 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include "dprints.h"            /* for dprints */
4#include "gribfuncs.h"          /* prototypes */
5/*
6*
7************************************************************************
8* A.  FUNCTION  gribhdr2file
9*       write out the Grib message stored in GRIB_HDR struct to stream;
10*       if the 'shuffle' flag is set, write each individual section out, else
11*       write 'entire_msg' all at once;
12*               
13*    INTERFACE:
14*       int    gribhdr2file (gh, fn, errmsg)
15*
16*    ARGUMENTS (I=input, O=output, I&O=input and output):
17*      (I) GRIB_HDR *gh    holds the GRIB message to be written out
18*      (I) FILE *stream    open strem to write to
19*      (O) char *errmsg    array returned empty unless error occurred;
20*
21*     RETURN CODE:
22*     0>  no errors,  GRIB file successfully created;
23*     1>  error; errmsg is filled;
24************************************************************************
25*/
26#if PROTOTYPE_NEEDED
27int    gribhdr2file ( GRIB_HDR *gh, FILE *stream, char *errmsg)
28#else
29int    gribhdr2file ( gh, stream, errmsg)
30                        GRIB_HDR *gh; 
31                        FILE *stream; 
32                        char *errmsg;
33#endif
34{
35  int fd;
36  int stat;
37  char *func= "gribhdr2file";
38
39  fd = fileno(stream);
40  if (fd == -1) 
41    {
42      DPRINT1 ("%s: Invalid file stream encountered.\n", func);
43      return 1;
44     
45    }
46
47  stat = gribhdr2filed ( gh, fd, errmsg);
48  return stat;
49 
50}
51
52
53/*
54*
55************************************************************************
56* A.  FUNCTION  gribhdr2file
57*       write out the Grib message stored in GRIB_HDR struct to file
58*       descriptor;
59*       if the 'shuffle' flag is set, write each individual section out, else
60*       write 'entire_msg' all at once;
61*               
62*    INTERFACE:
63*       int    gribhdr2file (gh, fn, errmsg)
64*
65*    ARGUMENTS (I=input, O=output, I&O=input and output):
66*      (I) GRIB_HDR *gh    holds the GRIB message to be written out
67*      (I) int f1          open file descriptor to write to
68*      (O) char *errmsg    array returned empty unless error occurred;
69*
70*     RETURN CODE:
71*     0>  no errors,  GRIB file successfully created;
72*     1>  error; errmsg is filled;
73************************************************************************
74*/
75#if PROTOTYPE_NEEDED
76int    gribhdr2filed ( GRIB_HDR *gh, int f1, char *errmsg)
77#else
78int    gribhdr2filed ( gh, f1, errmsg)
79                        GRIB_HDR *gh; 
80                        int f1; 
81                        char *errmsg;
82#endif
83{
84/*
85*
86* A.0     DEFAULT to error status of 1
87*/
88char *func= "gribhdr2file";
89int  stat=1;           
90char wrstring[4];
91 int check;
92
93/*
94*
95* A.1     IF (entire msg array is null or msg length is 0)
96*         THEN
97*            RETURN error stat !errmsg filled
98*         ENDIF
99*/
100  DPRINT1("Entering %s\n", func);
101  if (gh->entire_msg == NULL || gh->msg_length <= 0) {
102        DPRINT1 ("%s: GRIB_HDR message buffer is null, OR msg_length=0\n",func);
103        sprintf(errmsg,"%s: GRIB_HDR message buffer is null, OR msg_length=0\n",
104        func);
105        goto BYE;
106        }       
107
108/*
109*
110* A.2     IF (in Shuffle mode)
111*         THEN
112*            IF (length of EDS/PDS/BDS/EDS is 0) THEN
113*               RETURN error stat !errmsg filled
114*            ENDIF
115*         ENDIF
116*/
117  if (gh->shuffled) {
118        if (!gh->ids_len|| !gh->pds_len || !gh->bds_len|| !gh->eds_len) {
119           DPRINT1("%s:  Shuffle mode: Zero length encountered, quit\n", func);
120           sprintf(errmsg,
121           "%s:  Shuffle mode: Zero length encountered, quit\n", func);
122           goto BYE; }
123        DPRINT1 ("%s:   this mesg is in shuffled mode;\n", func);
124        }
125
126/*
127*
128* A.4     IF (in shuffled mode)
129* A.4.a   THEN
130*/
131  if (gh->shuffled) {
132/*
133* A.4.a.1    IF (fails to write IDS OR fails to write PDS OR
134*                (GDS exists AND fails to write GDS) OR
135*                (BMS exists AND fails to write BMS) OR
136*                fails to write BDS or fails to write EDS)
137*            THEN
138*               RETURN error stat !errmsg filled
139*            ENDIF
140*/
141     if (write (f1, gh->ids_ptr , gh->ids_len) != gh->ids_len)
142        {
143          DPRINT1 ("%s:  failed to write IDS to file\n", func);
144          sprintf(errmsg,"%s:  failed to write IDS to file\n", func);
145          goto BYE;
146        }
147     if (write (f1, gh->pds_ptr , gh->pds_len) != gh->pds_len)
148        {
149          DPRINT1 ("%s:  failed to write PDS to file\n", func);
150          sprintf(errmsg,"%s:  failed to write PDS to file\n", func);
151          goto BYE;
152        }
153     if (gh->gds_len)
154     if (write (f1, gh->gds_ptr , gh->gds_len) != gh->gds_len)
155        {
156          DPRINT1 ("%s:  failed to write GDS to file\n", func);
157          sprintf(errmsg,"%s:  failed to write GDS to file\n", func);
158          goto BYE;
159        }
160     if (gh->bms_len)
161     if (write (f1, gh->bms_ptr , gh->bms_len) != gh->bms_len)
162        {
163          DPRINT1 ("%s:  failed to write BMS to file\n", func);
164          sprintf(errmsg,"%s:  failed to write BMS to file\n", func);
165          goto BYE;
166        }
167     if (write (f1, gh->bds_ptr , gh->bds_len) != gh->bds_len)
168        {
169          DPRINT1 ("%s:  failed to write BDS to file\n", func);
170          sprintf(errmsg,"%s:  failed to write BDS to file\n", func);
171          goto BYE;
172        }
173     if (write (f1, gh->eds_ptr , gh->eds_len) != gh->eds_len)
174        {
175          DPRINT1 ("%s:  failed to write EDS to file\n", func);
176          sprintf(errmsg,"%s:  failed to write EDS to file\n", func);
177          goto BYE;
178        }
179     DPRINT0 ("ALL Sections to written to file successfully\n");
180     }
181/*
182* A.4.b   ELSE
183*/
184   else {
185        DPRINT0 ("Writing gh->entire_msg (non-shuffled)\n");
186/*
187* A.4.b.1    IF (fails to write msg_length byte straight from Entire_msg)
188*            THEN
189*                RETURN error stat !errmsg filled
190*            ENDIF
191*/
192        if ((check = write (f1, gh->entire_msg, gh->msg_length)) != 
193            gh->msg_length) {
194            DPRINT1( "%s:  failed to write GH's entire Msg to file\n",func);
195            sprintf(errmsg,
196            "%s:  failed to write GH's entire Msg to file %d\n",func,check);
197            /*      goto BYE;  */
198           }
199        DPRINT0 ("write GH's entire_msg to file successful\n");
200/*
201* A.4     ENDIF
202*/
203        }
204/*
205*
206* A.5     DONE, set status to 0  !no errors
207*/
208  stat = 0;
209
210BYE:
211
212/*
213*
214* A.7     RETURN with stat
215*/
216  DPRINT2 ("Leaving %s, stat=%d;\n", func, stat);
217  return stat;
218/*
219*
220* END OF FUNCTION
221*/
222}
Note: See TracBrowser for help on using the repository browser.