Skip to content

Commit

Permalink
Merge branch 'main' into develop/sync_externals
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Smith committed Oct 2, 2023
2 parents 15e3d74 + 18a764e commit aa3c088
Show file tree
Hide file tree
Showing 5 changed files with 306 additions and 164 deletions.
136 changes: 97 additions & 39 deletions six/modules/c++/cphd/include/cphd/CPHDWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
#include <vector>
#include <std/span>
#include <std/cstddef>
#include <functional>
#include <memory>
#include <std/filesystem>

#include <types/RowCol.h>
#include <io/FileOutputStream.h>
Expand Down Expand Up @@ -74,6 +77,12 @@ struct CPHDWriter final
* that may be used if byte swapping is necessary.
* Default is 4 MB
*/
CPHDWriter(
const Metadata& metadata,
io::SeekableOutputStream& stream,
const std::vector<std::filesystem::path>* pSchemaPaths = nullptr,
size_t numThreads = 0,
size_t scratchSpaceSize = 4 * 1024 * 1024);
CPHDWriter(
const Metadata& metadata,
std::shared_ptr<io::SeekableOutputStream> stream,
Expand Down Expand Up @@ -106,6 +115,12 @@ struct CPHDWriter final
const std::vector<std::string>& schemaPaths = std::vector<std::string>(),
size_t numThreads = 0,
size_t scratchSpaceSize = 4 * 1024 * 1024);
CPHDWriter(
const Metadata& metadata,
const std::filesystem::path& pathname,
const std::vector<std::filesystem::path>* pSchemaPaths = nullptr,
size_t numThreads = 0,
size_t scratchSpaceSize = 4 * 1024 * 1024);

CPHDWriter() = delete;
CPHDWriter(const CPHDWriter&) = delete;
Expand All @@ -114,6 +129,9 @@ struct CPHDWriter final
CPHDWriter& operator=(CPHDWriter&&) = delete;
~CPHDWriter() = default;

std::unique_ptr<DataWriter> make_DataWriter(io::OutputStream&) const;
std::unique_ptr<DataWriter> make_DataWriter() const;

