File: wcsutil.c

package info (click to toggle)
wcslib 7.12%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 8,612 kB
  • sloc: ansic: 34,618; lex: 9,328; fortran: 6,731; sh: 3,367; sed: 497; pascal: 190; makefile: 15
file content (541 lines) | stat: -r--r--r-- 11,523 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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*============================================================================
  WCSLIB 7.12 - an implementation of the FITS WCS standard.
  Copyright (C) 1995-2022, Mark Calabretta

  This file is part of WCSLIB.

  WCSLIB is free software: you can redistribute it and/or modify it under the
  terms of the GNU Lesser General Public License as published by the Free
  Software Foundation, either version 3 of the License, or (at your option)
  any later version.

  WCSLIB 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 Lesser General Public License for
  more details.

  You should have received a copy of the GNU Lesser General Public License
  along with WCSLIB.  If not, see http://www.gnu.org/licenses.

  Author: Mark Calabretta, Australia Telescope National Facility, CSIRO.
  http://www.atnf.csiro.au/people/Mark.Calabretta
  $Id: wcsutil.c,v 7.12 2022/09/09 04:57:58 mcalabre Exp $
*===========================================================================*/

#include <ctype.h>
#include <locale.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "wcsutil.h"
#include "wcsmath.h"

//----------------------------------------------------------------------------

void wcsdealloc(void *ptr)

{
  free(ptr);

  return;
}

//----------------------------------------------------------------------------

void wcsutil_strcvt(int n, char c, int nt, const char src[], char dst[])

{
  if (n <= 0) return;

  if (c != '\0') c = ' ';

  if (src == 0x0) {
    if (dst) {
      memset(dst, c, n);
    }

  } else {
    // Copy to the first NULL character.
    int j;
    for (j = 0; j < n; j++) {
      if ((dst[j] = src[j]) == '\0') {
        break;
      }
    }

    if (j < n) {
      // The given string is null-terminated.
      memset(dst+j, c, n-j);

    } else {
      // The given string is not null-terminated.
      if (c == '\0') {
        // Work backwards, looking for the first non-blank.
        for (j = n - 1; j >= 0; j--) {
          if (dst[j] != ' ') {
            break;
          }
        }

        j++;
	if (j == n && !nt) {
	  dst[n-1] = '\0';
	} else {
          memset(dst+j, '\0', n-j);
	}
      }
    }
  }

  if (nt) dst[n] = '\0';

  return;
}

//----------------------------------------------------------------------------

void wcsutil_blank_fill(int n, char c[])

{
  if (n <= 0) return;

  if (c == 0x0) {
    return;
  }

  // Replace the terminating null and all successive characters.
  for (int j = 0; j < n; j++) {
    if (c[j] == '\0') {
      memset(c+j, ' ', n-j);
      break;
    }
  }

  return;
}

//----------------------------------------------------------------------------

void wcsutil_null_fill(int n, char c[])

{
  if (n <= 0) return;

  if (c == 0x0) {
    return;
  }

  // Find the first NULL character.
  int j;
  for (j = 0; j < n; j++) {
    if (c[j] == '\0') {
      break;
    }
  }

  // Ensure null-termination.
  if (j == n) {
    j = n - 1;
    c[j] = '\0';
  }

  // Work backwards, looking for the first non-blank.
  j--;
  for (; j > 0; j--) {
    if (c[j] != ' ') {
      break;
    }
  }

  if (++j < n) {
    memset(c+j, '\0', n-j);
  }

  return;
}

//----------------------------------------------------------------------------

int wcsutil_all_ival(int nelem, int ival, const int iarr[])

{
  for (int i = 0; i < nelem; i++) {
    if (iarr[i] != ival) return 0;
  }

  return 1;
}

//----------------------------------------------------------------------------

int wcsutil_all_dval(int nelem, double dval, const double darr[])

{
  for (int i = 0; i < nelem; i++) {
    if (darr[i] != dval) return 0;
  }

  return 1;
}

//----------------------------------------------------------------------------

int wcsutil_all_sval(int nelem, const char *sval, const char (*sarr)[72])

{
  for (int i = 0; i < nelem; i++) {
    if (strncmp(sarr[i], sval, 72)) return 0;
  }

  return 1;
}

//----------------------------------------------------------------------------

int wcsutil_allEq(int nvec, int nelem, const double *first)

