From 5365b6b13916eeb0d5bd4d73e3d8268c06e36b3f Mon Sep 17 00:00:00 2001
From: Jean-Baptiste Bayle <j2b.bayle@gmail.com>
Date: Thu, 30 Jun 2022 06:03:53 +0200
Subject: [PATCH] Add unit tests for GW response reading

---
 .gitattributes       |   2 +
 tests/gws-1-1.h5     |   3 +
 tests/gws-2-0-dev.h5 |   3 +
 tests/test_gws.py    | 164 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 172 insertions(+)
 create mode 100644 tests/gws-1-1.h5
 create mode 100644 tests/gws-2-0-dev.h5
 create mode 100644 tests/test_gws.py

diff --git a/.gitattributes b/.gitattributes
index 18fb6d7..df50dbe 100755
--- a/.gitattributes
+++ b/.gitattributes
@@ -4,3 +4,5 @@ tests/keplerian-orbits-2-0.h5 filter=lfs diff=lfs merge=lfs -text
 tests/esa-trailing-orbits-2-0.h5 filter=lfs diff=lfs merge=lfs -text
 tests/keplerian-fplan-1-1.h5 filter=lfs diff=lfs merge=lfs -text
 tests/esa-trailing-fplan-1-1.h5 filter=lfs diff=lfs merge=lfs -text
