File: extract.c

package info (click to toggle)
fitscut 1.4.4-4
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 924 kB
  • ctags: 465
  • sloc: ansic: 6,024; sh: 767; makefile: 132
file content (286 lines) | stat: -rw-r--r-- 10,144 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
/* -*- mode:C; indent-tabs-mode:nil; tab-width:8; c-basic-offset:8; -*-
 *
 * Cutout extraction functions
 *
 * Author: William Jon McCann
 *
 * 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.
 *
 * $Id: extract.c,v 1.20 2004/05/11 22:05:06 mccannwj Exp $
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
#include <errno.h>
#include "getopt.h"
#include <signal.h>
#include <ctype.h>
#include <math.h>

#ifdef  HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
                                                                                
#ifdef HAVE_CFITSIO_FITSIO_H
#include <cfitsio/fitsio.h> 
#else
#include <fitsio.h>
#endif

#include "fitscut.h"
#include "extract.h"
#include "resize.h"

#ifdef DMALLOC
#include <dmalloc.h>
#define DMALLOC_FUNC_CHECK 1
#define LARGEST_BLOCK 60
#endif

void
extract_fits (FitsCutImage *Image)
{
        fitsfile *fptr;       /* pointer to the FITS file; defined in fitsio.h */
        int status;
        long fpixel[2];
        long lpixel[2];
        long inc[2];
        float *arrayptr;
        float *arrayptr2;
        int datatype, anynull;
        int nullval = 0;
        int rows_used = 0;
        int cols_used = 0;
        int xoffset = 0;
        int yoffset = 0;
        unsigned char first_channel = TRUE;

        int naxis;
        long naxes[2];
        long x1, y1, x0, y0;

        int k, i, j;
        int num_keys, more_keys;
        char *header;

        float zoom_factor;
        int resize_width = 0;
        int resize_height = 0;
  
        for (k = 0; k < Image->channels; k++) {

                status = 0;
    
                fitscut_message (1, "\tExamining FITS channel %d...\n", k);
    
                /* reset data pointer */
                Image->data[k] = NULL;

                if (Image->input_filename[k] == NULL)
                        continue;
                if (fits_open_file (&fptr, Image->input_filename[k], READONLY, &status)) 
                        printerror (status);

                /* check that cutout is within image dimensions */
                if (fits_get_img_dim (fptr, &naxis, &status))
                        printerror (status);

                if (fits_get_img_size (fptr, 2, naxes, &status))
                        printerror (status);
                if (Image->ncols[k] == MAGIC_SIZE_ALL_NUMBER) {
                        Image->ncols[k] = naxes[0];
                        Image->x0[k] = 0;
                }
                if (Image->nrows[k] == MAGIC_SIZE_ALL_NUMBER) {
                        Image->nrows[k] = naxes[1];
                        Image->y0[k] = 0;
                }
    
                x0 = Image->x0[k];
                y0 = Image->y0[k];

                /* CFITSIO starts indexing at 1 */
                x0 += 1;
                y0 += 1;
                x1 = x0 + Image->ncols[k]-1;
                y1 = y0 + Image->nrows[k]-1;
                fpixel[0] = MAX (1,x0);
                fpixel[1] = MAX (1,y0);
    
                inc[0] = 1;
                inc[1] = 1;
                lpixel[0] = MIN (naxes[0],x1);
                lpixel[1] = MIN (naxes[1],y1);

                if (fits_get_img_type (fptr, &datatype, &status))
                        printerror (status);
                Image->input_datatype[k] = datatype;

                rows_used = lpixel[1] - fpixel[1] + 1;
                cols_used = lpixel[0] - fpixel[0] + 1;

                fitscut_message (1, "\tAllocating space for %d x %d array\n",
                                 cols_used, rows_used);
                arrayptr = cutout_alloc (cols_used, rows_used, sizeof (float));

                if (fits_get_hdrspace (fptr, &num_keys, &more_keys, &status))
                        printerror (status);

                fitscut_message (3, "\t\theader has %d keys with space for %d more\n",
                                 num_keys, more_keys);

                /* let cfitsio extract the entire header as a string */
                if (fits_get_image_wcs_keys (fptr, &header, &status))
                        printerror (status);

                Image->header_cards[k] = num_keys;
                Image->header[k] = header;

                fitscut_message (1, "\tExtracting %s[%ld:%ld,%ld:%ld]...\n",
                                 Image->input_filename[k],
                                 fpixel[0], lpixel[0], fpixel[1], lpixel[1]);
                if (fits_read_subset (fptr, TFLOAT, fpixel, lpixel, inc,
                                      &nullval, arrayptr, &anynull, &status))
                        printerror (status);

                if (fits_close_file (fptr, &status)) 
                        printerror (status);

                if ((cols_used != Image->ncols[k]) || (rows_used != Image->nrows[k])) {
                        /* place the subset read from the fits file into an array
                         * of size ncols x nrows */

                        fitscut_message (1, "\tallocating space for %ld x %ld array\n",
                                         Image->ncols[k], Image->nrows[k]);
                        arrayptr2 = cutout_alloc (Image->ncols[k], Image->nrows[k], sizeof (float));
                        xoffset = (fpixel[0] == x0) ? 0 : (fpixel[0] - x0);
                        yoffset = (fpixel[1] == y0) ? 0 : (fpixel[1] - y0);

                        fitscut_message (2, "\tplacing subarray into array using offsets x=%d y=%d\n",
                                         xoffset, yoffset);
                        for (i = 0; i < cols_used; i++) {
                                for (j = 0; j < rows_used; j++) {
                                        *(arrayptr2 + (i + xoffset) + (j + yoffset) * Image->ncols[k])
                                                = *(arrayptr + i + j * cols_used);

                                        fitscut_message (3, "\tplacing [%d,%d]=%f into [%d,%d]\n",
                                                         i, j, *(arrayptr + i + j * cols_used),
                                                         i + xoffset, j + xoffset);
                                }
                        }
                        Image->data[k] = arrayptr2;
                        free (arrayptr);
                }
                else {
                        Image->data[k] = arrayptr;
                }

                if (Image->output_zoom != 0) {

                        if (first_channel) {
                                if (Image->output_zoom > 0) {
                                        zoom_factor = Image->output_zoom;
                                }
                                else {
                                        zoom_factor = 1.0;
                                }

                                fitscut_message (2, "Calculated zoom factor of %f from %f\n",
                                                 zoom_factor, Image->output_zoom);
                                resize_height = zoom_factor * Image->nrows[k];
                                resize_width = zoom_factor * Image->ncols[k];

                                fitscut_message (2, "Calculated zoom size of x=%d y=%d\n",
                                                 resize_width, resize_height);
                        }

                        fitscut_message (2, "\tresizing channel to x=%d y=%d\n",
                                         resize_width, resize_height);

                        resize_image_channel (Image, k, resize_width, resize_height);
                }

                first_channel = FALSE;
        }
}

