def test_decompose(): df = pd.read_csv("tests/testdata/wl.csv", parse_dates=True, index_col=0) my_tide = Tide.decompose(heights=df.heights, t=df.index) assert len(my_tide.model) == 43
def do_harmonics(Times, series, name): """ Perform the harmonic analysis on a single time series. This makes this function much more usable in parallel. Parameters ---------- Times : datetime datetime object of the times. series : ndarray Time series data to analyse (e.g. u, v, zeta). name : str Station name. Returns ------- modelled : dict Dictionary of the results with keys 'constituent', 'phase' and 'amplitude' of type list (of strings), ndarray and ndarray, respectively. """ analysis = Tide.decompose(series, Times) modelled = {} modelled['constituent'] = [c.name for c in analysis.model['constituent']] modelled['amplitude'] = analysis.model['amplitude'] modelled['phase'] = analysis.model['phase'] return modelled, name
def deconstruct_tide(self, water_level, times, cons=[], n_period=6, positive_ph=False): """Method to use pytides to deconstruct the tides and reorganize results back into the class structure. Args: water_level (ndarray(float)): Array of water levels. times (ndarray(datetime)): Array of datetime objects associated with each water level data point. cons (list(str), optional): List of constituents requested, defaults to all constituents if None or empty. n_period(int): Number of periods a constituent must complete during times to be considered in analysis. positive_ph (bool, optional): Indicate if the returned phase should be all positive [0 360] (True) or [-180 180] (False, the default). Returns: A dataframe of constituents information in Constituents class """ # Fit the tidal data to the harmonic model using pytides if not cons: cons = pycons.noaa else: cons = [ eval('pycons._{}'.format(self.PYTIDES_CON_MAPPER.get(c, c))) for c in cons if c in self.constituents.NOAA_SPEEDS ] self.model_to_dataframe(pyTide.decompose(water_level, times, constituents=cons, n_period=n_period), times[0], positive_ph=positive_ph) return self
def build_tide_model(data): """Builds a model given tide data. :param data: list of tuples [(date, height),...)] :returns: Pytides model or None if data insufficient. """ try: dates, heights = zip(*data) dates = [datetime.strptime(date, '%Y-%m-%d-%H') for date in dates] return Tide.decompose(heights, dates).model except: return None
def build_tide_model(data): """Builds a model given tide data. :param data: list of tuples [(date, height),...)] :returns: Pytides model or None if data insufficient. """ app.logger.debug('ACTOR:`%s` ACTION:`%s` DTG:`%s`', 'bf-tidepredicition', 'Building tide model from data', datetime.utcnow().isoformat() + 'Z') # historic dates and heights try: dates, heights = zip(*data) dates = [datetime.strptime(date, '%Y-%m-%d-%H') for date in dates] return Tide.decompose(heights, dates).model except: return None
def build_tide_model(data): """Builds a model given tide data. :param data: list of tuples [(date, height),...)] :returns: Pytides model or None if data insufficient. """ logAudit(severity=7, actor="bf-tideprediction", action="buildingTideModel", message="Building Tide Model From Data") # historic dates and heights try: dates, heights = zip(*data) dates = [datetime.strptime(date, '%Y-%m-%d-%H') for date in dates] return Tide.decompose(heights, dates).model except: logAudit(severity=2, actor="bf-tideprediction", action="failedTideModel", message="A Tide Model Failed to Build") return None
def get_WaterLevelForecast(foredate, place): historyFileNm = "/megdata/data_meg-logic/real_data.txt" # カラム名のサンプル ####################### # date,obsrPoint,waterLevel,precip,temp,odor ############################################# df0 = pd.read_csv(historyFileNm, index_col=0, parse_dates=True) water_level = df0['waterLevel'] demeaned = water_level.values - water_level.values.mean() tide = Tide.decompose(demeaned, water_level.index.to_datetime()) constituent = [c.name for c in tide.model['constituent']] df = DataFrame(tide.model, index=constituent).drop('constituent', axis=1) df.sort_values('amplitude', ascending=False).head(10) # df.sort_values('phase', 0, ascending=False).head(20) # 今日の日付を取得 today = datetime.datetime.today() # 翌日を取得 # ※テスト用に5日後 next_day = today + datetime.timedelta(days=5) dates = date_range(start=today.strftime('%Y-%m-%d'), end=next_day.strftime('%Y-%m-%d'), freq='10T') hours = np.cumsum(np.r_[ 0, [t.total_seconds() / 3600.0 for t in np.diff(dates.to_pydatetime())]]) times = Tide._times(dates[0], hours) prediction = Series(tide.at(times) + water_level.values.mean(), index=dates) return prediction
def _compute(self): self.tidedata = {} self.predictiondata = {} self.last_time = np.max(self.epochlist) for name, const in self.constituent.items(): t0 = self.time.tolist()[0] hours = self.prediction_interval * np.arange(self.days * 24 * 10) self.times = Tide._times(t0, hours) data = Tide.decompose(self.xmag, self.time.tolist(), None, None, self.constituent[name]) try: self.tidedata[name] = data pred = self.tidedata[name].at(self.times) self.predictiondata[name] = pred * self.scale except: print "RECOMPUTE DEBUG\n Constutuent: {}".format( self.constituent) print " Value of self.tidedata {}\n Value of self.time {}\n".format( self.tidedata, self.time) return return self.predictiondata
def test_example_observations_2(): observations = Observations.from_csv( fixture_filename('example_observations_2.csv')) datetimes, heights = observations.to_numpy_arrays() my_tide = Tide.decompose(heights, datetimes) predictions = my_tide.at(datetimes) for i, expected in [ (0, 1.2301709375405498), (5, 1.4042197729365355), (10, 1.25361670249086), (50, 1.252261186358141), (100, 1.2792179066476739), (500, 1.3432777519478505), (1000, 1.7457095044631059), (1500, 1.472996756326626), (2000, 1.3003693570959916), (2500, 1.6887094920566177), (7500, 1.2907397986524105), ]: yield _assert_prediction_is_correct, expected, predictions[i]
def dectide(u, tt, filename): tide = Tide.decompose(u, tt, filename=filename) c = [c.name for c in tide.model['constituent']] a = tide.model['amplitude'] p = tide.model['phase'] return a, p, c, tide
heights.append(float(y)) #print heights #print t #For a quicker decomposition, we'll only use hourly readings rather than 6-minutely readings. # heights = np.array(heights[::10]) # t = np.array(t[::10]) ##Prepare a list of datetimes, each 6 minutes apart, for a week. prediction_t0 = datetime(2016,10,4) hours = np.arange(6 * 24) times = Tide._times(prediction_t0, hours) # print times ##Fit the tidal data to the harmonic model using Pytides my_tide = Tide.decompose(heights, t) ##Predict the tides using the Pytides model. my_prediction = my_tide.at(times) ##Prepare NOAA's results noaa_verified = [] noaa_predicted = [] column = ['time', 'prediction', 'verified'] result = pd.read_csv('/Users/anonymous/clawpack_src/clawpack-v5.4.1rc-beta/geoclaw/examples/storm-surge/matthew/detide/data/FernandinaBeach_noaa.csv', header=None, names=column) for x in result.prediction: if (x != 'prediction'): print x noaa_predicted.append(float(y)) for y in result.verified: if (y != 'verified'):
# <markdowncell> # ####Prepare a list of datetimes, each 6 minutes apart, for a the period of the dataset. # <codecell> total_hours = float((time_all[-1] - time_all[0]).total_seconds()) / (60.0 * 60.0) nsamples = time_all.shape[0] hours = linspace(0.0, float(total_hours), nsamples) times = Tide._times(time_all[0], hours) print times # <codecell> ##Fit the tidal data to the harmonic model using Pytides current_data = tide[2][:, 1].astype(float) current_date = tide[2][:, 0] print len(current_date), current_date print len(current_data), current_data my_tide = Tide.decompose(current_data, t=current_date) print my_tide.model["amplitude"] print my_tide.model["phase"] for c in my_tide.model["constituent"]: print c.name ##Predict the tides using the Pytides model. my_prediction = my_tide.at(times) print my_prediction # <codecell>
datetime.strptime(k_tides["date"][x] + " " + k_tides["time"][x], "%Y-%m-%d %H:%M") for x in range(len(k_tides['date'])) ] #print(f) # for i, line in enumerate(f): # t.append(datetime.strptime(" ".join(line.split()[:2]), "%Y-%m-%d %H:%M")) # heights.append(float(line.split()[2])) # f.close() #height = [-0.56,0.48,-0.52,0.6,-0.59,0.52,-0.55,0.62,-0.63,0.59,-0.59,0.64,-0.69,0.67,-0.64,0.67] #date = ["6/8/2018","6/8/2018","6/8/2018","6/9/2018","6/9/2018","6/9/2018","6/9/2018","6/10/2018","6/10/2018","6/10/2018","6/10/2018","6/11/2018","6/11/2018","6/11/2018","6/11/2018","6/12/2018"] #time = ["5:48","11:55","17:56","0:10","6:29","12:39","18:43","0:55","7:13","13:26","19:33","1:43","7:59","14:15","20:26","2:33"] #date_time = [datetime.strptime(date[x] + " "+time[x], "%m/%d/%Y %H:%M") for x in range(len(date))] prediction_t0 = datetime(2018, 6, 8) hours = 0.1 * np.arange(7 * 24 * 10) times = Tide._times(prediction_t0, hours) my_tide = Tide.decompose(k_tides['height'], k_tides['date_time']) my_prediction = my_tide.at(times) f = interp1d(k_tides['date_time'], k_tides['height'], kind='cubic') plt.plot(times, my_prediction) plt.plot(k_tides['date_time'], k_tides['height']) plt.show() #my_prediction = my_tide.at(times) #print(my_tide) #plt.plot(my_tide) #plt.show()