Commit 6ff138f6 authored by jclamber's avatar jclamber

add writing support to py_unsio

git-svn-id: http://svn.oamp.fr/repos/unsio/trunk@171 ce2cc22f-6084-46ce-a062-084b172ee5dc
parent 87b27f0a
......@@ -49,6 +49,41 @@
%rename(getArrayI) getData(const std::string,const std::string,int *,int **);
%rename(getArrayI) getData(const std::string,int *,int **);
// OUTPUT operations
//%apply float *INPUT { float * fvalue };
//%apply int *INPUT { int * ivalue };
%apply (int DIM1 , float * INPLACE_ARRAY1) {( int size, float * farray)};
%apply (int DIM1 , int * INPLACE_ARRAY1) {( int size, int * iarray)};
// rename methods because of overloading limitations with swig c++
%rename(setValueF) setData(const std::string,float);
%rename(setArrayF_do_not_used) setData(const std::string,const std::string,int ,float *,const bool _addr=false);
%rename(setArrayF_do_not_used) setData(const std::string,int ,float *,const bool _addr=false);
%rename(setValueI) setData(const std::string,int,const bool _addr=false);
%rename(setArrayI) setData(const std::string,const std::string,int ,int *,const bool _addr=false);
%rename(setArrayI) setData(const std::string,int ,int *);
// Parse the original header file
%include "uns.h"
// Extend class
%extend uns::CunsOut {
// we rewrite setArrayF because numpy array size is different from nbody for 3D arrays
int setArrayF(const std::string comp,const std::string prop,
int size,float * farray, const bool _addr=false) {
if (prop=="pos" || prop=="vel" || prop=="acc") size /= 3;
int status = $self->snapshot->setData(comp,prop,size,farray,_addr);
return status;
}
// we rewrite setArrayF because numpy array size is different from nbody for 3D arrays
int setArrayF(const std::string prop,
int size,float * farray, const bool _addr=false) {
if (prop=="pos" || prop=="vel" || prop=="acc") size /= 3;
int status = $self->snapshot->setData(prop,size,farray,_addr);
return status;
}
};
......@@ -1513,51 +1513,59 @@ int CSnapshotGadgetOut::setData(std::string name,std::string array, const int n
bool ok=true;
int status=0;
switch(CunsOut::s_mapStringValues[array]) {
case uns::Pos :
status = setPos(name, n, data, _addr);
break;
case uns::Vel :
status = setVel(name, n, data, _addr);
break;
case uns::Mass :
status = setMass(name, n, data, _addr);
break;
case uns::Pot :
status = setPot(name, n, data, _addr);
break;
case uns::Acc :
status = setAcc(name, n, data, _addr);
break;
case uns::Hsml :
status = setHsml(n, data, _addr);
break;
case uns::Rho :
status = setRho(n, data, _addr);
break;
case uns::U :
status = setU(n, data, _addr);
break;
case uns::Temp :
status = setTemp(n, data, _addr);
break;
case uns::Age :
status = setAge(n, data, _addr);
break;
case uns::GasMetal :
status = setMetalGas(n, data, _addr);
break;
case uns::StarsMetal :
status = setMetalStars(n, data, _addr);
break;
default: ok=false;
}
if ( name == "EXTRA" ) { // it's an extra data
status = setExtra(array,n, data, _addr);
} else {
switch(CunsOut::s_mapStringValues[array]) {
case uns::Pos :
status = setPos(name, n, data, _addr);
break;
case uns::Vel :
status = setVel(name, n, data, _addr);
break;
case uns::Mass :
status = setMass(name, n, data, _addr);
break;
case uns::Pot :
status = setPot(name, n, data, _addr);
break;
case uns::Acc :
status = setAcc(name, n, data, _addr);
break;
case uns::Hsml :
status = setHsml(n, data, _addr);
break;
case uns::Rho :
status = setRho(n, data, _addr);
break;
case uns::U :
status = setU(n, data, _addr);
break;
case uns::Temp :
status = setTemp(n, data, _addr);
break;
case uns::Age :
status = setAge(n, data, _addr);
break;
case uns::GasMetal :
status = setMetalGas(n, data, _addr);
break;
case uns::StarsMetal :
status = setMetalStars(n, data, _addr);
break;
default: ok=false;
}
}
if (verbose) {
if (ok) {
std::cerr << "CSnapshotGadgetOut::setData name["<<name<<"]=" << CunsOut::s_mapStringValues[name] << "\n";
} else {
std::cerr << "** WARNING ** CSnapshotGadgetOut::setData Value ["<<name<<"] does not exist.....\n";
if (name != "EXTRA") {
std::cerr << "** WARNING ** CSnapshotGadgetOut::setData Value ["<<name<<"] does not exist.....\n";
} else {
std::cerr << "CSnapshotGadgetOut::setData EXTRA tags["<<array<<"]\n";
}
}
}
return status;
......@@ -1830,8 +1838,8 @@ int CSnapshotGadgetOut::setPos(std::string name, const int _n, float * _pos, con
// ============================================================================
// setRho:
int CSnapshotGadgetOut::setRho(const int _n, float * _rho, const bool addr)
{
assert(_n==header.npart[0]);; // #rho particles = #gas particles
{
assert(_n==header.npart[0]);; // #rho particles = #gas particles
if (addr) { // map address
rho = _rho;
}
......@@ -1952,6 +1960,15 @@ int CSnapshotGadgetOut::setPos(std::string name, const int _n, float * _pos, con
bits |= AGE_BIT;
return 1;
}
// ============================================================================
// setExtra:
int CSnapshotGadgetOut::setExtra(std::string tag, const int _n, float * _data, const bool addr)
{
s_mapStringVector[tag].clear();
s_mapStringVector[tag].resize(_n);
memcpy((float *) &s_mapStringVector[tag][0],_data,sizeof(float)*_n);
return 1;
}
//// ============================================================================
//// saveDataMPV:
// int CSnapshotGadgetOut::saveDataMPV(const int _n, const float * _m, const float * _p, const float * _v)
......@@ -2303,6 +2320,16 @@ int CSnapshotGadgetOut::write()
writeData((char *) age, sizeof(float), header.npart[4]);
writeFRecord(blk);
}
// Write EXTRA
std::map<std::string,std::vector <float> >::const_iterator it;
for (it = s_mapStringVector.begin(); it != s_mapStringVector.end(); it++) {
std::cerr << "Saving EXTRA Tag=["<< it->first << "] of size="<<it->second.size()<<"\n";
blk=sizeof(float)*it->second.size();
writeBlockName(it->first,blk);
writeFRecord(blk);
writeData((char *) &s_mapStringVector[it->first][0] , sizeof(float), it->second.size());
writeFRecord(blk);
}
return 1;
}
// ============================================================================
......@@ -2313,9 +2340,13 @@ bool CSnapshotGadgetOut::writeBlockName(std::string block_name, int nextblock)
if (version == 2 ) { // gadget2 file format
int dummy=8;
nextblock += (2*sizeof(int));
char block[4];
std::string str (" ");
str.copy(block,4,0);
block_name.copy(block,block_name.length()<=4?block_name.length():4,0);
writeData((char *) &dummy , sizeof(int), 1); // write
writeData((char *) block_name.c_str() , sizeof(char), 4); // write label
//writeData((char *) block_name.c_str() , sizeof(char), 4); // write label
writeData((char *) block , sizeof(char), 4); // write label
writeData((char *) &nextblock , sizeof(int), 1); // write nextblock
writeData((char *) &dummy , sizeof(int), 1); // write
status = out.good();
......
......@@ -294,6 +294,8 @@ typedef struct particle_data_lite
int * id[6];
int ntot_withmasses;
std::ofstream out;
// Map to associate EXTRA tags with vectors
std::map<std::string, std::vector <float> > s_mapStringVector;
// Map to associate the strings with the bool values
std::map<std::string, bool> ptrIsAlloc[6];
t_io_header_1 header;
......@@ -327,6 +329,9 @@ typedef struct particle_data_lite
int setTemp(const int _n, float * _temp, const bool addr);
int setMetalGas(const int _n, float * _mg , const bool addr);
int setMetalStars(const int _n, float * _ms , const bool addr);
// extra TAGS
int setExtra(std::string tag, const int _n, float * _data, const bool addr);
}; // end of class CSnapshotGadgetOut
} // namespace
......
This diff is collapsed.
......@@ -93,17 +93,19 @@ namespace uns {
// py wrapper
//
int nextFrame(const char * _bits);
// float
bool getData(const std::string comp,const std::string prop,
int * size,float ** farray);
int * size,float ** farray);
bool getData(const std::string prop,
int * size,float ** farray);
int * size,float ** farray);
bool getData(const std::string prop,float * fvalue);
// int
bool getData(const std::string comp,const std::string prop,
int * size,int ** iarray);
int * size,int ** iarray);
bool getData(const std::string prop,
int * size,int ** iarray);
bool getData(const std::string prop,int * ivalue);
bool getData(const std::string prop,
int * size,int ** iarray);
bool getData(const std::string prop,int * ivalue);
......@@ -132,14 +134,32 @@ namespace uns {
// constructor for WRITING operations
CunsOut(const std::string, const std::string, const bool verb=false);
~CunsOut();
bool isValid() { return valid;};
bool isValid() { return valid;}
uns::CSnapshotInterfaceOut * snapshot; // object to store data
// Map to associate the strings with the enum values
static std::map<std::string, StringData> s_mapStringValues;
// py wrapper
// setData FLOAT
int setData(const std::string comp,const std::string prop,
int size,float * farray, const bool _addr=false);
int setData(const std::string prop,
int size,float * farray, const bool _addr=false);
int setData(const std::string prop,
float fvalue);
// setData INT
int setData(const std::string comp,const std::string prop,
int size,int * iarray, const bool _addr=false);
int setData(const std::string prop,
int size,int * iarray, const bool _addr=false);
int setData(const std::string prop,
int ivalue);
//
int save();
// py wrapper
static void initializeStringMap(const bool);
private:
......
......@@ -133,7 +133,12 @@ int main(int argc, char ** argv )
processComponent("bndry",unsin,unsout);
processComponent("gas" ,unsin,unsout);
processComponent("all" ,unsin,unsout); // only all particles selected
float * extra=new float[10];
for (int i=0; i<10; i++) {
extra[i]=i*10.0;
}
unsout->snapshot->setData("EXTRA","bug",10,extra);
// according to user's input request ("select" parameter)
// check if gas component exist from input snapshot
bool is_gas=unsin->snapshot->getRangeSelect("gas",&cnbody,&cfirst,&clast);
......
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