improved attribute handling for NeXus IDF 1

This commit is contained in:
nemu 2011-12-22 13:51:04 +00:00
parent 7e2ea62ac3
commit e76399634f
3 changed files with 55 additions and 18 deletions

View File

@ -3751,7 +3751,7 @@ Bool_t PRunDataHandler::WriteNexusFile(TString fln)
nxs->GetEntryIdf1()->GetInstrument()->GetDetector()->SetNumber(fData[0].GetNoOfHistos()); nxs->GetEntryIdf1()->GetInstrument()->GetDetector()->SetNumber(fData[0].GetNoOfHistos());
nxs->GetEntryIdf1()->GetInstrument()->GetCollimator()->SetType("n/a"); nxs->GetEntryIdf1()->GetInstrument()->GetCollimator()->SetType("n/a");
// calculate the total number of counts // calculate the total number of counts
int total_counts = 0; double total_counts = 0;
for (unsigned int i=0; i<fData[0].GetNoOfHistos(); i++) { for (unsigned int i=0; i<fData[0].GetNoOfHistos(); i++) {
for (unsigned int j=0; j<fData[0].GetDataBin(i)->size(); j++) for (unsigned int j=0; j<fData[0].GetDataBin(i)->size(); j++)
total_counts += fData[0].GetDataBin(i)->at(j); total_counts += fData[0].GetDataBin(i)->at(j);

View File

@ -2913,9 +2913,11 @@ int PNeXus::ReadFileIdf1()
NXclosegroup(fFileHandle); NXclosegroup(fFileHandle);
// open group NXsample // open group NXsample
if (NXopengroup(fFileHandle, "sample", "NXsample") == NX_ERROR) { if (SearchInGroup("NXsample", "class", nxname, nxclass, dataType)) {
if (!ErrorHandler(NXopengroup(fFileHandle, "sample", "NXsample"), PNEXUS_GROUP_OPEN_ERROR, "couldn't open NeXus subgroup 'sample'!")) return NX_ERROR;
} else {
cout << endl << ">> **WARNING** unable to open subgroup NXsample, will try NXSample." << endl; cout << endl << ">> **WARNING** unable to open subgroup NXsample, will try NXSample." << endl;
if (!ErrorHandler(NXopengroup(fFileHandle, "sample", "NXSample"), PNEXUS_GROUP_OPEN_ERROR, "couldn't open NeXus subgroup sample!")) return NX_ERROR; if (!ErrorHandler(NXopengroup(fFileHandle, "sample", "NXSample"), PNEXUS_GROUP_OPEN_ERROR, "couldn't open NeXus subgroup 'sample'!")) return NX_ERROR;
} }
// read sample name // read sample name
@ -2939,7 +2941,8 @@ int PNeXus::ReadFileIdf1()
fNxEntry1->GetSample()->SetPhysProp("magnetic_field", (double)fval, str); fNxEntry1->GetSample()->SetPhysProp("magnetic_field", (double)fval, str);
// read sample shape, e.g. powder, single crystal, etc (THIS IS AN OPTIONAL ENTRY) // read sample shape, e.g. powder, single crystal, etc (THIS IS AN OPTIONAL ENTRY)
if (NXopendata(fFileHandle, "shape") == NX_OK) { if (SearchInGroup("shape", "name", nxname, nxclass, dataType)) {
if (!ErrorHandler(NXopendata(fFileHandle, "shape"), PNEXUS_OPEN_DATA_ERROR, "couldn't open 'shape' data in sample group!")) return NX_ERROR;
if (!ErrorHandler(GetStringData(str), PNEXUS_GET_DATA_ERROR, "couldn't read 'shape' data in sample group!")) return NX_ERROR; if (!ErrorHandler(GetStringData(str), PNEXUS_GET_DATA_ERROR, "couldn't read 'shape' data in sample group!")) return NX_ERROR;
if (!ErrorHandler(NXclosedata(fFileHandle), PNEXUS_CLOSE_DATA_ERROR, "couldn't close 'shape' data in sample group")) return NX_ERROR; if (!ErrorHandler(NXclosedata(fFileHandle), PNEXUS_CLOSE_DATA_ERROR, "couldn't close 'shape' data in sample group")) return NX_ERROR;
fNxEntry1->GetSample()->SetShape(str); fNxEntry1->GetSample()->SetShape(str);
@ -2967,19 +2970,16 @@ int PNeXus::ReadFileIdf1()
fNxEntry1->GetSample()->SetMagneticFieldVectorCoordinateSystem(str); fNxEntry1->GetSample()->SetMagneticFieldVectorCoordinateSystem(str);
// workaround since not all ISIS IDF 1 files have the 'units' entry!! // workaround since not all ISIS IDF 1 files have the 'units' entry!!
int i, status, attlen, atttype; memset(cstr, '\0', sizeof(cstr));
char cstr[VGNAMELENMAX]; strncpy(cstr, "units", sizeof(cstr));
NXname data_value; if (SearchAttrInData(cstr, attLen, attType)) {
status = NXgetattr(fFileHandle, cstr, data_value, &attLen, &attType);
attlen = VGNAMELENMAX - 1; if (status == NX_OK) {
atttype = NX_CHAR; strncpy(cstr, data_value, sizeof(cstr));
status = NXgetattr(fFileHandle, const_cast<char*>("units"), data_value, &attlen, &atttype); str = cstr;
if (status == NX_OK) { } else {
for (i = 0; i < attlen; i++) str = string("Gauss");
cstr[i] = *(data_value + i); }
cstr[i] = '\0';
str = cstr;
} else { } else {
str = string("Gauss"); str = string("Gauss");
} }
@ -3101,7 +3101,9 @@ int PNeXus::ReadFileIdf1()
attType = NX_INT32; attType = NX_INT32;
memset(cstr, '\0', sizeof(cstr)); memset(cstr, '\0', sizeof(cstr));
strncpy(cstr, "T0_bin", sizeof(cstr)); strncpy(cstr, "T0_bin", sizeof(cstr));
if (NXgetattr(fFileHandle, cstr, &ival, &attLen, &attType) == NX_ERROR) { if (SearchAttrInData(cstr, attLen, attType)) {
if (!ErrorHandler(NXgetattr(fFileHandle, cstr, &ival, &attLen, &attType), PNEXUS_OPEN_DATA_ERROR, "couldn't open 'T0_bin' data in NXdata group!")) return NX_ERROR;
} else {
cout << endl << ">> **WARNING** didn't find attribute 'T0_bin' in NXdata/counts, will try 't0_bin'." << endl; cout << endl << ">> **WARNING** didn't find attribute 'T0_bin' in NXdata/counts, will try 't0_bin'." << endl;
memset(cstr, '\0', sizeof(cstr)); memset(cstr, '\0', sizeof(cstr));
strncpy(cstr, "t0_bin", sizeof(cstr)); strncpy(cstr, "t0_bin", sizeof(cstr));
@ -5328,3 +5330,37 @@ bool PNeXus::SearchInGroup(string str, string tag, NXname &nxname, NXname &nxcla
return found; return found;
} }
//------------------------------------------------------------------------------------------
// SearchAttrInData (private)
//------------------------------------------------------------------------------------------
/**
* <p>Searches an attribute (labelled by str) in the currently open data set.
*
* <b>return:</b>
* - true if attribute 'str' is found in the currently open data set. nxname, nxclass keeps than the entry information.
* - false otherwise
*
* \param str label of the attribute to be looked for
* \param length of the attribute data it entry is found
* \param dataType of the entry if entry is found
*/
bool PNeXus::SearchAttrInData(string str, int &length, int &dataType)
{
bool found = false;
int status;
char name[128];
memset(name, 0, sizeof(name));
NXinitattrdir(fFileHandle);
do {
status = NXgetnextattr(fFileHandle, name, &length, &dataType);
if (!str.compare(name)) {
found = true;
break;
}
} while (!found && (status == NX_OK));
return found;
}

View File

@ -631,6 +631,7 @@ class PNeXus {
virtual bool IsValidIdf2(bool strict); virtual bool IsValidIdf2(bool strict);
virtual bool SearchInGroup(string str, string tag, NXname &nxname, NXname &nxclass, int &dataType); virtual bool SearchInGroup(string str, string tag, NXname &nxname, NXname &nxclass, int &dataType);
virtual bool SearchAttrInData(string str, int &length, int &dataType);
}; };
#endif // _PNEXUS_H_ #endif // _PNEXUS_H_