def EnableDebugging(): global _debugging _debugging = True debug.dprint("Enabled debugging") return
def DisableDebugging(): global _debugging _debugging = False debug.dprint("Disabled debugging") return
def _initialise_parameters(self): """ Initialise the parameters used in evaluating the Lomb-Scargle periodogram """ self._mean = MeanVal(self._x) self._variance = 0.0 for x in self._x: self._variance += (x - self._mean)**2 self._variance /= float(self.DataPointsCount() - 1) debug.dprint("Mean = " + str(self._mean), 3) debug.dprint("Variance = " + str(self._variance), 3) return
def _initialise_parameters(self): """ Initialise the parameters used in evaluating the Lomb-Scargle periodogram """ self._mean = MeanVal(self._x) self._variance = 0.0 for x in self._x: self._variance += (x - self._mean) ** 2 self._variance /= float(self.DataPointsCount() - 1) debug.dprint("Mean = " + str(self._mean), 3) debug.dprint("Variance = " + str(self._variance), 3) return
def InterpolatedSSA(v, t, N_T, n, J=1, t0=None, t1=None): """ Perform a singular systems analysis of the supplied non-uniform data using linear interpolation """ if t0 is None: t0 = t[0] if t1 is None: t1 = t[-1] dt = (t1 - t0) / float(N_T) debug.dprint("Interpolation dt: " + str(dt)) lt = [t0 + i * dt for i in range(N_T)] lv = LinearlyInterpolateField(v, t, lt) print lv, lt return SSA(lv, n, J=J)
def InterpolatedSSA(v, t, N_T, n, J = 1, t0 = None, t1 = None): """ Perform a singular systems analysis of the supplied non-uniform data using linear interpolation """ if t0 is None: t0 = t[0] if t1 is None: t1 = t[-1] dt = (t1 - t0) / float(N_T) debug.dprint("Interpolation dt: " + str(dt)) lt = [t0 + i * dt for i in range(N_T)] lv = LinearlyInterpolateField(v, t, lt) print lv, lt return SSA(lv, n, J = J)
def SSA(v, n, J=1): """ Perform a singular systems analysis of the supplied array of data. See: Inertia-Gravity Wave Generation by Baroclinic Instability, Tom Jacoby, First year report, AOPP, September 2007 """ N_T = len(v) N = N_T - (n - 1) * J shape = (n, N) debug.dprint("Assembling matrix for SSA, shape = " + str(shape)) X = numpy.empty(shape) for i in range(N): for j in range(n): X[j, i] = v[i + j * J] theta = numpy.dot(X, X.transpose()) / float(N) del (X) debug.dprint("Performing eigendecomposition") return Eigendecomposition(theta, returnEigenvectors=True)
def SSA(v, n, J = 1): """ Perform a singular systems analysis of the supplied array of data. See: Inertia-Gravity Wave Generation by Baroclinic Instability, Tom Jacoby, First year report, AOPP, September 2007 """ N_T = len(v) N = N_T - (n - 1) * J shape = (n, N) debug.dprint("Assembling matrix for SSA, shape = " + str(shape)) X = numpy.empty(shape) for i in range(N): for j in range(n): X[j, i] = v[i + j * J] theta = numpy.dot(X, X.transpose()) / float(N) del(X) debug.dprint("Performing eigendecomposition") return Eigendecomposition(theta, returnEigenvectors = True)
def Read(self, filename, includeMc = False, subsample = 1): """ Read a .stat file """ def ParseRawS(s, delimiter): newS = {} for key1 in s.keys(): assert(not key1 in ["val", "value"]) if isinstance(s[key1], dict): if len(s[key1].keys()) == 1 and s[key1].keys()[0] in ["val", "value"]: newS[str(key1)] = s[key1][s[key1].keys()[0]] else: subS = ParseRawS(s[key1], delimiter) newS[str(key1)] = {} for key2 in subS.keys(): newS[str(key1)][str(key2)] = subS[key2] else: rank = len(s[key1].shape) if rank > 1: assert(rank == 2) if includeMc: # Add in this vector # parser gives this in an inconvenient matrix order. Take the # transpose here to make life easier. newS[str(key1)] = s[key1].transpose() # Add in the vector field components for i in range(len(s[key1])): newS[str(key1) + delimiter + str(i + 1)] = s[key1][i] else: try: # Add in this scalar newS[str(key1)] = s[key1] except TypeError: debug.deprint("Type error for data " + str(s[key1]), 0) raise Exception("ParseRawS failure") except ValueError: debug.deprint("Value error for data " + str(s[key1]), 0) raise Exception("ParseRawS failure") return newS debug.dprint("Reading .stat file: " + filename) if subsample == 1: # Handle this case separately, as it's convenient to be backwards # compatible statParser = statfile.parser(filename) else: statParser = statfile.parser(filename, subsample = subsample) self._s = ParseRawS(statParser, self._delimiter) if "ElapsedTime" in self.keys(): t = self["ElapsedTime"] if t.shape[0] > 0: debug.dprint("Time range: " + str((t[0], t[-1]))) else: debug.dprint("Time range: No data") return
def EnablePsyco(): if PsycoSupport(): psyco.full() debug.dprint("Enabled psyco specialising compiler") return
def DominantModeUnstructured(amps, times, N = 250, tMin = None, tMax = None): """ Compute the period and amplitude of the dominant mode in an uneven data series. """ def Omegas(ts): return [2.0 * math.pi / t for t in ts] debug.dprint("Finding dominant mode") assert(len(amps) == len(times)) if tMin is None: tMin = Inf() for i in range(len(times) -1): tMin = min(tMin, times[i + 1] - times[i]) tMin *= 2.0 if tMax is None: tMax = 2.0 * (times[-1] - times[0]) debug.dprint("Period bounds = " + str((tMin, tMax)), 1) ls = NormalisedLombScargle(amps, times) amp = 0.0 t = 0.0 while True: debug.dprint("Period bounds = " + str((tMin, tMax)), 2) ts = [tMin + (tMax - tMin) * float(i) / float(N - 1) for i in range(N)] # Note the factor of two - we want the least squares harmonic # fit amplitude, not the FFT amplitude lsAmps = 2.0 * ls.Evaluate(Omegas(ts)) lastT = t amp = 0.0 for i, lsAmp in enumerate(lsAmps): if lsAmp > amp: t = ts[i] amp = lsAmp if AlmostEquals(t, lastT): break tMin = t - (tMax - tMin) / float(N - 1) tMax = t + (t - tMin) N = 4 debug.dprint("Period = " + str(t)) debug.dprint("Amplitude = " + str(amp)) debug.dprint("Done") return t, amp
def JoinStat(*args): """ Joins a series of stat files together. Useful for combining checkpoint .stat files. Selects data in later stat files over earlier stat files. Assumes data in stat files are sorted by ElapsedTime. """ nStat = len(args) assert(nStat > 0) times = [stat["ElapsedTime"] for stat in args] startT = [t[0] for t in times] permutation = utils.KeyedSort(startT, range(nStat)) stats = [args[index] for index in permutation] startT = [startT[index] for index in permutation] times = [times[index] for index in permutation] endIndices = numpy.array([len(time) for time in times], dtype = int) for i, t in enumerate(times[:-1]): for j, time in enumerate(t): if calc.AlmostEquals(startT[i + 1], time, tolerance = 1.0e-6): endIndices[i] = max(j - 1, 0) break elif startT[i + 1] < time: endIndices[i] = j break debug.dprint("Time ranges:") if len(times) > 0: for i in range(nStat): debug.dprint((startT[i], times[i][endIndices[i] - 1])) else: debug.dprint("No data") dataIndices = numpy.empty(len(args) + 1, dtype = int) dataIndices[0] = 0 for i, index in enumerate(endIndices): dataIndices[i + 1] = dataIndices[i] + index stat = stats[0] data = {} for key in stat.keys(): arr = stat[key] shape = list(arr.shape) shape[0] = dataIndices[-1] data[key] = numpy.empty(shape, dtype = arr.dtype) data[key][:dataIndices[1]] = arr[:endIndices[0]] data[key][dataIndices[1]:] = calc.Nan() delimiter = stat.GetDelimiter() for i in range(1, nStat): stat = stats[i] for key in stat.keys(): arr = stat[key] if not key in data: shape = list(arr.shape) shape[0] = dataIndices[-1] data[key] = numpy.empty(shape, dtype = arr.dtype) data[key][:dataIndices[i]] = calc.Nan() data[key][dataIndices[i + 1]:] = calc.Nan() data[key][dataIndices[i]:dataIndices[i + 1]] = arr[:endIndices[i]] output = Stat(delimiter = delimiter) for key in data.keys(): output[key] = numpy.array(data[key]) return output
def DominantModeUnstructured(amps, times, N=250, tMin=None, tMax=None): """ Compute the period and amplitude of the dominant mode in an uneven data series. """ def Omegas(ts): return [2.0 * math.pi / t for t in ts] debug.dprint("Finding dominant mode") assert (len(amps) == len(times)) if tMin is None: tMin = Inf() for i in range(len(times) - 1): tMin = min(tMin, times[i + 1] - times[i]) tMin *= 2.0 if tMax is None: tMax = 2.0 * (times[-1] - times[0]) debug.dprint("Period bounds = " + str((tMin, tMax)), 1) ls = NormalisedLombScargle(amps, times) amp = 0.0 t = 0.0 while True: debug.dprint("Period bounds = " + str((tMin, tMax)), 2) ts = [tMin + (tMax - tMin) * float(i) / float(N - 1) for i in range(N)] # Note the factor of two - we want the least squares harmonic # fit amplitude, not the FFT amplitude lsAmps = 2.0 * ls.Evaluate(Omegas(ts)) lastT = t amp = 0.0 for i, lsAmp in enumerate(lsAmps): if lsAmp > amp: t = ts[i] amp = lsAmp if AlmostEquals(t, lastT): break tMin = t - (tMax - tMin) / float(N - 1) tMax = t + (t - tMin) N = 4 debug.dprint("Period = " + str(t)) debug.dprint("Amplitude = " + str(amp)) debug.dprint("Done") return t, amp