Convenience function: Detect file extension and read via Atomistica or ASE.
    If reading a NetCDF files, frame numbers can be appended via '@'.
    e.g., a = read('traj.nc@5')
768671
def read(fn, **kwargs):
    
    ext = fn[fn.rfind('.'):].split('@')
    if len(ext) == 1:
        if ext[0] == '.out' or ext[0] == '.dat':
            dirname = os.path.dirname(fn)
            if len(dirname) == 0:
                dirname = '.'
            cycfn = dirname+'/cyc.dat'
            if os.path.exists(cycfn):
                return read_atoms(fn, cycfn=cycfn)
            return read_atoms(fn)
        elif ext[0] == '.nc':
            traj = NetCDFTrajectory(fn, **kwargs)
            return traj[-1]
        else:
            return ase.io.read(fn, **kwargs)
    elif len(ext) == 2:
        if ext[0] == '.nc':
            frame = int(ext[1])
            fn = fn[:fn.rfind('@')]
            traj = NetCDFTrajectory(fn)
            return traj[frame]
        else:
            return ase.io.read(fn, **kwargs)
    else:
        return ase.io.read(fn, **kwargs)
78869
def is_netcdf(url):
    '''
    
    '''
    # Try an obvious exclusion of remote resources
    if url.startswith('http'):
        return False

    # If it's a known extension, give it a shot
    if url.endswith('nc'):
        return True

    # Brute force
    with open(url, 'rb') as f:
        magic_number = f.read(4)
        if len(magic_number) < 4:
            return False
        if is_classic_netcdf(magic_number):
            return True
        elif is_hdf5(magic_number):
            return True

        return False
27988
def read(fname):
    
    fpath = os.path.join(os.path.dirname(__file__), fname)
    with codecs.open(fpath, 'r', 'utf8') as fhandle:
        return fhandle.read().strip()
779437
def detect(self, fstring, fname=None):
        """"""
        if fname is not None and '.' in fname:
            extension = fname.rsplit('.', 1)[1]
            if extension in {'pdf', 'html', 'xml'}:
                return False
        return True
993906
def _cdata_io(self, action, data, ctype, frames):
        """"""
        assert ctype in _ffi_types.values()
        self._check_if_closed()
        if self.seekable():
            curr = self.tell()
        func = getattr(_snd, 'sf_' + action + 'f_' + ctype)
        frames = func(self._file, data, frames)
        _error_check(self._errorcode)
        if self.seekable():
            self.seek(curr + frames, SEEK_SET)  # Update read & write position
        return frames
