Skip to content

Commit

Permalink
Re #6151 update UI catalog for APIv2
Browse files Browse the repository at this point in the history
  • Loading branch information
mdoucet committed Dec 19, 2012
1 parent ffcdead commit 4f26cd9
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@
import sys
import traceback

# Check whether Mantid is available
try:
from MantidFramework import *
mtd.initialise(False)
import mantidsimple
HAS_MANTID = True
except:
HAS_MANTID = False

class DataType(object):
TABLE_NAME = "datatype"

Expand Down Expand Up @@ -61,13 +52,15 @@ class DataSet(object):
TABLE_NAME = "dataset"
data_type_cls = DataType

def __init__(self, run_number, title, run_start, duration, sdd, id=None):
def __init__(self, run_number, title, run_start, duration,
sdd, id=None, python_api=1):
self.run_number = run_number
self.title = title
self.run_start = run_start
self.duration = duration
self.sdd = sdd
self.id = id
self.python_api = python_api

@classmethod
def header(cls):
Expand Down Expand Up @@ -136,7 +129,7 @@ def find(cls, file_path, cursor, process_files=True):
rows = cursor.fetchall()

if len(rows) == 0:
if HAS_MANTID and process_files:
if process_files:
log_ws = "__log"
if cls.load_meta_data(file_path, outputWorkspace=log_ws):
return cls.read_properties(log_ws, run, cursor)
Expand Down Expand Up @@ -183,10 +176,7 @@ def __init__(self, replace_db=True):
try:
self._create_db(db_path, replace_db)
except:
if HAS_MANTID:
mtd.sendLogMessage("DataCatalog: Could not access local data catalog\n%s" % sys.exc_value)
else:
raise
print "DataCatalog: Could not access local data catalog\n%s" % sys.exc_value

def _create_db(self, db_path, replace_db):
"""
Expand Down Expand Up @@ -239,8 +229,7 @@ def get_string_list(self, data_dir=None):

def add_type(self, run, type):
if self.db is None:
if HAS_MANTID:
mtd.sendLogMessage("DataCatalog: Could not access local data catalog")
print "DataCatalog: Could not access local data catalog"
return

c = self.db.cursor()
Expand All @@ -258,20 +247,26 @@ def list_data_sets(self, data_dir=None, call_back=None, process_files=True):
self.catalog = []

if self.db is None:
if HAS_MANTID:
mtd.sendLogMessage("DataCatalog: Could not access local data catalog")
print "DataCatalog: Could not access local data catalog"
return

c = self.db.cursor()

if not os.path.isdir(data_dir):
print "Data path not a valid directory:", data_dir
return

try:
for f in os.listdir(data_dir):
if f.endswith(self.extension):
path = os.path.join(data_dir, f)
d = self.data_set_cls.find(path, c, process_files=process_files)
file_path = os.path.join(data_dir, f)

if hasattr(self.data_set_cls, "find_with_api"):
d = self.data_set_cls.find_with_api(file_path, c,
process_files=process_files)
else:
d = self.data_set_cls.find(file_path, c,
process_files=process_files)
if d is not None:
if call_back is not None:
attr_list = d.as_string_list()
Expand All @@ -283,7 +278,5 @@ def list_data_sets(self, data_dir=None, call_back=None, process_files=True):
self.db.commit()
c.close()
except:
if HAS_MANTID:
mtd.sendLogMessage("DataCatalog: Error working with the local data catalog\n%s" % str(traceback.format_exc()))
else:
raise
print "DataCatalog: Error working with the local data catalog\n%s" % str(traceback.format_exc())

Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,26 @@
import os
import time

# Check whether Mantid is available
try:
from MantidFramework import *
mtd.initialise(False)
import mantidsimple
HAS_MANTID = True
except:
HAS_MANTID = False

class HFIRDataType(DataType):
TABLE_NAME="hfir_datatype"

class HFIRDataSet(DataSet):
TABLE_NAME="hfir_dataset"
data_type_cls = HFIRDataType
python_api = 1

def __init__(self, run_number, title, run_start, duration, sdd):
super(HFIRDataSet, self).__init__(run_number, title, run_start, duration, sdd)
def __init__(self, run_number, title, run_start, duration, sdd, python_api=1):
super(HFIRDataSet, self).__init__(run_number, title, run_start,
duration, sdd, python_api=1)

@classmethod
def load_meta_data(cls, file_path, outputWorkspace):
def load_meta_data(cls, file_path, outputWorkspace, python_api=1):
try:
mantidsimple.LoadSpice2D(file_path, OutputWorkspace=outputWorkspace)
if python_api==1:
import mantidsimple as api
else:
import mantid.simpleapi as api
api.LoadSpice2D(Filename=file_path, OutputWorkspace=outputWorkspace)
return True
except:
return False
Expand All @@ -45,15 +42,57 @@ def handle(cls, file_path):
return handle

@classmethod
def read_properties(cls, ws, run, cursor):
def find_with_api(cls, file_path, cursor, process_files=True, python_api=1):
"""
Find an entry in the database, or create on as needed
"""
run = cls.handle(file_path)
if run is None:
return None

t = (run,)
cursor.execute('select * from %s where run=?'% cls.TABLE_NAME, t)
rows = cursor.fetchall()

if len(rows) == 0:
if process_files:
log_ws = "__log"
if cls.load_meta_data(file_path,
outputWorkspace=log_ws,
python_api=python_api):
return cls.read_properties(log_ws, run, cursor,
python_api=python_api)
else:
return None
else:
return None
else:
row = rows[0]
return DataSet(row[1], row[2], row[3], row[4], row[5], id=row[0])


@classmethod
def read_properties(cls, ws, run, cursor, python_api=1):
def read_prop(prop):
try:
return str(mtd[ws].getRun().getProperty(prop).value)
if python_api==1:
from MantidFramework import mtd
return str(mtd[ws].getRun().getProperty(prop).value)
else:
from mantid.api import AnalysisDataService
ws_object = AnalysisDataService.retrieve(ws)
return str(ws_object.getRun().getProperty(prop).value)
except:
return ""
def read_series(prop):
try:
return float(mtd[ws].getRun().getProperty(prop).getStatistics().mean)
if python_api==1:
from MantidFramework import mtd
return float(mtd[ws].getRun().getProperty(prop).getStatistics().mean)
else:
from mantid.api import AnalysisDataService
ws_object = AnalysisDataService.retrieve(ws)
return str(ws_object.getRun().getProperty(prop).getStatistics().mean)
except:
return -1

Expand Down

0 comments on commit 4f26cd9

Please sign in to comment.