~ubuntu-branches/debian/stretch/cfitsio/stretch

« back to all changes in this revision

Viewing changes to wcssub.c

  • Committer: Bazaar Package Importer
  • Author(s): Gopal Narayanan
  • Date: 2002-02-26 11:27:29 UTC
  • Revision ID: james.westby@ubuntu.com-20020226112729-3q2o993rhh81ipp4
Tags: upstream-2.401
ImportĀ upstreamĀ versionĀ 2.401

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdlib.h>
 
2
#include <string.h>
 
3
#include "fitsio2.h"
 
4
 
 
5
/*--------------------------------------------------------------------------*/
 
6
int ffgiwcs(fitsfile *fptr,  /* I - FITS file pointer                    */
 
7
           char **header,   /* O - pointer to the WCS related keywords  */
 
8
           int *status)     /* IO - error status                        */
 
9
/*
 
10
  int fits_get_image_wcs_keys 
 
11
  return a string containing all the image WCS header keywords.
 
12
  This string is then used as input to the wcsinit WCSlib routine.
 
13
*/
 
14
{
 
15
    int hdutype;
 
16
 
 
17
    if (*status > 0)
 
18
        return(*status);
 
19
 
 
20
    fits_get_hdu_type(fptr, &hdutype, status);
 
21
    if (hdutype != IMAGE_HDU)
 
22
    {
 
23
      ffpmsg(
 
24
     "Error in ffgiwcs. This HDU is not an image. Can't read WCS keywords");
 
25
      return(*status = NOT_IMAGE);
 
26
    }
 
27
 
 
28
    /* read header keywords into a long string of chars */
 
29
    if (ffh2st(fptr, header, status) > 0)
 
30
    {
 
31
        ffpmsg("error creating string of image WCS keywords (ffgiwcs)");
 
32
        return(*status);
 
33
    }
 
34
 
 
35
    return(*status);
 
36
}
 
37
/*--------------------------------------------------------------------------*/
 
38
int ffgtwcs(fitsfile *fptr,  /* I - FITS file pointer              */
 
39
           int xcol,        /* I - column number for the X column  */
 
40
           int ycol,        /* I - column number for the Y column  */
 
41
           char **header,   /* O - string of all the WCS keywords  */
 
42
           int *status)     /* IO - error status                   */
 
43
/*
 
44
  int fits_get_table_wcs_keys
 
45
  Return string containing all the WCS keywords appropriate for the 
 
46
  pair of X and Y columns containing the coordinate
 
47
  of each event in an event list table.  This string may then be passed
 
48
  to Doug Mink's WCS library wcsinit routine, to create and initialize the
 
49
  WCS structure.  The calling routine must free the header character string
 
50
  when it is no longer needed. 
 
51
*/
 
