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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
/***********************************************/
/**
* @file archiveBinary.cpp
*
* @brief Read/write archive files in binary format.
*
* @author Torsten Mayer-Guerr
* @date 2005-01-11
*
*/
/***********************************************/
#include "base/importStd.h"
#include "base/doodson.h"
#include "base/sphericalHarmonics.h"
#include "base/gnssType.h"
#include "archive.h"
#include "archiveBinary.h"
/***********************************************/
OutArchiveBinary::OutArchiveBinary(std::ostream &_stream, const std::string &type, UInt version) : stream(_stream)
{
stream<<"B";
save(type);
std::stringstream ss;
ss<<version;
save(ss.str());
// padding to 64 bit
// size computation is probably wrong, but we keep it for compatibility reasons.
std::string empty((1+type.size()+ss.str().size())%8, ' ');
if(empty.size())
stream.write(&empty.at(0), empty.size()*sizeof(char));
}
/***********************************************/
InArchiveBinary::InArchiveBinary(std::istream &_stream) : stream(_stream), _version(0)
{
char c;
stream>>c;
if(c=='b')
{
oldVersion = TRUE;
return; // old version
}
if(c!='B')
throw(Exception("in InArchiveBinary: magic byte expected"));
oldVersion = FALSE;
std::string _versionStr;
load(typeStr);
load(_versionStr);
_version = versionStr2version(_versionStr);
// padding to 64 bit
// size computation is probably wrong, but we keep it for compatibility reasons.
std::string empty((1+typeStr.size()+_versionStr.size())%8, ' ');
if(empty.size())
stream.read(&empty.at(0), empty.size()*sizeof(char));
}
/***********************************************/
void OutArchiveBinary::save(const Double &x){stream.write(reinterpret_cast<const char*>(&x), sizeof(Double));}
void InArchiveBinary::load(Double &x) {stream.read(reinterpret_cast<char*>(&x), sizeof(Double));}
/***********************************************/
void OutArchiveBinary::save(const Angle &x) {save(static_cast<Double>(x));}
void InArchiveBinary::load(Angle &x) {Double w; load(w); x=Angle(w);}
/***********************************************/
/***********************************************/
void OutArchiveBinary::save(const Int &x)
{
Int64 y = static_cast<Int64>(x);
stream.write(reinterpret_cast<const char*>(&y), sizeof(Int64));
}
/***********************************************/
void InArchiveBinary::load(Int &x)
{
if(oldVersion)
{
Int32 y;
stream.read(reinterpret_cast<char*>(&y), sizeof(Int32));
x = static_cast<Int>(y);
}
else
{
Int64 y;
stream.read(reinterpret_cast<char*>(&y), sizeof(Int64));
x = static_cast<Int>(y);
}
}
/***********************************************/
/***********************************************/
void OutArchiveBinary::save(const UInt &x)
{
UInt64 y = static_cast<UInt64>(x);
stream.write(reinterpret_cast<const char*>(&y), sizeof(UInt64));
}
/***********************************************/
void InArchiveBinary::load(UInt &x)
{
if(oldVersion)
{
UInt32 y;
stream.read(reinterpret_cast<char*>(&y), sizeof(UInt32));
x = static_cast<UInt>(y);
}
else
{
UInt64 y;
stream.read(reinterpret_cast<char*>(&y), sizeof(UInt64));
x = static_cast<UInt>(y);
}
}
/***********************************************/
/***********************************************/
void OutArchiveBinary::save(const Bool &x)
{
UInt64 y = static_cast<UInt64>(x);
stream.write(reinterpret_cast<const char*>(&y), sizeof(UInt64));
}
/***********************************************/
void InArchiveBinary::load(Bool &x)
{
if(oldVersion)
stream.read(reinterpret_cast<char*>(&x), sizeof(Bool));
else
{
UInt64 y;
stream.read(reinterpret_cast<char*>(&y), sizeof(UInt64));
x = static_cast<Bool>(y);
}
}
/***********************************************/
/***********************************************/
void OutArchiveBinary::save(const std::string &x)
{
save(static_cast<UInt>(x.size()));
if(x.size())
stream.write(&x.at(0), x.size()*sizeof(char));
}
/***********************************************/
void InArchiveBinary::load(std::string &x)
{
UInt size;
load(size);
x.resize(size);
if(x.size())
stream.read(&x.at(0), size*sizeof(char));
}
/***********************************************/
/***********************************************/
void OutArchiveBinary::save(const Time &x)
{
save(x.mjdInt());
save(x.mjdMod());
}
/***********************************************/
void InArchiveBinary::load(Time &x)
{
if(oldVersion)
{
Int mjd, milli;
load(mjd);
load(milli);
x = Time(mjd,milli/(1000.*60*60*24));
}
else
{
Int mjdInt;
Double mjdMod;
load(mjdInt);
load(mjdMod);
x = Time(mjdInt, mjdMod);
}
}
/***********************************************/
/***********************************************/
void OutArchiveBinary::save(const Doodson &x)
{
for(UInt i=0; i<6; i++)
save(x.d[i]);
}
/***********************************************/
void InArchiveBinary::load(Doodson &x)
{
if(oldVersion)
{
throw(Exception("Old GROOPS binary file is not supported anymore."));
}
else
{
for(UInt i=0; i<6; i++)
load(x.d[i]);
}
}
/***********************************************/
/***********************************************/
void OutArchiveBinary::save(const GnssType &x)
{
UInt64 y = static_cast<UInt64>(x.type);
stream.write(reinterpret_cast<const char*>(&y), sizeof(UInt64));
}
/***********************************************/
void InArchiveBinary::load(GnssType &x)
{
UInt64 y;
stream.read(reinterpret_cast<char*>(&y), sizeof(UInt64));
x.type = y;
}
/***********************************************/
/***********************************************/
void OutArchiveBinary::save(const Vector &x)
{
Matrix A = x;
save(A);
}
/***********************************************/
void InArchiveBinary::load(Vector &x)
{
if(oldVersion)
{
UInt rows;
load(rows);
x = Vector(rows);
if(x.rows()!=0)
stream.read(reinterpret_cast<char*>(x.field()), x.size()*sizeof(Double));
}
else
{
Matrix A;
load(A);
x = A;
}
}
/***********************************************/
/***********************************************/
void OutArchiveBinary::save(const const_MatrixSlice &x)
{
save(static_cast<UInt>(x.getType()));
if(x.getType()==Matrix::GENERAL)
{
save(x.rows()); save(x.columns());
if(x.size()==0)
return;
if((!x.isRowMajorOrder()) && (x.rows()==x.ld()))
stream.write(reinterpret_cast<const char*>(x.const_field()), x.size()*sizeof(Double));
else if(!x.isRowMajorOrder())
for(UInt s=0; s<x.columns(); s++)
stream.write(reinterpret_cast<const char*>(x.const_field()+s*x.ld()), x.rows()*sizeof(Double));
else
for(UInt s=0; s<x.columns(); s++)
for(UInt z=0; z<x.rows(); z++)
save(x(z,s));
}
else
{
UInt uplo = (x.isUpper()) ? 0 : 1; // compability to old version
save(uplo); save(x.rows());
if(x.size()==0)
return;
if(!x.isRowMajorOrder())
{
if(x.isUpper())
for(UInt s=0; s<x.columns(); s++)
stream.write(reinterpret_cast<const char*>(x.const_field()+s*x.ld()), (s+1)*sizeof(Double));
else
for(UInt s=0; s<x.columns(); s++)
stream.write(reinterpret_cast<const char*>(x.const_field()+(s*x.ld()+s)), (x.rows()-s)*sizeof(Double));
}
else
{
if(x.isUpper())
for(UInt s=0; s<x.columns(); s++)
for(UInt z=0; z<=s; z++)
save(x(z,s));
else
for(UInt s=0; s<x.columns(); s++)
for(UInt z=s; z<x.rows(); z++)
save(x(z,s));
}
}
}
/***********************************************/
void InArchiveBinary::load(Matrix &x)
{
UInt type;
load(type);
if(static_cast<Matrix::Type>(type)==Matrix::GENERAL)
{
UInt rows, columns;
load(rows); load(columns);
x = Matrix(rows, columns, Matrix::NOFILL);
if(x.size()==0)
return;
stream.read(reinterpret_cast<char*>(x.field()), x.size()*sizeof(Double));
}
else
{
UInt dim, uplo;
load(uplo); load(dim);
x = Matrix(dim, static_cast<Matrix::Type>(type), ((uplo==1) ? Matrix::LOWER : Matrix::UPPER));
if(x.size()==0)
return;
if(uplo==0)
{
for(UInt s=0; s<x.columns(); s++)
stream.read(reinterpret_cast<char*>(&x(0,s)), (s+1)*sizeof(Double));
}
else
{
for(UInt s=0; s<x.columns(); s++)
stream.read(reinterpret_cast<char*>(&x(s,s)), (x.rows()-s)*sizeof(Double));
}
}
}
/***********************************************/
/***********************************************/
void OutArchiveBinary::save(const SphericalHarmonics &harm)
{
save(harm.GM());
save(harm.R());
save(harm.cnm());
save(harm.snm());
save(harm.sigma2cnm());
save(harm.sigma2snm());
}
/***********************************************/
void InArchiveBinary::load(SphericalHarmonics &harm)
{
Double GM, R;
Matrix cnm, snm, sigma2cnm, sigma2snm;
load(GM);
load(R);
load(cnm);
load(snm);
load(sigma2cnm);
load(sigma2snm);
harm = SphericalHarmonics(GM, R, cnm, snm, sigma2cnm, sigma2snm);
}
/***********************************************/
|