def test_lookup_asdataframe(): """lookup - asdataframe""" r = xy.lookup(test_points, asdataframe=True) assert r.shape[0] == len(test_points) assert r.shape[1] >= 4 assert 'DataFrame' in str(type(r)) assert isinstance(r, pd.DataFrame) r = xy.lookup(test_points, shoredistance=True, grids=False, areas=False, asdataframe=True) assert r.shape[0] == len(test_points) assert r.shape[1] == 1 r = xy.lookup(test_points, shoredistance=False, grids=True, areas=False, asdataframe=True) assert r.shape[0] == len(test_points) assert r.shape[1] >= 3 r = xy.lookup(test_points, shoredistance=False, grids=False, areas=True, asdataframe=True) assert r.shape[0] == len(test_points) assert r.shape[1] == 1 r = xy.lookup(test_points, shoredistance=True, grids=True, areas=True, asdataframe=True) assert r.shape[0] == len(test_points) assert r.shape[1] >= 5
def test_lookup_duplicate_points(): """Lookup duplicate points""" points = test_points * 10000 r = xy.lookup(points, shoredistance=True, grids=False, areas=False, asdataframe=False) assert len(r) == len(points) r = xy.lookup(points, shoredistance=True, grids=False, areas=False, asdataframe=True) assert r.shape[0] == len(points) assert r.shape[1] >= 1
def test_lookup_na_points(): """Lookup NA points with duplicates""" import math points = [[0, 1], [float('nan'), 3], [4, float('nan')], [5, 6]] * 3 r = xy.lookup(points, shoredistance=True, grids=False, areas=False, asdataframe=False) assert len(r) == len(points) for i in [1, 2, 5, 6, 9, 10]: assert r[i] == {} r = xy.lookup(points, shoredistance=True, grids=True, areas=True, asdataframe=True) assert r.shape[0] == len(points) assert r.shape[1] >= 1 for i in [1, 2, 5, 6, 9, 10]: assert math.isnan(r["shoredistance"][i])
def test_lookup_na_points(): """Lookup NA points""" import math points = [[0, 1], [float('nan'), 3], [4, float('nan')], [5, 6]] r = xy.lookup(points, shoredistance=True, grids=False, areas=False, asdataframe=False) assert len(r) == len(points) assert r[1] == {} assert r[2] == {} r = xy.lookup(points, shoredistance=True, grids=True, areas=True, asdataframe=True) assert r.shape[0] == len(points) assert r.shape[1] >= 1 assert math.isnan(r["shoredistance"][1]) assert math.isnan(r["shoredistance"][2])
def test_lookup_many_points(): """Lookup many points""" import random random.seed(42) points = [[random.uniform(-180, 180), random.uniform(-90, 90)] for _ in range(50000)] r = xy.lookup(points, shoredistance=True, grids=True, areas=True, asdataframe=True) assert r.shape[0] == len(points) assert r.shape[1] >= 5
def environmental(points, duplicate_indices, mad_coef, iqr_coef, qcstats=None, return_values=False): if mad_coef is None: mad_coef = MAD_COEF if iqr_coef is None: iqr_coef = IQR_COEF env = xy.lookup(points, shoredistance=False, grids=True, areas=False, asdataframe=True) qc = {} for grid in ['bathymetry', 'sssalinity', 'sstemperature']: values = env.get(grid, np.full(len(points), np.nan)) if qcstats is None: median, mad, q1, q3 = stats.get_values_stats(values) else: median, mad, q1, q3 = qcstats[grid] qc[grid] = _values_qc(values, median, mad, q1, q3, mad_coef, iqr_coef, duplicate_indices, return_values) return qc
def do_xylookup(results): output = [None] * len(results) indices = [] coordinates = [] for i in range(len(results)): result = results[i] if "decimalLongitude" in result[ "annotations"] and "decimalLatitude" in result["annotations"]: indices.append(i) coordinates.append([ result["annotations"]["decimalLongitude"], result["annotations"]["decimalLatitude"] ]) if len(coordinates) > 0: xy = pyxylookup.lookup(coordinates, shoredistance=True, grids=True, areas=True) for i in range(len(indices)): output[indices[i]] = xy[i] return output
def do_xylookup(records): """ derived from equivalent in obis-qc - takes a list of records already QCd for LAT - LON presence and validity - QC field must be present :param - records IMPORTANT - with VALID LAT, LON :returns - a list of result records as retrieved from pyxylookup for each point """ import pyxylookup as pxy # and then use pyxylookup.lookup output = [None] * len(records) indices = [] coordinates = [] performLookup = False for i in range(len(records)): record = records[i] # The record has been already checked LAT LON validity, but verify anyway... if "decimalLongitude" in record and \ "decimalLatitude" in record and \ "qc" in record and \ record["qc"] & qc_flags.QCFlag.GEO_LAT_LON_VALID.bitmask and \ record["qc"] & qc_flags.QCFlag.GEO_LAT_LON_PRESENT.bitmask: indices.append(i) lon = check_float(record["decimalLongitude"])["float"] lat = check_float(record["decimalLatitude"])["float"] coordinates.append([lon, lat]) if lat is not None and lon is not None: performLookup = True if performLookup: xy = pxy.lookup(coordinates, shoredistance=True, grids=True, areas=True) for i in range(len(indices)): output[indices[i]] = xy[i] return output
def test_lookup_wrong_points_error(points): xy.lookup(points)
def check_lookup_all_false_error(asdataframe): xy.lookup(test_points, shoredistance=False, grids=False, areas=False, asdataframe=asdataframe)
def test_lookup_points_tuples_list(): """lookup - points tuples list""" test_points_tuples = [(4, 5), (6, 7)] r = xy.lookup(test_points_tuples) assert len(r) == 2
def test_lookup_pandas_points(): """lookup - numpy points""" points = np.asarray(test_points) r = xy.lookup(points) assert len(r) == len(test_points)
def test_lookup_points_array(): """lookup - points array""" r = xy.lookup(test_points) assert len(r) == len(test_points)