def test_distortions_ex1(): e_x1 = [-14.95369787, 88.60095332, 999.76814675, 2019.84941119] e_y1 = [1202.73558767, 898.46492016, 192.1974284, -26.51887593] x0 = [1, 100, 1000, 1990] y0 = [1200, 900, 200, 5] x1, y1 = exvp(x0, y0) assert numpy.allclose(x1, e_x1) assert numpy.allclose(y1, e_y1)
def test_distortions_ex2(): """Inverse transformation, not very precise""" x0 = [1, 100, 1000, 1990] y0 = [1200, 900, 200, 5] p_x1 = [16.20132521, 110.97939823, 1000.22549019, 1962.80728145] p_y1 = [1197.39342201, 901.47856688, 207.58843519, 33.71359561] x1, y1 = exvp(p_x1, p_y1) assert numpy.allclose(x1, x0, rtol=2e-2) assert numpy.allclose(y1, y0, rtol=2e-2)
def compute_slits(self, hdulist, csu_conf): self.logger.debug('finding borders of slits') self.logger.debug('not strictly necessary...') data = hdulist[0].data self.logger.debug('dtype of data %s', data.dtype) self.logger.debug('median filter (3x3)') image_base = ndi.filters.median_filter(data, size=3) # Cast as original type for skimage self.logger.debug('casting image to unit16 (for skimage)') iuint16 = np.iinfo(np.uint16) image = np.clip(image_base, iuint16.min, iuint16.max).astype(np.uint16) self.logger.debug('compute Sobel filter') # FIXME: compute sob and sob_v is redundant sob = filt.sobel(image) self.save_intermediate_array(sob, 'sobel_image.fits') sob_v = filt.sobel_v(image) self.save_intermediate_array(sob_v, 'sobel_v_image.fits') # Compute detector coordinates of bars all_coords_virt = np.empty((110, 2)) all_coords_real = np.empty((110, 2)) # Origin of coordinates is 1 for bar in csu_conf.bars.values(): all_coords_virt[bar.idx - 1] = bar.xpos, bar.y0 # Origin of coordinates is 1 for this function _x, _y = dist.exvp(all_coords_virt[:, 0], all_coords_virt[:, 1]) all_coords_real[:, 0] = _x all_coords_real[:, 1] = _y # FIXME: hardcoded value h = 16 slit_h_virt = 16.242 slit_h_tol = 3 slits_bb = {} mask1 = np.zeros_like(hdulist[0].data) for idx in range(EMIR_NBARS): lbarid = idx + 1 rbarid = lbarid + EMIR_NBARS ref_x_l_v, ref_y_l_v = all_coords_virt[lbarid - 1] ref_x_r_v, ref_y_r_v = all_coords_virt[rbarid - 1] ref_x_l_d, ref_y_l_d = all_coords_real[lbarid - 1] ref_x_r_d, ref_y_r_d = all_coords_real[rbarid - 1] width_v = ref_x_r_v - ref_x_l_v # width_d = ref_x_r_d - ref_x_l_d if (ref_y_l_d >= 2047 + h) or (ref_y_l_d <= 1 - h): # print('reference y position is outlimits, skipping') continue if width_v < 5: # print('width is less than 5 pixels, skipping') continue plot = False regionw = 12 px1 = coor_to_pix_1d(ref_x_l_d) - 1 px2 = coor_to_pix_1d(ref_x_r_d) - 1 prow = coor_to_pix_1d(ref_y_l_d) - 1 comp_l, comp_r = calc0(image, sob_v, prow, px1, px2, regionw, h=h, plot=plot, lbarid=lbarid, rbarid=rbarid, plot2=False) if np.any(np.isnan([comp_l, comp_r])): self.logger.warning("converting NaN value, border of=%d", idx + 1) self.logger.warning("skipping bar=%d", idx + 1) continue elif comp_l > comp_r: # Not refining self.logger.warning("computed left border of=%d greater than right border", idx + 1) comp2_l, comp2_r = px1, px2 else: region2 = 5 px21 = coor_to_pix_1d(comp_l) px22 = coor_to_pix_1d(comp_r) comp2_l, comp2_r = calc0(image, sob_v, prow, px21, px22, region2, refine=True, plot=plot, lbarid=lbarid, rbarid=rbarid, plot2=False) if np.any(np.isnan([comp2_l, comp2_r])): self.logger.warning("converting NaN value, border of=%d", idx + 1) comp2_l, comp2_r = comp_l, comp_r elif comp2_l > comp2_r: # Not refining self.logger.warning("computed left border of=%d greater than right border", idx + 1) comp2_l, comp2_r = comp_l, comp_r # print('slit', lbarid, '-', rbarid, comp_l, comp_r) # print('pos1', comp_l, comp_r) # print('pos2', comp2_l, comp2_r) xpos1_virt, _ = dist.pvex(comp2_l + 1, ref_y_l_d) xpos2_virt, _ = dist.pvex(comp2_r + 1, ref_y_r_d) y1_virt = ref_y_l_v - slit_h_virt - slit_h_tol y2_virt = ref_y_r_v + slit_h_virt + slit_h_tol _, y1 = dist.exvp(xpos1_virt + 1, y1_virt) _, y2 = dist.exvp(xpos2_virt + 1, y2_virt) # print(comp2_l, comp2_r, y1 - 1, y2 - 1) cbb = BoundingBox.from_coordinates(comp2_l, comp2_r, y1 - 1, y2 - 1) slits_bb[lbarid] = cbb mask1[cbb.slice] = lbarid self.save_intermediate_array(mask1, 'mask_slit_computed.fits') return slits_bb
def find_bars(hdulist, bars_nominal_positions, csupos, dtur, average_box_row_size=7, average_box_col_size=21, fit_peak_npoints=3, median_filter_size=5, logger=None): logger.debug('filtering image') # Processed array arr_median = median_filtering(hdulist, median_filter_size) xfac = dtur[0] / EMIR_PIXSCALE yfac = -dtur[1] / EMIR_PIXSCALE vec = [yfac, xfac] logger.debug('DTU shift is %s', vec) # and the table of approx positions of the slits barstab = bars_nominal_positions # Currently, we only use fields 0 and 2 # of the nominal positions file # Number or rows used # These other parameters cab be tuned also bstart = 1 bend = 2047 logger.debug('ignoring columns outside %d - %d', bstart, bend - 1) # extract a region to average # wy = (average_box_row_size // 2) # wx = (average_box_col_size // 2) # logger.debug('extraction window is %d rows, %d cols', 2 * wy + 1, 2 * wx + 1) # Fit the peak with these points # wfit = 2 * (fit_peak_npoints // 2) + 1 # logger.debug('fit with %d points', wfit) # Minimum threshold threshold = 5 * EMIR_RON # Savitsky and Golay (1964) filter to compute the X derivative # scipy >= xx has a savgol_filter function # for compatibility we do it manually allpos = {} slits = numpy.zeros((EMIR_NBARS, 8), dtype='float') logger.info('find peaks in derivative image') for ks in [3, 5, 7, 9]: logger.debug('kernel size is %d', ks) # S and G kernel for derivative kw = ks * (ks * ks - 1) / 12.0 coeffs_are = -numpy.arange((1 - ks) // 2, (ks - 1) // 2 + 1) / kw logger.debug('kernel weights are %s', coeffs_are) logger.debug('derive image in X direction') arr_deriv = convolve1d(arr_median, coeffs_are, axis=-1) # self.save_intermediate_array(arr_deriv, 'deriv_image_k%d.fits' % ks) # Axis 0 is # # logger.debug('derive image in Y direction (with kernel=3)') # arr_deriv_alt = convolve1d(arr_median_alt, ypos3_kernel, axis=0) positions = [] logger.info('using bar parameters') for idx in range(EMIR_NBARS): params_l = barstab[idx] params_r = barstab[idx + EMIR_NBARS] lbarid = int(params_l[0]) # CSUPOS for this bar rbarid = lbarid + EMIR_NBARS current_csupos_l = csupos[lbarid - 1] current_csupos_r = csupos[rbarid - 1] logger.debug('CSUPOS for bar %d is %f', lbarid, current_csupos_l) logger.debug('CSUPOS for bar %d is %f', rbarid, current_csupos_r) ref_y_coor_virt = params_l[1] # Do I need to add vec[1]? ref_x_l_coor_virt = params_l[3] + current_csupos_l * params_l[2] ref_x_r_coor_virt = params_r[3] + current_csupos_r * params_r[2] # Transform to REAL.. ref_x_l_coor, ref_y_l_coor = dist.exvp(ref_x_l_coor_virt, ref_y_coor_virt) ref_x_r_coor, ref_y_r_coor = dist.exvp(ref_x_r_coor_virt, ref_y_coor_virt) # FIXME: check if DTU has to be applied # ref_y_coor = ref_y_coor + vec[1] prow = coor_to_pix_1d(ref_y_l_coor) - 1 fits_row = prow + 1 # FITS pixel index logger.debug('looking for bars with ids %d - %d', lbarid, rbarid) logger.debug('ref Y virtual position is %7.2f', ref_y_coor_virt) logger.debug('ref X virtual positions are %7.2f %7.2f', ref_x_l_coor_virt, ref_x_r_coor_virt) logger.debug('ref X positions are %7.2f %7.2f', ref_x_l_coor, ref_x_r_coor) logger.debug('ref Y positions are %7.2f %7.2f', ref_y_l_coor, ref_y_r_coor) # if ref_y_coor is outlimits, skip this bar # ref_y_coor is in FITS format if (ref_y_l_coor >= 2047 + 16) or (ref_y_l_coor <= 1 - 16): logger.debug('reference y position is outlimits, skipping') positions.append( [lbarid, fits_row, fits_row, fits_row, 1, 1, 0, 3]) positions.append( [rbarid, fits_row, fits_row, fits_row, 1, 1, 0, 3]) continue # minimal width of the slit minwidth = 0.9 if abs(ref_x_l_coor_virt - ref_x_r_coor_virt) < minwidth: # FIXME: if width < minwidth fit peak in image logger.debug('slit is less than %d virt pixels, skipping', minwidth) positions.append( [lbarid, fits_row, fits_row, fits_row, 1, 1, 0, 3]) positions.append( [rbarid, fits_row, fits_row, fits_row, 1, 1, 0, 3]) continue # Left bar # Dont add +1 to virtual pixels logger.debug('measure left border (%d)', lbarid) regionw = 10 bstart1 = coor_to_pix_1d(ref_x_l_coor - regionw) bend1 = coor_to_pix_1d(ref_x_l_coor + regionw) + 1 centery, centery_virt, xpos1, xpos1_virt, fwhm, st = char_bar_peak_l( arr_deriv, prow, bstart1, bend1, threshold) insert1 = [ lbarid, centery + 1, centery_virt, fits_row, xpos1 + 1, xpos1_virt, fwhm, st ] positions.append(insert1) # Right bar # Dont add +1 to virtual pixels logger.debug('measure rigth border (%d)', rbarid) bstart2 = coor_to_pix_1d(ref_x_r_coor - regionw) bend2 = coor_to_pix_1d(ref_x_r_coor + regionw) + 1 centery, centery_virt, xpos2, xpos2_virt, fwhm, st = char_bar_peak_r( arr_deriv, prow, bstart2, bend2, threshold) # This centery/centery_virt should be equal to ref_y_coor_virt insert2 = [ rbarid, centery + 1, centery_virt, fits_row, xpos2 + 1, xpos2_virt, fwhm, st ] positions.append(insert2) # FIXME: hardcoded value y1_virt = ref_y_coor_virt - 16.242 y2_virt = ref_y_coor_virt + 16.242 _, y1 = dist.exvp(xpos1_virt + 1, y1_virt + 1) _, y2 = dist.exvp(xpos2_virt + 1, y2_virt + 1) # Update positions msg = 'bar %d, centroid-y %9.4f centroid-y virt %9.4f, ' \ 'row %d, x-pos %9.4f x-pos virt %9.4f, FWHM %6.3f, status %d' logger.debug(msg, *positions[-2]) logger.debug(msg, *positions[-1]) if ks == 5: slits[lbarid - 1] = numpy.array( [xpos1, y2, xpos2, y2, xpos2, y1, xpos1, y1]) # FITS coordinates slits[lbarid - 1] += 1.0 logger.debug('inserting bars %d-%d into "slits"', lbarid, rbarid) allpos[ks] = numpy.asarray( positions, dtype='float') # GCS doesn't like lists of lists return allpos, slits
def find_bars(hdulist, bars_nominal_positions, csupos, dtur, average_box_row_size=7, average_box_col_size=21, fit_peak_npoints=3, median_filter_size=5, logger=None): logger.debug('filtering image') # Processed array arr_median = median_filtering(hdulist, median_filter_size) xfac = dtur[0] / EMIR_PIXSCALE yfac = -dtur[1] / EMIR_PIXSCALE vec = [yfac, xfac] logger.debug('DTU shift is %s', vec) # and the table of approx positions of the slits barstab = bars_nominal_positions # Currently, we only use fields 0 and 2 # of the nominal positions file # Number or rows used # These other parameters cab be tuned also bstart = 1 bend = 2047 logger.debug('ignoring columns outside %d - %d', bstart, bend - 1) # extract a region to average # wy = (average_box_row_size // 2) # wx = (average_box_col_size // 2) # logger.debug('extraction window is %d rows, %d cols', 2 * wy + 1, 2 * wx + 1) # Fit the peak with these points # wfit = 2 * (fit_peak_npoints // 2) + 1 # logger.debug('fit with %d points', wfit) # Minimum threshold threshold = 5 * EMIR_RON # Savitsky and Golay (1964) filter to compute the X derivative # scipy >= xx has a savgol_filter function # for compatibility we do it manually allpos = {} slits = numpy.zeros((EMIR_NBARS, 8), dtype='float') logger.info('find peaks in derivative image') for ks in [3, 5, 7, 9]: logger.debug('kernel size is %d', ks) # S and G kernel for derivative kw = ks * (ks * ks - 1) / 12.0 coeffs_are = -numpy.arange((1 - ks) // 2, (ks - 1) // 2 + 1) / kw logger.debug('kernel weights are %s', coeffs_are) logger.debug('derive image in X direction') arr_deriv = convolve1d(arr_median, coeffs_are, axis=-1) # self.save_intermediate_array(arr_deriv, 'deriv_image_k%d.fits' % ks) # Axis 0 is # # logger.debug('derive image in Y direction (with kernel=3)') # arr_deriv_alt = convolve1d(arr_median_alt, ypos3_kernel, axis=0) positions = [] logger.info('using bar parameters') for idx in range(EMIR_NBARS): params_l = barstab[idx] params_r = barstab[idx + EMIR_NBARS] lbarid = int(params_l[0]) # CSUPOS for this bar rbarid = lbarid + EMIR_NBARS current_csupos_l = csupos[lbarid - 1] current_csupos_r = csupos[rbarid - 1] logger.debug('CSUPOS for bar %d is %f', lbarid, current_csupos_l) logger.debug('CSUPOS for bar %d is %f', rbarid, current_csupos_r) ref_y_coor_virt = params_l[1] # Do I need to add vec[1]? ref_x_l_coor_virt = params_l[3] + current_csupos_l * params_l[2] ref_x_r_coor_virt = params_r[3] + current_csupos_r * params_r[2] # Transform to REAL.. ref_x_l_coor, ref_y_l_coor = dist.exvp(ref_x_l_coor_virt, ref_y_coor_virt) ref_x_r_coor, ref_y_r_coor = dist.exvp(ref_x_r_coor_virt, ref_y_coor_virt) # FIXME: check if DTU has to be applied # ref_y_coor = ref_y_coor + vec[1] prow = coor_to_pix_1d(ref_y_l_coor) - 1 fits_row = prow + 1 # FITS pixel index logger.debug('looking for bars with ids %d - %d', lbarid, rbarid) logger.debug('ref Y virtual position is %7.2f', ref_y_coor_virt) logger.debug('ref X virtual positions are %7.2f %7.2f', ref_x_l_coor_virt, ref_x_r_coor_virt) logger.debug('ref X positions are %7.2f %7.2f', ref_x_l_coor, ref_x_r_coor) logger.debug('ref Y positions are %7.2f %7.2f', ref_y_l_coor, ref_y_r_coor) # if ref_y_coor is outlimits, skip this bar # ref_y_coor is in FITS format if (ref_y_l_coor >= 2047 + 16) or (ref_y_l_coor <= 1 - 16): logger.debug('reference y position is outlimits, skipping') positions.append([lbarid, fits_row, fits_row, fits_row, 1, 1, 0, 3]) positions.append([rbarid, fits_row, fits_row, fits_row, 1, 1, 0, 3]) continue # minimal width of the slit minwidth = 0.9 if abs(ref_x_l_coor_virt - ref_x_r_coor_virt) < minwidth: # FIXME: if width < minwidth fit peak in image logger.debug('slit is less than %d virt pixels, skipping', minwidth) positions.append([lbarid, fits_row, fits_row, fits_row, 1, 1, 0, 3]) positions.append([rbarid, fits_row, fits_row, fits_row, 1, 1, 0, 3]) continue # Left bar # Dont add +1 to virtual pixels logger.debug('measure left border (%d)', lbarid) regionw = 10 bstart1 = coor_to_pix_1d(ref_x_l_coor - regionw) bend1 = coor_to_pix_1d(ref_x_l_coor + regionw) + 1 centery, centery_virt, xpos1, xpos1_virt, fwhm, st = char_bar_peak_l(arr_deriv, prow, bstart1, bend1, threshold) insert1 = [lbarid, centery + 1, centery_virt, fits_row, xpos1 + 1, xpos1_virt, fwhm, st] positions.append(insert1) # Right bar # Dont add +1 to virtual pixels logger.debug('measure rigth border (%d)', rbarid) bstart2 = coor_to_pix_1d(ref_x_r_coor - regionw) bend2 = coor_to_pix_1d(ref_x_r_coor + regionw) + 1 centery, centery_virt, xpos2, xpos2_virt, fwhm, st = char_bar_peak_r(arr_deriv, prow, bstart2, bend2, threshold) # This centery/centery_virt should be equal to ref_y_coor_virt insert2 = [rbarid, centery + 1, centery_virt, fits_row, xpos2 + 1, xpos2_virt, fwhm, st] positions.append(insert2) # FIXME: hardcoded value y1_virt = ref_y_coor_virt - 16.242 y2_virt = ref_y_coor_virt + 16.242 _, y1 = dist.exvp(xpos1_virt + 1, y1_virt + 1) _, y2 = dist.exvp(xpos2_virt + 1, y2_virt + 1) # Update positions msg = 'bar %d, centroid-y %9.4f centroid-y virt %9.4f, ' \ 'row %d, x-pos %9.4f x-pos virt %9.4f, FWHM %6.3f, status %d' logger.debug(msg, *positions[-2]) logger.debug(msg, *positions[-1]) if ks == 5: slits[lbarid - 1] = numpy.array([xpos1, y2, xpos2, y2, xpos2, y1, xpos1, y1]) # FITS coordinates slits[lbarid - 1] += 1.0 logger.debug('inserting bars %d-%d into "slits"', lbarid, rbarid) allpos[ks] = numpy.asarray(positions, dtype='float') # GCS doesn't like lists of lists return allpos, slits