예제 #1
0
 def load_meta(self, group, reformat=True):
     """ Load the meta data as a Table
     Parameters
     ----------
     group : str
     """
     import json
     self.meta = spdbu.hdf_decode(self.hdf[group + '/meta'].value,
                                  itype='Table')
     # Attributes
     self.meta_attr = {}
     for key in self.hdf[group + '/meta'].attrs.keys():
         if 'SSA' in key:
             self.meta_attr[key] = json.loads(
                 spdbu.hdf_decode(self.hdf[group + '/meta'].attrs[key]))
         else:
             self.meta_attr[key] = spdbu.hdf_decode(
                 self.hdf[group + '/meta'].attrs[key])
     # Reformat
     if reformat:
         try:
             self.meta['RA_GROUP'].format = '8.4f'
         except KeyError:  # Backwards compatible, will deprecate
             self.meta['RA'].format = '8.4f'
             self.meta['DEC'].format = '8.4f'
             self.meta['zem'].format = '6.3f'
         else:
             self.meta['DEC_GROUP'].format = '8.4f'
             self.meta['zem_GROUP'].format = '6.3f'
         self.meta['WV_MIN'].format = '6.1f'
         self.meta['WV_MAX'].format = '6.1f'
     # Add group
     self.meta.meta['group'] = group
예제 #2
0
 def __init__(self, skip_test=True, db_file=None, verbose=False, **kwargs):
     """
     """
     if db_file is None:
         try:
             db_file = self.grab_dbfile(**kwargs)
         except:
             raise IOError(
                 "DB not found. Please either check the corresponding environmental "
                 "variable or directly provide the db_file")
     # Init
     self.verbose = verbose
     self.open_db(db_file)
     # Catalog
     self.qcat = QueryCatalog(self.hdf, verbose=self.verbose, **kwargs)
     self.cat = self.qcat.cat  # For convenience
     self.qcat.verbose = verbose
     self.groups = self.qcat.groups
     self.group_dict = self.qcat.group_dict
     self.idkey = self.qcat.idkey
     # Groups
     self._gdict = {}
     # Name, Creation date
     self.name = spdbu.hdf_decode(self.qcat.cat_attr['NAME'])
     print("Database is {:s}".format(self.name))
     print("Created on {:s}".format(
         spdbu.hdf_decode(self.qcat.cat_attr['CREATION_DATE'])))
     if self.qcat.version is not None:
         print("Version: {:s}".format(self.qcat.version))
     # Return
     return
예제 #3
0
    def load_cat(self, hdf, idkey=None):
        """ Open the DB catalog file
        Parameters
        ----------
        db_file : str
          Name of DB file
        idkey : str, optional
          Key for ID indices

        Returns
        -------

        """
        import json
        # Catalog and attributes
        self.cat = Table(hdf['catalog'].value)
        self.cat_attr = {}
        for key in hdf['catalog'].attrs.keys():
            self.cat_attr[key] = spdbu.hdf_decode(hdf['catalog'].attrs[key])
        # Set ID key
        self.idkey = idkey
        if idkey is None:
            for key in self.cat.keys():
                if 'ID' in key:
                    if self.idkey is not None:
                        raise ValueError("Two keys with ID in them.  You must specify idkey directly.")
                    self.idkey = key
        # Group dict
        self.group_dict = json.loads(spdbu.hdf_decode(hdf['catalog'].attrs['GROUP_DICT']))

        self.groups = list(self.group_dict.keys())
        try:
            self.version = self.cat_attr['VERSION']
        except KeyError:
            self.version = None

        if self.verbose:
            print("Available groups: {}".format(self.groups))
예제 #4
0
def main(pargs):
    """ Run
    Parameters
    ----------
    pargs

    Returns
    -------

    """
    import h5py
    import warnings
    from specdb import defs
    from specdb import utils as sdbu
    dbinfo = defs.dbase_info()

    # Open file
    hdf = h5py.File(pargs.db_file, 'r')

    # Deal with Python 3
    catalog = {}
    for key in hdf['catalog'].attrs:
        catalog[key] = hdf['catalog'].attrs[key]
    catalog = sdbu.hdf_decode(catalog)

    # Name
    try:
        dbname = catalog['NAME']
    except:
        warnings.warn('DB file has no name.  Must be really old.')
        dbname = None
    else:
        print("specdb DB file is from the {:s} database".format(str(dbname)))

    # Check Creation Date
    try:
        cdate = catalog['CREATION_DATE']
    except:
        warnings.warn('DB file has no Creation Date.  Must be really old..')
        return
    else:
        version = catalog['VERSION']
        print("specdb DB file version={} was created on {}".format(str(version),str(cdate)))
        if dbname is not None:
            try:
                print("Latest version for specdb DB type={} is version={}".format(
                        dbname, str(dbinfo[dbname]['latest_version'])))
            except KeyError:
                print("No version version history for {:s}".format(str(dbname)))
            else:
                # Check
                print("Latest creation date for this DB version was {}".format(
                    dbinfo[dbname][version]['newest_date']))
                print("Oldest valid DB file for this DB version was {}".format(
                        dbinfo[dbname][version]['oldest_ok_date']))
                # Compare?

    # Sources and spectra
    print("-------------------------------------------------")
    print("There are {:d} unique sources in the source catalog".format(len(hdf['catalog'].value)))

    # List datasets
    nspec = 0
    for key in hdf.keys():
        if key == 'catalog':
            continue
        print("Dataset: {:s}".format(key))
        # Spectra
        try:
            nspec += hdf[key]['spec'].size
        except:
            pass

    print("There are a total of {:d} spectra in the database".format(nspec))