Skip to content

Commit

Permalink
Add option to json export to filter for certain variables
Browse files Browse the repository at this point in the history
  • Loading branch information
wpreimes committed Feb 9, 2024
1 parent 873f4cf commit dc2cfa8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
14 changes: 12 additions & 2 deletions src/ismn/cli.py
Expand Up @@ -37,7 +37,7 @@ def collect_metadata(data_path, meta_path, parallel):

@click.command("export_geojson", short_help="Export ISMN sensors to geojson.")
@click.argument('data_path', type=click.STRING)
@click.option('--file_out',
@click.option('--file_out', '-o',
type=click.STRING, default=None,
help="Path to the json file that should be created. "
"If the file already exists it will be overwritten. "
Expand All @@ -49,7 +49,11 @@ def collect_metadata(data_path, meta_path, parallel):
"network, station, sensor, depth, timerange. \n "
"Or any sensor properties (also custom ones) that have a "
"value.")
def export_geojson(data_path, file_out, field):
@click.option('--variable', '-var', multiple=True,
help="To include only the metadata for a certain variable (e.g."
"soil_moisture) pass the name here. This option is allowed"
"multiple times.")
def export_geojson(data_path, file_out, field, variable):
"""
Calls
Command line program to initialise ISMN metadata collection. THIS WILL
Expand All @@ -73,6 +77,8 @@ def export_geojson(data_path, file_out, field):
file_out = os.path.join(ds.root.root_dir, 'ismn_sensors.json')
os.makedirs(os.path.dirname(file_out), exist_ok=True)
print(f"Exporting geojson to: {file_out}")
print(f"Include fields: {field}")
print(f"Filter for variables: {variable}")

kwargs = {}
field = [f.lower() for f in field]
Expand All @@ -84,6 +90,10 @@ def export_geojson(data_path, file_out, field):
kwargs[opt] = False

kwargs['extra_props'] = field if len(field) > 0 else None

if len(variable) > 0:
kwargs['filter_kwargs'] = {'variable': variable}

ds.collection.export_geojson(file_out, **kwargs)

@click.group(short_help="ISMN Command Line Programs.")
Expand Down
9 changes: 6 additions & 3 deletions src/ismn/components.py
Expand Up @@ -826,7 +826,7 @@ def export_citations(self, out_file=None):

def export_geojson(self, path, network=True, station=True, sensor=False,
depth=True, timerange=True, extra_props=None,
**filter_kwargs):
filter_kwargs=None):
"""
Filter sensors in collection and create geojson file containing all
features.
Expand All @@ -850,8 +850,9 @@ def export_geojson(self, path, network=True, station=True, sensor=False,
geojson file
By default only depth_from and depth_to are included
e.g. ['variable', 'frm_class'] etc.
filter_kwargs:
Keyword arguments to filter sensors in collection
filter_kwargs: dict, optional (default: None)
Keyword arguments to filter sensors in collection before extracting
metadata.
see :func:`ismn.components.Sensor.eval`
"""
extra_props = extra_props or []
Expand All @@ -860,6 +861,8 @@ def export_geojson(self, path, network=True, station=True, sensor=False,
"features": [],
}

filter_kwargs = filter_kwargs or dict()

for nw, stat, sens in self.iter_sensors(**filter_kwargs):
feature = {
"type": "Feature",
Expand Down
6 changes: 5 additions & 1 deletion tests/test_cli.py
Expand Up @@ -28,7 +28,9 @@ def test_cli_export_geojson():
os.path.join(tempdir, "test.geojson"),
"-f", "network",
'-f', "timerange",
"-f", "lc_2010"])
"-f", "lc_2010",
"-f", "variable",
"-var", "soil_moisture"])
assert result.exit_code == 0
assert os.path.isfile(os.path.join(tempdir, "test.geojson"))
with open(os.path.join(tempdir, "test.geojson"), "r") as f:
Expand All @@ -37,3 +39,5 @@ def test_cli_export_geojson():
assert "timerange_from" in content[0]
assert "timerange_to" in content[0]
assert "lc_2010" in content[0]
assert 'soil_moisture' in content[0]
assert "precipitation" not in content[0]

0 comments on commit dc2cfa8

Please sign in to comment.