1054457
def readabt(filename, dirs='.'):
    
    # resolve filename
    filename = misc.findfileindirs(filename, dirs)
    f = open(filename, 'rt')
    abt = {'offsetcorrected': False, 'params': {}, 'columns': [], 'data': [], 'title': '<no_title>',
           'offsets': {}, 'filename': filename}
    readingmode = ''
    for l in f:
        l = l.strip()
        if l.startswith('!') or len(l) == 0:
            continue
        elif l.startswith('%c'):
            readingmode = 'comments'
        elif l.startswith('%p'):
            readingmode = 'params'
        elif l.startswith('%d'):
            readingmode = 'data'
        elif readingmode == 'comments':
            m = re.match(
                r'(?P<scantype>\w+)-Scan started at (?P<startdate>\d+-\w+-\d+) (?P<starttime>\d+:\d+:\d+), ended (?P<endtime>\d+:\d+:\d+)', l)
            if m:
                abt.update(m.groupdict())
                continue
            else:
                m = re.match(r'Name: (?P<name>\w+)', l)
                if m:
                    abt.update(m.groupdict())
                    m1 = re.search(r'from (?P<from>\d+(?:.\d+)?)', l)
                    if m1:
                        abt.update(m1.groupdict())
                    m1 = re.search(r'to (?P<to>\d+(?:.\d+)?)', l)
                    if m1:
                        abt.update(m1.groupdict())
                    m1 = re.search(r'by (?P<by>\d+(?:.\d+)?)', l)
                    if m1:
                        abt.update(m1.groupdict())
                    m1 = re.search(r'sampling (?P<sampling>\d+(?:.\d+)?)', l)
                    if m1:
                        abt.update(m1.groupdict())
                    continue
            if l.find('Counter readings are offset corrected') >= 0:
                abt['offsetcorrected'] = True
                readingmode = 'offsets'
                continue
            # if we reach here in 'comments' mode, this is the title line
            abt['title'] = l
            continue
        elif readingmode == 'offsets':
            m = re.findall(r'(\w+)\s(\d+(?:.\d+)?)', l)
            if m:
                abt['offsets'].update(dict(m))
                for k in abt['offsets']:
                    abt['offsets'][k] = float(abt['offsets'][k])
        elif readingmode == 'params':
            abt['params'][l.split('=')[0].strip()] = float(
                l.split('=')[1].strip())
        elif readingmode == 'data':
            if l.startswith('Col'):
                abt['columns'].append(l.split()[2])
            else:
                abt['data'].append([float(x) for x in l.split()])
    f.close()
    # some post-processing
    # remove common prefix from column names
    maxcolnamelen = max(len(c) for c in abt['columns'])
    l = 1
    for l in range(1, maxcolnamelen):
        if len(set([c[:l] for c in abt['columns']])) > 1:
            break
    abt['columns'] = [c[l - 1:] for c in abt['columns']]
    # represent data as a structured array
    dt = np.dtype(list(zip(abt['columns'], itertools.repeat(np.double))))
    abt['data'] = np.array(abt['data'], dtype=np.double).view(dt)
    # dates and times in datetime formats
    monthnames = ['Jan', 'Feb', 'Mar', 'Apr', 'May',
                  'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    for m, i in zip(monthnames, itertools.count(1)):
        abt['startdate'] = abt['startdate'].replace(m, str(i))
    abt['startdate'] = datetime.date(
        *reversed([int(x) for x in abt['startdate'].split('-')]))
    abt['starttime'] = datetime.time(
        *[int(x) for x in abt['starttime'].split(':')])
    abt['endtime'] = datetime.time(
        *[int(x) for x in abt['endtime'].split(':')])
    abt['start'] = datetime.datetime.combine(
        abt['startdate'], abt['starttime'])
    if abt['endtime'] <= abt['starttime']:
        abt['end'] = datetime.datetime.combine(
            abt['startdate'] + datetime.timedelta(1), abt['endtime'])
    else:
        abt['end'] = datetime.datetime.combine(
            abt['startdate'], abt['endtime'])
    del abt['starttime']
    del abt['startdate']
    del abt['endtime']
    # convert some fields to float
    for k in ['from', 'to', 'by', 'sampling']:
        if k in abt:
            abt[k] = float(abt[k])
    # change space and dash in title to underscore
    abt['title'] = abt['title'].replace('-', '_').replace(' ', '_')
    return abt
564026
public void readAckFrame() throws IOException {
        byte[] buffer = new byte[6];
        int bytesReceived = 0;

        while ((bytesReceived = bytesReceived + in.read(buffer, bytesReceived, buffer.length - bytesReceived)) != -1) {
            if (TraceComponent.isAnyTracingEnabled() && tc.isEventEnabled())
                Tr.event(tc, "Ack - total bytes received = " + bytesReceived);

            if (bytesReceived == 6) {
                if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
                    Tr.debug(tc, "Ack - buffer = ", buffer);
                }

                String frameType = new String(Arrays.copyOfRange(buffer, 1, 2), UTF_8);
                if (frameType.equals("A")) {
                    int sequenceNumber = ByteBuffer.wrap(Arrays.copyOfRange(buffer, 2, 6)).getInt();

                    if (TraceComponent.isAnyTracingEnabled() && tc.isEventEnabled())
                        Tr.event(tc, "Ack - received sequence number = " + sequenceNumber + " expecting sequence number = " + (seqNumber - 1));

                    if (sequenceNumber >= (seqNumber - 1)) {
                        break;
                    }
                } else {
                    if (TraceComponent.isAnyTracingEnabled() && tc.isEventEnabled()) {
                        Tr.event(tc, "Ack - unknown frame type " + frameType);
                    }
                    //Unknown frame type
                }
                bytesReceived = 0;
                buffer = new byte[6];
            }
        }
    }
452754
def _read(self, directory, filename, session, path, name, extension, spatial, spatialReferenceID, replaceParamFile):
        
        # Persist name and extension of file
        self.name = name
        self.fileExtension = extension

        # Open file and parse into a data structure
        with open(path, 'r') as f:
            self.text = f.read()
