LSST Applications  21.0.0-172-gfb10e10a+18fedfabac,22.0.0+297cba6710,22.0.0+80564b0ff1,22.0.0+8d77f4f51a,22.0.0+a28f4c53b1,22.0.0+dcf3732eb2,22.0.1-1-g7d6de66+2a20fdde0d,22.0.1-1-g8e32f31+297cba6710,22.0.1-1-geca5380+7fa3b7d9b6,22.0.1-12-g44dc1dc+2a20fdde0d,22.0.1-15-g6a90155+515f58c32b,22.0.1-16-g9282f48+790f5f2caa,22.0.1-2-g92698f7+dcf3732eb2,22.0.1-2-ga9b0f51+7fa3b7d9b6,22.0.1-2-gd1925c9+bf4f0e694f,22.0.1-24-g1ad7a390+a9625a72a8,22.0.1-25-g5bf6245+3ad8ecd50b,22.0.1-25-gb120d7b+8b5510f75f,22.0.1-27-g97737f7+2a20fdde0d,22.0.1-32-gf62ce7b1+aa4237961e,22.0.1-4-g0b3f228+2a20fdde0d,22.0.1-4-g243d05b+871c1b8305,22.0.1-4-g3a563be+32dcf1063f,22.0.1-4-g44f2e3d+9e4ab0f4fa,22.0.1-42-gca6935d93+ba5e5ca3eb,22.0.1-5-g15c806e+85460ae5f3,22.0.1-5-g58711c4+611d128589,22.0.1-5-g75bb458+99c117b92f,22.0.1-6-g1c63a23+7fa3b7d9b6,22.0.1-6-g50866e6+84ff5a128b,22.0.1-6-g8d3140d+720564cf76,22.0.1-6-gd805d02+cc5644f571,22.0.1-8-ge5750ce+85460ae5f3,master-g6e05de7fdc+babf819c66,master-g99da0e417a+8d77f4f51a,w.2021.48
LSST Data Management Base Package
Classes | Public Member Functions | Static Public Member Functions | Public Attributes | List of all members
lsst.daf.persistence.registries.PosixRegistry Class Reference
Inheritance diagram for lsst.daf.persistence.registries.PosixRegistry:
lsst.daf.persistence.registries.Registry

Classes

class  LookupData
 

Public Member Functions

def __init__ (self, root)
 
def lookup (self, lookupProperties, reference, dataId, **kwargs)
 

Static Public Member Functions

def getHduNumber (template, dataId)
 
def lookupMetadata (filepath, template, lookupData, storage)
 
def lookupFitsMetadata (filepath, template, lookupData, dataId)
 
def create (location)
 

Public Attributes

 root
 

Detailed Description

A glob-based filesystem registry

Definition at line 106 of file registries.py.

Constructor & Destructor Documentation

◆ __init__()

def lsst.daf.persistence.registries.PosixRegistry.__init__ (   self,
  root 
)

Definition at line 109 of file registries.py.

109  def __init__(self, root):
110  Registry.__init__(self)
111  self.root = root
112 

Member Function Documentation

◆ create()

def lsst.daf.persistence.registries.Registry.create (   location)
staticinherited
Create a registry object of an appropriate type.
@param location (string) Path or URL for registry, or None if
                         unavailable

Definition at line 71 of file registries.py.

71  def create(location):
72  """Create a registry object of an appropriate type.
73  @param location (string) Path or URL for registry, or None if
74  unavailable"""
75 
76  if location is None:
77  return
78 
79  # if re.match(r'.*\.registry', location):
80  # return FileRegistry(location)
81 
82  if location.endswith(".pgsql"):
83  return PgsqlRegistry(location)
84 
85  # look for an sqlite3 registry
86  if re.match(r'.*\.sqlite3', location):
87  if not haveSqlite3:
88  raise RuntimeError("sqlite3 registry specified (%s), but unable to import sqlite3 module" %
89  (location,))
90  registry = SqliteRegistry(location)
91  if registry.conn is None:
92  return None
93  return registry
94 
95  # if re.match(r'mysql:', location):
96  # return DbRegistry(location)
97  # return FsRegistry(location)
98 
99  # next try to create a PosixRegistry
100  if os.path.isdir(location):
101  return PosixRegistry(root=location)
102 
103  raise RuntimeError("Unable to create registry using location: " + location)
104 
105 