+tests/gws-1-1.h5 filter=lfs diff=lfs merge=lfs -text
+tests/gws-2-0-dev.h5 filter=lfs diff=lfs merge=lfs -text
diff --git a/tests/gws-1-1.h5 b/tests/gws-1-1.h5
new file mode 100644
index 0000000..c8c5979
--- /dev/null
+++ b/tests/gws-1-1.h5
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:95a12c32a7ab80c75e88d67329e7840760eff990a180a79360da6b1d0f74d150
+size 28672
diff --git a/tests/gws-2-0-dev.h5 b/tests/gws-2-0-dev.h5
new file mode 100644
index 0000000..6abce64
--- /dev/null
+++ b/tests/gws-2-0-dev.h5
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:b14f934a46606fffe413bfa4baaf461162ff76346f546b09c5cfb7895d1b8f41
+size 20272
diff --git a/tests/test_gws.py b/tests/test_gws.py
new file mode 100644
index 0000000..5951f04
--- /dev/null
+++ b/tests/test_gws.py
@@ -0,0 +1,164 @@
+#! /usr/bin/env python3
+# -*- coding: utf-8 -*-
+"""Test reading of GW response and GW files."""
+
+import numpy as np
+from pytest import approx
+from h5py import File
+from lisainstrument import Instrument
+
+
+def test_no_gws():
+    """Test that we simulate without GWs."""
+
+    instru = Instrument(size=100, gws=None)
+    instru.disable_all_noises()
+
+    assert instru.gw_file is None
+    for mosa in instru.MOSAS:
+        assert instru.gws[mosa] == 0
+
+    instru.simulate()
+
+    for mosa in instru.MOSAS:
+        assert instru.distant_carrier_fluctuations[mosa] == approx(0)
+        assert instru.distant_usb_fluctuations[mosa] == approx(0)
+        assert instru.tps_isi_carrier_fluctuations[mosa] == approx(0)
+        assert instru.tps_isi_usb_fluctuations[mosa] == approx(0)
+        assert instru.isi_carrier_fluctuations[mosa] == approx(0)
+        assert instru.isi_usb_fluctuations[mosa] == approx(0)
+
+def test_dict_of_gws():
+    """Test that we pass link responses in a dictionary."""
+
+    responses = {
+        mosa: np.random.normal(size=100 * 16)
+        for mosa in Instrument.MOSAS
+    }
+
+    instru = Instrument(size=100, lock='six', gws=responses)
+    instru.disable_all_noises()
+
+    assert instru.gw_file is None
+    for mosa in instru.MOSAS:
+        assert np.all(instru.gws[mosa] == responses[mosa])
+
+    instru.simulate()
+
+    for mosa in instru.MOSAS:
+        assert approx(instru.distant_carrier_fluctuations[mosa]) == \
+            -(instru.central_freq + instru.local_carrier_offsets[mosa]) * responses[mosa]
+        assert approx(instru.distant_usb_fluctuations[mosa]) == \
+            -(instru.central_freq + instru.local_usb_offsets[mosa]) * responses[mosa]
+        assert approx(instru.tps_isi_carrier_fluctuations[mosa]) == \
+            -(instru.central_freq + instru.local_carrier_offsets[mosa]) * responses[mosa]
+        assert approx(instru.tps_isi_usb_fluctuations[mosa]) == \
+            -(instru.central_freq + instru.local_usb_offsets[mosa]) * responses[mosa]
+
+def test_gw_file_1_1():
+    """Test that we can read GW files v1.1 with orbit files v1.0.2.
+
+    Test GW file can be generated using LISA GW Response and the following script.
+
+        from h5py import File
+        from lisagwresponse import GalacticBinary
+
+        orbits = 'tests/keplerian-orbits-1-0-2.h5'
+        with File(orbits, 'r') as f:
+            t0 = f.attrs['t0'] + 10
+
+        galbin = GalacticBinary(
+            A=1.0,
+            f=1E-3,
+            orbits=orbits,
+            t0=t0,
+            size=100,
+            gw_beta=0, gw_lambda=0,
+        )
+
+        galbin.write('tests/gws-1-1.h5')
+    """
+
+    gw_file = 'tests/gws-1-1.h5'
+    gwf = File(gw_file, 'r')
+
+    instru = Instrument(
+        size=100,
+        t0=gwf.attrs['t0'],
+        dt=gwf.attrs['dt'],
+        physics_upsampling=1,
+        aafilter=None,
+        lock='six',
+        gws=gw_file,
+        orbits='tests/keplerian-orbits-1-0-2.h5')
+    instru.disable_all_noises()
+
+    assert instru.gw_file is gw_file
+    for mosa in instru.MOSAS:
+        assert instru.gws[mosa] == approx(gwf[f'tcb/l_{mosa}'][:])
+
+    instru.simulate()
+
+    for mosa in instru.MOSAS:
+        assert approx(instru.distant_carrier_fluctuations[mosa]) == \
+            -(instru.central_freq + instru.local_carrier_offsets[mosa]) * gwf[f'tcb/l_{mosa}'][:]
+        assert approx(instru.distant_usb_fluctuations[mosa]) == \
+            -(instru.central_freq + instru.local_usb_offsets[mosa]) * gwf[f'tcb/l_{mosa}'][:]
+        assert approx(instru.tps_isi_carrier_fluctuations[mosa]) == \
+            -(instru.central_freq + instru.local_carrier_offsets[mosa]) * gwf[f'tcb/l_{mosa}'][:]
+        assert approx(instru.tps_isi_usb_fluctuations[mosa]) == \
+            -(instru.central_freq + instru.local_usb_offsets[mosa]) * gwf[f'tcb/l_{mosa}'][:]
+
+def test_gw_file_2_0_dev():
+    """Test that we can read GW files v2.0.dev with orbit files v2.0.
+
+    Test GW file can be generated using LISA GW Response and the following script.
+
+        from h5py import File
+        from lisagwresponse import GalacticBinary
+
+        orbits = 'tests/keplerian-orbits-2-0.h5'
+        with File(orbits, 'r') as f:
+            t0 = f.attrs['t0'] + 10
+
+        galbin = GalacticBinary(
+            A=1.0,
+            f=1E-3,
+            orbits=orbits,
+            t0=t0,
+            size=100,
+            gw_beta=0, gw_lambda=0,
+        )
+
+        galbin.write('tests/gws-2-0-dev.h5')
+    """
+
+    gw_file = 'tests/gws-2-0-dev.h5'
+    gwf = File(gw_file, 'r')
+
+    instru = Instrument(
+        size=100,
+        t0=gwf.attrs['t0'],
+        dt=gwf.attrs['dt'],
+        physics_upsampling=1,
+        aafilter=None,
+        lock='six',
+        gws=gw_file,
+        orbits='tests/keplerian-orbits-2-0.h5')
+    instru.disable_all_noises()
+
+    assert instru.gw_file is gw_file
+    for imosa, mosa in enumerate(instru.MOSAS):
+        assert instru.gws[mosa] == approx(gwf['tcb/y'][:, imosa])
+
+    instru.simulate()
+
+    for imosa, mosa in enumerate(instru.MOSAS):
+        assert approx(instru.distant_carrier_fluctuations[mosa]) == \
+            -(instru.central_freq + instru.local_carrier_offsets[mosa]) * gwf['tcb/y'][:, imosa]
+        assert approx(instru.distant_usb_fluctuations[mosa]) == \
+            -(instru.central_freq + instru.local_usb_offsets[mosa]) * gwf['tcb/y'][:, imosa]
+        assert approx(instru.tps_isi_carrier_fluctuations[mosa]) == \
+            -(instru.central_freq + instru.local_carrier_offsets[mosa]) * gwf['tcb/y'][:, imosa]
+        assert approx(instru.tps_isi_usb_fluctuations[mosa]) == \
+            -(instru.central_freq + instru.local_usb_offsets[mosa]) * gwf['tcb/y'][:, imosa]
-- 
GitLab