bool LFIIOSource::initFile( ) { KstObject::UpdateType updateType; bool bRetVal = false; int iResult = 0; _numFrames = 0; // // read the metadata // if( !_filename.isNull( ) && !_filename.isEmpty( ) ) { QString str; fitsfile* ffits; int iStatus = 0; if( _first ) { iResult = fits_open_table( &ffits, _filename.toAscii( ), READONLY, &iStatus ); if( iResult == 0 ) { int keysexist; int morekeys; iResult = fits_get_hdrspace( ffits, &keysexist, &morekeys, &iStatus ); if( iResult == 0 ) { char keyname[FLEN_KEYWORD]; char value[FLEN_VALUE]; char comment[FLEN_COMMENT]; int keynum; for( keynum=1; keynum <= keysexist; ++keynum ) { iResult = fits_read_keyn( ffits, keynum, keyname, value, comment, &iStatus ); if( iResult == 0 ) { KstString *metaString; str.sprintf( "%s %s", value, comment ); metaString = new KstString( KstObjectTag( keyname, tag() ), this, str ); _metaData.insert( keyname, *metaString ); } } _first = false; } } } } updateType = update( ); if( updateType == KstObject::UPDATE ) { bRetVal = true; } return bRetVal; }
int LFIIOSource::readField( double *v, const QString& field, int s, int n ) { double dNan = strtod( "nan", NULL ); fitsfile* ffits; bool bOk; int i; int iCol; int iRead = -1; int iStatus = 0; int iAnyNull; int iResult = 0; if( n < 0 ) { n = 1; /* n < 0 means read one sample, not frame - irrelavent here */ } if( field == "INDEX" ) { for( i = 0; i < n; i++ ) { v[i] = (double)( s + i ); } iRead = n; } else { memset( v, 0, n * sizeof( double ) ); bOk = getColNumber( field, &iCol ); if( bOk ) { _valid = false; if( !_filename.isNull( ) && !_filename.isEmpty( ) ) { iResult = fits_open_table( &ffits, _filename.ascii( ), READONLY, &iStatus ); if( iResult == 0 ) { _valid = true; // // copy the data... // N.B. fitsio column indices are 1 based, so we ask for iCol+1 instead of just iCol // iResult = fits_read_col( ffits, TDOUBLE, iCol+1, s+1, 1, n, &dNan, v, &iAnyNull, &iStatus ); if( iResult == 0 ) { iRead = n; } iStatus = 0; fits_close_file( ffits, &iStatus ); } } } } return iRead; }
void InputFileFITS::open(const std::string &filename) { int status = 0; fits_open_table(&infptr, filename.c_str(), READONLY, &status); if (status) throwException("Error in InputFileFITS::Open() ", status); opened = true; }
void OutputFileFITS::open(const std::string &filename) { File::open(filename); int status = 0; fits_open_table(&infptr, filename.c_str(), READWRITE, &status); if (status) throwException("Error in OutputFileFITS::open() ", status); opened = true; }
// Reads a Healpix FITS file containing the map2alm weights into a double array: int ReadHealpixWeights(int col, int nside, double *weights) { char message[200]; std::string filename; fitsfile *fpointer; int status=0, anynul=0; long i, firstrow=1, firstelem=1, nelements=2*nside; double *nulval; filename = HEALPIX_DATA; if (filename.at(filename.length()-1)!='/') filename = filename+"/"; filename = filename+"weight_ring_n"+ZeroPad(nside, 10000)+".fits"; // Open file: fits_open_table(&fpointer, filename.c_str(), READONLY, &status); if (status!=0) { sprintf(message, "ReadHealpixWeights: could not open table in FITS file, ERR=%d", status); warning(message); } // Prepare to, read and check for errors: nulval = vector<double>(0, nelements-1); for(i=0; i<nelements; i++) nulval[i]=666.0; fits_read_col(fpointer, TDOUBLE, col, firstrow, firstelem, nelements, nulval, weights, &anynul, &status); if (status!=0) { sprintf(message, "ReadHealpixWeights: problem reading column in FITS file table, ERR=%d", status); warning(message); } if(anynul!=0) { warning("ReadHealpixWeights: found NULL values in FITS table"); printf("They are:\n"); for (i=0; i<nelements; i++) printf("%g ",nulval[i]); printf("\n"); } free_vector(nulval, 0, nelements-1); // Close file and exit: fits_close_file(fpointer, &status); if (status!=0) { sprintf(message, "ReadHealpixWeights: could not close FITS file, ERR=%d", status); warning(message); } return status; }
Kst::Object::UpdateType LFIIOSource::update() { Kst::Object::UpdateType updateType = Kst::Object::NO_CHANGE; QString strTemplate; QString strName; fitsfile* ffits; char charTemplate[FLEN_CARD]; char charName[FLEN_CARD]; long lNumFrames; long lMaxRepeat = 1; long lRepeat; long lWidth; int iColNumber; int iNumCols; int iStatus = 0; int iResult = 0; int iTypeCode; int i; int newNF = 0; _valid = false; if(!_filename.isNull() && !_filename.isEmpty()) { iResult = fits_open_table( &ffits, _filename.ascii(), READONLY, &iStatus ); if(iResult == 0) { // determine size of data... iResult = fits_get_num_cols( ffits, &iNumCols, &iStatus ); if(iResult == 0) { iResult = fits_get_num_rows( ffits, &lNumFrames, &iStatus ); if(iResult == 0) { _fieldList.clear(); _fieldList.append("INDEX"); _valid = true; _bHasTime = false; // need to multiply lNumFrames by the maximum value of the vector repeat value... for(i = 0; i<iNumCols; i++) { iStatus = 0; sprintf(charTemplate, "%d", i+1); iResult = fits_get_colname( ffits, CASEINSEN, charTemplate, charName, &iColNumber, &iStatus ); if(iResult == 0) { int iOffset = i; strName = charName; // ensure that we don't add duplicates to the _fieldList... while(_fieldList.findIndex(strName) != -1) { strName = QString("%1[%2]").arg(charName).arg(iOffset); iOffset++; } } else { strName.setNum(i); } _fieldList.append(strName); iStatus = 0; iResult = fits_get_coltype( ffits, i+1, &iTypeCode, &lRepeat, &lWidth, &iStatus ); if(iResult == 0) { if(lRepeat > lMaxRepeat) { lMaxRepeat = lRepeat; } } } // check if we have a time field defined by the header keys TIMEZERO and DELTA_T. // If so then we create a new field called $TIME_FIELD, unless such a field already // exists, in which case we do nothing... char charTimeZero[] = "TIMEZERO"; iStatus = 0; iResult = fits_read_key( ffits, TDOUBLE, charTimeZero, &_dTimeZero, 0L, &iStatus ); if(iResult == 0) { char charTimeDelta[] = "DELTA_T"; iResult = fits_read_key( ffits, TDOUBLE, charTimeDelta, &_dTimeDelta, 0L, &iStatus ); if(iResult == 0) { if(_fieldList.find(QString(TIME_FIELD)) == _fieldList.end()) { _bHasTime = true; _fieldList.append(TIME_FIELD); } } } if(lNumFrames * lMaxRepeat != _numFrames) { _numCols = iNumCols; newNF = lNumFrames * lMaxRepeat; updateType = Kst::Object::UPDATE; } } } iStatus = 0; fits_close_file( ffits, &iStatus ); } } bool isnew = newNF != _numFrames; _numFrames = newNF; return (isnew ? Kst::Object::UPDATE : Kst::Object::NO_CHANGE); }
KstObject::UpdateType LFIIOSource::update( int u ) { Q_UNUSED( u ) KstObject::UpdateType updateType = KstObject::NO_CHANGE; QString strTemplate; QString strName; fitsfile* ffits; char charTemplate[ FLEN_CARD ]; char charName[ FLEN_CARD ]; long lNumFrames; long lMaxRepeat = 1; long lRepeat; long lWidth; int iColNumber; int iNumCols; int iStatus = 0; int iResult = 0; int iTypeCode; int i; _valid = false; if( !_filename.isNull( ) && !_filename.isEmpty( ) ) { iResult = fits_open_table( &ffits, _filename.ascii( ), READONLY, &iStatus ); if( iResult == 0 ) { // // determine size of data... // iResult = fits_get_num_cols( ffits, &iNumCols, &iStatus ); if( iResult == 0 ) { iResult = fits_get_num_rows( ffits, &lNumFrames, &iStatus ); if( iResult == 0 ) { _strListColNames.clear( ); _valid = true; // // need to multiply lNumFrames by the maximum value of the vector repeat value... // for( i=0; i<iNumCols; i++ ) { iStatus = 0; sprintf( charTemplate, "%d", i+1 ); iResult = fits_get_colname( ffits, CASEINSEN, charTemplate, charName, &iColNumber, &iStatus ); if( iResult == 0 ) { strName = charName; _strListColNames.append( strName ); } else { strName.setNum( i ); _strListColNames.append( strName ); } iStatus = 0; iResult = fits_get_coltype( ffits, i+1, &iTypeCode, &lRepeat, &lWidth, &iStatus ); if( iResult == 0 ) { if( lRepeat > lMaxRepeat ) { lMaxRepeat = lRepeat; } } } if( lNumFrames * lMaxRepeat != _numFrames ) { _numCols = iNumCols; _numFrames = lNumFrames * lMaxRepeat; updateType = KstObject::UPDATE; } } } iStatus = 0; fits_close_file( ffits, &iStatus ); } } return updateType; }