765380
function(file, cb) {
                if (!file) return cb();

                readExtension(file, function(err, ext) {
                    // ignore errors from extension check
                    //if (err) console.error(err);

                    ext = ext || extname;

                    switch (ext.toLowerCase()) {
                    // Unzip and symlink to directory.
                    case '.zip':
                        if (nosymlink) {
                            unzip(file, function(err, file) {
                                if (err) return cb(err);
                                l.Datasource.file = file;
                                return cb();
                            });
                        } else {
                            unzip(file, function(err, file_found) {
                                if (err) return cb(err);
                                l.Datasource.file = path.join(base,
                                                      'layers',
                                                      name,
                                                      path.basename(file_found));
                                return utils.processFiles(file_found,
                                                        l.Datasource.file,
                                                        file_linking_method,
                                                        {cache:cache}, cb);
                            });
                        }
                        break;
                    case '.shp':
                        if (nosymlink) {
                            l.Datasource.file = file;
                            return cb();
                        } else {
                            l.Datasource.file =
                                path.join(base, 'layers', name, path.basename(file));
                            return utils.processFiles(file, l.Datasource.file, file_linking_method, {cache:cache}, cb);
                        }
                        break;
                    default:
                        if (nosymlink) {
                            l.Datasource.file = file;
                            return cb();
                        } else {
                            l.Datasource.file =
                                path.join(base, 'layers', name + ext);
                            return file_linking_method(file, l.Datasource.file, {cache:cache}, cb);
                        }
                        break;
                    }
                });
            }
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
1116966
def _read_var_data(self):
        

        var_bundles = OrderedDict()
        var_attrs = OrderedDict()

        n_vars = 0

        while self.fp.tell() < self.fsize:

            var_attr = OrderedDict()

            # read first and second header lines
            line = self.fp.readline('20sffii')
            modelname, res0, res1, halfpolar, center180 = line

            line = self.fp.readline('40si40sdd40s7i')
            category_name, number, unit, tau0, tau1, reserved = line[:6]
            dim0, dim1, dim2, dim3, dim4, dim5, skip = line[6:]
            var_attr['number'] = number

            # Decode byte-strings to utf-8
            category_name = str(category_name, 'utf-8')
            var_attr['category'] = category_name.strip()
            unit = str(unit, 'utf-8')

            # get additional metadata from tracerinfo / diaginfo
            try:
                cat_df = self.diaginfo_df[
                    self.diaginfo_df.name == category_name.strip()
                ]
                # TODO: Safer logic for handling case where more than one
                #       tracer metadata match was made
                # if len(cat_df > 1):
                #     raise ValueError(
                #         "More than one category matching {} found in "
                #         "diaginfo.dat".format(
                #             category_name.strip()
                #         )
                #     )
                # Safe now to select the only row in the DataFrame
                cat = cat_df.T.squeeze()

                tracer_num = int(cat.offset) + int(number)
                diag_df = self.tracerinfo_df[
                    self.tracerinfo_df.tracer == tracer_num
                ]
                # TODO: Safer logic for handling case where more than one
                #       tracer metadata match was made
                # if len(diag_df > 1):
                #     raise ValueError(
                #         "More than one tracer matching {:d} found in "
                #         "tracerinfo.dat".format(tracer_num)
                #     )
                # Safe now to select only row in the DataFrame
                diag = diag_df.T.squeeze()
                diag_attr = diag.to_dict()

                if not unit.strip():  # unit may be empty in bpch
                    unit = diag_attr['unit']  # but not in tracerinfo
                var_attr.update(diag_attr)
            except:
                diag = {'name': '', 'scale': 1}
                var_attr.update(diag)
            var_attr['unit'] = unit

            vname = diag['name']
            fullname = category_name.strip() + "_" + vname

            # parse metadata, get data or set a data proxy
            if dim2 == 1:
                data_shape = (dim0, dim1)         # 2D field
            else:
                data_shape = (dim0, dim1, dim2)
            var_attr['original_shape'] = data_shape

            # Add proxy time dimension to shape
            data_shape = tuple([1, ] + list(data_shape))
            origin = (dim3, dim4, dim5)
            var_attr['origin'] = origin

            timelo, timehi = cf.tau2time(tau0), cf.tau2time(tau1)

            pos = self.fp.tell()
            # Note that we don't pass a dtype, and assume everything is
            # single-fp floats with the correct endian, as hard-coded
            var_bundle = BPCHDataBundle(
                data_shape, self.endian, self.filename, pos, [timelo, timehi],
                metadata=var_attr,
                use_mmap=self.use_mmap, dask_delayed=self.dask_delayed
            )
            self.fp.skipline()

            # Save the data as a "bundle" for concatenating in the final step
            if fullname in var_bundles:
                var_bundles[fullname].append(var_bundle)
            else:
                var_bundles[fullname] = [var_bundle, ]
                var_attrs[fullname] = var_attr
                n_vars += 1

        self.var_data = var_bundles
        self.var_attrs = var_attrs
223137
def _readuntil(f, end=_TYPE_END):
	""""""
	buf = bytearray()
	byte = f.read(1)
	while byte != end:
		if byte == b'':
			raise ValueError('File ended unexpectedly. Expected end byte {}.'.format(end))
		buf += byte
		byte = f.read(1)
	return buf
