File: tunits.c

package info (click to toggle)
python-pywcs 1.11-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 11,888 kB
  • sloc: ansic: 31,441; lex: 6,170; fortran: 6,080; sh: 3,478; python: 3,122; sed: 408; makefile: 76
file content (144 lines) | stat: -rw-r--r-- 4,296 bytes parent folder | download
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
/*============================================================================

  WCSLIB 4.8 - an implementation of the FITS WCS standard.
  Copyright (C) 1995-2011, 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/>.

  Correspondence concerning WCSLIB may be directed to:
    Internet email: mcalabre@atnf.csiro.au
    Postal address: Dr. Mark Calabretta
                    Australia Telescope National Facility, CSIRO
                    PO Box 76
                    Epping NSW 1710
                    AUSTRALIA

  Author: Mark Calabretta, Australia Telescope National Facility
  http://www.atnf.csiro.au/~mcalabre/index.html
  $Id: tunits.c,v 4.8.1.1 2011/08/15 08:07:06 cal103 Exp cal103 $
*=============================================================================
*
* tunits tests wcsulex(), wcsutrn(), and wcsunits() the FITS units
* specification parser, translator and converter.
*
*---------------------------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <wcsunits.h>

int main()

{
  char   have[80], want[80];
  int    func, i, interactive, status;
  double offset, power, scale, units[WCSUNITS_NTYPE];

  interactive = isatty(0);

  printf("Testing FITS unit specification parser (tunits.c)\n"
         "-------------------------------------------------\n");
  if (interactive) printf("\nTo test wcsulex(), enter <CR> when prompted "
                          "with \"Unit string (want):\".\n");

  while (1) {
    if (interactive) printf("\nUnit string (have): ");
    if (!fgets(have, 80, stdin)) break;
    have[strlen(have)-1] = '\0';
    if (!interactive) printf("\nUnit string (have): %s\n", have);

    if ((status = wcsutrn(7, have)) >= 0) {
      printf("       Translation: %s", have);
      if (status == 0) {
        printf("\n");
      } else {
        printf("   (WARNING: %s)\n", wcsunits_errmsg[status]);
      }
    }

    if (interactive) printf("Unit string (want): ");
    if (!fgets(want, 80, stdin)) break;
    want[strlen(want)-1] = '\0';

    if (*want) {
      if (!interactive) printf("Unit string (want): %s\n", want);

      if ((status = wcsutrn(7, want)) >= 0) {
        printf("       Translation: %s", want);
        if (status == 0) {
          printf("\n");
        } else {
          printf("   (WARNING: %s)\n", wcsunits_errmsg[status]);
        }
      }

      printf("Conversion: \"%s\" -> \"%s\"\n", have, want);

      if ((status = wcsunits(have, want, &scale, &offset, &power))) {
        printf("wcsunits ERROR %d: %s.\n", status,
               wcsunits_errmsg[status]);
        continue;
      }

      printf("            = %s", (power == 1.0) ? "" : "(");

      if (scale == 1.0) {
        printf("value");
      } else {
        printf("%.8g * value", scale);
      }

      if (offset != 0.0) {
        printf(" + %.8g", offset);
      }

      if (power == 1.0) {
        printf("\n");
      } else {
        printf(")^%.8g\n", power);
      }

    } else {
      /* Parse the unit string. */
      printf("   Parsing: \"%s\"\n", have);

      if ((status = wcsulex(have, &func, &scale, units))) {
        printf("wcsulex ERROR %d: %s.\n", status,
               wcsunits_errmsg[status]);
        continue;
      }

      printf("%15.8g *\n", scale);
      for (i = 0; i < WCSUNITS_NTYPE; i++) {
        if (units[i] != 0.0) {
          printf("%11.2f %s", units[i], wcsunits_types[i]);
          if (strlen(wcsunits_units[i])) {
            printf(" (%s)\n", wcsunits_units[i]);
          } else {
            printf("\n");
          }
        }
      }
    }
  }

  printf("\n");

  return 0;
}