Go to the documentation of this file.00001
00010 #include "Mille.h"
00011
00012 #include <iostream>
00013
00014
00015
00016 Mille::Mille(const char *outFileName, bool asBinary, bool writeZero) :
00017 myOutFile(outFileName, (asBinary ? (std::ios::binary | std::ios::out) : std::ios::out)),
00018 myAsBinary(asBinary), myWriteZero(writeZero), myBufferPos(-1), myHasSpecial(false)
00019 {
00020
00021
00022
00023
00024 myBufferInt[0] = 0;
00025 myBufferFloat[0] = 0.;
00026
00027 if (!myOutFile.is_open()) {
00028 std::cerr << "Mille::Mille: Could not open " << outFileName
00029 << " as output file." << std::endl;
00030 }
00031 }
00032
00033
00034
00035 Mille::~Mille()
00036 {
00037
00038 myOutFile.close();
00039 }
00040
00041
00042
00043 void Mille::mille(int NLC, const float *derLc,
00044 int NGL, const float *derGl, const int *label,
00045 float rMeas, float sigma)
00046 {
00047 if (sigma <= 0.) return;
00048 if (myBufferPos == -1) this->newSet();
00049 if (!this->checkBufferSize(NLC, NGL)) return;
00050
00051
00052 ++myBufferPos;
00053 myBufferFloat[myBufferPos] = rMeas;
00054 myBufferInt [myBufferPos] = 0;
00055
00056
00057 for (int i = 0; i < NLC; ++i) {
00058 if (derLc[i] || myWriteZero) {
00059 ++myBufferPos;
00060 myBufferFloat[myBufferPos] = derLc[i];
00061 myBufferInt [myBufferPos] = i+1;
00062 }
00063 }
00064
00065
00066 ++myBufferPos;
00067 myBufferFloat[myBufferPos] = sigma;
00068 myBufferInt [myBufferPos] = 0;
00069
00070
00071 for (int i = 0; i < NGL; ++i) {
00072 if (derGl[i] || myWriteZero) {
00073 if ((label[i] > 0 || myWriteZero) && label[i] <= myMaxLabel) {
00074 ++myBufferPos;
00075 myBufferFloat[myBufferPos] = derGl[i];
00076 myBufferInt [myBufferPos] = label[i];
00077 } else {
00078 std::cerr << "Mille::mille: Invalid label " << label[i]
00079 << " <= 0 or > " << myMaxLabel << std::endl;
00080 }
00081 }
00082 }
00083 }
00084
00085
00086 void Mille::special(int nSpecial, const float *floatings, const int *integers)
00087 {
00088 if (nSpecial == 0) return;
00089 if (myBufferPos == -1) this->newSet();
00090 if (myHasSpecial) {
00091 std::cerr << "Mille::special: Special values already stored for this record."
00092 << std::endl;
00093 return;
00094 }
00095 if (!this->checkBufferSize(nSpecial, 0)) return;
00096 myHasSpecial = true;
00097
00098
00099
00100
00101
00102
00103
00104 ++myBufferPos;
00105 myBufferFloat[myBufferPos] = 0.;
00106 myBufferInt [myBufferPos] = 0;
00107
00108 ++myBufferPos;
00109 myBufferFloat[myBufferPos] = -nSpecial;
00110 myBufferInt [myBufferPos] = 0;
00111
00112 for (int i = 0; i < nSpecial; ++i) {
00113 ++myBufferPos;
00114 myBufferFloat[myBufferPos] = floatings[i];
00115 myBufferInt [myBufferPos] = integers[i];
00116 }
00117 }
00118
00119
00120
00121 void Mille::kill()
00122 {
00123
00124 myBufferPos = -1;
00125 }
00126
00127
00128
00129 void Mille::end()
00130 {
00131
00132 if (myBufferPos > 0) {
00133 const int numWordsToWrite = (myBufferPos + 1)*2;
00134
00135 if (myAsBinary) {
00136 myOutFile.write(reinterpret_cast<const char*>(&numWordsToWrite),
00137 sizeof(numWordsToWrite));
00138 myOutFile.write(reinterpret_cast<char*>(myBufferFloat),
00139 (myBufferPos+1) * sizeof(myBufferFloat[0]));
00140 myOutFile.write(reinterpret_cast<char*>(myBufferInt),
00141 (myBufferPos+1) * sizeof(myBufferInt[0]));
00142 } else {
00143 myOutFile << numWordsToWrite << "\n";
00144 for (int i = 0; i < myBufferPos+1; ++i) {
00145 myOutFile << myBufferFloat[i] << " ";
00146 }
00147 myOutFile << "\n";
00148
00149 for (int i = 0; i < myBufferPos+1; ++i) {
00150 myOutFile << myBufferInt[i] << " ";
00151 }
00152 myOutFile << "\n";
00153 }
00154 }
00155 myBufferPos = -1;
00156 }
00157
00158
00159
00160 void Mille::newSet()
00161 {
00162
00163 myBufferPos = 0;
00164 myHasSpecial = false;
00165 myBufferFloat[0] = 0.0;
00166 myBufferInt [0] = 0;
00167 }
00168
00169
00170
00171 bool Mille::checkBufferSize(int nLocal, int nGlobal)
00172 {
00173
00174
00175 if (myBufferPos + nLocal + nGlobal + 2 >= myBufferSize) {
00176 ++(myBufferInt[0]);
00177 std::cerr << "Mille::checkBufferSize: Buffer too short ("
00178 << myBufferSize << "),"
00179 << "\n need space for nLocal (" << nLocal<< ")"
00180 << "/nGlobal (" << nGlobal << ") local/global derivatives, "
00181 << myBufferPos + 1 << " already stored!"
00182 << std::endl;
00183 return false;
00184 } else {
00185 return true;
00186 }
00187 }