In [3]:
import os
import sys
import numpy as np
import matplotlib
matplotlib.use('nbagg')
#from matplotlib import style
#style.use('ggplot')
import matplotlib.pyplot as plt

from photutils import Background2D, MedianBackground

import photutils

from scipy import optimize

from astroscrappy import detect_cosmics

from skimage.morphology import reconstruction
from scipy.ndimage.filters import median_filter
from scipy.ndimage import gaussian_filter
from scipy.signal import medfilt

from skimage.filters.rank import median

import astropy.units as u

from astropy.modeling.models import Gaussian2D, Polynomial2D
from astropy.modeling.fitting import LevMarLSQFitter

from astropy.coordinates import SkyCoord, match_coordinates_3d

from astroscrappy import detect_cosmics

%load_ext autoreload
%autoreload 2

from astropy.io import fits
from mmtwfs.wfs import *
from mmtwfs.zernike import ZernikeVector

In [4]:
def implot(im):
    norm = wfs_norm(im)
    plt.imshow(im, norm=norm, origin='lower')
    plt.show()

In [5]:
mmirs = WFSFactory(wfs="mmirs")

In [6]:
mmirs_file = "/Users/tim/MMT/mmtwfs/notebooks/data.fits"
f9_file = "/Users/tim/MMT/wfsdat/20170318/TREY_p500_0000.fits"
ref_file = "/Users/tim/MMT/mmtwfs/mmtwfs/data/ref_images/mmirs_camera1_ref.fits"
#mmirs_file = "/Users/tim/MMT/wfsdat/20170414/rawdata/mmirs_wfs_0275.fits"
data = check_wfsdata(mmirs_file)
#cr_mask, data = detect_cosmics(data, sigclip=4., niter=6, cleantype='medmask')

In [8]:
implot(data)



In [252]:
d = data.copy()
sigma_clip = SigmaClip(sigma=3., iters=10)
bkg_estimator = MedianBackground()
bkg = Background2D(d, (12, 12), filter_size=(5, 5), bkg_estimator=bkg_estimator)
implot(d - bkg.background)



In [258]:
d = data.copy()
im = d - bkg.background
im[:5, :] = 0.0
im[:, :12] = 0.0
s = wfsfind(data, plot=True, fwhm=4.0, threshold=30.)
plt.show()



In [248]:
sf = s[s['flux'] > s['flux'].max()/3.]
sf.show_in_notebook()


