Skip to content

Commit 0713639

Browse files
Merge pull request #65 from developmentseed/feat/file-output-flag
Add option to output to file
2 parents 05dfc64 + 47df823 commit 0713639

3 files changed

Lines changed: 56 additions & 12 deletions

File tree

README.md

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,55 @@ When started, it does the following:
77
- Creates a STAC item with assets. The assets `href` is based on the given URL.
88
- Adds the STAC item to a STAC catalog. If an item with the same id already exists, it will be updated.
99

10+
## Installation
11+
12+
Install the package using pip:
13+
14+
```bash
15+
# Install from source
16+
pip install .
17+
18+
# Or install in editable mode for development
19+
pip install -e .
20+
```
21+
22+
After installation, the `eopf-stac` command will be available in your environment.
23+
1024
## Usage
1125

1226
The EOPF product must be referenced by an URL which must be provided as a command-line argument. Only `http(s)://`, `s3://` and `file://` URLs are supported. For debugging, a combination of the `--dry-run` and `--debug` option can be used to see the created STAC item in the logs without inserting it into a catalog.
1327

1428
```bash
15-
$ python src/eopf_stac/main.py --help
16-
usage: eopf-stac.py [-h] [--dry-run] [--debug] URL
29+
$ eopf-stac --help
30+
usage: eopf-stac.py [-h] [--source-uri SOURCE_URI] [--dry-run] [--output-file OUTPUT_FILE] [--debug] URL
1731

1832
positional arguments:
19-
URL Local file path or HTTP/S3 URL to the EOPF product.
33+
URL Local file path or URL to the EOPF product
2034

2135
options:
22-
-h, --help show this help message and exit
23-
--source-uri SOURCE_URI Reference to the original product which was input for the EOPF conversion
24-
--dry-run Create STAC item only. Do not insert it into the catalog
25-
--debug Enable verbose output
36+
-h, --help show this help message and exit
37+
--source-uri SOURCE_URI
38+
Reference to the original product which was input for the EOPF conversion
39+
--dry-run Create STAC item without trying to insert it into the catalog
40+
--output-file OUTPUT_FILE
41+
Save the STAC item as JSON to the specified file path
42+
--debug Enable verbose output
43+
```
44+
45+
Example usage:
46+
47+
```bash
48+
# Register STAC item to catalog
49+
eopf-stac s3://path/to/eopf.zarr
50+
51+
# Save STAC item to local file instead of registering
52+
eopf-stac --output-file item.json s3://path/to/eopf.zarr
53+
54+
# Dry run with debug output (only logs, doesn't save or register)
55+
eopf-stac --dry-run --debug s3://path/to/eopf.zarr
56+
57+
# With source URI
58+
eopf-stac --source-uri s3://original/product.nc s3://path/to/eopf.zarr
2659
```
2760

2861
## Settings
@@ -34,7 +67,7 @@ Additional settings need to be provided through the following environment variab
3467
| AWS_ACCESS_KEY_ID | If an s3 URL is provided, the access key for the object storage where the EOPF product is stored. | None |
3568
| AWS_SECRET_ACCESS_KEY | If an s3 URL is provided, the secret for the access key. | None |
3669
| S3_ENDPOINT_URL | If an s3 URL is provided, the endpoint URL of the object storage | None |
37-
| STAC_API_URL | The URL of the STAC catalog to register the created STAC item | None |
70+
| STAC_API_URL | The URL of the STAC catalog to register the created STAC item. Not required if `--output-file` is used. | None |
3871
| STAC_INGEST_USER | The username to access the transaction endpoints of the STAC API with HTTP Basic Auth | None |
3972
| STAC_INGEST_PASS | The password to access the transaction endpoints of the STAC API with HTTP Basic Auth | None |
4073

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ dependencies = [
2828
"netCDF4 == 1.7.2",
2929
"footprint-facility"
3030
]
31+
[project.scripts]
32+
eopf-stac = "eopf_stac.main:main"
33+
3134
[project.optional-dependencies]
3235
dev = [
3336
"ruff"

src/eopf_stac/main.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import logging
44
import os
55
from sys import exit
6+
from typing import Optional
67

78
from eopf_stac.io import create_item, read_metadata, register_item
89

@@ -25,7 +26,7 @@ def configure_logging(level: int):
2526
)
2627

2728

28-
def validate_env(url: str, dry_run: bool, env: dict):
29+
def validate_env(url: str, dry_run: bool, output_file: Optional[str], env):
2930
if url.startswith("s3://"):
3031
# if s3 url is provided, the credentials are required?
3132
missing_vars = []
@@ -41,7 +42,7 @@ def validate_env(url: str, dry_run: bool, env: dict):
4142
if len(missing_vars) > 0:
4243
raise ValueError(f"The following enviroment variables are missing: {missing_vars}")
4344

44-
if not dry_run:
45+
if not dry_run and not output_file:
4546
if ENV_STAC_API_URL not in env:
4647
raise ValueError(f"The enviroment variable {ENV_STAC_API_URL} is missing")
4748

@@ -62,6 +63,7 @@ def main():
6263
parser.add_argument(
6364
"--dry-run", help="Create STAC item without trying to insert it into the catalog", action="store_true"
6465
)
66+
parser.add_argument("--output-file", help="Save the STAC item as JSON to the specified file path", type=str)
6567
parser.add_argument("--debug", help="Enable verbose output", action="store_true")
6668
args = parser.parse_args()
6769

@@ -71,7 +73,7 @@ def main():
7173
configure_logging(logging.INFO)
7274

7375
try:
74-
validate_env(args.URL, args.dry_run, env=os.environ)
76+
validate_env(args.URL, args.dry_run, args.output_file, os.environ)
7577

7678
logger.debug("Opening metadata file ...")
7779
metadata = read_metadata(args.URL)
@@ -81,7 +83,13 @@ def main():
8183
logger.debug(json.dumps(item.to_dict(), indent=4))
8284

8385
if not args.dry_run:
84-
item = register_item(item=item, stac_api_url=os.environ[ENV_STAC_API_URL])
86+
if args.output_file:
87+
logger.info(f"Writing STAC item to {args.output_file}")
88+
with open(args.output_file, "w") as f:
89+
json.dump(item.to_dict(), f, indent=4)
90+
else:
91+
logger.info(f"Registering STAC item to {os.environ[ENV_STAC_API_URL]}")
92+
item = register_item(item=item, stac_api_url=os.environ[ENV_STAC_API_URL])
8593

8694
except Exception as e:
8795
logger.error(str(e))

0 commit comments

Comments
 (0)