LSST Applications  21.0.0-147-g0e635eb1+1acddb5be5,22.0.0+052faf71bd,22.0.0+1ea9a8b2b2,22.0.0+6312710a6c,22.0.0+729191ecac,22.0.0+7589c3a021,22.0.0+9f079a9461,22.0.1-1-g7d6de66+b8044ec9de,22.0.1-1-g87000a6+536b1ee016,22.0.1-1-g8e32f31+6312710a6c,22.0.1-10-gd060f87+016f7cdc03,22.0.1-12-g9c3108e+df145f6f68,22.0.1-16-g314fa6d+c825727ab8,22.0.1-19-g93a5c75+d23f2fb6d8,22.0.1-19-gb93eaa13+aab3ef7709,22.0.1-2-g8ef0a89+b8044ec9de,22.0.1-2-g92698f7+9f079a9461,22.0.1-2-ga9b0f51+052faf71bd,22.0.1-2-gac51dbf+052faf71bd,22.0.1-2-gb66926d+6312710a6c,22.0.1-2-gcb770ba+09e3807989,22.0.1-20-g32debb5+b8044ec9de,22.0.1-23-gc2439a9a+fb0756638e,22.0.1-3-g496fd5d+09117f784f,22.0.1-3-g59f966b+1e6ba2c031,22.0.1-3-g849a1b8+f8b568069f,22.0.1-3-gaaec9c0+c5c846a8b1,22.0.1-32-g5ddfab5d3+60ce4897b0,22.0.1-4-g037fbe1+64e601228d,22.0.1-4-g8623105+b8044ec9de,22.0.1-5-g096abc9+d18c45d440,22.0.1-5-g15c806e+57f5c03693,22.0.1-7-gba73697+57f5c03693,master-g6e05de7fdc+c1283a92b8,master-g72cdda8301+729191ecac,w.2021.39
LSST Data Management Base Package
Classes | Functions
lsst.daf.persistence.posixStorage Namespace Reference

Classes

class  PosixStorage
 

Functions

def readConfigStorage (butlerLocation)
 
def writeConfigStorage (butlerLocation, obj)
 
def readFitsStorage (butlerLocation)
 
def writeFitsStorage (butlerLocation, obj)
 
def readParquetStorage (butlerLocation)
 
def writeParquetStorage (butlerLocation, obj)
 
def writeYamlStorage (butlerLocation, obj)
 
def readPickleStorage (butlerLocation)
 
def writePickleStorage (butlerLocation, obj)
 
def readFitsCatalogStorage (butlerLocation)
 
def writeFitsCatalogStorage (butlerLocation, obj)
 
def readMatplotlibStorage (butlerLocation)
 
def writeMatplotlibStorage (butlerLocation, obj)
 
def readYamlStorage (butlerLocation)
 

Function Documentation

◆ readConfigStorage()

def lsst.daf.persistence.posixStorage.readConfigStorage (   butlerLocation)
Read an lsst.pex.config.Config from a butlerLocation.

Parameters
----------
butlerLocation : ButlerLocation
    The location for the object(s) to be read.

Returns
-------
A list of objects as described by the butler location. One item for
each location in butlerLocation.getLocations()

Definition at line 511 of file posixStorage.py.

511 def readConfigStorage(butlerLocation):
512  """Read an lsst.pex.config.Config from a butlerLocation.
513 
514  Parameters
515  ----------
516  butlerLocation : ButlerLocation
517  The location for the object(s) to be read.
518 
519  Returns
520  -------
521  A list of objects as described by the butler location. One item for
522  each location in butlerLocation.getLocations()
523  """
524  results = []
525  for locationString in butlerLocation.getLocations():
526  locStringWithRoot = os.path.join(butlerLocation.getStorage().root, locationString)
527  logLoc = LogicalLocation(locStringWithRoot, butlerLocation.getAdditionalData())
528  if not os.path.exists(logLoc.locString()):
529  raise RuntimeError("No such config file: " + logLoc.locString())
530  pythonType = butlerLocation.getPythonType()
531  if pythonType is not None:
532  if isinstance(pythonType, str):
533  pythonType = doImport(pythonType)
534  finalItem = pythonType()
535  finalItem.load(logLoc.locString())
536  results.append(finalItem)
537  return results
538 
539 
def readConfigStorage(butlerLocation)

◆ readFitsCatalogStorage()

def lsst.daf.persistence.posixStorage.readFitsCatalogStorage (   butlerLocation)
Read a catalog from a FITS table specified by ButlerLocation.

Parameters
----------
butlerLocation : ButlerLocation
    The location for the object(s) to be read.

Returns
-------
A list of objects as described by the butler location. One item for
each location in butlerLocation.getLocations()

Definition at line 768 of file posixStorage.py.

768 def readFitsCatalogStorage(butlerLocation):
769  """Read a catalog from a FITS table specified by ButlerLocation.
770 
771  Parameters
772  ----------
773  butlerLocation : ButlerLocation
774  The location for the object(s) to be read.
775 
776  Returns
777  -------
778  A list of objects as described by the butler location. One item for
779  each location in butlerLocation.getLocations()
780  """
781  pythonType = butlerLocation.getPythonType()
782  if pythonType is not None:
783  if isinstance(pythonType, str):
784  pythonType = doImport(pythonType)
785  results = []
786  additionalData = butlerLocation.getAdditionalData()
787  for locationString in butlerLocation.getLocations():
788  locStringWithRoot = os.path.join(butlerLocation.getStorage().root, locationString)
789  logLoc = LogicalLocation(locStringWithRoot, additionalData)
790  if not os.path.exists(logLoc.locString()):
791  raise RuntimeError("No such FITS catalog file: " + logLoc.locString())
792  kwds = {}
793  if additionalData.exists("hdu"):
794  kwds["hdu"] = additionalData.getInt("hdu")
795  if additionalData.exists("flags"):
796  kwds["flags"] = additionalData.getInt("flags")
797  finalItem = pythonType.readFits(logLoc.locString(), **kwds)
798  results.append(finalItem)
799  return results
800 
801 
def readFitsCatalogStorage(butlerLocation)

◆ readFitsStorage()

def lsst.daf.persistence.posixStorage.readFitsStorage (   butlerLocation)
Read objects from a FITS file specified by ButlerLocation.

The object is read using class or static method
``readFitsWithOptions(path, options)``, if it exists, else
``readFits(path)``. The ``options`` argument is the data returned by
``butlerLocation.getAdditionalData()``.

Parameters
----------
butlerLocation : ButlerLocation
    The location for the object(s) to be read.

Returns
-------
A list of objects as described by the butler location. One item for
each location in butlerLocation.getLocations()

Definition at line 557 of file posixStorage.py.

557 def readFitsStorage(butlerLocation):
558  """Read objects from a FITS file specified by ButlerLocation.
559 
560  The object is read using class or static method
561  ``readFitsWithOptions(path, options)``, if it exists, else
562  ``readFits(path)``. The ``options`` argument is the data returned by
563  ``butlerLocation.getAdditionalData()``.
564 
565  Parameters
566  ----------
567  butlerLocation : ButlerLocation
568  The location for the object(s) to be read.
569 
570  Returns
571  -------
572  A list of objects as described by the butler location. One item for
573  each location in butlerLocation.getLocations()
574  """
575  pythonType = butlerLocation.getPythonType()
576  if pythonType is not None:
577  if isinstance(pythonType, str):
578  pythonType = doImport(pythonType)
579  supportsOptions = hasattr(pythonType, "readFitsWithOptions")
580  if not supportsOptions:
581  from lsst.daf.base import PropertySet, PropertyList
582  if issubclass(pythonType, (PropertySet, PropertyList)):
583  from lsst.afw.fits import readMetadata
584  reader = readMetadata
585  else:
586  reader = pythonType.readFits
587  results = []
588  additionalData = butlerLocation.getAdditionalData()
589  for locationString in butlerLocation.getLocations():
590  locStringWithRoot = os.path.join(butlerLocation.getStorage().root, locationString)
591  logLoc = LogicalLocation(locStringWithRoot, additionalData)
592  # test for existence of file, ignoring trailing [...]
593  # because that can specify the HDU or other information
594  filePath = re.sub(r"(\.fits(.[a-zA-Z0-9]+)?)(\[.+\])$", r"\1", logLoc.locString())
595  if not os.path.exists(filePath):
596  raise RuntimeError("No such FITS file: " + logLoc.locString())
597  if supportsOptions:
598  finalItem = pythonType.readFitsWithOptions(logLoc.locString(), options=additionalData)
599  else:
600  fileName = logLoc.locString()
601  mat = re.search(r"^(.*)\[(\d+)\]$", fileName)
602 
603  if mat and reader == readMetadata: # readMetadata() only understands the hdu argument, not [hdu]
604  fileName = mat.group(1)
605  hdu = int(mat.group(2))
606 
607  finalItem = reader(fileName, hdu=hdu)
608  else:
609  finalItem = reader(fileName)
610  results.append(finalItem)
611  return results
612 
613 
def readFitsStorage(butlerLocation)

◆ readMatplotlibStorage()

def lsst.daf.persistence.posixStorage.readMatplotlibStorage (   butlerLocation)
Read from a butlerLocation (always fails for this storage type).

Parameters
----------
butlerLocation : ButlerLocation
    The location for the object(s) to be read.

Returns
-------
A list of objects as described by the butler location. One item for
each location in butlerLocation.getLocations()

Definition at line 823 of file posixStorage.py.

823 def readMatplotlibStorage(butlerLocation):
824  """Read from a butlerLocation (always fails for this storage type).
825 
826  Parameters
827  ----------
828  butlerLocation : ButlerLocation
829  The location for the object(s) to be read.
830 
831  Returns
832  -------
833  A list of objects as described by the butler location. One item for
834  each location in butlerLocation.getLocations()
835  """
836  raise NotImplementedError("Figures saved with MatplotlibStorage cannot be retreived using the Butler.")
837 
838 
def readMatplotlibStorage(butlerLocation)

◆ readParquetStorage()

def lsst.daf.persistence.posixStorage.readParquetStorage (   butlerLocation)
Read a catalog from a Parquet file specified by ButlerLocation.

The object returned by this is expected to be a subtype
of `ParquetTable`, which is a thin wrapper to `pyarrow.ParquetFile`
that allows for lazy loading of the data.

Parameters
----------
butlerLocation : ButlerLocation
    The location for the object(s) to be read.

Returns
-------
A list of objects as described by the butler location. One item for
each location in butlerLocation.getLocations()

Definition at line 640 of file posixStorage.py.

640 def readParquetStorage(butlerLocation):
641  """Read a catalog from a Parquet file specified by ButlerLocation.
642 
643  The object returned by this is expected to be a subtype
644  of `ParquetTable`, which is a thin wrapper to `pyarrow.ParquetFile`
645  that allows for lazy loading of the data.
646 
647  Parameters
648  ----------
649  butlerLocation : ButlerLocation
650  The location for the object(s) to be read.
651 
652  Returns
653  -------
654  A list of objects as described by the butler location. One item for
655  each location in butlerLocation.getLocations()
656  """
657  results = []
658  additionalData = butlerLocation.getAdditionalData()
659 
660  for locationString in butlerLocation.getLocations():
661  locStringWithRoot = os.path.join(butlerLocation.getStorage().root, locationString)
662  logLoc = LogicalLocation(locStringWithRoot, additionalData)
663  if not os.path.exists(logLoc.locString()):
664  raise RuntimeError("No such parquet file: " + logLoc.locString())
665 
666  pythonType = butlerLocation.getPythonType()
667  if pythonType is not None:
668  if isinstance(pythonType, str):
669  pythonType = doImport(pythonType)
670 
671  filename = logLoc.locString()
672 
673  # pythonType will be ParquetTable (or perhaps MultilevelParquetTable)
674  # filename should be the first kwarg, but being explicit here.
675  results.append(pythonType(filename=filename))
676 
677  return results
678 
679 
def readParquetStorage(butlerLocation)

◆ readPickleStorage()

def lsst.daf.persistence.posixStorage.readPickleStorage (   butlerLocation)
Read an object from a pickle file specified by ButlerLocation.

Parameters
----------
butlerLocation : ButlerLocation
    The location for the object(s) to be read.

Returns
-------
A list of objects as described by the butler location. One item for
each location in butlerLocation.getLocations()

Definition at line 717 of file posixStorage.py.

