source: fact/tools/pyscripts/pyfact/coor.py@ 13078

Last change on this file since 13078 was 13026, checked in by neise, 13 years ago
intitial commit of class that calculates list of NN for each pixel
  • Property svn:executable set to *
File size: 2.8 KB
Line 
1#!/usr/bin/python
2#
3# Dominik Neise
4#
5import os.path
6import numpy as np
7from pylab import *
8from euclid import *
9
10class Coordinator(object):
11 """ class to transform chid <-> hexagonal coordinates and vice versa """
12
13 def __init__(self, map_file_path = "../map_dn.txt"):
14 """ read map text file and generate from the three columns
15 chid, xe and ye
16 3 dictionaries: chid2coor, coor2chid, chid2nn
17 chid2nn means 'chid_to_next_neighbor_chids'
18
19 this is done by calculating the hexagonal coordinates
20 from the euclidian coordinates given in xe & ye.
21 the center and the two base vectors are hard coded to be:
22 center = Vector2( 0. , 1./2.)
23 ey = Vector2( 0. , 1. )
24 ex = Vector2( sqrt(3)/2. , 1./2. )
25 """
26 chid, y,x,xe,ye,yh,xh,softid,hardid = np.loadtxt("../map_dn.txt",unpack=True)
27 coors = zip(xe,ye,chid)
28 vectors_and_chids = []
29 for c in coors:
30 vectors_and_chids.append( (Vector2(c[0], c[1]) , int(c[2])) )
31
32
33 center = Vector2( 0. , 1./2.)
34 ey = Vector2( 0. , 1. )
35 ex = Vector2( sqrt(3)/2. , 1./2. )
36
37 coor2chid = {}
38 chid2coor = {}
39 for vector_and_chid in vectors_and_chids:
40 vec = vector_and_chid[0]
41 chid = vector_and_chid[1]
42
43 x = (vec-center).x / float(ex.x)
44 y = ((vec-center)-x*ex).y / float(ey.y)
45
46 if abs(x) < 0.01:
47 x=0.0
48 if abs(y) < 0.01:
49 y=0.0
50
51 coor = (int(round(x)),int(round(y)))
52
53 if coor in coor2chid:
54 print 'error while filling "coor2chid":'
55 print 'coor:',coor,'of chid:',chid,
56 print 'is equal to coor of chid:',coor2chid[coor]
57
58 coor2chid[ coor ] = chid
59 chid2coor[ chid ] = coor
60
61 # hard code the offsets to the next neighbors
62 offsets = [ Vector2(1,0) , Vector2(-1,0) , Vector2(1,-1) ,
63 Vector2(0,1) , Vector2(0,-1) , Vector2(-1,1) ]
64 chid2nn = {}
65 for chid in chid2coor.keys():
66 coor = Vector2( chid2coor[chid][0] , chid2coor[chid][1] )
67 nn_coors = []
68 nn_chids = []
69 for offset in offsets:
70 nn_coors.append( ((coor+offset).x , (coor+offset).y) )
71 for coor in nn_coors:
72 if coor in coor2chid:
73 nn_chids.append( coor2chid[coor] )
74 chid2nn[chid] = nn_chids
75 self.nn = chid2nn
76 self.chid2coor = chid2coor
77 self.coor2chid = coor2chid
78
79# for chid in chid2nn.keys():
80# print chid, '->',chid2nn[chid]
81
82def first(a):
83 return a[0]
84
85def second(a):
86 return a[1]
87
88if __name__ == '__main__':
89 co = Coordinator()
90
Note: See TracBrowser for help on using the repository browser.