{
  if (nvec <= 0 || nelem <= 0) return 0;

  double v0 = *first;
  for (const double *vp = first+nelem; vp < first + nvec*nelem; vp += nelem) {
    if (*vp != v0) return 0;
  }

  return 1;
}

//----------------------------------------------------------------------------

int wcsutil_dblEq(
  int nelem,
  double tol,
  const double *darr1,
  const double *darr2)

{
  if (nelem == 0) return 1;
  if (nelem  < 0) return 0;

  if (darr1 == 0x0 && darr2 == 0x0) return 1;

  if (tol == 0.0) {
    // Handled separately for speed of execution.
    for (int i = 0; i < nelem; i++) {
      double dval1 = (darr1 ? darr1[i] : UNDEFINED);
      double dval2 = (darr2 ? darr2[i] : UNDEFINED);

      // Undefined values must match exactly.
      if (dval1 == UNDEFINED && dval2 != UNDEFINED) return 0;
      if (dval1 != UNDEFINED && dval2 == UNDEFINED) return 0;

      if (dval1 != dval2) return 0;
    }

  } else {
    for (int i = 0; i < nelem; i++) {
      double dval1 = (darr1 ? darr1[i] : UNDEFINED);
      double dval2 = (darr2 ? darr2[i] : UNDEFINED);

      // Undefined values must match exactly.
      if (dval1 == UNDEFINED && dval2 != UNDEFINED) return 0;
      if (dval1 != UNDEFINED && dval2 == UNDEFINED) return 0;

      // Otherwise, compare within the specified tolerance.
      if (fabs(dval1 - dval2) > 0.5*tol) return 0;
    }
  }

  return 1;
}

//----------------------------------------------------------------------------

int wcsutil_intEq(int nelem, const int *iarr1, const int *iarr2)

{
  if (nelem == 0) return 1;
  if (nelem  < 0) return 0;

  if (iarr1 == 0x0 && iarr2 == 0x0) return 1;

  for (int i = 0; i < nelem; i++) {
    int ival1 = (iarr1 ?  iarr1[i] : 0);
    int ival2 = (iarr2 ?  iarr2[i] : 0);

    if (ival1 != ival2) return 0;
  }

  return 1;
}

//----------------------------------------------------------------------------

int wcsutil_strEq(int nelem, char (*sarr1)[72], char (*sarr2)[72])

{
  if (nelem == 0) return 1;
  if (nelem  < 0) return 0;

  if (sarr1 == 0x0 && sarr2 == 0x0) return 1;

  for (int i = 0; i < nelem; i++) {
    char *sval1 = (sarr1 ?  sarr1[i] : "");
    char *sval2 = (sarr2 ?  sarr2[i] : "");

    if (strncmp(sval1, sval2, 72)) return 0;
  }

  return 1;
}

//----------------------------------------------------------------------------

void wcsutil_setAll(int nvec, int nelem, double *first)

{
  if (nvec <= 0 || nelem <= 0) return;

  double v0 = *first;
  for (double *vp = first+nelem; vp < first + nvec*nelem; vp += nelem) {
    *vp = v0;
  }
}

//----------------------------------------------------------------------------

void wcsutil_setAli(int nvec, int nelem, int *first)

{
  if (nvec <= 0 || nelem <= 0) return;

  int v0 = *first;
  for (int *vp = first+nelem; vp < first + nvec*nelem; vp += nelem) {
    *vp = v0;
  }
}

//----------------------------------------------------------------------------

void wcsutil_setBit(int nelem, const int *sel, int bits, int *array)

{
  if (bits == 0 || nelem <= 0) return;

  if (sel == 0x0) {
    // All elements selected.
    for (int *arrp = array; arrp < array + nelem; arrp++) {
      *arrp |= bits;
    }

  } else {
    // Some elements selected.
    for (int *arrp = array; arrp < array + nelem; arrp++) {
      if (*(sel++)) *arrp |= bits;
    }
  }
}

//----------------------------------------------------------------------------

char *wcsutil_fptr2str(void (*fptr)(void), char hext[19])

{
  // Test for little-endian addresses.
  int *(ip[2]), j[2], le = 1;
  ip[0] = j;
  ip[1] = j + 1;
  unsigned char *p = (unsigned char *)(&fptr);
  if ((unsigned char *)ip[0] < (unsigned char *)ip[1]) {
    // Little-endian, reverse it.
    p += sizeof(fptr) - 1;
    le = -1;
  }

  char *t = hext;
  sprintf(t, "0x0");
  t += 2;

  int gotone = 0;
  for (size_t i = 0; i < sizeof(fptr); i++) {
    // Skip leading zeroes.
    if (*p) gotone = 1;

    if (gotone) {
      sprintf(t, "%02x", *p);
      t += 2;
    }

    p += le;
  }

  return hext;
}

