예제 #1
0
파일: fitsutils.c 프로젝트: sfabbro/allphot
int fits_exist_key(fitsfile *fptr, char *keyname) {
  /* bulletproof */
  if (!fptr || !keyname) return 0;

  static char keyval[FLEN_VALUE];
  int status = 0;
  return fits_read_keyword(fptr, keyname, keyval, 0, &status) == KEY_NO_EXIST ? 0: 1;
}
예제 #2
0
std::string FitsFile::GetKeywordValue(const std::string &keywordName)
{
	char keyValue[FLEN_VALUE];
	int status = 0;
	fits_read_keyword(_fptr, const_cast<char *>(keywordName.c_str()), keyValue, NULL, &status);
	CheckStatus(status);
	std::string val(keyValue);
	if(val.length() >= 2 && *val.begin()=='\'' && *val.rbegin()=='\'')
	{
		val = val.substr(1, val.length()-2);
		boost::trim(val);
	}
	return val;
}
예제 #3
0
bool FitsFile::GetKeywordValue(const std::string &keywordName, std::string &value)
{
	char keyValue[FLEN_VALUE];
	int status = 0;
	fits_read_keyword(_fptr, const_cast<char *>(keywordName.c_str()), keyValue, NULL, &status);
	if(status == 0) {
	  value = std::string(keyValue);
	  if(value.length() >= 2 && *value.begin()=='\'' && *value.rbegin()=='\'')
	    {
	      value = value.substr(1, value.length()-2);
	      boost::trim(value);
	    }
	  return true;
	} else {
	  return false;
	}
}
예제 #4
0
  int understands_wmap( KConfig*, const QString& filename )
  {
    fitsfile* ffits;
    int       iStatus = 0;
    int       iRetVal = 0;

    //
    // determine if it is a WMAP file...
    //
    if( fits_open_file( &ffits, filename.ascii( ), READONLY, &iStatus ) == 0 )
    {
      int iNumHeaderDataUnits;

      if( fits_get_num_hdus( ffits, &iNumHeaderDataUnits, &iStatus ) == 0 )
      {
        if( iNumHeaderDataUnits == 6 )
        {
          char  value[FLEN_VALUE];
          char  comment[FLEN_COMMENT];
          int   iHDUType;
          int   iColNum;
          int   i;
          int   j;

          for( i=0; i<iNumKeywords; ++i )
          {
            if( iStatus == 0 )
            {
              fits_read_keyword( ffits, keywords[i], value, comment, &iStatus );
            }
            else  
            {
              break;
            }
          }

          if( iStatus == 0 )
          {
            char **entries = metaDataHeaders;
            int iNumEntries = iNumMetaDataHeaders;

            for( i=0; i<iNumHeaderDataUnits-1; i++ )
            {
              switch (i)
              {
                case 0:
                  iNumEntries = iNumMetaDataHeaders;
                  entries = metaDataHeaders;
                  break;
                case 1:
                  iNumEntries = iNumScienceDataHeaders;
                  entries = scienceDataHeaders;
                  break;
                case 2:
                  iNumEntries = iNumAIHKDataHeaders;
                  entries = AIHKDataHeaders;
                  break;
                case 3:
                  iNumEntries = iNumDIHKDataHeaders;
                  entries = DIHKDataHeaders;
                  break;
                case 4:
                  iNumEntries = iNumLOSDataHeaders;
                  entries = LOSDataHeaders;
                  break;
                default:
                  iNumEntries = iNumMetaDataHeaders;
                  entries = metaDataHeaders;
                  break;
              }

              fits_movrel_hdu( ffits, 1, &iHDUType, &iStatus);
              if( iStatus == 0 && iHDUType == BINARY_TBL )
              {
                for( j=0; j<iNumEntries; ++j )
                {
                  if( iStatus == 0 )
                  {
                    fits_get_colnum( ffits, 0, entries[j], &iColNum, &iStatus );
                    if( strchr( entries[j], '#' ) != 0L &&  iStatus == COL_NOT_UNIQUE )
                    {
                      iStatus = 0;
                    }
                  }
                  else  
                  {
                    break;
                  }
                }
              }
            }
          }

          if( iStatus == 0 )
          {
            iRetVal = 99;
          }
        }
      }

      iStatus = 0;

      fits_close_file( ffits, &iStatus );
    }
    else
    {
      //
      // failed to open the file, so we can't understand it...
      //
    }

    return iRetVal;
  }
