From 6938d209acb1f094eaae45a41fc775fee53905bd Mon Sep 17 00:00:00 2001
From: David Rafferty <drafferty@hs.uni-hamburg.de>
Date: Mon, 23 Oct 2023 13:16:11 +0000
Subject: [PATCH] Adjust merge to exclude duplicate components

---
 scripts/merge_skymodels.py | 34 ++++++++++++++++++++++++++++------
 steps/merge_skymodels.cwl  | 17 +++++++++++++++++
 2 files changed, 45 insertions(+), 6 deletions(-)

diff --git a/scripts/merge_skymodels.py b/scripts/merge_skymodels.py
index a7e97f71..2baf2138 100755
--- a/scripts/merge_skymodels.py
+++ b/scripts/merge_skymodels.py
@@ -5,12 +5,28 @@ Append a LOFAR skymodel to an existing one
 
 import os, logging
 import lsmtool
-import numpy
 
 ########################################################################
 
-def main(inmodel1, inmodel2, outmodel = 'output.skymodel'):
-    
+def main(inmodel1, inmodel2, outmodel = 'output.skymodel', radius=30/3600, keep='from2'):
+    """
+    Merge two LOFAR skymodel text files.
+
+    Parameters
+    ----------
+    inmodel1 : str
+        Name (path) of input model #1
+    inmodel2 : str
+        Name (path) of input model #2
+    outmodel : str, optional
+        Name (path) of output merged model
+    radius : float, optional
+        Matching radius in degrees for determining duplicates
+    keep : str, optional
+        When duplicates are found, keep those from model #1 ('from1') or from
+        model #2 ('from2')
+    """
+
     logging.info('Reading ' + inmodel1)
     s1 = lsmtool.load(inmodel1)
     logging.info('Reading ' + inmodel2)
@@ -22,7 +38,10 @@ def main(inmodel1, inmodel2, outmodel = 'output.skymodel'):
         s2.group('single')
 
     logging.info('Adding skymodel ' + inmodel2 + ' to ' + inmodel1)
-    s1.concatenate(s2, inheritPatches=True)
+
+    # The following call will merge the two models, identifing (by position) duplicate
+    # sources/components that are present in both
+    s1.concatenate(s2, matchBy='position', radius=radius, keep=keep)
     s1.setPatchPositions()
     s1.write(outmodel)
 
@@ -36,11 +55,14 @@ if __name__ == '__main__':
     parser.add_argument('--inmodel1', type=str, default=None, help='input/output skymodel.')
     parser.add_argument('--inmodel2', type=str, default=None, help='skymodel to append')
     parser.add_argument('--outmodel', type=str, default='output.skymodel', help='output skymodel name')
+    parser.add_argument('--radius', type=float, default=30/3600, help='matching radius in degrees')
+    parser.add_argument('--keep', type=str, default='from2', help='keep duplicates from model 1 or 2')
 
     args = parser.parse_args()
 
     format_stream = logging.Formatter("%(asctime)s\033[1m %(levelname)s:\033[0m %(message)s","%Y-%m-%d %H:%M:%S")
     format_file   = logging.Formatter("%(asctime)s %(levelname)s: %(message)s","%Y-%m-%d %H:%M:%S")
     logging.root.setLevel(logging.INFO)
- 
-    main(inmodel1 = os.path.expandvars(args.inmodel1), inmodel2 = os.path.expandvars(args.inmodel2), outmodel = os.path.expandvars(args.outmodel))
+
+    main(inmodel1 = os.path.expandvars(args.inmodel1), inmodel2 = os.path.expandvars(args.inmodel2),
+         outmodel = os.path.expandvars(args.outmodel), radius=args.radius, keep=args.keep)
diff --git a/steps/merge_skymodels.cwl b/steps/merge_skymodels.cwl
index 45d7b41a..8a99b557 100755
--- a/steps/merge_skymodels.cwl
+++ b/steps/merge_skymodels.cwl
@@ -29,6 +29,23 @@ inputs:
       prefix: --outmodel=
       separate: false
       shellQuote: false
+  - id: radius
+    type: float?
+    default: 0.00833
+    inputBinding:
+      prefix: --radius=
+      separate: false
+      shellQuote: false
+    doc: Matching radius in degrees for identifying duplicate components
+  - id: keep
+    type: string?
+    default: 'from2'
+    inputBinding:
+      prefix: --keep=
+      separate: false
+      shellQuote: false
+    doc: Keep duplicate components from model 1 or model 2
+
 outputs:
   - id: skymodel_out
     type: File
-- 
GitLab