Tectonic_Utils.read_write package
general_io
Functions to read common file types into structures in Python. Example: a multi-segment file with polygons or lines, as could be used in GMT.
- read_gmt_multisegment_latlon(input_file, split_delimiter=' ')
Generalized GMT multisegment file reader. Returns lon and lat in a list of lists, each element with a single segment.
- Parameters
input_file (string) – name of input file
split_delimiter (string, optional) – delimiter between values on the same line, defaults to space
- Returns
list of lons, list of lats
- Return type
list
netcdf_read_write
Netcdf reading and writing functions. Only Netcdf3 and Netcdf4 files with PIXEL NODE REGISTRATION are valid. The assumption is 2D Netcdf files with 3 variables, in x-y-z order.
- flip_if_necessary(filename)
If netcdf3 file is stored with xinc or yinc backwards, we replace with a copy that flips the affected axis.
- Parameters
filename (string) – name of file
- give_metrics_on_grd(filename)
Print shape, min/max, and NaN metrics on a netcdf grid file.
- Parameters
filename (string) – name of grd file
- parse_pixelnode_registration(filename)
Ensure pixel node registration for netcdf file.
- Parameters
filename (string) – name of file
- produce_output_TS_grids(xdata, ydata, zdata, timearray, zunits, outfile)
Write many netcdf3 files, one for each step of a timearray. Each file will be named with a datetime suffix.
- Parameters
xdata – 1D array of floats
ydata – 1D array of floats
zdata – 3D array of floats
timearray – 1D array of anything
zunits – string
outfile – string, filename
- produce_output_netcdf(xdata, ydata, zdata, zunits, netcdfname, dtype=<class 'float'>)
Write netcdf3 grid file. NOTE: The pixel vs gridline registration of this function is not guaranteed; depends on file system and float type and precision :(. Safer to use write_netcdf4().
- produce_output_timeseries(xdata, ydata, zdata, timearray, zunits, netcdfname)
Write dataset with t, x, y, z into large 3D netcdf. Each 2D slice is the displacement at a particular time, associated with a time series. zdata comes in as a 2D array where each element is a timeseries (1D array), so it must be re-packaged into 3D array before we save it. Broke during long SoCal experiment for some reason. f.close() didn’t work.
- Parameters
xdata – 1D array of floats
ydata – 1D array of floats
zdata – 3D array of floats
timearray – 1D array of anything
zunits – string
netcdfname – string, filename
- properly_parse_three_variables(key1, key2, key3)
Set proper ordering for known keys in a netcdf file. Options: [x, y, z]; [lon, lat, z]; [longitude, latitude, z].
- Parameters
key1 (string) – names of netcdf variable key
key2 (string) – names of netcdf variable key
key3 (string) – names of netcdf variable key
- Returns
ordered keys
- Return type
list
- read_3D_netcdf(filename)
Reading function for 3D netcdf pixel-node registered files with key pattern ‘t, x, y, z’.
- Parameters
filename (string) – name of netcdf file
- Returns
[tdata, xdata, ydata, zdata]
- Return type
list of 4 np.ndarrays
- read_any_grd(filename)
A general netcdf4/netcdf3 reading function for pixel-node registered files with recognized key patterns.
- Parameters
filename (string) – name of file
- Returns
[xdata, ydata, zdata]
- Return type
list of 3 np.ndarrays
- read_netcdf3(filename)
A netcdf3 reading function for pixel-node registered files with recognized key patterns.
- Parameters
filename (string) – name of netcdf3 file
- Returns
[xdata, ydata, zdata]
- Return type
list of 3 np.ndarrays
- read_netcdf4(filename)
A netcdf4 reading function for pixel-node registered files with recognized key patterns.
- Parameters
filename (string) – name of netcdf4 file
- Returns
[xdata, ydata, zdata]
- Return type
list of 3 np.ndarrays
- write_netcdf4(x, y, z, outfile, precision=10)
Writing PIXEL NODE registered netcdf4 file from numpy array. Internal strategy: send out to a binary file and make GMT convert to netcdf. Note: Precision of 10 (or something high) ensures better performance for higher latitudes, like >60°.
- Parameters
x – 1D array of floats
y – 1D array of floats
z – 2D array of floats
outfile – filename, string
precision – how many decimal places for the x-inc and y-inc? Use higher precision for high latitudes.
- write_temp_output_txt(z, outfile)
A helper function for dumping grid data into pixel-node-registered grd files.
- Parameters
z – 2D array of floats
outfile – string, filename
read_kml
- read_simple_kml(infile)
Read a simple box drawn in Google Earth and saved as a KML file with field ‘coordinates’. For more complicated KMLs with many elements, I’m switching to use the Python library “fastkml” for reading.
- Parameters
infile (string) – kml file with simple box
- Returns
lons, lats as lists that represent the coordinates of box vertices
- Return type
list, list