Out[248]:
<Table length=142>
idxidxcentroidycentroidsharpnessroundness1roundness2npixskypeakfluxmag
06194.14516098159.852440380.601966396418-0.1068674006830.124262265669169.00.08386.1702231542.366141182-4.06754727284
17243.17503488660.36543997370.596143069148-0.2591907337020.073516614086169.00.08724.0216463944.5794849396-4.1228376161
28294.33991592659.77470824420.586342824824-0.3818675433620.155939134231169.00.07959.976252639.0521324889-3.97911188509
310169.58265818974.81175597950.603531912362-0.004674852324950.102881813247169.00.09660.553026948.314440991-4.21019239755
411218.79545016375.2007482760.630152689567-0.127448575334-0.00572740091703169.00.011798.13282459.3039893757-4.43270977328
512268.76539456775.45879811220.564972437428-0.17403588986-0.00223891779296169.00.010050.321065653.8602602413-4.32817111875
613319.07079459875.91347719090.56061890277-0.2147502650820.0229170470012169.00.09090.7422144147.8754125407-4.20028132323
714144.20997198488.97294453120.5817206362770.5214063404130.13284125444169.00.08274.0145377740.8930726262-4.02912435956
815194.32757683289.7890123590.591567809897-0.226859869489-0.0249270741896169.00.010969.773967656.7567041113-4.38504291958
916243.62784854689.73996809210.610970770052-0.128128656323-0.0701509373492169.00.09738.3497722749.5426441966-4.23744795523
1017294.41867004489.73814345430.59013974129-0.139090152434-0.157159927375169.00.010800.360039756.0210054922-4.37087724871
1118344.66744450991.29620849390.645634772141-0.270550678325-0.110442221373169.00.010148.767486549.2928057259-4.2319588468
1221118.666232345103.8479132920.5199371943720.2953132966330.179040486594169.00.05893.2849741231.428042292-3.74329332223
1322169.611246615104.2045840430.59199358574-0.2139335370820.0316417617393169.00.010865.191175956.0972436628-4.37235380682
1423219.182458255104.3161023770.611055288758-0.211051624218-0.00600358140159169.00.011340.120156857.7012797926-4.40296361437
1524268.988675496104.4112276030.638104512557-0.1414188758530.0146311555616169.00.011376.2579255.9395979239-4.36929835119
1625320.129859991104.4557254760.688868697102-0.253617899919-0.0258538155396169.00.010922.665813350.5587629516-4.25949109822
1726369.125077626105.4506570870.650605389394-0.245158021621-0.00528857849559169.00.010158.204286448.7868791893-4.22075759487
1828194.096452134119.0904726390.579984840902-0.3064651041340.0523852784004169.00.09995.8883905552.071878894-4.29150812119
1929243.847372191119.1976877980.564551094607-0.319750635786-0.108343432779169.00.08879.3797623647.0453607736-4.18129200832
2030294.587089442119.3921100270.687236166979-0.363020995331-0.0901623773329169.00.012220.059209956.926366834-4.38828366767
2131344.870092644118.5748617940.576563169959-0.169566963469-0.108594651576169.00.09551.8356391449.6116116045-4.23895833768
2232145.094022922119.919810460.621990219952-0.1909331463160.0875221618721169.00.011666.886780159.2776979267-4.43222832324
2333394.712225428119.6711577970.584584158039-0.292165215155-0.074044272192169.00.09686.8555406250.0008626431-4.24744374273
2434219.427503906133.4537081440.571050871985-0.338796779016-0.0454440230695169.00.09903.96563951.8639062455-4.2871630588
2535369.861116475133.4607576210.524900641797-0.220737834194-0.0122760571676169.00.09585.9715446953.9397734607-4.32977279537
2636169.319851348133.7392442550.628692530593-0.2638431229830.0636690776686169.00.011482.704439257.1917761934-4.39333396138
2737269.368690343133.7284907970.632490249249-0.344391245876-0.209596308123169.00.010990.768133954.3190733913-4.33738088217
2838319.521907756133.8673259570.597546351784-0.249676744977-0.0842400637466169.00.010991.492409556.8346885255-4.38653371062
2939120.744851618134.8683261910.558383938035-0.1689708462660.0422195818956169.00.011099.400368360.7538399678-4.45893433218
3042144.741999942147.9503237540.698023433883-0.259385507971-0.0545865303277169.00.013907.559855365.2289791621-4.53610145459
3143194.472366571147.9862964690.606039661101-0.341063841375-0.0532856283862169.00.010994.968338756.1855744443-4.37406206368
3244244.361977762147.5794391580.601669647689-0.365671898045-0.112666042006169.00.08631.2462257843.0308115673-4.08444884254
3345295.188677143148.4638671370.643769160052-0.124696076137-0.142677329832169.00.011225.235325854.2784322439-4.33656823796
3446345.121435852147.9875506320.639561561981-0.0464442179045-0.103961070315169.00.012152.359489759.8503438827-4.4426666252
3547395.646473886148.3286726390.584488462096-0.162250476699-0.0549459124059169.00.010623.40139655.7364100697-4.36534748194
364894.2773859576148.8697023910.587477035862-0.0264433252042-0.273036343229169.00.08736.4473734443.3624629547-4.09278485456
3750119.348018904162.9073167580.678952583979-0.2627890889350.0309296760902169.00.013201.251502262.882035777-4.49631648336
3851169.739788174163.0457580660.675485443906-0.1556053044290.102024553399169.00.012830.498653261.5152964511-4.47245780319
3952219.178361341162.6107351060.637311584805-0.265100960521-0.163780748788169.00.010986.017965153.8268840228-4.32749809932
4053270.121376086163.2120143730.577861533687-0.284563716006-0.199397191233169.00.010056.097647452.3157530313-4.2965812023
4154319.349721713162.8742170390.606741220234-0.111375955399-0.164219337043169.00.010952.445139855.3203685125-4.35721266117
4255370.111007249163.0008327490.651728234795-0.07299285511580.0500311464141169.00.011074.202850153.5411358915-4.3217189516
4358294.779560921177.2909642880.535070445789-0.253403296849-0.256737670594169.00.09512.7758282751.4404856966-4.27826265127
445994.8105885076178.394498340.63676718671-0.3917845815820.0758362258269169.00.011816.477702758.3533514286-4.41516451021
4560144.796764727177.6843459920.60422306158-0.1904102346640.133620727616169.00.010236.198829451.9127527226-4.28818514591
4661194.344834573177.7314815810.709067635929-0.299845510195-0.0247052736636169.00.012650.079146557.702899249-4.40299408645
4762244.634200864177.8260835240.618503028304-0.211005944696-0.235419963053169.00.09954.8787610249.1302980941-4.22837349797
4863345.310434325177.811131180.608741327276-0.0876154778483-0.085927740119169.00.010980.879573856.0655459803-4.37174013944
4964396.217253055178.1796032830.578011619105-0.0590500987992-0.0464446200193169.00.010089.860566653.091060056-4.31255349219
5068270.590918912191.0739932790.551141998572-0.35347867222-0.192083312409169.00.05845.5022049630.5936146618-3.7140769807
5169219.577176115192.0342948650.592566702423-0.271510351647-0.275982584997169.00.09941.4271589750.6693539697-4.26186341776
5270320.074489633191.9636180250.5752381443110.0501640303495-0.142512538941169.00.010200.627859552.8904121929-4.30844237909
5371370.664008071191.9616085110.5957328378540.0566062119489-0.0673813030015169.00.010578.235687854.6569592673-4.34411366727
5472119.432068574192.6569789760.707835464581-0.30433890914-0.0397050189855169.00.012712.080047958.3009384927-4.41418886455
5573169.821525272192.6544553490.672418158166-0.1909189185680.0429138861979169.00.012636.133845160.5689659572-4.45562539846
5677296.07810316205.0543802520.551197757802-0.178617778532-0.119074535487169.00.04886.3304291225.280006109-3.5069429364
5778194.127271611206.3943538350.582361571517-0.238512374563-0.11858621871169.00.010995.893615357.7836936049-4.40451324758
5879344.820293578206.3324404930.543164236382-0.144988954409-0.178430620034169.00.09936.1976649353.8514407248-4.32799331692
598094.1754855428206.9297605440.661170293759-0.261206407807-0.043240198139169.00.012736.921579462.4201669181-4.4883123142
6081145.1780924207.0483824740.700754288612-0.17762869729-0.00560127789497169.00.013147.516699461.5149070797-4.47245093082
6182396.361990524206.6200403950.601447159308-0.187192300053-0.148269841194169.00.010971.782878656.4935543223-4.37999724867
6285119.842220284221.0503268350.669481869921-0.1174079211660.0435921910003169.00.012700.695643760.8921939641-4.46140405524
6386169.896886309221.4485827590.652943600362-0.1677749797810.0304952234029169.00.012704.203854562.0438806506-4.48174738438
6487320.934940268220.676536680.637801062163-0.0329441457936-0.134975233184169.00.012279.095667160.3837352716-4.45229993625
6588371.57187164221.2700380460.587205704941-0.135918824835-0.124756884838169.00.010488.454590554.1754656696-4.33450663255
6692345.615366231235.2379783060.58385565431-0.105586053498-0.304260853952169.00.09439.4281752349.3201992908-4.23256205701
6795145.429966966236.2279835770.675693805783-0.0791687571420.146887161146169.00.011769.798731356.1563323899-4.37349683991
6896193.924292998236.0978739250.615660861478-0.0732916168687-0.0337452866756169.00.010675.307946153.7877917249-4.32670928637
6997396.783196759235.5297944340.624557837332-0.0349048576085-0.163632420453169.00.09541.6461231148.24821585-4.20870314597
709993.4156721689237.0033774760.659018856622-0.281763243870.0473142124568169.00.011569.762497155.6832206126-4.36431086526
71103320.778902976250.2979941740.665197200611-0.07157217255980.0242110845598169.00.07676.8475169835.3568301759-3.87118330638
72104371.390745944249.9963378220.663841122297-0.162297376677-0.0558154664248169.00.07563.924664335.0991644576-3.86324194531
73105120.002652562252.3588537230.608929718594-0.2070686497240.201727693873169.00.07321.8093917735.8359710977-3.88579794413
74106170.045827096251.4628939410.614344369475-0.05564501886030.243694890713169.00.06064.0987403128.6967523895-3.64458187606
75113194.091840868265.1580137710.654876430358-0.0433160295319-0.0393026078151169.00.012508.929685261.0330540181-4.46391275514
76114346.275798601264.8314894280.616900059768-0.0577586131346-0.215523974102169.00.011920.586187660.488105181-4.45417495059
77115396.993619313264.7015562760.639039493226-0.195408775258-0.223096680269169.00.012277.543055661.1705085752-4.46635522816
78116145.254848342266.1640272290.689582567839-0.1633230982740.0666050267642169.00.012595.364297659.5177435911-4.4366161452
7911793.9898243919266.6542431480.591968146697-0.239570871992-0.012372922464169.00.010048.59289951.379816197-4.27698136579
80120371.894221655279.0710147210.618385971849-0.198439775848-0.155197245066169.00.011946.337679760.7849653523-4.45949043371
81121169.759953752279.7485976870.643768825471-0.33306567646-0.120816211938169.00.011747.993145457.6446041366-4.40189665274
82122320.111666016279.5773323430.639356020194-0.231513183412-0.0999239704801169.00.011566.868882256.3380979728-4.37700545177
83124119.41614146280.7447145120.68537320658-0.3504233527280.0115381017885169.00.012164.976219356.8416354492-4.38666641242
84129194.446181635293.4742142280.563270683007-0.0514083514894-0.18567482051169.00.09749.6583472251.2221445549-4.27364439359
85130397.395809651293.2718387050.651983966531-0.00610500237678-0.230702998298169.00.011649.33319156.6661506844-4.38330928036
86131296.604237702294.3909306190.578535229557-0.0565224678804-0.0190948127306169.00.06823.9012881734.5365610919-3.84569772848
87132346.40965301293.9842337820.62162316938-0.0942178534657-0.144380796872169.00.011463.479393157.1736857803-4.39299047627
88133144.980007689294.8007575620.633557542908-0.4512020149760.0543469308595169.00.011087.293823354.5880399878-4.34274375254
8913494.6409326171295.8485982150.650233850008-0.299750795064-0.0220856223012169.00.011739.634994956.9948006883-4.38958809816
90138221.186937307308.4089746350.7272583155960.113130343241-0.132084215956169.00.011561.309358752.2073717824-4.29432957639
91139320.231885148308.3002431520.611057402257-0.105413159095-0.0743724304695169.00.011679.856897759.7532622807-4.44090405237
92140371.410698673307.8018584430.637677996451-0.111765412166-0.216440555171169.00.012018.433983759.8204997261-4.44212509176
93141168.751287647308.659262060.519339121802-0.422490917115-0.251627222738169.00.08881.3016175248.4684886262-4.21364869375
94142270.799443416309.0884124790.634691532559-0.062361121469-0.194902066725169.00.09878.0383329949.0634309762-4.22689478721
95144119.029534623309.8856507260.665373908106-0.267608778351-0.0100013427015169.00.012267.255877459.0361205994-4.42779452813
96145245.767650756322.3572285490.592566919168-0.342257100717-0.217412936878169.00.010721.749592255.3576493153-4.35794410034
97146346.183960579322.1530884550.688934430246-0.178905586908-0.145199959747169.00.013278.315596862.5718990826-4.49094834048
98147398.398427965322.4117153540.642333268476-0.0809210705128-0.221142589657169.00.012339.763946860.7688041878-4.4592017259
99148194.476149681323.2440027220.576690954215-0.416187498477-0.322628172637169.00.09893.5015886551.0704931007-4.27042512779
100149295.588932488322.7816114970.656297970828-0.234791906227-0.216784063149169.00.012348.734367159.6088406991-4.43827668887
10115094.821653334323.8049772960.610886394259-0.37048125192-0.181651090329169.00.09061.5528540544.4602483891-4.11992971168
102151144.45773434323.6852085530.646518318782-0.33850501385-0.125380139555169.00.011427.228928655.7944036432-4.3664766
103152271.358268796335.9598093680.59453025497-0.331273221606-0.290834655745169.00.010331.852056153.2704869301-4.31621666641
104153220.545661904337.1340188230.602074598666-0.565934502054-0.287972872051169.00.09549.0246990247.1530853315-4.18377528715
105154320.62179552336.6702557720.659039607256-0.129687171406-0.23474262164169.00.013178.852333363.990832502-4.51529440062
106155372.060792963337.2591924530.6393839028080.112341922269-0.0844763382804169.00.011376.087328855.7415221344-4.36544705956
107156169.071361997337.7735220860.58498625479-0.265350307367-0.332725081313169.00.010300.480023852.4922607473-4.30023819363
108158119.136262962338.5308463590.624715714661-0.276340217270.00560722754876169.00.010899.199246454.0732502447-4.33245618753
109159296.296750535350.0758929860.65966091595-0.173352204086-0.216574256648169.00.012891.50244362.2478884206-4.48531155959
110160245.602732901350.7784280040.578714950002-0.291074125318-0.283402684408169.00.010306.259864553.1357338396-4.31346670712
111161346.348166964351.3794117760.5929222980890.20913757786-0.148217180939169.00.010567.903899553.6631812955-4.32419103806
112162396.863733614350.8136076340.65919062891-0.115865948427-0.222602578757169.00.012543.347321759.9802573911-4.44502081275
113163194.839620382352.1836657660.601129574002-0.451533941531-0.207288414418169.00.010891.548686855.2435247726-4.35570345158
114164143.934806962352.5980654050.590280511359-0.256963384392-0.161311436305169.00.09991.676359650.6354178648-4.26113599578
11516593.4232254315353.656351810.550480393305-0.710328210241-0.217399972881169.00.07255.8448803137.3293349272-3.93012563112
116167271.251454052365.1146144220.562571122159-0.120610403803-0.202439762042169.00.08442.5216379844.3966517146-4.11837554484
117168321.366448286364.7620850890.580395413018-0.182515047278-0.184019764248169.00.010858.45239756.279472248-4.37587504063
118169219.895844536365.7681862590.640023497132-0.202996785219-0.263546828849169.00.011298.52517354.6852813722-4.34467612758
119170371.722739822365.8180104280.62202609518-0.226366056741-0.0172317563106169.00.011953.027627760.3210641068-4.45117248549
120171119.39169658367.1723049070.638363674606-0.418620431931-0.0776396030274169.00.010030.754848448.300329136-4.2098752255
121172169.410580627367.0067036430.551301239921-0.448396743278-0.314201930198169.00.09240.9375957748.3383241802-4.21072897493
122174296.359928819379.8058779820.601819402779-0.162429564623-0.0821003974935169.00.012372.450131263.8499894208-4.51290207411
123175347.937212787380.4282621050.603749963119-0.151583113146-0.02645244478169.00.010943.906056354.851433154-4.34796994818
124176397.383302188380.4977504290.591224155390.0158840599304-0.0897212615402169.00.010063.325120151.4285062035-4.27800977488
125177194.135432506380.9513085710.524864335695-0.357722249051-0.251588733029169.00.09239.0742965450.7089755193-4.26271209131
126178245.234844175380.6781836180.584161526405-0.143686675792-0.14249504731169.00.010048.714609751.5436258618-4.28043741487
127179144.287268703381.6031288730.635124610891-0.323885395755-0.0879522132868169.00.010527.168753750.9047643762-4.2668960789
128181371.869181178394.4824043810.586535723842-0.2196118422870.0615582045668169.00.010462.864053753.4349537286-4.31956359414
129182271.297415311395.020804360.534592681131-0.4554296066-0.123425951887169.00.07336.9878758538.8296368118-3.97290832111
130183168.959772605395.9452694850.542395533905-0.316148923722-0.272993647927169.00.010357.488022555.7817846273-4.36623101135
131184219.924483651396.078161340.572458679235-0.308170621468-0.250267664557169.00.010105.262333752.1472354786-4.29307822447
132185321.946501986396.2000246420.606599210478-0.140654499095-0.0834019725234169.00.011666.711826759.359973847-4.43373425232
133187346.976826864409.9683096030.714045880237-0.05982114967080.0376098898234169.00.013521.093757260.7062955651-4.45808433013
134188194.676185124410.6204014310.53881139184-0.24683374996-0.187448092019169.00.09714.7080600552.5774170384-4.30199811696
135189245.340545928410.5943558660.569511775476-0.248608508497-0.168364427119169.00.011034.182244759.3761344613-4.43402980124
136190297.496277899411.0086383220.580135443305-0.240408675381-0.15843115069169.00.011197.434003858.898406732-4.42525886698
137191143.659821578413.2290496910.540579385389-0.4516959848770.0152485354782169.00.05992.1017123531.5275874637-3.74672684795
138193321.757673455424.2116783140.61140351652-0.235136646891-0.07898515651169.00.09758.68060748.9471202029-4.22431786309
139194220.09097707425.2820730050.599936490478-0.335917014756-0.288265013515169.00.09266.9344858847.3701472982-4.18876183794
140195271.811271764424.788595120.623452842547-0.15539445808-0.318104549379169.00.010210.119751550.8251883107-4.26519749101
141197167.587695316428.0654853110.492261399591-0.384364868756-0.213296534272169.00.05625.4060698930.7730804852-3.72042743177

