File: utils.c

package info (click to toggle)
grace6 5.99.1%2Bdev4-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 10,492 kB
  • ctags: 13,269
  • sloc: ansic: 103,384; sh: 5,021; yacc: 617; makefile: 574; lex: 253; fortran: 56
file content (378 lines) | stat: -rw-r--r-- 10,990 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
 * Grace - GRaphing, Advanced Computation and Exploration of data
 * 
 * Home page: http://plasma-gate.weizmann.ac.il/Grace/
 * 
 * Copyright (c) 1996-2005 Grace Development Team
 * 
 * Maintained by Evgeny Stambulchik
 * 
 * 
 *                           All Rights Reserved
 * 
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 * 
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 * 
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 *
 * plot utils
 *
 */

#include <string.h>
#include <stdlib.h>
#include <time.h>

#include "grace/plotP.h"

#define RES_NONE    0
#define RES_DEG     1
#define RES_MIN     2
#define RES_SEC     3

/* FIXME: check for max size indeed! */
size_t strfgeo(char *str, size_t max, const char *format, double value, int prec)
{
    char *s = str, *fmt = (char *) format;
    size_t out_size = 0;
    int min_resolution = RES_NONE, sgn, np;
    double deg_value, min_value, sec_value;

    while (*fmt) {
        if (*fmt == '%') {
            fmt++;
            switch(*fmt) {
            case 'D':
                min_resolution = RES_DEG;
                break;
            case 'M':
                min_resolution = RES_MIN;
                break;
            case 'S':
                min_resolution = RES_SEC;
                break;
            }
        }
        fmt++;
    }
    
    sgn = sign(value);
    value = fabs(value);
    
    fmt = (char *) format;
    while (*fmt && out_size < max) {
        if (*fmt == '%') {
            fmt++;
            switch(*fmt) {
            case 'D':
                if (min_resolution == RES_DEG) {
                    deg_value = value;
                    np = sprintf(s, "%.*f", prec, deg_value);
                } else {
                    deg_value = floor(value);
                    np = sprintf(s, "%d", (int) deg_value);
                }
                break;
            case 'M':
                deg_value = floor(value);
                if (min_resolution == RES_MIN) {
                    min_value = (value - deg_value)*60.0;
                    np = sprintf(s, "%.*f", prec, min_value);
                } else {
                    min_value = floor(value - deg_value);
                    np = sprintf(s, "%d", (int) min_value);
                }
                break;
            case 'S':
                min_value = (value - floor(value))*60.0;
                sec_value = (min_value - floor(min_value))*60.0;
                np = sprintf(s, "%.*f", prec, sec_value);
                break;
            case 'X':
                if (sgn < 0) {
                    np = sprintf(s, "%c", 'W');
                } else
                if (sgn > 0) {
                    np = sprintf(s, "%c", 'E');
                } else {
                    np = 0;
                }
                break;
            case 'Y':
                if (sgn < 0) {
                    np = sprintf(s, "%c", 'S');
                } else
                if (sgn > 0) {
                    np = sprintf(s, "%c", 'N');
                } else {
                    np = 0;
                }
                break;
            default:
                np = 0;
                break;
            }
            out_size += np;
            s += np;
        } else {
            *s = *fmt;
            out_size++;
            s++;
        }
        fmt++;
    }
    return out_size;
}

/*
 * reduce years according to the following rules :
 * [ wrap_year ; 100*(1 + wrap_year/100) - 1 ] -> [wy ; 99]
 * [ 100*(1 + wrap_year/100) ; wrap_year + 99] -> [00 ; wy-1]
 */
static int reduced_year(const Project *pr, int y)
{
    if (pr->two_digits_years) {
        int century = 100*(1 + pr->wrap_year/100);
        if (y < pr->wrap_year) {
            return y;
        } else if (y < century) {
            return y - (century - 100);
        } else if (y < (pr->wrap_year + 100)) {
            return y - century;
        } else {
            return y;
        }
    } else {
        return y;
    }
}

void jdate_to_datetime(const Quark *q, double jday, int rounding,
                         int *y, int *m, int *d,
                         int *hour, int *min, int *sec)
{
    Project *pr = project_get_data(q);
    
    if (pr) {
        /* compensate for the reference date */
        jday += pr->ref_date;

        jul_to_cal_and_time(jday, rounding, y, m, d, hour, min, sec);

        /* introduce the y2k bug for those who want it :) */
        *y = reduced_year(pr, *y);
    }
}

static int dayofweek(double j)
{
    int i = (int) floor(j + 1.5);
    return (i <= 0) ? 6 - (6 - i)%7 : i%7;
}

