def predict_tides(station, dtg=None): """Predict the tide level at a station and date. :param station_id: The nearest stations id. :type station_id: String :param dtg: Date time group. :type dtg: String -- "Y-m-d-H-M" """ action = 'Predicting tides for station %s and dtg %s' % (station, dtg) app.logger.debug('ACTOR:`%s` ACTION:`%s` DTG:`%s`', 'bf-tidepredicition', action, datetime.utcnow().isoformat() + 'Z') if dtg is None: dtg = datetime.now() dtg = datetime.strftime(dtg, '%Y-%m-%d-%H-%M') prediction_t0 = datetime.strptime(dtg, '%Y-%m-%d-%H-%M') else: prediction_t0 = datetime.strptime(dtg, '%Y-%m-%d-%H-%M') hours = 0.1 * np.arange(1 * 24 * 10) times = Tide._times(prediction_t0, hours) # Predict the tides using the Pytides model. try: model = TIDE_MODEL[station] my_prediction = model.at(times) ctide = float(my_prediction[0]) / 1000 mint = float(min(my_prediction)) / 1000 maxt = float(max(my_prediction)) / 1000 except: ctide = 'null' mint = 'null' maxt = 'null' return mint, maxt, ctide, str(times[0])
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 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 predict_tides(station, dtg=None): """Predict the tide level at a station and date. :param station_id: The nearest stations id. :type station_id: String :param dtg: Date time group. :type dtg: String -- "Y-m-d-H-M" """ if dtg is None: dtg = datetime.now() dtg = datetime.strftime(dtg, '%Y-%m-%d-%H-%M') prediction_t0 = datetime.strptime(dtg, '%Y-%m-%d-%H-%M') else: prediction_t0 = datetime.strptime(dtg, '%Y-%m-%d-%H-%M') hours = 0.1 * np.arange(1 * 24 * 10) times = Tide._times(prediction_t0, hours) # Predict the tides using the Pytides model. try: model = TIDE_MODEL[station] my_prediction = model.at(times) ctide = float(my_prediction[0]) / 1000 mint = float(min(my_prediction)) / 1000 maxt = float(max(my_prediction)) / 1000 except: ctide = 'null' mint = 'null' maxt = 'null' return mint, maxt, ctide, str(times[0])
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 execute(args): times = pyTide._times(datetime.fromordinal(args.start_date.toordinal()), np.arange(args.length * 24., dtype=float)) tide = Tide().reconstruct_tide(loc=[args.lat, args.lon], times=times, model=args.model, cons=args.cons, positive_ph=args.positive_phase) out = tide.data.to_csv(args.output, sep='\t', header=True, index=False) if args.output is None: print(out) print('\nComplete.\n')
def tidal_model(tide_station_name='Fort_Point'): """ This function gives a model of the predicted tides. To get tides at a specific time, use the command: tide.at([datetime(year,month,day,hour,minute)]) Input: tide_station_name - Name of the tide station for which we want the tidal model Output: tide_model - Model of the tides using NOAA's Harmonic Constituents """ # Determine which station was inputed - to make easier, we are converting # all inputs to a lowercase string ts = str(tide_station_name).lower() if (ts == 'fort_point' or ts == 'fort point' or ts == '8423898'): tide_station = 'Fort_Point' elif (ts == 'wells' or ts == 'wells, me' or ts == '8419317'): tide_station = 'Wells' elif (ts == 'boston' or ts == 'boston, ma' or ts == '8443970'): tide_station = 'Boston' else: print 'ERROR: Invalid Tide Station. Create a new file for {}.'.format( tide_station_name) return None station_filename = '{}/{}.txt'.format(tide_station, tide_station) station_info = parse_file(station_filename) # These are the NOAA constituents, in the order presented on their website. constituents = [c for c in cons.noaa if c != cons._Z0] # Phases and amplitudes (relative to GMT and in degrees and meters) published_amplitudes = station_info[0][1] published_phases = station_info[1][1] # We can add a constant offset (e.g. for a different datum, we will use # relative to MLLW): # MTL = station_info[4][1] MSL = station_info[5][1] MLLW = station_info[6][1] offset = MSL - MLLW constituents.append(cons._Z0) published_phases.append(0) published_amplitudes.append(offset) # Build the model. assert (len(constituents) == len(published_phases) == len(published_amplitudes)) model = np.zeros(len(constituents), dtype=Tide.dtype) model['constituent'] = constituents model['amplitude'] = published_amplitudes model['phase'] = published_phases tide = Tide(model=model, radians=False) return tide
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 prediction(tide, tide_station_name, start_date=dt(2016, 10, 23)): """ Predicts the tide for a week from the given start date (in GMT) and prints out a plot of the Pytide tide model vs NOAA tide model vs Actual Tide. Inputs: tide - Model for the predicted tides from pytides using NOAA's Harmonic Constituents tide_station_name - Name of the tide station for which we want the tidal model start_date - date that you want the first measurement from format: datetime(year,month,day,hour,minute) """ if tide: ##Prepare a list of datetimes, each 6 minutes apart, for a week. prediction_t0 = start_date hours = 0.1 * np.arange(7 * 24 * 10) times = Tide._times(prediction_t0, hours) ##Prepare NOAA's results noaa_verified = [] noaa_predicted = [] with open('{}/actual.txt'.format(tide_station_name)) as f: for line in f: # temp stores the line we can store the first value as a float temp = line.split() noaa_verified.append(float(temp[0])) with open('{}/predict.txt'.format(tide_station_name)) as f1: for line in f1: # temp stores the line we can store the first value as a float temp = line.split() noaa_predicted.append(float(temp[0])) my_prediction = tide.at(times) fig, ax = plt.subplots(nrows=1, ncols=1) # create figure & 1 axis plt.plot(hours, my_prediction, label="Pytides") plt.plot(hours, noaa_predicted, label="NOAA Prediction") plt.plot(hours, noaa_verified, label="NOAA Verified") plt.legend() plt.title('Comparison of Pytides and NOAA predictions for {}'.format( tide_station_name)) plt.xlabel('Hours since {} (GMT)'.format(prediction_t0)) plt.ylabel('Meters') plt.savefig('{}/{}_Tides_2016_Oct23.png'.format( tide_station_name, tide_station_name)) return my_prediction, noaa_predicted, noaa_verified, hours else: print 'ERROR: Invalid Tide Station. Create a new file for {}.'.format( tide_station_name)
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_kings_point(self): # Check that Pytides prediction in King's Point for 0000 and 0600 GMT # on January 1 2013 are [-0.086250887498591222 2.207534179351927]. # Results have been updated to take into account commit #7d5e3c7 # These are the NOAA constituents, in the order presented on their website. constituents = [c for c in cons.noaa if c != cons._Z0] # Phases and amplitudes (relative to GMT and in degrees and metres) published_phases = [115.7, 140.7, 92.6, 192, 145.5, 220.6, 159.9, 202.8, 152.3, 117.2, 92, 0, 0, 69.7, 224.5, 141.7, 121.9, 228.4, 252.1, 0, 60.1, 135.5, 0, 0, 204.5, 212.2, 112.3, 141.8, 249.1, 211.1, 75.1, 181.4, 140.4, 202.4, 141.8, 155, 160.9] published_amplitudes = [1.142, 0.189, 0.241, 0.1, 0.036, 0.066, 0.08, 0.01, 0.004, 0.022, 0.052, 0, 0, 0.03, 0.007, 0.025, 0.009, 0.005, 0.008, 0, 0.024, 0.065, 0, 0, 0.004, 0.017, 0.015, 0.002, 0.002, 0.032, 0.003, 0.007, 0.07, 0.009, 0.053, 0.007, 0.008] # We can add a constant offset (e.g. for a different datum, we will use relative to MLLW): MTL = 5.113 MLLW = 3.928 offset = MTL - MLLW constituents.append(cons._Z0) published_phases.append(0) published_amplitudes.append(offset) # Build the model. assert(len(constituents) == len(published_phases) == len(published_amplitudes)) model = np.zeros(len(constituents), dtype=Tide.dtype) model['constituent'] = constituents model['amplitude'] = published_amplitudes model['phase'] = published_phases tide = Tide(model=model, radians=False) heights = tide.at([datetime(2013, 1, 1, 0, 0, 0), datetime(2013, 1, 1, 6, 0, 0)]) self.assertEqual(heights[0], -0.086250887498591222) self.assertEqual(heights[1], 2.207534179351927)
def build_tide_models(tide_model_file): """Build models for all stations. :returns: Dict -- {'station_id': model, ...} """ # We try to read the pre fitted tidal models # if they don't exists we create them # this model for all the stations # is very tiny, so run locally # if the database of station data is updated # to re-fit the model. logAudit(severity=7, actor="bf-tideprediction", action="buildingTideModels", actee=tide_model_file, message="Building tide models from file %s" % tide_model_file) try: with open(tide_model_file, 'rb') as tm: tide_models = dill.load(tm) except IOError: tide_models = {} for station in all_stations(): # station is like (1, ) station = station[0] data = station_data(station) model = build_tide_model(data) if model is not None: # see below, model is pickelable # Tide is not. tide_models[station] = model else: tide_models[station] = None # Store data for later with open(tide_model_file, 'wb') as tm: dill.dump(tide_models, tm, protocol=dill.HIGHEST_PROTOCOL) # we are doing it like this because the instantiated Tide # is apparently not pickelable even using dill, also # we can't build a model if v is None. return { k: Tide(model=v, radians=False) if v is not None else v for (k, v) in tide_models.iteritems() }
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 execute(args): """Execute the reconstruct CLI command. Args: args (...): Variable length positional arguments """ times = pyTide._times(datetime.fromordinal(args.start_date.toordinal()), np.arange(args.length * 24., dtype=float)) tide = Tide(model=args.model).reconstruct_tide( loc=[args.lat, args.lon], times=times, cons=args.cons, positive_ph=args.positive_phase) out = tide.data.to_csv(args.output, sep='\t', header=True, index=False) if args.output is None: print(out) print('\nComplete.\n')
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 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
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()
t.append(datetime_object) for y in data.data: if (y != 'data'): 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'):
ax.plot(t[:, 0], t[:, 1], linewidth=2) ax.plot(de_tide[:, 0], de_tide[:, 1], linewidth=0.75, color="red") set_monthsFmt(ax) ylabel("Elevation, m") # show() # <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.