Debian Codegraph @

cextern/cfitsio/utilities/fvrf_data.c

raw
1 #include "fverify.h"
2 typedef struct {
3 int nnum;
4 int ncmp;
5 int nfloat;
6 int *indatatyp;
7 double *datamax;
8 double *datamin;
9 double *tnull;
10 unsigned char *mask; /* for bit X column only */
11 int ntxt;
12 FitsHdu *hduptr;
13 FILE* out;
14 }UserIter;
15
16 /*************************************************************
17 *
18 * test_data
19 *
20 * Test the HDU data
21 *
22 * This routine reads every row and column of ASCII tables to
23 * verify that the values have the correct format.
24 *
25 * This routine checks the following types of columns in binary tables:
26 *
27 * Logical L - value must be T, F or zero
28 * Bit nX - if n != a multiple of 8, then check that fill bits = 0
29 * String A - must contain ascii text, or zero
30 *
31 * It is impossible to write an invalid value to the other types of
32 * columns in binary tables (B, I, J, K, E, D, C and M) so these
33 * columns are not read.
34 *
35 * Since it is impossible to write an invalid value in a FITS image,
36 * this routine does not read the image pixels.
37 *
38 *************************************************************/
39 void test_data(fitsfile *infits, /* input fits file */
40 FILE *out, /* output ascii file */
41 FitsHdu *hduptr /* fits hdu pointer */
42 )
43
44 {
45 iteratorCol *iter_col=0;
46 int ncols;
47
48 int nnum = 0;
49 int *numlist; /* the list of the column whose data
50 type is numerical(scalar and complex) */
51 int nfloat = 0;
52 int *floatlist; /* the list of the floating point columns in ASCII table */
53
54 int ncmp = 0;
55 int *cmplist; /* the list of the column whose data
56 type is numerical(scalar and complex) */
57 int ntxt = 0;
58 int *txtlist; /* the list of column whose data type is
59 string, logical, bit or complex */
60 int niter = 0; /* total columns read into the literator function */
61
62 int ndesc = 0;
63 int *desclist; /* the list of column which is the descriptor of
64 the variable length array. */
65 int *isVarQFormat; /* Format type for each of the ndesc variable-length
66 columns: 0 = type 'P', 1 = type 'Q' */
67
68 long rows_per_loop = 0, offset;
69 UserIter usrdata;
70
71 int datatype;
72 long repeat;
73
74 long totalrows;
75 LONGLONG length;
76 LONGLONG toffset;
77 long *maxlen;
78 int icol;
79 char *cdata;
80 double *ndata;
81 int *idata;
82 int *maxminflag;
83 int *dflag;
84 char lnull = 2;
85 int anynul;
86
87 long rlength;
88 long bytelength;
89 long maxmax;
90
91 int i = 0;
92 int j = 0;
93 long jl = 0;
94 long k = 0;
95 int status = 0;
96 char errtmp[80];
97 int* perbyte;
98
99 LONGLONG naxis2;
100
101 int largeVarLengthWarned = 0;
102 int largeVarOffsetWarned = 0;
103
104 if(testcsum)
105 test_checksum(infits,out);
106
107 if(testfill) {
108 test_agap(infits,out,hduptr); /* test the bytes between the
109 ascii table columns. */
110 if(ffcdfl(infits, &status)) {
111 wrtferr(out,"checking data fill: ", &status, 1);
112 status = 0;
113 }
114 }
115
116 if(hduptr->hdutype != ASCII_TBL &&
117 hduptr->hdutype != BINARY_TBL ) return;
118
119 ncols = hduptr->ncols;
120 if(ncols <= 0) return;
121
122 ffgkyjj(infits, "NAXIS2", &naxis2, NULL, &status);
123
124 if (naxis2 > 2147483647) {
125 wrtout(out, "Cannot test data in tables with more than 2**31 (2147483647) rows.");
126 return;
127 }
128
129 /* separate the numerical, complex, text and
130 the variable length vector columns */
131 numlist =(int*)malloc(ncols * sizeof(int));
132 floatlist =(int*)malloc(ncols * sizeof(int));
133 cmplist =(int*)malloc(ncols * sizeof(int));
134 txtlist =(int*)malloc(ncols * sizeof(int));
135 desclist =(int*)malloc(ncols * sizeof(int));
136
137 if(hduptr->hdutype == ASCII_TBL) {
138
139 /*read every column of an ASCII table */
140 rows_per_loop = 0;
141 for (i=0; i< ncols; i++){
142 if(fits_get_coltype(infits, i+1, &datatype, NULL, NULL, &status)){
143 sprintf(errmes,"Column #%d: ",i);
144 wrtferr(out,errmes, &status,2);
145 }
146 if ( datatype != TSTRING ) {
147 numlist[nnum] = i+1;
148 nnum++;
149
150 if (datatype > TLONG) { /* floating point number column */
151 floatlist[nfloat] = i + 1;
152 nfloat++;
153 }
154
155 } else {
156 txtlist[ntxt] = i+1;
157 ntxt++;
158 }
159 }
160
161 } else if (hduptr->hdutype == BINARY_TBL) {
162
163 /* only check Bit, Logical and String columns in Binary tables */
164 rows_per_loop = 0;
165 for (i=0; i< ncols; i++){
166 if(fits_get_coltype(infits, i+1, &datatype, &repeat, NULL,
167 &status)){
168 sprintf(errmes,"Column #%d: ",i);
169 wrtferr(out,errmes, &status,2);
170 }
171
172 if(datatype < 0) { /* variable length column */
173 desclist[ndesc] = i+1;
174 ndesc++;
175
176 } else if(datatype == TBIT && (repeat%8) )
177 { /* bit column that does not have a multiple of 8 bits */
178 numlist[nnum] = i+1;
179 nnum++;
180
181 } else if( (datatype == TLOGICAL) ||
182 (datatype == TSTRING ) ) {
183 txtlist[ntxt] = i+1;
184 ntxt++;
185 }
186 /* ignore all other types of columns (B I J K E D C and M ) */
187 }
188 }
189
190
191 /* Use Iterator to read the columns that are not variable length arrays */
192 /* columns from 1 to nnum are scalar numerical columns.
193 columns from nnum+1 to nnum+ncmp are complex columns.
194 columns from nnum+ncmp are text columns */
195 niter = nnum + ncmp + ntxt + nfloat;
196
197 if(niter)iter_col = (iteratorCol *) malloc (sizeof(iteratorCol)*niter);
198
199 for (i=0; i< nnum; i++){
200 fits_iter_set_by_num(&iter_col[i], infits, numlist[i], TDOUBLE,
201 InputCol);
202 }
203 for (i=0; i< ncmp; i++){
204 j = nnum + i;
205 fits_iter_set_by_num(&iter_col[j], infits, cmplist[i], TDBLCOMPLEX,
206 InputCol);
207 }
208 for (i=0; i< ntxt; i++){
209 j = nnum + ncmp + i;
210 fits_iter_set_by_num(&iter_col[j], infits, txtlist[i], 0,
211 InputCol);
212 }
213 for (i=0; i< nfloat; i++){
214 j = nnum + ncmp + ntxt + i;
215 fits_iter_set_by_num(&iter_col[j], infits, floatlist[i], TSTRING,
216 InputCol);
217 }
218
219
220 offset = 0;
221 usrdata.nnum = nnum;
222 usrdata.ncmp = ncmp;
223 if (nnum > 0 || ncmp > 0) {
224 usrdata.datamax = (double *)calloc((nnum+ncmp), sizeof(double));
225 usrdata.datamin = (double *)calloc((nnum+ncmp), sizeof(double));
226 }
227 usrdata.tnull = (double *)calloc(ncols, sizeof(double));
228 usrdata.ntxt = ntxt;
229 usrdata.hduptr = hduptr;
230 usrdata.out = out;
231 usrdata.nfloat = nfloat;
232
233 /* get the mask for the bit X column
234 for column other than the X, it always 255
235 for Column nX, it will be 000...111, where # of 0 is n%8,
236 # of 1 is 8 - n%8.
237 */
238
239 if(nnum > 0) usrdata.mask =
240 (unsigned char *)calloc(nnum,sizeof( unsigned char));
241 if(nnum > 0) usrdata.indatatyp =
242 (int *)calloc(nnum,sizeof( int));
243 for (i=0; i< nnum; i++){
244 j = fits_iter_get_colnum(&(iter_col[i]));
245 if(fits_get_coltype(infits, j, &datatype, &repeat, NULL, &status)){
246 sprintf(errmes,"Column #%d: ",i);
247 wrtferr(out,errmes, &status,2);
248 }
249 usrdata.indatatyp[i] = datatype;
250 usrdata.mask[i] = 255;
251 if(datatype == TBIT) {
252 repeat = repeat%8;
253 usrdata.mask[i] = (usrdata.mask[i])>>repeat;
254 if(!repeat) usrdata.mask[i] = 0;
255 }
256 }
257
258
259 if(niter > 0) {
260 if(fits_iterate_data(niter, iter_col, offset,rows_per_loop, iterdata,
261 &usrdata,&status)){
262 wrtserr(out,"When Reading data, ",&status,2);
263 }
264 }
265
266 if(niter>0) free(iter_col);
267 free(numlist);
268 free(floatlist);
269 free(cmplist);
270 free(txtlist);
271 if(nnum > 0) free(usrdata.mask);
272 if(nnum > 0) free(usrdata.indatatyp);
273 if(nnum > 0 || ncmp > 0) {
274 free(usrdata.datamax);
275 free(usrdata.datamin);
276 }
277 free(usrdata.tnull);
278 if(!ndesc ) {
279 goto data_end;
280 }
281
282 /* ------------read the variable length vectors -------------------*/
283 usrdata.datamax = (double *)calloc(ndesc, sizeof(double));
284 usrdata.datamin = (double *)calloc(ndesc, sizeof(double));
285 usrdata.tnull = (double *)calloc(ndesc, sizeof(double));
286 maxminflag = (int *) calloc(ndesc , sizeof(int));
287 maxlen = (long *) calloc(ndesc , sizeof(long));
288 dflag = (int *) calloc(ndesc , sizeof(int));
289 perbyte = (int *) calloc(ndesc , sizeof(int));
290 isVarQFormat = (int *) calloc(ndesc , sizeof(int));
291 fits_get_num_rows(infits,&totalrows,&status);
292 status = 0;
293
294 /* this routine now only reads and test BIT, LOGICAL, and STRING columns */
295 /* There is no point in reading the other columns because the other datatypes */
296 /* have no possible invalid values. */
297
298 for (i = 0; i < ndesc; i++) {
299 icol = desclist[i];
300 parse_vtform(infits,out,hduptr,icol,&datatype,&maxlen[i],&isVarQFormat[i]);
301 dflag[i] = 4;
302 switch (datatype) {
303 case -TBIT:
304 dflag[i] = 1;
305 perbyte[i] = -8;
306 break;
307 case -TBYTE:
308 perbyte[i] = 1;
309 break;
310 case -TLOGICAL:
311 dflag[i] = 3;
312 perbyte[i] = 1;
313 break;
314 case -TSTRING:
315 dflag[i] = 0;
316 perbyte[i] = 1;
317 break;
318 case -TSHORT:
319 perbyte[i] = 2;
320 break;
321 case -TLONG:
322 perbyte[i] = 4;
323 break;
324 case -TFLOAT:
325 perbyte[i] = 4;
326 break;
327 case -TDOUBLE:
328 perbyte[i] = 8;
329 break;
330 case -TCOMPLEX:
331 dflag[i] = 2;
332 perbyte[i] = 8;
333 break;
334 case -TDBLCOMPLEX:
335 dflag[i] = 2;
336 perbyte[i] = 16;
337 break;
338 default:
339 break;
340 }
341 }
342
343 maxmax = maxlen[0];
344 for (i = 1; i < ndesc; i++) {
345 if(maxmax < maxlen[i]) maxmax = maxlen[i];
346 }
347 if(maxmax < 0) maxmax = 100;
348 ndata = (double *)malloc(2*maxmax*sizeof(double));
349 cdata = (char *)malloc((maxmax+1) *sizeof(char));
350 idata = (int *)malloc(maxmax *sizeof(int));
351
352
353 for (jl = 1; jl <= totalrows; jl++) {
354 for (i = 0; i < ndesc; i++) {
355 icol = desclist[i];
356
357 /* read and check the descriptor length and offset values */
358 if(fits_read_descriptll(infits, icol ,jl,&length,
359 &toffset, &status)){
360
361 sprintf(errtmp,"Row #%ld Col.#%d: ",jl,icol);
362 wrtferr(out,errtmp,&status,2);
363 }
364 if (!isVarQFormat[i])
365 {
366 if (!largeVarLengthWarned && length > 2147483647)
367 {
368 strcpy(errmes,"Var row length exceeds maximum 32-bit signed int. ");
369 sprintf(errtmp,"First detected for Row #%ld Column #%d",jl,icol);
370 strcat(errmes,errtmp);
371 wrtwrn(out,errmes,0);
372 largeVarLengthWarned = 1;
373 }
374 if (!largeVarOffsetWarned && toffset > 2147483647)
375 {
376 strcpy(errmes,"Heap offset for var length row exceeds maximum 32-bit signed int. ");
377 sprintf(errtmp,"First detected for Row #%ld Column #%d",jl,icol);
378 strcat(errmes,errtmp);
379 wrtwrn(out,errmes,0);
380 largeVarOffsetWarned = 1;
381 }
382 }
383
384 if(length > maxlen[i] && maxlen[i] > -1 ) {
385 sprintf(errmes, "Descriptor of Column #%d at Row %ld: ",
386 icol, jl);
387 sprintf(errtmp,"nelem(%ld) > maxlen(%ld) given by TFORM%d.",
388 (long) length,maxlen[i],icol);
389 strcat(errmes,errtmp);
390 wrterr(out,errmes,1);
391 }
392
393 if( perbyte[i] < 0)
394 bytelength = length/8;
395 else
396 bytelength = length*perbyte[i];
397
398 if(toffset + bytelength > hduptr->pcount ) {
399 sprintf(errmes, "Descriptor of Column #%d at Row %ld: ",
400 icol, jl);
401 sprintf(errtmp,
402 " offset of first element(%ld) + nelem(%ld)",
403 (long) toffset, (long) length);
404 strcat(errmes,errtmp);
405 if(perbyte[i] < 0)
406 sprintf(errtmp, "/8 > total heap area = %ld.",
407 (long) hduptr->pcount);
408 else
409 sprintf(errtmp, "*%d > total heap area = %ld.",
410 perbyte[i], (long) hduptr->pcount);
411 strcat(errmes,errtmp);
412 wrterr(out,errmes,2);
413 }
414
415 if(!length) continue; /* skip the 0 length array */
416
417 /* now check the values in BIT, LOGICAL, and String columns */
418 rlength = length;
419 if(length > maxmax) rlength = maxmax;
420
421 if(dflag[i] == 1) { /* read BIT column */
422 anynul = 0;
423
424 /* NOT YET IMPLEMENTED: This code should test that the fill bits that
425 pad out the last byte are all zero. Currently this test is applied
426 to fixed length logical arrays, but has not yet been done for
427 the variable length logical array case. It is probably safe to assume
428 that not many FITS files will contain variable length Logical columns,
429 to adding this test is not a high priority.
430
431 if(fits_read_col(infits, TDOUBLE, icol , jl, 1,
432 nelem, &nullval, ndata, &anynul, &status)) {
433 wrtferr(out,"",&status,2);
434 }
435 */
436 }
437
438 else if(dflag[i] == 0) { /* read String column */
439 if(fits_read_col(infits, TSTRING, icol, jl, 1,
440 rlength, NULL, &cdata, &anynul, &status)) {
441 sprintf(errtmp,"Row #%ld Col.#%d: ",jl,icol);
442 wrtferr(out,errtmp,&status,2);
443 }
444 else {
445 j = 0;
446 while (cdata[j] != 0) {
447
448 if ((cdata[j] > 126) || (cdata[j] < 32) ) {
449 sprintf(errmes,
450 "String in row #%ld, and column #%d contains non-ASCII text.", jl,icol);
451 wrterr(out,errmes,1);
452 strcpy(errmes,
453 " (This error is reported only once; other rows may have errors).");
454 print_fmt(out,errmes,13);
455 break;
456 }
457 j++;
458 }
459 }
460 }
461 else if(dflag[i] == 3) { /* read Logical column */
462 if(fits_read_col(infits, TLOGICAL, icol, jl, 1,
463 rlength, &lnull, cdata, &anynul, &status)) {
464 sprintf(errtmp,"Row #%ld Col.#%d: ",jl,icol);
465 wrtferr(out,errtmp,&status,2);
466 }
467 else {
468 for (k = 0; k < rlength; k++) {
469 if (cdata[k] > 2) {
470 sprintf(errmes,
471 "Logical value in row #%ld, column #%d not equal to 'T', 'F', or 0",
472 jl, icol);
473 wrterr(out,errmes,1);
474 strcpy(errmes,
475 " (This error is reported only once; other rows may have errors).");
476 print_fmt(out,errmes,13);
477 break;
478 }
479 }
480 }
481 }
482 }
483 }
484 free(ndata);
485 free(cdata);
486 free(idata);
487
488 free(usrdata.datamax);
489 free(usrdata.datamin);
490 free(usrdata.tnull);
491 free(maxminflag);
492 free(maxlen);
493 free(dflag);
494 free(perbyte);
495 free(isVarQFormat);
496
497 data_end:
498 free(desclist);
499 for ( i = 0; i< ncols; i++) {
500 (hduptr->datamax[i])[12] = '\0';
501 (hduptr->datamin[i])[12] = '\0';
502 (hduptr->tnull[i])[11] = '\0';
503 }
504
505 return;
506 }
507
508 /***********************************************************************/
509 /* iterator work function */
510
511 int iterdata(long totaln,
512 long offset,
513 long firstn,
514 long nrows,
515 int narray,
516 iteratorCol *iter_col,
517 void *usrdata
518 )
519 {
520 static UserIter *usrpt;
521 /*
522 static FitsHdu *hdupt;
523 */
524 static int nnum;
525 static int ntxt;
526 static int ncmp;
527 static int nfloat;
528 static int *flag_minmax = 0; /* define the initial min and max value */
529 static long *repeat;
530 static int *datatype;
531 static int find_badbit = 0;
532 static int find_baddot = 0;
533 static int find_badspace = 0;
534 static int find_badchar = 0;
535 static int find_badlog = 0;
536
537 double *data;
538 unsigned char *ldata;
539 char **cdata;
540 unsigned char *ucdata;
541 char *floatvalue;
542
543 /* bit column working space */
544 static unsigned char bdata;
545
546 int i;
547 long j,k,l;
548 long nelem;
549
550 if(firstn == 1 ) { /* first time for this table, so initialize */
551 usrpt = (UserIter *)usrdata;
552 /*
553 hdupt= usrpt->hduptr;
554 */
555 nnum = usrpt->nnum;
556 ncmp = usrpt->ncmp;
557 ntxt = usrpt->ntxt;
558 nfloat = usrpt->nfloat;
559 flag_minmax = (int *)calloc(nnum+ncmp, sizeof(int));
560 repeat = (long *)calloc(narray,sizeof(long));
561 datatype = (int *)calloc(narray,sizeof(int));
562 for (i=0; i < narray; i++) {
563 repeat[i] = fits_iter_get_repeat(&(iter_col[i]));
564 datatype[i] = fits_iter_get_datatype(&(iter_col[i]));
565 }
566 find_badbit = 0;
567 find_baddot = 0;
568 find_badspace = 0;
569 find_badchar = 0;
570 find_badlog = 0;
571 }
572
573 /* columns from 1 to nnum are scalar numerical columns.
574 columns from nnum+1 to nnum+ncmp are complex columns. (not used any more)
575 columns from nnum+ncmp are text columns */
576
577 /* deal with the numerical column */
578 for (i=0; i < nnum+ncmp; i++) {
579 data = (double *) fits_iter_get_array(&(iter_col[i]));
580 j = 1;
581 nelem = nrows * repeat[i];
582 if(i >= nnum) nelem = 2 * nrows *repeat[i];
583 if(nelem == 0) continue;
584 find_badbit = 0;
585
586 /* check for the bit jurisfication */
587 if(!find_badbit && usrpt->indatatyp[i] == TBIT ) {
588 for (k = 0; k < nrows; k++) {
589 j = (k+1)*repeat[i];
590 bdata = (unsigned char)data[j];
591 if( bdata & usrpt->mask[i] ) {
592 sprintf(errmes,
593 "Row #%ld, and Column #%d: X vector ", firstn+k,
594 fits_iter_get_colnum(&(iter_col[i])));
595 for (l = 1; l<= repeat[i]; l++) {
596 sprintf(comm, "0x%02x ", (unsigned char) data[k*repeat[i]+l]);
597 strcat(errmes,comm);
598 }
599 strcat(errmes,"is not left justified.");
600 wrterr(usrpt->out,errmes,2);
601 strcpy(errmes,
602 " (Other rows may have errors).");
603 print_fmt(usrpt->out,errmes,13);
604 find_badbit = 1;
605 break;
606 }
607 }
608 }
609 }
610
611 /* deal with character and logical columns */
612 for (i = nnum + ncmp; i < nnum + ncmp + ntxt; i++) {
613 if(datatype[i] == TSTRING ) { /* character */
614 nelem = nrows;
615 if(nelem == 0) continue;
616 cdata = (char **) fits_iter_get_array(&(iter_col[i]));
617 find_badchar = 0;
618
619 /* test for illegal ASCII text characters > 126 or < 32 */
620 if (!find_badchar) {
621 for (k = 0; k < nrows; k++) {
622 ucdata = (unsigned char *)cdata[k+1];
623 j = 0;
624 while (ucdata[j] != 0) {
625
626 if ((ucdata[j] > 126) || (ucdata[j] < 32)) {
627 sprintf(errmes,
628 "String in row #%ld, column #%d contains non-ASCII text.", firstn+k,
629 fits_iter_get_colnum(&(iter_col[i])));
630 wrterr(usrpt->out,errmes,1);
631 strcpy(errmes,
632 " (Other rows may have errors).");
633 print_fmt(usrpt->out,errmes,13);
634 find_badchar = 1;
635 break;
636 }
637 j++;
638 }
639 }
640 }
641 }
642
643 else { /* logical value */
644 nelem = nrows * repeat[i];
645 if(nelem == 0) continue;
646 ldata = (unsigned char *) fits_iter_get_array(&(iter_col[i]));
647
648 /* test for illegal logical column values */
649 /* The first element in the array gives the value that is used to represent nulls */
650 if (!find_badlog) {
651 for(j = 1; j <= nrows * repeat[i]; j++) {
652 if (ldata[j] > 2) {
653 sprintf(errmes,
654 "Logical value in row #%ld, column #%d not equal to 'T', 'F', or 0",
655 (firstn+j - 2)/repeat[i] +1,
656 fits_iter_get_colnum(&(iter_col[i])));
657 wrterr(usrpt->out,errmes,1);
658 strcpy(errmes,
659 " (Other rows may have similar errors).");
660 print_fmt(usrpt->out,errmes,13);
661 find_badlog = 1;
662 break;
663 }
664 }
665 }
666 }
667 }
668
669 for (i = nnum + ncmp +ntxt; i < nnum + ncmp + ntxt + nfloat; i++) {
670 nelem = nrows;
671 if(nelem == 0) continue;
672 cdata = (char **) fits_iter_get_array(&(iter_col[i]));
673 find_baddot = 0;
674 find_badspace = 0;
675
676 /* test for missing (implicit) decimal point in floating point numbers */
677 if (!find_baddot) {
678 for (k = 0; k < nrows; k++) {
679 floatvalue = (char *)cdata[k+1];
680 if (strcmp(cdata[0], floatvalue) && !strchr(floatvalue, '.') ) {
681
682 while (*floatvalue == ' ') /* skip leading spaces */
683 floatvalue++;
684
685 if (strlen(floatvalue)) { /* ignore completely blank fields */
686
687 sprintf(errmes,
688 "Number in row #%ld, column #%d has no decimal point:", firstn+k,
689 fits_iter_get_colnum(&(iter_col[i])));
690 wrterr(usrpt->out,errmes,1);
691 strcpy(errmes, floatvalue);
692 strcat(errmes,
693 " (Other rows may have similar errors).");
694 print_fmt(usrpt->out,errmes,13);
695 find_baddot = 1;
696 break;
697 }
698 }
699 }
700 }
701
702 if (!find_badspace) {
703 for (k = 0; k < nrows; k++) {
704 floatvalue = (char *)cdata[k+1];
705
706 if (strcmp(cdata[0], floatvalue) ) { /* not a null value? */
707 while (*floatvalue == ' ') /* skip leading spaces */
708 floatvalue++;
709
710 l = strlen(floatvalue) - 1;
711 while (l > 0 && floatvalue[l] == ' ') { /* remove trailing spaces */
712 floatvalue[l] = '\0';
713 l--;
714 }
715
716 if (strchr(floatvalue, ' ') ) {
717 sprintf(errmes,
718 "Number in row #%ld, column #%d has embedded space:", firstn+k,
719 fits_iter_get_colnum(&(iter_col[i])));
720 wrterr(usrpt->out,errmes,1);
721 strcpy(errmes, floatvalue);
722 strcat(errmes,
723 " (Other rows may have similar errors).");
724 print_fmt(usrpt->out,errmes,13);
725 find_badspace = 1;
726 break;
727 }
728 }
729 }
730 }
731 }
732
733 if(firstn + nrows - 1 == totaln) {
734 free(flag_minmax);
735 free(datatype);
736 free(repeat);
737 }
738 return 0;
739 }
740
741 /*************************************************************
742 *
743 * test_agap
744 *
745 * Test the bytes between the ASCII table column.
746 *
747 *
748 *************************************************************/
749 void test_agap(fitsfile *infits, /* input fits file */
750 FILE *out, /* output ascii file */
751 FitsHdu *hduptr /* fits hdu pointer */
752 )
753 {
754 int ncols;
755 LONGLONG nrows;
756 long irows;
757 LONGLONG rowlen;
758 unsigned char *data;
759 int *temp;
760 unsigned char *p;
761 LONGLONG i, j;
762 int k, m, t;
763 long firstrow = 1;
764 long ntodo;
765 long nerr = 0;
766 int status = 0;
767 char keyname[9];
768 char tform[FLEN_VALUE], comment[256];
769 int typecode, decimals;
770 long width, tbcol;
771 nerr = 0;
772
773 if(hduptr->hdutype != ASCII_TBL) return;
774 ncols = hduptr->ncols;
775 fits_get_num_rowsll(infits,&nrows,&status);
776 status = 0;
777
778 fits_get_rowsize(infits, &irows, &status);
779 status = 0;
780 rowlen = hduptr->naxes[0];
781 data = (unsigned char*)malloc(rowlen*sizeof(unsigned char)*irows);
782
783 /* Create a template row with data fields filled with 1s.
784 Used below - different ASCII rules apply within data columns
785 vs. between data columns. */
786
787 temp = (int*)malloc(rowlen * sizeof(int));
788 for (m = 0; m<rowlen; m++ ) temp[m]=0;
789 for (k = 1; k<=ncols; k++ ) {
790 sprintf(keyname, "TFORM%d",k);
791 fits_read_key_str(infits, keyname, tform, comment, &status);
792 if (fits_ascii_tform(tform, &typecode, &width, &decimals, &status))
793 wrtferr(out,"",&status,1);
794 sprintf(keyname, "TBCOL%d",k);
795 fits_read_key_lng(infits, keyname, &tbcol, comment, &status);
796 for (t = tbcol; t < tbcol+width; t++) temp[t-1]=1;
797 }
798
799 i = nrows;
800 while( i > 0) {
801 if( i > irows)
802 ntodo = irows;
803 else
804 ntodo = i;
805
806 p = data;
807 if(fits_read_tblbytes(infits,firstrow,1, rowlen*ntodo,
808 data, &status)){
809 wrtferr(out,"",&status,1);
810 }
811 for (j = 0; j<rowlen*ntodo; j++ ) {
812 if(!isascii(*p)) {
813 if(!nerr) {
814 #if (USE_LL_SUFFIX == 1)
815 sprintf(errmes,
816 "row %lld contains non-ASCII characters.", j/rowlen+1);
817 #else
818 sprintf(errmes,
819 "row %ld contains non-ASCII characters.", j/rowlen+1);
820 #endif
821 wrterr(out,errmes,1);
822 }
823 nerr++;
824 } else if(isascii(*p) && !isprint(*p)) {
825 if(temp[j%rowlen]) {
826 if(!nerr) {
827 #if (USE_LL_SUFFIX == 1)
828 sprintf(errmes,
829 "row %lld data contains non-ASCII-text characters.", j/rowlen+1);
830 #else
831 sprintf(errmes,
832 "row %ld data contains non-ASCII-text characters.", j/rowlen+1);
833 #endif
834 wrterr(out,errmes,1);
835 }
836 nerr++;
837 }
838 }
839 p++;
840 }
841 firstrow += ntodo;
842 i -=ntodo;
843 }
844 if(nerr) {
845 sprintf(errmes,
846 "This ASCII table contains %ld non-ASCII-text characters",nerr);
847 wrterr(out,errmes,1);
848 }
849 free(data);
850 free(temp);
851 return;
852 }
853
854
855 /*************************************************************
856 *
857 * test_checksum
858 *
859 * Test the checksum of the hdu
860 *
861 *
862 *************************************************************/
863 void test_checksum(fitsfile *infits, /* input fits file */
864 FILE *out /* output ascii file */
865 )
866 {
867 int status = 0;
868 int dataok, hduok;
869
870 if (fits_verify_chksum(infits, &dataok, &hduok, &status))
871 {
872 wrtferr(out,"verifying checksums: ",&status,2);
873 return;
874 }
875
876 if(dataok == -1)
877 wrtwrn(out,
878 "Data checksum is not consistent with the DATASUM keyword",0);
879
880 if(hduok == -1 ) {
881 if(dataok == 1) {
882 wrtwrn(out,
883 "Invalid CHECKSUM means header has been modified. (DATASUM is OK) ",0);
884 }
885 else {
886 wrtwrn(out, "HDU checksum is not in agreement with CHECKSUM.",0);
887 }
888 }
889 return;
890 }