Exemplo n.º 1
0
 /** Get all the Nexus entry types for a file
  *
  * Try to open named Nexus file and return all entries plus the definition found for each.
  * If definition not found, try and return "analysis" field (Muon V1 files)
  * Closes file on exit.
  *
  * @param fileName :: file to open
  * @param entryName :: vector that gets filled with strings with entry names
  * @param definition :: vector that gets filled with the "definition" or "analysis" string.
  * @return count of entries if OK, -1 failed to open file.
  */
 int getNexusEntryTypes(const std::string& fileName, std::vector<std::string>& entryName,
     std::vector<std::string>& definition )
 {
   //
   //
   NXhandle fileH;
   NXaccess mode= NXACC_READ;
   NXstatus stat=NXopen(fileName.c_str(), mode, &fileH);
   if(stat==NX_ERROR) return(-1);
   //
   entryName.clear();
   definition.clear();
   char *nxname,*nxclass;
   int nxdatatype;
   nxname= new char[NX_MAXNAMELEN];
   nxclass = new char[NX_MAXNAMELEN];
   int rank,dims[2],type;
   //
   // Loop through all entries looking for the definition section in each (or analysis for MuonV1)
   //
   std::vector<std::string> entryList;
   while( ( stat=NXgetnextentry(fileH,nxname,nxclass,&nxdatatype) ) == NX_OK )
   {
     std::string nxc(nxclass);
     if(nxc.compare("NXentry")==0)
       entryList.push_back(nxname);
   }
   // for each entry found, look for "analysis" or "definition" text data fields and return value plus entry name
   for(size_t i=0;i<entryList.size();i++)
   {
     //
     stat=NXopengroup(fileH,entryList[i].c_str(),"NXentry");
     // loop through field names in this entry
     while( ( stat=NXgetnextentry(fileH,nxname,nxclass,&nxdatatype) ) == NX_OK )
     {
       std::string nxc(nxclass),nxn(nxname);
       // if a data field
       if(nxc.compare("SDS")==0)
         // if one of the two names we are looking for
         if(nxn.compare("definition")==0 || nxn.compare("analysis")==0)
         {
           NXopendata(fileH,nxname);
           stat=NXgetinfo(fileH,&rank,dims,&type);
           if(stat==NX_ERROR)
             continue;
           char* value=new char[dims[0]+1];
           stat=NXgetdata(fileH,value);
           if(stat==NX_ERROR)
             continue;
           value[dims[0]]='\0';
           // return e.g entryName "analysis"/definition "muonTD"
           definition.push_back(value);
           entryName.push_back(entryList[i]);
           delete[] value;
           NXclosegroup(fileH); // close data group, then entry
           stat=NXclosegroup(fileH);
           break;
         }
     }
   }
   stat=NXclose(&fileH);
   delete[] nxname;
   delete[] nxclass;
   return(static_cast<int>(entryName.size()));
 }
Exemplo n.º 2
0
/** Performs the copying from the input to the output file,
 *  while modifying the data and time_of_flight fields.
 */
int SaveToSNSHistogramNexus::copy_file(const char *inFile, int nx_read_access,
                                       const char *outFile,
                                       int nx_write_access) {
  int nx_is_definition = 0;
  links_count = 0;
  current_path[0] = '\0';
  NXlink link;

  /* Open NeXus input file and NeXus output file */
  if (NXopen(inFile, nx_read_access, &inId) != NX_OK) {
    printf("NX_ERROR: Can't open %s\n", inFile);
    return NX_ERROR;
  }

  if (NXopen(outFile, nx_write_access, &outId) != NX_OK) {
    printf("NX_ERROR: Can't open %s\n", outFile);
    return NX_ERROR;
  }

  /* Output global attributes */
  if (WriteAttributes(nx_is_definition) != NX_OK) {
    return NX_ERROR;
  }
  /* Recursively cycle through the groups printing the contents */
  if (WriteGroup(nx_is_definition) != NX_OK) {
    return NX_ERROR;
  }
  /* close input */
  if (NXclose(&inId) != NX_OK) {
    return NX_ERROR;
  }

  // HDF5 only
  {
    /* now create any required links */
    for (int i = 0; i < links_count; i++) {
      if (NXopenpath(outId, links_to_make[i].to) != NX_OK)
        return NX_ERROR;
      if (NXgetdataID(outId, &link) == NX_OK ||
          NXgetgroupID(outId, &link) == NX_OK) {
        if (NXopenpath(outId, links_to_make[i].from) != NX_OK)
          return NX_ERROR;
        char *tstr = strrchr(links_to_make[i].to, '/');
        if (!strcmp(links_to_make[i].name, tstr + 1)) {
          if (NXmakelink(outId, &link) != NX_OK)
            return NX_ERROR;
        } else {
          if (NXmakenamedlink(outId, links_to_make[i].name, &link) != NX_OK)
            return NX_ERROR;
        }
      } else {
        return NX_ERROR;
      }
    }
  }
  /* Close the input and output files */
  if (NXclose(&outId) != NX_OK) {
    return NX_ERROR;
  }
  return NX_OK;
}