Beispiel #1
0
    def getFileNames(self, maskName: str,
                     sampleFreq: float) -> Tuple[str, str]:
        """Get the name of a mask file
        
        This method is here to give consistent file names for mask files.

        Parameters
        ----------
        maskName : str
            The name of the mask
        sampleFreq : float
            The sampling frequency of the data

        Returns
        -------
        infoFile : str
            The name of the mask infoFile
        winFile : str
            The name of the mask winFile
        """
        checkAndMakeDir(self.datapath)
        sampleFreqStr = fileFormatSampleFreq(sampleFreq)
        name: str = maskName + "_{}".format(sampleFreqStr)
        infoFile: str = os.path.join(self.datapath, name + ".info")
        winFile: str = os.path.join(
            self.datapath, name)  # no need for extension here, numpy adds one
        return infoFile, winFile
Beispiel #2
0
    def write(self, statData: StatisticData, inc: int) -> None:
        """Write a statistic file

        Parameters
        ----------
        statData : StatisticData
            The statistic data to write out
        inc : int
            The increment, usually the decimation level
        """
        # write the info file - numWindows and channel ordering
        checkAndMakeDir(
            os.path.join(self.datapath, "{}".format(statData.statName)))
        statFile, infoFile, commentFile = self.getFileNames(
            self.datapath, statData.statName, inc)

        # info file
        # want a channel ordering
        f = open(infoFile, "w")
        f.write("{}\n".format(
            statData.refTime.strftime("%Y-%m-%d %H:%M:%S.%f")))
        f.write("{}\n".format(statData.sampleFreq))
        f.write("{}\n".format(statData.winSize))
        f.write("{}\n".format(statData.winOlap))
        f.write("{:d}\n".format(statData.numWindows))
        f.write("{}\n".format(" ".join(statData.winStats)))
        # now write out the evaluation frequencies
        f.write("{}\n".format(arrayToString(statData.evalFreq)))
        # finally, write out the datatype
        f.write("{}\n".format(statData.dtype))
        # write out the window information
        # only write out when not consecutive
        f.write("Window map: localIndex - globalIndex\n")
        prevI = -10
        for idx, gIndex in enumerate(statData.globalIndices):
            if gIndex != prevI + 1:
                f.write("{:d} - {:d}\n".format(idx, gIndex))
            prevI = gIndex
        f.close()

        # check to save comments
        # only want this on the first increment - no need to rewrite after the others
        if inc < 1:
            import resistics

            with open(commentFile, "w") as f:
                for c in statData.comments:
                    f.write("{}\n".format(c))
                f.write(
                    "Statistic data for statistic {} written to {} on {} using resistics {}\n"
                    .format(
                        statData.statName,
                        self.datapath,
                        datetime.now(),
                        resistics.__version__,
                    ))
                f.write(breakComment())

        # save binary stat file
        np.save(statFile, statData.stats)