717 def readPickleStorage(butlerLocation):
718  """Read an object from a pickle file specified by ButlerLocation.
719 
720  Parameters
721  ----------
722  butlerLocation : ButlerLocation
723  The location for the object(s) to be read.
724 
725  Returns
726  -------
727  A list of objects as described by the butler location. One item for
728  each location in butlerLocation.getLocations()
729  """
730  # Create a list of Storages for the item.
731  results = []
732  additionalData = butlerLocation.getAdditionalData()
733  for locationString in butlerLocation.getLocations():
734  locStringWithRoot = os.path.join(butlerLocation.getStorage().root, locationString)
735  logLoc = LogicalLocation(locStringWithRoot, additionalData)
736  if not os.path.exists(logLoc.locString()):
737  raise RuntimeError("No such pickle file: " + logLoc.locString())
738  with open(logLoc.locString(), "rb") as infile:
739  # py3: We have to specify encoding since some files were written
740  # by python2, and 'latin1' manages that conversion safely. See:
741  # http://stackoverflow.com/questions/28218466/unpickling-a-python-2-object-with-python-3/28218598#28218598
742  if sys.version_info.major >= 3:
743  finalItem = pickle.load(infile, encoding="latin1")
744  else:
745  finalItem = pickle.load(infile)
746  results.append(finalItem)
747  return results
748 
749 
def readPickleStorage(butlerLocation)

◆ readYamlStorage()

def lsst.daf.persistence.posixStorage.readYamlStorage (   butlerLocation)
Read an object from a YAML file specified by a butlerLocation.

Parameters
----------
butlerLocation : ButlerLocation
    The location for the object(s) to be read.

Returns
-------
A list of objects as described by the butler location. One item for
each location in butlerLocation.getLocations()

Definition at line 868 of file posixStorage.py.

868 def readYamlStorage(butlerLocation):
869  """Read an object from a YAML file specified by a butlerLocation.
870 
871  Parameters
872  ----------
873  butlerLocation : ButlerLocation
874  The location for the object(s) to be read.
875 
876  Returns
877  -------
878  A list of objects as described by the butler location. One item for
879  each location in butlerLocation.getLocations()
880  """
881  results = []
882  for locationString in butlerLocation.getLocations():
883  logLoc = LogicalLocation(butlerLocation.getStorage().locationWithRoot(locationString),
884  butlerLocation.getAdditionalData())
885  if not os.path.exists(logLoc.locString()):
886  raise RuntimeError("No such YAML file: " + logLoc.locString())
887  # Butler Gen2 repository configurations are handled specially
888  if butlerLocation.pythonType == 'lsst.daf.persistence.RepositoryCfg':
889  finalItem = Policy(filePath=logLoc.locString())
890  else:
891  try:
892  # PyYAML >=5.1 prefers a different loader
893  loader = yaml.UnsafeLoader
894  except AttributeError:
895  loader = yaml.Loader
896  with open(logLoc.locString(), "rb") as infile:
897  finalItem = yaml.load(infile, Loader=loader)
898  results.append(finalItem)
899  return results
900 
901 
902 PosixStorage.registerFormatters("FitsStorage", readFitsStorage, writeFitsStorage)
903 PosixStorage.registerFormatters("ParquetStorage", readParquetStorage, writeParquetStorage)
904 PosixStorage.registerFormatters("ConfigStorage", readConfigStorage, writeConfigStorage)
905 PosixStorage.registerFormatters("PickleStorage", readPickleStorage, writePickleStorage)
906 PosixStorage.registerFormatters("FitsCatalogStorage", readFitsCatalogStorage, writeFitsCatalogStorage)
907 PosixStorage.registerFormatters("MatplotlibStorage", readMatplotlibStorage, writeMatplotlibStorage)
908 PosixStorage.registerFormatters("YamlStorage", readYamlStorage, writeYamlStorage)
909 
910 Storage.registerStorageClass(scheme='', cls=PosixStorage)
911 Storage.registerStorageClass(scheme='file', cls=PosixStorage)
def readYamlStorage(butlerLocation)

◆ writeConfigStorage()

def lsst.daf.persistence.posixStorage.writeConfigStorage (   butlerLocation,
  obj 
)
Writes an lsst.pex.config.Config  object to a location specified by
ButlerLocation.

Parameters
----------
butlerLocation : ButlerLocation
    The location for the object to be written.
obj : object instance
    The object to be written.

Definition at line 540 of file posixStorage.py.

