COMPASS  5.4.4
End-to-end AO simulation tool using GPU acceleration
psfMap.py
1 
37 
38 import numpy as np
39 import astropy.io.fits as fits
40 import matplotlib.pyplot as plt
41 
42 
43 class PSF_map:
44 
45  def __init__(self, SR=np.zeros((0, 0)), radius=0, wl=0, NGS=None, LGS=None, TS=None,
46  AOtype="AO", sup=None, filename=None):
47  """ SR_map class
48 
49  SR : np.ndarray : array of strehl (organised by position)
50  radius : float : radius of the map (in arcsec)
51  wl : float : wavelength of observation (in nm)
52  NGS : np.ndarray : position (arcsec) of the NGS (1st line:x axis, 2nd:y axis)
53  LGS : np.ndarray : position (arcsec) of the LGS (1st line:x axis, 2nd:y axis)
54  TS : np.ndarray : position (arcsec) of the TS (1st line:x axis, 2nd:y axis)
55  sup : shesha.supervisor : shesha supervisor to retrieve all data from
56  filename: str : filename to read the psf map from
57 
58  warning:
59  using 'sup' will take only the supervisor into consideration
60  using 'filename' will overide previous values of the psf map (unless called with sup)
61 
62  """
63  self.mapmap = SR
64 
65  self._Rtar_Rtar = radius
66  self._Rngs_Rngs = 0
67  self._Rlgs_Rlgs = 0
68  self._Rlgs_Rlgs = 0
69  if (NGS is not None):
70  self.NGSxNGSx = NGS[0]
71  self.NGSyNGSy = NGS[1]
72  self._Rngs_Rngs = max((self.NGSxNGSx.max(), self.NGSyNGSy.max()))
73  else:
74  self.NGSxNGSx = np.zeros((0))
75  self.NGSyNGSy = np.zeros((0))
76 
77  if (LGS is not None):
78  self.LGSxLGSx = LGS[0]
79  self.LGSyLGSy = LGS[1]
80  self._Rlgs_Rlgs = max(self.LGSxLGSx.max(), self.LGSyLGSy.max())
81  else:
82  self.LGSxLGSx = np.zeros((0))
83  self.LGSyLGSy = np.zeros((0))
84 
85  if (TS is not None):
86  self.TSxTSx = TS[0]
87  self.TSyTSy = TS[1]
88  self._Rts_Rts = max(self.TSxTSx.max(), self.TSyTSy.max())
89  else:
90  self.TSxTSx = np.zeros((0))
91  self.TSyTSy = np.zeros((0))
92 
93  self.wavelengthwavelength = wl
94  self.typetype = AOtype
95 
96  if (sup is not None):
97  self.NGSxNGSx = np.array([t.xpos for t in sup.config.p_wfs_ngs[:-1]])
98  self.NGSyNGSy = np.array([t.ypos for t in sup.config.p_wfs_ngs[:-1]])
99  self._Rngs_Rngs = max((self.NGSxNGSx.max(), self.NGSyNGSy.max()))
100  self.LGSxLGSx = np.array([t.xpos for t in sup.config.p_wfs_lgs])
101  self.LGSyLGSy = np.array([t.ypos for t in sup.config.p_wfs_lgs])
102  self._Rlgs_Rlgs = max(self.LGSxLGSx.max(), self.LGSyLGSy.max())
103  self.TSxTSx = np.array([t.xpos for t in sup.config.p_wfs_ts])
104  self.TSyTSy = np.array([t.ypos for t in sup.config.p_wfs_ts])
105  self._Rts_Rts = max(self.TSxTSx.max(), self.TSyTSy.max())
106  self.wavelengthwavelength = sup.config.p_targets[0].Lambda
107  NTAR = len(sup.config.p_targets)
108  NTAR_side = int(np.sqrt(NTAR))
109  if (NTAR != NTAR_side**2):
110  raise ValueError("not a square nb of targets")
111  self.mapmap = np.zeros((NTAR_side, NTAR_side))
112  for i in range(NTAR):
113  #self.map.itemset(i,sup._sim.get_strehl(i)[1])
114  self.mapmap.itemset(i, sup._sim.get_strehl(i)[0])
115  tar = sup._sim.tar.d_targets[i]
116  self._Rtar_Rtar = max(self._Rtar_Rtar, tar.posx, tar.posy)
117 
118  elif (filename is not None):
119  self.readread(filename)
120 
121  def setNGS(self, NGS, NGSy=None):
122  if (NGSy is None):
123  self.NGSxNGSx = NGS[0]
124  self.NGSyNGSy = NGS[1]
125  else:
126  self.NGSNGS = NGS
127  self.NGSyNGSy = NGS
128  self._Rngs_Rngs = max((self.NGSxNGSx.max(), self.NGSyNGSy.max()))
129 
130  def setLGS(self, LGS, LGSy=None):
131  if (LGSy is None):
132  self.LGSxLGSx = LGS[0]
133  self.LGSyLGSy = LGS[1]
134  else:
135  self.LGSLGS = LGS
136  self.LGSyLGSy = LGS
137  self._Rlgs_Rlgs = max(self.LGSxLGSx.max(), self.LGSyLGSy.max())
138 
139  def setTS(self, TS, TSy=None):
140  if (TSy is None):
141  self.TSxTSx = TS[0]
142  self.TSyTSy = TS[1]
143  else:
144  self.TSxTSx = TS
145  self.TSyTSy = TSy
146  self._Rts_Rts = max(self.TSxTSx.max(), self.TSyTSy.max())
147 
148  def setWaveLength(self, wl):
149  self.wavelengthwavelength = wl
150 
151  def plot(self, title=False, GS=False, WFS=False, LGS=False, NGS=False, TS=False):
152  if (self.mapmap.shape[0] > 0 and self.mapmap.shape[1] > 0):
153  plt.matshow(self.mapmap,
154  extent=[-self._Rtar_Rtar, self._Rtar_Rtar, -self._Rtar_Rtar, self._Rtar_Rtar])
155  plt.colorbar()
156  if (GS or WFS or LGS):
157  plt.scatter(self.LGSyLGSy, self.LGSxLGSx, color="red")
158  if (GS or WFS or NGS):
159  plt.scatter(self.NGSyNGSy, self.NGSxNGSx, color="blue")
160  if (GS or TS):
161  plt.scatter(self.TSyTSy, self.TSxTSx, color="yellow", s=1)
162 
163  t = self.typetype + " Strehl"
164  if (self.wavelengthwavelength > 0):
165  t += " @ {:.3f} nm".format(self.wavelengthwavelength)
166 
167  if (self._Rts_Rts > 0):
168  t += ", {:.2f}".format(self._Rts_Rts) + " arcsec ring optim"
169  if (title):
170  plt.title(t)
171 
172  def save(self, name=""):
173  hdu_map = fits.PrimaryHDU(self.mapmap)
174  hdu_map.header["TYPE"] = self.typetype
175  hdu_map.header["LAMBDA"] = self.wavelengthwavelength
176  hdu_map.header["RTAR"] = self._Rtar_Rtar
177  hdu_map.header["RLGS"] = self._Rlgs_Rlgs
178  hdu_map.header["RNGS"] = self._Rngs_Rngs
179  hdu_map.header["RTS"] = self._Rts_Rts
180  hdu_LGSX = fits.ImageHDU(self.LGSxLGSx, name="LGSX")
181  hdu_LGSY = fits.ImageHDU(self.LGSyLGSy, name="LGSY")
182  hdu_NGSX = fits.ImageHDU(self.NGSxNGSx, name="NGSX")
183  hdu_NGSY = fits.ImageHDU(self.NGSyNGSy, name="NGSY")
184  hdu_TSX = fits.ImageHDU(self.TSxTSx, name="TSX")
185  hdu_TSY = fits.ImageHDU(self.TSyTSy, name="TSY")
186 
187  hdul = fits.HDUList([
188  hdu_map, hdu_LGSX, hdu_LGSY, hdu_NGSX, hdu_NGSY, hdu_TSX, hdu_TSY
189  ])
190  t = self.typetype + "_StrehlMap"
191  if (self.wavelengthwavelength > 0):
192  t += "_{:.3f}nm".format(self.wavelengthwavelength)
193  if (self._Rts_Rts > 0):
194  t += "_{:.2f}arcsec".format(self._Rts_Rts)
195  t += ".fits"
196  if (name == ""):
197  name = t
198  hdul.writeto(name, overwrite=1)
199 
200  def read(self, name):
201  hdu_map = fits.open(name)
202  self.typetype = hdu_map[0].header["TYPE"]
203  self.wavelengthwavelength = hdu_map[0].header["LAMBDA"]
204  self._Rtar_Rtar = hdu_map[0].header["RTAR"]
205  self._Rlgs_Rlgs = hdu_map[0].header["RLGS"]
206  self._Rngs_Rngs = hdu_map[0].header["RNGS"]
207  self._Rts_Rts = hdu_map[0].header["RTS"]
208  self.mapmap = hdu_map[0].data
209  self.LGSxLGSx = hdu_map["LGSx"].data
210  self.LGSyLGSy = hdu_map["LGSy"].data
211  self.NGSxNGSx = hdu_map["NGSx"].data
212  self.NGSyNGSy = hdu_map["NGSy"].data
213  self.TSxTSx = hdu_map["TSx"].data
214  self.TSyTSy = hdu_map["TSy"].data
def plot(self, title=False, GS=False, WFS=False, LGS=False, NGS=False, TS=False)
Definition: psfMap.py:151
def setLGS(self, LGS, LGSy=None)
Definition: psfMap.py:130
def read(self, name)
Definition: psfMap.py:200
def save(self, name="")
Definition: psfMap.py:172
def setNGS(self, NGS, NGSy=None)
Definition: psfMap.py:121
def setWaveLength(self, wl)
Definition: psfMap.py:148
def __init__(self, SR=np.zeros((0, 0)), radius=0, wl=0, NGS=None, LGS=None, TS=None, AOtype="AO", sup=None, filename=None)
SR_map class.
Definition: psfMap.py:62
def setTS(self, TS, TSy=None)
Definition: psfMap.py:139