In [178]:
xspacing, yspacing = grid_spacing(im)

In [15]:
masked_data = data.copy()
masked_data[masked_data < 300.] = 0.0
xcen, ycen, plot = center_pupil(masked_data, mmirs.telescope.pupil_mask(), plot=True)
plt.show()



In [16]:
xcen, ycen


Out[16]:
(211.0973735744067, 241.62204732849847)

In [259]:
apers = photutils.CircularAperture((s['xcentroid'], s['ycentroid']), r=(xspacing+yspacing)/4.)

In [260]:
masks = apers.to_mask(method='subpixel')

In [261]:
spot = np.zeros(masks[0].shape)

In [262]:
for m in masks:
    subim = m.cutout(data)
    try:
        spot += subim
    except:
        pass

In [263]:
implot(spot)



In [264]:
g = Gaussian2D(amplitude=spot.max(), x_mean=spot.shape[1]/2, y_mean=spot.shape[0]/2) + Polynomial2D(degree=0)
fit_g = LevMarLSQFitter()
y, x = np.mgrid[:spot.shape[0], :spot.shape[1]]

In [265]:
pars = fit_g(g, x, y, spot)

In [266]:
pars.y_stddev_0.value


Out[266]:
0.81893592801860204

In [189]:
pars


Out[189]:
<CompoundModel5(amplitude_0=46685.329878975026, x_mean_0=11.023890156088521, y_mean_0=11.032180846448338, x_stddev_0=2.3442806133454117, y_stddev_0=2.415009077439269, theta_0=280762209584.1609, c0_0_1=-231.22327058502037)>

