Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/7866_icat_cross_platform…
Browse files Browse the repository at this point in the history
…_archive_download'
  • Loading branch information
martyngigg committed Oct 17, 2013
2 parents a77a209 + 5bca4fc commit 28aee85
Show file tree
Hide file tree
Showing 26 changed files with 513 additions and 214 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,13 @@ namespace Mantid
void exec();
/// get location of data file or download method
int doDownload( ICATPortBindingProxy & icat);

/// If the extn of the file .raw it returns true
bool isDataFile(const std::string& fileName);

/// This method saves the downloaded file to disc
/// True if the stream is considered binary, false otherwise
bool isBinary(std::istream& stream);
/// Saves the downloaded file to disc
std::string saveFiletoDisk(std::istream& rs,const std::string &fileName);

/// This method saves downloaded file to local disk
/// Saves downloaded file to local disk
std::string doDownloadandSavetoLocalDrive(const std::string& URL,const std::string& fileName);

/// This method replaces backwardslash with forward slashes - for linux
void replaceBackwardSlash(std::string& inputString);

private:
/// progress indicator
double m_prog;
Expand Down
151 changes: 65 additions & 86 deletions Code/Mantid/Framework/ICat/src/CatalogDownloadDataFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ if the data archive is not accessible, it downloads the files from the data serv
#include "MantidAPI/ICatalog.h"
#include "MantidKernel/ConfigService.h"
#include "MantidKernel/FacilityInfo.h"

#include "MantidKernel/FileDescriptor.h"

#include <Poco/Net/HTTPClientSession.h>
#include <Poco/Net/HTTPRequest.h>
Expand Down Expand Up @@ -48,20 +48,18 @@ namespace Mantid
/// Sets documentation strings for this algorithm
void CatalogDownloadDataFiles::initDocs()
{
this->setWikiSummary("Downloads the given data files from the data server ");
this->setOptionalMessage("Downloads the given data files from the data server");
this->setWikiSummary("Obtains a list of file paths to the data files the user wants to download from the archives, or has downloaded and saved locally.");
}


