Export Your Model
Learning objectives
- Write the book's velocity model as NumPy, CSV and SEG-Y, and read the first bytes of each field by field
- Say what a reader needs besides the numbers: the byte order, the float format (IEEE or IBM), the unit, the digits kept and the sample interval
- Weigh the files by size, and by what a rounded or misread file does to the contrast at the top of the bed
- Say what a velocity file leaves out
A Model That Has to Leave the Page
Every model in this book has lived in the page as an array of numbers. To be run by another engine or opened in another package it has to become a file, and a file is bytes: the same numbers, plus whatever a reader needs to turn the bytes back into them. The figure writes the book's one bed, the standard shale around the standard soft sand (Part 3 builds it, and the figure gives it the four shapes of the book's test models), as its P velocity on the 160 by 120 grid of 5 m cells, 19,200 values, in four files, and reads each file back from its bytes alone. Four bytes a cell, 76,800 bytes, is the least a 32-bit float can cost; what a file adds to that, and what a reader has to be told, is the subject.
NumPy: a Dictionary and Four Bytes a Cell
The NumPy format, version 1.0, is the simplest of the three. Its own documentation sets it out: "The first 6 bytes are a magic string: exactly \x93NUMPY", then one byte for each of the major and minor version, then "a little-endian unsigned short int: the length of the header data HEADER_LEN", then the header, "an ASCII string which contains a Python literal expression of a dictionary", with the keys descr, fortran_order and shape. The header is "terminated by a newline (\n) and padded with spaces (\x20) to make the total of len(magic string) + 2 + len(length) + HEADER_LEN be evenly divisible by 64 for alignment purposes". Here that is 10 bytes and a dictionary of 118, 128 in all, and the file is 76,928 bytes. Everything a reader needs is in the dictionary: '<f4' says four-byte floats with the small end first, and (120, 160) says 120 rows of 160, the rows running down in depth. The first sample, 2800 m/s, is the four bytes 00 00 2F 45. A reader that ignores the < and takes them big-endian gets a number of the order of m/s.
CSV: Text Costs Digits
A CSV file has no header and no promise: the numbers are text, one row to a depth sample and one column to a trace, with as many decimals as the writer chose. With one decimal of m/s it is 134,400 bytes, 7 a cell, against 4 for the binary files. It also keeps only what it prints. In m/s the rounding is harmless: with one decimal the worst cell of the gas model is 0.024 m/s out. In km/s it is not. The brine sand is 2.89 km/s and the shale 2.80, a velocity contrast of +0.016 at the top of the bed; written with no decimals both are 3, and the bed is gone from the file. With one decimal they are 2.9 and 2.8 and the contrast reads +0.018; with two it reads +0.016 again, 96,000 bytes. The gas sand, 2.34 km/s, written with no decimals is 2 against the shale's 3, and its contrast reads −0.200 where the model has −0.089. The digits that a unit leaves are the digits a close pair of velocities needs.
SEG-Y: Headers, Big-Endian, and Two Kinds of Float
SEG-Y is the exchange format of the industry, and the figure writes revision 1 (Norris and Faichney, 2002). A file begins with a textual header of 3,200 bytes, forty lines of eighty characters, which the standard allows to be "encoded as EBCDIC or ASCII"; the figure writes EBCDIC, so the hex of its first line, C3 F0 F1 40, is the characters C01 and a space and not the ASCII a reader might expect. Then comes a binary header of 400 bytes, the fields of which sit at fixed positions, and then every trace as a header of 240 bytes and its samples. "In the 1975 SEG Y standard, all binary values are defined as using ‘big-endian’ byte ordering", the opposite of NumPy's. The binary header carries the sample interval (bytes 3217 and 3218), the samples per trace (3221 and 3222), the data sample format code (3225 and 3226), the measurement system (3255 and 3256), the revision (3501 and 3502) and the fixed length trace flag (3503 and 3504); each trace header repeats the samples and the interval and holds the trace's position, here the ensemble X in bytes 181 to 184 with a coordinate scalar of −10 in bytes 71 and 72, where "If positive, scalar is used as a multiplier; if negative, scalar is used as divisor."
The standard's format code 5 is a 4-byte IEEE float and code 1 a 4-byte IBM float. The IBM float is older: one sign bit, a 7-bit exponent "to the base 16 (not 2)" with "a bias of 64", and a 24-bit fraction (NASA NSSDC). The value is
and the shale's 2800 m/s is 43 AF 00 00, where the IEEE float is 45 2F 00 00. The format code is the only thing that says which one the four bytes are: a reader that assumes IEEE reads 43 AF 00 00 as 350.0 m/s, 8 times too small, and a reader that takes the bytes little-endian reads the order of m/s. Because a base-16 exponent leaves up to three leading zeros in the fraction, an IBM float can hold fewer significant bits than an IEEE one: in km/s the largest error over the gas model is 0.00033 m/s as IBM and 0.000095 m/s as IEEE, both far under what a velocity is known to.
Then there is the sample interval. The standard defines the field in microseconds: "Sample interval in microseconds (µs)". A model in depth has no microseconds, so the figure puts the cell, 5, in the field and says metres in the measurement system field (1) and in the textual header, which is the figure's own choice and not the standard's; a reader that takes the field at its word sees 5 µs between samples and 0.6 ms in a 120-sample trace, where the model has 5 m and 600 m. SEG-Y is the largest of the binary files for the same cells: 3,600 bytes of file headers and 240 on each of 160 traces, 42,000 bytes, make 118,800 in all, and 35.4 % of it is not samples.
What a Velocity File Leaves Out
Whichever file is written, it holds one number a cell. A reader of the gas model's velocity file finds a contrast of −0.089 at the top of the bed, the velocity contrast . The impedance contrast of the same model, the one the convolutional model of Part 3 uses, is −0.138, because the gas sand is also lighter than the shale and the file does not carry the density. An engine that reads only the velocity, as the wave-equation engine of Part 4 does, reflects less than the model says; whether a model is fit to run is the question of the next section, Model QC.
References
- NumPy Developers. numpy.lib.format: binary serialization, format version 1.0 (the documentation of the .npy format in the NumPy package, version 2.5.3).
- Norris, M. W., & Faichney, A. K. (Eds.) (2002). SEG Y rev 1 data exchange format, Release 1.0, May 2002. SEG Technical Standards Committee, Society of Exploration Geophysicists.
- NASA NSSDC. IBM 32-bit floating point format. https://nssdc.gsfc.nasa.gov/nssdc/formats/IBM_32-Bit.html