File: test_ccd_simulator.cpp

package info (click to toggle)
indi 1.9.9%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 13,792 kB
  • sloc: cpp: 186,158; ansic: 30,836; xml: 869; sh: 282; makefile: 11
file content (222 lines) | stat: -rw-r--r-- 10,048 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "indicom.h"
#include "indilogger.h"

#include <gtest/gtest.h>
#include <gmock/gmock.h>

using ::testing::_;
using ::testing::StrEq;

#include "ccd_simulator.h"

char _me[] = "MockCCDSimDriver";
char *me = _me;
class MockCCDSimDriver: public CCDSim
{
    public:
        MockCCDSimDriver(): CCDSim()
        {
            initProperties();
            ISGetProperties(me);
        }

        void testProperties()
        {
            INumberVectorProperty * const p = getNumber("SIMULATOR_SETTINGS");
            ASSERT_NE(p, nullptr);
            ASSERT_NE(IUFindNumber(p, "SIM_XRES"), nullptr);
            ASSERT_NE(IUFindNumber(p, "SIM_YRES"), nullptr);
            ASSERT_NE(IUFindNumber(p, "SIM_XSIZE"), nullptr);
            ASSERT_NE(IUFindNumber(p, "SIM_YSIZE"), nullptr);
            ASSERT_NE(IUFindNumber(p, "SIM_MAXVAL"), nullptr);
            ASSERT_NE(IUFindNumber(p, "SIM_SATURATION"), nullptr);
            ASSERT_NE(IUFindNumber(p, "SIM_LIMITINGMAG"), nullptr);
            ASSERT_NE(IUFindNumber(p, "SIM_NOISE"), nullptr);
            ASSERT_NE(IUFindNumber(p, "SIM_SKYGLOW"), nullptr);
            ASSERT_NE(IUFindNumber(p, "SIM_OAGOFFSET"), nullptr);
            ASSERT_NE(IUFindNumber(p, "SIM_POLAR"), nullptr);
            ASSERT_NE(IUFindNumber(p, "SIM_POLARDRIFT"), nullptr);
        }

        void testGuideAPI()
        {
            // At init, current RA and DEC are undefined - message will not appear because the test passes
            EXPECT_TRUE(isnan(currentRA)) << "Field 'currentRA' is undefined when initializing CCDSim.";
            EXPECT_TRUE(isnan(currentDE)) << "Field 'currentDEC' is undefined when initializing CCDSim.";

            // Guide rate is fixed
            EXPECT_EQ(GuideRate, 7 /* arcsec/s */);

            // Initial guide offsets are zero
            EXPECT_EQ(guideNSOffset, 0);
            EXPECT_EQ(guideWEOffset, 0);

            double const arcsec = 1.0 / 3600.0;

            // Guiding in DEC stores offset in arcsec for next simulation step
            EXPECT_EQ(GuideNorth(1000.0), IPS_OK);
            EXPECT_NEAR(guideNSOffset, +7 * arcsec, 1 * arcsec);
            EXPECT_EQ(GuideSouth(1000.0), IPS_OK);
            EXPECT_NEAR(guideNSOffset, +0 * arcsec, 1 * arcsec);
            EXPECT_EQ(GuideSouth(1000.0), IPS_OK);
            EXPECT_NEAR(guideNSOffset, -7 * arcsec, 1 * arcsec);
            EXPECT_EQ(GuideNorth(1000.0), IPS_OK);
            EXPECT_NEAR(guideNSOffset, +0 * arcsec, 1 * arcsec);

            // RA guiding rate depends on declination, we need a valid one
            currentDE = 0;

            // Guiding in RA stores offset in arcsec for next simulation step
            // There is an adjustemnt based on declination - here zero from previous test
            EXPECT_EQ(GuideWest(1000.0), IPS_OK);
            EXPECT_NEAR(guideWEOffset, +7 * arcsec, 15 * arcsec);
            EXPECT_EQ(GuideEast(1000.0), IPS_OK);
            EXPECT_NEAR(guideWEOffset, +0 * arcsec, 15 * arcsec);
            EXPECT_EQ(GuideEast(1000.0), IPS_OK);
            EXPECT_NEAR(guideWEOffset, -7 * arcsec, 15 * arcsec);
            EXPECT_EQ(GuideWest(1000.0), IPS_OK);
            EXPECT_NEAR(guideWEOffset, +0 * arcsec, 15 * arcsec);

            // TODO: verify DEC-biased RA guiding rate
            // TODO: verify property-based guiding API
        }

