-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbdc_main.py
More file actions
1562 lines (1211 loc) · 62.9 KB
/
bdc_main.py
File metadata and controls
1562 lines (1211 loc) · 62.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#this is the main script that calculates the distortion.
"""
Rough outline for code: follow the method in distortion_iterating.py.
But tidier.
Inputs:
Image starlists: We need to run starfinder on the images to get starlists. That could be included as a step here, or it could be a separate piece of code.
Either way, this script should take in the starlists.
Reference starlist: This will probably be the PCU grid, but could also be Hubble/Gaia catalogues. Make that an option.
Other target info? M15, M92, PCU grid
Initial transformation guesses: Very important for using the PCU. Maybe also important for Hubble/Gaia. I did end up using initial guesses. Might be hard to automate.
Several nights: The data may come in separate chunks, like with my observations being spread over several nights. Each night may have different parameters.
What should the name for these chunks be? I think nights is good, lines up well with observed nights. Maybe 'runs'? Or epochs? Epochs is broader.
Input parameters: May vary with different nights. Make a 'night' object that contains the parameters.
Create stacks: Combine images with the same PA into stacks with error measurements. Might need some intelligence here to pick the correct stacks.
Projecting velocity. Centring the solution. I think I'll stick with the 5th order polynomial, but it must be able to vary.
Maybe include functions for f_tests etc to compare.
For each night, have a text file with the parameters in it. Maybe lists of starlists too?
This may be used for Nirc2 as well, so don't hard code OSIRIS images.
I definitely need flystar.
Do I need KAI? instrument object do not include pixel dimensions.
I do need KAI for DAR and instruments.
Include some plotting functions.
run from the command line with an input script (config file), like maos. And an output folder.
Input script will contain links to nights.
"""
import argparse
import yaml
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--config", required = True, help = "Configuration file")
parser.add_argument("-o", "--output", required = True, help = "Output folder")
args = parser.parse_args()
print("Config file is: {}".format(args.config))
print("Output folder is : {}".format(args.output))
with open(args.config, "r") as ymlfile:
config = yaml.safe_load(ymlfile)
# for section in config:
# print(section)
# print('----')
# # print(cfg)
# print(config["reference"])
# print(config["n1"]['bad_files'])
# print(config["n1"]['bad_files'][0])
# for night in config['nights']:
# print(config[night]['target'])
def main():
hubbleData = fetch_hubble(config[night]['hubble_file'])
if config['generate_reference_frame']:
median_residuals_b = []
mean_residuals_b = []
mean_residuals_b_squared = []
min_residuals_b = []
max_residuals_b = []
num_residuals_b = []
median_residuals_b_radec = []
mean_residuals_a = []
refTable_current = {} #holds most recent refTable for each night
#this is the reference table that keeps getting updated by the reference section.
refTable_current_filename = '{}refTable_current_{}'.format(resultDir,solution_year)
#make these into a dictionary of lists.
if config['instrument'] == 'OSIRIS'
osiris = instruments.OSIRIS()
for ref_iteration in range(config['ref_iterations']):
print('\n \n Ref Iteration {} \n'.format(ref_iteration))
distortion_section()
#section B
reference_section()
#section A
#final result here.
else:
distortion_section()
#final result here.
def distortion_section(initial_distortion_correction = None):
# --------------------------------------- Section B ---------------------------------------------------------------
#fp_iteration starts here?
#get the distortion solution, correct the data, pass that back into the fitter.
#Do I want to save all fp_iterations, or just save the final fit? I think I want all iterations to be saved. So I can quickly re-run and plot them.
current_distortion_correction = initial_distortion_correction
tab1_initial = {'n1':[],'n2':[],'n3':[],'n4':[],'n5':[]} #is regenerated each ref_iteration
# do I need the empty lists here in order to append to them?
#if I am loading dist files then this can break if a set of fp_iterations was incomplete - it won't run the first iteration to generate it. Need to save as a file?
for fp_iteration in fp_iterations:
matched_star_table_name = '{}dist_measures_{}_{}_{}.txt'.format(resultDir,solution_year,ref_iteration,fp_iteration)
# if we want to generate a new matched_star_table:
if config['generate_new_fp']:
obs_nights = [night for night in config]
#-------------------------Night loop starts here-------------------------------
for night in obs_nights:
osiris_filenames = get_osiris_files(config[night]['stackDir'])
sl2 = slice(config[night]['slice'][0],config[night]['slice'][1])
osiris_filenames = osiris_filenames[sl2]
if night not in refTable_current.keys(): #if we have not generated a new reference frame, load the starting one.
if config['use_flystar_velocity']:
refTable_H = prepare_hubble_for_flystar(hubbleData,config[night]['ra_field'],config[night]['dec_field'],config[night]['target'])
else:
starlist0 = load_osiris_file(config[night]['stackDir'] ,osiris_filenames[0])
hubbleData_p = project_pos(hubbleData,starlist0,'hubble_'+config[night]['target'])
refTable_H = prepare_hubble_for_flystar(hubbleData_p,config[night]['ra_field'],config[night]['dec_field'],config[night]['target'])
refTable_current[night] = refTable_H.filled()
with open(refTable_current_filename, 'wb') as temp:
pickle.dump(refTable_current, temp)
for i, filename in enumerate(osiris_filenames):
if filename in config[night]['bad_files']:
print('{} {} flagged as bad, skipping'.format(i,filename))
else:
starlist = load_and_prepare_data(filename,refTable_current)
try:
trans_list = load_transformation_guess()
starlist_corrected = starlist[:]
# if current_distortion_correction is not None:
if fp_iteration > 0:
xt, yt = current_distortion_correction.evaluate(starlist_corrected['x'],starlist_corrected['y'])
starlist_corrected['x'] = xt
starlist_corrected['y'] = yt
if config[night]['single_fit']:
refTable_d_f = refTable_d #.filled(). not filling.
msc = align.MosaicToRef(
refTable_d_f, [starlist_corrected], iters=3,
dr_tol=config[night]['rad_tolerance'], dm_tol=config[night]['mag_tolerance'],
outlier_tol=[None, None, None],
# trans_class=transforms.PolyTransform,
trans_input=trans_list,
trans_class=transforms.four_paramNW,
trans_args=[{'order': 1}, {'order': 1}, {'order': 1}],
use_vel=False,
use_ref_new=False,
update_ref_orig=False,
mag_trans=True,
mag_lim=config[night]['mag_limits'], #[6,16],
weights='both,std',
calc_trans_inverse=True,
init_guess_mode='miracle', verbose=0)
msc.fit()
# tab1 = msc.ref_table
tform = msc.trans_list
tform_inv = msc.trans_list_inverse
#I only want to save the transformation from the subsequent iterations, not tab1.
if fp_iteration == 0:
tab1_initial[night].append(msc.ref_table)
tab1 = tab1_initial[night][i]
j = 0
else:
j=i # i=osiris index, j=gaia index
ref_idx = np.where(tab1['ref_orig'] == True)[j]
if len(ref_idx) <= 10:
print(i, filename, 'Only', len(ref_idx),'matched, skipping')
errorlist.append(filename[:-4])
continue
with open(tform_file_1, 'wb') as temp:
pickle.dump(tform, temp)
print(i, filename, len(refTable_d), 'Reference stars,', len(starlist_corrected), 'OSIRIS stars,', len(ref_idx), 'matches')
# ids1 = np.where(tab1['name_in_list'] == 's1')[0]
Ox = tab1['x_orig'][ref_idx,j]
Oy = tab1['y_orig'][ref_idx,j]
GRa = tab1['x0'][ref_idx]
GDec = tab1['y0'][ref_idx]
# ids = gaiaData['source_id'][ref_idx] #ID's of gaia sources
# ids = refTable_d['source_id'][ref_idx] #ID's of gaia sources
# ids = refTable_d['name'][ref_idx] #ID's of stars? I lose the catalogue names in MosaicToRef, so can't use this.
ids = tab1['name'][ref_idx] #ID's of stars?
px = tform_inv[j].px
py = tform_inv[j].py
theta = math.atan2(px[2],px[1])
scale = math.cos(theta) / px[1]
used_files.append(filename)
scales.append(scale)
rotations.append(math.degrees(theta))
PAs.append(PA)
if not recalculate_plate_scale:
# for k, x in enumerate(Gx):
# Gx[k], Gy[k] = tform_inv[j].evaluate(Gx[k],Gy[k]) #transform Hubble reference into pixels with 4-param transform.
Gx, Gy = tform_inv[j].evaluate(GRa,GDec) #should work fine? Don't need the loop above?
ORa, ODec = tform[j].evaluate(Ox,Oy)
w = tab1['w'][ref_idx]
print('Number of times weight=0.0:', (w==0.0).sum())
# print(np.median(w))
x_O.extend(Ox)
y_O.extend(Oy)
x_G.extend(Gx)
y_G.extend(Gy)
weights.extend(w)
idnums.extend(ids)
starlist_num.extend([i] * len(ids))
night_col.extend([night] * len(ids))
xe_O.extend(tab1['xe_orig'][ref_idx,j])
ye_O.extend(tab1['ye_orig'][ref_idx,j])
xe_G.extend(tab1['x0e'][ref_idx])
ye_G.extend(tab1['y0e'][ref_idx])
used_in_trans_1.extend(tab1['use_in_trans'][ref_idx])
Ra_O.extend(ORa)
Dec_O.extend(ODec)
Ra_G.extend(GRa)
Dec_G.extend(GDec)
plot_image(Gx,Gy,Ox,Oy,fitsfile,plotDir_n + 'img/'+ str(ref_iteration) + '/',tab1['use_in_trans'][ref_idx])
plot_image_dots(Gx,Gy,Ox,Oy,fitsfile,plotDir_n + 'img_d/'+ str(ref_iteration) + '/',tab1['use_in_trans'][ref_idx])
plot_quiver(Ox,Oy,Gx,Gy,filename[:-4],plotDir_n + 'quiver/'+ str(ref_iteration) + '/',tab1['use_in_trans'][ref_idx])
if show_plots:
plt.show()
# plot_quiver(tab1['x0'][ref_idx],tab1['y0'][ref_idx],tab1['x'][ref_idx,0],tab1['y'][ref_idx,0],filename[:-4])
except AssertionError as err:
print(filename[:-4], 'error:')
print(err)
errorlist.append(filename[:-4])
continue
except ValueError as err:
print(filename[:-4], 'error:')
print(err)
errorlist.append(filename[:-4])
continue
successlist.append(filename[:-4])
# plt.close('all')
# quit()
#--------------------------------------------Night loop finishes here----------------------------------------------
print('--------------------')
print('Succeeded for', len(successlist), 'files.')
print('Failed for', len(errorlist), 'files:')
print(errorlist)
print('Mean scale =', np.mean(scales))
offset = np.array(rotations)-np.array(PAs)
print('Mean rotation offset =', np.mean(offset))
if recalculate_plate_scale:
transform_table = Table([used_files, scales, rotations, PAs, offset], names = ('File','Scale', 'Rotation','PA','Difference'))
ascii.write(transform_table,'{}t_params_{}_{}.txt'.format(resultDir,ref_iteration,night),format='fixed_width', overwrite=True)
else:
x_O = np.array(x_O)
y_O = np.array(y_O)
x_G = np.array(x_G)
y_G = np.array(y_G)
weights = np.array(weights)
idnums = np.array(idnums)
starlist_num = np.array(starlist_num)
night_col = np.array(night_col)
used_in_trans_1 = np.array(used_in_trans_1)
output_table = Table([x_O,y_O,x_G,y_G,weights,idnums,xe_O,ye_O,xe_G,ye_G,Ra_O,Dec_O,Ra_G,Dec_G, night_col,starlist_num,used_in_trans_1], names = ('x_OSIRIS','y_OSIRIS','x_REF','y_REF','Weight','REF_ID','xe_OSIRIS','ye_OSIRIS','xe_REF','ye_REF','Ra_OSIRIS','Dec_OSIRIS','Ra_REF','Dec_REF','Night','Frame','UiT'),)
ascii.write(output_table,dist_file,format='fixed_width', overwrite=True)
# distortion_data = output_table
distortion_data = ascii.read(dist_file,format='fixed_width')
#I generate this for each 4p loop and overwrite it.
#always pass a first one in.
else:
print('b: Loading fit {}'.format(matched_star_table_name))
# Then always load from file.
distortion_data = ascii.read(matched_star_table_name,format='fixed_width')
# use the parameters in output table to calculate a new distortion solution (check fit_legendre script)0
# use that distortion solution to update correction_list
if trim_not_used_in_trans:
print('Trimming distortion_list to only include stars used in transformation')
used_in_trans_B = np.array(distortion_data['UiT'].data) #copy of data, not pointer to
used_in_trans_B = (used_in_trans_B == "True") #Converts "True" strings to True booleans
used_in_trans_B.tolist()
distortion_data = distortion_data[used_in_trans_B]
#I can probably compress this. Make the second line a list not a tuple?
x = distortion_data['x_OBS'].data
y = distortion_data['y_OBS'].data
xref = distortion_data['x_REF'].data
yref = distortion_data['y_REF'].data
weights = distortion_data['Weight'].data
gaia_id = distortion_data['REF_ID'].data
xe = distortion_data['xe_OBS'].data
ye = distortion_data['ye_OBS'].data
xeref = distortion_data['xe_REF'].data
yeref = distortion_data['ye_REF'].data
frame = distortion_data['Frame'].data
night_c = distortion_data['Night'].data
ra = distortion_data['Ra_OBS'].data
dec = distortion_data['Dec_OBS'].data
raref = distortion_data['Ra_REF'].data
decref = distortion_data['Dec_REF'].data
# flystar rejects outliers and sets their weights to zero, which sets their errors to inf. So we put them back.
ind_r = np.where(weights == 0.0)[0] #index of stars rejected with outlier_rejection, have their weights set to 0.0
weights[ind_r] = 1/np.sqrt((xe[ind_r]*0.01)**2 + (ye[ind_r]*0.01)**2 + xeref[ind_r]**2 + yeref[ind_r]**2)
print('{} stars rejected due to outlier_rejection'.format(len(ind_r)))
outliers, bad_names, m_distances = find_outliers(x,y,xref,yref,gaia_id)
print('Mean of mahalanobis distances:', np.mean(m_distances))
print('Standard deviation of mahalanobis distances:', np.std(m_distances))
bad_names.sort()
bad_count = Counter(bad_names)
all_count = Counter(gaia_id)
bad_stars = [k for (k,v) in bad_count.items() if v > 11]
include = ~outliers
print('Fitting Legendre Polynomial')
tform_leg = transforms.LegTransform.derive_transform(x[include], y[include], xref[include], yref[include], order, m=None, mref=None,init_gx=None, init_gy=None, weights=None, mag_trans=True)
# Defines a bivariate legendre tranformation from x,y -> xref,yref using Legnedre polynomials as the basis.
current_distortion_correction = tform_leg
xts,yts = tform_leg.evaluate(x,y)
# unexplained_x_error = 0.005
# unexplained_y_error = 0.005
unexplained_x_error = 0
unexplained_y_error = 0
if centred:
a,b = tform_leg.evaluate(1024,1024)
x_central = a-1024
y_central = b-1024
xts = xts - x_central
yts = yts - y_central
xref = xref - x_central
yref = yref - y_central
xts_ra = []
yts_dec = []
#-------------------plotting quivers in RA Dec -------------------
for night in obs_nights:
# plt.figure(num=1,figsize=(6,6),clear=True)
# plt.clf()
# plt.figure(num=2,figsize=(6,6),clear=True)
# plt.clf()
quiv_scale= 0.05
quiv_label_val = 0.001
quiv_label = '{} mas'.format(quiv_label_val*1000)
os.makedirs('{}sky_resid_b_individual/{}/{}/{}'.format(plotDir,ref_iteration,night,fp_iteration),exist_ok=True)
for f, filename_1 in enumerate(osiris_filenames_dict[night]):
# tform_file_4p = './transform_files/hubble/tform_{}_{}.p'.format(ref_iteration,filename_1)
tform_file_4p = '{}tform_{}_{}_{}.p'.format(tformDir,ref_iteration,fp_iteration,filename_1)
with open(tform_file_4p, 'rb') as trans_file:
transform_4p = pickle.load(trans_file)
# idx = np.where(frame[include] == f)[0]
idx = np.where((frame[include] == f) & (night_c[include] == night))[0]
# RA_ref, Dec_ref = transform_4p[0].evaluate(xref[include][idx],yref[include][idx]) #transform pixel coordinates to RA/Dec
# RA_xts, Dec_yts = transform_4p[0].evaluate(xts[include][idx],yts[include][idx]) #distortion corrected, converted to RA/Dec
RA_ref = raref[include][idx] #instead of applying the inverse transformation then the transformation, just use the orignal ref coords. Not Centred
Dec_ref = decref[include][idx]
RA_xts, Dec_yts = transform_4p[0].evaluate(xts[include][idx]+ x_central,yts[include][idx]+y_central) #not centred
xts_ra.extend(RA_xts)
yts_dec.extend(Dec_yts)
# print(f'idx = {idx}')
angle_colour = np.arctan2(Dec_ref-Dec_yts, RA_ref-RA_xts)
# norm = Normalize(vmin=0,vmax=2*math.pi)
# norm.autoscale(angle_colour)
# colourmap = 'hsv'
# q = plt.quiver(RA_xts,Dec_yts,(RA_ref-RA_xts),(Dec_ref-Dec_yts),angle_colour, norm=Normalize(vmin=-math.pi,vmax=math.pi), cmap='hsv', scale=quiv_scale, angles='xy',width=0.002) #from corrected to ref
plt.figure(num=2,figsize=(6,6),clear=False)
q = plt.quiver(RA_ref,Dec_ref,(-RA_ref+RA_xts),(-Dec_ref+Dec_yts),angle_colour, norm=Normalize(vmin=-math.pi,vmax=math.pi), cmap='hsv', scale=quiv_scale, angles='xy',width=0.002) #from ref to corrected
plt.figure(num=1,figsize=(6,6),clear=True)
# plt.clf()
# q = plt.quiver(RA_xts,Dec_yts,(RA_ref-RA_xts),(Dec_ref-Dec_yts),angle_colour, norm=Normalize(vmin=-math.pi,vmax=math.pi), cmap='hsv', scale=quiv_scale, angles='xy',width=0.002)
q = plt.quiver(RA_ref,Dec_ref,(-RA_ref+RA_xts),(-Dec_ref+Dec_yts),angle_colour, norm=Normalize(vmin=-math.pi,vmax=math.pi), cmap='hsv', scale=quiv_scale, angles='xy',width=0.002)
plt.quiverkey(q, 0.5, 0.85, quiv_label_val, quiv_label, coordinates='figure', labelpos='E', color='green')
plt.xlim(-20,20)
plt.ylim(-20,20)
plt.xlabel('RA (arcsec)')
plt.ylabel('Dec (arcsec)')
plt.title('Sky Distortion residuals_b {} {}'.format(ref_iteration,f))
plt.savefig('{}sky_resid_b_individual/{}/{}/{}/residual_b_quiver_{}_{}_{}_{}_{}.pdf'.format(plotDir,ref_iteration,night,fp_iteration,solution_year,ref_iteration,night,fp_iteration,f), bbox_inches='tight',dpi=200)
# q = plt.quiver(xts[outliers],yts[outliers],(xref[outliers]-xts[outliers]),(yref[outliers]-yts[outliers]), color='red', scale=quiv_scale, angles='xy',width=0.0005)
plt.figure(num=2,figsize=(6,6),clear=False)
plt.quiverkey(q, 0.5, 0.85, quiv_label_val, quiv_label, coordinates='figure', labelpos='E', color='green')
plt.xlim(-20,20)
plt.ylim(-20,20)
plt.xlabel('RA (arcsec)')
plt.ylabel('Dec (arcsec)')
plt.title('Sky Distortion residuals_b {}'.format(ref_iteration))
os.makedirs('{}sky_resid_b/{}/{}/'.format(plotDir,ref_iteration,night),exist_ok=True)
plt.savefig('{}sky_resid_b/{}/{}/residual_b_quiver_{}_{}_{}_{}.pdf'.format(plotDir,ref_iteration,night,solution_year,ref_iteration,night,fp_iteration), bbox_inches='tight',dpi=200)
# plt.close('all')
#-------------------------------------------------------------
#------------------Plot quivers in pixels--------------------
os.makedirs('{}residual_b/'.format(plotDir),exist_ok=True)
# plt.close('all')
plt.figure(num=1,figsize=(6,6),clear=True)
quiv_scale=100
quiv_label_val = 5
quiv_label = '{} pix'.format(quiv_label_val)
q = plt.quiver(xts[include],yts[include],(xref[include]-xts[include]),(yref[include]-yts[include]),np.arctan2(yref[include]-yts[include], xref[include]-xts[include]),norm=Normalize(vmin=-math.pi,vmax=math.pi), cmap='hsv', scale=quiv_scale, angles='xy',width=0.0005)
# q = plt.quiver(xts[outliers],yts[outliers],(xref[outliers]-xts[outliers]),(yref[outliers]-yts[outliers]), color='red', scale=quiv_scale, angles='xy',width=0.0005)
plt.quiverkey(q, 0.5, 0.85, quiv_label_val, quiv_label, coordinates='figure', labelpos='E', color='green')
plt.xlim(-400,2448)
plt.ylim(-400,2448)
# plt.axis('equal')
# plt.set_aspect('equal','box')
plt.title('Distortion residuals_b {}'.format(ref_iteration))
# plt.savefig('{}/residual_b/residual_b_quiver_{}_{}.pdf'.format(plotDir,solution_year,ref_iteration), bbox_inches='tight',dpi=200)
plt.savefig('{}/residual_b/residual_b_quiver_{}_{}_{}.jpg'.format(plotDir,solution_year,ref_iteration,fp_iteration), bbox_inches='tight',dpi=600)
for night in obs_nights:
#plot individual frame residuals
os.makedirs('{}resid_b_individual/{}/{}/{}'.format(plotDir,ref_iteration,night,fp_iteration),exist_ok=True)
for f in range(len(osiris_filenames_dict[night])):
# idx = np.where(frame[include] == f)[0]
idx = np.where((frame[include] == f) & (night_c[include] == night))[0]
plt.figure(num=1,figsize=(6,6),clear=True)
quiv_scale=20
quiv_label_val = 1
quiv_label = '{} pix'.format(quiv_label_val)
# print(f'idx = {idx}')
angle_colour = np.arctan2(yref[include][idx]-yts[include][idx], xref[include][idx]-xts[include][idx])
# norm = Normalize(vmin=0,vmax=2*math.pi)
# norm.autoscale(angle_colour)
# colourmap = 'hsv'
q = plt.quiver(xts[include][idx],yts[include][idx],(xref[include][idx]-xts[include][idx]),(yref[include][idx]-yts[include][idx]),angle_colour, norm=Normalize(vmin=-math.pi,vmax=math.pi), cmap='hsv', scale=quiv_scale, angles='xy',width=0.002)
# q = plt.quiver(xts[outliers],yts[outliers],(xref[outliers]-xts[outliers]),(yref[outliers]-yts[outliers]), color='red', scale=quiv_scale, angles='xy',width=0.0005)
plt.quiverkey(q, 0.5, 0.85, quiv_label_val, quiv_label, coordinates='figure', labelpos='E', color='green')
plt.xlim(-400,2448)
plt.ylim(-400,2448)
plt.xlabel('Pixels')
plt.ylabel('Pixels')
# plt.axis('equal')
# plt.set_aspect('equal','box')
plt.title('Distortion residuals_b {} {}'.format(ref_iteration, f))
plt.savefig('{}resid_b_individual/{}/{}/{}/residual_b_quiver_{}_{}_{}.pdf'.format(plotDir,ref_iteration,night,fp_iteration,night,ref_iteration,f), bbox_inches='tight',dpi=200)
#-----------------------------------
# plt.close('all')
xts_ra = np.array(xts_ra) #distortion corrected observations transformed into RA Dec coordinates [include] (not centred)
yts_dec = np.array(yts_dec)
residuals_b_radec = np.hypot(raref[include]-xts_ra, decref[include]-yts_dec)
median_4p_residual_radec = np.median(residuals_b_radec)
residuals_b = np.hypot(xref-xts,yref-yts) #distance from reference to transformed
median_4p_residual = np.median(residuals_b[include])
print(f'Ref_Iteration:{ref_iteration} fp_iteration:{fp_iteration} Median_residual:{median_4p_residual:.5f}')
with open('{}fp_iteration_residuals_{}.txt'.format(resultDir,solution_year), 'a') as temp:
temp.write(f'Ref_Iteration:{ref_iteration} fp_iteration:{fp_iteration} Median_residual_pix:{median_4p_residual:.5f} Median_residual_radec:{median_4p_residual_radec:.7f}\n')
x_coefficient_names = []
x_coefficient_values = []
y_coefficient_names = []
y_coefficient_values = []
for param in tform_leg.px.param_names:
# print(getattr(tform_leg.px, param))
a = getattr(tform_leg.px, param)
x_coefficient_names.append(a.name)
x_coefficient_values.append(a.value)
for param in tform_leg.py.param_names:
a = getattr(tform_leg.py, param)
y_coefficient_names.append(a.name)
y_coefficient_values.append(a.value)
output_table = Table([x_coefficient_names,x_coefficient_values, y_coefficient_names,y_coefficient_values], names = ('px_name','px_val','py_name','py_val'),)
ascii.write(output_table,'{}distortion_coefficients_{}_{}_{}.txt'.format(resultDir,solution_year,ref_iteration,fp_iteration),format='fixed_width', overwrite=True)
return tform_leg
#------------------------fp_iteration ends here---------------------------
#need some parameters back here. current_distortion_solution and a bunch of parameters
if centred:
xref = xref + x_central # x_ref is already centred, so un-centre it.
yref = yref + y_central
xc, yc = current_distortion_correction.evaluate(1024,1024)
xc -= 1024
yc -= 1024
else:
xc = 0
yc = 0
grid = np.arange(0,2048+1,64)
xx, yy = np.meshgrid(grid, grid)
x2,y2 = tform_leg.evaluate(xx,yy)
x1 = xx.flatten()
y1 = yy.flatten()
x2 = x2.flatten()
y2 = y2.flatten()
dr = np.sqrt((x2-x1)**2 + (y2-y1)**2)
print("Mean distortion before shift", np.mean(dr))
a,b = tform_leg.evaluate(1024,1024)
x_central = a-1024
y_central = b-1024
x2_c = x2 - x_central
y2_c = y2 - y_central
dr = np.sqrt((x2_c-x1)**2 + (y2_c-y1)**2)
print("Mean distortion after shift", np.mean(dr))
xts, yts = tform_leg.evaluate(x,y)
distances2 = (xref-xts)**2 + (yref-yts)**2
distances_ref = np.hypot(xref-x,yref-y) #distance from OSIRIS original to reference.
distances_tr = np.hypot(xts-x,yts-y) #distance from OSIRIS original to transformed.
unexplained_error = np.sqrt(unexplained_x_error**2 + unexplained_y_error**2)
# unexplained_error = 0
variances = 1/weights**2 + unexplained_error**2
weights = 1/np.sqrt(variances)
residuals_b_std = np.hypot((xts-xref),(yts-yref))*weights*0.01 #hoping that the units are wrong 10 mas per pixel. 0.01 arcsec per pixel
#xe*0.01 to get it into arcseconds?
#* 0.01 to get it into pixels
residuals_b_x = (xts-xref) / np.sqrt((xe*0.01)**2 + xeref**2 + unexplained_x_error**2) *0.01 #np.hypot(xe,xeref)
residuals_b_y = (yts-yref) / np.sqrt((ye*0.01)**2 + yeref**2 + unexplained_y_error**2) *0.01 #np.hypot(ye,yeref)
weighted_mean_residual_b = np.average(residuals_b[include], weights = weights[include])
weighted_mean_residual_b_squared = np.sqrt(np.average(residuals_b[include]**2, weights = weights[include]))
print('min,median,max residuals:',np.min(residuals_b[include]),np.median(residuals_b[include]),np.max(residuals_b[include]))
print('min,median,max uncertainties:',np.min(1/weights[include]),np.median(1/weights[include]),np.max(1/weights[include]))
print('weighted mean residual_b: {}'.format(weighted_mean_residual_b))
print('weighted mean residual_b squared: {}'.format(weighted_mean_residual_b_squared))
bad_a = np.nonzero(residuals_b > 22)
bad_o = np.nonzero(outliers)
# print(bad_a)
# print(bad_o) #manually finding outliers by their residuals
print('Residual_b > 22, not caught as outliers:', np.setdiff1d(bad_a, bad_o, assume_unique=True))
# quit()
#----------------------------------
# median_residual_b = np.median(residuals_b[include])
# median_residuals_b.append(median_residual_b)
median_residuals_b.append(median_4p_residual)
mean_residuals_b.append(weighted_mean_residual_b)
mean_residuals_b_squared.append(weighted_mean_residual_b_squared)
min_residuals_b.append(np.min(residuals_b[include]))
max_residuals_b.append(np.max(residuals_b[include]))
num_residuals_b.append(len(residuals_b[include]))
median_residuals_b_radec.append(median_4p_residual_radec)
#This section was at the end, after A. Moved here.
with open(resultDir + 'iteration_residuals_{}.txt'.format(solution_year), 'w') as temp:
for r, resid in enumerate(median_residuals_b):
# temp.write(str(resid) + '\n')
temp.write(f'Min:{min_residuals_b[r]:.5f} Median:{median_residuals_b[r]:.5f} Max:{max_residuals_b[r]:.5f} Num:{num_residuals_b[r]:.5f} Weighted mean:{mean_residuals_b[r]:.5f} Weighted mean squared:{mean_residuals_b_squared[r]:.5f} Median_mas:{median_residuals_b_radec[r]:.7f} \n') #| Mean_a:{mean_residuals_a[r]:.5f}
print('Median residual = {}'.format(median_4p_residual))
print('Median residual radec = {}'.format(median_4p_residual_radec))
print('Weighted mean residual = {}'.format(weighted_mean_residual_b))
print('Weighted mean residual squared = {}'.format(weighted_mean_residual_b_squared))
#removed break condition. Will complete all ref_iterations
# if ref_iteration > 5:
# # improvement = median_residuals_b[ref_iteration-1]-median_residuals_b[ref_iteration]
# improvement = mean_residuals_b[ref_iteration-1]-mean_residuals_b[ref_iteration]
# print('Improvement = {}'.format(improvement))
# if ref_iteration > 5:
# if 0 < improvement < 0.005: #0.05 mas = 0.005 pixels 50 mas = 5 pixel. 0.05 mas = 0.005
# print('Improvement < 0.05, breaking at ref_iteration {}'.format(ref_iteration))
# break
print('All ref_iteration median residuals_b = {}'.format(median_residuals_b))
print('All ref_iteration weighted mean residuals_b = {}'.format(mean_residuals_b))
print('All ref_iteration weighted mean residuals_b squared= {}'.format(mean_residuals_b_squared))
print('All ref_iteration mean residuals_a = {}'.format(mean_residuals_a))
# plt.close('all')
# quit()
return current_distortion_correction #maybe?
def reference_section():
# --------------------------------------- Section A ---------------------------------------------------------------
if len(refTable_current) == 0:
with open(refTable_current_filename, 'rb') as temp:
refTable_current = pickle.load(temp)
#------------------------Night loop 1 starts here---------------------------
obs_nights = [night for night in config]
print(obs_nights)
for night in obs_nights:
if night == 'n1':
target = 'm15'
nightDir = '/u/mfreeman/work/d/n1/'
cleanDir = nightDir + 'clean/m15_kn3_tdOpen/'
hubble_file = '/g/lu/data/m15/hst_ref/NGC7078cb.pm' #'je0o61lzq_flt.xymrduvqpk'
targetID = 1745948323734090368 #gaia ID of target star. Ra and Dec used in prepare_gaia_for_flystar()
# ra_field = '21:29:57.60' #approximate centre of FoV, selects gaia stars in radius
# dec_field = '12:10:28.3'
ra_field = 322.48999069
dec_field = 12.17453385
radius = 20 #arcseconds
minmag = 15.4 #dimmest mag for cut
single_fit = True #run Mosaic2Ref for each image individually
rad_tolerance = [0.4, 0.4, 0.2]
mag_tolerance = [2, 2, 2]
mag_limits = [6,16]
bad_files = ['ci200804_a022007_flip_0.8_stf.lis',
'ci200804_a026012_flip_0.8_stf.lis',
'ci200804_a027003_flip_0.8_stf.lis',
]
dont_trim = ['ci200804_a014004_flip_0.8_stf.lis', 'ci200804_a026009_flip_0.8_stf.lis']
# 026002 is bad either way. Didn't find the brightest star. Could be that flag I selected. Try without?
sl = slice(0,None)
# sl = slice(34,38)
show_plots = False
elif night == 'n2':
target = 'm92'
nightDir = '/u/mfreeman/work/d/n2/'
cleanDir = nightDir + 'clean/m92_kp_tdOpen/'
hubble_file = '/g/lu/data/m92/hst_ref/NGC6341cp.pm' #'idk901xpq_flt.xymrduvqpk'
targetID = 1360405503461790848 #gaia ID of target star. Ra and Dec used in prepare_gaia_for_flystar()
ra_field = 259.285096306648 #approximate centre of FoV, selects gaia stars in radius
dec_field = 43.13751895071527
radius = 20 #arcseconds
minmag = 15.4 #dimmest mag for cut
rad_tolerance = [0.4, 0.4, 0.2]
mag_tolerance = [2, 2, 2]
mag_limits = None
single_fit = True #run Mosaic2Ref for each image individually
bad_files = []
dont_trim = []
sl = slice(0,None)
# sl = slice(151,None)
show_plots = False
elif night == 'n3':
target = 'm92'
nightDir = '/u/mfreeman/work/d/n3/'
cleanDir = nightDir + 'clean/m92_kp_tdOpen/'
hubble_file = '/g/lu/data/m92/hst_ref/NGC6341cp.pm' #'idk901xpq_flt.xymrduvqpk'
targetID = 1360405503461790848 #gaia ID of target star. Ra and Dec used in prepare_gaia_for_flystar()
ra_field = 259.285096306648 #approximate centre of FoV, selects gaia stars in radius
dec_field = 43.13751895071527
radius = 20 #arcseconds
minmag = 15.4 #dimmest mag for cut
rad_tolerance = [0.4, 0.4, 0.2]
mag_tolerance = [2, 2, 2]
mag_limits = None
single_fit = True #run Mosaic2Ref for each image individually
bad_files = []
dont_trim = []
sl = slice(0,None)
# sl = slice(151,None)
show_plots = False
elif night == 'n4':
target = 'm92'
nightDir = '/u/mfreeman/work/d/n4/'
cleanDir = nightDir + 'clean/m92_kp_tdOpen/'
hubble_file = '/g/lu/data/m92/hst_ref/NGC6341cp.pm' #'idk901xpq_flt.xymrduvqpk'
targetID = 1360405503461790848 #gaia ID of target star. Ra and Dec used in prepare_gaia_for_flystar()
ra_field = 259.285096306648 #approximate centre of FoV, selects gaia stars in radius
dec_field = 43.13751895071527
radius = 20 #arcseconds
minmag = 15.4 #dimmest mag for cut
rad_tolerance = [0.4, 0.4, 0.2]
mag_tolerance = [2, 2, 2]
mag_limits = None
single_fit = True #run Mosaic2Ref for each image individually
bad_files = ['ci200814_a032008_flip_0.8_stf.lis']
dont_trim = []
sl = slice(0,None)
# sl = slice(151,None)
show_plots = False
elif night == 'n5':
target = 'm15'
nightDir = '/u/mfreeman/work/d/n5/'
cleanDir = nightDir + 'clean/m15_kn3_tdhBand/'
hubble_file = '/g/lu/data/m15/hst_ref/NGC7078cb.pm' #'icbe05m9q_flt.xymrduvqpk' #there are other files too?
targetID = 1745948328028761984 #gaia ID of target star. Ra and Dec used in prepare_gaia_for_flystar()
ra_field = 322.4912419147502 #approximate centre of FoV, selects gaia stars in radius
dec_field = 12.164721331771977
radius = 20 #arcseconds
minmag = 15.4 #dimmest starlist mag for cut
rad_tolerance = [0.4, 0.4, 0.2]
mag_tolerance = [2, 2, 2]
mag_limits = [6,16]
single_fit = True #run Mosaic2Ref for each image individually
bad_files = ['ci211024_a011009_flip_0.8_stf.lis','ci211024_a012010_flip_0.8_stf.lis','ci211024_a020012_flip_0.8_stf.lis']
dont_trim = []
sl = slice(0,None)
# sl = slice(79,None)
show_plots = False
else:
print('No night selected')
quit()
plotDir_n = plotDir + night + '/'
osiris_filenames = get_osiris_files(config[night]['stackDir'])
sl2 = slice(0,None)
osiris_filenames = osiris_filenames[sl2]
print(osiris_filenames)
print(len(osiris_filenames), 'OSIRIS images')
# combined_ref_filename = resultDir + 'combined_ref_table_' + str(ref_iteration) + '.txt'
combined_ref_filename = '{}combined_ref_table_{}_{}.txt'.format(resultDir,night,ref_iteration)
combined_ref_filename_previous = previous_results_location + reference_instrument + '_' + fitmode + '/' + 'combined_ref_table_' + str(ref_iteration) + '.txt'
combined_ref_filename_previous = '{}{}_{}/combined_ref_table_{}_{}.txt'.format(previous_results_location,reference_instrument,fitmode,night,ref_iteration)
if os.path.exists(combined_ref_filename):
combined_ref_filename_toUse = combined_ref_filename
elif os.path.exists(combined_ref_filename_previous):
combined_ref_filename_toUse = combined_ref_filename_previous
else:
combined_ref_filename_toUse = None
# if (not create_combined_reflist) and os.path.exists(combined_ref_filename):
# print('a: Loading previous combined refTable')
# with open(combined_ref_filename, 'rb') as combined_ref_file:
# refTable_a = pickle.load(combined_ref_file)
if (not create_combined_reflist) and (combined_ref_filename_toUse is not None):
print('a: Loading previous combined refTable')
with open(combined_ref_filename_toUse, 'rb') as combined_ref_file:
refTable_a = pickle.load(combined_ref_file)
else:
list_of_starlists = []
print('a: Generating new combined refTable')
for i, filename in enumerate(osiris_filenames):
if filename in bad_files:
print('{} {} flagged as bad, skipping'.format(i,filename))
# errorlist.append(filename[:-4])
else:
print('{} {} applying distortion correction'.format(i,filename))
starlist = load_osiris_file(config[night]['config[night]['stackDir']'] ,filename)
# plt.close('all')
# ido = np.where(starlist['m'] < 15.5)
# starlist = starlist[ido]
fitsfile = config[night]['cleanDir'] + filename[:-12] + '.fits'
PA = get_PA(fitsfile)
# print('PA', PA)
# starlist = brightest_n(starlist,170)
starlist = mag_cut(starlist,0,minmag)
if not filename in config[night]['dont_trim']:
starlist = edge_cut(starlist,5)
if len(starlist) == 0:
print(i,filename, '0 stars remaining after edge cut, skipping image')
errorlist.append(filename[:-4])
continue
#----------------------------------------
# plt.figure()
# plt.hist(starlist['m'])
# # plt.scatter(starlist['m'],starlist['vxe'],ptsize,alpha=0.2,label='Ref')
# plt.show()
# quit()
#--------------------------------------
#apply distortion correction
xt, yt = correction_list[ref_iteration].evaluate(starlist['x'],starlist['y'])
# if centred:
# xc, yc = correction_list[ref_iteration].evaluate(1024,1024)
# xc -= 1024
# yc -= 1024
# else:
# xc = 0
# yc = 0
#I think I always want the centred version. The DAR is calculated based on the header elevation, so I want the central pixel to be undistorted
#maybe I just always want it centred. Because that is the actual distortion. And it doesn't effect the model.
xc, yc = correction_list[ref_iteration].evaluate(1024,1024)
xc -= 1024
yc -= 1024
starlist['x'] = xt - xc
starlist['y'] = yt - yc
# if reference_instrument == 'Hubble':
# refTable_t = refTable
# elif reference_instrument == 'GAIA':
# refTable_t = trim_gaia(refTable,filename,PA)
plt.figure(num=4,figsize=(6,6),clear=True)
if recalculate_plate_scale:
# refTable_d = dar.applyDAR(fitsfile, refTable_t, plot=False, instrument=osiris, plotdir=plotDir + 'dar/')
starlist = dar.removeDAR(fitsfile,starlist, plot=False, instrument=osiris, plotdir=plotDir_n + 'dar_r/'+ str(ref_iteration) + '/')
else:
# refTable_d = dar.applyDAR(fitsfile, refTable_t, plot=True, instrument=osiris, plotdir=plotDir + 'dar/')
starlist = dar.removeDAR(fitsfile,starlist, plot=True, instrument=osiris, plotdir=plotDir_n + 'dar_r/'+ str(ref_iteration) + '/')
#print(starlist.info)
# plot_dots(refTable_current[night],starlist,filename,PA,plotDir_n + 'dots_a/' + str(ref_iteration) + '/')
#refTable isn't declared if I load section B. So just don't plot it? I already run plot_dots in section B
list_of_starlists.append(starlist)
# successlist.append(filename[:-4])
# plt.close('all')
# quit()
print('--------------------')
print('Completed corrections')
# print('Succeeded for', len(successlist), 'files.')
# print('Failed for', len(errorlist), 'files:')
# print(errorlist)
print(len(list_of_starlists))
print([len(j) for j in list_of_starlists])
# do mosaicSelfref on list_of_starlists to generate a master list.
if reference_instrument == 'Hubble':
# os.makedirs('./transform_files/hubble_{}'.format(fitmode),exist_ok=True)
# tform_file_ref = './transform_files/hubble_{}/tform_{}_{}.p'.format(fitmode,ref_iteration,'ref')
# tform_file_ref_last = './transform_files/hubble_{}/tform_{}_{}.p'.format(fitmode,ref_iteration-1,'ref')
# tform_file_ref_previous = ['/u/mfreeman/work/d/transform_files/hubble/tform_{}.p'.format(i) for i in osiris_filenames]
os.makedirs('{}combining_ref/'.format(tformDir),exist_ok=True)
tform_file_ref = '{}combining_ref/tform_{}_{}.p'.format(tformDir,ref_iteration,night)
tform_file_ref_2 = '{}combining_ref/tform_{}_{}.p'.format(transform_files_location,fitmode,ref_iteration,night)
tform_file_ref_last = '{}combining_ref/tform_{}_{}.p'.format(tformDir,fitmode,ref_iteration-1,night)
tform_file_ref_previous = ['/u/mfreeman/work/d/transform_files/hubble/tform_{}.p'.format(i) for i in osiris_filenames]
elif reference_instrument == 'GAIA':
#haven't updated to use new location
tform_file_ref = './transform_files/gaia_{}/tform_{}_{}.p'.format(fitmode,ref_iteration,'ref')
tform_file_ref_previous = ['/u/mfreeman/work/d/transform_files/gaia/tform_{}.p'.format(i) for i in osiris_filenames]
use_individual_trans_guesses = False
# if ref_iteration == 2:
if os.path.exists(tform_file_ref):
print('Loading transform from {}'.format(tform_file_ref))
with open(tform_file_ref, 'rb') as trans_file:
trans_list_temp = pickle.load(trans_file)
if len(trans_list_temp) == len(list_of_starlists):
print('Transform guess has correct number of lists {}'.format(len(trans_list_temp)))
trans_list = trans_list_temp
else:
use_individual_trans_guesses = True
elif os.path.exists(tform_file_ref_2):
print('Loading a previous transform from {}'.format(tform_file_ref_2))
with open(tform_file_ref_2, 'rb') as trans_file:
trans_list_temp = pickle.load(trans_file)
if len(trans_list_temp) == len(list_of_starlists):
print('Transform guess has correct number of lists: {}'.format(len(trans_list_temp)))
trans_list = trans_list_temp
else:
use_individual_trans_guesses = True
elif os.path.exists(tform_file_ref_last):
print('Loading last transform from {}'.format(tform_file_ref_last))
with open(tform_file_ref_last, 'rb') as trans_file:
trans_list_temp = pickle.load(trans_file)
if len(trans_list_temp) == len(list_of_starlists):
print('Transform guess has correct number of lists: {}'.format(len(trans_list_temp)))
trans_list = trans_list_temp
else:
use_individual_trans_guesses = True
else:
use_individual_trans_guesses = True
if use_individual_trans_guesses:
print('Loading old transform')
if os.path.exists(tform_file_ref_previous[0]):
trans_list = []
for i, tform_filename in enumerate(tform_file_ref_previous):
with open(tform_filename, 'rb') as trans_file:
trans_list.extend(pickle.load(trans_file)) #initial guess for the transformation
#each file is a list with one element, so using .extend
else:
# trans_list = last_good_transform
print('No trans_list found')
trans_list = None
if fitmode == 'FF':
set_use_ref_new = False
set_update_ref_orig = False
elif fitmode == 'TF':
set_use_ref_new = True
set_update_ref_orig = False
elif fitmode == 'FT':