3 # Copyright (C) 2010-2012 W. Trevor King <wking@drexel.edu>
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Lesser General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this program. If not, see
17 # <http://www.gnu.org/licenses/>.
20 """Convert `clickloc.tk` pixel coordinates to physical coordinates
23 from math import exp, log
29 if __name__ == '__main__':
30 from argparse import ArgumentParser
32 parser = ArgumentParser(description=__doc__, version=__version__)
34 '-x', '--logx', default=False, action='store_const', const=True,
35 help='Use a log scale for the physical abscissa')
37 '-y', '--logy', default=False, action='store_const', const=True,
38 help='Use a log scale for the physical ordinate')
40 dest='xmin', type=float,
41 help='Physical coordinate for the xmin-pixel pair')
43 dest='xmax', type=float,
44 help='Physical coordinate for the xmax-pixel pair')
46 dest='ymin', type=float,
47 help='Physical coordinate for the ymin-pixel pair')
49 dest='ymax', type=float,
50 help='Physical coordinate for the ymax-pixel pair')
53 help='File containing pixel coordinates')
54 args = parser.parse_args()
56 with open(args.input, 'r') as f:
57 # reference points in pixels from the first 4 clicks
58 xmin_p = float(f.readline().split()[0])
59 xmax_p = float(f.readline().split()[0])
60 ymin_p = float(f.readline().split()[1])
61 ymax_p = float(f.readline().split()[1])
63 for key in ['xmin', 'xmax', 'ymin', 'ymax']:
64 loc[key] = getattr(args, key)
66 print(('# Xp -> exp((Xp-{xmin_p})/({xmax_p}-{xmin_p})'
67 '*log({xmax}/{xmin}) + log({xmin}))').format(**loc))
69 print(('# Xp -> ({xmax}-{xmin})/({xmax_p}-{xmin_p})'
70 '*(Xp-{xmin_p}) + {xmin}').format(**loc))
72 print(('# Yp -> exp((Yp-{ymin_p})/({ymax_p}-{ymin_p})'
73 '*log({ymax}/{ymin}) + log({ymin}))').format(**loc))
75 print(('# Yp -> ({ymax}-{ymin})/({ymax_p}-{ymin_p}'
76 '*(Yp-{ymin_p}) + {ymin}').format(**loc))
79 x_p,y_p = [float(x) for x in line.split()]
82 (x_p - xmin_p)/(xmax_p - xmin_p)*log(xmax/xmin)
85 x = (xmax - xmin)/(xmax_p - xmin_p) * (x_p - xmin_p) + xmin
88 (y_p - ymin_p)/(ymax_p - ymin_p)*log(ymax/ymin)
91 y = (ymax - ymin)/(ymax_p - ymin_p) * (y_p - ymin_p) + ymin
92 print('{}\t{}'.format(x, y))