diff --git a/README.md b/README.md new file mode 100644 index 0000000000000000000000000000000000000000..be0c2210c2f22a5f48814a7cb40795ca473b6132 --- /dev/null +++ b/README.md @@ -0,0 +1,62 @@ +# `asccol` ASCII Columnar Data Parser + +Use this simple library to parse aligned text data into Python named +tuples, using your own specifications for keyword, type, and column +width. + +## Example usage + +```py +import math +import sys +from asccol import DataSpec, Columns, parse_data + +def simple_time(s): + """Convert a HHMM string to an (H, M) tuple.""" + hour = s[:2] + minute = s[2:4] + return tuple(map(int, (hour, minute))) + +def filtered_float(s): + """Like float, but ignore the special value 444.0.""" + val = float(s) + return val if val != 444.0 else math.nan + +# 10 minute data +# See https://amrc.ssec.wisc.edu/data/ftp/pub/aws/q10/q10readme.txt +Q10 = DataSpec( + leading=2, # lines to delete + cols=Columns( + ('year', int, 4), # keyword, type (converter function), width + ('jdate', int, 4), + ('month', int, 3), + ('date', int, 3), + ('time', simple_time, 5), + ('temp', filtered_float, 7), + ('pressure', filtered_float, 7), + ('wind_speed', filtered_float, 7), + ('wind_dir', filtered_float, 7), + ('rel_hum', filtered_float, 7), + ('delta_t', filtered_float, 7), + ), +) + +for row in parse_data(sys.stdin, Q10): + print(row) +``` + +The program above will transform this input: + +``` +Year: 2021 Month: 02 ID: BYD ARGOS: 8903 Name: Byrd +Lat: 80.02S Lon: 119.44W Elev: 1539m +2021 32 2 1 0000 -16.3 803.9 8.9 22.0 79.0 444.0 +2021 32 2 1 0010 -16.4 803.9 8.6 25.0 79.0 444.0 +``` + +...into this output: + +``` +row(year=2021, jdate=32, month=2, date=1, time=(0, 0), temp=-16.3, pressure=803.9, wind_speed=8.9, wind_dir=22.0, rel_hum=79.0, delta_t=nan) +row(year=2021, jdate=32, month=2, date=1, time=(0, 10), temp=-16.4, pressure=803.9, wind_speed=8.6, wind_dir=25.0, rel_hum=79.0, delta_t=nan) +```