def processProject(projData: ProjectData, **kwargs) -> None: """Process a project Parameters ---------- projData : ProjectData The project data instance for the project sites : List[str], optional List of sites sampleFreqs : List[float], optional List of sample frequencies to process specdir : str, optional The spectra directories to use inchans : List[str], optional Channels to use as the input of the linear system inputsite : str, optional Site from which to take the input channels. The default is to use input and output channels from the same site outchans : List[str], optional Channels to use as the output of the linear system remotesite : str, optional The site to use as the remote site remotechans : List[str], optional Channels to use from the remote reference site crosschannels : List[str], optional List of channels to use for cross powers masks : Dict, optional Masks dictionary for passing mask data. The key should be a site name and the value should either be a string for a single mask or a list of multiple masks. datetimes : List, optional List of datetime constraints, each one as a dictionary. For example [{"type": "datetime", "start": 2018-08-08 00:00:00, "end": 2018-08-08 16:00:00, "levels": [0,1]}]. Note that levels is optional. postpend : str, optional String to postpend to the transfer function output """ options: Dict = dict() options["sites"]: List[str] = projData.getSites() options["sampleFreqs"]: List[float] = projData.getSampleFreqs() options["specdir"]: str = projData.config.configParams["Spectra"][ "specdir"] options["inchans"]: List[str] = ["Hx", "Hy"] options["inputsite"]: str = "" options["outchans"]: List[str] = ["Ex", "Ey"] options["remotesite"]: str = "" options["remotechans"]: List[str] = options["inchans"] options["crosschannels"]: List[str] = [] options["masks"]: Dict = {} options["datetimes"]: List = [] options["postpend"]: str = "" options = parseKeywords(options, kwargs) for site in options["sites"]: siteData = projData.getSiteData(site) siteFreqs = siteData.getSampleFreqs() for sampleFreq in siteFreqs: # check if not included if sampleFreq not in options["sampleFreqs"]: continue processSite(projData, site, sampleFreq, **options)
def viewTipper(projData: ProjectData, **kwargs) -> None: """View transfer function data Parameters ---------- projData : projecData The project data sites : List[str], optional List of sites to plot transfer functions for sampleFreqs : List[float], optional List of samples frequencies for which to plot transfer functions specdir : str, optional The spectra directories used postpend : str, optional The postpend on the transfer function files cols : bool, optional Boolean flag, True to arrange tipper plot as 1 row with 3 columns show : bool, optional Show the spectra plot save : bool, optional Save the plot to the images directory plotoptions : Dict A dictionary of plot options. For example, set the resistivity y limits using res_ylim, set the phase y limits using phase_ylim and set the xlimits using xlim """ options = {} options["sites"] = projData.getSites() options["sampleFreqs"] = projData.getSampleFreqs() options["specdir"] = projData.config.configParams["Spectra"]["specdir"] options["postpend"] = "" options["cols"] = True options["save"] = False options["show"] = True options["plotoptions"] = plotOptionsTipper() options = parseKeywords(options, kwargs) # loop over sites for site in options["sites"]: siteData = projData.getSiteData(site) sampleFreqs = set(siteData.getSampleFreqs()) # find the intersection with the options["freqs"] sampleFreqs = sampleFreqs.intersection(options["sampleFreqs"]) sampleFreqs = sorted(list(sampleFreqs)) # if prepend is a string, then make it a list if isinstance(options["postpend"], str): options["postpend"] = [options["postpend"]] plotfonts = options["plotoptions"]["plotfonts"] # now loop over the postpend options for pp in options["postpend"]: # add an underscore if not empty postpend = "_{}".format(pp) if pp != "" else pp fig = plt.figure(figsize=options["plotoptions"]["figsize"]) mks = ["o", "*", "d", "^", "h"] lstyles = ["solid", "dashed", "dashdot", "dotted"] # loop over sampling frequencies includedFreqs = [] for idx, sampleFreq in enumerate(sampleFreqs): tfData = getTransferFunctionData(projData, site, sampleFreq, specdir=options["specdir"], postpend=pp) if not tfData: continue includedFreqs.append(sampleFreq) projectText( "Plotting tipper for site {}, sample frequency {}".format( site, sampleFreq)) mk = mks[idx % len(mks)] ls = lstyles[idx % len(lstyles)] tfData.viewTipper( fig=fig, rows=options["cols"], mk=mk, ls=ls, label="{}".format(sampleFreq), xlim=options["plotoptions"]["xlim"], length_ylim=options["plotoptions"]["length_ylim"], angle_ylim=options["plotoptions"]["angle_ylim"], plotfonts=options["plotoptions"]["plotfonts"], ) # check if any files found if len(includedFreqs) == 0: continue # sup title sub = "Site {} tipper: {}".format(site, options["specdir"] + postpend) sub = "{}\nfs = {}".format( sub, arrayToString(includedFreqs, decimals=3)) st = fig.suptitle(sub, fontsize=plotfonts["suptitle"]) st.set_y(0.99) fig.tight_layout() fig.subplots_adjust(top=0.85) if options["save"]: impath = projData.imagePath filename = "tipper_{}_{}{}".format(site, options["specdir"], postpend) savename = savePlot(impath, filename, fig) projectText("Image saved to file {}".format(savename)) if not options["show"]: plt.close("all") else: plt.show(block=options["plotoptions"]["block"])
def calculateRemoteStatistics(projData: ProjectData, remoteSite: str, **kwargs): """Calculate statistics involving a remote reference site Parameters ---------- projData : ProjectData A project data instance remoteSite : str The name of the site to use as the remote site sites : List[str], optional A list of sites to calculate statistics for sampleFreqs : List[float], optional List of sampling frequencies for which to calculate statistics chans : List[str], optional List of data channels to use specdir : str, optional The spectra directory for which to calculate statistics remotestats : List[str], optional The statistics to calculate out. Acceptable statistics are: "RR_coherence", "RR_coherenceEqn", "RR_absvalEqn", "RR_transferFunction", "RR_resPhase". Configuration file values are used by default. """ options = {} options["sites"] = projData.getSites() options["sampleFreqs"] = projData.getSampleFreqs() options["chans"] = [] options["specdir"] = projData.config.configParams["Spectra"]["specdir"] options["remotestats"] = projData.config.configParams["Statistics"][ "remotestats"] options = parseKeywords(options, kwargs) projectText( "Calculating stats: {} for sites: {} with remote site {}".format( listToString(options["remotestats"]), listToString(options["sites"]), remoteSite, )) # create the statistic calculator and IO object statCalculator = StatisticCalculator() statIO = StatisticIO() # loop over sites for site in options["sites"]: siteData = projData.getSiteData(site) measurements = siteData.getMeasurements() for meas in measurements: sampleFreq = siteData.getMeasurementSampleFreq(meas) if sampleFreq not in options["sampleFreqs"]: # don't need to calculate statistics for this sampling frequency continue projectText( "Calculating stats for site {}, measurement {} with reference {}" .format(site, meas, remoteSite)) # decimation and window parameters decParams = getDecimationParameters(sampleFreq, projData.config) decParams.printInfo() numLevels = decParams.numLevels winParams = getWindowParameters(decParams, projData.config) # create the window selector and find the shared windows winSelector = getWindowSelector(projData, decParams, winParams) winSelector.setSites([site, remoteSite]) # calc shared windows between site and remote winSelector.calcSharedWindows() # create the spectrum reader specReader = SpectrumReader( os.path.join(siteData.getMeasurementSpecPath(meas), options["specdir"])) # loop through decimation levels for iDec in range(0, numLevels): # open the spectra file for the current decimation level check = specReader.openBinaryForReading("spectra", iDec) if not check: # probably because this decimation level not calculated continue specReader.printInfo() # get a set of the shared windows at this decimation level # these are the global indices sharedWindows = winSelector.getSharedWindowsLevel(iDec) # get other information regarding only this spectra file refTime = specReader.getReferenceTime() winSize = specReader.getWindowSize() winOlap = specReader.getWindowOverlap() numWindows = specReader.getNumWindows() evalFreq = decParams.getEvalFrequenciesForLevel(iDec) sampleFreqDec = specReader.getSampleFreq() globalOffset = specReader.getGlobalOffset() fArray = specReader.getFrequencyArray() # now want to find the size of the intersection between the windows in this file and the shared windows sharedWindowsMeas = sharedWindows.intersection( set(np.arange(globalOffset, globalOffset + numWindows))) sharedWindowsMeas = sorted(list(sharedWindowsMeas)) numSharedWindows = len(sharedWindowsMeas) statHandlers = {} # create the statistic handlers for stat in options["remotestats"]: statElements = getStatElements(stat) statHandlers[stat] = StatisticData(stat, refTime, sampleFreqDec, winSize, winOlap) # remember, this is with the remote reference, so the number of windows is number of shared windows statHandlers[stat].setStatParams(numSharedWindows, statElements, evalFreq) statHandlers[stat].comments = specReader.getComments() statHandlers[stat].addComment( projData.config.getConfigComment()) statHandlers[stat].addComment( "Calculating remote statistic: {}".format(stat)) statHandlers[stat].addComment( "Statistic components: {}".format( listToString(statElements))) # loop over the shared windows between the remote station and local station for iW, globalWindow in enumerate(sharedWindowsMeas): # get data and set in the statCalculator winData = specReader.readBinaryWindowGlobal(globalWindow) statCalculator.setSpectra(fArray, winData, evalFreq) # for the remote site, use the reader in win selector remoteSF, remoteReader = winSelector.getSpecReaderForWindow( remoteSite, iDec, globalWindow) winDataRR = remoteReader.readBinaryWindowGlobal( globalWindow) statCalculator.addRemoteSpec(winDataRR) for sH in statHandlers: data = statCalculator.getDataForStatName(sH) statHandlers[sH].addStat(iW, globalWindow, data) # save statistic for sH in statHandlers: statIO.setDatapath( os.path.join(siteData.getMeasurementStatPath(meas), options["specdir"])) statIO.write(statHandlers[sH], iDec)
def calculateStatistics(projData: ProjectData, **kwargs): """Calculate statistics for sites Parameters ---------- projData : ProjectData A project data instance sites : List[str], optional A list of sites to calculate statistics for sampleFreqs : List[float], optional List of sampling frequencies for which to calculate statistics chans : List[str], optional List of data channels to use specdir : str, optional The spectra directory for which to calculate statistics stats : List[str], optional The statistics to calculate out. Acceptable values are: "absvalEqn" "coherence", "psd", "poldir", "transFunc", "resPhase", "partialcoh". Configuration file values are used by default. """ options = {} options["sites"] = projData.getSites() options["sampleFreqs"] = projData.getSampleFreqs() options["chans"] = [] options["specdir"] = projData.config.configParams["Spectra"]["specdir"] options["stats"] = projData.config.configParams["Statistics"]["stats"] options = parseKeywords(options, kwargs) projectText("Calculating stats: {} for sites: {}".format( listToString(options["stats"]), listToString(options["sites"]))) # create the statistic calculator and IO object statCalculator = StatisticCalculator() statIO = StatisticIO() # loop through sites and calculate statistics for site in options["sites"]: siteData = projData.getSiteData(site) measurements = siteData.getMeasurements() for meas in measurements: sampleFreq = siteData.getMeasurementSampleFreq(meas) if sampleFreq not in options["sampleFreqs"]: # don't need to calculate statistics for this sampling frequency continue projectText("Calculating stats for site {}, measurement {}".format( site, meas)) # decimation parameters decParams = getDecimationParameters(sampleFreq, projData.config) decParams.printInfo() numLevels = decParams.numLevels # create the spectrum reader specReader = SpectrumReader( os.path.join(siteData.getMeasurementSpecPath(meas), options["specdir"])) # loop through decimation levels for iDec in range(0, numLevels): # open the spectra file for the current decimation level check = specReader.openBinaryForReading("spectra", iDec) if not check: # probably because this decimation level not calculated continue specReader.printInfo() # get windows refTime = specReader.getReferenceTime() winSize = specReader.getWindowSize() winOlap = specReader.getWindowOverlap() numWindows = specReader.getNumWindows() evalFreq = decParams.getEvalFrequenciesForLevel(iDec) sampleFreqDec = specReader.getSampleFreq() globalOffset = specReader.getGlobalOffset() fArray = specReader.getFrequencyArray() statHandlers = {} # create the statistic handlers for stat in options["stats"]: statElements = getStatElements(stat) statHandlers[stat] = StatisticData(stat, refTime, sampleFreqDec, winSize, winOlap) statHandlers[stat].setStatParams(numWindows, statElements, evalFreq) statHandlers[stat].comments = specReader.getComments() statHandlers[stat].addComment( projData.config.getConfigComment()) statHandlers[stat].addComment( "Calculating statistic: {}".format(stat)) statHandlers[stat].addComment( "Statistic components: {}".format( listToString(statElements))) # loop over windows and calculate the relevant statistics for iW in range(0, numWindows): # get data winData = specReader.readBinaryWindowLocal(iW) globalIndex = iW + globalOffset # give the statistic calculator the spectra statCalculator.setSpectra(fArray, winData, evalFreq) # get the desired statistics for sH in statHandlers: data = statCalculator.getDataForStatName(sH) statHandlers[sH].addStat(iW, globalIndex, data) # save statistic for sH in statHandlers: statIO.setDatapath( os.path.join(siteData.getMeasurementStatPath(meas), options["specdir"])) statIO.write(statHandlers[sH], iDec)
def preProcess(projData: ProjectData, **kwargs) -> None: """Pre-process project time data Preprocess the time data using filters, notch filters, resampling or interpolation. A new measurement folder is created under the site. The name of the new measurement folder is: prepend_[name of input measurement]_postpend. By default, prepend is "proc" and postpend is empty. Processed time series data can be saved in a new site by using the outputsite option. Parameters ---------- projData : ProjectData A project data object sites : str, List[str], optional Either a single site or a list of sites sampleFreqs : int, float, List[float], optional The frequencies to preprocess start : str, optional Start date of data to preprocess in format "%Y-%m-%d %H:%M:%S" stop : str, optional Stop date of data to process in format "%Y-%m-%d %H:%M:%S" outputsite : str, optional A site to output the preprocessed time data to. If this site does not exist, it will be created polreverse : Dict[str, bool] Keys are channels and values are boolean flags for reversing scale : Dict[str, float] Keys are channels and values are floats to multiply the channel data by calibrate : bool, optional Boolean flag for calibrating the data. Default is false and setting to True will calibrate where files can be found. normalise : bool, optional Boolean flag for normalising the data. Default is False and setting to True will normalise each channel independently. filter : Dict, optional Filtering options in a dictionary notch : List[float], optional List of frequencies to notch in spectra given as a list of floats resamp : Dict, optional Resampling parameters in a dictionary with entries in the format: {sampleRateFrom: sampleRateTo}. All measurement directories of sampleRateFrom will be resampled to sampleRateTo interp : bool, optional Boolean flag for interpolating the data on to the second, so that sampling is coincident with seconds. This is not always the case. For example, SPAM data is not necessarily sampled on the second, whereas ATS data is. This function is useful when combining data of multiple formats. Interpolation does not change the sampling rate. Default is False. prepend : str, optional String to prepend to the output folder. Default is "proc". postpend : str, optional String to postpend to the output folder. Default is empty. """ options: Dict = {} options["sites"]: List = projData.getSites() options["sampleFreqs"]: List[float] = projData.getSampleFreqs() options["start"]: Union[bool, str] = False options["stop"]: Union[bool, str] = False options["outputsite"]: str = "" options["polreverse"]: Union[bool, Dict[str, bool]] = False options["scale"]: Union[bool, Dict[str, float]] = False options["calibrate"]: bool = False options["normalise"]: bool = False options["filter"]: Dict = {} options["notch"]: List[float] = [] options["resamp"]: Dict = {} options["interp"]: bool = False options["prepend"]: str = "proc" options["postpend"]: str = "" options = parseKeywords(options, kwargs) # print info text: List = ["Processing with options"] for op, val in options.items(): text.append("\t{} = {}".format(op, val)) projectBlock(text) if isinstance(options["sites"], str): options["sites"] = [options["sites"]] # outputting to another site if options["outputsite"] != "": projectText( "Preprocessed data will be saved to output site {}".format( options["outputsite"] ) ) # create the site projData.createSite(options["outputsite"]) projData.refresh() outputSitePath = projData.getSiteData(options["outputsite"]).timePath # output naming outPre = options["prepend"] + "_" if options["prepend"] != "" else "" outPost = "_" + options["postpend"] if options["postpend"] != "" else "" if outPre == "" and outPost == "" and options["outputsite"] == "": outPre = "proc_" # create a data calibrator writer instance cal = Calibrator(projData.calPath) if options["calibrate"]: cal.printInfo() writer = DataWriterInternal() # format dates if options["start"]: options["start"] = datetime.strptime(options["start"], "%Y-%m-%d %H:%M:%S") if options["stop"]: options["stop"] = datetime.strptime(options["stop"], "%Y-%m-%d %H:%M:%S") for site in options["sites"]: siteData = projData.getSiteData(site) siteData.printInfo() # loop over frequencies for sampleFreq in options["sampleFreqs"]: measurements = siteData.getMeasurements(sampleFreq) if len(measurements) == 0: # no data files at this sample rate continue # otherwise, process for meas in measurements: # get the reader projectText("Processing site {}, measurement {}".format(site, meas)) reader = siteData.getMeasurement(meas) startTime = reader.getStartDatetime() stopTime = reader.getStopDatetime() if (options["start"] or options["stop"]) and not checkDateOptions( options, startTime, stopTime ): continue # if the data contributes, copy in the data if relevant if options["start"]: startTime = options["start"] if options["stop"]: stopTime = options["stop"] # calculate the samples sampleStart, sampleEnd = reader.time2sample(startTime, stopTime) # now get the data timeData = reader.getPhysicalSamples( startSample=sampleStart, endSample=sampleEnd ) timeData.printInfo() headers = reader.getHeaders() chanHeaders, chanMap = reader.getChanHeaders() # apply options applyPolarisationReversalOptions(options, timeData) applyScaleOptions(options, timeData) applyCalibrationOptions(options, cal, timeData, reader) applyFilterOptions(options, timeData) applyNotchOptions(options, timeData) applyInterpolationOptions(options, timeData) applyResampleOptions(options, timeData) applyNormaliseOptions(options, timeData) # output dataset path if options["outputsite"] != "": timePath = outputSitePath else: timePath = siteData.timePath outPath = os.path.join(timePath, "{}{}{}".format(outPre, meas, outPost)) # write time data - need to manually change some headers (hence the keywords) writer = DataWriterInternal() writer.setOutPath(outPath) writer.writeData( headers, chanHeaders, timeData, start_time=timeData.startTime.strftime("%H:%M:%S.%f"), start_date=timeData.startTime.strftime("%Y-%m-%d"), stop_time=timeData.stopTime.strftime("%H:%M:%S.%f"), stop_date=timeData.stopTime.strftime("%Y-%m-%d"), numSamples=timeData.numSamples, sample_freq=timeData.sampleFreq, physical=True, ) writer.printInfo()
def viewTime( projData: ProjectData, startDate: str, endDate: str, **kwargs ) -> Union[plt.figure, None]: """View timeseries in the project Parameters ---------- startDate : str The start of the data range to plot endDate : str The end of the date range to plot sites : List[str], optional List of sites sampleFreqs : List[float], optional List of sample frequencies to plot chans : List[str], optional List of channels to plot polreverse : Dict[str, bool] Keys are channels and values are boolean flags for reversing calibrate : bool, optional Boolean flag to calibrate data normalise : bool, optional Boolean flag to normalise the data. Default is False and setting to True will normalise each channel independently. notch : List[float], optional List of frequencies to notch out filter : Dict, optional Filter parameters show : bool, optional Boolean flag to show the plot save : bool, optional Boolean flag to save the plot to images folder plotoptions : Dict Dictionary of plot options Returns ------- matplotlib.pyplot.figure or None A matplotlib figure unless the plot is not shown and is saved, in which case None and the figure is closed. """ # default options options = {} options["sites"]: List[str] = projData.sites options["sampleFreqs"]: Union[List[float], List[str]] = projData.getSampleFreqs() options["chans"]: List[str] = ["Ex", "Ey", "Hx", "Hy", "Hz"] options["polreverse"]: Union[bool, Dict[str, bool]] = False options["calibrate"]: bool = False options["normalise"]: bool = False options["filter"]: Dict = {} options["notch"]: List[float] = [] options["show"]: bool = True options["save"]: bool = False options["plotoptions"]: Dict = plotOptionsTime() options = parseKeywords(options, kwargs) # prepare calibrator cal = getCalibrator(projData.calPath, projData.config) if options["calibrate"]: cal.printInfo() # format startDate and endDate start = datetime.strptime("{}.000".format(startDate), "%Y-%m-%d %H:%M:%S.%f") stop = datetime.strptime("{}.000".format(endDate), "%Y-%m-%d %H:%M:%S.%f") # collect relevant data - dictionary to store timeData timeDataAll = {} for site in options["sites"]: siteData = projData.getSiteData(site) siteData.printInfo() measurements = siteData.getMeasurements() timeDataAll[site] = {} # loop over measurements and save data for each one for meas in measurements: sampleFreq = siteData.getMeasurementSampleFreq(meas) if sampleFreq not in options["sampleFreqs"]: continue # check if data contributes to user defined time period siteStart = siteData.getMeasurementStart(meas) siteStop = siteData.getMeasurementEnd(meas) if siteStop < start or siteStart > stop: continue reader = siteData.getMeasurement(meas) # get the samples of the datetimes sampleStart, sampleStop = reader.time2sample(start, stop) # as the samples returned from time2sample are rounded use sample2time to get the appropriate start and end times for those samples readStart, readStop = reader.sample2time(sampleStart, sampleStop) # get the data for any available channels meaning even those sites with missing channels can be plotted timeData = reader.getPhysicalData(readStart, readStop) projectText( "Plotting measurement {} of site {} between {} and {}".format( meas, site, readStart, readStop ) ) # apply various options applyPolarisationReversalOptions(options, timeData) applyCalibrationOptions(options, cal, timeData, reader) applyFilterOptions(options, timeData) applyNotchOptions(options, timeData) applyNormaliseOptions(options, timeData) timeDataAll[site][meas] = timeData # plot all the data plotfonts = options["plotoptions"]["plotfonts"] fig = plt.figure(figsize=options["plotoptions"]["figsize"]) for site in timeDataAll: for meas in timeDataAll[site]: timeData = timeDataAll[site][meas] timeData.view( sampleStop=timeDataAll[site][meas].numSamples - 1, fig=fig, chans=options["chans"], label="{} - {}".format(site, meas), xlim=[start, stop], plotfonts=plotfonts, ) # add the suptitle st = fig.suptitle( "Time data from {} to {}".format( start.strftime("%Y-%m-%d %H-%M-%S"), stop.strftime("%Y-%m-%d %H-%M-%S") ), fontsize=plotfonts["suptitle"], ) st.set_y(0.98) # do the axis labels numChans = len(options["chans"]) for idx, chan in enumerate(options["chans"]): plt.subplot(numChans, 1, idx + 1) # do the yaxis if isElectric(chan): plt.ylabel("mV/km", fontsize=plotfonts["axisLabel"]) if len(options["plotoptions"]["Eylim"]) > 0: plt.ylim(options["plotoptions"]["Eylim"]) else: if options["calibrate"]: plt.ylabel("nT", fontsize=plotfonts["axisLabel"]) else: plt.ylabel("mV", fontsize=plotfonts["axisLabel"]) if len(options["plotoptions"]["Hylim"]) > 0: plt.ylim(options["plotoptions"]["Hylim"]) plt.legend(loc=1, fontsize=plotfonts["legend"]) # plot format fig.tight_layout(rect=[0, 0.02, 1, 0.96]) fig.subplots_adjust(top=0.92) # plot show and save if options["save"]: impath = projData.imagePath filename = "timeData_{}_{}".format( start.strftime("%Y-%m-%d_%H-%M-%S_"), stop.strftime("%Y-%m-%d_%H-%M-%S") ) savename = savePlot(impath, filename, fig) projectText("Image saved to file {}".format(savename)) if options["show"]: plt.show(block=options["plotoptions"]["block"]) if not options["show"] and options["save"]: plt.close(fig) return None return fig
def calculateSpectra(projData: ProjectData, **kwargs) -> None: """Calculate spectra for the project time data The philosophy is that spectra are calculated out for all data and later limited using statistics and time constraints Parameters ---------- projData : ProjectData A project data object sites : str, List[str], optional Either a single site or a list of sites sampleFreqs : int, float, List[float], optional The frequencies in Hz for which to calculate the spectra. Either a single frequency or a list of them. chans : List[str], optional The channels for which to calculate out the spectra polreverse : Dict[str, bool] Keys are channels and values are boolean flags for reversing scale : Dict[str, float] Keys are channels and values are floats to multiply the channel data by calibrate : bool, optional Flag whether to calibrate the data or not notch : List[float], optional List of frequencies to notch filter : Dict, optional Filter parameters specdir : str, optional The spectra directory to save the spectra data in """ # default options options = {} options["sites"] = projData.getSites() options["sampleFreqs"]: List[float] = projData.getSampleFreqs() options["chans"]: List[str] = [] options["polreverse"]: Union[bool, Dict[str, bool]] = False options["scale"]: Union[bool, Dict[str, float]] = False options["calibrate"]: bool = True options["notch"]: List[float] = [] options["filter"]: Dict = {} options["specdir"]: str = projData.config.configParams["Spectra"]["specdir"] options = parseKeywords(options, kwargs) # prepare calibrator cal = getCalibrator(projData.calPath, projData.config) if options["calibrate"]: cal.printInfo() datetimeRef = projData.refTime for site in options["sites"]: siteData = projData.getSiteData(site) siteData.printInfo() # calculate spectra for each frequency for sampleFreq in options["sampleFreqs"]: measurements = siteData.getMeasurements(sampleFreq) projectText( "Site {} has {:d} measurement(s) at sampling frequency {:.2f}".format( site, len(measurements), sampleFreq ) ) if len(measurements) == 0: continue # no data files at this sample rate for meas in measurements: projectText( "Calculating spectra for site {} and measurement {}".format( site, meas ) ) # get measurement start and end times - this is the time of the first and last sample reader = siteData.getMeasurement(meas) startTime = siteData.getMeasurementStart(meas) stopTime = siteData.getMeasurementEnd(meas) dataChans = ( options["chans"] if len(options["chans"]) > 0 else reader.getChannels() ) timeData = reader.getPhysicalData(startTime, stopTime, chans=dataChans) timeData.addComment(breakComment()) timeData.addComment("Calculating project spectra") timeData.addComment(projData.config.getConfigComment()) # apply various options applyPolarisationReversalOptions(options, timeData) applyScaleOptions(options, timeData) applyCalibrationOptions(options, cal, timeData, reader) applyFilterOptions(options, timeData) applyNotchOptions(options, timeData) # define decimation and window parameters decParams = getDecimationParameters(sampleFreq, projData.config) decParams.printInfo() numLevels = decParams.numLevels winParams = getWindowParameters(decParams, projData.config) dec = Decimator(timeData, decParams) timeData.addComment( "Decimating with {} levels and {} frequencies per level".format( numLevels, decParams.freqPerLevel ) ) # loop through decimation levels for iDec in range(0, numLevels): # get the data for the current level check = dec.incrementLevel() if not check: break # not enough data timeData = dec.timeData # create the windower and give it window parameters for current level sampleFreqDec = dec.sampleFreq win = Windower( datetimeRef, timeData, winParams.getWindowSize(iDec), winParams.getOverlap(iDec), ) if win.numWindows < 2: break # do no more decimation # add some comments timeData.addComment( "Evaluation frequencies for this level {}".format( listToString(decParams.getEvalFrequenciesForLevel(iDec)) ) ) timeData.addComment( "Windowing with window size {} samples and overlap {} samples".format( winParams.getWindowSize(iDec), winParams.getOverlap(iDec) ) ) # create the spectrum calculator and statistics calculators specCalc = SpectrumCalculator( sampleFreqDec, winParams.getWindowSize(iDec) ) # get ready a file to save the spectra specPath = os.path.join( siteData.getMeasurementSpecPath(meas), options["specdir"] ) specWrite = SpectrumWriter(specPath, datetimeRef) specWrite.openBinaryForWriting( "spectra", iDec, sampleFreqDec, winParams.getWindowSize(iDec), winParams.getOverlap(iDec), win.winOffset, win.numWindows, dataChans, ) # loop though windows, calculate spectra and save for iW in range(0, win.numWindows): # get the window data winData = win.getData(iW) # calculate spectra specData = specCalc.calcFourierCoeff(winData) # write out spectra specWrite.writeBinary(specData) # close spectra file specWrite.writeCommentsFile(timeData.getComments()) specWrite.closeFile()