/*
 * Allocate memory for cutout
 */
float *
cutout_alloc (unsigned int nx, unsigned int ny, unsigned int size)
{
        unsigned int nelem;
        float *ptr;

        nelem = nx * ny;
        ptr = (float *) calloc (nelem,size);
        if (ptr == NULL) {
                fitscut_message (0, "Unable to allocate memory for %d x %d image\n",
                                 nx, ny);
                do_exit (1);
        }
        return ptr;
}

/*
 * Print out cfitsio error messages and exit program
 */
void
printerror (int status)
{

        if (status) {
                fits_report_error (stderr, status); /* print error report */
                do_exit (status);    /* terminate the program, returning error status */
        }
}

double
fits_get_exposure_time (char *header, int num_cards)
{
        int status = 0;
        char keyname[FLEN_KEYWORD];
        int i,namelen;
        char card[FLEN_CARD+1];
        char value_string[FLEN_VALUE];
        char comment[FLEN_COMMENT];
        double value = -1.0;
        if (header != NULL) {
                keyname[0] = '\0';
 
                for (i = 0; i < num_cards; i++) {
                        strncpy (card, header + i * (FLEN_CARD - 1), FLEN_CARD - 1);
                        fits_get_keyname (card, keyname, &namelen, &status);
                        if (strequ (keyname, "EXPTIME")) {
                                fits_parse_value (card, value_string, comment, &status);
                                value = strtod (value_string, (char **)NULL);
                                fitscut_message (2, "Found EXPTIME='%s' converted to %f\n",
                                                 value_string, value);
                                return value;
                        }
                }
        }
        return value;
}