def load(self, hdf5_file): """Load the data from the given hdf5 file and the sqlite3 database.""" # Load the calibration object associated with this data. self.calib = Calibration(self.detector, _date_from_shot(self.shot)) # Load the raw data. data_name = 'data_{0}'.format(self.detector.sn) self.y = hdf5_file.get(data_name).value # Read data from the database. query = 'SELECT shot, aperture, filter, port, dt, t0, timestamp ' query += 'FROM xRayData WHERE shot=? AND detector=?' row = dbutil.fetchall(const.DB_XRAY_PATH, query, (self.shot, int(self.detector.sn)))[0] self.shot = row[0] self.aperture = row[1] self.filter = row[2] self.port = row[3] self.dt = row[4] self.t0 = row[5] self.timestamp = row[6] if self.aperture is not None: self.aperture = mp.XRayAperture.get(self.aperture) if self.filter is not None: self.filter = mp.XRayFilter.get(self.filter) if self.port is not None: self.port = mp.Port.get(self.port)
def get(cls, sn): """Get an item with the given serial number. If you pass an object of type cls, it will be returned. """ # If the user passed in an object of this class, return it. if isinstance(sn, cls): return sn try: # We first attempt to return the object from cache. return cls.__OBJECT_CACHE[cls][sn] except: # The object wasn't in cache, so we read it from the database. table_name = cls.__name__ + 's' query = 'SELECT * FROM {0} WHERE sn=?'.format(table_name) obj = dbutil.fetchall(const.DB_PHYSICAL_PATH, query, (sn, ), cls)[0] # And of course we cache it. if cls not in cls.__OBJECT_CACHE: cls.__OBJECT_CACHE[cls] = {} cls.__OBJECT_CACHE[cls][sn] = obj return obj
def _load_mu_o_rho(self): """Load the mass attenuation coefficients.""" query = 'SELECT energy, mu_o_rho FROM xRayMassAttenuationCoeffs' query += ' WHERE material=? ORDER BY energy ASC' rows = np.array( dbutil.fetchall(const.DB_PHYSICAL_PATH, query, (self.sn, ))) self._energies = rows[:, 0] self._mu_o_rho = rows[:, 1]
def _load_efficiency(self): try: query = 'SELECT energy, efficiency FROM ' query += 'XRayDetectorEfficiencies ' query += 'WHERE detector=? ORDER BY energy ASC' rows = np.array( dbutil.fetchall(const.DB_PHYSICAL_PATH, query, (self.sn, ))) self._energies = rows[:, 0] self._efficiencies = rows[:, 1] except Exception: print('No detector efficiency for SN {0}'.format(self.sn)) self._energies = None self._efficiencies = None
def get_all(cls, in_use=None): """Get all items. If in_use is not None, then we only want those items that are in use. This condition isn't defined for all objects. """ table_name = cls.__name__ + 's' query = 'SELECT * FROM {0}'.format(table_name) query_args = [] if in_use is not None: query += ' WHERE inUse=?' query_args.append(in_use) query += ' ORDER BY sn' objs = dbutil.fetchall(const.DB_PHYSICAL_PATH, query, query_args, cls) if cls not in cls.__OBJECT_CACHE: cls.__OBJECT_CACHE[cls] = {} for obj in objs: cls.__OBJECT_CACHE[cls][obj.sn] = obj return objs
def _load(self): # Load properties from the database. query = 'SELECT date, eFactor, m_sigma, sigma_E, pulseFilename ' query += 'FROM xRayCalib ' query += 'WHERE detector = ? AND date <= ? ' query += 'ORDER BY date DESC' try: row = dbutil.fetchall(const.DB_XRAY_PATH, query, (self.detector.sn, self.data_date))[0] except: row = None if row is None: print('Warning: No calibration in database.', '\n Detector:', self.detector.sn, '\n Date:', self.data_date) else: (self.date, self.eFactor, self.m_sigma, self.sigma_E, self.filename) = row # Load the pulse shape from the HDF5 file. path = self._pulse_path() if not os.path.exists(path): print('Warning: Characteristic pulse file not found: ', path) return try: f = h5py.File(path, 'r') self.y_pulse = f.get('y_pulse').value f.close() except Exception, ex: msg = 'Failed to read pulse file.' msg += '\n Pulse path: {0}'.format(path) msg += '\n Exception: {0}'.format(ex) raise Exception(msg)
def load(self, hdf5_file): """Load the data from the given hdf5 file and the sqlite3 database.""" # Load the calibration object associated with this data. self.calib = Calibration(self.detector, _date_from_shot(self.shot)) # Load the raw data. data_name = 'data_{0}'.format(self.detector.sn) # Set self.y to a numpy array of the values of the detector with the specified serial number self.y = hdf5_file.get(data_name).value # Read data from the database. query = 'SELECT filename, shot, detector, port, shapingTime, ' query += 'gain, aperture, filter, t0, timestamp, ' query += 'FROM xRayData WHERE shot=? AND detector=?' row = dbutil.fetchall(TD3_DB_XRAY_PATH, query, (self.shot, int(self.detector.sn)))[0] self.filename = row[0] self.shot = row[1] self.detector = row[2] self.physPort = row[3] self.shapingTime = row[4] self.gain = row[5] self.aperture = row[6] self.filter = row[7] self.t0 = row[8] self.timestamp = row[9] if self.aperture is not None: self.aperture = mp.XRayAperture.get(self.aperture) if self.filter is not None: self.filter = mp.XRayFilter.get(self.filter) if self.port is not None: self.port = mp.Port.get(self.port)
import os import datetime import h5py import numpy as np from mst import mdsplus as mds from calibration import Calibration import constants as const import physical as mp import dbutil TD3_DB_XRAY_PATH = '/home/xray/takedata3/mstxray/db/derp.sqlite' #xRayTD3.sqlite' shot = '1' query = 'SELECT * FROM xRayData WHERE sn=?' row = dbutil.fetchall(TD3_DB_XRAY_PATH, query, (shot))[0] print row