Index: /trunk/SophyaProg/PMixer/skymixer.cc
===================================================================
--- /trunk/SophyaProg/PMixer/skymixer.cc	(revision 1354)
+++ /trunk/SophyaProg/PMixer/skymixer.cc	(revision 1355)
@@ -25,8 +25,8 @@
 // -----------------------------------------------------------------
 // ------------- Function declaration ------------------------------
-int CheckCards(DataCards & dc, string & msg);
+int CheckCards(DataCards & dc, string & msg, FitsOutFile& fios);
 char * BuildFITSFileName(string const & fname);
-SpectralResponse * getSpectralResponse(DataCards & dc);
-RadSpectra * getEmissionSpectra(DataCards & dc, int nc);
+SpectralResponse * getSpectralResponse(DataCards & dc, FitsOutFile& fios);
+RadSpectra * getEmissionSpectra(DataCards & dc, int nc, FitsOutFile& fios);
 void SKM_MergeFITSKeywords(char * flnm);
 void SKM_MergeFITSKeywords2(DVList & dvl);
@@ -72,7 +72,8 @@
 static POutPersist * so = NULL;    // Debug PPFOut file
 static DVList * dvl_fitskw = NULL; // Global DVList for all FITS Keywords 
-
+bool checkpdmtype=false;
+bool dvliston= false;
 // --------- SkyMixer Version --------------
-static double skm_version = 1.4;
+static double skm_version = 1.5;
 
 // -------------------------------------------------------------------------
@@ -87,5 +88,6 @@
   
   InitTim();
-
+  FitsOutFile fios(arg[2]);
+     
   cout << " skymixer Version= " << skm_version << " SophyaVersion= " 
        << SophyaVersion() << endl;
@@ -107,5 +109,5 @@
     dc.ReadFile(dcard);
     