예제 #5
0
/* Print all the extension informations. */
void
fits_print_extension_info(struct fitsparams *p)
{
  uint16_t *ui16;
  fitsfile *fptr;
  gal_data_t *cols=NULL, *tmp;
  char **tstra, **estra, **sstra;
  size_t i, numext, *dsize, ndim;
  int j, nc, numhdu, hdutype, status=0, type;
  char *msg, *tstr=NULL, sstr[1000], extname[FLEN_VALUE];


  /* Open the FITS file and read the first extension type, upon moving to
     the next extension, we will read its type, so for the first we will
     need to do it explicitly. */
  fptr=gal_fits_hdu_open(p->filename, "0", READONLY);
  if (fits_get_hdu_type(fptr, &hdutype, &status) )
    gal_fits_io_error(status, "reading first extension");


  /* Get the number of HDUs. */
  if( fits_get_num_hdus(fptr, &numhdu, &status) )
    gal_fits_io_error(status, "finding number of HDUs");
  numext=numhdu;


  /* Allocate all the columns (in reverse order, since this is a simple
     linked list). */
  gal_list_data_add_alloc(&cols, NULL, GAL_TYPE_STRING, 1, &numext, NULL, 1,
                          p->cp.minmapsize, "HDU_SIZE", "name", "Size of "
                          "image or table number of rows and columns.");
  gal_list_data_add_alloc(&cols, NULL, GAL_TYPE_STRING, 1, &numext, NULL, 1,
                          p->cp.minmapsize, "HDU_TYPE", "name", "Image "
                          "data type or `table' format (ASCII or binary).");
  gal_list_data_add_alloc(&cols, NULL, GAL_TYPE_STRING, 1, &numext, NULL, 1,
                          p->cp.minmapsize, "EXTNAME", "name",
                          "Extension name of this HDU (EXTNAME in FITS).");
  gal_list_data_add_alloc(&cols, NULL, GAL_TYPE_UINT16, 1, &numext, NULL, 1,
                          p->cp.minmapsize, "HDU_INDEX", "count", "Index "
                          "(starting from zero) of each HDU (extension).");


  /* Keep pointers to the array of each column for easy writing. */
  ui16  = cols->array;
  estra = cols->next->array;
  tstra = cols->next->next->array;
  sstra = cols->next->next->next->array;

  cols->next->disp_width=15;
  cols->next->next->disp_width=15;


  /* Fill in each column. */
  for(i=0;i<numext;++i)
    {
      /* Work based on the type of the extension. */
      switch(hdutype)
        {
        case IMAGE_HDU:
          gal_fits_img_info(fptr, &type, &ndim, &dsize, NULL, NULL);
          tstr=gal_type_name(type , 1);
          break;

        case ASCII_TBL:
        case BINARY_TBL:
          ndim=2;
          tstr = hdutype==ASCII_TBL ? "table_ascii" : "table_binary";
          dsize=gal_data_malloc_array(GAL_TYPE_SIZE_T, 2, __func__, "dsize");
          gal_fits_tab_size(fptr, dsize+1, dsize);
          break;

        default:
          error(EXIT_FAILURE, 0, "%s: a bug! the `hdutype' code %d not "
                "recognized", __func__, hdutype);
        }


      /* Read the extension name*/
      fits_read_keyword(fptr, "EXTNAME", extname, NULL, &status);
      switch(status)
        {
        case 0:
          gal_fits_key_clean_str_value(extname);
          break;

        case KEY_NO_EXIST:
          sprintf(extname, "%s", GAL_BLANK_STRING);
          status=0;
          break;

        default:
          gal_fits_io_error(status, "reading EXTNAME keyword");
        }
      status=0;


      /* Write the size into a string. `sprintf' returns the number of
         written characters (excluding the `\0'). So for each dimension's
         size that is written, we add to `nc' (the number of
         characters). Note that FITS allows blank extensions, in those
         cases, return "0". */
      if(ndim>0)
        {
          nc=0;
          for(j=ndim-1;j>=0;--j)
            nc += sprintf(sstr+nc, "%zux", dsize[j]);
          sstr[nc-1]='\0';
          free(dsize);
        }
      else
        {
          sstr[0]='0';
          sstr[1]='\0';
        }


      /* Write the strings into the columns. */
      j=0;
      for(tmp=cols; tmp!=NULL; tmp=tmp->next)
        {
          switch(j)
            {
            case 0: ui16[i]=i;                                    break;
            case 1: gal_checkset_allocate_copy(extname, estra+i); break;
            case 2: gal_checkset_allocate_copy(tstr, tstra+i);    break;
            case 3: gal_checkset_allocate_copy(sstr, sstra+i);    break;
            }
          ++j;
        }


      /* Move to the next extension if we aren't on the last extension. */
      if( i!=numext-1 && fits_movrel_hdu(fptr, 1, &hdutype, &status) )
        {
          asprintf(&msg, "moving to hdu %zu", i+1);
          gal_fits_io_error(status, msg);
        }
    }


  /* Print the resutls. */
  if(!p->cp.quiet)
    {
      printf("%s\nRun on %s-----\n", PROGRAM_STRING, ctime(&p->rawtime));
      printf("HDU (extension) information: `%s'.\n", p->filename);
      printf(" Column 1: Index (counting from 0, usable with `--hdu').\n");
      printf(" Column 2: Name (`EXTNAME' in FITS standard, usable with "
             "`--hdu').\n");
      printf(" Column 3: Image data type or `table' format (ASCII or "
             "binary).\n");
      printf(" Column 4: Size of data in HDU.\n");
      printf("-----\n");
    }
  gal_table_write(cols, NULL, GAL_TABLE_FORMAT_TXT, NULL, NULL);
  gal_list_data_free(cols);
}