My Project
|
00001 /* 00002 $Id: WriteNeXusFile.hh 2247 2011-04-27 05:20:41Z jisuzuki $ 00003 */ 00004 00005 #ifndef WRITENEXUSFILE 00006 #define WRITENEXUSFILE 00007 00008 #include <map> 00009 #include "Header.hh" 00010 #include "HeaderBase.hh" 00011 #include "ElementContainer.hh" 00012 #include "NeutronVector.hh" 00013 #include "UInt4Container.hh" 00014 #include "napi.h" 00015 #include "ElementContainerArray.hh" 00016 #include "ElementContainerMatrix.hh" 00017 #include "UInt4ContainerArray.hh" 00018 #include "UInt4ContainerMatrix.hh" 00019 #include "TwoDimElementContainer.hh" 00020 00022 00034 class WriteNeXusFile 00035 { 00036 private: 00037 UInt4 Comp; 00038 UInt4 r_handleFlag; 00039 protected: 00040 void CreateNeXusFile( const string &FileName, const string &UserName ); 00041 //void CloseNeXusFile(); 00042 NXhandle handle; 00043 NXlink link; 00044 00045 public: 00046 WriteNeXusFile( const string &FileName, const string &UserName="UserName", UInt4 CompMode=1 ); 00055 WriteNeXusFile( NXhandle r_handle, UInt4 CompMode=1 ); 00056 00057 00058 ~WriteNeXusFile(); 00061 NXhandle PutHandle(){ return handle; } 00062 NXlink PutLink(){ return link; } 00063 00064 void CloseNeXusFile(); 00065 00066 void AppendAttribute( const string &Name, string value ); 00067 void AppendAttribute( const string &Name, UInt4 value ); 00068 void AppendAttribute( const string &Name, Int4 value ); 00069 void AppendAttribute( const string &Name, Double value ); 00070 void AppendAttribute( const string &Name, vector<Int4> value ); 00071 void AppendAttribute( const string &Name, vector<Double> value ); 00072 void AppendAttribute( const string &Name, vector<string> value ); 00073 void AppendAttribute( HeaderBase *header ); 00074 00075 void MakeOpenGroup( const string &Name, string Class ); 00080 void CloseGroup(); 00082 void CloseData(); 00085 00086 00097 template <class T> 00098 void WriteData( const string &Name, const T &t ); 00099 00107 template <class T> 00108 void WriteData( const string &Name, const std::vector<T *> &t ); 00109 00118 template <class T> 00119 void WriteData( const string &Name, const std::map<string, T> &t ); 00121 00122 //void WriteData( const string &Name ); 00124 void WriteData( const string &Name, UInt4 value ); 00126 void WriteData( const string &Name, Int4 value ); 00128 void WriteData( const string &Name, Double value ); 00130 void WriteData( const string &Name, string value ); 00132 void WriteData( const string &Name, const UInt4 *value, UInt4 size ); 00134 void WriteData( const string &Name, const Int4 *value, UInt4 size ); 00136 void WriteData( const string &Name, const Double *value, UInt4 size ); 00138 void WriteData( const string &Name, const vector< string > &value ); 00140 void WriteData( const string &Name, const vector< UInt4 > &value ); 00142 void WriteData( const string &Name, const vector< Int4 > &value ); 00144 void WriteData( const string &Name, const vector< Double > &value ); 00146 void WriteData1( const string &Name, ElementContainer EC ); 00152 void WriteData1( const string &Name, HeaderBase Header ); 00158 void WriteData1( const string &Name, ElementContainerArray ECA ); 00163 void WriteData1( const string &Name, ElementContainerMatrix ECM ); 00169 void WriteData1( const string &Name, UInt4ContainerMatrix UCM ); 00170 void WriteData1( const string &Name, UInt4ContainerArray UCA ); 00171 void WriteData1( const string &Name, UInt4Container UC ); 00172 00173 void WriteData1( const string &Name, TwoDimElementContainer TDEC ); 00174 00175 void MakeLink( NXlink Link ); 00180 }; 00181 00183 template <class T> 00184 void WriteNeXusFile:: 00185 WriteData( const string &Name, const T &t ) 00186 { 00187 if( Name == "" ){ 00188 MakeOpenGroup( "ManyoLibData", "NXdata" ); 00189 } 00190 else{ 00191 MakeOpenGroup( Name, "NXdata" ); 00192 } 00193 00194 // this new WriteData() scheme writes data with an attribute "version"=2 00195 AppendAttribute("version", 2); // writes 'version="NX_INT32:2"' 00196 //NXputattr( handle, "version", (void*)( &v ), 1, NX_UINT32 ); 00197 00198 t.NXwrite(*this); 00199 00200 00201 NXgetgroupID( handle, &link ); 00202 CloseGroup(); 00203 } 00204 00206 template <class T> 00207 void WriteNeXusFile:: 00208 WriteData( const string &Name, const std::vector<T *> &t ) 00209 { 00210 if( Name == "" ){ 00211 MakeOpenGroup( "std::vector_pointer_data", "NXdata" ); 00212 } 00213 else{ 00214 MakeOpenGroup( Name, "NXdata" ); 00215 } 00216 00217 UInt4 size = t.size(); 00218 WriteData( "size", size ); 00219 static char index[128]; 00220 for( UInt4 i=0; i<size; i++ ){ 00221 sprintf(index, "%s%d", Name.c_str(), i); 00222 WriteData(string(index), *t[i]); 00223 } 00224 00225 NXgetgroupID( handle, &link ); 00226 CloseGroup(); 00227 } 00229 template <class T> 00230 void WriteNeXusFile:: 00231 WriteData( const string &Name, const std::map<string, T> &t ) 00232 { 00233 if( Name == "" ){ 00234 MakeOpenGroup( "std::map_data", "NXdata" ); 00235 } 00236 else{ 00237 MakeOpenGroup( Name, "NXdata" ); 00238 } 00239 00240 for (typename std::map<string, T>::const_iterator p=t.begin(), 00241 end=t.end();p!=end;++p) { 00242 WriteData(p->first, p->second); 00243 } 00244 00245 NXgetgroupID( handle, &link ); 00246 CloseGroup(); 00247 } 00248 #endif 00249 00250 00251 00252 /* Test code on Python environment. 00253 e = Neutron.ElementContainer() 00254 00255 w = Neutron.WriteNeXusFile( "nx.nx", "suzuki" ) 00256 w.MakeOpenGroup("Entry1","NXentry") 00257 w.MakeOpenGroup("Data1","NXdata") 00258 00259 w.WriteData( "ElementContainerData", e ) 00260 00261 w.CloseGroup() # Close Data1 00262 w.CloseGroup() # Close Entry1 00263 del w 00264 */ 00265 00266 00267 00268 /* test code on Python interface 00269 import Neutron 00270 w = Neutron.WriteNeXusFile("nx.nx", "suzuki") 00271 w.AppendAttribute("x", "xxx") 00272 w.AppendAttribute("x", 12) 00273 w.MakeOpenGroup("Entry1","NXentry") 00274 w.MakeOpenGroup("Data1","NXdata") 00275 00276 w.WriteData("user_name", "suzuki") 00277 00278 vui = Neutron.MakeUInt4Vector( 20 ) 00279 vi = Neutron.MakeInt4Vector( 30 ) 00280 vd = Neutron.MakeDoubleVector( 10 ) 00281 00282 for x in range( 10 ): 00283 vd[x] = float( x ) 00284 00285 w.WriteData( "vui", vui ) 00286 w.AppendAttribute("x", 12) 00287 00288 w.WriteData( "vi", vi ) 00289 w.AppendAttribute("x", 13) 00290 00291 w.WriteData( "vd", vd ) 00292 w.AppendAttribute("x", 14) 00293 00294 s = Neutron.MakeStringVector() 00295 s.append( "ooho" ) 00296 s.append( "kasuga" ) 00297 s.append( "tsukuba" ) 00298 s.append( "Ibarakiken Tsukubashi kasuga" ) 00299 w.WriteData( "s", s ) 00300 00301 w.CloseGroup() # Close Data1 00302 w.CloseGroup() # Close Entry1 00303 del w 00304 */