Skip to content

Commit

Permalink
Merge pull request #55 from umr-lops/improve_download
Browse files Browse the repository at this point in the history
add a client for query
  • Loading branch information
agrouaze authored Dec 12, 2023
2 parents a1b1664 + 0db49b7 commit 24938c8
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ cp config.yml localconfig.yml

```bash
queryCDS -h
# or
queryCDS --collection SENTINEL-1 --startdate 20230101T00:00:00 --stopdate 20230105T10:10:10 --mode IW --product SLC --querymode seq --geometry "POINT (-5.02 48.4)"
```

or use the method `cdsodatacli.query.fetch_data()`
Expand Down
88 changes: 85 additions & 3 deletions cdsodatacli/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
Polygon,
MultiPolygon,
)
from shapely import wkt
import geopandas as gpd
import shapely
from shapely.ops import unary_union
Expand All @@ -26,6 +27,83 @@
import warnings

DEFAULT_TOP_ROWS_PER_QUERY = 1000


def query_client():
"""
Returns
-------
result_query (pd.DataFrame): containing columns (footprint, Name, Id, original_query_id, ...)
"""
root = logging.getLogger()
if root.handlers:
for handler in root.handlers:
root.removeHandler(handler)

import argparse

parser = argparse.ArgumentParser(description="query-CDSE-OData")
parser.add_argument("--verbose", action="store_true", default=False)
parser.add_argument(
"--collection",
required=False,
default="SENTINEL-1",
help="SENTINEL-1 or SENTINEL-2 ...",
)
parser.add_argument("--startdate", required=True, help=" YYYYMMDDTHH:MM:SS")
parser.add_argument("--stopdate", required=True, help=" YYYYMMDDTHH:MM:SS")
parser.add_argument("--mode", choices=["EW", "IW", "WV", "SM"])
parser.add_argument("--product", choices=["GRD", "SLC", "RAW", "OCN"])
parser.add_argument("--querymode", choices=["seq", "multi"])
parser.add_argument(
"--geometry",
required=False,
default=None,
help=" [optional, default=None -> global query] example: POINT (-5.02 48.4) or POLYGON ((-12 35, 15 35, 15 58, -12 58, -12 35))",
)
args = parser.parse_args()
fmt = "%(asctime)s %(levelname)s %(filename)s(%(lineno)d) %(message)s"
if args.verbose:
logging.basicConfig(
level=logging.DEBUG, format=fmt, datefmt="%d/%m/%Y %H:%M:%S", force=True
)
else:
logging.basicConfig(
level=logging.INFO, format=fmt, datefmt="%d/%m/%Y %H:%M:%S", force=True
)
t0 = time.time()
sta = datetime.datetime.strptime(args.startdate, "%Y%m%dT%H:%M:%S")
sto = datetime.datetime.strptime(args.stopdate, "%Y%m%dT%H:%M:%S")
gdf = gpd.GeoDataFrame(
{
"start_datetime": [sta],
"end_datetime": [sto],
"geometry": [wkt.loads(args.geometry)],
"collection": [args.collection],
"name": [None],
"sensormode": [args.mode],
"producttype": [args.product],
"Attributes": [None],
}
)
result_query = fetch_data(
gdf,
date=None,
dtime=None,
timedelta_slice=datetime.timedelta(days=14),
start_datetime=None,
end_datetime=None,
min_sea_percent=None,
fig=None,
top=None,
cache_dir=None,
mode=args.querymode,
)
logging.info('time to query : %1.1f sec',time.time()-t0)
return result_query


def fetch_data(
gdf,
date=None,
Expand Down Expand Up @@ -219,7 +297,7 @@ def normalize_gdf(
for date_col in norm_gdf.select_dtypes(include=["datetime64"]).columns:
try:
norm_gdf[date_col] = norm_gdf[date_col].dt.tz_localize("UTC")
logging.debug('norm_gdf[date_col] %s',type(norm_gdf[date_col].iloc[0]))
logging.debug("norm_gdf[date_col] %s", type(norm_gdf[date_col].iloc[0]))
# logger.warning("Assuming UTC date on col %s" % date_col)
except TypeError:
# already localized
Expand Down Expand Up @@ -444,8 +522,12 @@ def fetch_one_url(url, cpt, index, cache_dir):
# collected_data_x.append(collected_data)
cpt["product_proposed_by_CDS"] += len(collected_data["Name"])
collected_data["id_original_query"] = index
if len(collected_data)==DEFAULT_TOP_ROWS_PER_QUERY:
logging.warning("%i products found in a single CDSE OData query (maximum is %s): make sure the timedelta_slice parameters is small enough to avoid truncated results",len(collected_data),DEFAULT_TOP_ROWS_PER_QUERY)
if len(collected_data) == DEFAULT_TOP_ROWS_PER_QUERY:
logging.warning(
"%i products found in a single CDSE OData query (maximum is %s): make sure the timedelta_slice parameters is small enough to avoid truncated results",
len(collected_data),
DEFAULT_TOP_ROWS_PER_QUERY,
)
if pd.isna(collected_data["Name"]).any():
raise Exception("Name field contains NaN")
cpt["answer_append"] += 1
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dependencies = [
]
dynamic = ["version"]
[project.scripts]
queryCDS = "cdsodatacli.__main__:app"
queryCDS = "cdsodatacli.query:query_client"
downloadFromCDS = "cdsodatacli.download:main"
#[tool.poetry.dependencies]
#python = "^3.9"
Expand Down

0 comments on commit 24938c8

Please sign in to comment.