Skip to content

Commit

Permalink
style: Updating syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
castelao committed Oct 6, 2023
1 parent c256469 commit 2a50bff
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 76 deletions.
9 changes: 4 additions & 5 deletions OceanColor/backend/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def __setitem__(self, index, value):
raise NotImplementedError("Missing __setitem__ for this Backend")


class FileSystem(object):
class FileSystem:
"""Backend for OceanColorDB based on files and directories
A file system backend for OceanColorDB to save the data files in
Expand Down Expand Up @@ -197,8 +197,7 @@ def __getitem__(self, index):
return ds

def __setitem__(self, index, ds):
"""Saves Dataset ds identified by index
"""
"""Saves Dataset ds identified by index"""
if not isinstance(ds, xr.Dataset):
self.logger.warn("Trying to save a non xr.Dataset object")
raise ValueError
Expand All @@ -216,7 +215,7 @@ def path(self, product_name: str):
return p.replace(".nc", ".zarr")


class Filename(object):
class Filename:
"""Parse implicit information on NASA's filename
NASA's data filename, and granules, follows a logical standard that can be
Expand Down Expand Up @@ -338,7 +337,7 @@ class InMemory(BaseStorage):

__data = OrderedDict()

def __init__(self, quota: int = 5 * 1024 ** 3):
def __init__(self, quota: int = 5 * 1024**3):
"""Initialize an InMemory object
Parameters
Expand Down
32 changes: 20 additions & 12 deletions OceanColor/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import logging
import os
from typing import Any, Dict, Optional, Sequence
from typing import Any, Dict, Optional
from collections.abc import Sequence

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -35,7 +36,7 @@ def ds_attrs(ds):
return output


class Catalog(object):
class Catalog:
"""
ToDo
Expand All @@ -49,10 +50,12 @@ class Catalog(object):
"""

def __init__(self, dbfilename):
self.store = pd.HDFStore(dbfilename, mode="a", complevel=9, fletcher32=True)
self.store = pd.HDFStore(
dbfilename, mode="a", complevel=9, fletcher32=True
)

def __getitem__(self, product_name):
record = self.store.select("catalog", "index == '{}'".format(product_name))
record = self.store.select("catalog", f"index == '{product_name}'")
if record.size == 0:
raise KeyError

Expand All @@ -76,7 +79,9 @@ def __setitem__(self, key, value):
self.store.append("catalog", value, format="t", data_columns=True)

def __del__(self):
module_logger.debug("Closing Catalog's storage: {}".format(self.store.filename))
module_logger.debug(
f"Closing Catalog's storage: {self.store.filename}"
)
# self.store.flush()
self.store.close()

Expand All @@ -88,18 +93,22 @@ def record(self, ds):
assert attrs["product_name"] not in self, (
"There is a record in the database for %s" % attrs["filename"]
)
module_logger.debug("New record: {}".format(attrs))
module_logger.debug(f"New record: {attrs}")
attrs = pd.DataFrame([attrs])
attrs = attrs.set_index("product_name")
# if ('catalog' in self.store):
# tmp = tmp.set_index(tmp.index + self.store.catalog.index.max() + 1)
self.store.append(
"catalog", attrs, format="t", data_columns=True, min_itemsize={"values": 42}
"catalog",
attrs,
format="t",
data_columns=True,
min_itemsize={"values": 42},
)

def bloom_filter(
self,
track: Sequence[Dict],
track: Sequence[dict],
sensor: Optional[Any] = None,
dtype: Optional[Any] = None,
dt_tol: Optional[Any] = None,
Expand All @@ -113,12 +122,11 @@ def bloom_filter(
cond = []
cond.append("time_coverage_end >= %r" % (track.time.min() - dt_tol))
cond.append("time_coverage_start <= %r" % (track.time.max() + dt_tol))
cond.append("geospatial_lat_max > {}".format(track.lat.min()))
cond.append("geospatial_lat_min > {}".format(track.lat.max()))
cond.append(f"geospatial_lat_max > {track.lat.min()}")
cond.append(f"geospatial_lat_min > {track.lat.max()}")
cond.append(
"(geospatial_lon_min <= {} & geospatial_lon_max >= {}) or (geospatial_lon_max < 0 & geospatial_lon_min > 0)".format(
track.lon.max(), track.lon.min()
)
)
for f in self.store.select("catalog", where=cond).index:
yield f
yield from self.store.select("catalog", where=cond).index
53 changes: 35 additions & 18 deletions OceanColor/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,50 @@
# At this point it's just a Proof of concept
# OceanColor InRange --username=myUser --password=myPassword 2019-05-21,15,-38


@click.group()
def main():
"""Console script for OceanColor."""
pass


@main.command(name="InRange")
@click.option('--username', required=True,
help="Username on NASA's EarthData System")
@click.option('--password', required=True,
help="Password on NASA's EarthData System")
@click.option('--sensor', type=click.Choice(['aqua', 'terra']),
default='aqua')
@click.option('--data-type', 'dtype', type=click.Choice(['L2', 'L3m']),
default='L3m')
@click.option('--time-tolerance', 'dt_tol', type=click.INT , default=12,
help='Time difference [hours] tolerance to matchup')
@click.option('--distance-tolerance', 'dL_tol', type=float, default=10e3,
help='Distance difference [m] tolerance to matchup')
@click.argument('track', required=True)
@click.option(
"--username", required=True, help="Username on NASA's EarthData System"
)
@click.option(
"--password", required=True, help="Password on NASA's EarthData System"
)
@click.option("--sensor", type=click.Choice(["aqua", "terra"]), default="aqua")
@click.option(
"--data-type", "dtype", type=click.Choice(["L2", "L3m"]), default="L3m"
)
@click.option(
"--time-tolerance",
"dt_tol",
type=click.INT,
default=12,
help="Time difference [hours] tolerance to matchup",
)
@click.option(
"--distance-tolerance",
"dL_tol",
type=float,
default=10e3,
help="Distance difference [m] tolerance to matchup",
)
@click.argument("track", required=True)
def cli_inrange(username, password, sensor, dtype, dt_tol, dL_tol, track):
time, lat, lon = track.split(',')
track = pd.DataFrame({"time": [np.datetime64(time)],
"lat": [float(lat)],
"lon": [float(lon)]})
time, lat, lon = track.split(",")
track = pd.DataFrame(
{
"time": [np.datetime64(time)],
"lat": [float(lat)],
"lon": [float(lon)],
}
)

dt_tol = np.timedelta64(dt_tol, 'h')
dt_tol = np.timedelta64(dt_tol, "h")
matchup = InRange(username, password, npes=3)
matchup.search(track, sensor, dtype, dt_tol, dL_tol)
for m in matchup:
Expand Down
2 changes: 1 addition & 1 deletion OceanColor/cmr.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def granules_search(
granules = api.downloadable().get()
print([g["producer_granule_id"] for g in granules])
for granule in api.get():
yield granule['producer_granule_id']
yield granule["producer_granule_id"]


def search_criteria(**kwargs):
Expand Down
16 changes: 10 additions & 6 deletions OceanColor/gsfc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

import json
import logging
from typing import Any, Dict, Optional, Sequence
from typing import Any, Dict, Optional
from collections.abc import Sequence
import urllib

import aiohttp
Expand Down Expand Up @@ -76,7 +77,9 @@ def oceandata_file_search(
if (edate - sdate) > block:
for start in np.arange(sdate, edate, block):
end = start + block - np.timedelta64(1, "D")
filenames = oceandata_file_search(sensor, dtype, start, end, search)
filenames = oceandata_file_search(
sensor, dtype, start, end, search
)
yield from filenames
return

Expand Down Expand Up @@ -178,7 +181,7 @@ def search_criteria(**kwargs):


def bloom_filter(
track: Sequence[Dict],
track: Sequence[dict],
sensor: [Sequence[str], str],
dtype: str,
dt_tol: Optional[Any] = None,
Expand Down Expand Up @@ -214,8 +217,7 @@ def bloom_filter(

search = search_criteria(sensor=sensor, dtype=dtype)
filenames = oceandata_file_search(sensor, dtype, sdate, edate, search)
for f in filenames:
yield f
yield from filenames


def read_remote_file(filename, username, password):
Expand All @@ -227,7 +229,9 @@ def read_remote_file(filename, username, password):
url_base = "https://oceandata.sci.gsfc.nasa.gov/ob/getfile/"
url = requests.compat.urljoin(url_base, filename)

fs = fsspec.filesystem('https', client_kwargs={'auth': aiohttp.BasicAuth(username, password)})
fs = fsspec.filesystem(
"https", client_kwargs={"auth": aiohttp.BasicAuth(username, password)}
)
f = fs.open(url)
content = f.read()
return content
Loading

0 comments on commit 2a50bff

Please sign in to comment.