예제 #1
0
 def extract_otps_range(self, date_list):
     # open the otps lon/lat file
     tp = list()
     ln = 0
     la = 0
     if self.tide_post:
         ln = eval(self.tide_post)[0]
         la = eval(self.tide_post)[1]
     else:
         print(
             "reading from tidal model file and using Haversine algorithm to extract shortest distance"
         )
         from operator import itemgetter
         # first find centroid of lat lon range
         la = (self.lat[0] + self.lat[1]) / 2
         ln = (self.lon[0] + self.lon[1]) / 2
         rdlist = list()
         fname = "./epoch_tide_post_model.csv"
         try:
             with open(fname, 'rt') as f:
                 reader = csv.reader(f, delimiter=',')
                 for rd in reader:
                     rdlist.append((rd[0], rd[1],
                                    self.distance(la, ln, float(rd[1]),
                                                  float(rd[0]))))
             rdlist = sorted(rdlist, key=itemgetter(2))
             print("Found tide post coordinates and shortest distance",
                   rdlist[0])
             la = float(rdlist[0][1])
             ln = float(rdlist[0][0])
         except IOError as e:
             print("Unable to open file: " + str(fname))
             sys.exit()
     for dt in date_list:
         tp.append(TimePoint(ln, la, dt))
     tides = predict_tide(tp)
     if len(tides) == 0:
         print(
             "No tide height observed from OTPS model within lat/lon range")
         sys.exit()
     print("received from predict tides ", str(datetime.now()))
     date_low = list()
     date_high = list()
     tide_dict = dict()
     for tt in tides:
         tide_dict[datetime.strptime(
             tt.timepoint.timestamp.isoformat()[0:19],
             "%Y-%m-%dT%H:%M:%S")] = tt.tide_m
     tide_dict = sorted(tide_dict.items(), key=lambda x: x[1])
     # lowest = round(float(self.per)*len(tide_dict)/100)
     if self.debug:
         print("")
         print("ALL TIDES LIST",
               [[datetime.strftime(date[0], "%Y-%m-%d"), date[1]]
                for date in tide_dict])
         print("")
     MY_DATE['ALL_TIDES'] = [[x] for x in tide_dict]
     return
예제 #2
0
def load_tide_model(all_dates, lon, lat):
    """
    Load otps module and pass a list of tide information

    :param all_dates: Input a list of dates
    :param lon: model longitude
    :param lat: model latitude
    :return: a list of tides
    """
    try:
        from otps.predict_wrapper import predict_tide
        from otps import TimePoint
    except ImportError:
        raise StatsConfigurationError("otps module not found. Please load otps module separately ...")

    return predict_tide([TimePoint(lon, lat, dt) for dt in all_dates])
예제 #3
0
 def extract_ebb_flow_tides(self, date_list):
     tp = list()
     tide_dict = dict()
     ln = eval(self.tide_post)[0]
     la = eval(self.tide_post)[1]
     ndate_list = list()
     mnt = timedelta(minutes=15)
     for dt in date_list:
         ndate_list.append(dt - mnt)
         ndate_list.append(dt)
         ndate_list.append(dt + mnt)
     for dt in ndate_list:
         tp.append(TimePoint(ln, la, dt))
     tides = predict_tide(tp)
     if len(tides) == 0:
         print(
             "No tide height observed from OTPS model within lat/lon range")
         sys.exit()
     print("received from predict tides ", str(datetime.now()))
     # collect in ebb/flow list
     for tt in tides:
         tide_dict[datetime.strptime(
             tt.timepoint.timestamp.isoformat()[0:19],
             "%Y-%m-%dT%H:%M:%S")] = tt.tide_m
     tide_dict = sorted(tide_dict.items(), key=lambda x: x[1])
     tmp_lt = list()
     for k, v in tide_dict.items():
         tmp_lt.append([k, v])
     if self.debug:
         print(str(tmp_lt))
     tmp_lt = [[tmp_lt[i+1][0].strftime("%Y-%m-%d"), 'ph'] \
              if tmp_lt[i][1] < tmp_lt[i+1][1] and tmp_lt[i+2][1] <  tmp_lt[i+1][1]  else \
              [tmp_lt[i+1][0].strftime("%Y-%m-%d"), 'pl'] if tmp_lt[i][1] > tmp_lt[i+1][1] and \
              tmp_lt[i+2][1] >  tmp_lt[i+1][1]  else [tmp_lt[i+1][0].strftime("%Y-%m-%d"),'f'] \
              if tmp_lt[i][1] < tmp_lt[i+2][1] else [tmp_lt[i+1][0].strftime("%Y-%m-%d"),'e'] \
              for i in range(0, len(tmp_lt), 3)]
     return tmp_lt
예제 #4
0
 def extract_otps_range(self, date_list):
     # open the otps lon/lat file
     my_file = "/g/data/u46/users/bxb547/otps/cell_map_lon_lat.csv"
     lon = ''
     lat = ''
     tp = list()
     with open(my_file, 'r') as f:
        reader = csv.reader(f, dialect='excel', delimiter='/')
        for row in reader:
            if self.cell in row:
               lon = row[1]
               lat = row[2]
               break
     for dt in date_list:
         if len(lon) > 0 and len(lat) > 0:
             tp.append(TimePoint(float(lon), float(lat), dt)) 
         else:
             print ("Please provide longitude and latitude of tidal point through a file cell_map_lon_lat.csv")
             return
     tides = predict_tide(tp)
     print ("received from predict tides ", str(datetime.now()))
     date_low = list()
     date_high = list()
     tide_dict = dict()
     for tt in tides:
         # print (tt) 
         # print (datetime.strptime(tt.timepoint.timestamp.isoformat()[0:19], "%Y-%m-%dT%H:%M:%S"), tt.tide_m)
         tide_dict[datetime.strptime(tt.timepoint.timestamp.isoformat()[0:19], "%Y-%m-%dT%H:%M:%S")] = tt.tide_m
     tide_dict = sorted(tide_dict.items(), key=lambda x: x[1])
     lowest = round(float(self.per)*len(tide_dict)/100)
     date_low = tide_dict[:int(lowest)]
     print ("lowest tides list", date_low)
     date_high = tide_dict[-int(len(tide_dict)-lowest):]
     print ("highest tides list", date_high)
     date_low = [dd[0] for dd in date_low]
     date_high = [dd[0] for dd in date_high]
     return date_low, date_high 
