nodes: 0, module; 1, function_definition; 2, function_name:open; 3, parameters; 4, block; 5, identifier:filename; 6, default_parameter; 7, default_parameter; 8, default_parameter; 9, default_parameter; 10, default_parameter; 11, default_parameter; 12, expression_statement; 13, if_statement; 14, expression_statement; 15, if_statement; 16, import_from_statement; 17, expression_statement; 18, expression_statement; 19, expression_statement; 20, expression_statement; 21, try_statement; 22, if_statement; 23, return_statement; 24, identifier:mode; 25, string:"r"; 26, identifier:iline; 27, integer:189; 28, identifier:xline; 29, integer:193; 30, identifier:strict; 31, True; 32, identifier:ignore_geometry; 33, False; 34, identifier:endian; 35, string; 36, comment:"""Open a segy file.
  
      Opens a segy file and tries to figure out its sorting, inline numbers,
      crossline numbers, and offsets, and enables reading and writing to this
      file in a simple manner.
  
      For reading, the access mode `r` is preferred. All write operations will
      raise an exception. For writing, the mode `r+` is preferred (as `rw` would
      truncate the file). Any mode with `w` will raise an error. The modes used
      are standard C file modes; please refer to that documentation for a
      complete reference.
  
      Open should be used together with python's ``with`` statement. Please refer
      to the examples. When the ``with`` statement is used the file will
      automatically be closed when the routine completes or an exception is
      raised.
  
      By default, segyio tries to open in ``strict`` mode. This means the file will
      be assumed to represent a geometry with consistent inline, crosslines and
      offsets. If strict is False, segyio will still try to establish a geometry,
      but it won't abort if it fails. When in non-strict mode is opened,
      geometry-dependent modes such as iline will raise an error.
  
      If ``ignore_geometry=True``, segyio will *not* try to build iline/xline or
      other geometry related structures, which leads to faster opens. This is
      essentially the same as using ``strict=False`` on a file that has no
      geometry.
  
      Parameters
      ----------
  
      filename : str
          Path to file to open
  
      mode : {'r', 'r+'}
          File access mode, read-only ('r', default) or read-write ('r+')
  
      iline : int or segyio.TraceField
          Inline number field in the trace headers. Defaults to 189 as per the
          SEG-Y rev1 specification
  
      xline : int or segyio.TraceField
          Crossline number field in the trace headers. Defaults to 193 as per the
          SEG-Y rev1 specification
  
      strict : bool, optional
          Abort if a geometry cannot be inferred. Defaults to True.
  
      ignore_geometry : bool, optional
          Opt out on building geometry information, useful for e.g. shot
          organised files. Defaults to False.
  
      endian : {'big', 'msb', 'little', 'lsb'}
          File endianness, big/msb (default) or little/lsb
  
      Returns
      -------
  
      file : segyio.SegyFile
          An open segyio file handle
  
      Raises
      ------
  
      ValueError
          If the mode string contains 'w', as it would truncate the file
  
      Notes
      -----
  
      .. versionadded:: 1.1
  
      .. versionchanged:: 1.8
          endian argument
  
      When a file is opened non-strict, only raw traces access is allowed, and
      using modes such as ``iline`` raise an error.
  
  
      Examples
      --------
  
      Open a file in read-only mode:
  
      >>> with segyio.open(path, "r") as f:
      ...     print(f.ilines)
      ...
      [1, 2, 3, 4, 5]
  
      Open a file in read-write mode:
  
      >>> with segyio.open(path, "r+") as f:
      ...     f.trace = np.arange(100)
  
      Open two files at once:
  
      >>> with segyio.open(path) as src, segyio.open(path, "r+") as dst:
      ...     dst.trace = src.trace # copy all traces from src to dst
  
      Open a file little-endian file:
  
      >>> with segyio.open(path, endian = 'little') as f:
      ...     f.trace[0]
  
      """; 37, comparison_operator:'w' in mode; 38, block; 39, assignment; 40, comparison_operator:endian not in endians; 41, block; 42, relative_import; 43, dotted_name; 44, assignment; 45, call; 46, assignment; 47, assignment; 48, block; 49, except_clause; 50, identifier:ignore_geometry; 51, block; 52, call; 53, string_content:big; 54, string; 55, identifier:mode; 56, expression_statement; 57, expression_statement; 58, raise_statement; 59, identifier:endians; 60, dictionary; 61, identifier:endian; 62, identifier:endians; 63, expression_statement; 64, expression_statement; 65, raise_statement; 66, import_prefix; 67, identifier:_segyio; 68, identifier:fd; 69, call; 70, attribute; 71, argument_list; 72, identifier:metrics; 73, call; 74, identifier:f; 75, call; 76, expression_statement; 77, expression_statement; 78, expression_statement; 79, expression_statement; 80, block; 81, return_statement; 82, identifier:infer_geometry; 83, argument_list; 84, string_content:w; 85, assignment; 86, assignment; 87, call; 88, pair; 89, comment:# (1 << 8); 90, pair; 91, pair; 92, pair; 93, assignment; 94, assignment; 95, call; 96, attribute; 97, argument_list; 98, identifier:fd; 99, identifier:segyopen; 100, attribute; 101, argument_list; 102, attribute; 103, argument_list; 104, assignment; 105, assignment; 106, assignment; 107, assignment; 108, expression_statement; 109, raise_statement; 110, identifier:f; 111, identifier:f; 112, identifier:metrics; 113, identifier:iline; 114, identifier:xline; 115, identifier:strict; 116, identifier:problem; 117, string; 118, identifier:solution; 119, string; 120, identifier:ValueError; 121, argument_list; 122, string; 123, integer:256; 124, string; 125, integer:256; 126, string; 127, integer:0; 128, string; 129, integer:0; 130, identifier:problem; 131, string; 132, identifier:opts; 133, call; 134, identifier:ValueError; 135, argument_list; 136, identifier:_segyio; 137, identifier:segyiofd; 138, call; 139, identifier:mode; 140, subscript; 141, identifier:fd; 142, identifier:metrics; 143, identifier:segyio; 144, identifier:SegyFile; 145, identifier:fd; 146, keyword_argument; 147, keyword_argument; 148, keyword_argument; 149, keyword_argument; 150, keyword_argument; 151, identifier:dt; 152, binary_operator:segyio.tools.dt(f, fallback_dt = 4000.0) / 1000.0; 153, identifier:t0; 154, subscript; 155, identifier:samples; 156, subscript; 157, attribute; 158, binary_operator:(numpy.arange(samples) * dt) + t0; 159, call; 160, string_content:w in mode would truncate the file; 161, string_content:use r+ to open in read-write; 162, call; 163, string_content:little; 164, string_content:lsb; 165, string_content:big; 166, string_content:msb; 167, string_content:unknown endianness {}, expected one of:; 168, attribute; 169, argument_list; 170, binary_operator:problem.format(endian) + opts; 171, identifier:str; 172, argument_list; 173, identifier:endians; 174, identifier:endian; 175, identifier:filename; 176, call; 177, identifier:mode; 178, identifier:mode; 179, identifier:iline; 180, identifier:iline; 181, identifier:xline; 182, identifier:xline; 183, identifier:endian; 184, identifier:endian; 185, call; 186, float:1000.0; 187, subscript; 188, attribute; 189, identifier:metrics; 190, string; 191, identifier:f; 192, identifier:_samples; 193, parenthesized_expression; 194, identifier:t0; 195, attribute; 196, argument_list; 197, attribute; 198, argument_list; 199, string; 200, identifier:join; 201, call; 202, call; 203, identifier:opts; 204, identifier:filename; 205, identifier:str; 206, argument_list; 207, attribute; 208, argument_list; 209, attribute; 210, integer:0; 211, attribute; 212, identifier:DelayRecordingTime; 213, string_content:samplecount; 214, binary_operator:numpy.arange(samples) * dt; 215, identifier:f; 216, identifier:close; 217, string; 218, identifier:join; 219, tuple; 220, string_content:; 221, attribute; 222, argument_list; 223, attribute; 224, argument_list; 225, identifier:filename; 226, attribute; 227, identifier:dt; 228, identifier:f; 229, keyword_argument; 230, identifier:f; 231, identifier:header; 232, identifier:segyio; 233, identifier:TraceField; 234, call; 235, identifier:dt; 236, string_content:,; 237, identifier:problem; 238, identifier:solution; 239, identifier:endians; 240, identifier:keys; 241, identifier:problem; 242, identifier:format; 243, identifier:endian; 244, identifier:segyio; 245, identifier:tools; 246, identifier:fallback_dt; 247, float:4000.0; 248, attribute; 249, argument_list; 250, identifier:numpy; 251, identifier:arange; 252, identifier:samples
