1 /*
2  * (C) Copyright 2005- ECMWF.
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  *
7  * In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
8  * virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
9  */
10 
11 #include "grib_api_internal.h"
12 
13 /*
14    This is used by make_class.pl
15 
16    START_CLASS_DEF
17    CLASS      = accessor
18    SUPER      = grib_accessor_class_long
19    IMPLEMENTS = unpack_long;pack_long
20    IMPLEMENTS = init;dump
21    MEMBERS=const char* verification_yearmonth
22    MEMBERS=const char* base_date
23    MEMBERS=const char* day
24    MEMBERS=const char* hour
25    MEMBERS=const char* fcmonth
26    MEMBERS=const char* check
27    END_CLASS_DEF
28 
29  */
30 
31 /* START_CLASS_IMP */
32 
33 /*
34 
35 Don't edit anything between START_CLASS_IMP and END_CLASS_IMP
36 Instead edit values between START_CLASS_DEF and END_CLASS_DEF
37 or edit "accessor.class" and rerun ./make_class.pl
38 
39 */
40 
41 static int pack_long(grib_accessor*, const long* val, size_t* len);
42 static int unpack_long(grib_accessor*, long* val, size_t* len);
43 static void dump(grib_accessor*, grib_dumper*);
44 static void init(grib_accessor*, const long, grib_arguments*);
45 static void init_class(grib_accessor_class*);
46 
47 typedef struct grib_accessor_g1forecastmonth
48 {
49     grib_accessor att;
50     /* Members defined in gen */
51     /* Members defined in long */
52     /* Members defined in g1forecastmonth */
53     const char* verification_yearmonth;
54     const char* base_date;
55     const char* day;
56     const char* hour;
57     const char* fcmonth;
58     const char* check;
59 } grib_accessor_g1forecastmonth;
60 
61 extern grib_accessor_class* grib_accessor_class_long;
62 
63 static grib_accessor_class _grib_accessor_class_g1forecastmonth = {
64     &grib_accessor_class_long,             /* super                     */
65     "g1forecastmonth",                     /* name                      */
66     sizeof(grib_accessor_g1forecastmonth), /* size                      */
67     0,                                     /* inited */
68     &init_class,                           /* init_class */
69     &init,                                 /* init                      */
70     0,                                     /* post_init                      */
71     0,                                     /* free mem                       */
72     &dump,                                 /* describes himself         */
73     0,                                     /* get length of section     */
74     0,                                     /* get length of string      */
75     0,                                     /* get number of values      */
76     0,                                     /* get number of bytes      */
77     0,                                     /* get offset to bytes           */
78     0,                                     /* get native type               */
79     0,                                     /* get sub_section                */
80     0,                                     /* grib_pack procedures long      */
81     0,                                     /* grib_pack procedures long      */
82     &pack_long,                            /* grib_pack procedures long      */
83     &unpack_long,                          /* grib_unpack procedures long    */
84     0,                                     /* grib_pack procedures double    */
85     0,                                     /* grib_unpack procedures double  */
86     0,                                     /* grib_pack procedures string    */
87     0,                                     /* grib_unpack procedures string  */
88     0,                                     /* grib_pack array procedures string    */
89     0,                                     /* grib_unpack array procedures string  */
90     0,                                     /* grib_pack procedures bytes     */
91     0,                                     /* grib_unpack procedures bytes   */
92     0,                                     /* pack_expression */
93     0,                                     /* notify_change   */
94     0,                                     /* update_size   */
95     0,                                     /* preferred_size   */
96     0,                                     /* resize   */
97     0,                                     /* nearest_smaller_value */
98     0,                                     /* next accessor    */
99     0,                                     /* compare vs. another accessor   */
100     0,                                     /* unpack only ith value          */
101     0,                                     /* unpack a subarray         */
102     0,                                     /* clear          */
103     0,                                     /* clone accessor          */
104 };
105 
106 
107 grib_accessor_class* grib_accessor_class_g1forecastmonth = &_grib_accessor_class_g1forecastmonth;
108 
109 
init_class(grib_accessor_class * c)110 static void init_class(grib_accessor_class* c)
111 {
112     c->next_offset            = (*(c->super))->next_offset;
113     c->string_length          = (*(c->super))->string_length;
114     c->value_count            = (*(c->super))->value_count;
115     c->byte_count             = (*(c->super))->byte_count;
116     c->byte_offset            = (*(c->super))->byte_offset;
117     c->get_native_type        = (*(c->super))->get_native_type;
118     c->sub_section            = (*(c->super))->sub_section;
119     c->pack_missing           = (*(c->super))->pack_missing;
120     c->is_missing             = (*(c->super))->is_missing;
121     c->pack_double            = (*(c->super))->pack_double;
122     c->unpack_double          = (*(c->super))->unpack_double;
123     c->pack_string            = (*(c->super))->pack_string;
124     c->unpack_string          = (*(c->super))->unpack_string;
125     c->pack_string_array      = (*(c->super))->pack_string_array;
126     c->unpack_string_array    = (*(c->super))->unpack_string_array;
127     c->pack_bytes             = (*(c->super))->pack_bytes;
128     c->unpack_bytes           = (*(c->super))->unpack_bytes;
129     c->pack_expression        = (*(c->super))->pack_expression;
130     c->notify_change          = (*(c->super))->notify_change;
131     c->update_size            = (*(c->super))->update_size;
132     c->preferred_size         = (*(c->super))->preferred_size;
133     c->resize                 = (*(c->super))->resize;
134     c->nearest_smaller_value  = (*(c->super))->nearest_smaller_value;
135     c->next                   = (*(c->super))->next;
136     c->compare                = (*(c->super))->compare;
137     c->unpack_double_element  = (*(c->super))->unpack_double_element;
138     c->unpack_double_subarray = (*(c->super))->unpack_double_subarray;
139     c->clear                  = (*(c->super))->clear;
140     c->make_clone             = (*(c->super))->make_clone;
141 }
142 
143 /* END_CLASS_IMP */
144 
init(grib_accessor * a,const long l,grib_arguments * c)145 static void init(grib_accessor* a, const long l, grib_arguments* c)
146 {
147     grib_accessor_g1forecastmonth* self = (grib_accessor_g1forecastmonth*)a;
148     int n                               = 0;
149 
150     self->verification_yearmonth = grib_arguments_get_name(grib_handle_of_accessor(a), c, n++);
151     self->base_date              = grib_arguments_get_name(grib_handle_of_accessor(a), c, n++);
152     self->day                    = grib_arguments_get_name(grib_handle_of_accessor(a), c, n++);
153     self->hour                   = grib_arguments_get_name(grib_handle_of_accessor(a), c, n++);
154     self->fcmonth                = grib_arguments_get_name(grib_handle_of_accessor(a), c, n++);
155     self->check                  = grib_arguments_get_name(grib_handle_of_accessor(a), c, n++);
156 }
157 
dump(grib_accessor * a,grib_dumper * dumper)158 static void dump(grib_accessor* a, grib_dumper* dumper)
159 {
160     grib_dump_long(dumper, a, NULL);
161 }
162 
unpack_long(grib_accessor * a,long * val,size_t * len)163 static int unpack_long(grib_accessor* a, long* val, size_t* len)
164 {
165     int ret                             = 0;
166     grib_accessor_g1forecastmonth* self = (grib_accessor_g1forecastmonth*)a;
167 
168     long verification_yearmonth = 0;
169     long base_yearmonth         = 0;
170     long base_date              = 0;
171     long day                    = 0;
172     long hour                   = 0;
173 
174     long vyear  = 0;
175     long vmonth = 0;
176     long byear  = 0;
177     long bmonth = 0;
178 
179     long fcmonth           = 0;
180     long gribForecastMonth = 0;
181     long check             = 0;
182 
183     if ((ret = grib_get_long_internal(grib_handle_of_accessor(a),
184                                       self->verification_yearmonth, &verification_yearmonth)) != GRIB_SUCCESS)
185         return ret;
186     if ((ret = grib_get_long_internal(grib_handle_of_accessor(a), self->base_date, &base_date)) != GRIB_SUCCESS)
187         return ret;
188     if ((ret = grib_get_long_internal(grib_handle_of_accessor(a), self->day, &day)) != GRIB_SUCCESS)
189         return ret;
190     if ((ret = grib_get_long_internal(grib_handle_of_accessor(a), self->hour, &hour)) != GRIB_SUCCESS)
191         return ret;
192     if ((ret = grib_get_long_internal(grib_handle_of_accessor(a), self->fcmonth, &gribForecastMonth)) != GRIB_SUCCESS)
193         return ret;
194     if ((ret = grib_get_long_internal(grib_handle_of_accessor(a), self->check, &check)) != GRIB_SUCCESS)
195         return ret;
196 
197     base_yearmonth = base_date / 100;
198 
199     vyear  = verification_yearmonth / 100;
200     vmonth = verification_yearmonth % 100;
201     byear  = base_yearmonth / 100;
202     bmonth = base_yearmonth % 100;
203 
204     fcmonth = (vyear - byear) * 12 + (vmonth - bmonth);
205     if (day == 1 && hour == 0)
206         fcmonth++;
207 
208     if (gribForecastMonth != 0 && gribForecastMonth != fcmonth) {
209         if (check) {
210             grib_context_log(a->context, GRIB_LOG_FATAL, "%s=%ld (%s-%s)=%ld", self->fcmonth,
211                              gribForecastMonth, self->base_date, self->verification_yearmonth, fcmonth);
212             Assert(gribForecastMonth == fcmonth);
213         }
214         else {
215             *val = gribForecastMonth;
216             return GRIB_SUCCESS;
217         }
218     }
219 
220     *val = fcmonth;
221 
222     return GRIB_SUCCESS;
223 }
224 
225 /* TODO: Check for a valid date */
pack_long(grib_accessor * a,const long * val,size_t * len)226 static int pack_long(grib_accessor* a, const long* val, size_t* len)
227 {
228     grib_accessor_g1forecastmonth* self = (grib_accessor_g1forecastmonth*)a;
229     return grib_set_long_internal(grib_handle_of_accessor(a), self->fcmonth, *val);
230 }
231