/// declaring algorithm properties
void CatalogDownloadDataFiles::init()
{
declareProperty(new ArrayProperty<int64_t> ("FileIds"),"List of fileids to download from the data server");
declareProperty(new ArrayProperty<std::string> ("FileNames"),"List of filenames to download from the data server");
declareProperty(new ArrayProperty<std::string>("FileLocations",std::vector<std::string>(),
declareProperty(new ArrayProperty<std::string>("Filelocations",std::vector<std::string>(),
boost::make_shared<NullValidator>(),
Direction::Output),
"A list of containing locations of files downloaded from data server");
"A list of file locations to the ICAT datafiles.");
}

/// Raise an error concerning catalog searching
Expand All @@ -78,100 +76,96 @@ namespace Mantid
/// Execute the algorithm
void CatalogDownloadDataFiles::exec()
{
ICatalog_sptr catalog;

ICatalog_sptr catalog_sptr;
try
{
catalog_sptr=CatalogFactory::Instance().create(ConfigService::Instance().getFacility().catalogName());
catalog = CatalogFactory::Instance().create(ConfigService::Instance().getFacility().catalogInfo().catalogName());

}
catch(Kernel::Exception::NotFoundError&)
{
throwCatalogError();
}
if(!catalog_sptr)

if(!catalog)
{
throwCatalogError();
}
//get file ids
std::vector<int64_t> fileids = getProperty("FileIds");
//get file names
std::vector<std::string> filenames= getProperty("FileNames");

std::vector<std::string> filelocations;
std::vector<int64_t>::const_iterator citr1 =fileids.begin();
std::vector<std::string>::const_iterator citr2=filenames.begin();
m_prog =0.0;
//loop over file ids
for(;citr1!=fileids.end();++citr1,++citr2)
{
m_prog+=0.1;
double prog=m_prog/(double(fileids.size())/10);

std::string filelocation;
//get the location string from catalog
// Used in order to transform the archive path to the user's operating system.
CatalogInfo catalogInfo = ConfigService::Instance().getFacility().catalogInfo();

std::vector<int64_t> fileIDs = getProperty("FileIds");
std::vector<std::string> fileNames = getProperty("FileNames");

// Stores the paths to the related files located in the archives (if user has access).
// Otherwise, stores the path to the downloaded file.
std::vector<std::string> fileLocations;

m_prog = 0.0;

std::vector<int64_t>::const_iterator fileID = fileIDs.begin();
std::vector<std::string>::const_iterator fileName = fileNames.begin();

// For every file with the given ID.
for(; fileID != fileIDs.end(); ++fileID, ++fileName)
{
m_prog += 0.1;
double prog = m_prog / (double(fileIDs.size()) /10 );

progress(prog,"getting location string...");
catalog_sptr->getFileLocation(*citr1,filelocation);

//if we are able to open the file from the location returned by getDatafile api
//the user got the permission to acess archive
std::ifstream isisfile(filelocation.c_str());
if(isisfile)
// The location of the file on the ICAT server (E.g. in the archives).
std::string fileLocation;
catalog->getFileLocation(*fileID,fileLocation);

// Transform the archive path to the path of the user's operating system.
fileLocation = catalogInfo.transformArchivePath(fileLocation);

// Can we open the file (Hence, have access to the archives?)
std::ifstream hasAccessToArchives(fileLocation.c_str());
if(hasAccessToArchives)
{
g_log.information()<<"isis archive location for the file with id "<<*citr1<<" is "<<filelocation<<std::endl;
filelocations.push_back(filelocation);
g_log.information() << "File (" << *fileName << ") located in archives." << std::endl;

fileLocations.push_back(fileLocation);
}
else
{
g_log.information()<<"File with id "<<*citr1<<" can not be opened from archive,now file will be downloaded over internet from data server"<<std::endl;
g_log.information() << "Unable to open file (" << *fileName << ") from archive. Beginning to download over Internet." << std::endl;

std::string url;
progress(prog/2,"getting the url ....");
//getting the url for the file to downlaod from respective catalog
catalog_sptr->getDownloadURL(*citr1,url);

// Obtain URL for related file to download from net.
std::string url;
catalog->getDownloadURL(*fileID,url);

progress(prog,"downloading over internet...");

// Download file from the data server to the machine where mantid is installed
std::string fullPathDownloadedFile = doDownloadandSavetoLocalDrive(url,*citr2);
// Download file from the data server to the machine where Mantid is installed
std::string fullPathDownloadedFile = doDownloadandSavetoLocalDrive(url,*fileName);

// replace "\" with "/" before adding to filelocations
replaceBackwardSlash(fullPathDownloadedFile);
filelocations.push_back(fullPathDownloadedFile);
fileLocations.push_back(fullPathDownloadedFile);
}

}

// Inform the user where they files are being saved.
g_log.notice() << "Saving file to: " << filelocations.at(0);

//set the filelocations property
setProperty("FileLocations",filelocations);
// Set the fileLocations property
setProperty("FileLocations",fileLocations);
}

/** This method checks the file extn and if it's a raw file reurns true
* This is useful when the we download a file over internet and save to local drive,
* to open the file in binary or ascii mode
* @param fileName :: file name
* @returns true if the file is a data file
/**
* Checks to see if the file to be downloaded is a datafile.
* @param stream :: input stream
* @returns True if the stream is not considered ASCII (e.g. binary), false otherwise
*/
bool CatalogDownloadDataFiles::isDataFile(const std::string & fileName)
bool CatalogDownloadDataFiles::isBinary(std::istream& stream)
{
std::basic_string <char>::size_type dotIndex;
//const std::basic_string <char>::size_type npos = -1;
//find the position of .in row file
dotIndex = fileName.find_last_of (".");
std::string fextn=fileName.substr(dotIndex+1,fileName.size()-dotIndex);
std::transform(fextn.begin(),fextn.end(),fextn.begin(),tolower);

bool binary;
(!fextn.compare("raw")|| !fextn.compare("nxs")) ? binary = true : binary = false;
return binary;

return !FileDescriptor::isAscii(stream);
}

/** This method downloads file over internet using Poco HTTPClientSession
/**
* Downloads file over Internet using Poco HTTPClientSession
* @param URL- URL of the file to down load
* @param fileName :: file name
* @return Full path of where file is saved to
Expand Down Expand Up @@ -218,7 +212,8 @@ namespace Mantid
return retVal_FullPath;
}

/** This method saves the input stream to a file
/**
* Saves the input stream to a file
* @param rs :: input stream
* @param fileName :: name of the output file
* @return Full path of where file is saved to
Expand All @@ -229,9 +224,8 @@ namespace Mantid
Poco::Path path(defaultSaveDir, fileName);
std::string filepath = path.toString();

std::ios_base::openmode mode;
//if raw/nexus file open it in binary mode else ascii
isDataFile(fileName)? mode = std::ios_base::binary : mode = std::ios_base::out;
std::ios_base::openmode mode = isBinary(rs) ? std::ios_base::binary : std::ios_base::out;

std::ofstream ofs(filepath.c_str(), mode);
if ( ofs.rdstate() & std::ios::failbit )
{
Expand All @@ -243,7 +237,8 @@ namespace Mantid
return filepath;
}

/** This method is used for unit testing purpose.
/**
* This method is used for unit testing purpose.
* as the Poco::Net library httpget throws an exception when the nd server n/w is slow
* I'm testing the download from mantid server.
* as the downlaod method I've written is private I can't access that in unit testing.
Expand All @@ -257,22 +252,6 @@ namespace Mantid
return doDownloadandSavetoLocalDrive(URL,fileName);

}
/** This method replaces backward slash with forward slash for linux compatibility.
* @param inputString :: input string
*/
void CatalogDownloadDataFiles::replaceBackwardSlash(std::string& inputString)
{
std::basic_string <char>::iterator iter;
for(iter=inputString.begin();iter!=inputString.end();++iter)
{
if((*iter)=='\\')
{
(*iter)='/';
}
}

}


}
}
3 changes: 1 addition & 2 deletions Code/Mantid/Framework/ICat/src/CatalogGetDataFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ namespace Mantid
ICatalog_sptr catalog_sptr;
try
{
catalog_sptr=CatalogFactory::Instance().create(ConfigService::Instance().getFacility().catalogName());

catalog_sptr=CatalogFactory::Instance().create(ConfigService::Instance().getFacility().catalogInfo().catalogName());
}
catch(Kernel::Exception::NotFoundError&)
{
Expand Down
3 changes: 1 addition & 2 deletions Code/Mantid/Framework/ICat/src/CatalogGetDataSets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ namespace Mantid
ICatalog_sptr catalog_sptr;
try
{
catalog_sptr=CatalogFactory::Instance().create(ConfigService::Instance().getFacility().catalogName());

catalog_sptr=CatalogFactory::Instance().create(ConfigService::Instance().getFacility().catalogInfo().catalogName());
}
catch(Kernel::Exception::NotFoundError&)
{
Expand Down
4 changes: 1 addition & 3 deletions Code/Mantid/Framework/ICat/src/CatalogListInstruments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ namespace Mantid
ICatalog_sptr catalog_sptr;
try
{

catalog_sptr=CatalogFactory::Instance().create(ConfigService::Instance().getFacility().catalogName());

catalog_sptr=CatalogFactory::Instance().create(ConfigService::Instance().getFacility().catalogInfo().catalogName());
}
catch(Kernel::Exception::NotFoundError&)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace Mantid
ICatalog_sptr catalog_sptr;
try
{
catalog_sptr=CatalogFactory::Instance().create(ConfigService::Instance().getFacility().catalogName());
catalog_sptr=CatalogFactory::Instance().create(ConfigService::Instance().getFacility().catalogInfo().catalogName());
}
catch(Kernel::Exception::NotFoundError&)
{
Expand Down
5 changes: 2 additions & 3 deletions Code/Mantid/Framework/ICat/src/CatalogLogin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ namespace Mantid
ICatalog_sptr catalog_sptr;
try
{
g_log.information() << "Attempting to login to " << ConfigService::Instance().getFacility().catalogName()
g_log.information() << "Attempting to login to " << ConfigService::Instance().getFacility().catalogInfo().catalogName()
<< " for " << ConfigService::Instance().getFacility().name() << "\n";
catalog_sptr=CatalogFactory::Instance().create(ConfigService::Instance().getFacility().catalogName());

catalog_sptr=CatalogFactory::Instance().create(ConfigService::Instance().getFacility().catalogInfo().catalogName());
}
catch(Kernel::Exception::NotFoundError&)
{
Expand Down
3 changes: 1 addition & 2 deletions Code/Mantid/Framework/ICat/src/CatalogLogout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ namespace Mantid
ICatalog_sptr catalog_sptr;
try
{
catalog_sptr=CatalogFactory::Instance().create(ConfigService::Instance().getFacility().catalogName());

catalog_sptr=CatalogFactory::Instance().create(ConfigService::Instance().getFacility().catalogInfo().catalogName());
}
catch(Kernel::Exception::NotFoundError&)
{
Expand Down
3 changes: 1 addition & 2 deletions Code/Mantid/Framework/ICat/src/CatalogMyDataSearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ namespace Mantid
ICatalog_sptr catalog_sptr;
try
{
catalog_sptr=CatalogFactory::Instance().create(ConfigService::Instance().getFacility().catalogName());

catalog_sptr=CatalogFactory::Instance().create(ConfigService::Instance().getFacility().catalogInfo().catalogName());
}
catch(Kernel::Exception::NotFoundError&)
{
Expand Down
3 changes: 1 addition & 2 deletions Code/Mantid/Framework/ICat/src/CatalogSearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ namespace Mantid
ICatalog_sptr catalog_sptr;
try
{
catalog_sptr=CatalogFactory::Instance().create(ConfigService::Instance().getFacility().catalogName());

catalog_sptr=CatalogFactory::Instance().create(ConfigService::Instance().getFacility().catalogInfo().catalogName());
}
catch(Kernel::Exception::NotFoundError&)
{
Expand Down
8 changes: 0 additions & 8 deletions Code/Mantid/Framework/ICat/src/ICat3/ICat3Helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1090,14 +1090,6 @@ namespace Mantid
filelocation=*response.return_->location;
}
}
std::basic_string <char>::iterator iter;
for(iter=filelocation.begin();iter!=filelocation.end();++iter)
{
if((*iter)=='\\')
{
(*iter)='/';
}
}
}


Expand Down
3 changes: 3 additions & 0 deletions Code/Mantid/Framework/Kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set ( SRC_FILES
src/ArrayProperty.cpp
src/Atom.cpp
src/BinFinder.cpp
src/CatalogInfo.cpp
src/CPUTimer.cpp
src/CompositeValidator.cpp
src/ConfigService.cpp
Expand Down Expand Up @@ -117,6 +118,7 @@ set ( INC_FILES
inc/MantidKernel/BinFinder.h
inc/MantidKernel/BinaryFile.h
inc/MantidKernel/BoundedValidator.h
inc/MantidKernel/CatalogInfo.h
inc/MantidKernel/CPUTimer.h
inc/MantidKernel/Cache.h
inc/MantidKernel/CompositeValidator.h
Expand Down Expand Up @@ -245,6 +247,7 @@ set ( TEST_FILES
BoundedValidatorTest.h
CPUTimerTest.h
CacheTest.h
CatalogInfoTest.h
CompositeValidatorTest.h
ConfigServiceTest.h
DataServiceTest.h
Expand Down

0 comments on commit 28aee85

Please sign in to comment.