resistics.spectra.io module¶
-
class
resistics.spectra.io.
SpectrumReader
(datapath: str)[source]¶ Bases:
resistics.common.base.ResisticsBase
Reads in spectra data for measurements
Spectrum reader reads in the info file for the spectra data and .dat files (for ascii formatted spectra data) and .bin files (for binary formatted data).
Spectra files are located in: project -> specData -> site -> datapath -> spectra data files
- Attributes
- datapathstr
Data root for spectra data
- headerKeys :
Spectra file header keys
- headersDict
Spectra file header values
- comments: List[str]
Spectra file comments
- dataType :
Data type of spectra data
- dataByteSizeint
Byte size of a single data point
- filepathstr
Filepath for spectra files
- filebool
The data file
Methods
__init__(datapath)
Initialise the SpectrumReader getReferenceTime() Get the reference time used for the spectrum calculation
getChannels()
Get the channels in the spectra data
getNumChannels()
Get the number of channels
getSampleFreq()
Get the sampling frequency of the data
getNumWindows()
Get the number of windows in the spectra file
getWindowSize()
Get the window size used for the data
getWindowOverlap()
Get the window overlap size
getDataSize()
Get the number of samples in a spectra window
getGlobalOffset()
Get the window global offset
getGlobalRange()
Get the window global indices range
getFrequencyArray()
Get the frequency array of the frequencies of the spectra data points
getComments()
Get a deepcopy of the comments
openBinaryForReading(filename, fileInc):
Open a binary spectra file given by filename and fileInc (the decimation level)
readBinaryWindowLocal(localIndex)
Read a spectra window given by a local index in binary format
readBinaryWindowGlobal(globalIndex)
Read a spectra window given by a global index (relative to reference time) in binary format
openAsciiForReading(filename, fileInc):
Open a ascii spectra file given by filename and fileInc (the decimation level)
readAsciiWindowLocal(localIndex)
Read a spectra window given by a local index in ascii format
readAsciiWindowGlobal(globalIndex)
Read a spectra window given by a global index (relative to reference time) in ascii format
readInfoFile(filepath)
Read the spectra information file given by filepath
getInfoValue(header, line)
Put header value in the correct format for header and line
readCommentsFile(filepath):
Read the comments file given by filepath
getFileBase(filename, fileInc):
Get the spectra file name
closeFile()
Close spectra data file
printList()
Class status returned as list of strings
-
getChannels
(self) → List[str][source]¶ Get the channels in the spectra data
- Returns
- List[str]
List of channels
-
getComments
(self) → List[str][source]¶ Get a deepcopy of the comments
- Returns
- List[str]
Dataset comments as a list of strings
-
getDataSize
(self) → int[source]¶ Get the size of the corresponding spectrum data for a window
- Returns
- int
Number of samples in one spectrum window
-
getFileBase
(self, filename: str, fileInc: int) → str[source]¶ Read comments file from filepath
- Parameters
- filename: str
Filename of spectra files
- fileIncint
The decimation level
-
getFrequencyArray
(self) → numpy.ndarray[source]¶ Returns the frequency array
Frequency array are the frequency points in the spectra
- Returns
- np.ndarray
Frequency array
-
getGlobalOffset
(self) → int[source]¶ Get global window offset
The global window offset references windows to the reference time rather than the start time of the time data
- Returns
- int
Global window offset
-
getGlobalRange
(self) → List[int][source]¶ Get the range of window global indices
The first global index is given by getGlobalOffset() as the counting starts from zero. The last global index is self.getGlobalOffset() + self.getNumWindows() - 1.
- Returns
- List[int]
List with two elements, the first being the global index of the first spectrum window and the second, the global index of the last spectrum window
-
getInfoValue
(self, header: str, line: str) → Any[source]¶ Format some of the info file values
- Parameters
- headerstr
The header
- linestr
The line from the info file
-
getNumWindows
(self) → int[source]¶ Get number of windows for which spectra have been calculated
- Returns
- int
The number of windows
-
getReferenceTime
(self) → datetime.datetime[source]¶ Get reference time for spectra calculation
- Returns
- datetime
The reference time used for the spectra calculation
-
getSampleFreq
(self) → float[source]¶ Get sampling frequency of time data used for spectra calculation
- Returns
- float
Sampling frequency of the time data
-
getSpectrumData
(self, localIndex: int, data: Dict[str, numpy.ndarray]) → resistics.spectra.data.SpectrumData[source]¶ Return a spectrum data object from a data dictionary and the local index
- Parameters
- localIndexint
The local index of the spectra window
- dataDict[str, np.ndarray]
The data dictionary
- Returns
- SpectrumData
A SpectrumData object
-
getWindowOverlap
(self) → int[source]¶ Get the size of the time data overlap in samples
- Returns
- int
Size of time data overlap in samples
-
getWindowSize
(self) → int[source]¶ Get the size of the time data window in samples
- Returns
- int
Size of time data window in samples
-
openAsciiForReading
(self, filename: str, fileInc: int) → bool[source]¶ Open a ascii data file for reading
- Parameters
- filename: str
Filename of spectra files
- fileIncint
The decimation level
-
openBinaryForReading
(self, filename: str, fileInc: int) → bool[source]¶ Open a binary data file for reading
self.file is not set in this method because spectra data is read using memmap.
- Parameters
- filename: str
Filename of spectra files
- fileIncint
The decimation level
-
printList
(self) → List[str][source]¶ Class information as a list of strings
- Returns
- outList[str]
List of strings with information
-
readAsciiWindowGlobal
(self, globalIndex: int) → resistics.spectra.data.SpectrumData[source]¶ Get spectrum data for a window defined by a global index (for ascii formatted data)
- Parameters
- globalIndex: int
The global index
-
readAsciiWindowLocal
(self, localIndex: int) → resistics.spectra.data.SpectrumData[source]¶ Get spectrum data for a window defined by a local index (for ascii formatted data)
- Parameters
- localIndex: int
The local index
-
readBinaryBatchGlobal
(self, globalIndices: Union[List[int], NoneType] = None) → Tuple[List[resistics.spectra.data.SpectrumData], numpy.ndarray][source]¶ Batch read binary windows
Batch reading binary windows allows the data for calculation to be split over multi processes
- Parameters
- globalIndicesList[int], None, optional
The global indices to batch read. By default, all windows will be returned if not specified
- Returns
- List[SpectrumData]
A list of SpectrumData objects
- globalIndices: np.ndarray
An array of the global indices for the SpectrumData actually returned
-
readBinaryWindowGlobal
(self, globalIndex: int) → resistics.spectra.data.SpectrumData[source]¶ Get spectrum data for a window defined by a global index (for binary formatted data)
- Parameters
- globalIndex: int
The global index
-
readBinaryWindowLocal
(self, localIndex: int) → resistics.spectra.data.SpectrumData[source]¶ Get spectrum data for a window defined by a local index (for binary formatted data)
- Parameters
- localIndex: int
The local index
-
class
resistics.spectra.io.
SpectrumWriter
(datapath: str, refTime: datetime.datetime)[source]¶ Bases:
resistics.common.base.ResisticsBase
Writes spectra data for measurements
Spectrum writer writes out spectra data as either binary (recommended) or ascii (more space required). A spectra file should be written out for each decimation level along with an information file, again for each decimation level. A single comments file is written out to record the processing sequence.
Spectra files are located in: project -> specData -> site -> datapath -> spectra data files
.dat files are ascii formatted data .bin files are binary formatted data
- Attributes
- datapathstr
Data root for spectra data
- filepathstr
Filepath for spectra files
- refTimedatetime
The reference time for the project
- filebool
The data file
Methods
__init__(datapath, refTime)
Initialise the SpectrumWriter
openBinaryForWriting(filename, fileInc, sampleFreq, winSize, winOverlap, globalOffset, numWindows, channels)
Open file for writing binary spectra data
writeBinary(specData)
Write out binary spectra data for a single time window
openAsciiForWriting(filename, fileInc, sampleFreq, winSize, winOverlap, globalOffset, numWindows, channels)
Open file for writing ascii spectra data
writeAscii(specData)
Write out ascii spectra data for a single time window
writeInfoFile(filepath, sampleFreq, winSize, winOverlap, globalOffset, numWindows, channels)
Write out the spectra information file
writeCommentsFile(comments)
Write out the comments file
getFileBase(filename, fileInc)
Get the spectra file name
closeFile()
Close spectra data file
printList()
Class status returned as list of strings
-
getFileBase
(self, filename: str, fileInc: int) → str[source]¶ Read comments file from filepath
- Parameters
- filename: str
Filename of spectra files
- fileIncint
The decimation level
-
openAsciiForWriting
(self, filename: str, fileInc: str, sampleFreq: float, winSize: int, winOverlap: int, globalOffset: int, numWindows: int, channels: List[str]) → None[source]¶ Write ascii spectrum file
- Parameters
- filenamestr
Filename for spectra files
- fileIncint
The decimation level
- sampleFreqfloat
Sampling frequeny of time data
- winSizeint
Window size in samples for time data windows
- winOverlapint
Overlap size in samples for time data windows
- globalOffsetint
Global offset for local and global indices
- numWindowsint
The number of windows in the time data
- channelsList[str]
Channels in data
-
openBinaryForWriting
(self, filename, fileInc, sampleFreq, winSize, winOverlap, globalOffset, numWindows, channels) → None[source]¶ Write binary spectrum file
- Parameters
- filenamestr
Filename for spectra files
- fileIncint
The decimation level
- sampleFreqfloat
Sampling frequeny of time data
- winSizeint
Window size in samples for time data windows
- winOverlapint
Overlap size in samples for time data windows
- globalOffsetint
Global offset for local and global indices
- numWindowsint
The number of windows in the time data
- channelsList[str]
Channels in data
-
printList
(self) → List[str][source]¶ Class information as a list of strings
- Returns
- outList[str]
List of strings with information
-
writeAscii
(self, specData: resistics.spectra.data.SpectrumData) → None[source]¶ Write spectrum data to ascii file
- Parameters
- specDataSpectrumData
Spectrum data
-
writeBinary
(self, specData)[source]¶ Write spectrum data to binary file
- Parameters
- specDataSpectrumData
Spectrum data
-
writeCommentsFile
(self, comments: List[str]) → None[source]¶ Write comments file
- Parameters
- commentsList[str]
List of comments
-
writeInfoFile
(self, filepath: str, sampleFreq: float, winSize: int, winOverlap: int, globalOffset: int, numWindows: int, channels: List[str]) → None[source]¶ Write info file
- Parameters
- filepathstr
Filepath for info file
- sampleFreqfloat
Sampling frequeny of time data
- winSizeint
Window size in samples for time data windows
- winOverlapint
Overlap size in samples for time data windows
- globalOffsetint
Global offset for local and global indices
- numWindowsint
The number of windows in the time data
- channelsList[str]
Channels in data