Beispiel #3
0
    def writeResult(self, specdir: str, postpend: str, freq, data: np.ndarray,
                    variances: np.ndarray, **kwargs):
        """Write the transfer function file

        Parameters
        ----------
        specdir : str
            The spectra data being used for the transfer function estimate
        postpend : str
            The optional postpend to the transfer function file
        data : np.ndarray
            The transfer function estimates
        variances : np.ndarray
            The transfer function variances
        remotesite : str, optional
            Optionally, if there is a remote site
        remotechans : List[str], optional
            Optionally add the remote channels if there is a remote site
        """
        # path for writing out to
        sampleFreqStr = fileFormatSampleFreq(self.decParams.sampleFreq)
        if postpend == "":
            filename = "{}_fs{:s}_{}".format(self.outSite, sampleFreqStr,
                                             specdir)
        else:
            filename = "{}_fs{:s}_{}_{}".format(self.outSite, sampleFreqStr,
                                                specdir, postpend)
        datapath = os.path.join(self.outpath, sampleFreqStr)
        checkAndMakeDir(datapath)
        outfile = os.path.join(datapath, filename)
        # now construct the transferFunctionData object
        numFreq = len(freq)
        dataDict = {}
        varDict = {}
        for i in range(0, self.outSize):
            for j in range(0, self.inSize):
                key = "{}{}".format(self.outChannels[i], self.inChannels[j])
                dataArray = np.empty(shape=(numFreq), dtype="complex")
                varArray = np.empty(shape=(len(freq)), dtype="float")
                for ifreq in range(0, numFreq):
                    dataArray[ifreq] = data[ifreq][i, j]
                    varArray[ifreq] = variances[ifreq][i, j]
                dataDict[key] = dataArray
                varDict[key] = varArray
        tfData = TransferFunctionData(freq, dataDict, varDict)
        # now make the writer and write out
        tfWriter = TransferFunctionWriter(outfile, tfData)
        tfWriter.setHeaders(
            sampleFreq=self.decParams.sampleFreq,
            insite=self.inSite,
            inchans=self.inChannels,
            outsite=self.outSite,
            outchans=self.outChannels,
        )
        if "remotesite" in kwargs:
            tfWriter.addHeader("remotesite", kwargs["remotesite"])
        if "remotechans" in kwargs:
            tfWriter.addHeader("remotechans", kwargs["remotechans"])
        tfWriter.write()
Beispiel #4
0
    def openAsciiForWriting(
        self,
        filename: str,
        fileInc: str,
        sampleFreq: float,
        winSize: int,
        winOverlap: int,
        globalOffset: int,
        numWindows: int,
        channels: List[str],
    ) -> None:
        """Write ascii spectrum file 

        Parameters
        ----------
        filename : str
            Filename for spectra files
        fileInc : int
            The decimation level
        sampleFreq : float
            Sampling frequeny of time data
        winSize : int
            Window size in samples for time data windows
        winOverlap : int
            Overlap size in samples for time data windows
        globalOffset : int
            Global offset for local and global indices
        numWindows : int
            The number of windows in the time data
        channels : List[str]
            Channels in data
        """
        # sort channels alphabetically - matching the order in the data files
        self.channels = sorted(channels)

        checkAndMakeDir(self.datapath)
        filebase: str = filename + "{:02d}".format(fileInc)
        # info file
        filepathInfo: str = os.path.join(self.datapath, filebase + ".info")
        self.writeInfoFile(
            filepathInfo,
            sampleFreq,
            winSize,
            winOverlap,
            globalOffset,
            numWindows,
            self.channels,
        )
        # open file for data
        self.filepath: str = os.path.join(self.datapath, filebase + ".dat")
        self.printText("Opening file {}".format(self.filepath))
        self.file = open(self.filepath, "w")
Beispiel #5
0
    def createSite(self, site: str) -> bool:
        """Creates a site folder in the time data path

        Parameters
        ----------
        site : str
            Name of the site
        """
        sitePath = os.path.join(self.timePath, site)
        chk = checkDirExistence(sitePath)
        if chk:
            self.printWarning(
                "Site {} already exists in project time data folder".format(
                    site))
            return False
        checkAndMakeDir(sitePath)
        return True
Beispiel #6
0
    def writeDataset(self,
                     reader: TimeReader,
                     physical: bool = True,
                     **kwargs) -> None:
        """Write out a dataset by passing a data reader

        This method is intended to transform an existing dataset into internal format

        Parameters
        ----------
        reader : DataReader
            A list of the global header words of interest for writing out
        physical : bool, optional
            An optional flag designating whether to use physical samples or not. Default is true
        """
        if self.getOutPath() == "":
            self.printError("No output filepath given", quitrun=True)
        checkAndMakeDir(self.getOutPath())
        # write using information from a reader file
        headers = reader.getHeaders()
        chanHeaders, chanMap = reader.getChanHeaders()
        # now write depending on whether scaling_applied or not
        if physical:
            # make sure dataset is written out in float and with scaling_applied header set to True
            self.dtype = np.float32
            kwargs["scaling_applied"] = True
            # write out
            self.write(headers, chanHeaders, chanMap,
                       reader.getPhysicalSamples(), **kwargs)
        else:
            # write out unscaled samples
            self.printWarning(
                "Wrinting out of unscaled samples is not recommended due to scaling differences between the formats."
            )
            self.printWarning(
                "Dataset will be written out but problems may be encountered in the future."
            )
            self.write(headers, chanHeaders, chanMap,
                       reader.getUnscaledSamples(), **kwargs)
