Read data from a FITS file into a MOC.

    Any additional keyword arguments are passed to the
    astropy.io.fits.open method.
190243
def read_moc_fits(moc, filename, include_meta=False, **kwargs):
    

    hl = fits.open(filename, mode='readonly', **kwargs)

    read_moc_fits_hdu(moc, hl[1], include_meta)
1193044
def read_projection_from_fits(fitsfile, extname=None):
    
    f = fits.open(fitsfile)
    nhdu = len(f)
    # Try and get the energy bounds
    try:
        ebins = find_and_read_ebins(f)
    except:
        ebins = None

    if extname is None:
        # If there is an image in the Primary HDU we can return a WCS-based
        # projection
        if f[0].header['NAXIS'] != 0:
            proj = WCS(f[0].header)
            return proj, f, f[0]
    else:
        if f[extname].header['XTENSION'] == 'IMAGE':
            proj = WCS(f[extname].header)
            return proj, f, f[extname]
        elif extname in ['SKYMAP', 'SKYMAP2']:
            proj = HPX.create_from_hdu(f[extname], ebins)
            return proj, f, f[extname]
        elif f[extname].header['XTENSION'] == 'BINTABLE':
            try:
                if f[extname].header['PIXTYPE'] == 'HEALPIX':
                    proj = HPX.create_from_hdu(f[extname], ebins)
                    return proj, f, f[extname]
            except:
                pass
        return None, f, None

    # Loop on HDU and look for either an image or a table with HEALPix data
    for i in range(1, nhdu):
        # if there is an image we can return a WCS-based projection
        if f[i].header['XTENSION'] == 'IMAGE':
            proj = WCS(f[i].header)
            return proj, f, f[i]
        elif f[i].header['XTENSION'] == 'BINTABLE':
            if f[i].name in ['SKYMAP', 'SKYMAP2']:
                proj = HPX.create_from_hdu(f[i], ebins)
                return proj, f, f[i]
            try:
                if f[i].header['PIXTYPE'] == 'HEALPIX':
                    proj = HPX.create_from_hdu(f[i], ebins)
                    return proj, f, f[i]
            except:
                pass

    return None, f, None
837791
def load_fits(self, filepath):
        
        image = AstroImage.AstroImage(logger=self.logger)
        image.load_file(filepath)

        self.set_image(image)
794258
def read_umi_tools(filename: PathLike, dtype: str='float32') -> AnnData:
    
    # import pandas for conversion of a dict of dicts into a matrix
    # import gzip to read a gzipped file :-)
    import gzip
    from pandas import DataFrame

    dod = {}  # this will contain basically everything
    fh = gzip.open(fspath(filename))
    header = fh.readline()  # read the first line

    for line in fh:
        t = line.decode('ascii').split('\t')  # gzip read bytes, hence the decoding
        try:
            dod[t[1]].update({t[0]:int(t[2])})
        except KeyError:
            dod[t[1]] = {t[0]:int(t[2])}

    df = DataFrame.from_dict(dod, orient='index')  # build the matrix
    df.fillna(value=0., inplace=True)  # many NaN, replace with zeros
    return AnnData(np.array(df), {'obs_names': df.index}, {'var_names': df.columns}, dtype=dtype)
967761
def write_moc_fits_hdu(moc):
    

    # Ensure data are normalized.
    moc.normalize()

    # Determine whether a 32 or 64 bit column is required.
    if moc.order < 14:
        moc_type = np.int32
        col_type = 'J'
    else:
        moc_type = np.int64
        col_type = 'K'

    # Convert to the NUNIQ value which guarantees that one of the
    # top two bits is set so that the order of the value can be
    # determined.
    nuniq = []
    for (order, cells) in moc:
        uniq_prefix = 4 * (4 ** order)
        for npix in cells:
            nuniq.append(npix + uniq_prefix)

    # Prepare the data, and sort into numerical order.
    nuniq = np.array(nuniq, dtype=moc_type)
    nuniq.sort()

    # Create the FITS file.
    col = fits.Column(name='UNIQ', format=col_type, array=nuniq)

    cols = fits.ColDefs([col])
    rec = fits.FITS_rec.from_columns(cols)
    tbhdu = fits.BinTableHDU(rec)

    # Mandatory Keywords.
    tbhdu.header['PIXTYPE'] = 'HEALPIX'
    tbhdu.header['ORDERING'] = 'NUNIQ'
    tbhdu.header['COORDSYS'] = 'C'
    tbhdu.header['MOCORDER'] = moc.order
    tbhdu.header.comments['PIXTYPE'] = 'HEALPix magic code'
    tbhdu.header.comments['ORDERING'] = 'NUNIQ coding method'
    tbhdu.header.comments['COORDSYS'] = 'ICRS reference frame'
    tbhdu.header.comments['MOCORDER'] = 'MOC resolution (best order)'

    # Optional Keywords.
    tbhdu.header['MOCTOOL'] = 'PyMOC ' + version
    tbhdu.header.comments['MOCTOOL'] = 'Name of MOC generator'
    if moc.type is not None:
        tbhdu.header['MOCTYPE'] = moc.type
        tbhdu.header.comments['MOCTYPE'] = 'Source type (IMAGE or CATALOG)'
    if moc.id is not None:
        tbhdu.header['MOCID'] = moc.id
        tbhdu.header.comments['MOCID'] = 'Identifier of the collection'
    if moc.origin is not None:
        tbhdu.header['ORIGIN'] = moc.origin
        tbhdu.header.comments['ORIGIN'] = 'MOC origin'
    tbhdu.header['DATE'] = datetime.utcnow().replace(
        microsecond=0).isoformat()
    tbhdu.header.comments['DATE'] = 'MOC creation date'
    if moc.name is not None:
        tbhdu.header['EXTNAME'] = moc.name
        tbhdu.header.comments['EXTNAME'] = 'MOC name'

    return tbhdu
51070
def _open_fix(file):
    
    import pyfits, re, string
    temp = pyfits.HDUList()
    hdu = pyfits.PrimaryHDU()

    hdu._file=open(file,'rb')

    _number_RE = re.compile(
                r'(?P<sign>[+-])?0*(?P<digt>(\.\d+|\d+(\.\d*)?)([deDE][+-]?\d+)?)')

    ### here's the real difference between pyFits and cfh12kFits.
    ### I'm more flexible on the format of the header file so that allows me
    ### read more files.
    card_RE=re.compile(r"""
    (?P<KEY>[-A-Z0-9_a-za ]{8})   ### keyword is the first 8 bytes... i'll allow small letters
    (
     (
      (?P<VALUE>=\s)             ### =\s indicats a value coming.
      (\s*
       (   
        (?P<STRING>\'[^\']*[\'/])   ### a string 
        |
        (?P<FLOAT>([+-]?(\.\d+|\d+\.\d*)([dDEe][+-]?\d+)?))  ### a floating point number
        |
        (?P<INT>[+-]?\d+)      ### an integer
        |
        (?P<BOOL>[TFtf])       ### perhaps value is boolian
        )
       \s*
       (( / )?(?P<COMMENT>.*))?    ### value related comment.
      )
     )
     |
     (?P<C2>.*)     ### strickly a comment field
    )
    """,re.VERBOSE)

    
    done=0
    while ( not done):

        ### read a line of 80 characters up to a new line from the file.
        block=hdu._file.readline(80)

        string_end=79
        if len(block)== 0:
            done=1
            continue
        if block[-1]=='\n':
            string_end=len(block)-2

        line = re.match(r'[ -~]{0,'+str(string_end)+'}',block)

        line = string.ljust(line.group(0),80)[0:79]

        if line[0:8] == 'END     ':
            done=1
            break

        card=card_RE.match(line)
        if not card or not card.group('KEY'):
            print card.groups()
            raise SyntaxError("Failed to get keyword from FITS Card %s" % line)
            
        key=card.group('KEY')
        value=None
        if card.group('INT'):
            try:
                value=int(card.group('INT'))
            except:
                value=card.group('INT')
        elif card.group('FLOAT'):
            try:
                value=float(card.group('FLOAT'))
            except:
                value=float(card.group('FLOAT'))
        elif card.group('BOOL'):
            value=pyfits.Boolean(card.group('BOOL'))
        elif card.group('STRING'):
            value=card.group('STRING')[1:-1]
            
        if card.group('COMMENT'):
            _comment=card.group('COMMENT')
        elif card.group('C2'):
            _comment=card.group('C2')
        else:
            _comment=None

        try:
            if key =='COMMENT ':
                hdu.header.add_comment(_comment)
            elif key =='HISTORY ':
                hdu.header.add_history(_comment)
            elif key =='        ':
                hdu.header.add_blank(_comment)
            elif key:
                if key =='DATE-OBS' and value:
                    value=string.replace(value,'/','-')
                hdu.header.update(key,value,comment=_comment)
        except:
            raise SyntaxError("Failed to convert line to FITS Card %s" % line)

    ### set some internal variables to decided on data flow.
    hdu._bzero=hdu.header.get('BZERO',0)
    hdu._bscale=hdu.header.get('BSCALE',1)
    hdu._bitpix=hdu.header.get('BITPIX',-16)

    if hdu.header.get('NAXIS',0)>0:
        naxis1=hdu.header.get('NAXIS1',1)
        naxis2=hdu.header.get('NAXIS2',1)
    ### now read the data... this is a HACK from pyfits.py
        import numarray as num
    
        code = pyfits._ImageBaseHDU.NumCode[hdu._bitpix]
        dims = tuple([naxis2,naxis1])
        raw_data = num.fromfile(hdu._file,type=code,shape=dims)
        raw_data._byteorder='big'

        if ( hdu._bzero != 0
             or hdu._bscale!=1 ):
            if  hdu._bitpix > 0 :
                hdu.data=num.array(raw_data,type=num.Float32)
            else:
                hdu.data=raw_data
            if hdu._bscale != 1:
                num.multiply(hdu.data,hdu._bscale,hdu.data)
            if hdu._bzero!=0:
                hdu.data=hdu.data + hdu._bzero

            del hdu.header['BSCALE']
            del hdu.header['BZERO']
            hdu.header['BITPIX']=pyfits._ImageBaseHDU.ImgCode[hdu.data.type()]
            
    temp.append(hdu)
    return temp
809742
def _read_from_file(self, filename):
        """"""
        try:
            with open(filename, 'rb') as f:
                return self._loads(f.read())
        except (IOError, OSError):
            logger.warning('Error opening file: {}'.format(filename))
            return None
1209607
def read_lines_from_file(cls_name, filename):
  """"""
  with tf.io.gfile.GFile(filename, "rb") as f:
    lines = [tf.compat.as_text(line)[:-1] for line in f]
  header_line = "%s%s" % (_HEADER_PREFIX, cls_name)
  if lines[0] != header_line:
    raise ValueError("File {fname} does not seem to have been created from "
                     "{name}.save_to_file.".format(
                         fname=filename, name=cls_name))
  metadata_dict = json.loads(lines[1][len(_METADATA_PREFIX):])
  return lines[2:], metadata_dict
958524
def read(self, fp):
        ""
        base_size = struct.unpack(str("=I"), fp.read(4))[0]
        self._units.fromfile(fp, base_size)
257951
def read_bytes(fd, byte_order, dtype, count):
    """"""
    return numpy.fromfile(fd, byte_order+dtype[-1], count).tostring()
1271801
def _from_io(self, source: IO):
        
        read = source.read

        if unpack('>I', source.read(4))[0] != ClassFile.MAGIC:
            raise ValueError('invalid magic number')

        # The version is swapped on disk to (minor, major), so swap it back.
        self.version = unpack('>HH', source.read(4))[::-1]

        self._constants.unpack(source)

        # ClassFile access_flags, see section #4.1 of the JVM specs.
        self.access_flags.unpack(read(2))

        # The CONSTANT_Class indexes for "this" class and its superclass.
        # Interfaces are a simple list of CONSTANT_Class indexes.
        self._this, self._super, interfaces_count = unpack('>HHH', read(6))
        self._interfaces = unpack(
            f'>{interfaces_count}H',
            read(2 * interfaces_count)
        )

        self.fields.unpack(source)
        self.methods.unpack(source)
        self.attributes.unpack(source)
