1 /*  Copyright (C) 1988-2005 by Brian Doty and the Institute
2                   of Global Environment and Society (IGES).
3 
4     See file COPYRIGHT for more information.   */
5 
6 #ifndef GABUFR_H
7 #define GABUFR_H
8 
9 #define GABUFR_X_BITS 6
10 #define GABUFR_Y_BITS 8
11 
12 #define GABUFR_TBL_SIZE   ((1 << GABUFR_X_BITS) * (1 << GABUFR_Y_BITS))
13 
14 #define GABUFR_NUM_TYPE 0
15 #define GABUFR_STR_TYPE 1
16 
17 #define GABUFR_DEF 0
18 #define GABUFR_UNDEF 1
19 
20 typedef struct {
21   int scale;
22   int offset;
23   int width;
24   int datatype; /* flag to indicate numerical or string data */
25   char * description;
26 } gabufr_varinf;
27 
28 
29 typedef struct gabufr_val_struct {
30   int x; /* BUFR ID (F,X,Y) */
31   int y; /* BUFR ID (F,X,Y) */
32   int z; /* replication offset (vert. level), if present, or -1 */
33   char undef; /* set to GABUFR_UNDEF if packed data was all ones */
34   double val; /* data value when datatype is NUM, or DBL_MIN otherwise */
35   char * sval; /* data value when datatype is STR, or NULL otherwise*/
36   struct gabufr_val_struct * next;
37 } gabufr_val;
38 
39 typedef struct {
40   int bufr_edition;
41   int master_tbl_num;
42   int master_tbl_version;
43   int local_tbl_version;
44 } gabufr_tbl_inf;
45 
46 
47 typedef struct gabufr_msg_struct {
48   /* base time for entire message */
49   int year;
50   int month;
51   int day;
52   int hour;
53   int min;
54 
55   int subcnt; /* number of subsets */
56   gabufr_val ** subs; /* array of linked lists, with size nsub;
57 			  *  one linked list per subset in message */
58   struct gabufr_msg_struct * next;
59   int fileindex; /* index of message in file, just for reference */
60 
61   int is_new_tbl; /* if 0, message contains data, otherwise it's a
62 		     replacement BUFR table */
63 
64   /* remainder for use during parsing */
65   char * section0;
66   char * section1;
67   char * section3;
68   char * section4;
69   char * end;
70   gabufr_tbl_inf tbl_inf;
71 
72 } gabufr_msg;
73 
74 typedef struct {
75   gabufr_msg * msgs; /* linked list of decoded messages
76 			(some may be missing if parsing failed */
77   int msgcnt; /* number of messages in file */
78 
79   /* remainder for use during parsing */
80   void * buf;
81   int len;
82 
83 } gabufr_dset;
84 
85 
86 /***** external interface ******/
87 
88 /* Open a BUFR datafile and parse into a gabufr_dset structure */
89 gabufr_dset * gabufr_open(const char * path);
90 
91 /* Open a BUFR datafile and print descriptors. The only data
92  * that are parsed are any replacement tables. */
93 gabufr_dset * gabufr_scan(const char * path);
94 
95 /* Release all memory associated with a parsed dset structure */
96 void gabufr_close(gabufr_dset * dset);
97 
98 /* Free BUFR tables */
99 void gabufr_reset_tbls();
100 
101 /* Set directory where BUFR tables can be found */
102 void gabufr_set_tbl_base_path(const char * path);
103 
104 /***** internals *****/
105 
106 #define GABUFR_OK 0
107 #define GABUFR_ERR 1
108 
109 int gabufr_valid_varid(int f, int x, int y);
110 
111 typedef struct gabufr_varid_struct {
112   int f;
113   int x;
114   int y;
115   struct gabufr_varid_struct * next;
116 } gabufr_varid;
117 
118 /* Read tables into memory */
119 
120 int gabufr_read_tbls(gabufr_tbl_inf * tbl_inf);
121 
122 /* Return a Table B entry */
123 gabufr_varinf * gabufr_get_varinf(int x, int y);
124 
125 /* Return a Table D entry */
126 gabufr_varid * gabufr_get_seq(int x, int y);
127 
128 /* Free storage used by parsing results */
129 void gabufr_free(gabufr_dset * bufrdata);
130 
131 /* Extract BUFR table updates from a decoded NCEP BUFR message */
132 void gabufr_update_ncep_tbl(gabufr_dset * file, gabufr_msg * msg);
133 
134 /* Check if table has been loaded */
135 int gabufr_have_tbl(gabufr_tbl_inf * tbl_inf);
136 
137 
138 /* Free a list of varids */
139 void gabufr_free_varids(gabufr_varid * list);
140 
141 /* Free BUFR tables */
142 void gabufr_reset_tbls();
143 
144 
145 /* Free all varinfo data */
146 void gabufr_reinit();
147 
148 
149 #endif /* GABUFR_H */
150