/*
* \func write
* \brief Writes the complete CPHD into the file.
Expand All @@ -129,14 +147,27 @@ struct CPHDWriter final
* \param supportData (Optional) The support array data to write to disk.
*/
template<typename T>
void write(const PVPBlock& pvpBlock, const T* widebandData,
void write(io::SeekableOutputStream&, const PVPBlock& pvpBlock, const T* widebandData,
std::span<const std::byte> supportData);
template<typename T>
void write(const PVPBlock& pvpBlock, const T* widebandData,
std::span<const std::byte> supportData)
{
write(mStream, pvpBlock, widebandData, supportData);
}
template<typename T>
void write(const PVPBlock& pvpBlock, const T* widebandData)
{
write(pvpBlock, widebandData, std::span<const std::byte>());
}

template <typename T>
size_t writeChannel(DataWriter&, const T* widebandData, size_t);
template <typename T>
void writeChannels(DataWriter&, const T* widebandData);



/*
* \func writeMetadata
* \brief Writes the header, and metadata into the file.
Expand All @@ -146,7 +177,13 @@ struct CPHDWriter final
*
* \param pvpBlock The vector based metadata to write.
*/
void writeMetadata(const PVPBlock& pvpBlock);
void writeMetadata(io::OutputStream&, const PVPBlock& pvpBlock);
void writeMetadata(const PVPBlock& pvpBlock)
{
writeMetadata(mStream, pvpBlock);
}
void getMetadata(const PVPBlock&, size_t& supportSize, /*Optional*/ size_t& pvpSize, size_t& cphdSize) const;


/*
* \func writeSupportData
Expand All @@ -159,12 +196,17 @@ struct CPHDWriter final
* \param id The unique identifier of the support array
*/
template <typename T>
void writeSupportData(const T* data_,
const std::string& id)
void writeSupportData(io::OutputStream& stream,
const T* data_, const std::string& id)
{
const auto size = mMetadata.data.getSupportArrayById(id).size_bytes();
const auto data = sys::make_span<const std::byte>(data_, size);
writeSupportDataImpl(data, mMetadata.data.getSupportArrayById(id).bytesPerElement);
const auto data = sys::make_span<const std::byte>(data_, size);
writeSupportDataImpl(stream, data, mMetadata.data.getSupportArrayById(id).bytesPerElement);
}
template <typename T>
void writeSupportData(const T* data, const std::string& id)
{
writeSupportData(mStream, data, id);
}

/*
Expand All @@ -175,16 +217,28 @@ struct CPHDWriter final
*
* \param data A pointer to the start of the support array data block
*/
void writeSupportData(std::span<const std::byte>);
void writeSupportData(io::SeekableOutputStream&, std::span<const std::byte>);

template <typename T>
void writeSupportData(io::SeekableOutputStream& stream, std::span<const T> data)
{
writeSupportData(stream, std::as_bytes(data));
}
template <typename T>
void writeSupportData(std::span<const T> data)
{
writeSupportData(std::as_bytes(data));
writeSupportData(mStream, data);
}

template <typename T>
void writeSupportData(io::SeekableOutputStream& stream, const std::vector<T>& data)
{
writeSupportData(stream, sys::make_span(data));
}
template <typename T>
void writeSupportData(const std::vector<T>& data)
{
writeSupportData(sys::make_span(data));
writeSupportData(mStream, data);
}

/*
Expand All @@ -194,7 +248,13 @@ struct CPHDWriter final
* \param PVPBlock A populated PVPBlock object that will be written
* to the file as a block of data
*/
void writePVPData(const PVPBlock& PVPBlock);
void writePVPData(io::SeekableOutputStream&, const PVPBlock&);
void writePVPData(const PVPBlock& pvpBlock)
{
writePVPData(mStream, pvpBlock);
}
static void writePvpPadBytes(io::OutputStream& stream, int64_t pvpPadBytes);
static void writePVPData(DataWriter& dataWriter, const cphd::Data&, const PVPBlock&);

/*
* \func writeCPHDData
Expand All @@ -214,53 +274,49 @@ struct CPHDWriter final
* \param channel For selecting channel of compressed signal block
*/
template <typename T>
void writeCPHDData(const T* data,
void writeCPHDData(DataWriter&,
const T* data,
size_t numElements,
size_t channel = 1);
template <typename T>
void writeCPHDData(io::SeekableOutputStream&,
const T* data,
size_t numElements,
size_t channel = 1);

void close()
template <typename T>
void writeCPHDData(const T* data,
size_t numElements,
size_t channel = 1)
{
mStream->close();
writeCPHDData(mStream, data, numElements, channel);
}

std::shared_ptr<io::SeekableOutputStream> getStream() const
{
return mSharedStream;
}
io::SeekableOutputStream& stream() const
{
return mStream;
}
void close()
{
stream().close();
}

private:
/*
* Write metadata helper
*/
void writeMetadata(
void writeMetadata(io::OutputStream&,
size_t supportSize, // Optional
size_t pvpSize,
size_t cphdSize);

/*
* Write pvp helper
*/
void writePVPData(const std::byte* pvpBlock,
size_t index);

/*
* Implementation of write wideband
*/
void writeCPHDDataImpl(const std::byte* data,
size_t size);

/*
* Implementation of write compressed wideband
*/
void writeCompressedCPHDDataImpl(const std::byte* data,
size_t channel);

/*
* Implementation of write support data
*/
void writeSupportDataImpl(std::span<const std::byte>, size_t elementSize);

//! DataWriter object
std::unique_ptr<DataWriter> mDataWriter;
void writeSupportDataImpl(io::OutputStream&, std::span<const std::byte>, size_t elementSize);

// Book-keeping element
//! metadata information
Expand All @@ -272,9 +328,11 @@ struct CPHDWriter final
//! number of threads for parallelism
const size_t mNumThreads;
//! schemas for XML validation
const std::vector<std::string> mSchemaPaths;
const std::vector<std::filesystem::path> mSchemaPaths;
const std::vector<std::filesystem::path>* mpSchemaPaths = nullptr;
//! Output stream contains CPHD file
std::shared_ptr<io::SeekableOutputStream> mStream;
std::shared_ptr<io::SeekableOutputStream> mSharedStream;
io::SeekableOutputStream& mStream;
//! header information
FileHeader mHeader;
};
Expand Down
Loading

0 comments on commit aa3c088

Please sign in to comment.