#! /usr/bin/gawk -f # Last edited on 2023-09-02 00:02:05 by stolfi # Reads from {stdin} a file with user output coordinates of some # reference points. Applies specified adjustments to those ref points. # Writes the adjusted points to {stdout}. # The user must define (with "-v") the variables # # {oUnit} size of the output user coordinate unit in pixels. # {tagPref} the prefix of the tags of points to adjust. # {deltas} a string with the adjustments to the points. # # # The string {deltas} myst contain some number {NP} of pairs of numbers, # separated by spaces. The numbers may be negative and are assumed to be # inclrements of output /pixel/ coordinates. # # The input file must include lines of the format # # {PREF}{K} {XO} {YO} # # (with no leading space), where {PREF} is a string of one or more # letters, {K} is non-negative integer, and {XO} and {YO} are the # output user coordinates of a ref point. The string {PREF}{K} is # assumed to be the {TAG} of that point. # # The adjustments are applied only to those lines with {PREF} equal to # {tagPref} and {K} in {0..NP-1}. Any other lines in the input file # are ignored. # # The output file has the same format, only thet the user output # coordinates {XO} and {YO} are modified by the pair of pixel # displacements in the {deltas} list corresponding to {K}, namely # {deltas[2*K + 1]} and {deltas[2*K + 2]}, corrected by {oUnit}. BEGIN { nPref = length(tagPref); # Length of tag prefix. if (nPref == 0) { arg_error(("bad prefix \"" tagPref "\"")); } nd = split(deltas, dp); # Total number of deltas. if ((nd == 0) || (nd % 2 != 0)) { arg_error(("bad number of deltas = " nd)); } np = int(nd/2); # Number of points to map. no = 0; # Number of mapped points written out. } //{ # printf "%s\n", $0 > "/dev/stderr"; } /^[A-Za-z]+[0-9]+[ ]/ { tag=$1; if (substr(tag,1,nPref) != tagPref) { # printf "wrong tag prefix, ignored\n" > "/dev/stderr"; next; } # Original output user coordinates of ref point: ox = $2 + 0; oy = $3 +0; # Get number part {k} of tag: k = substr(tag,nPref+1) + 0; if (k >= np) { # printf "wrong index, ignored\n" > "/dev/stderr"; next; } # Get corresponding deltas and apply them: dx = dp[2*k + 1] + 0; dy = dp[2*k + 2] + 0; # Adjusted output user coordinates of ref point: nx = ox + dx/oUnit; ny = oy + dy/oUnit; # Write the adjusted point: printf "%-6s %6.1f %6.1f\n", tag, nx, ny; no++; next; } // { # printf "not point line, ignored\n" > "/dev/stderr"; next; } END{ printf "%d adjusted points written.\n", no > "/dev/stderr"; } function arg_error(msg) { printf "** %s\n", msg > "/dev/stderr"; exit(1) } function data_error(msg) { printf "%s:%d: ** %s\n", FILENAME, FNR, msg > "/dev/stderr"; printf " [[%s]]\n", $0; exit(1) }