00001
00002
00003
00004
00005
00006 """
00007 :py:class:`PSCalib.DCFileName` - file name object for Detector Calibration Store (DCS) project.
00008
00009 Usage::
00010
00011 # Import
00012 from PSCalib.DCFileName import DCFileName
00013
00014 # Instatiation
00015 o = DCFileName(env, 'Epix', calibdir='path-to/calib')
00016 # Methods
00017 o.set_dettype(env, src)
00018 o.set_detid(env, src)
00019 status = o.make_path_to_calib_file(mode=0770)
00020
00021 dt = o.dettype()
00022 did = o.detid()
00023 dn = o.detname()
00024
00025 fname = o.calib_file_name() # e.g., epix100a-3925868555.h5
00026 fdir = o.calib_file_dir() # e.g., /reg/neh/home4/dubrovin/LCLS/rel-calib/calib
00027 fpath = o.calib_file_path() # e.g., /reg/neh/home4/dubrovin/LCLS/rel-calib/calib/epix100a/epix100a-3925868555.h5
00028 fdir = o.calib_file_dir_repo() # e.g., /reg/g/psdm/detector/calib/epix100a/
00029 fpath = o.calib_file_path_repo() # e.g., /reg/g/psdm/detector/calib/epix100a/epix100a-3925868555.h5
00030 o.print_attrs() # print attributes
00031 o.log_attrs() # dump attributes in the logger
00032 s = o.str_attrs() # returns a string of attributes
00033
00034
00035 @see project modules
00036 * :py:class:`PSCalib.DCStore`
00037 * :py:class:`PSCalib.DCType`
00038 * :py:class:`PSCalib.DCRange`
00039 * :py:class:`PSCalib.DCVersion`
00040 * :py:class:`PSCalib.DCBase`
00041 * :py:class:`PSCalib.DCInterface`
00042 * :py:class:`PSCalib.DCUtils`
00043 * :py:class:`PSCalib.DCDetectorId`
00044 * :py:class:`PSCalib.DCConfigParameters`
00045 * :py:class:`PSCalib.DCFileName`
00046 * :py:class:`PSCalib.DCLogger`
00047 * :py:class:`PSCalib.DCMethods`
00048 * :py:class:`PSCalib.DCEmail`
00049
00050
00051 This software was developed for the SIT project.
00052 If you use all or part of it, please give an appropriate acknowledgment.
00053
00054 @version $Id: DCFileName.py 12777 2016-10-19 21:37:38Z dubrovin@SLAC.STANFORD.EDU $
00055
00056 @author Mikhail S. Dubrovin
00057 """
00058
00059
00060 __version__ = "$Revision: 12777 $"
00061
00062
00063 import os
00064 from PSCalib.DCLogger import log
00065 import PSCalib.DCUtils as gu
00066 import PSCalib.DCDetectorId as did
00067 from PSCalib.DCConfigParameters import cp
00068
00069
00070
00071 class DCFileName() :
00072 """File name generator object for Detector Calibration Store (DCS) project.
00073
00074 Parameters
00075
00076 evt : psana.Event -> event time
00077 src : str - source short/full name, alias or full
00078 calibdir : str - fallback path to calib dir (if xtc file is copied - calib and experiment name are lost)
00079 """
00080
00081 fname_ext = 'h5'
00082 notype = 'notype'
00083 noid = 'noid'
00084
00085 def __init__(self, env, src, calibdir=None) :
00086 self._name = self.__class__.__name__
00087 log.debug('c-tor', self._name)
00088 self._env = env
00089 self._src = src
00090 self._set_detname(env, src)
00091 self._set_calib_dir(env, calibdir)
00092
00093
00094 def str_attrs(self) :
00095 return '%s attributes:' % self._name\
00096 + '\n env : %s' % self._env\
00097 + '\n src : %s' % self._src\
00098 + '\n src_name : %s' % self._src_name\
00099 + '\n dettype : %s' % self.dettype()\
00100 + '\n detid : %s' % self.detid()\
00101 + '\n detname : %s' % self.detname()\
00102 + '\n file name: %s' % self.calib_file_name()\
00103 + '\n calibdir : %s' % self._calibdir\
00104 + '\n file dir : %s' % self.calib_file_dir()\
00105 + '\n file path: %s' % self.calib_file_path()\
00106 + '\n repo dir : %s' % self.calib_file_dir_repo()\
00107 + '\n repo path: %s' % self.calib_file_path_repo()
00108
00109
00110 def print_attrs(self) :
00111 print self.str_attrs()
00112
00113
00114 def log_attrs(self) :
00115 log.info(self.str_attrs(), self._name)
00116
00117
00118 def set_dettype(self, env, src) :
00119 self._src_name = gu.source_full_name(env, src)
00120 if self._src_name is None :
00121 self._dettype = self.notype
00122 return
00123 self._dettype = gu.dettype_from_str_source(self._src_name).lower()
00124 if self._dettype is None :
00125 self._dettype = self.notype
00126
00127
00128 def set_detid(self, env, src) :
00129 if self._dettype == 'epix100a': self._detid = did.id_epix(env, src)
00130 else : self._detid = did.id_det_noid(env, src)
00131 if self._detid is None : self._detid = self.noid
00132
00133
00134 def _set_detname(self, env, src) :
00135 self.set_dettype(env, src)
00136 self.set_detid(env, self._src_name)
00137 self._detname = '%s-%s' % (self._dettype, self._detid.replace(':','-'))
00138
00139
00140 def _set_calib_dir(self, env, calibdir=None) :
00141 if calibdir is not None and calibdir != 'None' :
00142 self._calibdir = calibdir
00143 return
00144
00145 cdir = env.calibDir()
00146 self._calibdir = cdir
00147 self._calibdir = None if '///' in cdir else cdir
00148
00149
00150 def dettype(self) :
00151 """Returns detector id, ex.: epix100a"""
00152 return self._dettype
00153
00154
00155 def detid(self) :
00156 """Returns detector id, ex.: 3925868555"""
00157 return self._detid
00158
00159
00160 def detname(self) :
00161 """Returns detector name, ex.: epix100a-3925868555"""
00162 return self._detname
00163
00164
00165 def calib_file_dir(self) :
00166 """Returns file directory name, ex.: .../calib/epix100a/"""
00167 if self._calibdir is None : return None
00168 else : return '%s/%s' % (self._calibdir, self._dettype)
00169
00170
00171 def calib_file_dir_repo(self) :
00172 """Returns repository directory, ex.: /reg/g/psdm/detector/calib/epix100a/"""
00173 return '%s/%s' % (cp.dir_repo.value(), self._dettype)
00174
00175
00176 def calib_file_name(self) :
00177 """Returns file name, ex.: epix100a-3925868555.h5"""
00178 return '%s.%s' % (self._detname, self.fname_ext)
00179
00180
00181 def calib_file_path(self) :
00182 """Returns path to the file, ex.: .../calib/epix100a/epix100a-3925868555.h5"""
00183 if self._calibdir is None : return None
00184 else : return '%s/%s/%s.%s' % (self._calibdir, self._dettype, self._detname, self.fname_ext)
00185
00186
00187 def calib_file_path_repo(self) :
00188 """Returns path to the file in repository, ex.: /reg/g/.../calib/epix100a/epix100a-3925868555.h5"""
00189 return '%s/%s/%s.%s' % (cp.dir_repo.value(), self._dettype, self._detname, self.fname_ext)
00190
00191
00192 def make_path_to_calib_file(self, depth=2, mode=0775) :
00193 """Creates path beginning from calib directory, ex.: .../calib/epix100a/
00194 Returns True if path created and exists.
00195 """
00196 fdir = self.calib_file_dir()
00197
00198 return gu.create_path(fdir, depth, mode)
00199
00200
00201 def _parse_path_to_file(self, pathf) :
00202
00203
00204 if os.path.exists(pathf) :
00205 self.path, self.fname = os.path.split(pathf)
00206
00207
00208 if pathf is None\
00209 or pathf is '' : raise IOError('%s: File name "%s" is not allowed'%(self._name, pathf))
00210
00211 self.path = pathf
00212
00213
00214 self.fname = '%s.h5'%self.fname if self.ext != 'h5' else self.fname
00215
00216
00217 if pathf == '' : self.fname = os.path.join(cp.repo.value(), self.fname)
00218
00219
00220 log.info('Set file name: %s'%self.fname, self._name)
00221
00222
00223 def __del__(self) :
00224 log.debug('d-tor', self._name)
00225
00226
00227
00228 def test_DCFileName() :
00229 print 20*'_', '\n%s:' % sys._getframe().f_code.co_name
00230
00231 import psana
00232 ds = psana.DataSource('/reg/g/psdm/detector/data_test/types/0007-NoDetector.0-Epix100a.0.xtc')
00233 env=ds.env()
00234
00235
00236 ofn1 = DCFileName(env, 'Imp'); ofn1.print_attrs()
00237 ofn2 = DCFileName(env, 'Epix', calibdir='path-to/calib'); ofn2.print_attrs()
00238 ofn3 = DCFileName(env, 'cs140_0'); ofn3.print_attrs()
00239 ofn4 = DCFileName(env, 'Cspad.', calibdir='path-to/calib'); ofn4.print_attrs()
00240 ds = psana.DataSource('exp=cxif5315:run=129')
00241 env=ds.env()
00242 ofn5 = DCFileName(env, 'Cspad.'); ofn5.print_attrs()
00243
00244
00245
00246 def test_make_path_to_calib_file() :
00247 print 20*'_', '\n%s:' % sys._getframe().f_code.co_name
00248
00249 import psana
00250 ds = psana.DataSource('/reg/g/psdm/detector/data_test/types/0007-NoDetector.0-Epix100a.0.xtc')
00251
00252 ofn = DCFileName(ds.env(), 'Epix', calibdir='%s/calib' % gu.get_cwd())
00253
00254 ofn.print_attrs()
00255 ofn.make_path_to_calib_file(mode=0770)
00256
00257
00258
00259 def do_test() :
00260 log.setPrintBits(0377)
00261
00262 tname = sys.argv[1] if len(sys.argv) > 1 else '0'
00263 print 50*'_', '\nTest %s:' % tname
00264 if tname == '0' : test_DCFileName()
00265 elif tname == '1' : test_DCFileName()
00266 elif tname == '2' : test_make_path_to_calib_file()
00267 else : print 'Not-recognized test: %s' % tname
00268 sys.exit('End of test %s' % tname)
00269
00270
00271
00272 if __name__ == "__main__" :
00273 import sys; global sys
00274 do_test()
00275
00276