Ejemplo n.º 1
0
BOOL CRarArchive::IsArchive()
//---------------------------
{
	struct MarkHeader MarkHead;
	int SFXLen,ArcType;
  
	SFXLen=ArcType=SolidType=LockedType=0;
	ArcFormat=0;

	if (tread(MarkHead.Mark,SIZEOF_MARKHEAD)!=SIZEOF_MARKHEAD) return FALSE;
	if (MarkHead.Mark[0]==0x52 && MarkHead.Mark[1]==0x45 &&
		MarkHead.Mark[2]==0x7e && MarkHead.Mark[3]==0x5e)
	{
		ArcFormat=OLD;
		tseek(0,SEEK_SET);
		ReadHeader(MAIN_HEAD);
		ArcType=(OldMhd.Flags & MHD_MULT_VOL) ? VOL : ARC;
	} else
	if (MarkHead.Mark[0]==0x52 && MarkHead.Mark[1]==0x61 &&
		MarkHead.Mark[2]==0x72 && MarkHead.Mark[3]==0x21 &&
		MarkHead.Mark[4]==0x1a && MarkHead.Mark[5]==0x07 &&
		MarkHead.Mark[6]==0x00)
	{
		ArcFormat=NEW;
		if (ReadHeader(MAIN_HEAD)!=SIZEOF_NEWMHD) return FALSE;
		ArcType = (NewMhd.Flags & MHD_MULT_VOL) ? VOL : ARC;
	}
	if (ArcFormat==OLD)
	{
		MainHeadSize=SIZEOF_OLDMHD;
		NewMhd.Flags=OldMhd.Flags & 0x3f;
		NewMhd.HeadSize=OldMhd.HeadSize;
	} else
	{
		MainHeadSize=SIZEOF_NEWMHD;
		if ((UWORD)~HeaderCRC!=NewMhd.HeadCRC)
		{
			// Warning
			return FALSE; //?
		}
	}
	if (NewMhd.Flags & MHD_SOLID) SolidType=1;
	if (NewMhd.Flags & MHD_LOCK) LockedType=1;
	return (ArcType) ? TRUE : FALSE;
}
Ejemplo n.º 2
0
int main(int argc, char **argv)
{
   int    c;
   char   header[32768];
   char   temp  [MAXSTR];
   char   fmt   [MAXSTR];
   char   rfmt  [MAXSTR];
   char   pfmt  [MAXSTR];
   char   cfmt  [MAXSTR];
   char   ofile [MAXSTR];
   char   scale [MAXSTR];
   int    i, j;
   int    namelen, nimages, ntotal, stat;
   double xpos, ypos;
   double lon, lat;
   double oxpix, oypix;
   int    oxpixMin, oypixMin;
   int    oxpixMax, oypixMax;
   int    offscl, mode;
   int    ncols;
   FILE  *fraw;
   FILE  *fproj;
   FILE  *fcorr;


   /***************************************/
   /* Process the command-line parameters */
   /***************************************/

   debug   = 0;
   opterr  = 0;

   fstatus = stdout;

   while ((c = getopt(argc, argv, "ds:")) != EOF) 
   {
      switch (c) 
      {
         case 'd':
            debug = 1;
            break;

         case 's':
            if((fstatus = fopen(optarg, "w+")) == (FILE *)NULL)
            {
               printf("[struct stat=\"ERROR\", msg=\"Cannot open status file: %s\"]\n",
                  optarg);
               exit(1);
            }
            break;

         default:
	    printf("[struct stat=\"ERROR\", msg=\"Usage: %s [-d][-s statusfile] images.tbl hdr.template raw.tbl projected.tbl corrected.tbl\"]\n", argv[0]);
            exit(1);
            break;
      }
   }

   if (argc - optind < 5) 
   {
      printf("[struct stat=\"ERROR\", msg=\"Usage: %s [-d][-s statusfile] images.tbl hdr.template raw.tbl projected.tbl corrected.tbl\"]\n", argv[0]);
      exit(1);
   }

   strcpy(origimg_file,  argv[optind]);
   strcpy(template_file, argv[optind + 1]);
   strcpy(rawimg_file,   argv[optind + 2]);
   strcpy(projimg_file,  argv[optind + 3]);
   strcpy(corrimg_file,  argv[optind + 4]);

   checkHdr(template_file, 1, 0);

   if(debug)
   {
      printf("\norigimg_file   = [%s]\n", origimg_file);
      printf("template_file  = [%s]\n\n", template_file);
      printf("rawimg_file    = [%s]\n", rawimg_file);
      printf("projimg_file   = [%s]\n", projimg_file);
      printf("corrimg_file   = [%s]\n", corrimg_file);
      fflush(stdout);
   }


   /*************************************************/ 
   /* Process the output header template to get the */ 
   /* image size, coordinate system and projection  */ 
   /*************************************************/ 

   readTemplate(template_file);

   if(debug)
   {
      printf("\noutput.sys       =  %d\n",  output.sys);
      printf("output.epoch     =  %-g\n", output.epoch);
      printf("output proj      =  %s\n",  output.wcs->ptype);

      fflush(stdout);
   }


   /*********************************************/ 
   /* Open the image header metadata table file */
   /*********************************************/ 

   ncols = topen(origimg_file);

   if(ncols <= 0)
   {
      fprintf(fstatus, "[struct stat=\"ERROR\", msg=\"Invalid image metadata file: %s\"]\n",
         origimg_file);
      exit(1);
   }


   icntr    = tcol("cntr");
   ictype1  = tcol("ctype1");
   ictype2  = tcol("ctype2");
   iequinox = tcol("equinox");
   inl      = tcol("nl");
   ins      = tcol("ns");
   icrval1  = tcol("crval1");
   icrval2  = tcol("crval2");
   icrpix1  = tcol("crpix1");
   icrpix2  = tcol("crpix2");
   icdelt1  = tcol("cdelt1");
   icdelt2  = tcol("cdelt2");
   icrota2  = tcol("crota2");
   iepoch   = tcol("epoch");
   ifname   = tcol("fname");
   iscale   = tcol("scale");

   icd11    = tcol("cd1_1");
   icd12    = tcol("cd1_2");
   icd21    = tcol("cd2_1");
   icd22    = tcol("cd2_2");

   if(ins < 0)
      ins = tcol("naxis1");

   if(inl < 0)
      inl = tcol("naxis2");

   if(ifname < 0)
      ifname = tcol("file");

   if(icd11 >= 0 && icd12 >= 0  && icd21 >= 0  && icd12 >= 0)
      mode = CD;

   else if(icdelt1 >= 0 && icdelt2 >= 0  && icrota2 >= 0)
      mode = CDELT;

   else
   {
      fprintf(fstatus, "[struct stat=\"ERROR\", msg=\"Not enough information to determine coverages (CDELTs or CD matrix)\"]\n");
      exit(1);
   }


   if(icntr   < 0
   || ictype1 < 0
   || ictype2 < 0
   || inl     < 0
   || ins     < 0
   || icrval1 < 0
   || icrval2 < 0
   || icrpix1 < 0
   || icrpix2 < 0
   || ifname  < 0)
   {
      fprintf(fstatus, "[struct stat=\"ERROR\", msg=\"Need columns: cntr ctype1 ctype2 nl ns crval1 crval2 crpix1 crpix2 cdelt1 cdelt2 crota2 fname (equinox optional)\"]\n");
      exit(1);
   }


   /******************************************************/
   /* Scan the table to get the true 'file' column width */
   /******************************************************/

   namelen = 0;

   while(1)
   {
      stat = tread();

      if(stat < 0)
	 break;

      strcpy(input.fname, fileName(tval(ifname)));

      if(strlen(input.fname) > namelen)
	 namelen = strlen(input.fname);
   }

   tseek(0);


   /*************************************/
   /* Write headers to the output files */
   /*************************************/

   if((fraw = (FILE *)fopen(rawimg_file, "w+")) == (FILE *)NULL)
   {
      fprintf(fstatus, "[struct stat=\"ERROR\", msg=\"Invalid output metadata file: %s\"]\n",
         rawimg_file);
      exit(1);
   }

   fprintf(fraw, "\\datatype=fitshdr\n");

   if(iscale >= 0)
   {
      sprintf(fmt, "|%%5s|%%8s|%%8s|%%6s|%%6s|%%10s|%%10s|%%10s|%%10s|%%11s|%%11s|%%8s|%%7s|%%10s|%%%ds|\n", namelen+2);

      fprintf(fraw, fmt,
	 "cntr",
	 "ctype1",
	 "ctype2",
	 "naxis1",
	 "naxis2",
	 "crval1",
	 "crval2",
	 "crpix1",
	 "crpix2",
	 "cdelt1",
	 "cdelt2",
	 "crota2",
	 "equinox",
	 "scale",
	 "file");

      fprintf(fraw, fmt,
	 "int",
	 "char",
	 "char",
	 "int",
	 "int",
	 "double",
	 "double",
	 "double",
	 "double",
	 "double",
	 "double",
	 "double",
	 "int",
	 "double",
	 "char");
   }
   else
   {
      sprintf(fmt, "|%%5s|%%8s|%%8s|%%6s|%%6s|%%10s|%%10s|%%10s|%%10s|%%11s|%%11s|%%8s|%%7s|%%%ds|\n", namelen+2);


      fprintf(fraw, fmt,
	 "cntr",
	 "ctype1",
	 "ctype2",
	 "naxis1",
	 "naxis2",
	 "crval1",
	 "crval2",
	 "crpix1",
	 "crpix2",
	 "cdelt1",
	 "cdelt2",
	 "crota2",
	 "equinox",
	 "file");

      fprintf(fraw, fmt,
	 "int",
	 "char",
	 "char",
	 "int",
	 "int",
	 "double",
	 "double",
	 "double",
	 "double",
	 "double",
	 "double",
	 "double",
	 "int",
	 "char");
   }

   if((fproj = (FILE *)fopen(projimg_file, "w+")) == (FILE *)NULL)
   {
      fprintf(fstatus, "[struct stat=\"ERROR\", msg=\"Invalid output metadata file: %s\"]\n",
         projimg_file);
      exit(1);
   }

   fprintf(fproj, "\\datatype=fitshdr\n");

   sprintf(fmt, "|%%5s|%%8s|%%8s|%%6s|%%6s|%%10s|%%10s|%%10s|%%10s|%%11s|%%11s|%%8s|%%7s|%%%ds|\n", namelen+2);

   fprintf(fproj, fmt,
      "cntr",
      "ctype1",
      "ctype2",
      "naxis1",
      "naxis2",
      "crval1",
      "crval2",
      "crpix1",
      "crpix2",
      "cdelt1",
      "cdelt2",
      "crota2",
      "equinox",
      "file");

   fprintf(fproj, fmt,
      "int",
      "char",
      "char",
      "int",
      "int",
      "double",
      "double",
      "double",
      "double",
      "double",
      "double",
      "double",
      "int",
      "char");


   if((fcorr = (FILE *)fopen(corrimg_file, "w+")) == (FILE *)NULL)
   {
      fprintf(fstatus, "[struct stat=\"ERROR\", msg=\"Invalid output metadata file: %s\"]\n",
         corrimg_file);
      exit(1);
   }

   fprintf(fcorr, "\\datatype=fitshdr\n");

   fprintf(fcorr, fmt,
      "cntr",
      "ctype1",
      "ctype2",
      "naxis1",
      "naxis2",
      "crval1",
      "crval2",
      "crpix1",
      "crpix2",
      "cdelt1",
      "cdelt2",
      "crota2",
      "equinox",
      "file");

   fprintf(fcorr, fmt,
      "int",
      "char",
      "char",
      "int",
      "int",
      "double",
      "double",
      "double",
      "double",
      "double",
      "double",
      "double",
      "int",
      "char");


   /************************************************/
   /* Read the metadata and process each image WCS */
   /************************************************/

   namelen = 0;
   nimages = 0;
   ntotal  = 0;

   if(iscale >= 0)
      sprintf(rfmt, " %%5d %%8s %%8s %%6d %%6d %%10.6f %%10.6f %%10.2f %%10.2f %%11.8f %%11.8f %%8.5f %%7.0f %%10s %%%ds\n", namelen+2);
   else
      sprintf(rfmt, " %%5d %%8s %%8s %%6d %%6d %%10.6f %%10.6f %%10.2f %%10.2f %%11.8f %%11.8f %%8.5f %%7.0f %%%ds\n", namelen+2);

   sprintf(pfmt, " %%5d %%8s %%8s %%6d %%6d %%10.6f %%10.6f %%10.2f %%10.2f %%11.8f %%11.8f %%8.5f %%7.0f p%%%ds\n", namelen+2);

   sprintf(cfmt, " %%5d %%8s %%8s %%6d %%6d %%10.6f %%10.6f %%10.2f %%10.2f %%11.8f %%11.8f %%8.5f %%7.0f c%%%ds\n", namelen+2);

   while(1)
   {
      stat = tread();

      if(stat < 0)
	 break;
      
      ++ntotal;

      strcpy(input.ctype1, tval(ictype1));
      strcpy(input.ctype2, tval(ictype2));

      input.cntr      = atoi(tval(icntr));
      input.naxis1    = atoi(tval(ins));
      input.naxis2    = atoi(tval(inl));
      input.crpix1    = atof(tval(icrpix1));
      input.crpix2    = atof(tval(icrpix2));
      input.crval1    = atof(tval(icrval1));
      input.crval2    = atof(tval(icrval2));

      if(mode == CDELT)
      {
	 input.cdelt1    = atof(tval(icdelt1));
	 input.cdelt2    = atof(tval(icdelt2));
	 input.crota2    = atof(tval(icrota2));
      }
      else
      {
	 input.cd11      = atof(tval(icd11));
	 input.cd12      = atof(tval(icd12));
	 input.cd21      = atof(tval(icd21));
	 input.cd22      = atof(tval(icd22));
      }

      input.epoch     = 2000;

      strcpy(header, "");
      sprintf(temp, "SIMPLE  = T"                    ); stradd(header, temp);
      sprintf(temp, "BITPIX  = -64"                  ); stradd(header, temp);
      sprintf(temp, "NAXIS   = 2"                    ); stradd(header, temp);
      sprintf(temp, "NAXIS1  = %d",     input.naxis1 ); stradd(header, temp);
      sprintf(temp, "NAXIS2  = %d",     input.naxis2 ); stradd(header, temp);
      sprintf(temp, "CTYPE1  = '%s'",   input.ctype1 ); stradd(header, temp);
      sprintf(temp, "CTYPE2  = '%s'",   input.ctype2 ); stradd(header, temp);
      sprintf(temp, "CRVAL1  = %11.6f", input.crval1 ); stradd(header, temp);
      sprintf(temp, "CRVAL2  = %11.6f", input.crval2 ); stradd(header, temp);
      sprintf(temp, "CRPIX1  = %11.6f", input.crpix1 ); stradd(header, temp);
      sprintf(temp, "CRPIX2  = %11.6f", input.crpix2 ); stradd(header, temp);

      if(mode == CDELT)
      {
      sprintf(temp, "CDELT1  = %11.6f", input.cdelt1 ); stradd(header, temp);
      sprintf(temp, "CDELT2  = %11.6f", input.cdelt2 ); stradd(header, temp);
      sprintf(temp, "CROTA2  = %11.6f", input.crota2 ); stradd(header, temp);
      }
      else
      {
      sprintf(temp, "CD1_1   = %11.6f", input.cd11   ); stradd(header, temp);
      sprintf(temp, "CD1_2   = %11.6f", input.cd12   ); stradd(header, temp);
      sprintf(temp, "CD2_1   = %11.6f", input.cd21   ); stradd(header, temp);
      sprintf(temp, "CD2_2   = %11.6f", input.cd22   ); stradd(header, temp);
      }

      sprintf(temp, "EQUINOX = %d",     input.equinox); stradd(header, temp);
      sprintf(temp, "END"                            ); stradd(header, temp);
      
      if(iequinox >= 0)
	 input.equinox = atoi(tval(iequinox));

      strcpy(input.fname, fileName(tval(ifname)));

      if(iscale >= 0)
	 strcpy(scale, tval(iscale));

      if(strlen(input.fname) > namelen)
	 namelen = strlen(input.fname);

      if(debug)
      {
	 printf("Image header to wcsinit():\n%s\n", header);
	 fflush(stdout);
      }

      input.wcs = wcsinit(header);

      checkWCS(input.wcs, 0);
			     
      if(input.wcs == (struct WorldCoor *)NULL)
      {
	 fprintf(fstatus, "[struct stat=\"ERROR\", msg=\"Bad WCS for image %d\"]\n", 
	    nimages);
	 exit(1);
      }


      /***************************************************/
      /* Check the boundaries of the input image against */
      /* the output region of interest                   */
      /***************************************************/

      oxpixMin =  100000000;
      oxpixMax = -100000000;
      oypixMin =  100000000;
      oypixMax = -100000000;

      /* Check input left and right */

      for (j=0; j<input.naxis2+1; ++j)
      {
	 pix2wcs(input.wcs, 0.5, j+0.5, &xpos, &ypos);

	 convertCoordinates(input.sys, input.epoch, xpos, ypos,
			    output.sys, output.epoch, &lon, &lat, 0.0);
	 
	 wcs2pix(output.wcs, lon, lat, &oxpix, &oypix, &offscl);

	 if(!offscl)
	 {
	    if(oxpix < oxpixMin) oxpixMin = oxpix;
	    if(oxpix > oxpixMax) oxpixMax = oxpix;
	    if(oypix < oypixMin) oypixMin = oypix;
	    if(oypix > oypixMax) oypixMax = oypix;
	 }

	 pix2wcs(input.wcs, input.naxis1+0.5, j+0.5, &xpos, &ypos);

	 convertCoordinates(input.sys, input.epoch, xpos, ypos,
			    output.sys, output.epoch, &lon, &lat, 0.0);
	 
	 wcs2pix(output.wcs, lon, lat, &oxpix, &oypix, &offscl);

	 if(!offscl)
	 {
	    if(oxpix < oxpixMin) oxpixMin = oxpix;
	    if(oxpix > oxpixMax) oxpixMax = oxpix;
	    if(oypix < oypixMin) oypixMin = oypix;
	    if(oypix > oypixMax) oypixMax = oypix;
	 }
      }


      /* Check input top and bottom */

      for (i=0; i<input.naxis1+1; ++i)
      {
	 pix2wcs(input.wcs, i+0.5, 0.5, &xpos, &ypos);

	 convertCoordinates(input.sys, input.epoch, xpos, ypos,
			    output.sys, output.epoch, &lon, &lat, 0.0);
	 
	 wcs2pix(output.wcs, lon, lat, &oxpix, &oypix, &offscl);

	 if(!offscl)
	 {
	    if(oxpix < oxpixMin) oxpixMin = oxpix;
	    if(oxpix > oxpixMax) oxpixMax = oxpix;
	    if(oypix < oypixMin) oypixMin = oypix;
	    if(oypix > oypixMax) oypixMax = oypix;
	 }

	 pix2wcs(input.wcs, i+0.5, input.naxis2+0.5, &xpos, &ypos);

	 convertCoordinates(input.sys, input.epoch, xpos, ypos,
			    output.sys, output.epoch, &lon, &lat, 0.0);
	 
	 wcs2pix(output.wcs, lon, lat, &oxpix, &oypix, &offscl);

	 if(!offscl)
	 {
	    if(oxpix < oxpixMin) oxpixMin = oxpix;
	    if(oxpix > oxpixMax) oxpixMax = oxpix;
	    if(oypix < oypixMin) oypixMin = oypix;
	    if(oypix > oypixMax) oypixMax = oypix;
	 }
      }


      /***************************************************/
      /* Check the boundaries of the region of interest  */
      /* against the input image                         */
      /***************************************************/

      /* Check ouput left and right */

      for (j=0; j<output.wcs->nypix+1; ++j)
      {
	 pix2wcs(output.wcs, 0.5, j+0.5, &xpos, &ypos);

	 convertCoordinates(output.sys, output.epoch, xpos, ypos,
			     input.sys,  input.epoch, &lon, &lat, 0.0);
	 
	 wcs2pix(input.wcs, lon, lat, &oxpix, &oypix, &offscl);

	 if(!offscl)
	 {
	    if(0.5   < oxpixMin) oxpixMin = 0.5;
	    if(0.5   > oxpixMax) oxpixMax = 0.5;

	    if(j+0.5 < oypixMin) oypixMin = j+0.5;
	    if(j+0.5 > oypixMax) oypixMax = j+0.5;
	 }

	 pix2wcs(output.wcs, output.wcs->nxpix+0.5, j+0.5, &xpos, &ypos);

	 convertCoordinates(output.sys, output.epoch, xpos, ypos,
			     input.sys,  input.epoch, &lon, &lat, 0.0);
	 
	 wcs2pix(input.wcs, lon, lat, &oxpix, &oypix, &offscl);

	 if(!offscl)
	 {
	    if(output.wcs->nxpix+0.5 < oxpixMin) oxpixMin = output.wcs->nxpix+0.5;
	    if(output.wcs->nxpix+0.5 > oxpixMax) oxpixMax = output.wcs->nxpix+0.5;

	    if(j+0.5 < oypixMin) oypixMin = j+0.5;
	    if(j+0.5 > oypixMax) oypixMax = j+0.5;
	 }
      }


      /* Check input top and bottom */

      for (i=0; i<output.wcs->nxpix+1; ++i)
      {
	 pix2wcs(output.wcs, i+0.5, 0.5, &xpos, &ypos);

	 convertCoordinates(output.sys, output.epoch, xpos, ypos,
			     input.sys,  input.epoch, &lon, &lat, 0.0);
	 
	 wcs2pix(input.wcs, lon, lat, &oxpix, &oypix, &offscl);

	 if(!offscl)
	 {
	    if(i+0.5 < oxpixMin) oxpixMin = i+0.5;
	    if(i+0.5 > oxpixMax) oxpixMax = i+0.5;

	    if(0.5   < oypixMin) oypixMin = 0.5  ;
	    if(0.5   > oypixMax) oypixMax = 0.5  ;
	 }

	 pix2wcs(output.wcs, i+0.5, output.wcs->nypix+0.5, &xpos, &ypos);

	 convertCoordinates(output.sys, output.epoch, xpos, ypos,
			     input.sys,  input.epoch, &lon, &lat, 0.0);
	 
	 wcs2pix(input.wcs, lon, lat, &oxpix, &oypix, &offscl);

	 if(!offscl)
	 {
	    if(i+0.5 < oxpixMin) oxpixMin = i+0.5;
	    if(i+0.5 > oxpixMax) oxpixMax = i+0.5;

	    if(output.wcs->nypix+0.5 < oypixMin) oypixMin = output.wcs->nypix+0.5;
	    if(output.wcs->nypix+0.5 > oypixMax) oypixMax = output.wcs->nypix+0.5;
	 }
      }

      if(oxpixMax < oxpixMin) continue;
      if(oypixMax < oypixMin) continue;


      /* Remove any possible compression extension */

      strcpy(ofile, input.fname);

      if(strlen(ofile) > 3 && strcmp(ofile+strlen(ofile)-3, ".gz") == 0)
	 ofile[strlen(ofile)-3] = '\0';

      else if(strlen(ofile) > 2 && strcmp(ofile+strlen(ofile)-2, ".Z") == 0)
	 ofile[strlen(ofile)-2] = '\0';

      else if(strlen(ofile) > 2 && strcmp(ofile+strlen(ofile)-2, ".z") == 0)
	 ofile[strlen(ofile)-2] = '\0';

      else if(strlen(ofile) > 4 && strcmp(ofile+strlen(ofile)-4, ".zip") == 0)
	 ofile[strlen(ofile)-4] = '\0';

      else if(strlen(ofile) > 2 && strcmp(ofile+strlen(ofile)-2, "-z") == 0)
	 ofile[strlen(ofile)-2] = '\0';

      else if(strlen(ofile) > 3 && strcmp(ofile+strlen(ofile)-3, "-gz") == 0)
	 ofile[strlen(ofile)-3] = '\0';


      /* Make sure the extension is ".fits" */

      if(strlen(ofile) > 5 && strcmp(ofile+strlen(ofile)-5, ".fits") == 0)
	 ofile[strlen(ofile)-5] = '\0';

      else if(strlen(ofile) > 5 && strcmp(ofile+strlen(ofile)-5, ".FITS") == 0)
	 ofile[strlen(ofile)-5] = '\0';

      else if(strlen(ofile) > 4 && strcmp(ofile+strlen(ofile)-4, ".fit") == 0)
	 ofile[strlen(ofile)-4] = '\0';

      else if(strlen(ofile) > 4 && strcmp(ofile+strlen(ofile)-4, ".FIT") == 0)
	 ofile[strlen(ofile)-4] = '\0';

      else if(strlen(ofile) > 4 && strcmp(ofile+strlen(ofile)-4, ".fts") == 0)
	 ofile[strlen(ofile)-4] = '\0';

      else if(strlen(ofile) > 4 && strcmp(ofile+strlen(ofile)-4, ".FTS") == 0)
	 ofile[strlen(ofile)-4] = '\0';

      strcat(ofile, ".fits");

      if(iscale >= 0)
      {
	 fprintf(fraw, rfmt,
	   nimages+1,
	   output.wcs->ctype[0],
	   output.wcs->ctype[1],
	   oxpixMax - oxpixMin + 1,
	   oypixMax - oypixMin + 1,
	   output.wcs->crval[0],
	   output.wcs->crval[1],
	   output.wcs->crpix[0] - oxpixMin,
	   output.wcs->crpix[1] - oypixMin,
	   output.wcs->cdelt[0],
	   output.wcs->cdelt[1],
	   output.wcs->rot,
	   output.epoch,
	   scale,
	   ofile);
      }
      else
      {
	 fprintf(fraw, rfmt,
	   nimages+1,
	   output.wcs->ctype[0],
	   output.wcs->ctype[1],
	   oxpixMax - oxpixMin + 1,
	   oypixMax - oypixMin + 1,
	   output.wcs->crval[0],
	   output.wcs->crval[1],
	   output.wcs->crpix[0] - oxpixMin,
	   output.wcs->crpix[1] - oypixMin,
	   output.wcs->cdelt[0],
	   output.wcs->cdelt[1],
	   output.wcs->rot,
	   output.epoch,
	   ofile);
      }

      fprintf(fproj, pfmt,
	nimages+1,
	output.wcs->ctype[0],
	output.wcs->ctype[1],
	oxpixMax - oxpixMin + 1,
	oypixMax - oypixMin + 1,
	output.wcs->crval[0],
	output.wcs->crval[1],
	output.wcs->crpix[0] - oxpixMin,
	output.wcs->crpix[1] - oypixMin,
	output.wcs->cdelt[0],
	output.wcs->cdelt[1],
	output.wcs->rot,
	output.epoch,
	ofile);

      fprintf(fcorr, cfmt,
	nimages+1,
	output.wcs->ctype[0],
	output.wcs->ctype[1],
	oxpixMax - oxpixMin + 1,
	oypixMax - oypixMin + 1,
	output.wcs->crval[0],
	output.wcs->crval[1],
	output.wcs->crpix[0] - oxpixMin,
	output.wcs->crpix[1] - oypixMin,
	output.wcs->cdelt[0],
	output.wcs->cdelt[1],
	output.wcs->rot,
	output.epoch,
	ofile);

      ++nimages;
   }


   fclose(fraw);
   fclose(fproj);
   fclose(fcorr);

   fprintf(fstatus, "[struct stat=\"OK\", count=\"%d\", total=\"%d\"]\n", 
      nimages, ntotal);
   fflush(stdout);

   exit(0);
}
Ejemplo n.º 3
0
int topen(char *fname)
{
   int         i, j, k, l, headlen, isblank;
   int         headlent, ncolt, nscan;
   char       *ptr, *kptr, *vptr;
   struct stat buf;
   char       *scan;

   tclear();

   /* Quick scan to determine table */
   /* record width                  */

   if(mtbl_maxline == 0)
   {
      if(tdebug)
      {
     printf("TDEBUG> Checking %s for header width<br>\n", fname);
     fflush(stdout);
      }

      nscan = MTBL_MAXLINE;

      while(1)
      {
     scan = (char *)malloc(nscan * sizeof(char));

     if(scan != (char *)NULL)
        break;

     if(nscan < 1024)
        return(MTBL_MALLOC);
      }

      if(tWrite)
	 tfile = fopen(fname, "r+");
      else
	 tfile = fopen(fname, "r");

      if (tfile == (FILE *)NULL)
     return(MTBL_NOFILE);

      while(1)
      {
     if(fgets(scan, nscan, tfile) == (char *)NULL)
        return (MTBL_RDERR);

     if(tdebug)
     {
        printf("TDEBUG> Scanning [%s]<br>\n", scan);
        fflush(stdout);
     }

     if(scan[0] == '|')
     {
        mtbl_maxline = 2 * strlen(scan);

        if(tdebug)
        {
           printf("TDEBUG> Scan -> max line length = %d<br>\n",
          mtbl_maxline);
           fflush(stdout);
        }

        break;
     }
      }

      if(mtbl_maxline < MTBL_MAXSTR)
      {
     mtbl_maxline = MTBL_MAXSTR;

     if(tdebug)
     {
        printf("TDEBUG> No header found -> max line length defaults to %d<br>\n",
           mtbl_maxline);
        fflush(stdout);
     }
      }

      free(scan);

      rewind(tfile);
      fclose(tfile);
   }

   if(tdebug)
   {
      printf("TDEBUG> Max line length = %d<br>\n", mtbl_maxline);
      fflush(stdout);
   }


   /* Allocate space for working strings */

   if (mtbl_linelen < mtbl_maxline)
   {
      if(tdebug)
      {
     printf("TDEBUG> Malloc %d character strings (tbl_hdr_len, etc.)<br>\n",
        mtbl_maxline);
     fflush(stdout);
      }
      tbl_rec_string = (char *)realloc(tbl_rec_string, mtbl_maxline * sizeof(char));
      tbl_hdr_string = (char *)realloc(tbl_hdr_string, mtbl_maxline * sizeof(char));
      tbl_typ_string = (char *)realloc(tbl_typ_string, mtbl_maxline * sizeof(char));
      tbl_uni_string = (char *)realloc(tbl_uni_string, mtbl_maxline * sizeof(char));
      tbl_nul_string = (char *)realloc(tbl_nul_string, mtbl_maxline * sizeof(char));
      dval           = (char *)realloc(dval,           mtbl_maxline * sizeof(char));
      mtbl_linelen   = mtbl_maxline;
   }

   strcpy(tbl_rec_string, "");
   strcpy(tbl_hdr_string, "");
   strcpy(tbl_typ_string, "");
   strcpy(tbl_uni_string, "");
   strcpy(tbl_nul_string, "");

   strcpy(dval, "");


   if(mtbl_maxcol == 0)
   {
      mtbl_maxcol = MTBL_MAXCOL;

      tbl_rec = (struct TBL_REC *)malloc(mtbl_maxcol * sizeof(struct TBL_REC));

      for(i=0; i<mtbl_maxcol; ++i)
      {
     for(k=0; k<MTBL_MAXSTR; ++k)
     {
        tbl_rec[i].name[k] = '\0';
        tbl_rec[i].type[k] = '\0';
        tbl_rec[i].unit[k] = '\0';
        tbl_rec[i].nuls[k] = '\0';
     }

     tbl_rec[i].endcol = 0;
     tbl_rec[i].colwd  = 0;
      }
   }


   /* OPEN FILE */

   if(tWrite)
      tfile = fopen(fname, "r+");
   else
      tfile = fopen(fname, "r");

   if (tfile == (FILE *)NULL)
      return(MTBL_NOFILE);


   /******************************************************/
   /* READ HEADER, find columns and extract column names */
   /******************************************************/

   headbytes =  0;
   reclen    =  0;
   nrec      = -1;
   nkey      =  0;
   nhdr      =  0;

   haveType = 0;
   haveUnit = 0;
   haveNull = 0;


   /* Parse keyword lines */

   while(FOREVER)
   {
      if(fgets(dval, mtbl_maxline, tfile) == (char *) NULL)
     break;

      if(tdebug)
      {
     printf("TDEBUG> Read keyword header line [%s]<br>\n", dval);
     fflush(stdout);
      }

      reclen     = (int)strlen(dval);
      headbytes += reclen;

      if(dval[strlen(dval) - 1] == '\n')
         dval[strlen(dval) - 1]  = '\0';

      if(dval[strlen(dval) - 1] == '\r')
         dval[strlen(dval) - 1]  = '\0';

      isblank = 1;
      for(i=0; i<strlen(dval); ++i)
      {
     if(dval[i] != ' ' && dval[i] != '\t')
     {
        isblank = 0;
        break;
     }
      }

      if(!isblank && dval[0] != '\\')
     break;

      if(nkeyalloc <= nkey)
      {
     nkeyalloc += MTBL_MAXKEY;

     keystr  = (char **)realloc(keystr,  nkeyalloc * sizeof(char *));
     keyword = (char **)realloc(keyword, nkeyalloc * sizeof(char *));
     value   = (char **)realloc(value,   nkeyalloc * sizeof(char *));

     for(i=nkey; i<nkeyalloc; ++i)
     {
        keystr [i] = (char *)malloc(MTBL_KEYLEN * sizeof(char));
        keyword[i] = (char *)malloc(MTBL_KEYLEN * sizeof(char));
        value  [i] = (char *)malloc(MTBL_KEYLEN * sizeof(char));
     }
      }

      strcpy(keystr[nhdr], dval);
      ++nhdr;

      kptr = dval + 1;
      ptr  = kptr;

      while(*ptr != ' '
     && *ptr != '='
     && *ptr != '\0')
        ++ptr;

      while(*ptr == ' ')
      {
     *ptr = '\0';
     ++ptr;
      }

      if(*ptr != '=')
     continue;

      *ptr = '\0';
      ++ptr;

      while(*ptr == ' ')
         ++ptr;

      vptr = ptr;

      for(i=strlen(vptr)-1; i>=0; --i)
      {
     if(vptr[i] == ' ')
        vptr[i] = '\0';
     else
        break;
      }

      if(strlen(kptr) > 0)
      {
     strcpy(keyword[nkey], kptr);
     strcpy(value  [nkey], vptr);
     ++nkey;
      }
   }

   strcpy(tbl_hdr_string, dval);

   if(dval[0] == '|')
      dval[0] =  ' ';

   headlen = reclen;

   if(tbl_hdr_string[headlen - 1] == '\n')
      tbl_hdr_string[headlen - 1] =  '\0';


   /* Parse the header line for column names and sizes */

   ncol = 0;

   j = 0;

   headlen = (int)strlen(dval);

   for(i=0; i<headlen; ++i)
   {
      if (dval[i] == '\\')
     break;

      if (dval[i] == '\n')
     break;

      else if (dval[i] == '|')
      {
     tbl_rec[ncol].endcol = i;
     tbl_rec[ncol].name[j] = '\0';
     ++ncol;

     if(ncol > mtbl_maxcol)
     {
        mtbl_maxcol += MTBL_MAXCOL;

        tbl_rec = (struct TBL_REC *)
           realloc(tbl_rec, mtbl_maxcol * sizeof(struct TBL_REC));

        for(k=mtbl_maxcol-MTBL_MAXCOL; k<mtbl_maxcol; ++k)
        {
           for(l=0; l<MTBL_MAXSTR; ++l)
           {
          tbl_rec[k].name[l] = '\0';
          tbl_rec[k].type[l] = '\0';
          tbl_rec[k].unit[l] = '\0';
          tbl_rec[k].nuls[l] = '\0';
           }

           tbl_rec[k].endcol = 0;
           tbl_rec[k].colwd  = 0;
        }
     }

     j = 0;

         if(i == 0)
        --ncol;
      }

      else if (dval[i] != ' ' )
      {
     tbl_rec[ncol].name[j] = dval[i];
     ++j;
      }
   }

   tbl_rec[0].colwd = tbl_rec[0].endcol + 1;

   for (i=1; i<ncol; ++i)
    tbl_rec[i].colwd = tbl_rec[i].endcol - tbl_rec[i-1].endcol;


   /* Read any additional header lines */

   while(FOREVER)
   {
      if(fgets(dval, mtbl_maxline, tfile) == (char *) NULL)
     break;

      if(tdebug)
      {
     printf("TDEBUG> Read additional header [%s]<br>\n", dval);
     fflush(stdout);
      }

      if(dval[0] != '|')
     break;

      reclen     = (int)strlen(dval);
      headbytes += reclen;

      if(!haveType)
      {
     if(dval[strlen(dval) - 1] == '\n')
        dval[strlen(dval) - 1]  = '\0';

     if(dval[strlen(dval) - 1] == '\r')
        dval[strlen(dval) - 1]  = '\0';

     haveType = 1;
     strcpy(tbl_typ_string, dval);

     j     = 0;
     ncolt = 0;

     headlent = (int)strlen(dval);

     for(i=0; i<headlent; ++i)
     {
        if (dval[i] == '\\')
           break;

        if (dval[i] == '\n')
           break;

        else if (dval[i] == '|')
        {
           ++ncolt;

           if(ncolt > MTBL_MAXCOL)
          return(MTBL_COLUMN);

           j = 0;

           if(i == 0)
          --ncolt;
        }

        else if (dval[i] != ' ' )
        {
           tbl_rec[ncolt].type[j] = dval[i];
           ++j;
        }
     }
      }

      else if(!haveUnit)
      {
     if(dval[strlen(dval) - 1] == '\n')
        dval[strlen(dval) - 1]  = '\0';

     if(dval[strlen(dval) - 1] == '\r')
        dval[strlen(dval) - 1]  = '\0';

     haveUnit = 1;
     strcpy(tbl_uni_string, dval);

     j     = 0;
     ncolt = 0;

     headlent = (int)strlen(dval);

     for(i=0; i<headlent; ++i)
     {
        if (dval[i] == '\\')
           break;

        if (dval[i] == '\n')
           break;

        else if (dval[i] == '|')
        {
           ++ncolt;

           if(ncolt > MTBL_MAXCOL)
          return(MTBL_COLUMN);

           j = 0;

           if(i == 0)
          --ncolt;
        }

        else if (dval[i] != ' ' )
        {
           tbl_rec[ncolt].unit[j] = dval[i];
           ++j;
        }
     }
      }

      else if(!haveNull)
      {
     if(dval[strlen(dval) - 1] == '\n')
        dval[strlen(dval) - 1]  = '\0';

     if(dval[strlen(dval) - 1] == '\r')
        dval[strlen(dval) - 1]  = '\0';

     haveNull = 1;
     strcpy(tbl_nul_string, dval);

     j     = 0;
     ncolt = 0;

     headlent = (int)strlen(dval);

     for(i=0; i<headlent; ++i)
     {
        if (dval[i] == '\\')
           break;

        if (dval[i] == '\n')
           break;

        else if (dval[i] == '|')
        {
           ++ncolt;

           if(ncolt > MTBL_MAXCOL)
          return(MTBL_COLUMN);

           j = 0;

           if(i == 0)
          --ncolt;
        }

        else if (dval[i] != ' ' )
        {
           tbl_rec[ncolt].nuls[j] = dval[i];
           ++j;
        }
     }
      }
   }


   /* Read the first data line to get record length */

   if(fgets(dval, mtbl_maxline, tfile) != (char *) NULL)
   {
      if(tdebug)
      {
     printf("TDEBUG> Read data line [%s]<br>\n", dval);
     fflush(stdout);
      }

      reclen = (int)strlen(dval);
   }


   if(tdebug)
   {
       printf("TDEBUG> tbl_hdr_string = [%s]<br>\n", tbl_hdr_string);
       printf("TDEBUG> tbl_typ_string = [%s]<br>\n", tbl_typ_string);
       printf("TDEBUG> tbl_uni_string = [%s]<br>\n", tbl_uni_string);
       printf("TDEBUG> tbl_nul_string = [%s]<br>\n", tbl_nul_string);
       printf("TDEBUG> firsrt record  = [%s](%d)<br>\n", dval, reclen);
       fflush(stdout);

       for (i=0; i<ncol; ++i)
       {
          printf("<br>\n");
          printf("TDEBUG> Column %d:<br>\n",    i+1);
          printf("TDEBUG> name   = [%s]<br>\n", tbl_rec[i].name);
          printf("TDEBUG> type   = [%s]<br>\n", tbl_rec[i].type);
          printf("TDEBUG> unit   = [%s]<br>\n", tbl_rec[i].unit);
          printf("TDEBUG> endcol =  %d<br>\n",  tbl_rec[i].endcol);
          printf("TDEBUG> colwd  =  %d<br>\n",  tbl_rec[i].colwd);
          fflush(stdout);
       }
   }


   /* Estimate the number of records in the file */
   /*  (correct for fixed-record table files)    */

   if(stat(fname, &buf) != 0)
      nrec = -1;

   if(reclen > 0)
      nrec = (buf.st_size - headbytes) / reclen;
   else
      nrec = -1;


   /* Reset pointer and exit */

   tseek(0);

   tbl_headbytes = headbytes;
   tbl_reclen    = reclen;

   return(ncol);
}