540 def writeConfigStorage(butlerLocation, obj):
541  """Writes an lsst.pex.config.Config object to a location specified by
542  ButlerLocation.
543 
544  Parameters
545  ----------
546  butlerLocation : ButlerLocation
547  The location for the object to be written.
548  obj : object instance
549  The object to be written.
550  """
551  filename = os.path.join(butlerLocation.getStorage().root, butlerLocation.getLocations()[0])
552  with SafeFilename(filename) as locationString:
553  logLoc = LogicalLocation(locationString, butlerLocation.getAdditionalData())
554  obj.save(logLoc.locString())
555 
556 
def writeConfigStorage(butlerLocation, obj)

◆ writeFitsCatalogStorage()

def lsst.daf.persistence.posixStorage.writeFitsCatalogStorage (   butlerLocation,
  obj 
)
Writes a catalog to a FITS table specified by ButlerLocation.

Parameters
----------
butlerLocation : ButlerLocation
    The location for the object to be written.
obj : object instance
    The object to be written.

Definition at line 802 of file posixStorage.py.

802 def writeFitsCatalogStorage(butlerLocation, obj):
803  """Writes a catalog to a FITS table specified by ButlerLocation.
804 
805  Parameters
806  ----------
807  butlerLocation : ButlerLocation
808  The location for the object to be written.
809  obj : object instance
810  The object to be written.
811  """
812  additionalData = butlerLocation.getAdditionalData()
813  locations = butlerLocation.getLocations()
814  with SafeFilename(os.path.join(butlerLocation.getStorage().root, locations[0])) as locationString:
815  logLoc = LogicalLocation(locationString, additionalData)
816  if additionalData.exists("flags"):
817  kwds = dict(flags=additionalData.getInt("flags"))
818  else:
819  kwds = {}
820  obj.writeFits(logLoc.locString(), **kwds)
821 
822 
def writeFitsCatalogStorage(butlerLocation, obj)

◆ writeFitsStorage()

def lsst.daf.persistence.posixStorage.writeFitsStorage (   butlerLocation,
  obj 
)
Writes an object to a FITS file specified by ButlerLocation.

The object is written using method
``writeFitsWithOptions(path, options)``, if it exists, else
``writeFits(path)``. The ``options`` argument is the data returned by
``butlerLocation.getAdditionalData()``.

Parameters
----------
butlerLocation : ButlerLocation
    The location for the object to be written.
obj : object instance
    The object to be written.

Definition at line 614 of file posixStorage.py.

614 def writeFitsStorage(butlerLocation, obj):
615  """Writes an object to a FITS file specified by ButlerLocation.
616 
617  The object is written using method
618  ``writeFitsWithOptions(path, options)``, if it exists, else
619  ``writeFits(path)``. The ``options`` argument is the data returned by
620  ``butlerLocation.getAdditionalData()``.
621 
622  Parameters
623  ----------
624  butlerLocation : ButlerLocation
625  The location for the object to be written.
626  obj : object instance
627  The object to be written.
628  """
629  supportsOptions = hasattr(obj, "writeFitsWithOptions")
630  additionalData = butlerLocation.getAdditionalData()
631  locations = butlerLocation.getLocations()
632  with SafeFilename(os.path.join(butlerLocation.getStorage().root, locations[0])) as locationString:
633  logLoc = LogicalLocation(locationString, additionalData)
634  if supportsOptions:
635  obj.writeFitsWithOptions(logLoc.locString(), options=additionalData)
636  else:
637  obj.writeFits(logLoc.locString())
638 
639 
def writeFitsStorage(butlerLocation, obj)

◆ writeMatplotlibStorage()

def lsst.daf.persistence.posixStorage.writeMatplotlibStorage (   butlerLocation,
  obj 
)
Writes a matplotlib.figure.Figure to a location, using the template's
filename suffix to infer the file format.

Parameters
----------
butlerLocation : ButlerLocation
    The location for the object to be written.
obj : matplotlib.figure.Figure
    The object to be written.

Definition at line 839 of file posixStorage.py.