1213377
def read_csv(csv_file, options, ensemble_list=None):
    
    name, ext = os.path.splitext(csv_file)
    try:
        if ext == '.gz':
            f = gzip.open(csv_file, 'rb')
        else:
            f = open(csv_file, 'rU')
    except IOError:
        print(" \n '{f}' could not be opened\n".format(f=os.path.basename(csv_file)))
        sys.exit(1)


    csv_reader = csv.reader(f)
    molList = []
    line_number = 1

    for line in csv_reader:

        if line_number == 1:
            if ensemble_list:
                prop_indices = read_header(line, options, ensemble_list)
            else:
                prop_indices = read_header(line, options)
        else:
            mol = Molecule()
            if ensemble_list:
                mol = read_line(line, options, prop_indices, mol, ensemble_list)
            else:
                mol = read_line(line, options, prop_indices, mol)
            if mol == 1:
                print(" skipping molecule {m}\n".format(m=(line_number - 1)))
            else:
                molList.append(mol)
        line_number += 1

    return molList
408436
def _fromfile(self):
        
        fd = self.parent._fd
        byte_order = self.parent.byte_order
        offset_size = self.parent.offset_size

        fmt = {4: 'I', 8: 'Q'}[offset_size]
        offset = struct.unpack(byte_order + fmt, fd.read(offset_size))[0]
        if not offset:
            raise StopIteration()

        # read standard tags
        tags = self.tags
        fd.seek(offset)
        fmt, size = {4: ('H', 2), 8: ('Q', 8)}[offset_size]
        try:
            numtags = struct.unpack(byte_order + fmt, fd.read(size))[0]
        except Exception:
            warnings.warn("corrupted page list")
            raise StopIteration()

        for _ in range(numtags):
            tag = TIFFtag(self.parent)
            tags[tag.name] = tag

        # read LSM info subrecords
        if self.is_lsm:
            pos = fd.tell()
            for name, reader in CZ_LSM_INFO_READERS.items():
                try:
                    offset = self.cz_lsm_info["offset_"+name]
                except KeyError:
                    continue
                if not offset:
                    continue
                fd.seek(offset)
                try:
                    setattr(self, "cz_lsm_"+name, reader(fd, byte_order))
                except ValueError:
                    pass
            fd.seek(pos)
1181820
def read(self, **keys):
        
        if not self.has_data():
            return None

        dtype, shape = self._get_dtype_and_shape()
        array = numpy.zeros(shape, dtype=dtype)
        self._FITS.read_image(self._ext+1, array)
        return array
1180414
def write_moc_fits(moc, filename, **kwargs):
    

    tbhdu = write_moc_fits_hdu(moc)
    prihdr = fits.Header()
    prihdu = fits.PrimaryHDU(header=prihdr)
    hdulist = fits.HDUList([prihdu, tbhdu])
    hdulist.writeto(filename, **kwargs)
1041297
def _openResources(self):
        
        try:
            rate, data = scipy.io.wavfile.read(self._fileName, mmap=True)
        except Exception as ex:
            logger.warning(ex)
            logger.warning("Unable to read wav with memmory mapping. Trying without now.")
            rate, data = scipy.io.wavfile.read(self._fileName, mmap=False)

        self._array = data
        self.attributes['rate'] = rate
1079488
def write_fits(self, data, outfile, extname="SKYMAP", clobber=True):
        
        hdu_prim = fits.PrimaryHDU()
        hdu_hpx = self.make_hdu(data, extname=extname)
        hl = [hdu_prim, hdu_hpx]
        if self.conv.energy_hdu == 'EBOUNDS':
            hdu_energy = self.make_energy_bounds_hdu()
        elif self.conv.energy_hdu == 'ENERGIES':
            hdu_energy = self.make_energies_hdu()
        if hdu_energy is not None:
            hl.append(hdu_energy)
        hdulist = fits.HDUList(hl)
        hdulist.writeto(outfile, overwrite=clobber)