In [190]:
ref = mmirs.modes['mmirs2']['reference']

In [191]:
refx = (xspacing/ref['xspacing'])*ref['apertures']['xcentroid']+305 #xcen
refy = (yspacing/ref['yspacing'])*ref['apertures']['ycentroid']+247 #ycen
ref_apers = photutils.CircularAperture(
    (refx, refy),
    r=(xspacing+yspacing)/4.,
)

In [192]:
ref_apers.plot()
#apers.plot()
implot(im)
plt.show()



In [193]:
src_coord = SkyCoord(x=s['xcentroid'], y=s['ycentroid'], z=0.0, representation='cartesian')
ref_coord = SkyCoord(x=refx, y=refy, z=0.0, representation='cartesian')

In [194]:
idx, sep, dist = match_coordinates_3d(src_coord, ref_coord)

In [195]:
src_coord.separation(ref_coord[idx]).std().to(u.deg)


Out[195]:
$0^\circ44{}^\prime06.3544{}^{\prime\prime}$

In [196]:
np.mean(abs(s['xcentroid'] - refx[idx]))


Out[196]:
5.9861596492506424

In [197]:
np.mean(s['xcentroid'] - refx[idx])


Out[197]:
4.4899420357542441

In [198]:
def fit_wfs_reference(pars, ref, spots):
    xc = pars[0]
    yc = pars[1]
    model = SkyCoord(
        representation='cartesian',
        x=ref['xcentroid'] + xc,
        y=ref['ycentroid'] + yc,
        z=0.0
    )
    measured = SkyCoord(
        representation='cartesian',
        x=spots['xcentroid'],
        y=spots['ycentroid'],
        z=0.0
    )
    idx, ang, dist = match_coordinates_3d(measured, model)
    stat = measured.separation(model[idx]).std().value
    return stat