◆ getHduNumber()

def lsst.daf.persistence.registries.PosixRegistry.getHduNumber (   template,
  dataId 
)
static
Looks up the HDU number for a given template+dataId.
:param template: template with HDU specifier (ends with brackets and an
identifier that can be populated by a key-value pair in dataId.
e.g. "%(visit)07d/instcal%(visit)07d.fits.fz[%(ccdnum)d]"
:param dataId: dictionary that hopefully has a key-value pair whose key
matches (has the same name) as the key specifier in the template.
:return: the HDU specified by the template+dataId pair, or None if the
HDU can not be determined.

Definition at line 114 of file registries.py.

114  def getHduNumber(template, dataId):
115  """Looks up the HDU number for a given template+dataId.
116  :param template: template with HDU specifier (ends with brackets and an
117  identifier that can be populated by a key-value pair in dataId.
118  e.g. "%(visit)07d/instcal%(visit)07d.fits.fz[%(ccdnum)d]"
119  :param dataId: dictionary that hopefully has a key-value pair whose key
120  matches (has the same name) as the key specifier in the template.
121  :return: the HDU specified by the template+dataId pair, or None if the
122  HDU can not be determined.
123  """
124  # sanity check that the template at least ends with a brace.
125  if not template.endswith(']'):
126  return None
127 
128  # get the key (with formatting) out of the brances
129  hduKey = template[template.rfind('[') + 1:template.rfind(']')]
130  # extract the key name from the formatting
131  hduKey = hduKey[hduKey.rfind('(') + 1:hduKey.rfind(')')]
132 
133  if hduKey in dataId:
134  return dataId[hduKey]
135  return None
136 

◆ lookup()

def lsst.daf.persistence.registries.PosixRegistry.lookup (   self,
  lookupProperties,
  reference,
  dataId,
**  kwargs 
)
Perform a lookup in the registry.

Return values are refined by the values in dataId.
Returns a list of values that match keys in lookupProperties.
e.g. if the template is 'raw/raw_v%(visit)d_f%(filter)s.fits.gz', and
dataId={'visit':1}, and lookupProperties is ['filter'], and the
filesystem under self.root has exactly one file 'raw/raw_v1_fg.fits.gz'
then the return value will be [('g',)]

:param lookupProperties: keys whose values will be returned.
:param reference: other data types that may be used to search for values.
:param dataId: must be an iterable. Keys must be string.
If value is a string then will look for elements in the repository that match value for key.
If value is a 2-item iterable then will look for elements in the repository are between (inclusive)
the first and second items in the value.
:param **kwargs: keys required for the posix registry to search for items. If required keys are not
provide will return an empty list.
'template': required. template parameter (typically from a policy) that can be used to look for files
'storage': optional. Needed to look for metadata in files. Currently supported values: 'FitsStorage'.
:return: a list of values that match keys in lookupProperties.

Definition at line 192 of file registries.py.

192  def lookup(self, lookupProperties, reference, dataId, **kwargs):
193  """Perform a lookup in the registry.
194 
195  Return values are refined by the values in dataId.
196  Returns a list of values that match keys in lookupProperties.
197  e.g. if the template is 'raw/raw_v%(visit)d_f%(filter)s.fits.gz', and
198  dataId={'visit':1}, and lookupProperties is ['filter'], and the
199  filesystem under self.root has exactly one file 'raw/raw_v1_fg.fits.gz'
200  then the return value will be [('g',)]
201 
202  :param lookupProperties: keys whose values will be returned.
203  :param reference: other data types that may be used to search for values.
204  :param dataId: must be an iterable. Keys must be string.
205  If value is a string then will look for elements in the repository that match value for key.
206  If value is a 2-item iterable then will look for elements in the repository are between (inclusive)
207  the first and second items in the value.
208  :param **kwargs: keys required for the posix registry to search for items. If required keys are not
209  provide will return an empty list.
210  'template': required. template parameter (typically from a policy) that can be used to look for files
211  'storage': optional. Needed to look for metadata in files. Currently supported values: 'FitsStorage'.
212  :return: a list of values that match keys in lookupProperties.
213  """
214  # required kwargs:
215  if 'template' in kwargs:
216  template = kwargs['template']
217  else:
218  return []
219  # optional kwargs:
220  storage = kwargs['storage'] if 'storage' in kwargs else None
221 
222  lookupData = PosixRegistry.LookupData(lookupProperties, dataId)
223  scanner = fsScanner.FsScanner(template)
224  allPaths = scanner.processPath(self.root)
225  retItems = [] # one item for each found file that matches
226  for path, foundProperties in allPaths.items():
227  # check for dataId keys that are not present in found properties
228  # search for those keys in metadata of file at path
229  # if present, check for matching values
230  # if not present, file can not match, do not use it.
231  lookupData.setFoundItems(foundProperties)
232  if 'incomplete' == lookupData.status():
233  PosixRegistry.lookupMetadata(os.path.join(self.root, path), template, lookupData, storage)
234  if 'match' == lookupData.status():
235  ll = tuple(lookupData.foundItems[key] for key in lookupData.lookupProperties)
236  retItems.append(ll)
237  return retItems
238 

◆ lookupFitsMetadata()

def lsst.daf.persistence.registries.PosixRegistry.lookupFitsMetadata (   filepath,
  template,
  lookupData,
  dataId 
)
static
Look up metadata in a fits file.
Will try to discover the correct HDU to look in by testing if the
template has a value in brackets at the end.
If the HDU is specified but the metadata key is not discovered in
that HDU, will look in the primary HDU before giving up.
:param filepath: path to the file
:param template: template that was used to discover the file. This can
be used to look up the correct HDU as needed.
:param lookupData: an instance if LookupData that contains the
lookupProperties, the dataId, and the data that has been found so far.
Will be updated with new information as discovered.
:param dataId:
:return:

Definition at line 247 of file registries.py.

247  def lookupFitsMetadata(filepath, template, lookupData, dataId):
248  """Look up metadata in a fits file.
249  Will try to discover the correct HDU to look in by testing if the
250  template has a value in brackets at the end.
251  If the HDU is specified but the metadata key is not discovered in
252  that HDU, will look in the primary HDU before giving up.
253  :param filepath: path to the file
254  :param template: template that was used to discover the file. This can
255  be used to look up the correct HDU as needed.
256  :param lookupData: an instance if LookupData that contains the
257  lookupProperties, the dataId, and the data that has been found so far.
258  Will be updated with new information as discovered.
259  :param dataId:
260  :return:
261  """
262  try:
263  hdulist = astropy.io.fits.open(filepath, memmap=True)
264  except IOError:
265  return
266  hduNumber = PosixRegistry.getHduNumber(template=template, dataId=dataId)
267  if hduNumber is not None and hduNumber < len(hdulist):
268  hdu = hdulist[hduNumber]
269  else:
270  hdu = None
271  if len(hdulist) > 0:
272  primaryHdu = hdulist[0]
273  else:
274  primaryHdu = None
275 
276  for property in lookupData.getMissingKeys():
277  propertyValue = None
278  if hdu is not None and property in hdu.header:
279  propertyValue = hdu.header[property]
280  # if the value is not in the indicated HDU, try the primary HDU:
281  elif primaryHdu is not None and property in primaryHdu.header:
282  propertyValue = primaryHdu.header[property]
283  lookupData.addFoundItems({property: propertyValue})
284 
285 

◆ lookupMetadata()

def lsst.daf.persistence.registries.PosixRegistry.lookupMetadata (   filepath,
  template,
  lookupData,
  storage 
)
static
Dispatcher for looking up metadata in a file of a given storage type

Definition at line 240 of file registries.py.

240  def lookupMetadata(filepath, template, lookupData, storage):
241  """Dispatcher for looking up metadata in a file of a given storage type
242  """
243  if storage == 'FitsStorage':
244  PosixRegistry.lookupFitsMetadata(filepath, template, lookupData, storage)
245 

Member Data Documentation

◆ root

lsst.daf.persistence.registries.PosixRegistry.root

Definition at line 111 of file registries.py.


The documentation for this class was generated from the following file: