Commit c21879c6 authored by jclamber's avatar jclamber

add STREAMing reading variable

git-svn-id: http://svn.oamp.fr/repos/unsio/trunk@221 ce2cc22f-6084-46ce-a062-084b172ee5dc
parent d7b446c8
......@@ -276,6 +276,82 @@ template <class T> int CSnapshotGadgetIn::readOneArray(T ** data, const int comp
assert(in.good() && len1==len2 && len1==bytes_counter);
return 1;
}
// ============================================================================
template <class T> int CSnapshotGadgetIn::readStreamBlock(const std::string req_blockname, int * nbody,T ** data)
{
if (is_read) { // file has already been read
close();
}
int fail = open(filename);
if (fail) assert(0); // fail is true abort
assert(sizeof(T)==sizeof(float)); // works only for float NOW
int last_len=0;
// loop on all the files
for (int i=0; i<header.num_files || (i==0 && header.num_files==0);i++) {
std::string infile;
if (header.num_files > 0 ) { // more than one file
std::ostringstream stm;
stm << "." << i; // add ".XX" extension
infile = filename + stm.str(); // new filename
if (i>0) {
close(); // close previous file
int fail=open(infile); // open new file,read header
if (fail) assert(0); // fail is true abort
}
}
else infile=filename; // lonely file
bool stop=false;
bool ok=false;
while(readBlockName() && !stop) {
if (req_blockname == block_name) { // found requested block name
bytes_counter=0;
int len1 = readFRecord();
if (*data==NULL) { // first time
*data = new T[len1/sizeof(T)]; // !! TO CHECK
}
else {
if (i>0) { // more than one file
T * tmp = new T[(last_len+len1)/sizeof(T)]; // upgrade array
mempcpy((T*) tmp,(T*) *data,last_len/sizeof(T)); // copy old data
delete [] (*data); // delete previous array
*data = tmp; // assign new array
} else {
std::cerr << "WARNING, pointer not allocated...\n";
}
}
T * ptr = *data;
readData((char *) &ptr[last_len], sizeof(T), len1/sizeof(T) );
int len2 = readFRecord();
if (len2==len1) ; // remove warning....
assert(in.good() && len1==len2 && len1==bytes_counter);
last_len += len1;
stop=true; // we must stop
ok=true;
} else {
if (!ok) {
if (in.eof()) { // it's end of file, must stop loop
stop=true;
}
else {
skipBlock();
}
}
}
}
}
if (last_len>0) { // found something
*nbody=last_len/sizeof(T);
}
return 1;
}
// ============================================================================
int CSnapshotGadgetIn::read(uns::UserSelection &user_select)
......@@ -743,6 +819,8 @@ int CSnapshotGadgetIn::readHeader(const int id)
}
storeComponents();
}
in_start_block = in.tellg(); // save poistion of input file
return 0;
}
// ============================================================================
......@@ -1009,7 +1087,15 @@ bool CSnapshotGadgetIn::getData(const std::string comp, std::string name, int *n
ok=false;
}
break;
default: ok=false;
default: // unkown name
if (comp=="STREAM") { // data stream reading
int status=readStreamBlock(name,n,data);
if (status>0) {
ok=true;
}
} else {
ok=false;
}
}
if (ok && !*data &&
(CunsOut::s_mapStringValues[name]!=uns::Nbody &&
......
......@@ -21,6 +21,8 @@
#include <assert.h>
#include <fstream>
#include <map>
#include <iostream>
#include <sstream>
#include "snapshotinterface.h"
namespace uns {
......@@ -135,7 +137,8 @@ typedef struct particle_data_lite
const uns::ComponentRangeVector getCRV() const { return crv;}
int getNtotal() const { return npartTotal;}
std::string filename,file0;
std::ifstream in;
std::ifstream in; // input descriptor
std::ios::pos_type in_start_block;
int multiplefiles;
bool lonely_file;
......@@ -213,6 +216,8 @@ typedef struct particle_data_lite
const int dim, const int nsel);
template <class T> int readGasStarsUnknownArray(T ** data, int * n,const int * compOffset);
template <class T> int readOneArray(T ** data, const int compid,const int * compOffset);
template <class T> int readStreamBlock(const std::string blockname, int * nbody,T ** data);
bool readBlockName();
std::string block_name;
int readHeader(const int);
......
......@@ -79,6 +79,11 @@ int main(int argc, char ** argv )
// get MASS from input snapshot
ok=uns->snapshot->getData("mass",&cnbody,&mass);
float * U;
ok=uns->snapshot->getData("STREAM","U",&cnbody,&U);
ok=uns->snapshot->getData("mass",&cnbody,&mass);
std::cerr << "nbody=" << nbody << " time="<<time <<"\n";
// OUTPUT operations
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment