Download the pre-compiled hdf5 formatet relative spectral response functions
    from the internet
979439
def download_rsr(**kwargs):
    

    #
    import tarfile
    import requests
    TQDM_LOADED = True
    try:
        from tqdm import tqdm
    except ImportError:
        TQDM_LOADED = False

    dest_dir = kwargs.get('dest_dir', LOCAL_RSR_DIR)
    dry_run = kwargs.get('dry_run', False)

    LOG.info("Download RSR files and store in directory %s", dest_dir)

    filename = os.path.join(dest_dir, "pyspectral_rsr_data.tgz")
    LOG.debug("Get data. URL: %s", HTTP_PYSPECTRAL_RSR)
    LOG.debug("Destination = %s", dest_dir)
    if dry_run:
        return

    response = requests.get(HTTP_PYSPECTRAL_RSR)
    if TQDM_LOADED:
        with open(filename, "wb") as handle:
            for data in tqdm(response.iter_content()):
                handle.write(data)
    else:
        with open(filename, "wb") as handle:
            for data in response.iter_content():
                handle.write(data)

    tar = tarfile.open(filename)
    tar.extractall(dest_dir)
    tar.close()
    os.remove(filename)
54259
def dict_to_hdf5(dict_like, h5group):
    
    # Write attributes
    for key, value in dict_like.items():
        if value is not None:
            h5group.attrs[str(key)] = value
522662
def hdf5_col(self, chain=-1):
        
        return self.db._tables[chain].colinstances[self.name]
179920
def read_spectral_data(hdu):
    
    ebins = read_energy_bounds(hdu)
    fluxes = np.ndarray((len(ebins)))
    try:
        fluxes[0:-1] = hdu.data.field("E_MIN_FL")
        fluxes[-1] = hdu.data.field("E_MAX_FL")[-1]
        npreds = hdu.data.field("NPRED")
    except:
        fluxes = np.ones((len(ebins)))
        npreds = np.ones((len(ebins)))
    return ebins, fluxes, npreds
228344
def hdrval(cls):
        

        hdrmap = {'Itn': 'Iter', 'X r': 'PrimalRsdl', 'X s': 'DualRsdl',
                  u('X ρ'): 'Rho', 'D cnstr': 'Cnstr', 'D dlt': 'DeltaD',
                  u('D η'): 'Eta'}
        return hdrmap
1316939
def dict_to_hdf5(dic, endpoint):
    
    filename = gen_filename(endpoint)
    with h5py.File(filename, 'w') as handler:
        walk_dict_to_hdf5(dic, handler)
    print('dumped to', filename)
868647
def hdf5_read_in(self):
        

        with h5py.File(self.WORKING_DIRECTORY + '/' + self.file_name) as f:

            # read in
            data = f['data']

            # find number of distinct x and y points.
            num_x_pts = len(np.unique(data[self.x_column_label][:]))
            num_y_pts = len(np.unique(data[self.y_column_label][:]))

            # create 2D arrays of x,y,z
            self.xvals = np.reshape(data[self.x_column_label][:], (num_y_pts, num_x_pts))
            self.yvals = np.reshape(data[self.y_column_label][:], (num_y_pts, num_x_pts))
            self.zvals = np.reshape(data[self.z_column_label][:], (num_y_pts, num_x_pts))
        return
1242544
def create_from_dict(cls, src_dict, roi_skydir=None, rescale=False):
        
        src_dict = copy.deepcopy(src_dict)
        src_dict.setdefault('SpatialModel', 'PointSource')
        src_dict.setdefault('Spectrum_Filename', None)
        src_dict.setdefault('SpectrumType', 'PowerLaw')
        src_dict['SpatialType'] = get_spatial_type(src_dict['SpatialModel'])

        spectrum_type = src_dict['SpectrumType']
        spatial_type = src_dict['SpatialType']

        spectral_pars = src_dict.pop('spectral_pars', {})
        spatial_pars = src_dict.pop('spatial_pars', {})

        if not spectral_pars:
            spectral_pars = extract_pars_from_dict(spectrum_type, src_dict)
            norm_par_name = get_function_norm_par_name(spectrum_type)
            if norm_par_name is not None:
                spectral_pars[norm_par_name].setdefault('free', True)

        if not spatial_pars:
            spatial_pars = extract_pars_from_dict(spatial_type, src_dict)
            for k in ['RA', 'DEC', 'Prefactor']:
                if k in spatial_pars:
                    del spatial_pars[k]

        spectral_pars = create_pars_from_dict(spectrum_type, spectral_pars,
                                              rescale)
        spatial_pars = create_pars_from_dict(spatial_type, spatial_pars,
                                             False)

        if 'file' in src_dict:
            src_dict['Spectrum_Filename'] = src_dict.pop('file')

        if spectrum_type == 'DMFitFunction' and src_dict['Spectrum_Filename'] is None:
            src_dict['Spectrum_Filename'] = os.path.join('$FERMIPY_DATA_DIR',
                                                         'gammamc_dif.dat')

        src_dict['spectral_pars'] = cast_pars_dict(spectral_pars)
        src_dict['spatial_pars'] = cast_pars_dict(spatial_pars)

        if 'name' in src_dict:
            name = src_dict['name']
            src_dict['Source_Name'] = src_dict.pop('name')
        elif 'Source_Name' in src_dict:
            name = src_dict['Source_Name']
        else:
            raise Exception('Source name undefined.')

        skydir = wcs_utils.get_target_skydir(src_dict, roi_skydir)

        src_dict['RAJ2000'] = skydir.ra.deg
        src_dict['DEJ2000'] = skydir.dec.deg

        radec = np.array([skydir.ra.deg, skydir.dec.deg])

        return cls(name, src_dict, radec=radec)
940631
def to_hdf(self,path,key,mode='a'):
        
        pd.DataFrame(self.serialize()).to_hdf(path,key,mode=mode,format='table',complib='zlib',complevel=9)
        f = h5py.File(path,'r+')
        f[key].attrs["microns_per_pixel"] = float(self.microns_per_pixel) if self.microns_per_pixel is not None else np.nan
        f.close()
645641
def _load(self, scale=1.0):
        

        LOG.debug("File: %s", str(self.requested_band_filename))

        data = np.genfromtxt(self.requested_band_filename,
                             unpack=True,
                             names=['wavelength',
                                    'wavenumber',
                                    'response'],
                             skip_header=2)

        wvl = data['wavelength'] * scale
        resp = data['response']

        self.rsr = {'wavelength': wvl, 'response': resp}
803198
def funcNrlTcMotPred(idxPrc,
                     varPixX,
                     varPixY,
                     NrlMdlChunk,
                     varNumTP,
                     aryBoxCar,  # aryCond
                     path,
                     varNumNrlMdls,
                     varNumMtDrctn,
                     varPar,
                     queOut):
    

#    # if hd5 method is used: open file for reading
#    filename = 'aryBoxCar' + str(idxPrc) + '.hdf5'
#    hdf5_path = os.path.join(path, filename)
#    fileH = tables.openFile(hdf5_path, mode='r')

    # Output array with pRF model time courses at all modelled standard
    # deviations for current pixel position:
    aryOut = np.empty((len(NrlMdlChunk), varNumTP, varNumMtDrctn),
                      dtype='float32')

    # Prepare status indicator if this is the first of the parallel processes:
    if idxPrc == 1:

        # We create a status indicator for the time consuming pRF model finding
        # algorithm. Number of steps of the status indicator:
        varStsStpSze = 20

        # Number of pRF models to fit:
        varNumLoops = varNumNrlMdls/varPar

        # Vector with pRF values at which to give status feedback:
        vecStatus = np.linspace(0,
                                varNumLoops,
                                num=(varStsStpSze+1),
                                endpoint=True)
        vecStatus = np.ceil(vecStatus)
        vecStatus = vecStatus.astype(int)

        # Vector with corresponding percentage values at which to give status
        # feedback:
        vecStatusPrc = np.linspace(0,
                                   100,
                                   num=(varStsStpSze+1),
                                   endpoint=True)
        vecStatusPrc = np.ceil(vecStatusPrc)
        vecStatusPrc = vecStatusPrc.astype(int)

        # Counter for status indicator:
        varCntSts01 = 0
        varCntSts02 = 0

    # Loop through all Gauss parameters that are in this chunk
    for idx, NrlMdlTrpl in enumerate(NrlMdlChunk):

        # Status indicator (only used in the first of the parallel
        # processes):
        if idxPrc == 1:

            # Status indicator:
            if varCntSts02 == vecStatus[varCntSts01]:

                # Prepare status message:
                strStsMsg = ('---------Progress: ' +
                             str(vecStatusPrc[varCntSts01]) +
                             ' % --- ' +
                             str(vecStatus[varCntSts01]) +
                             ' loops out of ' +
                             str(varNumLoops))

                print(strStsMsg)

                # Only increment counter if the last value has not been
                # reached yet:
                if varCntSts01 < varStsStpSze:
                    varCntSts01 = varCntSts01 + int(1)

        # x pos of Gauss model: NrlMdlTrpl[0]
        # y pos of Gauss model: NrlMdlTrpl[1]
        # std of Gauss model: NrlMdlTrpl[2]
        # index of tng crv model: NrlMdlTrpl[3]
        varTmpX = int(np.around(NrlMdlTrpl[0], 0))
        varTmpY = int(np.around(NrlMdlTrpl[1], 0))

        # Create pRF model (2D):
        aryGauss = funcGauss2D(varPixX,
                               varPixY,
                               varTmpX,
                               varTmpY,
                               NrlMdlTrpl[2])

        # Multiply pixel-wise box car model with Gaussian pRF models:
        aryNrlTcTmp = np.multiply(aryBoxCar, aryGauss[:, :, None, None])

        # Calculate sum across x- and y-dimensions - the 'area under the
        # Gaussian surface'. This is essentially an unscaled version of the
        # neural time course model (i.e. not yet scaled for the size of
        # the pRF).
        aryNrlTcTmp = np.sum(aryNrlTcTmp, axis=(0, 1))

        # Normalise the nrl time course model to the size of the pRF. This
        # gives us the ratio of 'activation' of the pRF at each time point,
        # or, in other words, the neural time course model.
        aryNrlTcTmp = np.divide(aryNrlTcTmp,
                                np.sum(aryGauss, axis=(0, 1)))

        # Put model time courses into the function's output array:
        aryOut[idx, :, :] = aryNrlTcTmp

        # Status indicator (only used in the first of the parallel
        # processes):
        if idxPrc == 1:
            # Increment status indicator counter:
            varCntSts02 = varCntSts02 + 1

    # Output list:
    lstOut = [idxPrc,
              aryOut,
              ]

    queOut.put(lstOut)
55407
def hdrmap(xmethod, dmethod, opt):
    

    hdr = {'Itn': 'Iter', 'Fnc': 'ObjFun', 'DFid': 'DFid',
           u('ℓ1'): 'RegL1', 'Cnstr': 'Cnstr'}
    if xmethod == 'admm':
        hdr.update({'r_X': 'XPrRsdl', 's_X': 'XDlRsdl', u('ρ_X'): 'XRho'})
    else:
        if opt['CBPDN', 'BackTrack', 'Enabled']:
            hdr.update({'F_X': 'X_F_Btrack', 'Q_X': 'X_Q_Btrack',
                        'It_X': 'X_ItBt', 'L_X': 'X_L'})
        else:
            hdr.update({'L_X': 'X_L'})
    if dmethod != 'fista':
        hdr.update({'r_D': 'DPrRsdl', 's_D': 'DDlRsdl', u('ρ_D'): 'DRho'})
    else:
        if opt['CCMOD', 'BackTrack', 'Enabled']:
            hdr.update({'F_D': 'D_F_Btrack', 'Q_D': 'D_Q_Btrack',
                        'It_D': 'D_ItBt', 'L_D': 'D_L'})
        else:
            hdr.update({'L_D': 'D_L'})
    return hdr
1165764
def get_hstwcs(filename,hdulist,extnum):
    
    hdrwcs = wcsutil.HSTWCS(hdulist,ext=extnum)
    hdrwcs.filename = filename
    hdrwcs.expname = hdulist[extnum].header['expname']
    hdrwcs.extver = hdulist[extnum].header['extver']

    return hdrwcs
429986
def spectral_pars_from_catalog(cat):
    """"""

    spectrum_type = cat['SpectrumType']
    pars = get_function_defaults(cat['SpectrumType'])
    par_idxs = {k: i for i, k in
                enumerate(get_function_par_names(cat['SpectrumType']))}

    for k in pars:
        pars[k]['value'] = cat['param_values'][par_idxs[k]]

    if spectrum_type == 'PowerLaw':

        pars['Index']['value'] *= -1.0
        pars['Index']['scale'] = -1.0
        pars['Scale']['scale'] = 1.0
        pars['Index']['max'] = max(5.0, pars['Index']['value'] + 1.0)
        pars['Index']['min'] = min(0.0, pars['Index']['value'] - 1.0)
        pars['Prefactor'] = make_parameter_dict(pars['Prefactor'])
        pars['Scale'] = make_parameter_dict(pars['Scale'], True, False)
        pars['Index'] = make_parameter_dict(pars['Index'], False, False)

    elif spectrum_type == 'LogParabola':

        pars['norm'] = make_parameter_dict(pars['norm'], False, True)
        pars['Eb'] = make_parameter_dict(pars['Eb'], True, False)
        pars['alpha'] = make_parameter_dict(pars['alpha'], False, False)
        pars['beta'] = make_parameter_dict(pars['beta'], False, False)

    elif spectrum_type == 'PLSuperExpCutoff':

        pars['Index1']['value'] *= -1.0
        pars['Index1']['scale'] = -1.0
        pars['Index2']['scale'] = 1.0
        pars['Prefactor'] = make_parameter_dict(pars['Prefactor'])
        pars['Scale'] = make_parameter_dict(pars['Scale'], True, False)
        pars['Index1'] = make_parameter_dict(pars['Index1'], False, False)
        pars['Index2'] = make_parameter_dict(pars['Index2'], False, False)
        pars['Cutoff'] = make_parameter_dict(pars['Cutoff'], False, True)

    elif spectrum_type == 'PLSuperExpCutoff2':

        pars['Index1']['value'] *= -1.0
        pars['Index1']['scale'] = -1.0
        pars['Index2']['scale'] = 1.0
        pars['Prefactor'] = make_parameter_dict(pars['Prefactor'])
        pars['Scale'] = make_parameter_dict(pars['Scale'], True, False)
        pars['Index1'] = make_parameter_dict(pars['Index1'], False, False)
        pars['Index2'] = make_parameter_dict(pars['Index2'], False, False)
        pars['Expfactor'] = make_parameter_dict(pars['Expfactor'], False, True)
    else:
        raise Exception('Unsupported spectral type:' + spectrum_type)

    return pars
23481
def read_abinit_hdr(self):
        
        d = {}
        for hvar in _HDR_VARIABLES.values():
            ncname = hvar.etsf_name if hvar.etsf_name is not None else hvar.name
            if ncname in self.rootgrp.variables:
                d[hvar.name] = self.read_value(ncname)
            elif ncname in self.rootgrp.dimensions:
                d[hvar.name] = self.read_dimvalue(ncname)
            else:
                raise ValueError("Cannot find `%s` in `%s`" % (ncname, self.path))
            # Convert scalars to (well) scalars.
            if hasattr(d[hvar.name], "shape") and not d[hvar.name].shape:
                d[hvar.name] = np.asscalar(d[hvar.name])
            if hvar.name in ("title", "md5_pseudos", "codvsn"):
                # Convert array of numpy bytes to list of strings
                if hvar.name == "codvsn":
                    d[hvar.name] = "".join(bs.decode("utf-8").strip() for bs in d[hvar.name])
                else:
                    d[hvar.name] = ["".join(bs.decode("utf-8") for bs in astr).strip()
                            for astr in d[hvar.name]]

        return AbinitHeader(d)
834379
def read_hdf(filename: PathLike, key: str) -> AnnData:
    
    with h5py.File(filename, 'r') as f:
        # the following is necessary in Python 3, because only
        # a view and not a list is returned
        keys = [k for k in f.keys()]
        if key == '':
            raise ValueError((
                'The file {} stores the following sheets:\n{}\n'
                'Call read/read_hdf5 with one of them.'
            ).format(filename, keys))
        # read array
        X = f[key][()]
        # try to find row and column names
        rows_cols = [{}, {}]
        for iname, name in enumerate(['row_names', 'col_names']):
            if name in keys:
                rows_cols[iname][name] = f[name][()]
    adata = AnnData(X, rows_cols[0], rows_cols[1], dtype=X.dtype.name)
    return adata
955679
def hdfoutput(outname, frames, dozip=False):
    ''''''
    with h5.File(outname,'a') as f:
        for frame in frames:
            group=str(frame['step']);
            h5w(f, frame, group=group,
                compression='lzf' if dozip else None);