//----------------------------------------------------------------------------

static void wcsutil_locale_to_dot(char *buf)

{
  struct lconv *locale_data = localeconv();
  const char *decimal_point = locale_data->decimal_point;

  if (decimal_point[0] != '.' || decimal_point[1] != 0) {
    size_t decimal_point_len = strlen(decimal_point);
    char *inbuf = buf;
    char *outbuf = buf;

    for ( ; *inbuf; inbuf++) {
      if (strncmp(inbuf, decimal_point, decimal_point_len) == 0) {
        *outbuf++ = '.';
        inbuf += decimal_point_len - 1;
      } else {
        *outbuf++ = *inbuf;
      }
    }

    *outbuf = '\0';
  }
}


void wcsutil_double2str(char *buf, const char *format, double value)

{
  sprintf(buf, format, value);
  wcsutil_locale_to_dot(buf);

  // Look for a decimal point or exponent.
  char *bp = buf;
  while (*bp) {
    if (*bp != ' ') {
      if (*bp == '.') return;
      if (*bp == 'e') return;
      if (*bp == 'E') return;
    }
    bp++;
  }

  // Not found, add a fractional part.
  bp = buf;
  if (*bp == ' ') {
    char *cp = buf + 1;
    if (*cp == ' ') cp++;

    while (*cp) {
      *bp = *cp;
      bp++;
      cp++;
    }

    *bp = '.';
    bp++;
    if (bp < cp) *bp = '0';
  }
}

//----------------------------------------------------------------------------

static const char *wcsutil_dot_to_locale(const char *inbuf, char *outbuf)

{
  struct lconv *locale_data = localeconv();
  const char *decimal_point = locale_data->decimal_point;

  if (decimal_point[0] != '.' || decimal_point[1] != 0) {
    char *out = outbuf;
    size_t decimal_point_len = strlen(decimal_point);

    for ( ; *inbuf; inbuf++) {
      if (*inbuf == '.') {
        memcpy(out, decimal_point, decimal_point_len);
        out += decimal_point_len;
      } else {
        *out++ = *inbuf;
      }
    }

    *out = '\0';

    return outbuf;
  } else {
    return inbuf;
  }
}


int wcsutil_str2double(const char *buf, double *value)

{
  char ctmp[72];
  return sscanf(wcsutil_dot_to_locale(buf, ctmp), "%lf", value) < 1;
}


int wcsutil_str2double2(const char *buf, double *value)

{
  value[0] = 0.0;
  value[1] = 0.0;

  // Get the integer part.
  char ltmp[72];
  if (sscanf(wcsutil_dot_to_locale(buf, ltmp), "%lf", value) < 1) {
    return 1;
  }
  value[0] = floor(value[0]);

  char ctmp[72];
  strcpy(ctmp, buf);

  // Look for a decimal point.
  char *dptr = strchr(ctmp, '.');

  // Look for an exponent.
  char *eptr;
  if ((eptr = strchr(ctmp, 'E')) == NULL) {
    if ((eptr = strchr(ctmp, 'D')) == NULL) {
      if ((eptr = strchr(ctmp, 'e')) == NULL) {
        eptr = strchr(ctmp, 'd');
      }
    }
  }

  int exp = 0;
  if (eptr) {
    // Get the exponent.
    if (sscanf(eptr+1, "%d", &exp) < 1) {
      return 1;
    }

    if (!dptr) {
      dptr = eptr;
      eptr++;
    }

    if (dptr+exp <= ctmp) {
      // There is only a fractional part.
      return sscanf(wcsutil_dot_to_locale(buf, ctmp), "%lf", value+1) < 1;
    } else if (eptr <= dptr+exp+1) {
      // There is no fractional part.
      return 0;
    }
  }

  // Get the fractional part.
  if (dptr) {
    char *cptr = ctmp;
    while (cptr <= dptr+exp) {
      if ('0' < *cptr && *cptr <= '9') *cptr = '0';
      cptr++;
    }

    if (sscanf(wcsutil_dot_to_locale(ctmp, ltmp), "%lf", value+1) < 1) {
      return 1;
    }
  }

  return 0;
}