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_double
19    IMPLEMENTS = unpack_double; pack_double
20    IMPLEMENTS = unpack_long; pack_long
21    IMPLEMENTS = unpack_string; pack_string
22    IMPLEMENTS = dump
23    IMPLEMENTS = init
24    IMPLEMENTS = pack_expression
25    MEMBERS    = const char *year
26    MEMBERS    = const char *month
27    MEMBERS    = const char *day
28    MEMBERS    = const char *hour
29    MEMBERS    = const char *minute
30    MEMBERS    = const char *second
31    MEMBERS    = const char *ymd
32    MEMBERS    = const char *hms
33    MEMBERS    = char sep[5]
34    END_CLASS_DEF
35 
36  */
37 
38 /* START_CLASS_IMP */
39 
40 /*
41 
42 Don't edit anything between START_CLASS_IMP and END_CLASS_IMP
43 Instead edit values between START_CLASS_DEF and END_CLASS_DEF
44 or edit "accessor.class" and rerun ./make_class.pl
45 
46 */
47 
48 static int pack_double(grib_accessor*, const double* val, size_t* len);
49 static int pack_long(grib_accessor*, const long* val, size_t* len);
50 static int pack_string(grib_accessor*, const char*, size_t* len);
51 static int pack_expression(grib_accessor*, grib_expression*);
52 static int unpack_double(grib_accessor*, double* val, size_t* len);
53 static int unpack_long(grib_accessor*, long* val, size_t* len);
54 static int unpack_string(grib_accessor*, char*, size_t* len);
55 static void dump(grib_accessor*, grib_dumper*);
56 static void init(grib_accessor*, const long, grib_arguments*);
57 static void init_class(grib_accessor_class*);
58 
59 typedef struct grib_accessor_julian_date
60 {
61     grib_accessor att;
62     /* Members defined in gen */
63     /* Members defined in double */
64     /* Members defined in julian_date */
65     const char* year;
66     const char* month;
67     const char* day;
68     const char* hour;
69     const char* minute;
70     const char* second;
71     const char* ymd;
72     const char* hms;
73     char sep[5];
74 } grib_accessor_julian_date;
75 
76 extern grib_accessor_class* grib_accessor_class_double;
77 
78 static grib_accessor_class _grib_accessor_class_julian_date = {
79     &grib_accessor_class_double,       /* super                     */
80     "julian_date",                     /* name                      */
81     sizeof(grib_accessor_julian_date), /* size                      */
82     0,                                 /* inited */
83     &init_class,                       /* init_class */
84     &init,                             /* init                      */
85     0,                                 /* post_init                      */
86     0,                                 /* free mem                       */
87     &dump,                             /* describes himself         */
88     0,                                 /* get length of section     */
89     0,                                 /* get length of string      */
90     0,                                 /* get number of values      */
91     0,                                 /* get number of bytes      */
92     0,                                 /* get offset to bytes           */
93     0,                                 /* get native type               */
94     0,                                 /* get sub_section                */
95     0,                                 /* grib_pack procedures long      */
96     0,                                 /* grib_pack procedures long      */
97     &pack_long,                        /* grib_pack procedures long      */
98     &unpack_long,                      /* grib_unpack procedures long    */
99     &pack_double,                      /* grib_pack procedures double    */
100     &unpack_double,                    /* grib_unpack procedures double  */
101     &pack_string,                      /* grib_pack procedures string    */
102     &unpack_string,                    /* grib_unpack procedures string  */
103     0,                                 /* grib_pack array procedures string    */
104     0,                                 /* grib_unpack array procedures string  */
105     0,                                 /* grib_pack procedures bytes     */
106     0,                                 /* grib_unpack procedures bytes   */
107     &pack_expression,                  /* pack_expression */
108     0,                                 /* notify_change   */
109     0,                                 /* update_size   */
110     0,                                 /* preferred_size   */
111     0,                                 /* resize   */
112     0,                                 /* nearest_smaller_value */
113     0,                                 /* next accessor    */
114     0,                                 /* compare vs. another accessor   */
115     0,                                 /* unpack only ith value          */
116     0,                                 /* unpack a subarray         */
117     0,                                 /* clear          */
118     0,                                 /* clone accessor          */
119 };
120 
121 
122 grib_accessor_class* grib_accessor_class_julian_date = &_grib_accessor_class_julian_date;
123 
124 
init_class(grib_accessor_class * c)125 static void init_class(grib_accessor_class* c)
126 {
127     c->next_offset            = (*(c->super))->next_offset;
128     c->string_length          = (*(c->super))->string_length;
129     c->value_count            = (*(c->super))->value_count;
130     c->byte_count             = (*(c->super))->byte_count;
131     c->byte_offset            = (*(c->super))->byte_offset;
132     c->get_native_type        = (*(c->super))->get_native_type;
133     c->sub_section            = (*(c->super))->sub_section;
134     c->pack_missing           = (*(c->super))->pack_missing;
135     c->is_missing             = (*(c->super))->is_missing;
136     c->pack_string_array      = (*(c->super))->pack_string_array;
137     c->unpack_string_array    = (*(c->super))->unpack_string_array;
138     c->pack_bytes             = (*(c->super))->pack_bytes;
139     c->unpack_bytes           = (*(c->super))->unpack_bytes;
140     c->notify_change          = (*(c->super))->notify_change;
141     c->update_size            = (*(c->super))->update_size;
142     c->preferred_size         = (*(c->super))->preferred_size;
143     c->resize                 = (*(c->super))->resize;
144     c->nearest_smaller_value  = (*(c->super))->nearest_smaller_value;
145     c->next                   = (*(c->super))->next;
146     c->compare                = (*(c->super))->compare;
147     c->unpack_double_element  = (*(c->super))->unpack_double_element;
148     c->unpack_double_subarray = (*(c->super))->unpack_double_subarray;
149     c->clear                  = (*(c->super))->clear;
150     c->make_clone             = (*(c->super))->make_clone;
151 }
152 
153 /* END_CLASS_IMP */
154 
155 
init(grib_accessor * a,const long l,grib_arguments * c)156 static void init(grib_accessor* a, const long l, grib_arguments* c)
157 {
158     grib_accessor_julian_date* self = (grib_accessor_julian_date*)a;
159     int n                           = 0;
160     grib_handle* h                  = grib_handle_of_accessor(a);
161 
162     self->year  = grib_arguments_get_name(h, c, n++);
163     self->month = grib_arguments_get_name(h, c, n++);
164 
165     self->day = grib_arguments_get_name(h, c, n++);
166     if (self->day == NULL) {
167         self->hour   = 0;
168         self->minute = 0;
169         self->second = 0;
170         self->ymd    = self->year;
171         self->hms    = self->month;
172         self->year   = 0;
173         self->month  = 0;
174     }
175     else {
176         self->ymd    = 0;
177         self->hms    = 0;
178         self->hour   = grib_arguments_get_name(h, c, n++);
179         self->minute = grib_arguments_get_name(h, c, n++);
180         self->second = grib_arguments_get_name(h, c, n++);
181     }
182     self->sep[0] = ' ';
183     self->sep[1] = 0;
184     self->sep[2] = 0;
185     self->sep[3] = 0;
186     self->sep[4] = 0;
187 
188     a->length = 0;
189 }
190 
dump(grib_accessor * a,grib_dumper * dumper)191 static void dump(grib_accessor* a, grib_dumper* dumper)
192 {
193     grib_dump_string(dumper, a, NULL);
194 }
195 
unpack_double(grib_accessor * a,double * val,size_t * len)196 static int unpack_double(grib_accessor* a, double* val, size_t* len)
197 {
198     int ret = 0;
199     long hour, minute, second;
200     long year, month, day, ymd, hms;
201     grib_handle* h                  = grib_handle_of_accessor(a);
202     grib_accessor_julian_date* self = (grib_accessor_julian_date*)a;
203 
204     if (self->ymd == NULL) {
205         ret = grib_get_long(h, self->year, &year);
206         if (ret != GRIB_SUCCESS)
207             return ret;
208         ret = grib_get_long(h, self->month, &month);
209         if (ret != GRIB_SUCCESS)
210             return ret;
211         ret = grib_get_long(h, self->day, &day);
212         if (ret != GRIB_SUCCESS)
213             return ret;
214 
215         ret = grib_get_long(h, self->hour, &hour);
216         if (ret != GRIB_SUCCESS)
217             return ret;
218         ret = grib_get_long(h, self->minute, &minute);
219         if (ret != GRIB_SUCCESS)
220             return ret;
221         ret = grib_get_long(h, self->second, &second);
222         if (ret != GRIB_SUCCESS)
223             return ret;
224     }
225     else {
226         ret = grib_get_long(h, self->ymd, &ymd);
227         if (ret != GRIB_SUCCESS)
228             return ret;
229         year = ymd / 10000;
230         ymd %= 10000;
231         month = ymd / 100;
232         ymd %= 100;
233         day = ymd;
234 
235         ret = grib_get_long(h, self->hms, &hms);
236         if (ret != GRIB_SUCCESS)
237             return ret;
238         hour = hms / 10000;
239         hms %= 10000;
240         minute = hms / 100;
241         hms %= 100;
242         second = hms;
243     }
244 
245     ret = grib_datetime_to_julian(year, month, day, hour, minute, second, val);
246 
247     return ret;
248 }
249 
pack_double(grib_accessor * a,const double * val,size_t * len)250 static int pack_double(grib_accessor* a, const double* val, size_t* len)
251 {
252     grib_accessor_julian_date* self = (grib_accessor_julian_date*)a;
253     int ret                         = 0;
254     long hour                       = 0;
255     long minute                     = 0;
256     long second                     = 0;
257     long ymd = 0, hms = 0;
258     long year, month, day;
259     grib_handle* h = grib_handle_of_accessor(a);
260 
261     ret = grib_julian_to_datetime(*val, &year, &month, &day, &hour, &minute, &second);
262     if (ret != 0)
263         return ret;
264 
265     if (self->ymd == NULL) {
266         ret = grib_set_long(h, self->year, year);
267         if (ret != 0)
268             return ret;
269         ret = grib_set_long(h, self->month, month);
270         if (ret != 0)
271             return ret;
272         ret = grib_set_long(h, self->day, day);
273         if (ret != 0)
274             return ret;
275         ret = grib_set_long(h, self->hour, hour);
276         if (ret != 0)
277             return ret;
278         ret = grib_set_long(h, self->minute, minute);
279         if (ret != 0)
280             return ret;
281         ret = grib_set_long(h, self->second, second);
282         if (ret != 0)
283             return ret;
284     }
285     else {
286         ymd = year * 10000 + month * 100 + day;
287         ret = grib_set_long(h, self->ymd, ymd);
288         if (ret != 0)
289             return ret;
290 
291         hms = hour * 10000 + minute * 100 + second;
292         ret = grib_set_long(h, self->hms, hms);
293         if (ret != 0)
294             return ret;
295     }
296 
297     return ret;
298 }
299 
unpack_string(grib_accessor * a,char * val,size_t * len)300 static int unpack_string(grib_accessor* a, char* val, size_t* len)
301 {
302     int ret = 0;
303     long hour, minute, second;
304     long year, month, day, ymd, hms;
305     grib_accessor_julian_date* self = (grib_accessor_julian_date*)a;
306     char* sep                       = self->sep;
307     grib_handle* h                  = grib_handle_of_accessor(a);
308 
309     if (*len < 15)
310         return GRIB_ARRAY_TOO_SMALL;
311 
312     if (self->ymd == NULL) {
313         ret = grib_get_long(h, self->year, &year);
314         if (ret != GRIB_SUCCESS)
315             return ret;
316         ret = grib_get_long(h, self->month, &month);
317         if (ret != GRIB_SUCCESS)
318             return ret;
319         ret = grib_get_long(h, self->day, &day);
320         if (ret != GRIB_SUCCESS)
321             return ret;
322 
323         ret = grib_get_long(h, self->hour, &hour);
324         if (ret != GRIB_SUCCESS)
325             return ret;
326         ret = grib_get_long(h, self->minute, &minute);
327         if (ret != GRIB_SUCCESS)
328             return ret;
329         ret = grib_get_long(h, self->second, &second);
330         if (ret != GRIB_SUCCESS)
331             return ret;
332     }
333     else {
334         ret = grib_get_long(h, self->ymd, &ymd);
335         if (ret != GRIB_SUCCESS)
336             return ret;
337         year = ymd / 10000;
338         ymd %= 10000;
339         month = ymd / 100;
340         ymd %= 100;
341         day = ymd;
342 
343         ret = grib_get_long(h, self->hms, &hms);
344         if (ret != GRIB_SUCCESS)
345             return ret;
346         hour = hms / 10000;
347         hms %= 10000;
348         minute = hms / 100;
349         hms %= 100;
350         second = hms;
351     }
352 
353     if (sep[1] != 0 && sep[2] != 0 && sep[3] != 0 && sep[4] != 0) {
354         sprintf(val, "%04ld%c%02ld%c%02ld%c%02ld%c%02ld%c%02ld", year, sep[0], month, sep[1], day, sep[2], hour, sep[3], minute, sep[4], second);
355     }
356     else if (sep[0] != 0) {
357         sprintf(val, "%04ld%02ld%02ld%c%02ld%02ld%02ld", year, month, day, sep[0], hour, minute, second);
358     }
359     else {
360         sprintf(val, "%04ld%02ld%02ld%02ld%02ld%02ld", year, month, day, hour, minute, second);
361     }
362     return ret;
363 }
364 
pack_string(grib_accessor * a,const char * val,size_t * len)365 static int pack_string(grib_accessor* a, const char* val, size_t* len)
366 {
367     int ret = 0;
368     long hour, minute, second;
369     long year, month, day, ymd, hms;
370     grib_accessor_julian_date* self = (grib_accessor_julian_date*)a;
371     char* sep                       = self->sep;
372     grib_handle* h                  = grib_handle_of_accessor(a);
373 
374     ret = sscanf(val, "%04ld%c%02ld%c%02ld%c%02ld%c%02ld%c%02ld", &year, &sep[0], &month, &sep[1], &day, &sep[2], &hour, &sep[3], &minute, &sep[4], &second);
375     if (ret != 11) {
376         if (strlen(val) == 15) {
377             ret = sscanf(val, "%04ld%02ld%02ld%c%02ld%02ld%02ld", &year, &month, &day, &sep[0], &hour, &minute, &second);
378             if (ret != 7) {
379                 grib_context_log(h->context, GRIB_LOG_ERROR, " Wrong date time format. Please use \"YYYY-MM-DD hh:mm:ss\"");
380                 return GRIB_INVALID_KEY_VALUE;
381             }
382             sep[1] = 0;
383             sep[2] = 0;
384             sep[3] = 0;
385             sep[4] = 0;
386         }
387         else {
388             ret = sscanf(val, "%04ld%02ld%02ld%02ld%02ld%02ld", &year, &month, &day, &hour, &minute, &second);
389             if (ret != 6) {
390                 grib_context_log(h->context, GRIB_LOG_ERROR, " Wrong date time format. Please use \"YYYY-MM-DD hh:mm:ss\"");
391                 return GRIB_INVALID_KEY_VALUE;
392             }
393             sep[0] = 0;
394             sep[1] = 0;
395             sep[2] = 0;
396             sep[3] = 0;
397             sep[4] = 0;
398         }
399     }
400 
401     if (self->ymd == NULL) {
402         ret = grib_set_long(h, self->year, year);
403         if (ret != 0)
404             return ret;
405         ret = grib_set_long(h, self->month, month);
406         if (ret != 0)
407             return ret;
408         ret = grib_set_long(h, self->day, day);
409         if (ret != 0)
410             return ret;
411         ret = grib_set_long(h, self->hour, hour);
412         if (ret != 0)
413             return ret;
414         ret = grib_set_long(h, self->minute, minute);
415         if (ret != 0)
416             return ret;
417         ret = grib_set_long(h, self->second, second);
418         if (ret != 0)
419             return ret;
420     }
421     else {
422         ymd = year * 10000 + month * 100 + day;
423         ret = grib_set_long(h, self->ymd, ymd);
424         if (ret != 0)
425             return ret;
426 
427         hms = hour * 10000 + minute * 100 + second;
428         ret = grib_set_long(h, self->hms, hms);
429         if (ret != 0)
430             return ret;
431     }
432 
433     return ret;
434 }
435 
unpack_long(grib_accessor * a,long * val,size_t * len)436 static int unpack_long(grib_accessor* a, long* val, size_t* len)
437 {
438     grib_context_log(a->context, GRIB_LOG_ERROR, " Cannot unpack %s as long", a->name);
439     return GRIB_NOT_IMPLEMENTED;
440 }
pack_long(grib_accessor * a,const long * v,size_t * len)441 static int pack_long(grib_accessor* a, const long* v, size_t* len)
442 {
443     grib_context_log(a->context, GRIB_LOG_ERROR, " Cannot pack %s as long", a->name);
444     return GRIB_NOT_IMPLEMENTED;
445 }
446 
pack_expression(grib_accessor * a,grib_expression * e)447 static int pack_expression(grib_accessor* a, grib_expression* e)
448 {
449     size_t len        = 1;
450     long lval         = 0;
451     double dval       = 0;
452     const char* cval  = NULL;
453     int ret           = 0;
454     grib_handle* hand = grib_handle_of_accessor(a);
455 
456     switch (grib_expression_native_type(hand, e)) {
457         case GRIB_TYPE_LONG: {
458             len = 1;
459             ret = grib_expression_evaluate_long(hand, e, &lval);
460             if (ret != GRIB_SUCCESS) {
461                 grib_context_log(a->context, GRIB_LOG_ERROR, "unable to set %s as long", a->name);
462                 return ret;
463             }
464             /*if (hand->context->debug)
465                     printf("ECCODES DEBUG grib_accessor_class_gen::pack_expression %s %ld\n", a->name,lval);*/
466             return grib_pack_long(a, &lval, &len);
467         }
468 
469         case GRIB_TYPE_DOUBLE: {
470             len = 1;
471             ret = grib_expression_evaluate_double(hand, e, &dval);
472             /*if (hand->context->debug)
473                     printf("ECCODES DEBUG grib_accessor_class_gen::pack_expression %s %g\n", a->name, dval);*/
474             return grib_pack_double(a, &dval, &len);
475         }
476 
477         case GRIB_TYPE_STRING: {
478             char tmp[1024];
479             len  = sizeof(tmp);
480             cval = grib_expression_evaluate_string(hand, e, tmp, &len, &ret);
481             if (ret != GRIB_SUCCESS) {
482                 grib_context_log(a->context, GRIB_LOG_ERROR, "unable to set %s as string", a->name);
483                 return ret;
484             }
485             len = strlen(cval);
486             /*if (hand->context->debug)
487                     printf("ECCODES DEBUG grib_accessor_class_gen::pack_expression %s %s\n", a->name, cval);*/
488             return grib_pack_string(a, cval, &len);
489         }
490     }
491 
492     return GRIB_NOT_IMPLEMENTED;
493 }
494