|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +""" |
| 4 | +Plot median of columns above/below amp boundaries |
| 5 | +""" |
| 6 | + |
| 7 | +import os, sys |
| 8 | +import argparse |
| 9 | +import numpy as np |
| 10 | +import fitsio |
| 11 | + |
| 12 | +import matplotlib.pyplot as plt |
| 13 | + |
| 14 | +p = argparse.ArgumentParser() |
| 15 | +p.add_argument('-n', '--night', type=int,help='YEARMMDD night') |
| 16 | +p.add_argument('-e', '--expid', type=int, help='Exposure ID') |
| 17 | +p.add_argument('-c', '--camera', type=str, help='Camera') |
| 18 | +p.add_argument('-i', '--input', help='input preproc file') |
| 19 | +p.add_argument('--nrow', type=int, default=41, |
| 20 | + help='number of rows to include in median in each amp') |
| 21 | +p.add_argument('--xminmax', nargs=2, type=int, default=(0, 0), |
| 22 | + help='x (column) range to plot') |
| 23 | +p.add_argument('--debias',action='store_true') |
| 24 | +p.add_argument('--below-in-front',action='store_true') |
| 25 | +#p.add_argument('--psf',type=str,default=None,required=False,help='use this psf to fold the profiles') |
| 26 | +p.add_argument('-r','--reference',type=str,default=None,required=False,help='reference preproc image to compare with (for 2 amp mode)') |
| 27 | + |
| 28 | +args = p.parse_args() |
| 29 | + |
| 30 | +n = args.nrow |
| 31 | +xmin, xmax = args.xminmax |
| 32 | + |
| 33 | +if args.input is None: |
| 34 | + from desispec.io import findfile |
| 35 | + args.input = findfile('preproc', night=args.night, expid=args.expid, |
| 36 | + camera=args.camera) |
| 37 | + |
| 38 | +img = fitsio.read(args.input, 'IMAGE') |
| 39 | + |
| 40 | +ny, nx = img.shape |
| 41 | + |
| 42 | +if xmax == 0 : |
| 43 | + #xmin = nx//2 -300 |
| 44 | + #xmax = nx//2 + 300 |
| 45 | + xmin = 0 |
| 46 | + xmax = nx |
| 47 | +above = np.mean(img[ny//2:ny//2+n, xmin:xmax], axis=0) |
| 48 | +below = np.mean(img[ny//2-n:ny//2, xmin:xmax], axis=0) |
| 49 | + |
| 50 | +if args.debias : |
| 51 | + margin=100 |
| 52 | + bias1 = np.median(img[ny//2:ny//2+n,0:margin]) |
| 53 | + bias2 = np.median(img[ny//2:ny//2+n,nx-margin:nx]) |
| 54 | + print("above bias = {} {}".format(bias1,bias2)) |
| 55 | + above[:nx//2-xmin] -= bias1 |
| 56 | + above[nx//2-xmin:] -= bias2 |
| 57 | + bias1 = np.median(img[ny//2-n:ny//2,0:margin]) |
| 58 | + bias2 = np.median(img[ny//2-n:ny//2,nx-margin:nx]) |
| 59 | + print("below bias = {} {}".format(bias1,bias2)) |
| 60 | + below[:nx//2-xmin] -= bias1 |
| 61 | + below[nx//2-xmin:] -= bias2 |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +xx = np.arange(xmin, xmax) |
| 69 | + |
| 70 | +title=os.path.basename(args.input).split(".")[0] |
| 71 | +plt.figure(title) |
| 72 | +plt.subplot(211) |
| 73 | +plt.title(os.path.basename(args.input)) |
| 74 | +extent = [xmin-0.5, xmax-0.5, ny//2-n-0.5, ny//2+n-0.5] |
| 75 | +plt.imshow(img[ny//2-n:ny//2+n, xmin:xmax], vmin=-5, vmax=80, extent=extent,aspect='auto') |
| 76 | +plt.axhline(ny//2,linestyle="--",color="white") |
| 77 | +plt.subplot(212) |
| 78 | +if args.below_in_front : |
| 79 | + plt.plot(xx, above, label='above',alpha=0.6) |
| 80 | + plt.plot(xx, below, label='below',alpha=0.6) |
| 81 | +else : |
| 82 | + plt.plot(xx, below, label='below',alpha=0.6) |
| 83 | + plt.plot(xx, above, label='above',alpha=0.6) |
| 84 | +plt.axvline(nx//2,linestyle="--",color="k") |
| 85 | +plt.legend(loc="upper left") |
| 86 | +plt.title(f'median of {n} rows above/below CCD amp boundary') |
| 87 | +plt.ylim(-5,min(80,max(np.max(above),np.max(below)))) |
| 88 | +plt.xlim(xmin, xmax) |
| 89 | +plt.xlabel('CCD column') |
| 90 | +plt.grid(True) |
| 91 | + |
| 92 | +if args.reference is not None : |
| 93 | + title="{}-vs-{}".format(os.path.basename(args.input).split(".")[0],os.path.basename(args.reference).split(".")[0]) |
| 94 | + plt.figure(title) |
| 95 | + refimg = fitsio.read(args.reference, 'IMAGE') |
| 96 | + prof = np.median(img[ny//2-n:ny//2+n, xmin:xmax], axis=0) |
| 97 | + refprof = np.median(refimg[ny//2-n:ny//2+n, xmin:xmax], axis=0) |
| 98 | + scale = np.sum(prof*refprof)/np.sum(refprof**2) |
| 99 | + ok=(refprof>10) |
| 100 | + scale = np.median(prof[ok]/refprof[ok]) |
| 101 | + refprof *= scale |
| 102 | + plt.plot(xx, refprof, label='{:0.3f} x {}'.format(scale,os.path.basename(args.reference)),alpha=0.6) |
| 103 | + plt.plot(xx, prof, label=os.path.basename(args.input),alpha=0.6) |
| 104 | + plt.title(f'median of {2*n} rows in the center of the CCD') |
| 105 | + plt.ylim(-5,min(80,max(np.max(above),np.max(below)))) |
| 106 | + plt.xlim(xmin, xmax) |
| 107 | + plt.xlabel('CCD column') |
| 108 | + plt.legend(loc="upper left") |
| 109 | + plt.grid(True) |
| 110 | + |
| 111 | +plt.figure() |
| 112 | +# folding |
| 113 | + |
| 114 | + |
| 115 | +plt.tight_layout() |
| 116 | +plt.show() |
0 commit comments