839 def writeMatplotlibStorage(butlerLocation, obj):
840  """Writes a matplotlib.figure.Figure to a location, using the template's
841  filename suffix to infer the file format.
842 
843  Parameters
844  ----------
845  butlerLocation : ButlerLocation
846  The location for the object to be written.
847  obj : matplotlib.figure.Figure
848  The object to be written.
849  """
850  additionalData = butlerLocation.getAdditionalData()
851  locations = butlerLocation.getLocations()
852  with SafeFilename(os.path.join(butlerLocation.getStorage().root, locations[0])) as locationString:
853  logLoc = LogicalLocation(locationString, additionalData)
854  # SafeFilename appends a random suffix, which corrupts the extension
855  # matplotlib uses to guess the file format.
856  # Instead, we extract the extension from the original location
857  # and pass that as the format directly.
858  _, ext = os.path.splitext(locations[0])
859  if ext:
860  ext = ext[1:] # strip off leading '.'
861  else:
862  # If there is no extension, we let matplotlib fall back to its
863  # default.
864  ext = None
865  obj.savefig(logLoc.locString(), format=ext)
866 
867 
def writeMatplotlibStorage(butlerLocation, obj)

◆ writeParquetStorage()

def lsst.daf.persistence.posixStorage.writeParquetStorage (   butlerLocation,
  obj 
)
Writes pandas dataframe to parquet file.

Parameters
----------
butlerLocation : ButlerLocation
    The location for the object(s) to be read.
obj : `lsst.qa.explorer.parquetTable.ParquetTable`
    Wrapped DataFrame to write.

Definition at line 680 of file posixStorage.py.

680 def writeParquetStorage(butlerLocation, obj):
681  """Writes pandas dataframe to parquet file.
682 
683  Parameters
684  ----------
685  butlerLocation : ButlerLocation
686  The location for the object(s) to be read.
687  obj : `lsst.qa.explorer.parquetTable.ParquetTable`
688  Wrapped DataFrame to write.
689 
690  """
691  additionalData = butlerLocation.getAdditionalData()
692  locations = butlerLocation.getLocations()
693  with SafeFilename(os.path.join(butlerLocation.getStorage().root, locations[0])) as locationString:
694  logLoc = LogicalLocation(locationString, additionalData)
695  filename = logLoc.locString()
696  obj.write(filename)
697 
698 
def writeParquetStorage(butlerLocation, obj)

◆ writePickleStorage()

def lsst.daf.persistence.posixStorage.writePickleStorage (   butlerLocation,
  obj 
)
Writes an object to a pickle file specified by ButlerLocation.

Parameters
----------
butlerLocation : ButlerLocation
    The location for the object to be written.
obj : object instance
    The object to be written.

Definition at line 750 of file posixStorage.py.

750 def writePickleStorage(butlerLocation, obj):
751  """Writes an object to a pickle file specified by ButlerLocation.
752 
753  Parameters
754  ----------
755  butlerLocation : ButlerLocation
756  The location for the object to be written.
757  obj : object instance
758  The object to be written.
759  """
760  additionalData = butlerLocation.getAdditionalData()
761  locations = butlerLocation.getLocations()
762  with SafeFilename(os.path.join(butlerLocation.getStorage().root, locations[0])) as locationString:
763  logLoc = LogicalLocation(locationString, additionalData)
764  with open(logLoc.locString(), "wb") as outfile:
765  pickle.dump(obj, outfile, pickle.HIGHEST_PROTOCOL)
766 
767 
def writePickleStorage(butlerLocation, obj)

◆ writeYamlStorage()

def lsst.daf.persistence.posixStorage.writeYamlStorage (   butlerLocation,
  obj 
)
Writes an object to a YAML file specified by ButlerLocation.

Parameters
----------
butlerLocation : ButlerLocation
    The location for the object to be written.
obj : object instance
    The object to be written.

Definition at line 699 of file posixStorage.py.

699 def writeYamlStorage(butlerLocation, obj):
700  """Writes an object to a YAML file specified by ButlerLocation.
701 
702  Parameters
703  ----------
704  butlerLocation : ButlerLocation
705  The location for the object to be written.
706  obj : object instance
707  The object to be written.
708  """
709  additionalData = butlerLocation.getAdditionalData()
710  locations = butlerLocation.getLocations()
711  with SafeFilename(os.path.join(butlerLocation.getStorage().root, locations[0])) as locationString:
712  logLoc = LogicalLocation(locationString, additionalData)
713  with open(logLoc.locString(), "w") as outfile:
714  yaml.dump(obj, outfile)
715 
716 
def writeYamlStorage(butlerLocation, obj)