-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_distortion_params2.pdl
521 lines (435 loc) · 16.8 KB
/
get_distortion_params2.pdl
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
#-*-perl-*-
=head2 get_distortion_params - load or generate distortion parameters for a sequence of HI-2 images
=for usage
$hash = get_distortion_params($hash, $opt);
hash is a values hash ref returned by remove_f_corona or similar. opt
is an options hash ref..
=for ref
Adds distortion parameters to the options hash, retrieving them as
necessary to calculate the interframe distortion function between any
two images in the data set. If necessary, the distortion function is
measured directly, otherwise it is loaded from the ancillary file
named in the options hash (or its default value). Ditto the actual
analytic distortion function parameters, which are either fitted
directly from the distortion function measurements or retrieved from a
cached disk file.
=cut
use PDL::IO::Dumper;
use Date::Parse;
use PDL::Transform;
use PDL::Transform::Cartography;
sub get_distortion_params2 {
my $hash = shift;
my $opt = shift;
my $VERSION = "2.0 5-May-2013";
$opt = {} unless(defined $opt);
##############################
# Set default values for options hash
$opt->{'DIST_CORR_FNAME'} = "correlations.pl" unless(exists($opt->{'DIST_CORR_FNAME'}));
$opt->{'DIST_PARAM_FNAME'} = "distortion-params.pl" unless(exists($opt->{'DIST_PARAM_FNAME'}));
$opt->{'DIST_CORR_NSAMP'} = 30 unless(exists($opt->{'DIST_CORR_NSAMP'}));
$opt->{'DIST_CORR_RSIZE'} = 20 unless(exists($opt->{'DIST_CORR_RSIZE'}));
$opt->{'DIST_CORR_PSIZE'} = 30 unless(exists($opt->{'DIST_CORR_PSIZE'}));
$opt->{'DIST_CORR_NCPU'} = 3 unless(exists($opt->{'DIST_CORR_NCPU'}));
$opt->{'DIST_MIN_PCORR'} = 0.99 unless(exists($opt->{'DIST_MIN_PCORR'}));
$opt->{'DIST_CORRTIME'} = 6 unless(exists($opt->{'DIST_CORRTIME'}));
$opt->{'DIST_GUESS_DRIFTRATE'} = 72/5 unless(exists($opt->{'DIST_GUESS_DRIFTRATE'}));
$opt->{'DIST_UNSHARP_D'} = 11 unless(exists($opt->{'DIST_UNSHARP_D'}));
$opt->{'DIST_NDELAYS'} = 2 unless(exists($opt->{'DIST_NDELAYS'}));
$opt->{'NPROC'} = 3 unless(exists($opt->{'NPROC'}));
$opt->{'PIXEL_YRANGE'} = pdl(75,950) unless(exists($opt->{'PIXEL_YRANGE'}));
$opt->{'PIXEL_XRANGE'} = undef unless(exists($opt->{'PIXEL_XRANGE'}));
$opt->{'DIST_FIT_ITER'} = 15 unless exists($opt->{DIST_FIT_ITER});
$opt->{'DEBUG'} = 0 unless(exists($opt->{'DEBUG'}));
$opt->{'locof'} = undef unless(exists($opt->{'locof'}));
print "DIST_NDELAYS=$opt->{DIST_NDELAYS}\n";
##############################
# Convert timestamps to UNIX time...
# Add in "EXPTIME"/2 to get the average time of the exposure.
my @times = map { str2time($_->{"DATE-OBS"}) + $_->{"EXPTIME"}/2 } @{$hash->{"RAW_HDRS"}};
my $times = pdl(@times);
$hash->{'UTIME'} = $times;
my $tmid = ($times->((0)) + $times->((-1)))/2;
my $trange = $times->((-1)) - $times->((0));
##############################
# Time consistency checks...
print "times is $times\n";
if(any($times->(1:-1) - $times->(0:-2)) < 0) {
die("get_distortion_params2: times appear not to be monotonic. Fix that!\n");
}
##############################
# Generate correlation data
my $n = 0;
my ($i,$j);
##############################
# Make sure we have a long enough interval....
if( $times->((-1))-$times->((0)) < 3 * 24 * 3600 ) {
die("get_distortion_params2: you need at least 3 days worth of data\n");
}
##########
# Pick two images near the middle of the data set, nominally 5 days apart...
# also an extra one 1/3 of the way between them, to do extra correlation.
my $tstart = $tmid - $opt->{'DIST_CORRTIME'}*3600*24 / 2;
my $st_dex;
if($times->((0))<=$tstart) {
$st_dex = max( which($times <= $tstart) );
} else {
$st_dex = 0;
}
my $tend = $times->($st_dex) + $opt->{'DIST_CORRTIME'} * 3600 * 24;
my $en_dex;
my $mid_dex;
if($times->((-1))>=$tend) {
$en_dex = min( which($times >= $tend) );
} else {
$en_dex = $times->dim(0)-1;
}
$mid_dex = ($st_dex*2 + $en_dex*1)/3;
my $im0 = $hash->{'BKSUB_CUBE'}->(:,:,($st_dex))->sever;
my $im1 = $hash->{'BKSUB_CUBE'}->(:,:,($en_dex))->sever;
my $im2 = $hash->{'BKSUB_CUBE'}->(:,:,($mid_dex))->sever;
my $t0 = $times->(($st_dex));
my $t1 = $times->(($en_dex));
my $t2 = $times->(($mid_dex));
if($opt->{DIST_UNSHARP_D}) {
print "unsharp masking...";
my $kr = rvals($opt->{DIST_UNSHARP_D}*2.5,$opt->{DIST_UNSHARP_D}*2.5);
my $k = -exp($kr*$kr/$opt->{DIST_UNSHARP_D}/$opt->{DIST_UNSHARP_D}*4);
$k /= $k->sum;
# $im0 -= $im0->convolveND($k,{method=>'direct'});
# $im1 -= $im1->convolveND($k,{method=>'direct'});
our $gdp_im0a = $im0->copy;
our $gdp_im1a = $im1->copy;
$im0 -= minismooth($im0,$opt->{DIST_UNSHARP_D},$opt->{DIST_UNSHARP_D}/2)->convolveND($k,{bound=>"m",method=>'direct'});
$im1 -= minismooth($im1,$opt->{DIST_UNSHARP_D},$opt->{DIST_UNSHARP_D}/2)->convolveND($k,{bound=>"m",method=>"direct"});
$im2 -= minismooth($im2,$opt->{DIST_UNSHARP_D},$opt->{DIST_UNSHARP_D}/2)->convolveND($k,{bound=>"m",method=>"direct"});
our $gdp_im0 = $im0->copy;
our $gdp_im1 = $im1->copy;
our $gdp_im2 = $im2->copy;
print "ok\n";
}
$im0->sethdr($hash->{'RAW_HDRS'}->[$st_dex]);
$im1->sethdr($hash->{'RAW_HDRS'}->[$en_dex]);
$im2->sethdr($hash->{'RAW_HDRS'}->[$mid_dex]);
use PDL::Transform;
my $guess_offset = ($t1 - $t0)/3600/24 * $opt->{DIST_GUESS_DRIFTRATE};
my $guess_offset02 = ($t2 - $t0)/3600/24 * $opt->{DIST_GUESS_DRIFTRATE};
my $guess_offset21 = ($t1 - $t2)/3600/24 * $opt->{DIST_GUESS_DRIFTRATE};
my $deltat = ($t1-$t0)/3600/24;
my $deltat2 =($t2-$t0)/3600/24;
print "Correlating between frame $st_dex and frame $en_dex -- interval is $deltat days...\n";
print "Also mid_dex - interval from st is $deltat2 days...";
my @of = ();
my @loc = ();
my @of02 = ();
my @of21 = ();
my @loc02 = ();
my @loc21 = ();
my $step0 = pdl($hash->{'BKSUB_CUBE'}->dim(0)/($opt->{'DIST_CORR_NSAMP'}+1))->floor;
my $step1 = pdl($hash->{'BKSUB_CUBE'}->dim(1)/($opt->{'DIST_CORR_NSAMP'}+1))->floor;
my $start0 = pdl(($hash->{'BKSUB_CUBE'}->dim(0) - $opt->{'DIST_CORR_NSAMP'} * $step0)/2)->floor;
my $start1 = pdl(($hash->{'BKSUB_CUBE'}->dim(1) - $opt->{'DIST_CORR_NSAMP'} * $step1)/2)->floor;
print "\n";
my($of,$loc);
if($opt->{locof}) {
print "Using parameterized locof passed in as option!\n";
$of = $opt->{locof}->{of}->copy;
$loc = $opt->{locof}->{loc}->copy;
} else {
for( $j=0; $j < $opt->{'DIST_CORR_NSAMP'}; $j++ ) {
printf("Row %3d of %3d: ",$j,$opt->{'DIST_CORR_NSAMP'});
for($i=0; $i < $opt->{'DIST_CORR_NSAMP'}; $i += ($opt->{'DIST_CORR_NCPU'}||1)) {
my $roundrobin = 0;
my %subs = ();
# Fork off $NCPU copies of ourselves if necessary
print "|";
if($opt->{'DIST_CORR_NCPU'} > 1) {
forker: for($roundrobin = 1; $roundrobin < $opt->{'DIST_CORR_NCPU'}; $roundrobin++) {
my $file = sprintf("/tmp/%s-subproc-%d.tmp",$$,$roundrobin);
open OUTPUT,">$file";
my $pid = fork();
last forker unless($pid);
$subs{$pid} = $file;
}
}
# Do the meat. But first we have to adjust $i...
my $ii = $i;
if($roundrobin) {
$ii += $roundrobin - 1;
}
my $loc = pdl( $start0 + $ii * $step0,
$start1 + $j * $step1
);
## This first pass is the short interval. We only
## bother to do the correlation if we're doing two
## delta-tees.
my $a;
if($opt->{'DIST_NDELAYS'}>1) {
$a = correlate_patch4($im0, $im2, $loc,
pdl( $guess_offset02, 0 ),
$opt->{'DIST_CORR_RSIZE'}/2,
$opt->{'DIST_CORR_PSIZE'}/2,
$opt->{'DIST_MIN_PCORR'});
if(defined $a){
push(@of02,$a);
push(@loc02, $loc);
if(all(isfinite($a))) {
print ".";
} else {
print "-";
}
}
else {
$a = asin(pdl(1.1,1.1));
}
} else {
# Not doing this one, just generate a pair of NaNs for the offset.
$a = asin(pdl(1.1,1.1));
}
# This second pass is the long interval (~5 days for HI-2).
# We do this every time.
my $b = correlate_patch4($im0, $im1, $loc,
pdl( $guess_offset, 0 ),
$opt->{'DIST_CORR_RSIZE'}/2,
$opt->{'DIST_CORR_PSIZE'}/2,
$opt->{'DIST_MIN_PCORR'});
if(defined $b){
push(@of,$b);
push(@loc, $loc);
if(all(isfinite($b))) {
print ".";
} else {
print "-";
}
}
else {
# print "!";
$b = asin(pdl(1.1,1.1));
}
# Now check if we are a daughter process. If so, write to the temp file. If not, do our business.
if($roundrobin && ($roundrobin < $opt->{'DIST_CORR_NCPU'})) {
printf OUTPUT "%.10e, %.10e, %.10e, %.10e, %.10e, %.10e\n", $loc->list, $a->list, $b->list;
close OUTPUT;
exit(0); # Exit of daughter process
} else {
# We're in a parent or singleton process. Snarf up tempfile stuff and put it in the right place.
while(keys %subs) {
my $pid = wait();
if($subs{$pid}) {
open FOO,"<".$subs{$pid};
my $line = <FOO>;
close FOO;
unlink $subs{$pid};
undef $subs{$pid};
my @fields = split /\,/,$line;
if(@fields==4) {
my $loc = pdl($fields[0],$fields[1]);
my $of02 = pdl($fields[2],$fields[3]);
my $of=pdl($fields[4],$fields[5]);
push(@of02, $of02);
push(@of, $of);
push(@loc,$loc);
}
} else {
if($pid<0) {
%subs = ();
}
}
}
}
}
print "\n";
}
our $gdp_loc = pdl(@loc); # debugging...
our $gdp_of = pdl(@of);
my $of0 = pdl(@of);
my $looplocs = pdl(@loc);
$mask = isfinite($of0->((0))) & isfinite($of0->((1)));
print "mask is $mask\n";
$loc = $looplocs->(:, which($mask));
$of = $of0->(:, which($mask));
our $locof = {loc=>$loc->copy, of=>$of->copy};
} # end of correlation section
my($of02, $mask02, $loc02);
if($opt->{DIST_NDELAYS}>1){
$of02 = pdl(@of02);
$mask02 = isfinite($of02->((0))) & isfinite($of02->((1)));
$of02 = $of02->(:,which($mask02))->sever;
$loc02 = pdl(@loc02)->(:,which($mask02));
}
my $h = { of => $of,
loc => $loc,
of02 => $of02,
loc02 => $loc02,
deltat => $deltat,
deltat2 => $deltat2,
guess => $guess_offset,
hdr0 => $hash->{'RAW_HDRS'}->[$st_dex],
hdr1 => $hash->{'RAW_HDRS'}->[$en_dex]
};
use PDL::IO::Dumper;
fdump($h, "distortion-hash.pl");
$hash->{'CORR'} = $h;
##############################
# Now fit the distortion data.
printf("Fitting correlation info...\n");
# Define convenience variables for the centroids
my $ct1 = $hash->{'CORR'}->{loc};
my $ct2 = $hash->{'CORR'}->{of} + $ct1;
my($ct1_02, $ct2_02);
if($opt->{DIST_NDELAYS}>1){
$ct1_02 = $hash->{'CORR'}->{loc02};
$ct2_02 = $hash->{'CORR'}->{of02}+$ct1_02;
}
print "ct1 is ".join("x",$ct1->dims)."\n";
my $ii=0;
# Identify maximum horizontal range...
if(defined $opt->{PIXEL_XRANGE}) {
$xr = pdl($opt->{'PIXEL_XRANGE'});
} else {
$xr = pdl(max_horiz_range($hash,$opt));
}
my $yr = pdl($opt->{PIXEL_YRANGE});
print "xr is $xr; yr is $yr\n";
# Trim fit points to the region of interest.
$ctwhich = which($ct1->((0)) >= $xr->((0)) &
$ct1->((0)) <= $xr->((1)) &
$ct2->((0)) >= $xr->((0)) &
$ct2->((0)) <= $xr->((1)) &
$ct1->((1)) >= $yr->((0)) &
$ct1->((1)) <= $yr->((1)) &
$ct2->((1)) >= $yr->((0)) &
$ct2->((1)) <= $yr->((1)));
p "ct1 is $ct1; ct2 is $ct2\n";
$ct1 = $ct1->(:,$ctwhich)->sever;
$ct2 = $ct2->(:,$ctwhich)->sever;
if($opt->{DIST_NDELAYS} > 1){
$ctwhich_02 = which($ct1_02->((0)) >= $xr->((0)) &
$ct1_02->((0)) <= $xr->((1)) &
$ct2_02->((0)) >= $xr->((0)) &
$ct2_02->((0)) <= $xr->((1)) &
$ct1_02->((1)) >= $yr->((0)) &
$ct1_02->((1)) <= $yr->((1)) &
$ct2_02->((1)) >= $yr->((0)) &
$ct2_02->((1)) <= $yr->((1)));
$ct1_02 = $ct1_02->(:,$ctwhich_02)->sever;
$ct2_02 = $ct2_02->(:,$ctwhich_02)->sever;
}
print "kept ".$ct1->dim(1)." points for fit (xr=$xr, yr=$yr)...\n";
# Calculate pixel offset of each boundary toward the middle using the guess rotation rate...
my $dt_to_center = ($hash->{'UTIME'}->((-1)) - $hash->{'UTIME'}->((0)))/2 / 3600 / 24;
my $margin = $dt_to_center / $hash->{'CORR'}->{'deltat'} * $hash->{'CORR'}->{'guess'};
my $margin2 = $dt_to_center / $hash->{'CORR'}->{'deltat2'} * $hash->{'CORR'}->{'guess'};
# shrink horizontal range by the pixel offset calculated, with an extra 5% margin.
print "\n***\nshrinking xr by $margin\n\n";
my $xr2 = $xr->copy;
$xr += pdl(1,-1) * $margin * 1.05 ;
$xr2 += pdl(1,-1) * $margin2 * 1.05;
$xr = floor($xr+0.5);
$xr2 = floor($xr2+0.5);
print "column range is $xr "."(margin is $margin)\n";
my $master_hdr = $hash->{'RAW_HDRS'}->[(0+@{$hash->{'RAW_HDRS'}})/2];
my $ct1a;
my $ct2a;
my $ct1a_02;
my $ct2a_02;
# Define a fit heuristic -- RMS error in location
my $heur_ret;
print "ct1 dims: ".join('x',$ct1->dims)."; ct2 dims: ".join("x",$ct2->dims)."\n";
my $heur = sub {
my $dx = shift;
my ($delta1, $delta2, $deltamu, $roll_angle, $r2, $orbital_rotation, $q, $qq) = $dx->list;
$deltamu /= 10;
$delta1 /= 100;
$delta2 /= 100;
my $tnl_params = [$master_hdr,$deltamu, $roll_angle, $r2, $delta1, $delta2, $q, $qq];
my $tnl = t_nl(@$tnl_params);
my $to = t_linear(pre=>[$orbital_rotation,0]);
my $to_02 = t_linear(pre=>[$orbital_rotation * $deltat2/$deltat,0]);
my $t_heur_02 = !$tnl x $to_02 x $tnl;
my $t_heur = !$tnl x $to x $tnl;
$ct1a = $ct1->apply( $t_heur );
$ct2a = $ct2->apply( !$t_heur );
my $dists = (($ct1a - $ct2a)*($ct1a - $ct2a))->sumover->sqrt;
if($opt->{'DIST_NDELAYS'}>1){
$ct1a_02 = $ct1_02->apply( $t_heur_02 );
$ct2a_02 = $ct2_02->apply( !$t_heur_02 );
my $dists_02 = (($ct1a_02-$ct2a_02)*($ct1a_02-$ct2a_02))->sumover->sqrt;
$avg = ($dists->avg + $dists_02->avg)/2;
$avg = 1e6 if(any(isbad($dists_02)));
} else {
$avg = $dists->avg;
}
$avg = 1e6 if(!isfinite($avg));
$avg = 1e6 if(any(isbad($dists)));
if($opt->{DISPLAY}) {
our $ww0,$ww1;
unless(defined $ww0) {
$ww0=gpwin(x11,size=>[4,3]);
$ww1=gpwin(x11,size=>[4,3]);
}
$diffs = $ct2a-$ct1a;
$z = zeroes(256,256,2);
$zr = $z->range(($ct2a+$ct1a)/8-5,[11,11],'e');
$zr->mv(-1,0) .= $ct2a-$ct1a;
$ww0->imag($z->(:,:,(0)),{cbr=>[-1.25,1.25],j=>1,title=>"X"});
$ww1->imag($z->(:,:,(1)),{cbr=>[-1.25,1.25],j=>1,title=>"Y"});
}
$heur_ret = $avg;
return $avg;
};
my $siz = 1;
our $nlx = pdl(0,0,0,0,0,0);
our @nlx = ();
$ii = 0;
print "initial fit...\n";
# Initial fit -- "basic six" parameters
for(;$ii<$opt->{DIST_FIT_ITER};$ii++) {
$nlx = amoeba($heur, $nlx, $siz/sqrt($ii+1), 1e-6, 1e-9, 1e4);
push(@nlx, $nlx);
}
print "fitted with the basic six -- refiningw with 8....\n";
$nlx = $nlx->append(pdl(0,0));
my $ii_targ = $ii+$opt->{DIST_FIT_ITER};
for(;$ii<$ii_targ;$ii++) {
$nlx = amoeba($heur, $nlx, $siz/sqrt($ii+1), 1e-6, 1e-9, 1e4);
push(@nlx, $nlx);
print "ii=$ii; h=$heur_ret ";
}
# Discard the worst 20% of the distances (ad hoc) to remove occasional missed stars
$dists = (($ct1a-$ct2a)*($ct1a-$ct2a))->sumover->sqrt;
$dqs = $dists->qsorti;
$ct1 = $ct1->(:,$dqs->(0:$dqs->dim(0)*0.8));
$ct2 = $ct2->(:,$dqs->(0:$dqs->dim(0)*0.8));
if($opt->{DIST_NDELAYS}>1){
$dists = (($ct1a_02 - $ct2a_02)*($ct1a_02-$ct2a_02))->sumover->sqrt;
$dqs = $dists->qsorti;
$ct1_02 = $ct1_02->(:,$dqs->(0:$dqs->dim(0)*0.8));
$ct2_02 = $ct2_02->(:,$dqs->(0:$dqs->dim(0)*0.8));
}
my $ii_targ = $ii+2*$opt->{DIST_FIT_ITER};
my $ii0 = $ii;
for(;$ii0<$ii_targ;$ii0++) {
$nlx = amoeba($heur, $nlx, $siz/sqrt($ii+1), 1e-7, 1e-9, 1e4);
push(@nlx, $nlx);
print "r:ii0=$ii0; h=$heur_ret ";
}
print "done!\n";
p "nlx=$nlx\n";
my ($delta1, $delta2, $deltamu, $roll_angle, $r2, $orbital_rotation, $q, $qq) = $nlx->list;
$deltamu /= 10;
$delta1 /= 100;
$delta2 /= 100;
print "saving params...\n";
$params = {
tnl_params=> [$master_hdr, $deltamu, $roll_angle, $r2, $delta1, $delta2, $q, $qq],
params => $nlx,
ospeed => $orbital_rotation * 2 / $hash->{'CORR'}->{'deltat'}, # degrees per day
t0=> ($hash->{'UTIME'}->((-1)) + $hash->{'UTIME'}->((0)))/2,
xr=>$xr,
yr=>$opt->{PIXEL_YRANGE}
};
$hash->{'D_PARAMS'} = $params;
push(@{$hash->{log}}," get_distortion_params2 v$VERSION");
return $hash;
}