예제 #5
0
 def extract_otps_range(self, date_list):
     # open the otps lon/lat file
     tp = list()
     ln = 0
     la = 0
     if self.tide_post:
         ln = eval(self.tide_post)[0]
         la = eval(self.tide_post)[1]
     else:
         print(
             "reading from tidal model file and using Haversine algorithm to extract shortest distance"
         )
         from operator import itemgetter
         # first find centroid of lat lon range
         la = (self.lat[0] + self.lat[1]) / 2
         ln = (self.lon[0] + self.lon[1]) / 2
         rdlist = list()
         fname = "./epoch_tide_post_model.csv"
         try:
             with open(fname, 'rt') as f:
                 reader = csv.reader(f, delimiter=',')
                 for rd in reader:
                     rdlist.append((rd[0], rd[1], rd[2],
                                    self.distance(la, ln, float(rd[1]),
                                                  float(rd[0]))))
             rdlist = sorted(rdlist, key=itemgetter(3))
             print(
                 "Found tide post coordinates,depth and shortest distance",
                 rdlist[0])
             la = float(rdlist[0][1])
             ln = float(rdlist[0][0])
         except IOError as e:
             print("Unable to open file: " + str(fname))
             sys.exit()
     for dt in date_list:
         tp.append(TimePoint(ln, la, dt))
     tides = predict_tide(tp)
     if len(tides) == 0:
         print(
             "No tide height observed from OTPS model within lat/lon range")
         sys.exit()
     print("received from predict tides ", str(datetime.now()))
     date_low = list()
     date_high = list()
     tide_dict = dict()
     for tt in tides:
         tide_dict[datetime.strptime(
             tt.timepoint.timestamp.isoformat()[0:19],
             "%Y-%m-%dT%H:%M:%S")] = tt.tide_m
     tide_dict = sorted(tide_dict.items(), key=lambda x: x[1])
     # lowest = round(float(self.per)*len(tide_dict)/100)
     dr = float(tide_dict[len(tide_dict) - 1][1]) - float(tide_dict[0][1])
     lmr = float(tide_dict[0][1]) + dr * float(
         self.per) * 0.01  # low tide max range
     hlr = float(tide_dict[len(tide_dict) - 1][1]) - dr * float(
         self.per) * 0.01  # low tide max range
     date_low = [x for x in tide_dict if x[1] <= lmr]
     date_high = [x for x in tide_dict if x[1] > hlr]
     # date_high = tide_dict[-int(lowest):]
     print("lowest tides range and number " + str(date_low[0][1]) + "," +
           str(date_low[len(date_low) - 1][1]) + " " + str(len(date_low)))
     print("highest tides range and number " + str(date_high[0][1]) + "," +
           str(date_high[len(date_high) - 1][1]) + " " +
           str(len(date_high)))
     or_date_low = copy.deepcopy(date_low)
     or_date_high = copy.deepcopy(date_high)
     if self.debug:
         print("lowest tides list ",
               [[datetime.strftime(date[0], "%Y-%m-%d"), date[1]]
                for date in date_low])
         print("")
         print("highest tides list",
               [[datetime.strftime(date[0], "%Y-%m-%d"), date[1]]
                for date in date_high])
         print("")
         print("ALL TIDES LIST",
               [[datetime.strftime(date[0], "%Y-%m-%d"), date[1]]
                for date in tide_dict])
     date_low = [dd[0] for dd in date_low]
     date_high = [dd[0] for dd in date_high]
     return date_low, date_high, or_date_low, or_date_high
예제 #6
0
        #################
        # Compute tides #
        #################

        # Group into unique times and locations, create TimePoints and model tides
        grouped_series = points_df.groupby(
            ['tidepoint_lat', 'tidepoint_lon', 'point_timeagg'])
        grouped_series = grouped_series.apply(
            lambda row: TimePoint(lon=row.iloc[0]['tidepoint_lon'],
                                  lat=row.iloc[0]['tidepoint_lat'],
                                  timestamp=row.iloc[0]['point_timeagg']))

        # Convert grouped data to dataframe and compute tides
        grouped_df = grouped_series.to_frame(name='point_tidal')
        grouped_df['point_tidal'] = [
            float(tp.tide_m) for tp in predict_tide(list(grouped_series))
        ]

        # Join back into main dataframe
        points_df = points_df.join(
            grouped_df,
            on=['tidepoint_lat', 'tidepoint_lon', 'point_timeagg'],
            rsuffix="_test")

        # Filter to keep only points located higher than instantaneous tide height and below max overall tide height
        filteredpoints_df = points_df[(points_df.point_z >
                                       (points_df.point_tidal + 0.15))]
        print('Discarding {} points below or at tidal height'.format(
            len(points_df) - len(filteredpoints_df)))

        # Select output columns and export to file