void BHFitsImageSet::loadImageData(TimeFrequencyData &data, const TimeFrequencyMetaDataPtr &metaData, const BHFitsImageSetIndex &index) { std::vector<num_t> buffer(_width * _height); _file->ReadCurrentImageData(0, &buffer[0], _width * _height); int rangeStart = _timeRanges[index._imageIndex].start, rangeEnd = _timeRanges[index._imageIndex].end; Image2DPtr image = Image2D::CreateZeroImagePtr(rangeEnd-rangeStart, _height); std::vector<num_t>::const_iterator bufferPtr = buffer.begin() + _height*rangeStart; for(int x=rangeStart; x!=rangeEnd; ++x) { for(int y=0; y!=_height; ++y) { image->SetValue(x-rangeStart, y, *bufferPtr); ++bufferPtr; } } data = TimeFrequencyData(TimeFrequencyData::AmplitudePart, SinglePolarisation, image); try { FitsFile flagFile(flagFilePath()); flagFile.Open(FitsFile::ReadOnlyMode); flagFile.ReadCurrentImageData(0, &buffer[0], _width * _height); bufferPtr = buffer.begin() + _height*rangeStart; Mask2DPtr mask = Mask2D::CreateUnsetMaskPtr(rangeEnd-rangeStart, _height); for(int x=rangeStart; x!=rangeEnd; ++x) { for(int y=0; y!=_height; ++y) { bool flag = false; if(*bufferPtr == 0.0) flag = false; else if(*bufferPtr == 1.0) flag = true; else std::runtime_error("Expecting a flag file with only ones and zeros, but this file contained other values."); mask->SetValue(x-rangeStart, y, flag); ++bufferPtr; } } data.SetGlobalMask(mask); } catch(std::exception &) { // Flag file could not be read; probably does not exist. Ignore this, flags will be initialized to false. } double frequencyDelta = _file->GetDoubleKeywordValue("CDELT1"), timeDelta = _file->GetDoubleKeywordValue("CDELT2"); BandInfo band; for(int ch=0; ch!=_height; ++ch) { ChannelInfo channel; channel.frequencyHz = ch * frequencyDelta * 1000000.0; band.channels.push_back(channel); } metaData->SetBand(band); const int rangeWidth = rangeEnd-rangeStart; std::vector<double> observationTimes(rangeWidth); for(int t=0; t!=rangeWidth; ++t) observationTimes[t] = (t + rangeStart) * timeDelta; metaData->SetObservationTimes(observationTimes); AntennaInfo antennaInfo; antennaInfo.id = 0; antennaInfo.name = RangeName(index._imageIndex); antennaInfo.diameter = 0.0; antennaInfo.mount = "Unknown"; antennaInfo.station = GetTelescopeName(); metaData->SetAntenna1(antennaInfo); metaData->SetAntenna2(antennaInfo); }
std::pair<TimeFrequencyData,TimeFrequencyMetaDataPtr> RSPReader::ReadAllBeamlets(unsigned long timestepStart, unsigned long timestepEnd, unsigned beamletCount) { const unsigned width = timestepEnd - timestepStart; Image2DPtr realX = Image2D::CreateZeroImagePtr(width, beamletCount); Image2DPtr imaginaryX = Image2D::CreateZeroImagePtr(width, beamletCount); Image2DPtr realY = Image2D::CreateZeroImagePtr(width, beamletCount); Image2DPtr imaginaryY = Image2D::CreateZeroImagePtr(width, beamletCount); Mask2DPtr mask = Mask2D::CreateSetMaskPtr<true>(width, beamletCount); std::ifstream file(_rawFile.c_str(), std::ios_base::binary | std::ios_base::in); size_t frame = 0; std::set<short> stations; TimeFrequencyMetaDataPtr metaData = TimeFrequencyMetaDataPtr(new TimeFrequencyMetaData()); BandInfo band; for(size_t i=0;i<beamletCount;++i) { ChannelInfo channel; channel.frequencyHz = i+1; channel.frequencyIndex = i; band.channels.push_back(channel); } metaData->SetBand(band); std::vector<double> observationTimes; // Read a header and determine the reading start position // Because timestepStart might fall within a block, the RCPApplicationHeader firstHeader; firstHeader.Read(file); const unsigned long bytesPerFrame = beamletCount * firstHeader.nofBlocks * RCPBeamletData::SIZE + RCPApplicationHeader::SIZE; const unsigned long startFrame = timestepStart / (unsigned long) firstHeader.nofBlocks; const unsigned long startByte = startFrame * bytesPerFrame; const unsigned long offsetFromStart = timestepStart - (startFrame * firstHeader.nofBlocks); //Logger::Debug << "Seeking to " << startByte << " (timestepStart=" << timestepStart << ", offsetFromStart=" << offsetFromStart << ", startFrame=" << startFrame << ",bytesPerFrame=" << bytesPerFrame << ")\n"; file.seekg(startByte, std::ios_base::beg); // Read the frames unsigned long x=0; while(x < width + offsetFromStart && file.good()) { RCPApplicationHeader header; header.Read(file); if(header.versionId != 2) { std::stringstream s; s << "Corrupted header found in frame " << frame << "!"; throw std::runtime_error(s.str()); } if(stations.count(header.stationId)==0) { stations.insert(header.stationId); AntennaInfo antenna; std::stringstream s; s << "LOFAR station with index " << header.stationId; antenna.name = s.str(); metaData->SetAntenna1(antenna); metaData->SetAntenna2(antenna); } for(size_t j=0;j<beamletCount;++j) { for(size_t i=0;i<header.nofBlocks;++i) { RCPBeamletData data; data.Read(file); if(i + x < width + offsetFromStart && i + x >= offsetFromStart) { const unsigned long pos = i + x - offsetFromStart; realX->SetValue(pos, j, data.xr); imaginaryX->SetValue(pos, j, data.xi); realY->SetValue(pos, j, data.yr); imaginaryY->SetValue(pos, j, data.yi); mask->SetValue(pos, j, false); } } } x += header.nofBlocks; ++frame; } //Logger::Debug << "Read " << frame << " frames.\n"; for(unsigned long i=0;i<width;++i) { const unsigned long pos = i + timestepStart; const double time = (double) pos * (double) STATION_INTEGRATION_STEPS / (double) _clockSpeed; observationTimes.push_back(time); } metaData->SetObservationTimes(observationTimes); std::pair<TimeFrequencyData,TimeFrequencyMetaDataPtr> data; data.first = TimeFrequencyData(Polarization::XX, realX, imaginaryX, Polarization::YY, realY, imaginaryY); data.first.SetGlobalMask(mask); data.second = metaData; return data; }