52
{
 
53
    int hdutype, ncols, tstatus, length;
 
54
    int naxis1 = 1, naxis2 = 1;
 
55
    long tlmin, tlmax;
 
56
    char keyname[FLEN_KEYWORD];
 
57
    char valstring[FLEN_VALUE];
 
58
    char comm[2];
 
59
    char *cptr;
 
60
    /*  construct a string of 80 blanks, for adding fill to the keywords */
 
61
                 /*  12345678901234567890123456789012345678901234567890123456789012345678901234567890 */
 
62
    char blanks[] = "                                                                                ";
 
63
 
 
64
    if (*status > 0)
 
65
        return(*status);
 
66
 
 
67
    fits_get_hdu_type(fptr, &hdutype, status);
 
68
    if (hdutype == IMAGE_HDU)
 
69
    {
 
70
        ffpmsg("Can't read table WSC keywords. This HDU is not a table");
 
71
        return(*status = NOT_TABLE);
 
72
    }
 
73
 
 
74
    fits_get_num_cols(fptr, &ncols, status);
 
75
    
 
76
    if (xcol < 1 || xcol > ncols)
 
77
    {
 
78
        ffpmsg("illegal X axis column number in fftwcs");
 
79
        return(*status = BAD_COL_NUM);
 
80
    }
 
81
 
 
82
    if (ycol < 1 || ycol > ncols)
 
83
    {
 
84
        ffpmsg("illegal Y axis column number in fftwcs");
 
85
        return(*status = BAD_COL_NUM);
 
86
    }
 
87
 
 
88
    /* allocate character string for all the WCS keywords */
 
89
    *header = calloc(1, 2401);  /* room for up to 30 keywords */
 
90
    if (*header == 0)
 
91
    {
 
92
        ffpmsg("error allocating memory for WCS header keywords (fftwcs)");
 
93
        return(*status = MEMORY_ALLOCATION);
 
94
    }
 
95
 
 
96
    cptr = *header;
 
97
    comm[0] = '\0';
 
98
    
 
99
    tstatus = 0;
 
100
    ffkeyn("TLMIN",xcol,keyname,status);
 
101
    ffgkyj(fptr,keyname, &tlmin,NULL,&tstatus);
 
102
 
 
103
    if (!tstatus)
 
104
    {
 
105
        ffkeyn("TLMAX",xcol,keyname,status);
 
106
        ffgkyj(fptr,keyname, &tlmax,NULL,&tstatus);
 
107
    }
 
108
 
 
109
    if (!tstatus)
 
110
    {
 
111
        naxis1 = tlmax - tlmin + 1;
 
112
    }
 
113
 
 
114
    tstatus = 0;
 
115
    ffkeyn("TLMIN",ycol,keyname,status);
 
116
    ffgkyj(fptr,keyname, &tlmin,NULL,&tstatus);
 
117
 
 
118
    if (!tstatus)
 
119
    {
 
120
        ffkeyn("TLMAX",ycol,keyname,status);
 
121
        ffgkyj(fptr,keyname, &tlmax,NULL,&tstatus);
 
122
    }
 
123
 
 
124
    if (!tstatus)
 
125
    {
 
126
        naxis2 = tlmax - tlmin + 1;
 
127
    }
 
128
 
 
129
    /*            123456789012345678901234567890    */
 
130
    strcat(cptr, "NAXIS   =                    2");
 
131
    strncat(cptr, blanks, 50);
 
132
    cptr += 80;
 
133
 
 
134
    ffi2c(naxis1, valstring, status);   /* convert to formatted string */
 
135
    ffmkky("NAXIS1", valstring, comm, cptr, status);  /* construct the keyword*/
 
136
    strncat(cptr, blanks, 50);  /* pad with blanks */
 
137
    cptr += 80;
 
138
 
 
139
    strcpy(keyname, "NAXIS2");
 
140
    ffi2c(naxis2, valstring, status);   /* convert to formatted string */
 
141
    ffmkky(keyname, valstring, comm, cptr, status);  /* construct the keyword*/
 
142
    strncat(cptr, blanks, 50);  /* pad with blanks */
 
143
    cptr += 80;
 
144
 
 
145
    /* read the required header keywords (use defaults if not found) */
 
146
 
 
147
    /*  CTYPE1 keyword */
 
148
    tstatus = 0;
 
149
    ffkeyn("TCTYP",xcol,keyname,status);
 
150
    if (ffgkey(fptr, keyname, valstring, NULL, &tstatus) )
 
151
       valstring[0] =  '\0';
 
152
    ffmkky("CTYPE1", valstring, comm, cptr, status);  /* construct the keyword*/
 
153
    length = strlen(cptr);
 
154
    strncat(cptr, blanks, 80 - length);  /* pad with blanks */
 
155
    cptr += 80;
 
156
 
 
157
    /*  CTYPE2 keyword */
 
158
    tstatus = 0;
 
159
    ffkeyn("TCTYP",ycol,keyname,status);
 
160
    if (ffgkey(fptr, keyname, valstring, NULL, &tstatus) )
 
161
       valstring[0] =  '\0';
 
162
    ffmkky("CTYPE2", valstring, comm, cptr, status);  /* construct the keyword*/
 
163
    length = strlen(cptr);
 
164
    strncat(cptr, blanks, 80 - length);  /* pad with blanks */
 
165
    cptr += 80;
 
166
 
 
167
    /*  CRPIX1 keyword */
 
168
    tstatus = 0;
 
169
    ffkeyn("TCRPX",xcol,keyname,status);
 
170
    if (ffgkey(fptr, keyname, valstring, NULL, &tstatus) )
 
171
       strcpy(valstring, "1");
 
172
    ffmkky("CRPIX1", valstring, comm, cptr, status);  /* construct the keyword*/
 
173
    strncat(cptr, blanks, 50);  /* pad with blanks */
 
174
    cptr += 80;
 
175
 
 
176
    /*  CRPIX2 keyword */
 
177
    tstatus = 0;
 
178
    ffkeyn("TCRPX",ycol,keyname,status);
 
179
    if (ffgkey(fptr, keyname, valstring, NULL, &tstatus) )
 
180
       strcpy(valstring, "1");
 
181
    ffmkky("CRPIX2", valstring, comm, cptr, status);  /* construct the keyword*/
 
182
    strncat(cptr, blanks, 50);  /* pad with blanks */
 
183
    cptr += 80;
 
184
 
 
185
    /*  CRVAL1 keyword */
 
186
    tstatus = 0;
 
187
    ffkeyn("TCRVL",xcol,keyname,status);
 
188
    if (ffgkey(fptr, keyname, valstring, NULL, &tstatus) )
 
189
       strcpy(valstring, "1");
 
190
    ffmkky("CRVAL1", valstring, comm, cptr, status);  /* construct the keyword*/
 
191
    strncat(cptr, blanks, 50);  /* pad with blanks */
 
192
    cptr += 80;
 
193
 
 
194
    /*  CRVAL2 keyword */
 
195
    tstatus = 0;
 
196
    ffkeyn("TCRVL",ycol,keyname,status);
 
197
    if (ffgkey(fptr, keyname, valstring, NULL, &tstatus) )
 
198
       strcpy(valstring, "1");
 
199
    ffmkky("CRVAL2", valstring, comm, cptr, status);  /* construct the keyword*/
 
200
    strncat(cptr, blanks, 50);  /* pad with blanks */
 
201
    cptr += 80;
 
202
 
 
203
    /*  CDELT1 keyword */
 
204
    tstatus = 0;
 
205
    ffkeyn("TCDLT",xcol,keyname,status);
 
206
    if (ffgkey(fptr, keyname, valstring, NULL, &tstatus) )
 
207
       strcpy(valstring, "1");
 
208
    ffmkky("CDELT1", valstring, comm, cptr, status);  /* construct the keyword*/
 
209
    strncat(cptr, blanks, 50);  /* pad with blanks */
 
210
    cptr += 80;
 
211
 
 
212
    /*  CDELT2 keyword */
 
213
    tstatus = 0;
 
214
    ffkeyn("TCDLT",ycol,keyname,status);
 
215
    if (ffgkey(fptr, keyname, valstring, NULL, &tstatus) )
 
216
       strcpy(valstring, "1");
 
217
    ffmkky("CDELT2", valstring, comm, cptr, status);  /* construct the keyword*/
 
218
    strncat(cptr, blanks, 50);  /* pad with blanks */
 
219
    cptr += 80;
 
220
 
 
221
    /* the following keywords may not exist */
 
222
 
 
223
    /*  CROTA2 keyword */
 
224
    tstatus = 0;
 
225
    ffkeyn("TCROT",ycol,keyname,status);
 
226
    if (ffgkey(fptr, keyname, valstring, NULL, &tstatus) == 0 )
 
227
    {
 
228
        ffmkky("CROTA2", valstring, comm, cptr, status);  /* construct keyword*/
 
229
        strncat(cptr, blanks, 50);  /* pad with blanks */
 
230
        cptr += 80;
 
231
    }
 
232
 
 
233
    /*  EPOCH keyword */
 
234
    tstatus = 0;
 
235
    if (ffgkey(fptr, "EPOCH", valstring, NULL, &tstatus) == 0 )
 
236
    {
 
237
        ffmkky("EPOCH", valstring, comm, cptr, status);  /* construct keyword*/
 
238
        length = strlen(cptr);
 
239
        strncat(cptr, blanks, 80 - length);  /* pad with blanks */
 
240
        cptr += 80;
 
241
    }
 
242
 
 
243
    /*  EQUINOX keyword */
 
244
    tstatus = 0;
 
245
    if (ffgkey(fptr, "EQUINOX", valstring, NULL, &tstatus) == 0 )
 
246
    {
 
247
        ffmkky("EQUINOX", valstring, comm, cptr, status); /* construct keyword*/
 
248
        length = strlen(cptr);
 
249
        strncat(cptr, blanks, 80 - length);  /* pad with blanks */
 
250
        cptr += 80;
 
251
    }
 
252
 
 
253
    /*  RADECSYS keyword */
 
254
    tstatus = 0;
 
255
    if (ffgkey(fptr, "RADECSYS", valstring, NULL, &tstatus) == 0 )
 
256
    {
 
257
        ffmkky("RADECSYS", valstring, comm, cptr, status); /*construct keyword*/
 
258
        length = strlen(cptr);
 
259
        strncat(cptr, blanks, 80 - length);  /* pad with blanks */
 
260
        cptr += 80;
 
261
    }
 
262
 
 
263
    /*  TELESCOPE keyword */
 
264
    tstatus = 0;
 
265
    if (ffgkey(fptr, "TELESCOP", valstring, NULL, &tstatus) == 0 )
 
266
    {
 
267
        ffmkky("TELESCOP", valstring, comm, cptr, status); 
 
268
        length = strlen(cptr);
 
269
        strncat(cptr, blanks, 80 - length);  /* pad with blanks */
 
270
        cptr += 80;
 
271
    }
 
272
 
 
273
    /*  INSTRUME keyword */
 
274
    tstatus = 0;
 
275
    if (ffgkey(fptr, "INSTRUME", valstring, NULL, &tstatus) == 0 )
 
276
    {
 
277
        ffmkky("INSTRUME", valstring, comm, cptr, status);  
 
278
        length = strlen(cptr);
 
279
        strncat(cptr, blanks, 80 - length);  /* pad with blanks */
 
280
        cptr += 80;
 
281
    }
 
282
 
 
283
    /*  DETECTOR keyword */
 
284
    tstatus = 0;
 
285
    if (ffgkey(fptr, "DETECTOR", valstring, NULL, &tstatus) == 0 )
 
286
    {
 
287
        ffmkky("DETECTOR", valstring, comm, cptr, status);  
 
288
        length = strlen(cptr);
 
289
        strncat(cptr, blanks, 80 - length);  /* pad with blanks */
 
290
        cptr += 80;
 
291
    }
 
292
 
 
293
    /*  MJD-OBS keyword */
 
294
    tstatus = 0;
 
295
    if (ffgkey(fptr, "MJD-OBS", valstring, NULL, &tstatus) == 0 )
 
296
    {
 
297
        ffmkky("MJD-OBS", valstring, comm, cptr, status);  
 
298
        length = strlen(cptr);
 
299
        strncat(cptr, blanks, 80 - length);  /* pad with blanks */
 
300
        cptr += 80;
 
301
    }
 
302
 
 
303
    /*  DATE-OBS keyword */
 
304
    tstatus = 0;
 
305
    if (ffgkey(fptr, "DATE-OBS", valstring, NULL, &tstatus) == 0 )
 
306
    {
 
307
        ffmkky("DATE-OBS", valstring, comm, cptr, status);  
 
308
        length = strlen(cptr);
 
309
        strncat(cptr, blanks, 80 - length);  /* pad with blanks */
 
310
        cptr += 80;
 
311
    }
 
312
 
 
313
    /*  DATE keyword */
 
314
    tstatus = 0;
 
315
    if (ffgkey(fptr, "DATE", valstring, NULL, &tstatus) == 0 )
 
316
    {
 
317
        ffmkky("DATE", valstring, comm, cptr, status);  
 
318
        length = strlen(cptr);
 
319
        strncat(cptr, blanks, 80 - length);  /* pad with blanks */
 
320
        cptr += 80;
 
321
    }
 
322
 
 
323
    strcat(cptr, "END");
 
324
    strncat(cptr, blanks, 77);
 
325
 
 
326
    return(*status);
 
327
}