-    rc = CheckCards(dc, msg);
+    rc = CheckCards(dc, msg,fios);
     if (rc) { 
       cerr << " Error condition -> Rc= " << rc << endl;
@@ -127,5 +129,5 @@
   // We create an output persist file for writing debug objects
   if (debuglev > 0) so = new POutPersist("skymixdbg.ppf");
-  
+  int countHead = 2;
   SphereHEALPix<float> outgs(hp_nside);
   try{
@@ -136,10 +138,25 @@
       cout << " Reading output HealPix map from FITS file " << (string)ifnm << endl;
       {
-	FITS_SphereHEALPix<float> fios(&outgs);
-	fios.Read(ifnm,2);
+	FitsInFile fitsin(ifnm);
+	fitsin >> outgs;
 	//  Getting FITS keywords in primary header
 	//	SKM_MergeFITSKeywords(ifnm); 
 	//  Getting FITS keywords in data object header
-	SKM_MergeFITSKeywords2(outgs.Info()); 
+	if(checkpdmtype)
+	  {
+	    int status = 1;
+	    string whatsup=fitsin.getStringKeyword(1,"PDMTYPE",status);
+	    if(!(status==0&&whatsup=="COMPMAP")) 
+	      {
+		cerr << " !!!! skymixer - file" << ifnm << " is not a COMPMAP " << endl;
+		throw ParmError("Error");
+	      }
+	    else cout << "read the comment COMPMAP from " << ifnm << endl;
+	  }
+	if(dvliston) SKM_MergeFITSKeywords2(outgs.Info());
+	else 
+	  {
+	    fios.appendInputHeader(fitsin, countHead);
+	  }
       }
       if(printlev>0)
@@ -160,5 +177,5 @@
     
     // Decoding detection pass-band filter 
-    sr = getSpectralResponse(dc);
+    sr = getSpectralResponse(dc,fios);
     PrtTim(" After FilterCreation ");
     
@@ -203,10 +220,27 @@
 	  SphereHEALPix<float> ings(hp_nside);
 	  {
-	    FITS_SphereHEALPix<float> fiosIn(&ings);
-	    fiosIn.Read(flnm,2);
+	    FitsInFile fiosIn(flnm);
+	    fiosIn >> ings;
 	    //  Getting FITS keywords in primary header
 	    //  SKM_MergeFITSKeywords(flnm); 
 	    //  Getting FITS keywords in data object header
-	    SKM_MergeFITSKeywords2(ings.Info()); 
+	    //if(fiosIn.hasKeyword(string("PDMTYPE"), 1)) cout << "read the comment COMPMAP from " << flnm << endl;
+	    if(checkpdmtype)
+	      {
+		int status = 1;
+		string ch = "PDMTYPE";
+		string whatsup=fiosIn.getStringKeyword(1,ch,status);
+		if(!(status==0&&whatsup=="COMPMAP")) 
+		  {
+		    cerr << " !!!! skymixer - file" << flnm << " is not a COMPMAP " << endl;
+		    throw ParmError("Error");
+		  }
+		else cout << "read the comment COMPMAP from " << flnm << endl;
+	      }
+	    if(dvliston) SKM_MergeFITSKeywords2(ings.Info());
+	    else 
+	      {
+		fios.appendInputHeader(fiosIn, countHead);
+	      }
 	  }
 	  if (debuglev > 4) {  // Writing the input map to the outppf
@@ -229,5 +263,5 @@
 	    {
 	      if (es) { delete es; es = NULL; }
-	      es = getEmissionSpectra(dc, sk);
+	      es = getEmissionSpectra(dc, sk,fios);
 	      addComponent(*sr, outgs, ings, *es, K);
 	    }
@@ -244,10 +278,26 @@
 	      SphereHEALPix<float> betaMap(hp_nside);
 	      {
-		FITS_SphereHEALPix<float> fiosBM(&betaMap);
-		fiosBM.Read(flnm,2);
+		FitsInFile fiosBM(flnm);
+		fiosBM >> betaMap;
 		//  Getting FITS keywords in primary header
 		// SKM_MergeFITSKeywords(flnm); 
 		//  Getting FITS keywords in data object header
-		SKM_MergeFITSKeywords2(betaMap.Info()); 
+		//if(fiosBM.hasKeyword(string("COMPMAP"), 1)) cout << "read the comment COMPMAP from " << flnm << endl;
+		if(checkpdmtype)
+		  {
+		    int status = 1;
+		    string whatsup=fiosBM.getStringKeyword(1,"PDMTYPE",status);
+		    if(!(status==0&&whatsup=="INDEXMAP")) 
+		      {
+			cerr << " !!!! skymixer - file" << flnm << " is not an INDEXMAP " << endl;
+			throw ParmError("Error");
+		      }
+		    else cout << "read the comment INDEXMAP from " << flnm << endl;
+		  }
+		if(dvliston) SKM_MergeFITSKeywords2(betaMap.Info()); 
+		else 
+		  {
+		    fios.appendInputHeader(fiosBM, countHead);
+		  }
 	      }
 	      if (printlev > 2) {
@@ -309,5 +359,5 @@
 	<< (string)(arg[2]) << endl;
    {
-     FitsOutFile fios(arg[2]);
+     //     FitsOutFile fios(arg[2]);
      fios.firstImageOnPrimaryHeader(false); // Use secondary header 
      DVList& dvl = (*dvl_fitskw);
@@ -348,5 +398,5 @@
 
 /* Nouvelle-Fonction */
-int CheckCards(DataCards & dc, string & msg)
+int CheckCards(DataCards & dc, string & msg, FitsOutFile& fios)
 //   Function to check datacards
 {
@@ -368,5 +418,22 @@
      return(rc);
   }
-  dvl.SetS("SKYMIX", dc.GetParams("SKYMIX"));
+  if(dvliston)
+    dvl.SetS("SKYMIX", dc.GetParams("SKYMIX"));
+  else
+    {
+      fios.insertCommentLineOnHeader("--------------------");
+      fios.insertCommentLineOnHeader("SkyMixer DataCard");
+      fios.insertCommentLineOnHeader("--------------------");
+ 
+       string key = "SKYMIX"+ (string)dc.GetParams("SKYMIX");
+       fios.insertCommentLineOnHeader(key);
+    }
+
+  key = "CHECKPDMTYPE";
+  if (dc.HasKey(key)) {
+    checkpdmtype=true;
+  }
+
+  
   key = "READMAP";
   if (dc.HasKey(key)) {
@@ -377,5 +444,11 @@
     }
     else rdmap = true;
-    dvl.SetS(key, dc.GetParams(key));
+    
+    if(dvliston) dvl.SetS(key, dc.GetParams(key));
+    else 
+      {
+	string keynew = key + (string)dc.GetParams(key);
+	fios.insertCommentLineOnHeader(keynew);
+      }
   }
 
@@ -389,8 +462,31 @@
     }
 
-  if (dc.HasKey(key))   dvl.SetS("GAUSFILT", dc.GetParams(key));
-  if (dc.HasKey(key2))  dvl.SetS("FILTFILE", dc.GetParams(key2));
-  if (dc.HasKey(key3))  dvl.SetS("DIPOLE", dc.GetParams(key3));
-
+  if (dc.HasKey(key))   
+    { 
+      if(dvliston) dvl.SetS("GAUSFILT", dc.GetParams(key));
+      else 
+	{
+	  string keynew = key + (string)dc.GetParams(key);
+	  fios.insertCommentLineOnHeader(keynew);
+	}
+    }
+  if (dc.HasKey(key2))
+    {
+      if(dvliston) dvl.SetS("FILTFILE", dc.GetParams(key2));
+      else
+	{
+	  string keynew = key2 + (string)dc.GetParams(key2);
+	  fios.insertCommentLineOnHeader(keynew);
+	}
+    }
+  if (dc.HasKey(key3))  
+    {
+      if(dvliston) dvl.SetS("DIPOLE", dc.GetParams(key3));
+      else
+	{
+	  string keynew = key3 + (string)dc.GetParams(key3);
+	  fios.insertCommentLineOnHeader(keynew);
+	}
+    }
   // Decoding number of component and pixelisation parameter
   int mg = 32;
@@ -417,14 +513,30 @@
     sprintf(buff, "DIPOLE%d", kc+1);
     key3 = buff;
-    if (dc.NbParam(key) < 1 && dc.NbParam(key3)<1) {   
-      msg = "Missing or invalid card : " + key + " " + key2 + " " + key3;
-      pb = true;  break;
-    }
-
+    if (dc.NbParam(key) < 1 && (dc.NbParam(key3)<1)) 
+      {   
+	msg = "Invalid card or Missing card" + key + " or " + key3 ;
+	pb = true;  break;
+      }
+	
     sprintf(buff, "MAPFIL%d", kc+1);
-    if (dc.HasKey(key))   dvl.SetS(buff, dc.GetParams(key));
+    if (dc.HasKey(key))  
+      {
+	if(dvliston) dvl.SetS(buff, dc.GetParams(key));
+	else 
+	  {
+	    string keynew = buff + (string)dc.GetParams(key);
+	    fios.insertCommentLineOnHeader(keynew);
+	  }
+      }	
     sprintf(buff, "DIPOLE%d", kc+1);
-    if (dc.HasKey(key3))  dvl.SetS(buff, dc.GetParams(key3));
-
+    if (dc.HasKey(key3))  
+      {
+	if(dvliston) dvl.SetS(buff, dc.GetParams(key3));
+	else
+	  {
+	    string keynew = buff + (string)dc.GetParams(key3);
+	    fios.insertCommentLineOnHeader(keynew);
+	  }
+      }
     sprintf(buff, "SPECTRAFITSFILE%d", kc+1);
     key = buff;
@@ -440,5 +552,5 @@
     key6 = buff;
     if ( (dc.NbParam(key) < 3) && (dc.NbParam(key2) < 1) && (dc.NbParam(key3) < 6) && (dc.NbParam(key4)<2)
-	 &&  (dc.NbParam(key6)<1) &&  (dc.NbParam(key5)<3))  {   
+	 &&  (dc.NbParam(key6)<1) &&  (dc.NbParam(key5)<1))  {   
       msg = "Missing card or invalid parameters : " + key + " " + key2 + " " + key3 + " " + key4+ " " + key5;
       pb = true;  break;
@@ -446,17 +558,51 @@
 
     sprintf(buff, "SPECTF%d", kc+1);
-    if (dc.HasKey(key))   dvl.SetS(buff, dc.GetParams(key));
+    if (dc.HasKey(key)) {   
+      if(dvliston) dvl.SetS(buff, dc.GetParams(key));
+      else { 
+	string keynew = buff + (string)dc.GetParams(key);
+	fios.insertCommentLineOnHeader(keynew);}
+    }
     sprintf(buff, "BLACKB%d", kc+1);
-    if (dc.HasKey(key2))  dvl.SetS(buff, dc.GetParams(key2));
+    if (dc.HasKey(key2)) {
+      if(dvliston) dvl.SetS(buff, dc.GetParams(key2));
+      else { 
+	string keynew = buff + (string)dc.GetParams(key2);
+	fios.insertCommentLineOnHeader(keynew);}
+    }
+
     sprintf(buff, "PLAWSP%d", kc+1);
-    if (dc.HasKey(key3))  dvl.SetS(buff, dc.GetParams(key3));
+    if (dc.HasKey(key3)) {
+      if(dvliston) dvl.SetS(buff, dc.GetParams(key3));
+      else { 
+	string keynew = buff + (string)dc.GetParams(key3);
+	fios.insertCommentLineOnHeader(keynew);}
+    }
+
     sprintf(buff, "BETAFI%d", kc+1);
-    if (dc.HasKey(key4))  dvl.SetS(buff, dc.GetParams(key4));
+    if (dc.HasKey(key4))  {
+      if(dvliston) dvl.SetS(buff, dc.GetParams(key4));
+      else { 
+	string keynew = buff + (string)dc.GetParams(key4);
+	fios.insertCommentLineOnHeader(keynew);}
+    }
+
     sprintf(buff, "DIPOLE%d", kc+1);
-    if (dc.HasKey(key5))  dvl.SetS(buff, dc.GetParams(key5));
+    if (dc.HasKey(key5))  {
+      if(dvliston) dvl.SetS(buff, dc.GetParams(key5));
+      else { 
+	string keynew = buff + (string)dc.GetParams(key5);
+	fios.insertCommentLineOnHeader(keynew);}
+    }
+
     sprintf(buff, "DERIBB%d", kc+1);
-    if (dc.HasKey(key6))  dvl.SetS(buff, dc.GetParams(key6));
-
-    }
+    if (dc.HasKey(key6))  {
+      if(dvliston) dvl.SetS(buff, dc.GetParams(key6));
+      else { 
+	string keynew = buff + (string)dc.GetParams(key6);
+	fios.insertCommentLineOnHeader(keynew);}
+    }
+    
+  }
 
   if (pb)  {
@@ -494,5 +640,5 @@
 
 /* Nouvelle-Fonction */
-SpectralResponse * getSpectralResponse(DataCards & dc)
+SpectralResponse * getSpectralResponse(DataCards & dc, FitsOutFile& fios)
 {
   SpectralResponse * filt = NULL;
@@ -513,5 +659,20 @@
     //    SKM_MergeFITSKeywords(ifnm); 
     //  Getting FITS keywords in data object header
-    SKM_MergeFITSKeywords2(mtx.Info()); 
+    if(checkpdmtype)
+      {
+	int status = 1;
+	string whatsup=fiis.getStringKeyword(1,"PDMTYPE",status);
+	if(!(status==0&&whatsup=="COMPMAP")) 
+	  {
+	    cerr << " !!!! skymixer - file" << ifnm << " is not a COMPMAP " << endl;
+	    throw ParmError("Error");
+	  }
+	else cout << "read the comment COMPMAP from " << ifnm << endl;
+      }
+    if(dvliston) SKM_MergeFITSKeywords2(mtx.Info()); 
+    else 
+      {
+	fios.appendInputHeader(fiis, 2);
+      }
     double numin = dc.DParam(key, 2, 1.);
     double numax = dc.DParam(key, 3, 9999.);
@@ -547,5 +708,5 @@
 
 /* Nouvelle-Fonction */
-RadSpectra * getEmissionSpectra(DataCards & dc, int nc)
+RadSpectra * getEmissionSpectra(DataCards & dc, int nc, FitsOutFile& fios)
 {
   char numb[16];
@@ -569,5 +730,21 @@
     //    SKM_MergeFITSKeywords(ifnm); 
     //  Getting FITS keywords in data object header
-    SKM_MergeFITSKeywords2(mtx.Info()); 
+    //if(fiis.hasKeyword(string("PDMTYPE"), 1)) cout << "read the comment COMPMAP from " << ifnm << endl;
+    if(checkpdmtype)
+      {
+	int status = 1;
+	string whatsup=fiis.getStringKeyword(1,"PDMTYPE",status);
+	if(!(status==0&&whatsup=="FGRSPEC")) 
+	  {
+	    cerr << " !!!! skymixer - file" << ifnm << " is not a  FGRSPEC" << endl;
+	    throw ParmError("Error");
+	  }
+	else cout << "read the comment FGRSPEC from " << ifnm << endl;
+      }
+    if(dvliston) SKM_MergeFITSKeywords2(mtx.Info()); 
+    else 
+      {
+	fios.appendInputHeader(fiis, 2);
+      }
     double numin = dc.DParam(key, 2, 1.);
     double numax = dc.DParam(key, 3, 9999.);
