Created
March 1, 2018 08:41
-
-
Save pont-us/b8c49800bd5f344568df60481a85ce9c to your computer and use it in GitHub Desktop.
Replace values in Depth column of a 2G magnetometer file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
""" | |
Replace the contents of the column headed "Depth" in a tab-delimited text | |
file with a repeated, increasing range of specified numbers. Intended to fix | |
incorrect depths in 2G magnetometer files. The column header is retained in | |
the output file. The values below it are replaced with a sequence starting at | |
the specified min_depth and ending at the specified max_depth (inclusive). | |
Depths are specified in centimetres and incremented by 1 cm per line, | |
but written in metres (se specifying --min_depth=15, --max_depth=114 will | |
produce 0.15, 0.16, ... , 1.13, 1.14). The range will loop back to the start | |
if there are more lines in the file than are covered by the range. | |
By Pontus Lurcock, 2018. Released into the public domain. | |
""" | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument("min_depth", type=int) | |
parser.add_argument("max_depth", type=int) | |
parser.add_argument("infile") | |
parser.add_argument("outfile") | |
args = parser.parse_args() | |
with open(args.infile, newline="") as fh: | |
inlines = fh.readlines() | |
depth_column = inlines[0].strip().split("\t").index("Depth") | |
outlines = [inlines[0]] | |
depth = args.min_depth | |
for line in inlines[1:]: | |
fields = line.split("\t") | |
fields[depth_column] = "{:.2f}".format(depth / 100) | |
depth += 1 | |
if depth > args.max_depth: | |
depth = args.min_depth | |
outlines.append("\t".join(fields)) | |
with open(args.outfile, "w") as outfh: | |
outfh.writelines(outlines) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment