LCOV - code coverage report
Current view: top level - synthesis/CalLibrary/test - tCalLibraryParse.cc (source / functions) Hit Total Coverage
Test: casacpp_coverage.info Lines: 77 91 84.6 %
Date: 2024-10-28 15:53:10 Functions: 3 3 100.0 %

          Line data    Source code
       1             : //# Copyright (C) 2015
       2             : //# Associated Universities, Inc. Washington DC, USA.
       3             : //#
       4             : //# This program is free software; you can redistribute it and/or modify it
       5             : //# under the terms of the GNU General Public License as published by the Free
       6             : //# Software Foundation; either version 2 of the License, or (at your option)
       7             : //# any later version.
       8             : //#
       9             : //# This program is distributed in the hope that it will be useful, but WITHOUT
      10             : //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
      11             : //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
      12             : //# more details.
      13             : //#
      14             : //# You should have received a copy of the GNU General Public License along
      15             : //# with this program; if not, write to the Free Software Foundation, Inc.,
      16             : //# 675 Massachusetts Ave, Cambridge, MA 02139, USA.
      17             : //#
      18             : //# Correspondence concerning AIPS++ should be addressed as follows:
      19             : //#        Internet email: casa-feedback@nrao.edu.
      20             : //#        Postal address: AIPS++ Project Office
      21             : //#                        National Radio Astronomy Observatory
      22             : //#                        520 Edgemont Road
      23             : //#                        Charlottesville, VA 22903-2475 USA
      24             : //#
      25             : 
      26             : #include <casacore/casa/aips.h>
      27             : #include <synthesis/CalLibrary/CalLibraryTools.h>
      28             : #include <casacore/casa/Containers/Record.h>
      29             : 
      30             : #include <fstream>
      31             : 
      32             : using namespace casacore;
      33             : using namespace casa;
      34             : using namespace std;
      35             : 
      36           2 : int parseString(String callibString, Bool testString=true) {
      37             :   /* Return value of 1 = parsing succeeded */
      38             :   try {
      39           2 :     cout << "\ntCalLibraryParse STRING " << endl;
      40             : 
      41           2 :     Record callibRec = callibSetParams(callibString);
      42           2 :     if (testString) {
      43             :         bool ok1, ok2, ok3, ok4;
      44             :         //cout << "DEBUG: callibRec = " << callibRec << endl;
      45             : 
      46             :         // Check 1 - number of fields in returned Record
      47           1 :         ok1 = (callibRec.nfields() == 1);
      48           1 :         cout << "callib Record nfields=1 check: ";
      49           1 :         cout << (ok1 ? "PASS" : "FAIL")<< endl;
      50             : 
      51             :         // Check 2 - bcal has 2 fields
      52           1 :         Record bcalRec = callibRec.asRecord("ngc5921.bcal");
      53           1 :         ok2 = (bcalRec.nfields() == 2);
      54           1 :         cout << "bcal Record nfields=2 check: ";
      55           1 :         cout << (ok2 ? "PASS" : "FAIL") << endl;
      56             : 
      57             :         // Check 3 - bcal tinterp check
      58           1 :         Record bcalRec0 = bcalRec.asRecord("0");
      59           1 :         ok3 = (bcalRec0.asString("tinterp") == "nearest"); 
      60           1 :         cout << "bcal tinterp='nearest' check: ";
      61           1 :         cout << (ok3 ? "PASS" : "FAIL") << endl;
      62             : 
      63             :         // Check 4 - bcal calwt=false 
      64           1 :         ok4 = (bcalRec.asBool("calwt") == 0); 
      65           1 :         cout << "bcal calwt=0 check: ";
      66           1 :         cout << (ok4 ? "PASS" : "FAIL") << endl;
      67             : 
      68           1 :         return (ok1 && ok2 && ok3 && ok4);
      69           1 :     } else {
      70             :         // Feedback to user; cannot check for right answers!
      71           1 :         cout << "callib Record nfields = " << callibRec.nfields() << endl;
      72           1 :         cout << "Record parsed from string:" << endl;
      73           1 :         cout << callibRec << endl;
      74           1 :         return 1;
      75             :     }
      76           2 :   } catch (AipsError x) {
      77           0 :     cout << "ERROR: " << x.getMesg() << endl;
      78           0 :     return 0;
      79           0 :   }
      80             : }
      81             : 
      82           1 : int parseFile(String callibName) {
      83             :   /* Return value of 1 = parsing succeeded */
      84           1 :   cout << "\ntCalLibraryParse FILE " << callibName << endl;
      85             : 
      86             :   try {
      87           1 :     Record callibRec = callibSetParams(callibName);
      88             :     //cout << "DEBUG: callibRec = " << callibRec << endl;
      89             :     
      90           1 :     if (callibName == "/tmp/test_callib_parse.txt") {
      91             :         bool ok1, ok2, ok3, ok4a, ok4b;
      92             : 
      93             :         // Check 1 - number of fields in returned Record
      94           1 :         ok1 = (callibRec.nfields() == 3);  // 3 caltables
      95           1 :         cout << "callib Record nfields=3 check: ";
      96           1 :         cout << (ok1 ? "PASS" : "FAIL") << endl;
      97             : 
      98             :         // Check 2 - gcal has 2 Records plus calwt
      99           1 :         ok2 = (callibRec.asRecord("ngc5921.gcal").nfields() == 3);
     100           1 :         cout << "gcal nfields=3 check: ";
     101           1 :         cout << (ok2 ? "PASS" : "FAIL") << endl;
     102             : 
     103             :         // Check 3 - gcal kept calwt=true setting
     104           1 :         Record gcalRec = callibRec.asRecord("ngc5921.gcal");
     105           1 :         ok3 = (gcalRec.asBool("calwt") == 1); 
     106           1 :         cout << "gcal calwt=1 check: ";
     107           1 :         cout << (ok3 ? "PASS" : "FAIL") << endl;
     108             : 
     109             :         // Check 4 - properly parsed fldmap list
     110           1 :         Record gcalRec1 = gcalRec.asRecord("1");
     111           1 :         ok4a = (gcalRec1.dataType("fldmap") == TpArrayInt); 
     112           1 :         cout << "gcal1 fldmap type is Array<Int> check: ";
     113           1 :         cout << (ok4a ? "PASS" : "FAIL") << endl;
     114           1 :         Array<Int> fldmap = gcalRec1.asArrayInt("fldmap");
     115           1 :         ok4b = (fldmap.size() == 4);
     116           1 :         cout << "gcal1 fldmap size=4 check: ";
     117           1 :         cout << (ok4b ? "PASS" : "FAIL") << endl;
     118             : 
     119           1 :         return (ok1 && ok2 && ok3 && ok4a && ok4b);
     120           1 :     } else {
     121             :         // Feedback to user; cannot check for right answers!
     122           0 :         cout << "callib Record nfields = " << callibRec.nfields() << endl;
     123           0 :         cout << "Record parsed from file:" << endl;
     124           0 :         cout << callibRec << endl;
     125           0 :         return 1;
     126             :     }
     127           1 :   } catch (AipsError x) {
     128           0 :     cout << "ERROR: " << x.getMesg() << endl;
     129           0 :     return 0;
     130           0 :   }
     131             : }
     132             : 
     133           1 : int main(int argc, const char* argv[])
     134             : {
     135           1 :     String callib;
     136           1 :     if (argc == 2) {
     137           0 :         callib = argv[1];
     138           0 :         if (callib.contains("="))
     139           0 :             parseString(callib, false);
     140             :         else
     141           0 :             parseFile(callib);
     142             :     } else {
     143           1 :         String cal1 = "caltable='ngc5921.bcal' calwt=false tinterp='nearest'";
     144           1 :         String cal2 = "caltable='ngc5921.fluxscale' calwt=T tinterp='nearest' fldmap='nearest'";
     145           1 :         String cal3 = "caltable='ngc5921.gcal' calwt=true field='0' tinterp='nearest' fldmap=[0]";
     146           1 :         String cal4 = "calwt=false field='1,2' tinterp='linear' fldmap=[0,1,1,3] caltable='ngc5921.gcal'";
     147           1 :         String cal5 = "# this is a comment";
     148             : 
     149           1 :         int ok_string = parseString(cal1);
     150             : 
     151           1 :         String multistring = cal1 + "," + cal2;
     152             :         // Don't check Record, just make sure it parses without error
     153           1 :         int ok_string2 = parseString(multistring, false);
     154             : 
     155             :         // make callib file from strings
     156           1 :         const char* filename = "/tmp/test_callib_parse.txt";
     157           1 :         ofstream callibFile;
     158           1 :         callibFile.open(filename);
     159           1 :         callibFile << cal1 << "\n";
     160           1 :         callibFile << cal5 << "\n";  // insert comment
     161           1 :         callibFile << cal2 << "\n";
     162           1 :         callibFile << "\n";  // insert blank line
     163           1 :         callibFile << cal3 << "\n";
     164           1 :         callibFile << cal4 << "\n";
     165           1 :         callibFile.close();
     166           1 :         callib = filename;
     167             : 
     168           1 :         int ok_file = parseFile(callib);
     169             : 
     170           1 :         cout << "\ntCalLibraryParse result = " << ((ok_string && ok_string2 && ok_file) ? "PASS" : "FAIL") << endl;
     171           1 :     }
     172           1 :   return 0;
     173           1 : }  

Generated by: LCOV version 1.16