00001
00002
00003
00004
00005
00006
00007
00008 """
00009 :py:class:`PSCalib.DCUtils` - contains a set of utilities
00010
00011 Usage::
00012
00013 # Import
00014 import PSCalib.DCUtils as gu
00015
00016 # Methods
00017 # Get string with time stamp, ex: 2016-01-26T10:40:53
00018 ts = gu.str_tstamp(fmt='%Y-%m-%dT%H:%M:%S', time_sec=None)
00019
00020 usr = gu.get_enviroment(env='USER')
00021 usr = gu.get_login()
00022 host = gu.get_hostname()
00023 cwd = gu.get_cwd()
00024 gu.create_directory(dir, mode=0775)
00025 gu.create_path(path, depth=2, mode=0775)
00026 gu.save_string_as_dset(grp, name, s)
00027 src = gu.source_full_name(env, src)
00028 dtype = gu.dettype_from_str_source(src)
00029
00030 src = gu.string_from_source(source) # source is psana.Source object or string like
00031 # 'CxiDs2.0:Cspad.0' from 'DetInfo(CxiDs2.0:Cspad.0)'
00032 dname = gu.detector_full_name(env, src)
00033 source = gu.psana_source(env, srcpar)
00034 t_sec = gu.evt_time(evt)
00035 fid = gu.evt_fiducials(evt)
00036
00037 # methods for HDF5
00038 sg = gu.get_subgroup(grp, subgr_name)
00039 gu.delete_object(grp, oname)
00040 gu.save_object_as_dset(grp, name, shape=None, dtype=None, data=0)
00041
00042
00043 @see project modules
00044 * :py:class:`PSCalib.DCStore`
00045 * :py:class:`PSCalib.DCType`
00046 * :py:class:`PSCalib.DCRange`
00047 * :py:class:`PSCalib.DCVersion`
00048 * :py:class:`PSCalib.DCBase`
00049 * :py:class:`PSCalib.DCInterface`
00050 * :py:class:`PSCalib.DCUtils`
00051 * :py:class:`PSCalib.DCDetectorId`
00052 * :py:class:`PSCalib.DCConfigParameters`
00053 * :py:class:`PSCalib.DCFileName`
00054 * :py:class:`PSCalib.DCLogger`
00055 * :py:class:`PSCalib.DCMethods`
00056 * :py:class:`PSCalib.DCEmail`
00057
00058 This software was developed for the SIT project.
00059 If you use all or part of it, please give an appropriate acknowledgment.
00060
00061 @version $Id: 2013-03-08$
00062
00063 @author Mikhail S. Dubrovin
00064 """
00065
00066
00067 __version__ = "$Revision: 12777 $"
00068
00069
00070 import sys
00071 import os
00072 import getpass
00073 import socket
00074 import numpy as np
00075 from time import localtime, strftime, time
00076 import psana
00077 from PSCalib.DCLogger import log
00078 import PSCalib.GlobalUtils as gu
00079
00080
00081
00082 class h5py_proxy :
00083 def __init__(self) :
00084 self.h5py = None
00085 self.Dataset = None
00086 self.Group = None
00087 self.File = None
00088
00089 def fetch_h5py(self) :
00090 if self.h5py is None :
00091 import h5py
00092 self.h5py = h5py
00093
00094
00095
00096 import h5py
00097
00098
00099 class Storage :
00100 def __init__(self) :
00101 self.dataset_t = h5py.Dataset
00102 self.group_t = h5py.Group
00103 self.File = h5py.File
00104
00105
00106
00107 sp = Storage()
00108
00109
00110
00111 def str_tstamp(fmt='%Y-%m-%dT%H:%M:%S', time_sec=None) :
00112 """Returns string timestamp for specified format and time in sec or current time by default
00113 """
00114 return strftime(fmt, localtime(time_sec))
00115
00116
00117
00118 def get_enviroment(env='USER') :
00119 """Returns the value of specified by string name environment variable
00120 """
00121 return os.environ[env]
00122
00123
00124
00125 def get_login() :
00126 """Returns login name
00127 """
00128
00129 return getpass.getuser()
00130
00131
00132
00133 def get_hostname() :
00134 """Returns login name
00135 """
00136
00137 return socket.gethostname()
00138
00139
00140
00141 def get_cwd() :
00142 """Returns current working directory
00143 """
00144 return os.getcwd()
00145
00146
00147
00148 def create_directory_v0(dir, verb=False) :
00149 if os.path.exists(dir) :
00150 if verb : print 'Directory exists: %s' % dir
00151 else :
00152 os.makedirs(dir)
00153 if verb : print 'Directory created: %s' % dir
00154
00155
00156
00157 def create_directory(dir, mode=0775) :
00158
00159 if os.path.exists(dir) :
00160 log.debug('Directory exists: %s' % dir, __name__)
00161 else :
00162 os.makedirs(dir, mode)
00163
00164
00165 log.info('Directory created: %s' % dir, __name__)
00166
00167
00168
00169 def create_path(path, depth=2, mode=0775) :
00170
00171 subdirs = path.rstrip('/').rsplit('/', depth)
00172 log.debug('subdirs: %s' % str(subdirs), __name__)
00173 cpath = subdirs[0]
00174 length = len(subdirs)
00175 for i,sd in enumerate(subdirs[1:]) :
00176 cpath += '/%s'% sd
00177
00178 create_directory(cpath, mode)
00179
00180
00181 return os.path.exists(cpath)
00182
00183
00184
00185 def save_string_as_dset(grp, name, s) :
00186 """Creates and returns the h5py dataset object with name for single string s
00187 """
00188 if s is None : return None
00189
00190
00191 dset = grp.create_dataset(name, shape=(1,), dtype='S%d'%len(s))
00192 dset[0] = s
00193 return dset
00194
00195
00196
00197 def source_full_name(env, src) :
00198 """Returns full name like 'DetInfo(XppGon.0:Cspad2x2.0)' of the brief source or its alias
00199 using env.configStore().keys()
00200 """
00201 str_src = str(src)
00202 for k in env.configStore().keys() :
00203 if str_src in str(k.src())\
00204 or str_src == str(k.alias()) : return k.src()
00205 return None
00206
00207
00208
00209 def dettype_from_str_source(src) :
00210 """Returns the detector type from full psana source name (Ex.: Cspad2x2 from DetInfo(XppGon.0:Cspad2x2.0)
00211 """
00212 str_src = str(src)
00213 str_split = str_src.rsplit(':',1)
00214 detname = str_split[1].split('.',1) if len(str_split)>1 else None
00215 return detname[0] if len(detname)>1 else None
00216
00217
00218
00219 def string_from_source(source) :
00220 """Returns string like 'CxiDs2.0:Cspad.0' from 'DetInfo(CxiDs2.0:Cspad.0)'
00221 or 'DsaCsPad' from 'Source('DsaCsPad')' form input string or psana.Source object
00222 """
00223 str_src = str(source)
00224 if '"' in str_src : return str_src.split('"')[1]
00225 str_split = str_src.rsplit('(',1)
00226 return str_split[1].split(')',1)[0] if len(str_split)>1 else str_src
00227
00228
00229
00230 def detector_full_name(env, src) :
00231 """Returns full detector name like 'XppGon.0:Cspad2x2.0' for short src, alias src, or psana.Source.
00232 """
00233 str_src = str(src)
00234 str_src = string_from_source(str_src)
00235 if str_src is None : return None
00236 str_src = source_full_name(env, str_src)
00237 if str_src is None : return None
00238 return string_from_source(str_src)
00239
00240
00241
00242 def psana_source(env, srcpar) :
00243 """returns psana.Source(src) from other psana.Source brief src or alias.
00244
00245 Parameters
00246
00247 srcpar : str - regular source or its alias, ex.: 'XppEndstation.0:Rayonix.0' or 'rayonix'
00248 set_sub : bool - default=True - propagates source parameter to low level package
00249 """
00250
00251
00252 src = srcpar if isinstance(srcpar, psana.Source) else psana.Source(srcpar)
00253 str_src = string_from_source(src)
00254
00255 amap = env.aliasMap()
00256 psasrc = amap.src(str_src)
00257 source = src if amap.alias(psasrc) == '' else amap.src(str_src)
00258
00259 if not isinstance(source, psana.Source) : source = psana.Source(source)
00260 return source
00261
00262
00263
00264 def get_subgroup(grp, subgr_name) :
00265 """For hdf5:
00266 returns subgroup of the group if it exists or creates and returns new subgroup
00267 """
00268
00269 if subgr_name in grp : return grp[subgr_name]
00270 return grp.create_group(subgr_name)
00271
00272
00273
00274 def delete_object(grp, oname) :
00275 """For hdf5: removes object from group.
00276 """
00277
00278
00279 if oname in grp : del grp[oname]
00280
00281
00282
00283
00284 def save_object_as_dset(grp, name, shape=None, dtype=None, data=0) :
00285 """Saves object as h5py dataset
00286
00287 Currently supports scalar int, double, string and numpy.array
00288 """
00289
00290
00291
00292 if name in grp.keys() : return
00293
00294 if isinstance(data, np.ndarray) :
00295 return grp.create_dataset(name, data=data)
00296
00297 sh = (1,) if shape is None else shape
00298 if dtype is not None :
00299 return grp.create_dataset(name, shape=sh, dtype=dtype, data=data)
00300
00301 if isinstance(data, str) :
00302 return save_string_as_dset(grp, name, data)
00303
00304 if isinstance(data, int) :
00305 return grp.create_dataset(name, shape=sh, dtype='int', data=data)
00306
00307 if isinstance(data, float) :
00308 return grp.create_dataset(name, shape=sh, dtype='double', data=data)
00309
00310 log.warning("Can't save parameter: %s of %s in the h5py group: %s" % (name, str(dtype), grp.name), 'DCUtils.save_object_as_dset')
00311
00312
00313
00314 def evt_time(evt) :
00315 """Returns event (double) time.
00316 """
00317 evid = evt.get(psana.EventId)
00318 ttuple = evid.time()
00319
00320 return float(ttuple[0]) + float(ttuple[1])*1e-9
00321
00322
00323
00324 def evt_fiducials(evt) :
00325 """Returns event fiducials.
00326 """
00327 evid = evt.get(psana.EventId)
00328 return evid.fiducials()
00329
00330
00331
00332
00333
00334
00335
00336 def test_source_full_name() :
00337 ds = psana.DataSource('/reg/g/psdm/detector/data_test/types/0007-NoDetector.0-Epix100a.0.xtc')
00338 env=ds.env()
00339 print 20*'_', '\n%s:' % sys._getframe().f_code.co_name
00340 print 'src="Epix" :', source_full_name(env, 'Epix')
00341 print 'src="Cspad." :', source_full_name(env, 'Cspad.')
00342 print 'src="Cspad" :', source_full_name(env, 'Cspad')
00343 print 'src="cs140_0" :', source_full_name(env, 'cs140_0')
00344
00345
00346
00347 def test_string_from_source() :
00348 ds = psana.DataSource('/reg/g/psdm/detector/data_test/types/0007-NoDetector.0-Epix100a.0.xtc')
00349 env=ds.env()
00350 source = psana_source(env, 'cs140_0')
00351 print 20*'_', '\n%s:' % sys._getframe().f_code.co_name
00352 print 'source', source
00353 print 'string_from_source', string_from_source(source)
00354
00355
00356
00357 def test_psana_source() :
00358 ds = psana.DataSource('/reg/g/psdm/detector/data_test/types/0007-NoDetector.0-Epix100a.0.xtc')
00359 env=ds.env()
00360 print 20*'_', '\n%s:' % sys._getframe().f_code.co_name
00361 print 'psana_source(env, "Epix") :', psana_source(env, 'Epix')
00362 print 'psana_source(env, "Cspad.") :', psana_source(env, 'Cspad.')
00363 print 'psana_source(env, "Cspad") :', psana_source(env, 'Cspad')
00364 print 'psana_source(env, "cs140_0") :', psana_source(env, 'cs140_0')
00365
00366
00367
00368 def test_detector_full_name() :
00369 ds = psana.DataSource('/reg/g/psdm/detector/data_test/types/0007-NoDetector.0-Epix100a.0.xtc')
00370 env=ds.env()
00371 print 20*'_', '\n%s:' % sys._getframe().f_code.co_name
00372 print 'src="Epix" :', detector_full_name(env, "Epix")
00373 print 'src=psana.Source("Epix")) :', detector_full_name(env, psana.Source('Epix'))
00374 print 'src="DetInfo(NoDetector.0:Epix100a.0)":', detector_full_name(env, 'DetInfo(NoDetector.0:Epix100a.0)')
00375 print 'for alias src="cs140_0" :', detector_full_name(env, 'cs140_0')
00376
00377
00378
00379 def test_evt_time() :
00380 ds = psana.DataSource('/reg/g/psdm/detector/data_test/types/0007-NoDetector.0-Epix100a.0.xtc')
00381 evt=ds.events().next()
00382 print 20*'_', '\n%s:' % sys._getframe().f_code.co_name
00383 t=evt_time(evt)
00384 print 'evt_time(evt) : %.9f' % t
00385
00386
00387
00388 def test_misc() :
00389 print 20*'_', '\n%s:' % sys._getframe().f_code.co_name
00390 print 'get_enviroment(USER) : %s' % get_enviroment()
00391 print 'get_login() : %s' % get_login()
00392 print 'get_hostname() : %s' % get_hostname()
00393 print 'get_cwd() : %s' % get_cwd()
00394
00395
00396
00397 def do_test() :
00398 import sys; global sys
00399 tname = sys.argv[1] if len(sys.argv) > 1 else '0'
00400 print 50*'_', '\nTest %s:' % tname
00401 if tname == '0' : test_misc(); test_source_full_name(); test_string_from_source();\
00402 test_psana_source(); test_detector_full_name()
00403 elif tname == '1' : test_source_full_name()
00404 elif tname == '2' : test_string_from_source()
00405 elif tname == '3' : test_psana_source()
00406 elif tname == '4' : test_detector_full_name()
00407 elif tname == '5' : test_evt_time()
00408 else : print 'Not-recognized test name: %s' % tname
00409 sys.exit('End of test %s' % tname)
00410
00411
00412
00413 if __name__ == "__main__" :
00414 do_test()
00415
00416