-
Notifications
You must be signed in to change notification settings - Fork 1
/
putcols.c
304 lines (256 loc) · 10.7 KB
/
putcols.c
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
/* This file, putcols.c, contains routines that write data elements to */
/* a FITS image or table, of type character string. */
/* The FITSIO software was written by William Pence at the High Energy */
/* Astrophysic Science Archive Research Center (HEASARC) at the NASA */
/* Goddard Space Flight Center. */
#include <string.h>
#include <stdlib.h>
#include "fitsio2.h"
/*--------------------------------------------------------------------------*/
int ffpcls( fitsfile *fptr, /* I - FITS file pointer */
int colnum, /* I - number of column to write (1 = 1st col) */
LONGLONG firstrow, /* I - first row to write (1 = 1st row) */
LONGLONG firstelem, /* I - first vector element to write (1 = 1st) */
LONGLONG nelem, /* I - number of strings to write */
char **array, /* I - array of pointers to strings */
int *status) /* IO - error status */
/*
Write an array of string values to a column in the current FITS HDU.
*/
{
int tcode, maxelem, hdutype, nchar;
long twidth, incre;
long ii, jj, ntodo;
LONGLONG repeat, startpos, elemnum, wrtptr, rowlen, rownum, remain, next, tnull;
double scale, zero;
char tform[20], *blanks;
char message[FLEN_ERRMSG];
char snull[20]; /* the FITS null value */
tcolumn *colptr;
double cbuff[DBUFFSIZE / sizeof(double)]; /* align cbuff on word boundary */
char *buffer, *arrayptr;
if (*status > 0) /* inherit input status value if > 0 */
return(*status);
/* reset position to the correct HDU if necessary */
if (fptr->HDUposition != (fptr->Fptr)->curhdu)
{
ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);
}
else if ((fptr->Fptr)->datastart == DATA_UNDEFINED)
{
if ( ffrdef(fptr, status) > 0) /* rescan header */
return(*status);
}
/*---------------------------------------------------*/
/* Check input and get parameters about the column: */
/*---------------------------------------------------*/
if (colnum < 1 || colnum > (fptr->Fptr)->tfield)
{
snprintf(message, FLEN_ERRMSG,"Specified column number is out of range: %d",
colnum);
ffpmsg(message);
return(*status = BAD_COL_NUM);
}
colptr = (fptr->Fptr)->tableptr; /* point to first column */
colptr += (colnum - 1); /* offset to correct column structure */
tcode = colptr->tdatatype;
if (tcode == -TSTRING) /* variable length column in a binary table? */
{
/* only write a single string; ignore value of firstelem */
nchar = maxvalue(1,strlen(array[0])); /* will write at least 1 char */
/* even if input string is null */
if (ffgcprll( fptr, colnum, firstrow, 1, nchar, 1, &scale, &zero,
tform, &twidth, &tcode, &maxelem, &startpos, &elemnum, &incre,
&repeat, &rowlen, &hdutype, &tnull, snull, status) > 0)
return(*status);
/* simply move to write position, then write the string */
ffmbyt(fptr, startpos, IGNORE_EOF, status);
ffpbyt(fptr, nchar, array[0], status);
if (*status > 0) /* test for error during previous write operation */
{
snprintf(message,FLEN_ERRMSG,
"Error writing to variable length string column (ffpcls).");
ffpmsg(message);
}
return(*status);
}
else if (tcode == TSTRING)
{
if (ffgcprll( fptr, colnum, firstrow, firstelem, nelem, 1, &scale, &zero,
tform, &twidth, &tcode, &maxelem, &startpos, &elemnum, &incre,
&repeat, &rowlen, &hdutype, &tnull, snull, status) > 0)
return(*status);
/* if string length is greater than a FITS block (2880 char) then must */
/* only write 1 string at a time, to force writein by ffpbyt instead of */
/* ffpbytoff (ffpbytoff can't handle this case) */
if (twidth > IOBUFLEN) {
maxelem = 1;
incre = twidth;
repeat = 1;
}
blanks = (char *) malloc(twidth); /* string for blank fill values */
if (!blanks)
{
ffpmsg("Could not allocate memory for string (ffpcls)");
return(*status = ARRAY_TOO_BIG);
}
for (ii = 0; ii < twidth; ii++)
blanks[ii] = ' '; /* fill string with blanks */
remain = nelem; /* remaining number of values to write */
}
else
return(*status = NOT_ASCII_COL);
/*-------------------------------------------------------*/
/* Now write the strings to the FITS column. */
/*-------------------------------------------------------*/
next = 0; /* next element in array to be written */
rownum = 0; /* row number, relative to firstrow */
while (remain)
{
/* limit the number of pixels to process at one time to the number that
will fit in the buffer space or to the number of pixels that remain
in the current vector, which ever is smaller.
*/
ntodo = (long) minvalue(remain, maxelem);
ntodo = (long) minvalue(ntodo, (repeat - elemnum));
wrtptr = startpos + (rownum * rowlen) + (elemnum * incre);
ffmbyt(fptr, wrtptr, IGNORE_EOF, status); /* move to write position */
buffer = (char *) cbuff;
/* copy the user's strings into the buffer */
for (ii = 0; ii < ntodo; ii++)
{
arrayptr = array[next];
for (jj = 0; jj < twidth; jj++) /* copy the string, char by char */
{
if (*arrayptr)
{
*buffer = *arrayptr;
buffer++;
arrayptr++;
}
else
break;
}
for (;jj < twidth; jj++) /* fill field with blanks, if needed */
{
*buffer = ' ';
buffer++;
}
next++;
}
/* write the buffer full of strings to the FITS file */
if (incre == twidth)
ffpbyt(fptr, ntodo * twidth, cbuff, status);
else
ffpbytoff(fptr, twidth, ntodo, incre - twidth, cbuff, status);
if (*status > 0) /* test for error during previous write operation */
{
snprintf(message,FLEN_ERRMSG,
"Error writing elements %.0f thru %.0f of input data array (ffpcls).",
(double) (next+1), (double) (next+ntodo));
ffpmsg(message);
if (blanks)
free(blanks);
return(*status);
}
/*--------------------------------------------*/
/* increment the counters for the next loop */
/*--------------------------------------------*/
remain -= ntodo;
if (remain)
{
elemnum += ntodo;
if (elemnum == repeat) /* completed a row; start on next row */
{
elemnum = 0;
rownum++;
}
}
} /* End of main while Loop */
if (blanks)
free(blanks);
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffpcns( fitsfile *fptr, /* I - FITS file pointer */
int colnum, /* I - number of column to write (1 = 1st col) */
LONGLONG firstrow, /* I - first row to write (1 = 1st row) */
LONGLONG firstelem, /* I - first vector element to write (1 = 1st) */
LONGLONG nelem, /* I - number of values to write */
char **array, /* I - array of values to write */
char *nulvalue, /* I - string representing a null value */
int *status) /* IO - error status */
/*
Write an array of elements to the specified column of a table. Any input
pixels flagged as null will be replaced by the appropriate
null value in the output FITS file.
*/
{
long repeat, width;
LONGLONG ngood = 0, nbad = 0, ii;
LONGLONG first, fstelm, fstrow;
if (*status > 0)
return(*status);
/* reset position to the correct HDU if necessary */
if (fptr->HDUposition != (fptr->Fptr)->curhdu)
{
ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);
}
else if ((fptr->Fptr)->datastart == DATA_UNDEFINED)
{
if ( ffrdef(fptr, status) > 0) /* rescan header */
return(*status);
}
/* get the vector repeat length of the column */
ffgtcl(fptr, colnum, NULL, &repeat, &width, status);
if ((fptr->Fptr)->hdutype == BINARY_TBL)
repeat = repeat / width; /* convert from chars to unit strings */
/* absolute element number in the column */
first = (firstrow - 1) * repeat + firstelem;
for (ii = 0; ii < nelem; ii++)
{
if (strcmp(nulvalue, array[ii])) /* is this a good pixel? */
{
if (nbad) /* write previous string of bad pixels */
{
fstelm = ii - nbad + first; /* absolute element number */
fstrow = (fstelm - 1) / repeat + 1; /* starting row number */
fstelm = fstelm - (fstrow - 1) * repeat; /* relative number */
if (ffpclu(fptr, colnum, fstrow, fstelm, nbad, status) > 0)
return(*status);
nbad=0;
}
ngood = ngood +1; /* the consecutive number of good pixels */
}
else
{
if (ngood) /* write previous string of good pixels */
{
fstelm = ii - ngood + first; /* absolute element number */
fstrow = (fstelm - 1) / repeat + 1; /* starting row number */
fstelm = fstelm - (fstrow - 1) * repeat; /* relative number */
if (ffpcls(fptr, colnum, fstrow, fstelm, ngood, &array[ii-ngood],
status) > 0)
return(*status);
ngood=0;
}
nbad = nbad +1; /* the consecutive number of bad pixels */
}
}
/* finished loop; now just write the last set of pixels */
if (ngood) /* write last string of good pixels */
{
fstelm = ii - ngood + first; /* absolute element number */
fstrow = (fstelm - 1) / repeat + 1; /* starting row number */
fstelm = fstelm - (fstrow - 1) * repeat; /* relative number */
ffpcls(fptr, colnum, fstrow, fstelm, ngood, &array[ii-ngood], status);
}
else if (nbad) /* write last string of bad pixels */
{
fstelm = ii - nbad + first; /* absolute element number */
fstrow = (fstelm - 1) / repeat + 1; /* starting row number */
fstelm = fstelm - (fstrow - 1) * repeat; /* relative number */
ffpclu(fptr, colnum, fstrow, fstelm, nbad, status);
}
return(*status);
}