182341
def MakeExpandedTrace(frame_records):
  """"""
  dump = []
  for (frame_obj, filename, line_num, fun_name, context_lines,
       context_index) in frame_records:
    dump.append('File "%s", line %d, in %s\n' % (filename, line_num,
                                                 fun_name))
    if context_lines:
      for (i, line) in enumerate(context_lines):
        if i == context_index:
          dump.append(' --> %s' % line)
        else:
          dump.append('     %s' % line)
    for local_name, local_val in frame_obj.f_locals.items():
      try:
        local_type_name = type(local_val).__name__
      except Exception as e:
        local_type_name = '    Exception in type({}).__name__: {}'.format(local_name, e)
      try:
        truncated_val = repr(local_val)[0:500]
      except Exception as e:
        dump.append('    Exception in repr({}): {}\n'.format(local_name, e))
      else:
        if len(truncated_val) >= 500:
          truncated_val = '%s...' % truncated_val[0:499]
        dump.append('    {} = {} ({})\n'.format(local_name, truncated_val, local_type_name))
    dump.append('\n')
  return dump
1123350
def _read(self, directory, filename, session, path, name, extension, spatial, spatialReferenceID, maskMap):
        
        # Assign file extension attribute to file object
        self.fileExtension = extension

        if isinstance(maskMap, RasterMapFile) and maskMap.fileExtension == 'msk':
            # Vars from mask map
            columns = maskMap.columns
            rows = maskMap.rows
            upperLeftX = maskMap.west
            upperLeftY = maskMap.north

            # Derive the cell size (GSSHA cells are square, so it is the same in both directions)
            cellSizeX = int(abs(maskMap.west - maskMap.east) / columns)
            cellSizeY = -1 * cellSizeX

            # Dictionary of keywords/cards and parse function names
            KEYWORDS = {'DATASET': wdc.datasetHeaderChunk,
                        'TS': wdc.datasetScalarTimeStepChunk}

            # Open file and read plain text into text field
            with open(path, 'r') as f:
                chunks = pt.chunk(KEYWORDS, f)

            # Parse header chunk first
            header = wdc.datasetHeaderChunk('DATASET', chunks['DATASET'][0])

            # Parse each time step chunk and aggregate
            timeStepRasters = []

            for chunk in chunks['TS']:
                timeStepRasters.append(wdc.datasetScalarTimeStepChunk(chunk, columns, header['numberCells']))

            # Set WMS dataset file properties
            self.name = header['name']
            self.numberCells = header['numberCells']
            self.numberData = header['numberData']
            self.objectID = header['objectID']

            if header['type'] == 'BEGSCL':
                self.objectType = header['objectType']
                self.type = self.SCALAR_TYPE

            elif header['type'] == 'BEGVEC':
                self.vectorType = header['objectType']
                self.type = self.VECTOR_TYPE

            # Create WMS raster dataset files for each raster
            for timeStep, timeStepRaster in enumerate(timeStepRasters):
                # Create new WMS raster dataset file object
                wmsRasterDatasetFile = WMSDatasetRaster()

                # Set the wms dataset for this WMS raster dataset file
                wmsRasterDatasetFile.wmsDataset = self

                # Set the time step and timestamp and other properties
                wmsRasterDatasetFile.iStatus = timeStepRaster['iStatus']
                wmsRasterDatasetFile.timestamp = timeStepRaster['timestamp']
                wmsRasterDatasetFile.timeStep = timeStep + 1

                # If spatial is enabled create PostGIS rasters
                if spatial:
                    # Process the values/cell array
                    wmsRasterDatasetFile.raster = RasterLoader.makeSingleBandWKBRaster(session,
                                                                                       columns, rows,
                                                                                       upperLeftX, upperLeftY,
                                                                                       cellSizeX, cellSizeY,
                                                                                       0, 0,
                                                                                       spatialReferenceID,
                                                                                       timeStepRaster['cellArray'])

                # Otherwise, set the raster text properties
                else:
                    wmsRasterDatasetFile.rasterText = timeStepRaster['rasterText']

            # Add current file object to the session
            session.add(self)

        else:
            log.warning("Could not read {0}. Mask Map must be supplied "
                     "to read WMS Datasets.".format(filename))
475098
def read_file(filename, binary_mode=False):
  
  mode = 'rb' if binary_mode else 'r'
  with open(filename, mode) as f:
    return f.read()
551873
def read(fname):
    
    path = os.path.join(SCRIPTDIR, fname)
    if PY3:
        f = open(path, 'r', encoding='utf8')
    else:
        f = open(path, 'r')
    content = f.read()
    f.close()
    return content
31697
def read_meta(self, f):
        
        if not isinstance(f, BacktrackableFile):
            f = BacktrackableFile(f)

        try:
            (name, value) = self.read_meta_line(f)
            while name:
                name = (name == 'nominal_offset' and 'timestamp_rounding' or
                        name)
                name = (name == 'actual_offset' and 'timestamp_offset' or name)
                method_name = 'get_{}'.format(name)
                method = getattr(self, method_name, None)
                if method:
                    method(name, value)
                name, value = self.read_meta_line(f)
                if not name and not value:
                    break
        except ParsingError as e:
            e.args = e.args + (f.line_number,)
            raise