        void testDrawStar()
        {
            int const xres = 65;
            int const yres = 65;
            int const maxval = pow(2, 8) - 1;

            // Setup a 65x65, 16-bit depth, 4.6u square pixel sensor
            INumberVectorProperty * const p = getNumber("SIMULATOR_SETTINGS");
            ASSERT_NE(p, nullptr);
            IUFindNumber(p, "SIM_XRES")->value = (double) xres;
            IUFindNumber(p, "SIM_YRES")->value = (double) yres;
            // There is no way to set depth, it is hardcoded at 16-bit - so set maximum value instead
            IUFindNumber(p, "SIM_MAXVAL")->value = (double) maxval;
            IUFindNumber(p, "SIM_XSIZE")->value = 4.6;
            IUFindNumber(p, "SIM_YSIZE")->value = 4.6;

            // Setup a saturation magnitude (max ADUs in one second) and limit magnitude (zero ADU whatever the exposure)
            IUFindNumber(p, "SIM_SATURATION")->value = 0.0;
            IUFindNumber(p, "SIM_LIMITINGMAG")->value = 30.0;

            // Setup some parameters to simplify verifications
            IUFindNumber(p, "SIM_SKYGLOW")->value = 0.0;
            IUFindNumber(p, "SIM_NOISE")->value = 0.0;
            this->seeing = 1.0f; // No way to control seeing from properties

            // Setup
            ASSERT_TRUE(setupParameters());

            // Assert our parameters
            ASSERT_EQ(PrimaryCCD.getBPP(), 16) << "Simulator CCD depth is hardcoded at 16 bits";
            ASSERT_EQ(PrimaryCCD.getXRes(), xres);
            ASSERT_EQ(PrimaryCCD.getYRes(), yres);
            ASSERT_EQ(PrimaryCCD.getPixelSizeX(), 4.6f);
            ASSERT_EQ(PrimaryCCD.getPixelSizeY(), 4.6f);
            ASSERT_NE(PrimaryCCD.getFrameBuffer(), nullptr) << "SetupParms creates the frame buffer";

            // Assert our simplifications
            EXPECT_EQ(this->seeing, 1.0f);
            EXPECT_EQ(this->ImageScalex, 1.0f);
            EXPECT_EQ(this->ImageScaley, 1.0f);
            EXPECT_EQ(this->m_SkyGlow, 0.0f);
            EXPECT_EQ(this->m_MaxNoise, 0.0f);

            // Validate our expectations about flux
            EXPECT_EQ(this->m_MaxVal, maxval);
            EXPECT_NEAR(this->flux(this->m_SaturationMag), maxval, 0.001);
            EXPECT_NEAR(this->flux(this->m_LimitingMag), 1.0, 0.001);

            // The CCD frame is NOT initialized after this call, so manually clear the buffer
            memset(this->PrimaryCCD.getFrameBuffer(), 0, this->PrimaryCCD.getFrameBufferSize());

            // Draw a star at the center row/column of the sensor
            // If we expose a magnitude of 0 for 1 second, we get max ADUs at center, gaussian decrement away by 4 pixels and zero elsewhere
            DrawImageStar(&PrimaryCCD, 0.0f, xres / 2 + 1, xres / 2 + 1, 1.0f);

            // Get a pointer to the 16-bit frame buffer
            uint16_t const * const fb = reinterpret_cast<uint16_t*>(PrimaryCCD.getFrameBuffer());

            // Look at center, and up to 3 pixels away, and find activated photosites there - there is no skyglow nor noise in our parameters
            int const center = xres / 2 + 1 + (yres / 2 + 1) * xres;

            // The choice of the gaussian of unitary integral makes the center less than maximum
            double const sigma = 1.0 / (2 * sqrt(2 * log(2)));
            double const fa0 = 1.0 / (sigma * sqrt(2 * 3.1416));

            // Center photosite
            uint16_t const ADU_at_center = static_cast<uint16_t>(fa0 * maxval);
            EXPECT_EQ(fb[center], ADU_at_center) << "Recorded flux of magnitude 0.0 for 1 second at center is " << ADU_at_center <<
                                                 " ADUs";

            // Up, left, right and bottom photosites at one pixel
            uint16_t const ADU_at_1pix = static_cast<uint16_t>(fa0 * maxval * exp(-(1 * 1 + 0 * 0) / (2 * sigma * sigma)));
            EXPECT_EQ(fb[center - xres], ADU_at_1pix);
            EXPECT_EQ(fb[center - 1], ADU_at_1pix);
            EXPECT_EQ(fb[center + 1], ADU_at_1pix);
            EXPECT_EQ(fb[center + xres], ADU_at_1pix);

            // Up, left, right and bottom photosites at two pixels
            uint16_t const ADU_at_2pix = static_cast<uint16_t>(fa0 * maxval * exp(-(2 * 2 + 0 * 0) / (2 * sigma * sigma)));
            EXPECT_EQ(fb[center - xres * 2], ADU_at_2pix);
            EXPECT_EQ(fb[center - 1 * 2], ADU_at_2pix);
            EXPECT_EQ(fb[center + 1 * 2], ADU_at_2pix);
            EXPECT_EQ(fb[center + xres * 2], ADU_at_2pix);

            // Up, left, right and bottom photosite neighbors at three pixels
            uint16_t const ADU_at_3pix = static_cast<uint16_t>(fa0 * maxval * exp(-(3 * 3 + 0 * 0) / (2 * sigma * sigma)));
            EXPECT_EQ(fb[center - xres * 3], ADU_at_3pix);
            EXPECT_EQ(fb[center - 1 * 3], ADU_at_3pix);
            EXPECT_EQ(fb[center + 1 * 3], ADU_at_3pix);
            EXPECT_EQ(fb[center + xres * 3], ADU_at_3pix);

            // Up, left, right and bottom photosite neighbors at four pixels
            EXPECT_EQ(fb[center - xres * 4], 0.0);
            EXPECT_EQ(fb[center - 1 * 4], 0.0);
            EXPECT_EQ(fb[center + 1 * 4], 0.0);
            EXPECT_EQ(fb[center + xres * 4], 0.0);

            // Conclude with a random benchmark
            auto const before = std::chrono::steady_clock::now();
            int const loops = 200000;
            for (int i = 0; i < loops; i++)
            {
                float const m = (15.0f * rand()) / RAND_MAX;
                float const x = static_cast<float>(xres * rand()) / RAND_MAX;
                float const y = static_cast<float>(yres * rand()) / RAND_MAX;
                float const e = (100.0f * rand()) / RAND_MAX;
                DrawImageStar(&PrimaryCCD, m, x, y, e);
            }
            auto const after = std::chrono::steady_clock::now();
            auto const duration = std::chrono::duration_cast <std::chrono::nanoseconds> (after - before).count() / loops;
            std::cout << "[          ] DrawStarImage - randomized no-noise no-skyglow benchmark: " << duration << "ns per call" <<
                      std::endl;
        }
};

TEST(CCDSimulatorDriverTest, test_properties)
{
    MockCCDSimDriver().testProperties();
}

TEST(CCDSimulatorDriverTest, test_guide_api)
{
    MockCCDSimDriver().testGuideAPI();
}

TEST(CCDSimulatorDriverTest, test_draw_star)
{
    MockCCDSimDriver().testDrawStar();
}

int main(int argc, char **argv)
{
    INDI::Logger::getInstance().configure("", INDI::Logger::file_off,
                                          INDI::Logger::DBG_ERROR, INDI::Logger::DBG_ERROR);

    ::testing::InitGoogleTest(&argc, argv);
    ::testing::InitGoogleMock(&argc, argv);
    return RUN_ALL_TESTS();
}