/* create format string */
char *create_fstring(const Quark *q, const Format *form, double loc, int type)
{
    char format[64], eng_prefix[6];
    static char s[MAX_STRING_LENGTH];
    double tmp;
    int m, d, y, h, mm, sec;
    int exponent;
    double mantissa;
    int yprec;
    Project *pr = project_get_data(q);
       
    if (pr->two_digits_years) {
        yprec = 2;
    } else {
        yprec = 4;
    }

    /* for locale decimal points */
    set_locale_num(TRUE);

    strcpy(format, "%.*lf");
    switch (form->type) {
    case FORMAT_DECIMAL:
        sprintf(s, format, form->prec, loc);
        tmp = atof(s);          /* fix reverse axes problem when loc == -0.0 */
        if (tmp == 0.0) {
            strcpy(format, "%.*lf");
            loc = 0.0;
            sprintf(s, format, form->prec, loc);
        }
        break;
    case FORMAT_EXPONENTIAL:
        strcpy(format, "%.*le");
        sprintf(s, format, form->prec, loc);
        tmp = atof(s);          /* fix reverse axes problem when loc == -0.0 */
        if (tmp == 0.0) {
            strcpy(format, "%.*le");
            loc = 0.0;
            sprintf(s, format, form->prec, loc);
        }
        break;
    case FORMAT_SCIENTIFIC:
        if (loc != 0.0) {
            exponent = (int) floor(log10(fabs(loc)));
            mantissa = loc/pow(10.0, (double) exponent);
            if (type == LFORMAT_TYPE_EXTENDED) {
                strcpy(format, "%.*f\\x\\c4\\C\\f{}10\\S%d\\N");
            } else {
                strcpy(format, "%.*fx10(%d)");
            }
            sprintf(s, format, form->prec, mantissa, exponent);
        } else {
            strcpy(format, "%.*f");
            sprintf(s, format, form->prec, 0.0);
        }
        break;
    case FORMAT_ENGINEERING:
        if (loc != 0.0) {
            exponent = (int) floor(log10(fabs(loc)));
            if (exponent < -18) {
                exponent = -18;
            } else if (exponent > 18) {
                exponent = 18;
            } else {
                exponent = (int) floor((double) exponent/3)*3;
            }
        } else {
            exponent = 0;
        }
        switch (exponent) {
        case -18: /* atto */
            strcpy(eng_prefix, "a");
            break;
        case -15: /* fempto */
            strcpy(eng_prefix, "f");
            break;
        case -12: /* pico */
            strcpy(eng_prefix, "p");
            break;
        case -9: /* nano */
            strcpy(eng_prefix, "n");
            break;
        case -6: /* micro */
            if (type == LFORMAT_TYPE_EXTENDED) {
                strcpy(eng_prefix, "\\xm\\f{}");
            } else {
                strcpy(eng_prefix, "mk");
            }
            break;
        case -3: /* milli */
            strcpy(eng_prefix, "m");
            break;
        case 3: /* kilo */
            strcpy(eng_prefix, "k");
            break;
        case 6: /* Mega */
            strcpy(eng_prefix, "M");
            break;
        case 9: /* Giga */
            strcpy(eng_prefix, "G");
            break;
        case 12: /* Tera */
            strcpy(eng_prefix, "T");
            break;
        case 15: /* Peta */
            strcpy(eng_prefix, "P");
            break;
        case 18: /* Exza (spelling?) */
            strcpy(eng_prefix, "E");
            break;
        default:
            strcpy(eng_prefix, "");
            break;
        }
        strcpy(format, "%.*f %s");
        sprintf(s, format, form->prec, loc/(pow(10.0, exponent)), eng_prefix);
        break;
    case FORMAT_POWER:
        if (loc < 0.0) {
            loc = log10(-loc);
            if (type == LFORMAT_TYPE_EXTENDED) {
                strcpy(format, "-10\\S%.*lf\\N");
            } else {
                strcpy(format, "-10(%.*lf)\\N");
            }
        } else if (loc == 0.0) {
            sprintf(format, "%.*f", form->prec, 0.0);
        } else {
            loc = log10(loc);
            if (type == LFORMAT_TYPE_EXTENDED) {
                strcpy(format, "10\\S%.*lf\\N");
            } else {
                strcpy(format, "10(%.*lf)\\N");
            }
        }
        sprintf(s, format, form->prec, loc);
        break;
    case FORMAT_GENERAL:
        strcpy(format, "%.*lg");
        sprintf(s, format, form->prec, loc);
        tmp = atof(s);
        if (tmp == 0.0) {
            strcpy(format, "%lg");
            loc = 0.0;
            sprintf(s, format, loc);
        }
        break;
    case FORMAT_DATETIME:
        if (!string_is_empty(form->fstring)) {
            struct tm datetime_tm;
            
            jdate_to_datetime(q, loc, ROUND_SECOND, &y, &m, &d, &h, &mm, &sec);
            
            memset(&datetime_tm, 0, sizeof(datetime_tm));
            datetime_tm.tm_year = y - 1900;
            datetime_tm.tm_mon  = m - 1;
            datetime_tm.tm_mday = d;
            datetime_tm.tm_hour = h;
            datetime_tm.tm_min  = mm;
            datetime_tm.tm_sec  = sec;
            datetime_tm.tm_wday = dayofweek(loc + pr->ref_date);
            datetime_tm.tm_yday = (int) (cal_to_jul(y, m, d) -
                                         cal_to_jul(y, 1, 1));
            
            if (strftime(s,
                MAX_STRING_LENGTH - 1, form->fstring, &datetime_tm) <= 0) {
                s[0] = '\0';
            }
        } else {
            s[0] = '\0';
        }
        break;
    case FORMAT_GEOGRAPHIC:
        if (!string_is_empty(form->fstring)) {
            if (strfgeo(s, MAX_STRING_LENGTH - 1, form->fstring, loc,
                form->prec) <= 0) {
                s[0] = '\0';
            }
        } else {
            s[0] = '\0';
        }
        break;
    default:
        sprintf(s, format, form->prec, loc);
        break;
    }

    /* revert to POSIX */
    set_locale_num(FALSE);
    
    return(s);
}