Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:48:49

0001 #!/usr/bin/env python
0002 #
0003 # Copyright (c) 2019 Opticks Team. All Rights Reserved.
0004 #
0005 # This file is part of Opticks
0006 # (see https://bitbucket.org/simoncblyth/opticks).
0007 #
0008 # Licensed under the Apache License, Version 2.0 (the "License"); 
0009 # you may not use this file except in compliance with the License.  
0010 # You may obtain a copy of the License at
0011 #
0012 #   http://www.apache.org/licenses/LICENSE-2.0
0013 #
0014 # Unless required by applicable law or agreed to in writing, software 
0015 # distributed under the License is distributed on an "AS IS" BASIS, 
0016 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
0017 # See the License for the specific language governing permissions and 
0018 # limitations under the License.
0019 #
0020 
0021 """
0022 meta.py
0023 ========
0024 
0025 Attempt at more general metadata handling than metadata.py.
0026 for example read the names of the files in the dir do not 
0027 assume certain ones are there, just read them according to extensions.
0028 
0029 """
0030 from datetime import datetime
0031 import os, re, logging, sys
0032 import numpy as np
0033 log = logging.getLogger(__name__)
0034 
0035 from opticks.ana.base import ini_, json_, splitlines_
0036 from opticks.ana.datedfolder import DatedFolder, dateparser
0037 
0038 class Meta(object):
0039     def __init__(self, path, base):
0040         """
0041         :param path: path of dated folder relative to base directory 
0042         :param base: directory 
0043 
0044         Reads json and ini files from the directory identified
0045         ito the d dict keyed on file stem names.
0046         """
0047         self.path = path
0048         self.base = base
0049          
0050 
0051         absdir = os.path.join(base, path) 
0052         self.absdir = absdir 
0053 
0054         self.datefold = os.path.basename(path)
0055         self.timestamp = dateparser(self.datefold)
0056         self.parentfold = os.path.basename(os.path.dirname(path))        
0057         assert self.timestamp is not None, "expected datedfolder argument %s " % path 
0058 
0059         self.d = {}
0060         for n in os.listdir(absdir):
0061             p = os.path.join(absdir,n)
0062             if not os.path.isfile(p): continue
0063             stem,ext = os.path.splitext(n) 
0064             if ext == ".json":
0065                 self.d[stem] = json_(p)
0066             elif ext == ".ini":
0067                 self.d[stem] = ini_(p)
0068             else:
0069                 pass
0070             pass
0071         pass
0072 
0073 
0074     def __getitem__(self, kspec):
0075         """
0076         :param kspec: eg parameters.NumPhotons or OpticksEvent_launch.launch001
0077         :return value: 
0078 
0079         Provides values from metadata dicts read from files such as parameters.json 
0080         or OpticksEvent_launch.ini from the dated folder.
0081         """
0082         elem = kspec.split(".")
0083         assert len(elem) == 2
0084         top, key = elem 
0085         assert top in self.d, (top, d.keys())
0086         p = self.d[top]
0087         return p.get(key, -1)
0088 
0089     def __repr__(self):
0090         return "%50s : %30s : %20s : %s " % ( self.path, self.parentfold, self.timestamp, repr(self.d.keys()) )
0091 
0092 
0093 if __name__ == '__main__':
0094     base = sys.argv[1] if len(sys.argv) > 1 else "." 
0095     dirs, dfolds, dtimes = DatedFolder.find(base)
0096     assert len(dfolds) == len(dtimes)
0097 
0098     for p in dirs:
0099         m = Meta(p, base)
0100         print(m)
0101     pass
0102 
0103     
0104