Beispiel #7
0
    def writeData(self,
                  headers,
                  chanHeaders,
                  timeData,
                  physical: bool = True,
                  **kwargs):
        """Write out time data 

        This method requires the user to pass global headers and chan headers explicitly.

        Parameters
        ----------
        headers : Dict
            Dictionary of headers
        chanHeaders : List
            List of channel headers
        timeData : TimeData
            Time series data to write out
        physical : bool, optional
            An optional flag designating whether the data is in field units (i.e. all scalings have been applied). This will result in the scaling_applied header being set to True. Default value for physical is True (i.e. data is assumed to be in field units).
        """
        if self.getOutPath() == "":
            self.printWarning("No output filepath given")
            return
        # make the directory
        checkAndMakeDir(self.getOutPath())
        # calculate our own cMap
        chanMap = {}
        for iChan in range(0, len(chanHeaders)):
            chanType = chanHeaders[iChan]["channel_type"]
            chanMap[chanType] = iChan
        # check if in physical units
        if physical:
            kwargs["scaling_applied"] = True
            self.dtype = np.float32
        # write the data
        self.write(headers, chanHeaders, chanMap, timeData, **kwargs)
Beispiel #8
0
 def checkDirectories(self) -> None:
     """Check to see if project directories exist and if not, make them"""
     # data directories
     checkAndMakeDir(self.timePath)
     checkAndMakeDir(self.specPath)
     checkAndMakeDir(self.statPath)
     checkAndMakeDir(self.maskPath)
     checkAndMakeDir(self.transFuncPath)
     # calibration directories
     checkAndMakeDir(self.calPath)
     # image directory
     checkAndMakeDir(self.imagePath)
Beispiel #9
0
def newProject(
    projectPath: str,
    refTime: Union[str, datetime],
    configFile: str = "",
    name: str = "mtProj",
) -> ProjectData:
    """Create a new project in project path

    A new project will be created in project path. If the project path directory does not exist, a new one will be made. If a project already exists in project path, this project will be loaded and returned.

    Parameters
    ----------
    projectPath : str
        Path for the project directory
    refTime : datetime
        The reference time for the project
    configFile : str, optional
        Path to a configuration file
    name : str, optional (default is "mtProj")
        The name of the project file

    Returns
    -------
    ProjectData
        A project data object
    """
    # create project path directory
    checkAndMakeDir(projectPath)
    # reference time
    if isinstance(refTime, str):
        refTime = datetime.strptime(refTime, "%Y-%m-%d %H:%M:%S")
    # print info
    textLst = [
        "Creating a new project in path: {}".format(projectPath),
        "Project name: {}".format(name),
    ]
    projectBlock(textLst)

    # create the subdirectories
    projectFile = os.path.join(projectPath, "{}.prj".format(name))
    timePath = os.path.join(projectPath, "timeData")
    specPath = os.path.join(projectPath, "specData")
    statPath = os.path.join(projectPath, "statData")
    maskPath = os.path.join(projectPath, "maskData")
    transFuncPath = os.path.join(projectPath, "transFuncData")
    calPath = os.path.join(projectPath, "calData")
    imagePath = os.path.join(projectPath, "images")
    saveProjectFile(
        projectFile,
        refTime,
        calPath,
        timePath,
        specPath,
        statPath,
        maskPath,
        transFuncPath,
        imagePath,
    )

    # configuration file
    config = ConfigData(configFile)
    proj = ProjectData(
        projectFile,
        refTime,
        calPath,
        timePath,
        specPath,
        statPath,
        maskPath,
        transFuncPath,
        imagePath,
        config=config,
    )
    proj.printInfo()
    proj.config.printInfo()

    return proj