File: testhealpix.cpp

package info (click to toggle)
kst 2.0.8-5
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 30,740 kB
  • sloc: cpp: 97,086; ansic: 13,364; python: 2,970; sh: 761; yacc: 184; lex: 143; makefile: 141; javascript: 122; perl: 30; xml: 30
file content (315 lines) | stat: -rw-r--r-- 11,663 bytes parent folder | download | duplicates (8)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 *  Copyright 2005, Theodore Kisner
 *  Licensed under GPL.
 */

#include <qmap.h>
#include <kconfig.h>
#include <ksttestcase.h>
#include <healpix_source.h>
#include <ksdebug.h>

#define XDIM 1200
#define YDIM 900
#define DEGRADE 1

QString myName;

typedef QMap<QString,QString> StringMap;

static void exitHelper() {
  KST::vectorList.clear();
  KST::scalarList.clear();
  KST::dataObjectList.clear();
}

int rc = KstTestSuccess;

#define doTest(x) testAssert(x, QString("Line %1").arg(__LINE__))
#define doTestD(x, y) testAssert(x, QString("%1: %2").arg(__LINE__).arg(y))

void testAssert(bool result, const QString& text = "Unknown") {
  if (!result) {
    KstTestFailed();
    printf("Test [%s] failed.\n", text.latin1());
  }
}

extern "C" {
KstDataSource *create_healpix(KConfig *cfg, const QString& filename, const QString& type);

KstDataSource *load_healpix(KConfig *cfg, const QString& filename, const QString& type, const QDomElement& e);

int understands_healpix(KConfig *cfg, const QString& filename);

QStringList matrixList_healpix(KConfig *cfg, const QString& filename, const QString& type, QString *typeSuggestion, bool *complete);

QStringList fieldList_healpix(KConfig *cfg, const QString& filename, const QString& type, QString *typeSuggestion, bool *complete);
}

