diff options
author | Mike Gerwitz <gerwitm@lovullo.com> | 2016-08-24 09:43:05 -0400 |
---|---|---|
committer | Mike Gerwitz <gerwitm@lovullo.com> | 2016-08-24 12:38:00 -0400 |
commit | ff01f39c1e8c9b9549d884a0db1f9a74799cf37e (patch) | |
tree | 35978db88a8d385250b1b47ad05966e19516373d /src/current/tools/csvi | |
parent | 6c0aa54bd1b7b49d736f0db3a8f48b7aa90b3b65 (diff) | |
download | tame-ff01f39c1e8c9b9549d884a0db1f9a74799cf37e.tar.gz tame-ff01f39c1e8c9b9549d884a0db1f9a74799cf37e.tar.bz2 tame-ff01f39c1e8c9b9549d884a0db1f9a74799cf37e.zip |
Liberate current implementation of "Calc DSL"
(Copyright headers will be added in the next commit; these are the
original files, unaltered in any way.)
The internal project name at LoVullo is simply "Calc DSL". This
liberates the entire thing. If anything was missed, I'll be added
later.
To continue building at LoVullo with this move, symlinks are used for
the transition; this is the exact code that is used in production.
There is a lot here---over 25,000 lines. Much of it is in disarray from
the environment surrounding its development, but it does work well for
what it was intended to do.
(LoVullo folks: fork point is 65723a0 in calcdsl.git.)
Diffstat (limited to 'src/current/tools/csvi')
-rwxr-xr-x | src/current/tools/csvi | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/src/current/tools/csvi b/src/current/tools/csvi new file mode 100755 index 0000000..08d80e2 --- /dev/null +++ b/src/current/tools/csvi @@ -0,0 +1,124 @@ +#!/usr/bin/awk -f +# +# Performs interpolation for columns in a CSV and outputs the result +# +# Configurable values (use -vname=value from command line): +# step - use predeterminated step instead of calculating from first two rows +# +# # + +function storeline() +{ + for ( i = 1; i <= hlen; i++ ) { + prev[i] = $i + } +} + +function clearline() +{ + for ( i = 1; i <= hlen; i++ ) { + prev[i] = 0 + } +} + +function getprev() +{ + for ( i = 1; i <= hlen; i++ ) { + $i = prev[i] + } +} + + +function interpolate() +{ + lastval = prev[1] + + curval = $1 + diff = curval - lastval + + # does this value fall in line with the requested step? + if ( diff == step ) + { + storeline() + + # we're good; continue + print + next + } + + # if we do not yet have a value large enough to reach our step, then continue + # until we do (do not store this line) + n = int( diff / step ) + if ( n <= 0 ) { + next + } + + # determine interpolation values + for ( i = 2; i <= hlen; i++ ) { + ival[i] = ( ( $i - prev[i] ) / n ) + } + + getprev() + + # let us interpolate values that are divisible by the step + do + { + # increase the last value by our step + $1 += step + + # interpolate each column value (notice that we skip the first column, which + # was handled directly above) + for ( i = 2; i <= hlen; i++ ) { + $i += ival[i] + } + + # print the new line + print + } while ( ( diff -= step ) > 0 ) + + # anything remaining does not fit into our step and will be ignored; we'll + # continue with our next step at the next line + + # consider this to be our last line + storeline() +} + + +BEGIN { + # the first row of the CSV is the header representing the column identifiers + getline + hlen = split( $0, header, /,/ ) + + # output the header + print $0 + + # delimit fields by commas (the field separator for CSVs); note that this + # won't work properly if strings contain commas + FS = OFS = "," + + clearline() + getline + + # if no step was provided, then calculate one based on the first two rows + if ( step == 0 ) { + # output the first row, which does not need to be interpolated + print + + # compute the step + vala = $1 + getline + valb = $1 + step = valb - vala + + # since the second line is used to determine the step, then it must match the + # step and therefore is good to output + print + + # begin. + storeline() + } +} + + +# for each row +{ interpolate() } |