In [199]:
scale_ref = {
    'xcentroid': (xspacing/ref['xspacing'])*ref['apertures']['xcentroid'],
    'ycentroid': (yspacing/ref['yspacing'])*ref['apertures']['ycentroid']
}
pars = [xcen, ycen]
xopt = optimize.fmin_powell(fit_wfs_reference, pars, xtol=1e-4, args=(scale_ref, s))
xopt, xcen, ycen


Optimization terminated successfully.
         Current function value: 0.426127
         Iterations: 4
         Function evaluations: 123
Out[199]:
(array([ 291.49643898,  241.49967025]), 298.87446519913766, 240.71421449526792)

In [200]:
refx_match = refx[idx]
refy_match = refy[idx]
xoff = np.mean(s['xcentroid'] - refx_match)
yoff = np.mean(s['ycentroid'] - refy_match)
ref_coord = SkyCoord(x=refx+xoff, y=refy+yoff, z=0.0, representation='cartesian')
idx, sep, dist = match_coordinates_3d(src_coord, ref_coord)
xoff = np.mean(s['xcentroid'] - refx[idx])
yoff = np.mean(s['ycentroid'] - refy[idx])

In [201]:
dist


Out[201]:
$[22.988103,~22.571043,~20.059587,~\dots, 0.37599438,~0.67268956,~0.41111856] \; \mathrm{}$

In [202]:
ref_apers = photutils.CircularAperture(
    (refx[idx]+xoff, refy[idx]+yoff),
    r=(xspacing+yspacing)/4.,
)
plt.imshow(im, origin='lower')
ref_apers.plot()
#apers.plot()
plt.show()



In [203]:
len(ref['apertures'])


Out[203]:
162

In [204]:
ref_im = fits.open("/Users/tim/MMT/mmtwfs/mmtwfs/data/ref_images/mmirs_camera2_ref.fits")[0].data

In [221]:
ref_xsum = np.sum(ref_im, axis=0)
ref_xsum /= ref_xsum.max()
ref_ysum = np.sum(ref_im, axis=1)
ref_ysum /= ref_ysum.max()
xl = int(xcen - 2*xspacing)
xu = int(xcen + 2*xspacing)
yl = int(ycen - 2*yspacing)
yu = int(ycen + 2*yspacing)
xsum = np.sum(im[yl:yu, :], axis=0)
xsum /= xsum.max()
xtot = np.sum(im, axis=0)
xtot /= xtot.max()
ysum = np.sum(im[:, xl:xu], axis=1)
ysum /= ysum.max()
ytot = np.sum(im, axis=1)
ytot /= ytot.max()
xdiff = xtot - xsum
xdiff[xdiff < 0.25] = 0.0
ydiff = ytot - ysum
ydiff[ydiff < 0.25] = 0.0

In [222]:
#plt.plot(ysum)
plt.plot(xdiff)
plt.show()



In [207]:
ndimage.measurements.center_of_mass(xdiff)


Out[207]:
(299.63836185646653,)

In [137]:
xcen, ycen


Out[137]:
(295.124388162137, 249.40394388393889)

In [223]:
detect_cosmics?

In [ ]: