File: fvrf_file.c

package info (click to toggle)
fitsverify 4.20-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 288 kB
  • sloc: ansic: 4,868; sh: 14; makefile: 9
file content (289 lines) | stat: -rw-r--r-- 7,635 bytes parent folder | download | duplicates (7)
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
#include "fverify.h"
static HduName  **hduname; 
static int total_err=1;  /* initialzed to 1 in case fail to open file */
static int total_warn=0;

int get_total_warn()
{
    return (total_warn);
}
int get_total_err()
{
    return (total_err);
}

/* Get the total hdu number and allocate the memory for hdu array */  
void init_hduname() 
{
    int i;
    /* allocate memories for the hdu structure array  */
    hduname = (HduName **)malloc(totalhdu*sizeof(HduName *));
    for (i=0; i < totalhdu; i++) { 
	hduname[i] = (HduName *)calloc(1, sizeof(HduName));
	hduname[i]->hdutype = -1;
        hduname[i]->errnum = 0; 
        hduname[i]->wrnno = 0;
        strcpy(hduname[i]->extname,"");
        hduname[i]->extver = 0;
    }
    return;
}
/* set the hduname memeber hdutype, extname, extver */
void set_hduname(  int hdunum,		/* hdu number */ 
		   int hdutype,		/* hdutype */
		   char* extname,	/* extension name */
                   int  extver 		/* extension version */
                )
{
    int i; 
    i = hdunum - 1;
    hduname[i]->hdutype = hdutype;
    if(extname!=NULL)
        strcpy (hduname[i]->extname,extname); 
    else 
        strcpy(hduname[i]->extname,"");
    hduname[i]->extver = extver;
    return;
} 


/* get the total errors and total warnings in this hdu */
void set_hduerr(int hdunum	/* hdu number */ 
                )
{
    int i; 
    i = hdunum - 1;
    num_err_wrn(&(hduname[i]->errnum), &(hduname[i]->wrnno));
    reset_err_wrn();   /* reset the error and warning counter */
    return;
} 

/* set the basic information for hduname structure */
void set_hdubasic(int hdunum,int hdutype)
{ 
   set_hduname(hdunum, hdutype, NULL, 0);
   set_hduerr(hdunum); 
   return;
}

/* test to see whether the two extension having the same name */
/* return 1: identical 0: different */
int test_hduname (int hdunum1,		/* index of first hdu */
		  int hdunum2		/* index of second hdu */
                  )
{ 
    HduName *p1;
    HduName *p2; 

    p1 = hduname[hdunum1-1];
    p2 = hduname[hdunum2-1];
    if(!strlen(p1->extname) || !strlen(p2->extname)) return 0;
    if(!strcmp(p1->extname,p2->extname) && p1->hdutype == p2->hdutype
       && p2->extver == p1->extver && hdunum1 != hdunum2){ 
	   return 1;
    } 
    return 0;
} 

/* Added the error numbers */
void total_errors (int *toterr, int * totwrn)  
{    
   int i = 0;
   int ierr, iwrn;
   *toterr = 0;
   *totwrn = 0;

   if (totalhdu == 0) { /* this means the file couldn't be opened */
       *toterr = 1;
       return;
   }

   for (i = 0; i < totalhdu; i++) { 
       *toterr += hduname[i]->errnum; 
       *totwrn += hduname[i]->wrnno;
   } 
   /*check the end of file errors */
   num_err_wrn(&ierr, &iwrn); 
   *toterr +=ierr; 
   *totwrn +=iwrn; 
   return;
}
    
/* print the extname, exttype, extver, errnum and wrnno in a  table */ 
void hdus_summary(FILE *out)
{
   HduName *p;
   int i;
   int ierr, iwrn;
   char temp[FLEN_VALUE];
   char temp1[FLEN_VALUE];

   wrtsep(out,'+'," Error Summary  ",60);
   wrtout(out," ");
   sprintf(comm," HDU#  Name (version)       Type             Warnings  Errors");
   wrtout(out,comm);

   sprintf(comm," 1                          Primary Array    %-4d      %-4d  ", 
	   hduname[0]->wrnno,hduname[0]->errnum); 
   wrtout(out,comm);
   for (i=2; i <= totalhdu; i++) { 
       p = hduname[i-1];
       strcpy(temp,p->extname);
       if(p->extver && p->extver!= -999) { 
           sprintf(temp1," (%-d)",p->extver);
           strcat(temp,temp1);
       }
       switch(hduname[i-1]->hdutype){ 
	   case IMAGE_HDU: 
               sprintf(comm," %-5d %-20s Image Array      %-4d      %-4d  ", 
	               i,temp, p->wrnno,p->errnum); 
               wrtout(out,comm); 
	       break;
	   case ASCII_TBL: 
               sprintf(comm," %-5d %-20s ASCII Table      %-4d      %-4d  ", 
	               i,temp, p->wrnno,p->errnum); 
               wrtout(out,comm); 
	       break;
	   case BINARY_TBL: 
               sprintf(comm," %-5d %-20s Binary Table     %-4d      %-4d  ", 
	               i,temp, p->wrnno,p->errnum); 
               wrtout(out,comm); 
	       break;
           default:
               sprintf(comm," %-5d %-20s Unknown HDU      %-4d      %-4d  ", 
	               i,temp, p->wrnno,p->errnum); 
               wrtout(out,comm); 
	       break; 
      }
   } 
   /* check the end of file */ 
   num_err_wrn(&ierr, &iwrn); 
   if (iwrn || ierr) {
     sprintf(comm," End-of-file %-30s  %-4d      %-4d  ", "", iwrn,ierr); 
     wrtout(out,comm); 
   }
   wrtout(out," ");
   return;
}

		   

void destroy_hduname() 
{ 
   int i;
   for (i=0; i < totalhdu; i++) free(hduname[i]);
   free(hduname);
   return;
} 

/* Routine to test the extra bytes at the end of file */ 
   void  test_end(fitsfile *infits, 
		  FILE *out) 

{   
   int status = 0; 
   LONGLONG headstart, datastart, dataend;
   int hdutype;

   /* check whether there are any HDU left */ 
   fits_movrel_hdu(infits,1, &hdutype, &status);
   if (!status) {
       wrtout(out,"< End-of-File >");
       sprintf(errmes, 
    "There are extraneous HDU(s) beyond the end of last HDU.");
       wrterr(out,errmes,2);
       wrtout(out," ");
       return;
   }

   if (status != END_OF_FILE) { 
      wrtserr(out,"Bad HDU? ",&status,2);
      return;
   } 

   status = 0;  
   fits_clear_errmsg();
   if(ffghadll(infits, &headstart, &datastart, &dataend, &status)) 
       wrtferr(out, "",&status,1);

   /* try to move to the last byte of this extension.  */
   if (ffmbyt(infits, dataend - 1,0,&status))
   {
       sprintf(errmes, 
   "Error trying to read last byte of the file at byte %ld.", (long) dataend);
       wrterr(out,errmes,2);
       wrtout(out,"< End-of-File >");
       wrtout(out," ");
       return;
   } 

   /* try to move to what would be the first byte of the next extension. 
     If successfull, we have a problem... */

   ffmbyt(infits, dataend,0,&status);
   if(status == 0) { 
       wrtout(out,"< End-of-File >");
       sprintf(errmes, 
     "File has extra byte(s) after last HDU at byte %ld.", (long) dataend);
       wrterr(out,errmes,2);
       wrtout(out," ");
   } 

   return;
}



/******************************************************************************
* Function
*      init_report
*
*
* DESCRIPTION:
*      Initialize the fverify report
*
*******************************************************************************/
void init_report(FILE *out,              /* output file */
                 char *rootnam          /* input file name */
                 )
{
    sprintf(comm,"\n%d Header-Data Units in this file.",totalhdu);
    wrtout(out,comm);
    wrtout(out," ");

    reset_err_wrn();
    init_hduname();
}

/******************************************************************************
* Function
*      close_report
*
*
* DESCRIPTION:
*      Close the fverify report
*
*******************************************************************************/
void close_report(FILE *out              /* output file */ )
{
    int numerrs = 0;                    /* number of the errors         */
    int numwrns = 0;                    /* number of the warnings       */

    /* print out a summary of all the hdus */
    if(prstat)hdus_summary(out);
    total_errors (&numerrs, &numwrns);

    total_warn = numwrns;
    total_err  = numerrs;

    /* get the total number of errors and warnnings */
    sprintf(comm,"**** Verification found %d warning(s) and %d error(s). ****",
              numwrns, numerrs);
    wrtout(out,comm);

    update_parfile(numerrs,numwrns);
    /* destroy the hdu name */
    destroy_hduname();
    return ;
}