code: def open(filename, mode="r", iline = 189,
                               xline = 193,
                               strict = True,
                               ignore_geometry = False,
                               endian = 'big'):
      """Open a segy file.
  
      Opens a segy file and tries to figure out its sorting, inline numbers,
      crossline numbers, and offsets, and enables reading and writing to this
      file in a simple manner.
  
      For reading, the access mode `r` is preferred. All write operations will
      raise an exception. For writing, the mode `r+` is preferred (as `rw` would
      truncate the file). Any mode with `w` will raise an error. The modes used
      are standard C file modes; please refer to that documentation for a
      complete reference.
  
      Open should be used together with python's ``with`` statement. Please refer
      to the examples. When the ``with`` statement is used the file will
      automatically be closed when the routine completes or an exception is
      raised.
  
      By default, segyio tries to open in ``strict`` mode. This means the file will
      be assumed to represent a geometry with consistent inline, crosslines and
      offsets. If strict is False, segyio will still try to establish a geometry,
      but it won't abort if it fails. When in non-strict mode is opened,
      geometry-dependent modes such as iline will raise an error.
  
      If ``ignore_geometry=True``, segyio will *not* try to build iline/xline or
      other geometry related structures, which leads to faster opens. This is
      essentially the same as using ``strict=False`` on a file that has no
      geometry.
  
      Parameters
      ----------
  
      filename : str
          Path to file to open
  
      mode : {'r', 'r+'}
          File access mode, read-only ('r', default) or read-write ('r+')
  
      iline : int or segyio.TraceField
          Inline number field in the trace headers. Defaults to 189 as per the
          SEG-Y rev1 specification
  
      xline : int or segyio.TraceField
          Crossline number field in the trace headers. Defaults to 193 as per the
          SEG-Y rev1 specification
  
      strict : bool, optional
          Abort if a geometry cannot be inferred. Defaults to True.
  
      ignore_geometry : bool, optional
          Opt out on building geometry information, useful for e.g. shot
          organised files. Defaults to False.
  
      endian : {'big', 'msb', 'little', 'lsb'}
          File endianness, big/msb (default) or little/lsb
  
      Returns
      -------
  
      file : segyio.SegyFile
          An open segyio file handle
  
      Raises
      ------
  
      ValueError
          If the mode string contains 'w', as it would truncate the file
  
      Notes
      -----
  
      .. versionadded:: 1.1
  
      .. versionchanged:: 1.8
          endian argument
  
      When a file is opened non-strict, only raw traces access is allowed, and
      using modes such as ``iline`` raise an error.
  
  
      Examples
      --------
  
      Open a file in read-only mode:
  
      >>> with segyio.open(path, "r") as f:
      ...     print(f.ilines)
      ...
      [1, 2, 3, 4, 5]
  
      Open a file in read-write mode:
  
      >>> with segyio.open(path, "r+") as f:
      ...     f.trace = np.arange(100)
  
      Open two files at once:
  
      >>> with segyio.open(path) as src, segyio.open(path, "r+") as dst:
      ...     dst.trace = src.trace # copy all traces from src to dst
  
      Open a file little-endian file:
  
      >>> with segyio.open(path, endian = 'little') as f:
      ...     f.trace[0]
  
      """
  
      if 'w' in mode:
          problem = 'w in mode would truncate the file'
          solution = 'use r+ to open in read-write'
          raise ValueError(', '.join((problem, solution)))
  
      endians = {
          'little': 256, # (1 << 8)
          'lsb': 256,
          'big': 0,
          'msb': 0,
      }
  
      if endian not in endians:
          problem = 'unknown endianness {}, expected one of: '
          opts = ' '.join(endians.keys())
          raise ValueError(problem.format(endian) + opts)
  
      from . import _segyio
      fd = _segyio.segyiofd(str(filename), mode, endians[endian])
      fd.segyopen()
      metrics = fd.metrics()
  
      f = segyio.SegyFile(fd,
              filename = str(filename),
              mode = mode,
              iline = iline,
              xline = xline,
              endian = endian,
      )
  
      try:
          dt = segyio.tools.dt(f, fallback_dt = 4000.0) / 1000.0
          t0 = f.header[0][segyio.TraceField.DelayRecordingTime]
          samples = metrics['samplecount']
          f._samples = (numpy.arange(samples) * dt) + t0
  
      except:
          f.close()
          raise
  
      if ignore_geometry:
          return f
  
      return infer_geometry(f, metrics, iline, xline, strict)