void doTests() {
  // the data file to use.
  // first determine the path used to call this program,
  // and then prepend that to the data file name.
  QString path = myName.section('/', 0, -3);
  QString datafile = path;
  datafile.append("/healpix_example_sm.fits");
  
  // create a temporary config file to use
  QString cfgfile = "testhealpix.temp";
  KConfig *cfg = new KConfig(cfgfile, false, false);
  cfg->setGroup("Healpix General");
  cfg->setGroup(datafile);
  cfg->writeEntry("Matrix X Dimension", XDIM);
  cfg->writeEntry("Matrix Y Dimension", YDIM);
  cfg->writeEntry("Theta Autoscale", true);
  cfg->writeEntry("Theta Units", 0);
  cfg->writeEntry("Theta Min", 0.0);
  cfg->writeEntry("Theta Max", 1.0);
  cfg->writeEntry("Phi Autoscale", true);
  cfg->writeEntry("Phi Units", 0);
  cfg->writeEntry("Phi Min", 0.0);
  cfg->writeEntry("Phi Max", 1.0);
  cfg->writeEntry("Vector Theta", 1);
  cfg->writeEntry("Vector Phi", 2);
  cfg->writeEntry("Vector Degrade Factor", DEGRADE);
  cfg->writeEntry("Vector Magnitude Autoscale", true);
  cfg->writeEntry("Vector Max Magnitude", 1.0);
  cfg->writeEntry("Vector is QU", true);
  
  // use the C functions to test for healpix support
  int verstehen = understands_healpix(cfg, datafile);
  if (verstehen) {
    kstdDebug() << "Data file " << datafile << " is supported" << endl;
    QString suggestion;
    bool complete;
    
    QStringList matrices = matrixList_healpix(cfg, datafile, "HEALPIX", &suggestion, &complete);
    kstdDebug() << "Available matrices are:" << endl;
    for ( QStringList::Iterator it = matrices.begin(); it != matrices.end(); ++it ) {
      kstdDebug() << "  " << *it << endl;
    }
    kstdDebug() << "  suggestion = " << suggestion << endl;
    kstdDebug() << "  complete = " << complete << endl;

    QStringList fields = fieldList_healpix(cfg, datafile, "HEALPIX", &suggestion, &complete);
    kstdDebug() << "Available fields are:" << endl;
    for ( QStringList::Iterator it = fields.begin(); it != fields.end(); ++it ) {
      kstdDebug() << "  " << *it << endl;
    }
    kstdDebug() << "  suggestion = " << suggestion << endl;
    kstdDebug() << "  complete = " << complete << endl;
    
    // actually create HealpixSource
    HealpixSource *hpx = new HealpixSource(cfg, datafile, "HEALPIX");
    
    // test that saveConfig produces the same as the 
    // original input configuration
    QString cfgfile2 = "testhealpix.temp2";
    KConfig *chk = new KConfig(cfgfile2, false, false);
    hpx->saveConfig(chk);
    StringMap cfgmap = cfg->entryMap(datafile);
    StringMap chkmap = chk->entryMap(datafile);
    
    /*
    kstdDebug() << cfgmap["Matrix X Dimension"] << " " << chkmap["Matrix X Dimension"] << endl;
    kstdDebug() << cfgmap["Matrix Y Dimension"] << " " << chkmap["Matrix Y Dimension"] << endl;
    kstdDebug() << cfgmap["Theta Autoscale"] << " " << chkmap["Theta Autoscale"] << endl;
    kstdDebug() << cfgmap["Theta Units"] << " " << chkmap["Theta Units"] << endl;
    kstdDebug() << cfgmap["Theta Min"] << " " << chkmap["Theta Min"] << endl;
    kstdDebug() << cfgmap["Theta Max"] << " " << chkmap["Theta Max"] << endl;
    kstdDebug() << cfgmap["Phi Autoscale"] << " " << chkmap["Phi Autoscale"] << endl;
    kstdDebug() << cfgmap["Phi Units"] << " " << chkmap["Phi Units"] << endl;
    kstdDebug() << cfgmap["Phi Min"] << " " << chkmap["Phi Min"] << endl;
    kstdDebug() << cfgmap["Phi Max"] << " " << chkmap["Phi Max"] << endl;
    kstdDebug() << cfgmap["Vector Theta"] << " " << chkmap["Vector Theta"] << endl;
    kstdDebug() << cfgmap["Vector Phi"] << " " << chkmap["Vector Phi"] << endl;
    kstdDebug() << cfgmap["Vector Degrade Factor"] << " " << chkmap["Vector Degrade Factor"] << endl;
    kstdDebug() << cfgmap["Vector Magnitude Autoscale"] << " " << chkmap["Vector Magnitude Autoscale"] << endl;
    kstdDebug() << cfgmap["Vector Max Magnitude"] << " " << chkmap["Vector Max Magnitude"] << endl;
    kstdDebug() << cfgmap["Vector is QU"] << " " << chkmap["Vector is QU"] << endl;
    */
    
    if (cfgmap["Matrix X Dimension"] != chkmap["Matrix X Dimension"]) {
      QString msg = "Matrix X Dimension integrity";
      testAssert(false, msg);
    }
    if (cfgmap["Matrix Y Dimension"] != chkmap["Matrix Y Dimension"]) {
      QString msg = "Matrix Y Dimension integrity";
      testAssert(false, msg);
    }
    if (cfgmap["Theta Autoscale"] != chkmap["Theta Autoscale"]) {
      QString msg = "Theta Autoscale integrity";
      testAssert(false, msg);
    }
    if (cfgmap["Theta Units"] != chkmap["Theta Units"]) {
      QString msg = "Theta Units integrity";
      testAssert(false, msg);
    }
    if (cfgmap["Theta Min"] != chkmap["Theta Min"]) {
      QString msg = "Theta Min integrity";
      testAssert(false, msg);
    }
    if (cfgmap["Theta Max"] != chkmap["Theta Max"]) {
      QString msg = "Theta Max integrity";
      testAssert(false, msg);
    }
    if (cfgmap["Phi Autoscale"] != chkmap["Phi Autoscale"]) {
      QString msg = "Phi Autoscale integrity";
      testAssert(false, msg);
    }
    if (cfgmap["Phi Units"] != chkmap["Phi Units"]) {
      QString msg = "Phi Units integrity";
      testAssert(false, msg);
    }
    if (cfgmap["Phi Min"] != chkmap["Phi Min"]) {
      QString msg = "Phi Min integrity";
      testAssert(false, msg);
    }
    if (cfgmap["Phi Max"] != chkmap["Phi Max"]) {
      QString msg = "Phi Max integrity";
      testAssert(false, msg);
    }
    if (cfgmap["Vector Theta"] != chkmap["Vector Theta"]) {
      QString msg = "Vector Theta integrity";
      testAssert(false, msg);
    }
    if (cfgmap["Vector Phi"] != chkmap["Vector Phi"]) {
      QString msg = "Vector Phi integrity";
      testAssert(false, msg);
    }
    if (cfgmap["Vector Degrade Factor"] != chkmap["Vector Degrade Factor"]) {
      QString msg = "Vector Degrade Factor integrity";
      testAssert(false, msg);
    }
    if (cfgmap["Vector Magnitude Autoscale"] != chkmap["Vector Magnitude Autoscale"]) {
      QString msg = "Vector Magnitude Autoscale integrity";
      testAssert(false, msg);
    }
    if (cfgmap["Vector Max Magnitude"] != chkmap["Vector Max Magnitude"]) {
      QString msg = "Vector Max Magnitude integrity";
      testAssert(false, msg);
    }
    if (cfgmap["Vector is QU"] != chkmap["Vector is QU"]) {
      QString msg = "Vector is QU integrity";
      testAssert(false, msg);
    }
    kstdDebug() << "Save/Load config is consistent." << endl;
    
    // print _metaData and compute NSIDE and number
    // of samples in the vectors
    int nside = 0;
    int nvec;
    QString key;
    QString data;
    kstdDebug() << "Checking metaData:" << endl;
    StringMap metamap = hpx->metaData();
    for ( StringMap::Iterator it = metamap.begin(); it != metamap.end(); ++it ) {
      key = it.key();
      data = it.data();
      kstdDebug() << "  " << key.latin1() << " = " << data.latin1() << endl;
      if (key == "NSIDE") {
        nside = data.toInt();
      }
    }
    kstdDebug() << "Data file has nside = " << nside << endl;
    testAssert(nside != 0, "data file NSIDE");
    for (int i = 0; i < DEGRADE; i++) {
      nside = (int)(nside/2);
    }
    nvec = 12*nside*nside;
    kstdDebug() << "Degraded vectorfield has nside = " << nside << " and " << nvec << " full-sphere pixels" << endl;
    
    // check that all returned fields are valid, and that
    // optionally field number names are valid.
    kstdDebug() << "Checking matrix validity:" << endl;
    int num = 1;
    int xdim, ydim, nframe, sampframe;
    for ( QStringList::Iterator it = matrices.begin(); it != matrices.end(); ++it ) {
      if (hpx->isValidMatrix(*it)) {
        kstdDebug() << "  \"" << *it << "\" is VALID" << endl;
        hpx->matrixDimensions(*it, &xdim, &ydim);
        kstdDebug() << "    and has dimensions " << xdim << "x" << ydim << endl;
        testAssert((xdim == XDIM)&&(ydim == YDIM), "dimension integrity");
      } else {
        QString msg = (*it);
        msg.append(" validity");
        testAssert(false, msg);
      }
      QString numfield = QString("%1").arg(num);
      if (hpx->isValidMatrix(numfield)) {
        kstdDebug() << "  \"" << numfield << "\" is VALID" << endl;
        hpx->matrixDimensions(numfield, &xdim, &ydim);
        kstdDebug() << "    and has dimensions " << xdim << "x" << ydim << endl;
        testAssert((xdim == XDIM)&&(ydim == YDIM), "dimension integrity");
      } else {
        QString msg = numfield;
        msg.append(" validity");
        testAssert(false, msg);
      }
      num++;
    }
    kstdDebug() << "Checking field validity:" << endl;
    num = 1;
    for ( QStringList::Iterator it = fields.begin(); it != fields.end(); ++it ) {
      if (hpx->isValidField(*it)) {
        kstdDebug() << "  \"" << *it << "\" is VALID" << endl;
        nframe = hpx->frameCount(*it);
        sampframe = hpx->samplesPerFrame(*it);
        kstdDebug() << "    and has " << nframe << " frames of " << sampframe << " sample(s) each" << endl;
        testAssert(sampframe == 1, "samples per frame");
        testAssert(nframe == nvec, "number of frames");
      } else {
        QString msg = (*it);
        msg.append(" validity");
        testAssert(false, msg);
      }
      QString numfield = QString("%1").arg(num);
      if (hpx->isValidField(numfield)) {
        kstdDebug() << "  \"" << numfield << "\" is VALID" << endl;
        nframe = hpx->frameCount(numfield);
        sampframe = hpx->samplesPerFrame(numfield);
        kstdDebug() << "    and has " << nframe << " frames of " << sampframe << " sample(s) each" << endl;
        testAssert(sampframe == 1, "samples per frame");
        testAssert(nframe == nvec, "number of frames");
      } else {
        QString msg = numfield;
        msg.append(" validity");
        testAssert(false, msg);
      }
      num++;
    }
    
    // check reset function
    if (hpx->reset()) {
      kstdDebug() << "Reset function is implemented." << endl;
    } else {
      testAssert(false, "reset");
    }
        
    
    
    
  } else {
    testAssert(false, "understanding");
  }
  
}


int main(int argc, char **argv) {
  atexit(exitHelper);

  myName = argv[0];
  KApplication app(argc, argv, "testhealpix", false, false);

  doTests();

  exitHelper(); // before app dies
  if (rc == KstTestSuccess) {
    printf("All tests passed!\n");
  }
  return -rc;
}

// vim: ts=2 sw=2 et