Debian Codegraph @

cextern/cfitsio/utilities/ftverify.c

raw
1 #include "fverify.h"
2
3 #ifndef STANDALONE
4 #include "pil.h"
5 #include "headas.h"
6 #include "headas_error.h"
7 #endif
8
9 #define MAXMSG 256
10
11 /*
12 HISTORY
13 -------
14
15 The original fverify.f Fortran program was written by William Pence in 1994.
16 Ning Gan rewrote fverify in C in 1998, and continued to make enhancements
17 throughout 1999 and 2000.
18 Ziqin Pan adapted fverify to build in the new HEADAS environment
19 in 2002, renaming it to ftverify.
20 William Pence made additional enhancements, and rationalized the code
21 so that fverify and ftverify share many of the same source files
22 in January 2003
23
24
25 * Detailed modification History
26 *
27 * MODIFICATION HISTORY:
28 * 1998-08-24 Ning Gan, v3.0.0
29 * Beta version: xverify v1.0
30 * 1998-12-18 Ning Gan, v3.0.1
31 * Beta version: xverify v1.1
32 * 1999-02-18 Ning Gan, v3.0.2
33 * Beta version: xverify v1.2
34 * Added more checks for keywords.
35 * 1999-03-04 Ning Gan, v3.0.3
36 * Added a feature of multiple input files.
37 * 1999-05-19 Ning Gan, v3.0.5
38 * Wrote numwrns and numerrs to the par file.
39 * If the # of errors exceeds the MAXERRORS,
40 * quit and wrote the summary.
41 * Took out the limits on warnings.
42 * 1999-06-03 Ning Gan, v3.0.6
43 * Wrote the version number of underlying
44 * cfitsio.
45 * 1999-06-07 Ning Gan, v3.0.7
46 * Improve the error handling. If there are
47 * errors on opening fitsfile, the program set
48 * numerr to 1 and quit.
49 * 1999-06-30 Ning Gan, v3.0.8
50 * Improve the layout of the output.
51 * 1999-08-25 Ning Gan, v3.0.9
52 * Always write errors to stderr.
53 * Added ffnchk
54 * Took out the checks of rejecting the
55 * TDISP like I2.0 and the column name
56 * startingnerror with a digit.
57 * 1999-10-25 Ning Gan, v3.1.0
58 * The TDISP can take format I4.
59 * Beutified the CFISIO error stack output
60 * The numerrs and numwrns are the accumulated
61 * number of errors and warnings if multiple
62 * FITS file are tested in on fverify session.
63 * Checked the X Column is left justified.
64 * 1999-12-12 Ning Gan, v3.1.1
65 * Added the basiconly and heasarc parameters.
66 * 1999-12-20 Ning Gan, v3.1.2
67 * Added the parameters of errreport and prstat,
68 * removed the parameters of basiconly, erronly and
69 * errpluswrn.
70 * 1999-12-29 Ning Gan, v3.1.3
71 * fixed a bug on solaris
72 * 2000-05-03 Ning Gan, v3.1.4
73 * Skip the blank column names in column name
74 * tests.
75 * 2000-06-09 Ning Gan, v3.1.5
76 * Fixed the memory problem(The bug will crash
77 * 2000-09-26 Ning Gan, v3.1.6
78 * Fixed the TDISP format bug (not accept
79 * format such as E15.5E3).
80 * 2002-09-30 Ziqin Pan converted fverify to ftverify for HEADAS environ.
81 * 2003-01-09 W Pence v4.0
82 * Added support for the new WCSAXES keyword
83 * Added support for random groups
84 * several small changes to the output report format
85 *
86 * 2004-06-21 W Pence fixed reporting error when prstat=no and when
87 * opening a nonexistent or non-FITS file.
88 * Also fixed elusive memory allocation error.
89 *
90 * 2009-06-08 W Pence v4.15
91 * updates to comply with V3.0 of the FITS Standard
92 * 2010-07-26 W Pence v4.16
93 * Updates to WCS keyword checks, plus other V3.0 issues.
94 * Also check for non-zero heap if binary table has no
95 * variable length array columns.
96 * 2013-08-12 W Pence v4.17
97 * Ignore blank keywords preceding the END keyword.
98 * Support (partially at least) files with PCOUNT > 2GB.
99 * 2016-04-13 B Irby v4.18
100 * Change verify_fits from void to int; check return
101 * status for abort conditions and set nerrs (as it
102 * is not set by close_report) in this case for the
103 * one-line file summary.
104 * 2018-Jan/May B Irby v4.19
105 * Put webtool-only variable declarations inside an ifdef.
106 * Silenced warnings about implicit function declarations when
107 * building standalone fitsverify. Moved conditional include
108 * of fitsverify.c to be after the function prototypes.
109 * 2019-03-15 W Pence v4.20
110 * Added 'testhierarch' parameter to test that HIERARCH
111 * keywords conform to the ESO convention.
112 */
113
114 #define TOOLSUB ftverify
115 /* headas_main() requires that TOOLSUB be defined first */
116
117 long totalerr, totalwrn;
118
119 /* Function Prototypes */
120 int ftverify (void);
121 int ftverify_getpar (char *infile, char *outfile,int * prehead,
122 int* prstat, char* errreport, int* testdata, int* testcsum,
123 int* testfill, int* heasarc_conv, int* testhierarch);
124 int ftverify_work (char *infile, char *outfile,
125 int prehead, int prstat, char* errreport, int testdata, int testcsum,
126 int testfill, int heasarc_conv, int testhierarch);
127
128 #ifdef STANDALONE
129 #include "fitsverify.c"
130 #else
131 #include "headas_main.c"
132 #endif
133
134 int err_report=0;
135 int prhead=0;
136 int prstat=1;
137 int testdata=1;
138 int testcsum=1;
139 int testfill=1;
140 int heasarc_conv=1;
141 int testhierarch=0;
142 int totalhdu=0;
143
144
145 /*---------------------------------------------------------------------------*/
146 int ftverify (void)
147 {
148 /* Read a FITS file and verify that it conforms to the FITS standard */
149
150 char infile[PIL_LINESIZE],outfile[PIL_LINESIZE];
151 int status;
152 char errreport[PIL_LINESIZE];
153
154 static char taskname[80] = "ftverify";
155 static char version[8] = "4.22";
156
157 /* Register taskname and version. */
158
159 set_toolname(taskname);
160 set_toolversion(version);
161
162 /* get input parameters */
163 status = ftverify_getpar(infile, outfile,&prhead,&prstat,errreport,
164 &testdata,&testcsum,&testfill,&heasarc_conv,&testhierarch);
165
166 /* call work function to verify that infile conforms to the FITS
167 standard and write report to the output file */
168 if (!status)
169 status = ftverify_work(infile, outfile,prhead,prstat,errreport,
170 testdata,testcsum,testfill,heasarc_conv,testhierarch);
171
172 return(status);
173 }
174 /*---------------------------------------------------------------------------*/
175 int ftverify_getpar(
176 char *infile, /* O - Input file name (Fits) */
177 char *outfile, /* O - Output file name (ASCII) */
178 int * prhead,
179 int * prstat,
180 char * errreport,
181 int * testdata,
182 int * testcsum,
183 int * testfill,
184 int * heasarc_conv,
185 int * testhierarch)
186
187 /* read input parameters for the ftverify task from the .par file */
188 {
189 int status;
190 char msg[MAXMSG];
191
192 if ((status = PILGetString("infile", infile))) {
193 sprintf(msg, "Error reading the 'infile' parameter.");
194 HD_ERROR_THROW(msg,status);
195 }
196
197
198 else if ((status = PILGetString("outfile", outfile))) {
199 sprintf(msg, "Error reading the 'outfile' parameter.");
200 HD_ERROR_THROW(msg,status);
201 }
202
203 else if ((status = PILGetBool("prhead", prhead))) {
204 sprintf(msg, "Error reading the 'prhead' parameter.");
205 HD_ERROR_THROW(msg,status);
206 }
207
208 else if ((status = PILGetBool("prstat", prstat))) {
209 sprintf(msg, "Error reading the 'prstat' parameter.");
210 HD_ERROR_THROW(msg,status);
211 }
212
213 else if ((status = PILGetString("errreport", errreport))) {
214 sprintf(msg, "Error reading the 'errreport' parameter.");
215 HD_ERROR_THROW(msg,status);
216 }
217
218 else if ((status = PILGetBool("testdata", testdata))) {
219 sprintf(msg, "Error reading the 'testdata' parameter.");
220 HD_ERROR_THROW(msg,status);
221 }
222
223 else if ((status = PILGetBool("tchksum", testcsum))) {
224 sprintf(msg, "Error reading the 'tchksum' parameter.");
225 HD_ERROR_THROW(msg,status);
226 }
227
228 else if ((status = PILGetBool("testfill", testfill))) {
229 sprintf(msg, "Error reading the 'testfill' parameter.");
230 HD_ERROR_THROW(msg,status);
231 }
232
233 else if ((status = PILGetBool("heasarc", heasarc_conv))) {
234 sprintf(msg, "Error reading the 'heasarc' parameter.");
235 HD_ERROR_THROW(msg,status);
236 }
237
238 else if ((status = PILGetBool("hierarch", testhierarch))) {
239 sprintf(msg, "Error reading the 'hierarch' parameter.");
240 HD_ERROR_THROW(msg,status);
241 }
242
243 return(status);
244 }
245 /*---------------------------------------------------------------------------*/
246 int ftverify_work(
247 char *infile, /* I - Input file name (Fits) */
248 char *outfile, /* I - Output file name (ASCII) */
249 int prehead,
250 int prstat,
251 char * errreport,
252 int testdata,
253 int testcsum,
254 int testfill,
255 int heasarc_conv,
256 int testhierarch)
257
258 /* call work function to verify that infile conforms to the FITS
259 standard and write report to the output file */
260 {
261 #ifdef WEBTOOL
262 FILE *runfile = 0;
263 int runnum;
264 char runchars[30];
265 #endif
266 FILE *outfptr = 0;
267 FILE *list=0;
268 int status = 0, vfstatus = 0, filestatus;
269 char * p;
270 char task[80];
271 char tversion[80];
272 float fversion;
273 int i, nerrs, nwarns;
274 char msg[MAXMSG];
275
276 /* determine 'Severe error", "Error", or "Warning" report level */
277 if( *errreport == 's' || *errreport == 'S') err_report = 2;
278 if( *errreport == 'e' || *errreport == 'E') err_report = 1;
279
280 p = infile;
281 if (*p == '@') {
282 p++;
283 if( (list = fopen(p,"r")) == NULL ) {
284 fprintf(stderr,"Cannot open the list file: %s.",p);
285 leave_early(NULL);
286 return (FILE_NOT_OPENED);
287 }
288 }
289
290 headas_clobberfile(outfile); /* delete existing file if clobber=YES */
291 p = outfile;
292
293 /* test if writing output log to a disk file */
294 if(prstat && strlen(p) && strcmp(p, "STDOUT") && strcmp(p, "STDERR")
295 && (outfptr = fopen(p,"r") ) != NULL ) {
296 sprintf(msg,"Clobber is not set. Cannot overwrite the file%s",p);
297 status = FILE_NOT_CREATED;
298 HD_ERROR_THROW(msg,status);
299 leave_early(NULL);
300 fclose(outfptr);
301 return (status);
302 }
303
304 if(prstat && (!strlen(p) || !strcmp(p, "STDOUT"))) {
305 outfptr = stdout;
306 }
307 else if(prstat && (!strlen(p) || !strcmp(p, "STDERR"))) {
308 outfptr = stderr;
309 }
310 else if (!prstat) {
311 outfptr=NULL;
312 }
313 else if( (outfptr = fopen(p,"w") ) == NULL) {
314 fprintf(stderr,"Error open output file %s. Using stdout instead.",
315 outfile);
316 outfptr = stdout;
317 }
318
319 #ifdef WEBTOOL
320 /* try opening and incrementing the file containing cumulative # of runs */
321 runfile=fopen("/tmp.shared/fits/tmpverify/counter.fitsverify","r+");
322 /* runfile=fopen("counter.fitsverify","r+"); */
323 if (runfile) {
324 fgets(runchars,20,runfile);
325 runnum=atoi(runchars);
326 runnum++;
327 sprintf(comm," Run Number %d",runnum);
328 wrtout(outfptr,comm);
329 sprintf(runchars, "%d", runnum);
330 fflush(runfile);
331 rewind(runfile);
332 fputs(runchars, runfile);
333 }
334
335 #endif
336
337 wrtout(outfptr," ");
338 fits_get_version(&fversion);
339 get_toolname(task);
340 get_toolversion(tversion);
341 sprintf(comm,"%s %s (CFITSIO V%.3f)",task,tversion,fversion);
342 wrtsep(outfptr,' ',comm,60);
343 for(i = 0; comm[i]!='\0'; i++) comm[i] = '-';
344 wrtsep(outfptr,' ',comm,60);
345 wrtout(outfptr," ");
346 switch (err_report) {
347 case 2:
348 sprintf(comm, "Caution: Only checking for the most severe FITS format errors.");
349 wrtout(outfptr,comm);
350 break;
351 case 1:
352 break;
353 case 0:
354 break;
355 }
356
357 if(heasarc_conv) {
358 sprintf(comm, "HEASARC conventions are being checked.");
359 wrtout(outfptr,comm);
360 }
361
362 if(testhierarch) {
363 sprintf(comm, "ESO HIERARCH keywords are being checked.");
364 wrtout(outfptr,comm);
365 }
366
367 /* process each file */
368 if (list == NULL) {
369 vfstatus = verify_fits(infile,outfptr);
370
371 if (outfptr == NULL) { /* print one-line file summary */
372
373 /* verify_fits returns a non-zero status for catastrophic
374 * file I/O problems (an abort), and in this case total_err
375 * is not updated via close_report(), so we need to set
376 * nerrs accordingly for the one-line file summary. */
377 if (vfstatus) nerrs = 1; else nerrs = get_total_err();
378
379 nwarns = get_total_warn();
380
381 filestatus = ((nerrs+nwarns)>0) ? 1 : 0;
382 if (filestatus)
383 {
384 if (err_report)
385 printf("verification FAILED: %-20s, %d errors\n",
386 infile, nerrs);
387 else
388 printf("verification FAILED: %-20s, %d warnings and %d errors\n",
389 infile, nwarns, nerrs);
390 }
391 else
392 printf("verification OK: %-20s\n", infile);
393 }
394 }
395 else {
396 while((p = fgets(infile, FLEN_FILENAME, list))!= NULL) {
397 vfstatus = verify_fits(infile,outfptr);
398
399 if (outfptr == NULL) { /* print one-line file summary */
400
401 /* verify_fits returns a non-zero status for catastrophic
402 * file I/O problems (an abort), and in this case total_err
403 * is not updated via close_report(), so we need to set
404 * nerrs accordingly for the one-line file summary. */
405 if (vfstatus) nerrs = 1; else nerrs = get_total_err();
406
407 nwarns = get_total_warn();
408
409 filestatus = ((nerrs+nwarns) >0) ? 1 : 0;
410 if (filestatus)
411 {
412 if (err_report)
413 printf("verification FAILED: %-20s, %d errors\n",
414 infile, nerrs);
415 else
416 printf("verification FAILED: %-20s, %d warnings and %d errors\n",
417 infile, nwarns, nerrs);
418 }
419 else
420 printf("verification OK: %-20s\n", infile);
421 }
422
423 for (i = 1; i < 3; i++) wrtout(outfptr," ");
424 }
425 fclose(list);
426 }
427
428 /* close the output file */
429 if (outfptr != stdout && outfptr != NULL) fclose(outfptr);
430
431 return(status);
432 }
433
434 /******************************************************************************
435 * Function
436 * update_parfile
437 *
438 *
439 * DESCRIPTION:
440 * Update the numerrs and numwrns parameters in the parfile.
441 *
442 *******************************************************************************/
443 void update_parfile(int nerr, int nwrn)
444 {
445 int status = 0;
446 char parname[32];
447
448 totalerr += (long )nerr;
449 totalwrn += (long )nwrn;
450 /* write the total accumulated total warnings and errors to the
451 parfile */
452 strcpy(parname, "numwrns");
453 status=PILPutInt(parname, totalwrn);
454 if(status) {
455 fprintf(stderr,"Error to update the numwrns keyword.\n");
456 status = 0;
457 }
458 strcpy(parname, "numerrs");
459 status=PILPutInt(parname, totalerr);
460 if(status) {
461 fprintf(stderr,"Error to update the numerrs keyword.\n");
462 status = 0;
463 }
464 }
465
466