From c31d6cd9510ae89f0b539c0b61df3d12c1f6ac67 Mon Sep 17 00:00:00 2001
From: Wouter Klijn <klijn@astron.nl>
Date: Thu, 24 May 2012 11:40:34 +0000
Subject: [PATCH] Task #3139: Add ComplexArray.py

---
 .gitattributes                                |  1 +
 .../recipes/sip/helpers/ComplexArray.py       | 54 +++++++++++++++++++
 2 files changed, 55 insertions(+)
 create mode 100644 CEP/Pipeline/recipes/sip/helpers/ComplexArray.py

diff --git a/.gitattributes b/.gitattributes
index dc339d7d1c7..cd996e0584b 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -1118,6 +1118,7 @@ CEP/Pipeline/recipes/sip/demixing/bbs_TauA.parset eol=lf
 CEP/Pipeline/recipes/sip/demixing/bbs_TauA_smoothcal.parset eol=lf
 CEP/Pipeline/recipes/sip/demixing/bbs_VirA.parset eol=lf
 CEP/Pipeline/recipes/sip/demixing/bbs_VirA_smoothcal.parset eol=lf
+CEP/Pipeline/recipes/sip/helpers/ComplexArray.py -text
 CEP/Pipeline/recipes/sip/helpers/WritableParmDB.py -text
 CEP/Pipeline/recipes/sip/helpers/__init__.py eol=lf
 CEP/Pipeline/recipes/sip/helpers/metadata.py eol=lf
diff --git a/CEP/Pipeline/recipes/sip/helpers/ComplexArray.py b/CEP/Pipeline/recipes/sip/helpers/ComplexArray.py
new file mode 100644
index 00000000000..5b245706d2c
--- /dev/null
+++ b/CEP/Pipeline/recipes/sip/helpers/ComplexArray.py
@@ -0,0 +1,54 @@
+import numpy
+
+# Untested copu pasta of jon swinbanks code
+class ComplexArray(object):
+    def __init__(self):
+        raise NotImplementedError
+
+    def get_amp(self):
+        return numpy.absolute(numpy.nan_to_num(self.data))
+    def set_amp(self, new_amps):
+        self.data = numpy.array(new_amps) * self.data / numpy.absolute(self.data)
+    amp = property(get_amp, set_amp)
+    Ampl = amp
+
+    def get_phase(self):
+        return numpy.angle(numpy.nan_to_num(self.data))
+    def set_phase(self, new_phase):
+        self.data = numpy.vectorize(cmath.rect)(
+            numpy.absolute(self.data), numpy.array(new_phase)
+        )
+    phase = property(get_phase, set_phase)
+    Phase = phase
+
+    def get_real(self):
+        return numpy.real(numpy.nan_to_num(self.data))
+    def set_real(self, new_real):
+        self.data = numpy.array(new_real) + 1j * numpy.imag(self.data)
+    real = property(get_real, set_real)
+    Real = real
+
+    def get_imag(self):
+        return numpy.imag(numpy.nan_to_num(self.data))
+    def set_imag(self, new_imag):
+        self.data = numpy.real(self.data) + 1j * numpy.array(new_imag)
+    imag = property(get_imag, set_imag)
+    Imag = imag
+
+    @property
+    def writeable(self):
+        return dict((key, getattr(self, key)) for key in self.keys)
+
+class RealImagArray(ComplexArray):
+    keys = ("Real", "Imag")
+    def __init__(self, real, imag):
+        self.data = numpy.array(real) + 1j * numpy.array(imag)
+
+class AmplPhaseArray(ComplexArray):
+    keys = ("Ampl", "Phase")
+    def __init__(self, ampl, phase):
+        self.data = numpy.vectorize(cmath.rect)(
+            numpy.array(ampl), numpy.array(phase)
+        )
+
+ARRAY_TYPES = [RealImagArray, AmplPhaseArray]
-- 
GitLab