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_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 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)