00001 
00002 
00003 
00004 
00005 
00006 template <typename DataType>
00007 void DataDumper::dumpAsCsv(TableData<DataType>& aData, std::ostream& aOut)
00008 {
00009   for (unsigned rowNo = 0; rowNo < aData.qNumRows(); ++rowNo)
00010   {
00011     
00012     
00013     if (isUnique(aData, rowNo))
00014     {
00015       for (unsigned colNo = 0; colNo < aData.qNumCols() - 1; ++colNo)
00016       {
00017         formatOut(aData.at(colNo, rowNo), mInfo.getFormat(colNo), aOut);
00018         aOut << ",";
00019       }
00020       formatOut(aData.at(aData.qNumCols()-1, rowNo),
00021                 mInfo.getFormat(aData.qNumCols()-1),
00022                 aOut);
00023       aOut << std::endl;
00024     }
00025   }
00026   
00027   mKeyCount.clear();
00028 }
00029 
00031 template <typename DataType>
00032 void DataDumper::addDefaultRow(TableData<DataType>& aData, FitsFile& fcat)
00033 {
00034   unsigned mNumCols = mInfo.getNumAttributes();
00035   unsigned mNumRows = aData.qNumRows();
00036   unsigned mNumExts = aData.qNumExts();
00037   int rowNum = 0;
00038   unsigned outRow = -1;
00039   int row1 = 0;
00040   
00041   TableData<Numeric>* tmpData = new TableData<Numeric>(mNumCols, mNumRows);
00042   tmpData -> setNumExts(mNumExts);
00043   
00044   for (int hduNum = 2; hduNum <= fcat.getNumHdus(); ++hduNum)
00045   {
00046     fcat.movabsHdu(hduNum);
00047     int hduRowNum = fcat.getNumRows();
00048     if (hduRowNum > 0)
00049     {
00050       outRow += 1;
00051       for (unsigned col = 0; col < mNumCols; ++col)
00052       {
00053         if (mInfo.getName(col) == ExpectNames::multiframeID ||
00054             mInfo.getName(col) == ExpectNames::extNum ||
00055             mInfo.getName(col) == ExpectNames::filterID ||
00056             mInfo.getName(col) == ExpectNames::cuEventID)
00057         {
00058           Numeric numval;
00059           aData.value(col, rowNum, numval);
00060           tmpData->assign(col, outRow, numval);
00061         }
00062         else 
00063         {
00064           string format = mInfo.getFormat(col);
00065           if (format == "bigint")
00066           {
00067             tmpData->assign(col, outRow, DefaultValues::bigintdef);
00068           }
00069           else if (format == "tinyint")
00070           {
00071             tmpData->assign(col, outRow, DefaultValues::tinyintdef);
00072           }
00073           else if (format == "int")
00074           {
00075             tmpData->assign(col, outRow, DefaultValues::intdef);
00076           }
00077           else if (format == "smallint")
00078           {
00079             tmpData->assign(col, outRow, DefaultValues::smallintdef);
00080           }
00081           else if (format == "real" || format == "float")
00082           {
00083             tmpData->assign(col, outRow, DefaultValues::realdef);
00084           }
00085           else if (format.find("varchar") != string::npos)
00086           {
00087             unsigned varcharLen = 0;
00088             int posa = format.find("(") + 1;
00089             int posb = format.rfind(")");
00090             varcharLen = atoi(format.substr(posa, posb - posa).c_str());
00091             if (varcharLen == 1)
00092             {
00093               tmpData->assign(col, outRow, DefaultValues::varchar1def);
00094             } else if (varcharLen == 2) {
00095               tmpData->assign(col, outRow, DefaultValues::varchar2def);
00096             } else if (varcharLen == 3) {
00097               tmpData->assign(col, outRow, DefaultValues::varchar3def);
00098             } else if (varcharLen > 3) {
00099               tmpData->assign(col, outRow, DefaultValues::varchar4def);
00100             }
00101           }
00102           else if (format == "datetime")
00103           {
00104             tmpData->assign(col, outRow, DefaultValues::datetimedef);
00105           }
00106           string defValue = mInfo.getDefault(col);
00107           if (defValue.length() > 0)
00108           {
00109             tmpData->assign(col, outRow, defValue);
00110           }
00111         }
00112       }
00113       int row2 = row1 + hduRowNum;
00114       if (row2 != row1)
00115       {
00116         for (int rowNo = row1; rowNo < row2; ++rowNo)
00117         {
00118           outRow += 1;
00119           for (unsigned col = 0; col < mNumCols; ++col)
00120           {
00121             Numeric numval;
00122             if (aData.isDefined(col, rowNo))
00123             {
00124               aData.value(col, rowNo, numval);
00125               tmpData->assign(col, outRow, numval);
00126             } else {
00127               tmpData->setundef(col, outRow);
00128             }
00129           }
00130         }
00131         row1 = row2;
00132       }
00133     }
00134     rowNum += hduRowNum;
00135   }
00136   row1 = 0;
00137   for (int hduNum = 2; hduNum <= fcat.getNumHdus(); ++hduNum)
00138   {
00139     fcat.movabsHdu(hduNum);
00140     int hduRowNum = fcat.getNumRows();
00141     int row2 = row1 + hduRowNum + 1;
00142     if (row2 != row1)
00143     {
00144       for (int rowNo = row1; rowNo < row2; ++rowNo)
00145       {
00146         for (unsigned col = 0; col < mNumCols; ++col)
00147         {
00148           if (tmpData->isDefined(col, rowNo))
00149           {
00150             Numeric numval;
00151             tmpData->value(col, rowNo, numval);
00152             aData.assign(col, rowNo, numval);
00153           }
00154         }
00155       }
00156       row1 = row2;
00157     }
00158   }
00159 }
00160 
00161 
00162 template <typename DataType>
00163 bool DataDumper::isUnique(TableData<DataType>& aData, unsigned aRow)
00164 {
00165   using namespace std;
00166   
00167   
00168   
00169   string key;
00170   for (list<int>::iterator colConstItr = mColConstList.begin();
00171                            colConstItr != mColConstList.end();
00172                            ++colConstItr)
00173   {
00174     string val;
00175     aData.value(*colConstItr, aRow, val);
00176     key += val;
00177   }
00178 
00179   
00180   mKeyCount[key] += 1;
00181 
00182   return (mKeyCount[key] == 1 ||  mColConstList.empty());
00183 }
00184