From 7f94ce84fc36874647c8529e90fd74e8d1438050 Mon Sep 17 00:00:00 2001 From: Brett Date: Mon, 25 Nov 2024 14:37:14 -0500 Subject: [PATCH 1/7] fix return code for asn_from_list and skycell_asn, remove unused scripts --- pyproject.toml | 6 ++---- romancal/associations/asn_from_list.py | 4 ++++ romancal/associations/skycell_asn.py | 7 +++++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 892f51edf..4eeb29b7f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -88,11 +88,9 @@ webbpsf = "pytest_plugin.webbpsf_plugin" [project.scripts] roman_static_preview = "romancal.scripts.static_preview:command" -schema_editor = "romancal.scripts.schema_editor:main" -schemadoc = "romancal.scripts.schemadoc:main" verify_install_requires = "romancal.scripts.verify_install_requires:main" -asn_from_list = "romancal.associations.asn_from_list:Main" -skycell_asn = "romancal.associations.skycell_asn:Main" +asn_from_list = "romancal.associations.asn_from_list:main" +skycell_asn = "romancal.associations.skycell_asn:main" [build-system] requires = [ diff --git a/romancal/associations/asn_from_list.py b/romancal/associations/asn_from_list.py index 03c877c71..12f6dbeb0 100644 --- a/romancal/associations/asn_from_list.py +++ b/romancal/associations/asn_from_list.py @@ -145,3 +145,7 @@ def __init__(self, args=None): ) _, serialized = asn.dump(format=parsed.format) outfile.write(serialized) + + +def main(): + Main() diff --git a/romancal/associations/skycell_asn.py b/romancal/associations/skycell_asn.py index b323d2b06..845048976 100644 --- a/romancal/associations/skycell_asn.py +++ b/romancal/associations/skycell_asn.py @@ -190,6 +190,9 @@ def __init__(self, args=None): skycell_asn(self) -if __name__ == "__main__": - +def main(): Main() + + +if __name__ == "__main__": + main() From 0bb8ddca4a7a6a932fc91cf75dc477bf0830c5c0 Mon Sep 17 00:00:00 2001 From: Brett Date: Mon, 25 Nov 2024 14:46:39 -0500 Subject: [PATCH 2/7] rename main to _cli --- pyproject.toml | 4 ++-- romancal/associations/asn_from_list.py | 2 +- romancal/associations/skycell_asn.py | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 4eeb29b7f..071679d24 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -89,8 +89,8 @@ webbpsf = "pytest_plugin.webbpsf_plugin" [project.scripts] roman_static_preview = "romancal.scripts.static_preview:command" verify_install_requires = "romancal.scripts.verify_install_requires:main" -asn_from_list = "romancal.associations.asn_from_list:main" -skycell_asn = "romancal.associations.skycell_asn:main" +asn_from_list = "romancal.associations.asn_from_list:_cli" +skycell_asn = "romancal.associations.skycell_asn:_cli" [build-system] requires = [ diff --git a/romancal/associations/asn_from_list.py b/romancal/associations/asn_from_list.py index 12f6dbeb0..804b60576 100644 --- a/romancal/associations/asn_from_list.py +++ b/romancal/associations/asn_from_list.py @@ -147,5 +147,5 @@ def __init__(self, args=None): outfile.write(serialized) -def main(): +def _cli(): Main() diff --git a/romancal/associations/skycell_asn.py b/romancal/associations/skycell_asn.py index 845048976..b3b922d27 100644 --- a/romancal/associations/skycell_asn.py +++ b/romancal/associations/skycell_asn.py @@ -190,9 +190,9 @@ def __init__(self, args=None): skycell_asn(self) -def main(): +def _cli(): Main() if __name__ == "__main__": - main() + _cli() From e65d66c283fe1749a6fb423aa00404f200602eb4 Mon Sep 17 00:00:00 2001 From: Brett Date: Mon, 25 Nov 2024 14:58:41 -0500 Subject: [PATCH 3/7] drop Main from asn_from_list and skycell_asn --- docs/roman/associations/asn_from_list.rst | 3 +- docs/roman/associations/skycell_asn.rst | 3 +- romancal/associations/asn_from_list.py | 181 +++++++++--------- romancal/associations/skycell_asn.py | 167 ++++++++-------- .../associations/tests/test_asn_from_list.py | 11 +- .../associations/tests/test_skycell_asn.py | 6 +- 6 files changed, 178 insertions(+), 193 deletions(-) diff --git a/docs/roman/associations/asn_from_list.rst b/docs/roman/associations/asn_from_list.rst index 9f5c54013..3b848a7f5 100644 --- a/docs/roman/associations/asn_from_list.rst +++ b/docs/roman/associations/asn_from_list.rst @@ -4,8 +4,7 @@ asn_from_list ============= Create an association using either the command line tool -``asn_from_list`` or through the Python API using either -:class:`romancal.associations.asn_from_list.Main` or +``asn_from_list`` or through the Python API using :func:`romancal.associations.asn_from_list.asn_from_list` diff --git a/docs/roman/associations/skycell_asn.rst b/docs/roman/associations/skycell_asn.rst index ee8490c33..d9118d892 100644 --- a/docs/roman/associations/skycell_asn.rst +++ b/docs/roman/associations/skycell_asn.rst @@ -4,8 +4,7 @@ skycell_asn =========== Create an association using either the command line tool -``skycell_asn`` or through the Python API using either -:class:`romancal.associations.skycell_asn.Main` or +``skycell_asn`` or through the Python API using :func:`romancal.associations.skycellasn.skycell_asn` diff --git a/romancal/associations/asn_from_list.py b/romancal/associations/asn_from_list.py index 804b60576..de946ff95 100644 --- a/romancal/associations/asn_from_list.py +++ b/romancal/associations/asn_from_list.py @@ -43,7 +43,7 @@ def asn_from_list(items, rule=DMS_ELPP_Base, **kwargs): return asn -class Main: +def _cli(args=None): """Command-line interface for list_to_asn Parameters @@ -55,97 +55,90 @@ class Main: with the similar structure as `sys.argv` """ - def __init__(self, args=None): - if args is None: - args = sys.argv[1:] - if isinstance(args, str): - args = args.split(" ") - - parser = argparse.ArgumentParser( - description="Create an association from a list of files", - usage="asn_from_list -o mosaic_asn.json\n--product-name my_mosaic *.fits", - ) - - parser.add_argument( - "-o", - "--output-file", - type=str, - required=True, - help="File to write association to", - ) - - parser.add_argument( - "-f", - "--format", - type=str, - default="json", - help='Format of the association files. Default: "%(default)s"', - ) - - parser.add_argument( - "--product-name", - type=str, - help="The product name when creating a Level 3 association", - ) - - parser.add_argument( - "-r", - "--rule", - type=str, - default="DMS_ELPP_Base", - help=( - 'The rule to base the association structure on. Default: "%(default)s"' - ), - ) - parser.add_argument( - "--ruledefs", - action="append", - help=( - "Association rules definition file(s) If not specified, the default" - " rules will be searched." - ), + if args is None: + args = sys.argv[1:] + if isinstance(args, str): + args = args.split(" ") + + parser = argparse.ArgumentParser( + description="Create an association from a list of files", + usage="asn_from_list -o mosaic_asn.json\n--product-name my_mosaic *.fits", + ) + + parser.add_argument( + "-o", + "--output-file", + type=str, + required=True, + help="File to write association to", + ) + + parser.add_argument( + "-f", + "--format", + type=str, + default="json", + help='Format of the association files. Default: "%(default)s"', + ) + + parser.add_argument( + "--product-name", + type=str, + help="The product name when creating a Level 3 association", + ) + + parser.add_argument( + "-r", + "--rule", + type=str, + default="DMS_ELPP_Base", + help=('The rule to base the association structure on. Default: "%(default)s"'), + ) + parser.add_argument( + "--ruledefs", + action="append", + help=( + "Association rules definition file(s) If not specified, the default" + " rules will be searched." + ), + ) + parser.add_argument( + "-i", + "--id", + type=str, + default="o999", + help='The association candidate id to use. Default: "%(default)s"', + dest="acid", + ) + parser.add_argument( + "-t", + "--target", + type=str, + default="None", + help='The target name for the association. Default: "%(default)s"', + dest="target", + ) + + parser.add_argument( + "filelist", + type=str, + nargs="+", + help="File list to include in the association", + ) + + parsed = parser.parse_args(args=args) + print("Parsed args:", parsed) + + # Get the rule + rule = AssociationRegistry(parsed.ruledefs, include_bases=True)[parsed.rule] + + with open(parsed.output_file, "w") as outfile: + asn = asn_from_list( + parsed.filelist, + rule=rule, + product_name=parsed.product_name, + acid=parsed.acid, + target=parsed.target, ) - parser.add_argument( - "-i", - "--id", - type=str, - default="o999", - help='The association candidate id to use. Default: "%(default)s"', - dest="acid", - ) - parser.add_argument( - "-t", - "--target", - type=str, - default="None", - help='The target name for the association. Default: "%(default)s"', - dest="target", - ) - - parser.add_argument( - "filelist", - type=str, - nargs="+", - help="File list to include in the association", - ) - - parsed = parser.parse_args(args=args) - print("Parsed args:", parsed) - - # Get the rule - rule = AssociationRegistry(parsed.ruledefs, include_bases=True)[parsed.rule] - - with open(parsed.output_file, "w") as outfile: - asn = asn_from_list( - parsed.filelist, - rule=rule, - product_name=parsed.product_name, - acid=parsed.acid, - target=parsed.target, - ) - _, serialized = asn.dump(format=parsed.format) - outfile.write(serialized) - - -def _cli(): - Main() + _, serialized = asn.dump(format=parsed.format) + outfile.write(serialized) diff --git a/romancal/associations/skycell_asn.py b/romancal/associations/skycell_asn.py index b3b922d27..9b88712fc 100644 --- a/romancal/associations/skycell_asn.py +++ b/romancal/associations/skycell_asn.py @@ -20,11 +20,11 @@ logger.setLevel("INFO") -def skycell_asn(self): - """Create the associaton from the list""" +def skycell_asn(filelist, output_file_root, product_type, release_product): + """Create the skycell associaton from the list""" all_patches = [] file_list = [] - for file_name in self.parsed.filelist: + for file_name in filelist: cal_file = rdm.open(file_name) filter_id = cal_file.meta.instrument.optical_element.lower() file_patch_list = pm.find_patch_matches(cal_file.meta.wcs) @@ -59,9 +59,8 @@ def skycell_asn(self): ) parsed_visit_id = parse_visitID(member_list[0][1:20]) program_id = parsed_visit_id["Program"] - root_asn_name = self.parsed.output_file_root - product_type = self.parsed.product_type - product_release = self.parsed.release_product + root_asn_name = output_file_root + product_release = release_product sep = "_" product_name_mapping = { @@ -105,7 +104,7 @@ def skycell_asn(self): outfile.write(serialized) -class Main: +def _cli(args=None): """Command-line interface for list_to_asn Parameters @@ -116,83 +115,77 @@ class Main: - `[str, ...]`: A list of strings which create the command line with the similar structure as `sys.argv` """ - - def __init__(self, args=None): - if args is None: - args = sys.argv[1:] - if isinstance(args, str): - args = args.split(" ") - - parser = argparse.ArgumentParser( - description="Create an association from a list of files", - usage="skycell_asn --product-type visit --release-product prompt *_cal.asdf -o r512", - ) - - parser.add_argument( - "-o", - "--output-file-root", - type=str, - required=True, - help="Root string for file to write association to", - ) - - parser.add_argument( - "-f", - "--format", - type=str, - default="json", - help='Format of the association files. Default: "%(default)s"', - ) - - parser.add_argument( - "--product-type", - type=str, - default="visit", - help="The product type when creating the association", - ) - - parser.add_argument( - "--release-product", - type=str, - default="p", - help="The release product when creating the association", - ) - - parser.add_argument( - "-r", - "--rule", - type=str, - default="DMS_ELPP_Base", - help=( - "The rule to base the association structure on." - ' Default: "%(default)s"' - ), - ) - parser.add_argument( - "-i", - "--id", - type=str, - default="o999", - help='The association candidate id to use. Default: "%(default)s"', - dest="acid", - ) - - parser.add_argument( - "filelist", - type=str, - nargs="+", - help="File list to include in the association", - ) - - self.parsed = parser.parse_args(args=args) - logger.info("Command-line arguments: %s", self.parsed) - - skycell_asn(self) - - -def _cli(): - Main() - - -if __name__ == "__main__": - _cli() + if args is None: + args = sys.argv[1:] + if isinstance(args, str): + args = args.split(" ") + + parser = argparse.ArgumentParser( + description="Create an association from a list of files", + usage="skycell_asn --product-type visit --release-product prompt *_cal.asdf -o r512", + ) + + parser.add_argument( + "-o", + "--output-file-root", + type=str, + required=True, + help="Root string for file to write association to", + ) + + parser.add_argument( + "-f", + "--format", + type=str, + default="json", + help='Format of the association files. Default: "%(default)s"', + ) + + parser.add_argument( + "--product-type", + type=str, + default="visit", + help="The product type when creating the association", + ) + + parser.add_argument( + "--release-product", + type=str, + default="p", + help="The release product when creating the association", + ) + + parser.add_argument( + "-r", + "--rule", + type=str, + default="DMS_ELPP_Base", + help=( + "The rule to base the association structure on." ' Default: "%(default)s"' + ), + ) + parser.add_argument( + "-i", + "--id", + type=str, + default="o999", + help='The association candidate id to use. Default: "%(default)s"', + dest="acid", + ) + + parser.add_argument( + "filelist", + type=str, + nargs="+", + help="File list to include in the association", + ) + + parsed = parser.parse_args(args=args) + logger.info("Command-line arguments: %s", parsed) + + skycell_asn( + parsed.filelist, + parsed.output_file_root, + parsed.product_type, + parsed.release_product, + ) diff --git a/romancal/associations/tests/test_asn_from_list.py b/romancal/associations/tests/test_asn_from_list.py index f258e47cf..d71498d93 100644 --- a/romancal/associations/tests/test_asn_from_list.py +++ b/romancal/associations/tests/test_asn_from_list.py @@ -3,7 +3,7 @@ import pytest from romancal.associations import Association, AssociationRegistry, load_asn -from romancal.associations.asn_from_list import Main, asn_from_list +from romancal.associations.asn_from_list import _cli, asn_from_list from romancal.associations.exceptions import AssociationNotValidError @@ -111,11 +111,11 @@ def test_cmdline_fails(): # No arguments with pytest.raises(SystemExit): - Main([]) + _cli([]) # Only the association file argument with pytest.raises(SystemExit): - Main(["-o", "test_asn.json"]) + _cli(["-o", "test_asn.json"]) @pytest.mark.parametrize("format", ["json", "yaml"]) @@ -126,7 +126,7 @@ def test_cmdline_success(format, tmp_path): inlist = ["a", "b", "c"] args = ["-o", str(path), "--product-name", product_name, "--format", format] args = args + inlist - Main(args) + return_code = _cli(args) with path.open() as fp: asn = load_asn(fp, format=format) assert len(asn["products"]) == 1 @@ -134,6 +134,7 @@ def test_cmdline_success(format, tmp_path): members = asn["products"][0]["members"] expnames = [member["expname"] for member in members] assert inlist == expnames + assert not return_code def test_cmdline_change_rules(tmp_path): @@ -150,7 +151,7 @@ def test_cmdline_change_rules(tmp_path): "test", ] args = args + inlist - Main(args) + _cli(args) with path.open() as fp: asn = load_asn(fp, registry=AssociationRegistry(include_bases=True)) # assert inlist == asn['members'] diff --git a/romancal/associations/tests/test_skycell_asn.py b/romancal/associations/tests/test_skycell_asn.py index dadc68cee..a4e0418e5 100644 --- a/romancal/associations/tests/test_skycell_asn.py +++ b/romancal/associations/tests/test_skycell_asn.py @@ -4,7 +4,7 @@ # from romancal.associations import Association, AssociationRegistry, load_asn import romancal.associations.skycell_asn as skycell_asn -from romancal.associations.skycell_asn import Main +from romancal.associations.skycell_asn import _cli def test_cmdline_fails(): @@ -12,11 +12,11 @@ def test_cmdline_fails(): # No arguments with pytest.raises(SystemExit): - Main([]) + _cli([]) # Only the association file argument with pytest.raises(SystemExit): - Main(["-o", "test_asn.json"]) + _cli(["-o", "test_asn.json"]) def test_parse_visitID(): From 443090c52e6e9b7db066707d6d0f35bd8ccc95bd Mon Sep 17 00:00:00 2001 From: Brett Date: Mon, 25 Nov 2024 15:36:59 -0500 Subject: [PATCH 4/7] add changelog --- changes/1538.associations.rst | 1 + changes/1538.scripts.rst | 1 + 2 files changed, 2 insertions(+) create mode 100644 changes/1538.associations.rst create mode 100644 changes/1538.scripts.rst diff --git a/changes/1538.associations.rst b/changes/1538.associations.rst new file mode 100644 index 000000000..da0b9bd5c --- /dev/null +++ b/changes/1538.associations.rst @@ -0,0 +1 @@ +Switch association scripts from using ``Main`` class to ``_cli`` function to fix return code. diff --git a/changes/1538.scripts.rst b/changes/1538.scripts.rst new file mode 100644 index 000000000..b715da8c2 --- /dev/null +++ b/changes/1538.scripts.rst @@ -0,0 +1 @@ +Remove install of missing scripts "schema_editor" and "schemadoc". From e34ae6188c84fc9bd8df85c274314966152351c7 Mon Sep 17 00:00:00 2001 From: Brett Date: Mon, 25 Nov 2024 17:13:57 -0500 Subject: [PATCH 5/7] remove Main use in regtest --- romancal/regtest/test_skycell_generation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/romancal/regtest/test_skycell_generation.py b/romancal/regtest/test_skycell_generation.py index 29d8c2f31..59ac0f163 100644 --- a/romancal/regtest/test_skycell_generation.py +++ b/romancal/regtest/test_skycell_generation.py @@ -21,7 +21,7 @@ def test_skycell_asn_generation(rtdata): rtdata.get_data("WFI/image/r0000101001001001001_0002_wfi01_cal.asdf") rtdata.get_data("WFI/image/r0000101001001001001_0002_wfi10_cal.asdf") - skycell_asn.Main(args) + skycell_asn._cli(args) # skycell associations that should be generated output_files = [ From 7f483469ff0a05e6ec5e2d2bd74ae37e7a1d3ace Mon Sep 17 00:00:00 2001 From: Brett Date: Tue, 3 Dec 2024 17:26:24 -0500 Subject: [PATCH 6/7] remove stray fits mention --- romancal/associations/asn_from_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/romancal/associations/asn_from_list.py b/romancal/associations/asn_from_list.py index de946ff95..38e863dfb 100644 --- a/romancal/associations/asn_from_list.py +++ b/romancal/associations/asn_from_list.py @@ -62,7 +62,7 @@ def _cli(args=None): parser = argparse.ArgumentParser( description="Create an association from a list of files", - usage="asn_from_list -o mosaic_asn.json\n--product-name my_mosaic *.fits", + usage="asn_from_list -o mosaic_asn.json\n--product-name my_mosaic *.asdf", ) parser.add_argument( From 800fd01c50622407faa5cdc7f18d5c81758f31ea Mon Sep 17 00:00:00 2001 From: Brett Date: Tue, 3 Dec 2024 17:26:35 -0500 Subject: [PATCH 7/7] add suggested docstring --- romancal/associations/skycell_asn.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/romancal/associations/skycell_asn.py b/romancal/associations/skycell_asn.py index 9b88712fc..50f66bec1 100644 --- a/romancal/associations/skycell_asn.py +++ b/romancal/associations/skycell_asn.py @@ -21,7 +21,27 @@ def skycell_asn(filelist, output_file_root, product_type, release_product): - """Create the skycell associaton from the list""" + """ + Create the skycell association from the list of files. + + This function processes a list of files, identifies matching patches, generates + TAN WCS parameters, and creates an association file for the identified sky patches. + + Parameters + ---------- + filelist : list of str + List of file names to be processed. + output_file_root : str + Root string for the output association file. + product_type : str + Type of product when creating the association (e.g., 'visit', 'daily'). + release_product : str + Release product identifier for the association. + + Returns + ------- + None + """ all_patches = [] file_list = [] for file_name in filelist: