-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathaoutil.i
2096 lines (1846 loc) · 70.7 KB
/
aoutil.i
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
/* AOUTIL.I
*
* A collection of utility routines to go with yao.i
*
* This file is part of the yao package, an adaptive optics simulation tool.
*
* Copyright (c) 2002-2017, Francois Rigaut
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details (to receive a copy of the GNU
* General Public License, write to the Free Software Foundation, Inc., 675
* Mass Ave, Cambridge, MA 02139, USA).
*
*/
func yao_tricks(void)
{
write,format="%-78s\n","FLAGS:";
txt = "dispImImav: Image (PSF) display: (0) inst. PSF (1): avg PSF (2) phases";
grow,txt,"display_phase_not_wfs: (bool) Display phases instead of WFS images";
write,format="%-78s\n",txt;
write,"";
write,format="%-78s\n","HOOKS: (see code in yao.i, go() function)";
txt = "go_user_func: called at the start of go(), before entering the loop is go has been called with all=1";
grow,txt,"go2_user_func: called at the start of go(), after entering the loop is go has been called with all=1";
grow,txt,"user_loop_err: called after calculation of err from measurements";
grow,txt,"user_loop_command: called after calculation of command vector";
grow,txt,"";
write,format="%-78s\n",txt;
write,"";
}
func init_image_display(void)
{
if (target._ntarget == 1) {
// if only one target, display correct plate scale for image display
*target.dispzoom = [(*target.lambda)(0)*1e-6/4.848e-6/
tel.diam*sim.pupildiam/sim._size/2*sim._size];
// last 2 terms to accomodate internal dispzoom scaling
}
txpos = *target.xposition;
typos = *target.yposition;
disp2d,im,txpos,typos,1,zoom=*target.dispzoom,init=1;
for (j=1;j<=nwfs;j++) {
// plg,wfs(j).gspos(2),wfs(j).gspos(1),marker='\2',
// type="none",marks=1,color="red";
if (wfs(j).gsalt==0) plp,wfs(j).gspos(2),wfs(j).gspos(1),symbol=8,color="fg",width=2,size=0.6;
else if (wfs(j).gsalt>50000) plp,wfs(j).gspos(2),wfs(j).gspos(1),symbol=8,color="orange",width=2,size=0.6;
else plp,wfs(j).gspos(2),wfs(j).gspos(1),symbol=8,color="green",width=2,size=0.6;
}
myxytitles,"","arcsec",[0.005,0.01],height=12;
plmargin;
if (wfs_display_mode=="spatial") {
disp2d,wfs._dispimage,wfs.pupoffset(1,),wfs.pupoffset(2,),2,\
zoom=wfs.dispzoom,init=1;
} else {
disp2d,wfs._dispimage,wfs.gspos(1,),wfs.gspos(2,),2,zoom=wfs.dispzoom,\
init=1;
}
return 0;
}
func create_yao_window(dpi)
/* DOCUMENT create_yao_window(dpi)
Open or re-open (e.g. after a fork() ) the main and only yao graphical
window.
SEE ALSO:
*/
{
// dpi is in fact already stored in extern (sigh)
if (!dpi) dpi=default_dpi;
if (window_exists(2)) winkill,2;
if (yao_pyk_parent_id) {
// there's a GUI, re-parent within GUI drawingarea:
yao_win_init,yao_pyk_parent_id;
} else {
// there's no GUI, re-open normal graphical window:width=635,height=650
// check if there's one existing of the right size. I'm fed up to see
// yao window pop up everywhere ;-(
need2open = 0;
// is there a window 0?
if (!window_exists(0)) {
need2open=1;
// write,"There is no window 0, need to create";
} else {
// is the size allright?
wg = long(window_geometry(0));
// write,wg(-1:0);
// write,long([635*(dpi/75.),650*(dpi/75.)]);
if (nallof(wg(-1:0)==long([635*(dpi/75.),650*(dpi/75.)]))) {
need2open=1;
} else {
// is there 5 plsys, in which case it's probably a yao window?
window,0;
get_style,l,sys;
if (numberof(sys)!=5) need2open=1;
}
}
if (need2open) {
if (window_exists(0)) winkill,0;
window,0,style="yao.gs",dpi=dpi,width=long(635*(dpi/75.)), \
height=long(650*(dpi/75.)),wait=1;
if ( (xft!=[]) && (xft()) ) {
get_style, landscape, systems, legends, clegends;
systems.ticks.vert.textStyle.height(4)*=1.5;
systems.ticks.horiz.textStyle.height(4)*=1.5;
set_style, landscape, systems, legends, clegends;
}
}
}
plsys,1; limits; limits,square=1;
plsys,2; limits; limits,square=1;
}
//----------------------------------------------------
func xyxy2xxyy(void)
{
offset=0;
for (n=1;n<=nwfs;n++) {
tmp = indgen(wfs(n)._nsub)*2;
tmp = _(tmp,tmp+1);
grow,reordered,tmp+offset;
offset+=wfs(n)._nmes;
}
return reordered;
}
func xxyy2xyxy(void)
{
offset=0;
for (n=1;n<=nwfs;n++) {
grow,reordered,transpose(reform(indgen(wfs(n)._nmes),
[2,wfs(n)._nsub,2]))(*)+offset;
offset+=wfs(n)._nmes;
}
return reordered;
}
//----------------------------------------------------
func get_turb_phase_initCheckOverflow(void)
/* DOCUMENT func get_turb_phase_initCheckOverflow
This routine has the sole purpose of checking the possible "overflow"
of the Y index in the future calls to get_turb_phase. In any of the
Y index at which the interpolation is to be done is larger than the
phase screen(s) Y dimension, then an error is flagged.
SEE ALSO: get_turb_phase_init, get_turb_phase
*/
{
extern screendim;
dimy = screendim(2);
if (max(wfsyposcub(max,,)+yposvec(max,)(,-)) > dimy) {
write,"\nSome of the phase screens are too small (Y dimension) "+
"for the specified system.";
write,format="The following WFS/screens will cause a "+
"index overflow (current Ydim is %d):\n",dimy;
maxind = wfsyposcub(max,,)+yposvec(max,)(,-);
w2 = where2(maxind > dimy);
for (i=1;i<=dimsof(w2)(3);i++) {
write,format="WFS#%d, screen#%d, Ymax=%f\n",w2(2,i),w2(1,i),
maxind(w2(1,i),w2(2,i));
}
write,"To remedy this situation, you can either:";
write," - use larger phase screens (Y axis) using 'create_phase_screens'";
write," - Modify the extremum Y offsets of your WFSs";
write," - Lower the altitude of the offending atmospheric layer";
exit;
}
if (max(gsyposcub(max,,)+yposvec(max,)(,-)) > dimy) {
write,"\nSome of the phase screens are too small (Y dimension) "+
"for the specified system.";
write,format="The following Perf Star/screens will cause a "+
"index overflow (current Ydim is %d):\n",dimy;
maxind = gsyposcub(max,,)+yposvec(max,)(,-);
w2 = where2(maxind > dimy);
for (i=1;i<=dimsof(w2)(3);i++) {
write,format="Perf.Star#%d, screen#%d, Ymax=%f\n",w2(2,i),w2(1,i),
maxind(w2(1,i),w2(2,i));
}
write,"To remedy this situation, you can either:";
write," - use larger phase screens (Y axis) using 'create_phase_screens'";
write," - Modify the Y position of your Perf. Star";
write," - Lower the altitude of the offending atmospheric layer";
exit;
}
}
//----------------------------------------------------
func graphic_config(subsystemnum,dmnum)
/* DOCUMENT func configGraphic(void)
Plots a graphical representation of the system config,
per subsystem and per level (altitude)
subsystemnum and dmnum optional. If not set all subsystems
and dms are displayed
SEE ALSO:
*/
{
t = span(0.,2*pi,100);
col = ["red","blue","green","cyan","magenta","yellow"];
markers = ['1','2','3','4','5','6','7','8','9'];
markers = char(indgen(36)+48); // 1,2,...A,B...
maxcol = numberof(col);
plsys,1;
limits,square=1;
psize = tel.diam/sim.pupildiam;
for (nss=1;nss<=max(dm.subsystem);nss++) {
if ( (subsystemnum != [] ) && (subsystemnum != nss) ) continue;
fma;
// one level by one level
for (i=1;i<=ndm;i++) {
if ( (dmnum != [] ) && (dmnum != i) ) continue;
if ( dm(i).subsystem != nss ) continue;
if ( dm(i).type == "aniso" ) continue;
for (j=1;j<=nwfs;j++) {
if ( wfs(j).subsystem != nss ) continue;
// overplot subaperture position and size for first wfs in this subsystem
first_wfs_plotted = 0;
if ( (first_wfs_plotted==0) && (dm(i).alt==0.) && (dm(i).type!="tiptilt") ) {
for (jj=1;jj<=wfs(j)._nsub4disp;jj++) {
if ((*wfs(j)._validsubs)(jj)==0) continue;
x1 = (*wfs(j)._istart)(jj);
y1 = (*wfs(j)._jstart)(jj);
subsize = sim.pupildiam/wfs(j).shnxsub;
if (wfs(j).npixpersub) subsize = wfs(j).npixpersub;
l1 = subsize;
// convert pixels in meters
x1 = (x1-sim._cent)*tel.diam/sim.pupildiam;
y1 = (y1-sim._cent)*tel.diam/sim.pupildiam;
l1 = l1*tel.diam/sim.pupildiam;
plg,_(y1,y1,y1+l1,y1+l1,y1),_(x1,x1+l1,x1+l1,x1,x1),color=[50,50,50];
}
first_wfs_plotted = 1;
}
}
// radius of outmost actuator in meters:
// rad = (dm(i).nxact-1)/2.*dm(i).pitch*psize;
// plots circle containing all actuators
// plg,rad*sin(t),rad*cos(t),type=2;
// radius of outmost object beam in meter:
tmp = sqrt((*target.xposition)^2.+(*target.yposition)^2.);
maxobjrad = max(tmp);
rad = max(tmp)*4.848e-6*dm(i).alt+tel.diam/2.;
// plots circle containing all rays from objects:
plg,rad*sin(t),rad*cos(t),type=3;
if ( dm(i).type == "stackarray" ) {
// build array containing (x,y) location of valid/controlled actuators:
loc = ([*(dm(i)._x),*(dm(i)._y)]-sim._cent)*psize;
// and plot:
plg,loc(,2),loc(,1),type=0,marker=markers(i),color=col(i%maxcol);
if (numberof(*dm(i)._ex) != 0) {
// build array containing (x,y) location of extrapolated actuators:
loc = ([*(dm(i)._ex),*(dm(i)._ey)]-sim._cent)*psize;
// and plot:
plg,loc(,2),loc(,1),type=0,marker=markers(i);
}
}
// loop on sensor:
for (j=1;j<=nwfs;j++) {
if ( wfs(j).subsystem != nss ) continue;
// computes scaling factor due to limited altitude:
if (wfs(j)._gsalt != 0) {
fact = (wfs(j)._gsalt-dm(i).alt)/wfs(j)._gsalt;
} else {
fact=1;
}
offsets = wfs(j).gspos*4.848e-6*dm(i).alt;
rad=tel.diam/2.*fact;
// plotting beam shape for this wfs at this dm altitude:
plg,offsets(2)+rad*sin(t),offsets(1)+rad*cos(t),color=col(i%maxcol),width=3;
}
myxytitles,"Beam outline [m]","Beam outline [m]",[0.0,0.0];
mypltitle,swrite(format="Patches of guide star beams on DM#%d, Subsystem %d",i,nss);
comment = swrite(format="Configuration actuator/beams in a plane at altitude %.0f\n"+
"Solid lines: Outline of this subsystem GS beams\n"+
"Dotted line: Circle including outermost ray to specified science \n"+
" objects (at %.1f arcsec off-center)\n"+
"Numbers for stackarrays actuators: colored:controlled, BW: extrapolated\n"+
"# of active actuators= %d, # of extrapolated actuators=%d",
dm(i).alt,maxobjrad,dm(i)._nact,dm(i)._enact);
plt,comment,0.06,0.22,tosys=0,justify="LT";
plt,sim.name,0.01,0.227,tosys=0;
limits; lim=limits(); limits,lim(1)*1.1,lim(2)*1.1,lim(3)*1.1,lim(4)*1.1;
hcp;
if (sim.debug >=1) typeReturn;
fma;
}
if ( subsystemnum != [] ) continue;
// all levels
for (i=1;i<=ndm;i++) {
if ( dm(i).subsystem != nss ) continue;
if ( dm(i).type == "aniso" ) continue;
// radius of outmost actuator in meters:
rad = (dm(i).nxact-1)/2.*dm(i).pitch*psize;
// plots circle containing all actuators
plg,rad*sin(t),rad*cos(t),type=i;
if ( dm(i).type == "stackarray" ) {
// build array containing (x,y) location of valid actuators:
loc = ([*(dm(i)._x),*(dm(i)._y)]-sim._cent)*psize;
// and plot:
plg,loc(,2),loc(,1),type=0,marker=markers(i),color=col(i%maxcol);
}
// loop on sensor:
for (j=1;j<=nwfs;j++) {
if ( wfs(j).subsystem != nss ) continue;
// computes scaling factor due to limited altitude:
if (wfs(j)._gsalt != 0) {
fact = (wfs(j)._gsalt-dm(i).alt)/wfs(j)._gsalt;
} else {
fact=1;
}
offsets = wfs(j).gspos*4.848e-6*dm(i).alt;
rad=tel.diam/2.*fact;
// plotting beam shape for this wfs at this dm altitude:
plg,offsets(2)+rad*sin(t),offsets(1)+rad*cos(t),color=col(i%maxcol),width=3;
}
}
myxytitles,"Beam outline [m]","Beam outline [m]",[0.0,0.0];
mypltitle,swrite(format="Patches of guide star beams on DMs, Subsystem %d",nss);
plt,sim.name,0.01,0.227,tosys=0;
limits; lim=limits(); limits,lim(1)*1.1,lim(2)*1.1,lim(3)*1.1,lim(4)*1.1;
hcp;
if (sim.debug >=1) typeReturn;
}
}
//----------------------------------------------------
func check_parameters(void)
/* DOCUMENT func check_parameters(void)
Check the parameters in yao parfile
- set defaults
- value valid?
- compatibility with other parameters
SEE ALSO:
*/
{
extern sim,atm,wfs,dm,mat,tel,target,gs,loop;
if (sim.verbose) write,format="Checking parameters ... %s\n","";
//==================================================================
// BASIC EXISTENCE AND CONSISTENCY CHECKINGS AND DEFAULT ASSIGNMENTS
//==================================================================
extern nwfs,ndm,ntarget,noptics;
nwfs = numberof(wfs);
ndm = numberof(dm);
ntarget = numberof(*target.lambda);
noptics = numberof(opt);
// SIM STRUCTURE
if (sim.pupildiam == 0) {exit,"sim.pupildiam has not been set";}
if ( ((sim.svipc>>2)&1) && (!((sim.svipc>>0)&1)) ) {
write,"If you have (sim.svipc>>2)&1, you should have (sim.svipc>>0)&1";
write,"Setting sim.svipc first bit to 1"
sim.svipc++;
pause,2000;
}
if (sim.svipc_wfs_nfork>nwfs) {
write,"sim.svipc_wfs_nfork > nwfs, setting sim.svipc_wfs_nfork = nwfs"
sim.svipc_wfs_nfork = nwfs;
pause,2000;
}
// ATM STRUCTURE
if ((*atm.screen) == []) {exit,"atm.screen has not been set";}
if (typeof(*atm.screen) != "string") {exit,"*atm.screen is not a string";}
// Check that all phase screens are different. If the same file is repeated, this makes the turbulence much worse than what is specified by r0!
ftmp = *atm.screen;
for (i=1;i<=numberof(ftmp);i++){
if (sum(ftmp == (ftmp)(i)) > 1){
write, "atm.screen must not have repeated values";
write, "doing so changes the statistics of the atmospheric turbulence";
error,"exiting";
}
}
for (i=1;i<=numberof(ftmp);i++) {
if (!open(ftmp(i),"r",1)) { // file does not exist
msg = swrite(format="Phase screen %s not found!\\n"+
"You need to generate phase screens for yao.\\n"+
"Go to \"Phase Screen -> Create phase Screen\"\\n"+
"If you already have phase screens, you can modify\\n"+
"the path in the parfile atm.screen definition",ftmp(i));
if (_pyk_proc) pyk,"set_cursor_busy(0)";
pyk_warning,msg;
msg = swrite(format="\nWARNING: %s not found!\nEdit the par file and change the "+
"path,\nand/or run \"Phase Screen -> Create phase Screen\"\n",ftmp(i));
write,msg;
break;
}
}
if ((*atm.layerfrac) == []) {exit,"atm.layerfrac has not been set";}
if ((*atm.layerspeed) == []) {exit,"atm.layerspeed has not been set";}
if ((*atm.layeralt) == []) {exit,"atm.layeralt has not been set";}
if ((*atm.winddir) == []) {exit,"atm.winddir has not been set";}
if (nallof(_(numberof(*atm.screen),numberof(*atm.layerfrac),numberof(*atm.layerspeed), \
numberof(*atm.layeralt)) == numberof(*atm.winddir))) {
exit,"Some elements within atm.screen, layerfrac, layerspeed, layeralt \n"+\
"or winddir do not have the same number of elements.";
}
// WFS STRUCTURE
for (ns=1;ns<=nwfs;ns++) {
if (wfs(ns).type == string()) {
exit,swrite(format="wfs(%d).type has not been set",ns);
}
// if (wfs(ns).subsystem == 0) {wfs(ns).subsystem = 1;}
if (wfs(ns).lambda == 0) {
exit,swrite(format="wfs(%d).lambda has not been set",ns);
}
//~ if (wfs(ns).dispzoom == 0) {wfs(ns).dispzoom = 1;}
if ((wfs(ns).type == "curvature") && ((*wfs(ns).nsubperring) == [])) {
write,format="wfs(%d).nsubperring has not been set.\n",ns;
write,"Valid values are, for example:";
exit,"wfs(n).nsubperring = &([6,12,18]);";
}
if ((wfs(ns).type == "curvature") && (wfs(ns).l == 0)) {
exit,swrite(format="wfs(%d).l has not been set",ns);
}
if ((wfs(ns).type == "curvature") && (wfs(ns).gsdepth > 0)) {
exit,swrite(format="wfs(%d): gsdepth not implemented for CWFS, sorry",ns);
}
if ((wfs(ns).type == "curvature") && (wfs(ns).rayleighflag == 1)) {
exit,swrite(format="wfs(%d): Rayleigh not implemented for CWFS, sorry",ns);
}
if ((wfs(ns).gsalt == 0) && (wfs(ns).rayleighflag == 1)) {
write,swrite(format="wfs(%d): gsalt = 0 and Rayleigh flag is set !",ns);
exit,"Rayleigh is not computed for NGS sensors, sorry.";
}
if ((wfs(ns).gsalt > 0) && (wfs(ns).laserpower == 0)) {
exit,swrite(format="wfs(%d): this is a LGS and you haven't set wfs.laserpower",ns);
}
if ((wfs(ns).type == "curvature") && (wfs(ns).fieldstopdiam == 0)) {
wfs(ns).fieldstopdiam = 1.f;
}
if ((wfs(ns).type == "hartmann") && (wfs(ns).shmethod == 0)) {
exit,swrite(format="wfs(%d).shmethod has not been set",ns);
}
if ((wfs(ns).type == "hartmann") && (wfs(ns).shnxsub == 0)) {
exit,swrite(format="wfs(%d).shnxsub has not been set",ns);
}
if ((wfs(ns).type == "hartmann") && (wfs(ns).shmethod == 2)) {
if ((wfs(ns).type == "hartmann") && (wfs(ns).pixsize == 0)) {
exit,swrite(format="wfs(%d).pixsize has not been set",ns);
}
if ((wfs(ns).type == "hartmann") && (wfs(ns).npixels == 0)) {
exit,swrite(format="wfs(%d).npixels has not been set",ns);
}
if ((wfs(ns).type == "hartmann") && (wfs(ns).shthmethod == 0)) {
wfs(ns).shthmethod = 1;
}
if ((wfs(ns).type == "hartmann") && (wfs(ns).shthmethod == 3) &&
((wfs(ns).shthreshold > (wfs(ns).npixels)^2) || (wfs(ns).shthreshold <= 0))) {
exit,swrite(format="Wrong wfs(%d).shthreshold value for wfs(%d).shthmethod = %d",ns,ns,wfs(ns).shthmethod);
}
}
if ((wfs(ns).type == "hartmann") && (wfs(ns).shmethod == 2)) {
if ((strlen(wfs(ns).fsname)>0) && (strlen(wfs(ns).fstop)>0)) {
write,format="You have set both wfs(%d).fsname and wfs(%d).fstop\n",ns,ns;
write,format="I will ignore wfs(%d).fstop and use wfs(%d).fsname !\n",ns,ns;
}
if ((strlen(wfs(ns).fsname)==0) && (strlen(wfs(ns).fstop)==0)) {
write,format="No field stop defined for wfs %d. Setting to 'square'\n",ns;
wfs(ns).fstop = "square";
}
if (wfs(ns).fssize==0) {
write,format="wfs(%d).fssize has not been set, will be forced to subap FoV\n",ns;
}
}
if ((wfs(ns).type != "hartmann")&&(wfs(ns).LLT_uplink_turb)) {
write,format="WARNING, wfs#%d: LLT_uplink_turb only implemented for SHWFS, ignoring.\n",ns;
}
if (wfs(ns).LLT_uplink_turb) {
if ((wfs(ns).LLTr0==0)) {
write,format="WARNING, wfs(%d).LLTr0 undefined, Using atm-defined r0\n",ns;
wfs(ns).LLTr0 = tel.diam/atm.dr0at05mic * (wfs(ns).lambda/0.5)^1.2;
}
if ((wfs(ns).LLTdiam==0)) \
error,swrite(format="wfs#%d: LLT_uplink_turb set but LLTdiam not defined",ns);
if ((wfs(ns).LLT1overe2diam==0)) \
error,swrite(format="wfs#%d: LLT_uplink_turb set but LLT1overe2diam not defined",ns);
}
if (wfs(ns).nintegcycles == 0) {wfs(ns).nintegcycles = 1;}
if (wfs(ns).fracIllum == 0) {wfs(ns).fracIllum = 0.5;}
if (wfs(ns).optthroughput == 0) {wfs(ns).optthroughput = 1.0;}
if (wfs(ns).svipc>1) {
if (wfs(ns).type!="hartmann") {
write,format="wfs(%d).svipc >1 only for SHWFS, will have no effect\n",ns;
wfs(ns).svipc = 0;
} else if (wfs(ns).shnxsub < 2) {
write,format="wfs(%d).svipc >1 but SH 1x1 sub. Disabling ||.\n",ns;
wfs(ns).svipc = 0;
}
}
if (wfs(ns).type == "pyramid") {
if ((wfs(ns).pyr_mod_loc!="after") && (wfs(ns).pyr_mod_loc!="before")) {
write,format="wfs(%d).pyr_mod_loc is not set, setting to \"after\"\n",ns;
}
}
// DMs in this WFS path. Default is for all DM to be in path:
wfs(ns)._dmnotinpath = &array(0n,ndm);
if (wfs(ns).dmnotinpath) {
if (anyof(*wfs(ns).dmnotinpath>ndm)||anyof(*wfs(ns).dmnotinpath<1)) \
error,swrite(format="Issue with WFS#%d dmnotinpath definition (indices)",ns);
(*wfs(ns)._dmnotinpath)(int(*wfs(ns).dmnotinpath)) = 1n;
}
wfs(ns).ron = float(wfs(ns).ron);
if ((wfs(ns).type == "hartmann") && (wfs(ns).shmethod == 1) && (wfs(ns).noise == 1) && (wfs(ns).ron > 0.)){
write, format = "WARNING: wfs(%d).shmethod = 1 and wfs(%d).noise = 1\n",ns,ns;
write, format = "This feature produces a measurement error in wfs(%d) of wfs(%d).ron arcsec\n",ns,ns;
}
if (!wfs(ns).lgs_prof_amp) wfs(ns).lgs_prof_amp = &float([0.]);
if (!wfs(ns).lgs_prof_alt) wfs(ns).lgs_prof_alt = &float([0.]);
if (numberof(*wfs(ns).lgs_prof_amp)!=numberof(*wfs(ns).lgs_prof_alt)) {
error,"numberof(*wfs(ns).lgs_prof_amp)!=numberof(*wfs(ns).lgs_prof_alt)";
}
if (wfs(ns).minzer == 0) {
wfs(ns).minzer = 1;
}
}
// DM STRUCTURE
for (nm=1;nm<=ndm;nm++) {
if (dm(nm).type == string()) {
exit,swrite(format="dm(%d).type has not been set",nm);
}
// disk harmonic type string has changed. for compatibility
// with old API:
if (dm(nm).type == "diskharmonic") dm(nm).type = "dh";
if (dm(nm).subsystem == 0) {dm(nm).subsystem = 1;}
if (dm(nm).iffile == string()) {dm(nm).iffile = "";}
if (dm(nm).ecmatfile == string()) {dm(nm).ecmatfile = "";}
if (dm(nm).push4imat == 0) {dm(nm).push4imat = 20;}
if (dm(nm).filterpiston == 0) {dm(nm).filterpiston = 1;}
if (dm(nm).gain == 0) {write,format=" WARNING: dm(%d).gain set to 0\n",nm;}
if (dm(nm).unitpervolt == 0) {
write,format=" WARNING: dm(%d).unitpervolt set to 0\n",nm;
// below: this is stupid. but I don't dare to change it now (2011mar16)
if ( (dm(nm).type == "tiptilt") || (dm(nm).type == "zernike")) {
dm(nm).unitpervolt = 0.0005;
} else if ( (dm(nm).type == "bimorph") || (dm(nm).type == "dh")) {
dm(nm).unitpervolt = 1.0;
} else {
dm(nm).unitpervolt = 0.01;
}
write,format=" WARNING: dm(%d).unitpervolt overwritten to %f\n",nm,dm(nm).unitpervolt;
}
if ( (dm(nm).type == "bimorph") && ((*dm(nm).nelperring) == []) ) {
write,format="dm(%d).nelperring has not been set.\n",nm;
write,"Valid values are, for example:";
exit,"dm(n).nelperring = &([6,12,18]);";
}
if ( (dm(nm).type == "stackarray") && (dm(nm).nxact == 0) ) {
exit,swrite(format="dm(%d).nxact has not been set",nm);
}
if ( (dm(nm).type == "stackarray") && (dm(nm).pitch == 0) ) {
exit,swrite(format="dm(%d).pitch has not been set",nm);
}
if (dm(nm).type=="stackarray") {
if ((dm(nm).coupling<0.0) || (dm(nm).coupling>0.8)) {
write,format="Invalid value for dm(%d).coupling -> %f\n",nm,dm(nm).coupling;
exit,"Valid values from 0 to 0.80";
}
}
if ( (dm(nm).type == "zernike") && (dm(nm).nzer == 0) ) {
exit,swrite(format="dm(%d).nzer has not been set",nm);
}
if ( (dm(nm).type == "dh") && (dm(nm).ndh == 0) ) {
exit,swrite(format="dm(%d).ndh has not been set",nm);
}
if (dm(nm).minzer == 0) {
dm(nm).minzer = 1;
}
if ( (dm(nm).filtertilt) && (noneof(dm(nm).type == ["zernike","stackarray"]) )) {write, "WARNING: filtertilt only defined for zernike and stackarray DMs";}
if ( (dm(nm).type == "kl") && (dm(nm).nkl == 0) ) {
exit,swrite(format="dm(%d).nkl has not been set",nm);
}
if (dm(nm).irexp==1) {
if (dm(nm).irfact == 0.) {
dm(nm).irfact = 1.;
write,format="dm(%d).irfact set to %f\n",nm,dm(nm).irfact;
}
}
// check that laplacian only applies to stackarrays
if ((dm(nm).regtype == "laplacian") && (dm(nm).type != "stackarray")){
exit, "regtype can only be set to laplacian for stackarray DMs";
}
// set the regparam for stack arrays to something other than 0
if ((dm(nm).type == "stackarray") && (dm(nm).regparam == 0)){
write,"Setting regparam to 1e-5";
dm(nm).regparam = 1e-5;
}
// check that any virtual DMs have a lower DM number than a DM that uses it
if (*dm(nm).dmfit_which != []){
if (max(*dm(nm).dmfit_which) > nm){
write,format="Virtual DMs (%d) must have a lower numbering than the DM (%d) that fits to them\n",max(*dm(nm).dmfit_which),nm;
exit, "Exiting";
}
}
if ((dm(nm).hyst < 0.)+(dm(nm).hyst > 0.25)){exit,"DM hysteresis must be between 0 and 0.25";}
if (dm(nm).hyst > 0.){write,"WARNING: DM hysteresis implementation assumes that voltages are of the order of 0.1 to 10. If not, do not use hysteresis.";}
}
nmaniso = where(dm.type == "aniso");
if (numberof(nmaniso) > 1){
exit, "The number of DMs with dm.type == aniso must be 0 or 1";
}
if (numberof(nmaniso) == 1){
nmaniso = nmaniso(1);
// use dm.dmfit_which to specify which DMs these modes are projected onto
dmfit = dm(nmaniso).anisodmfit;
if (numberof(dmfit) != 2){
// the user needs to specify two DMs at different altitudes
exit,"Exactly 2 DMs must be specified in anisodmfit for aniso DM";
}
}
// MAT STRUCTURE
if (mat.method == string()) {mat.method = "svd";};
if (mat.method == "svd"){
if ((*mat.condition) == []) {exit,"mat.condition has not been set";}
if (numberof(*mat.condition) != max(_(wfs.subsystem,dm.subsystem)) ) {
exit,"dimension of *mat.condition is not equal to the number of subsystems";
}
}
if (mat.file == string()) {mat.file = "";}
if (mat.sparse_MR == long()){mat.sparse_MR = 10000;}
if (mat.sparse_MN == long()){mat.sparse_MN = 200000;}
if (mat.sparse_thresh == float()){mat.sparse_thresh = 1e-8;}
if (mat.sparse_pcgtol == float()){mat.sparse_pcgtol = 1e-6;}
if (mat.fit_subsamp == long()){mat.fit_subsamp = 1;}
if (mat.fit_minval == float()){mat.fit_minval = 1e-2;}
// TEL STRUCTURE
if (tel.diam == 0) exit,"tel.diam has not been set";
// TARGET STRUCTURE
if ((*target.lambda) == []) exit,"target.lambda has not been set";
target.lambda = &(float(*target.lambda)); // *target.lambda must be floats
if ((*target.xposition) == []) exit,"target.xposition has not been set";
if ((*target.yposition) == []) exit,"target.yposition has not been set";
if ((*target.dispzoom) == []) {
target.dispzoom = &(array(1.,numberof(*target.xposition)));
}
if (nallof(_(numberof(*target.xposition), \
numberof(*target.yposition)) == numberof(*target.dispzoom) )) {
exit,"Some elements within target.xposition, yposition, dispzoom "+\
"do not have the same number of elements.";
}
// GS STRUCTURE
if (gs.zenithangle > 0 && anyof(wfs.gsalt > 0)){write,"WARNING: The return from the LGS is assumed to vary as cos(gs.zenithangle).";}
if (anyof(wfs.gsalt != 0) && (gs.lgsreturnperwatt == 0)) {
gs.lgsreturnperwatt = 22.;
write,format="gs.lgsreturnperwatt set to %f\n",gs.lgsreturnperwatt;
}
if (anyof((wfs.gsalt == 0)*(wfs.zeropoint == 0)) && (gs.zeropoint == 0)) {
exit,"You have some NGS and gs.zeropoint has not been set";
}
if (anyof((wfs.gsalt != 0)*(wfs.zeropoint == 0)*(wfs.skymag != 0)) && (gs.zeropoint == 0)) {
exit,"You have some LGS with sky background and gs.zeropoint has not been set";
}
// LOOP STRUCTURE
if (loop.method == string()) loop.method = "closed-loop";
loop_method_options = ["closed-loop","open-loop","pseudo open-loop","none"];
if (noneof(loop_method_options == loop.method)) exit,"ERROR: loop.method should be set to closed-loop, open-loop or pseudo open-loop";
if ((loop.method == "open-loop") && (loop.gain != 1 || loop.leak != 1)){
write, "Warning: For open-loop simulations the recommended settings are";
write, "loop.gain = 1. and loop.leak = 1.";
}
if (loop.gain == 0) write,format="%s\n","Warning: loop.gain = 0";
if ( (numberof(*loop.gainho)) != (numberof(*loop.leakho)) ) \
exit,"*loop.gainho should have same number of elements as *loop.leakho";
if (loop.niter == 0) exit,"loop.niter has not been set";
if (loop.ittime == 0) exit,"loop.ittime has not been set";
if (loop.startskip == 0) loop.startskip = 10;
if (loop.skipby == 0) loop.skipby = 10000;
if (loop.modalgainfile == string()) loop.modalgainfile = "";
if (loop.stats_every==0) loop.stats_every=4;
//============================================================================
// DONE WITH BASIC EXISTENCE AND CONSISTENCY CHECKINGS AND DEFAULT ASSIGNMENTS
//============================================================================
// NOW GOING INTO MORE ELABORATE CHECKINGS
// WFSs:
for (ns=1;ns<=nwfs;ns++) {
if (wfs(ns).zeropoint == 0){
wfs(ns)._zeropoint = gs.zeropoint;
} else {
wfs(ns)._zeropoint = wfs(ns).zeropoint;
}
if (wfs(ns).framedelay == -1){
wfs(ns)._framedelay = loop.framedelay;
} else {
wfs(ns)._framedelay = wfs(ns).framedelay;
if ((sim.verbose) && (loop.framedelay != wfs(ns).framedelay)){
write, format="Using a loop delay of %d frames for wfs(%d)\n",wfs(ns).framedelay, ns;
}
}
if (wfs(ns).lambda < 0.1) {
write,format="WFS#%d: wfs.lambda < 0.1. That seems weird.\n",ns;
write,"Remember: lambda should be in microns";
exit;
}
if (wfs(ns).shthreshold < 0.) {
write,format="WFS#%d: wfs.shthreshold < 0 does not make sense\n",ns;
exit;
}
if ((wfs(ns).filtertilt == 1) && (wfs(ns).correctUpTT == 0)) {
write,format="WARNING! WFS#%d: wfs.correctUpTT = 0 with wfs.filtertilt = 1\n",ns;
}
if ((wfs(ns).correctUpTT == 1) && (wfs(ns).uplinkgain == 0)) {
write,format="WARNING! WFS#%d: wfs.correctUpTT = 1 but wfs.uplinkgain = 0\n",ns;
}
// for shmethod = 1, we still need to set npixels and pixsize to avoid crashing the
// WFS initialization routine that is still used to set other parameters used with
// method 1. So we put DUMMY VALUES.
if (wfs(ns).shmethod == 1) {
if (wfs(ns).pixsize == 0){wfs(ns).pixsize = 0.1;}
if (wfs(ns).npixels == 0){wfs(ns).npixels = 2;}
}
if ( (wfs(ns).type=="curvature") && (wfs(ns).nintegcycles != 1) ) {
write,"\n I have not implemented yet nintegcycle > 1 for curvature WFS.";
write,"I can see no reason to do it, as curvature sensors are usually";
write,"read-out noise free, therefore reducing the gain should be";
write,"prefered. If you have a compelling reason and want it, drop";
write,"me an email.";
exit;
}
// Are we using a WFS we know?
wfs_type = strtolower(wfs(ns).type);
if ( (wfs_type != "curvature") && (wfs_type != "hartmann") &&
(wfs_type != "zernike") && (wfs_type !="kl") &&
(wfs_type != "pyramid") && (wfs_type !="dh")) {
// check if this is a user supplied function
cmd = swrite(format="totype = typeof(%s)",wfs(ns).type);
include,[cmd],1;
if ( totype != "function") {
error,swrite(format="wfs(%d).type : Unknown value or non-existing function \"%s\"",
ns,wfs(ns).type)
}
} else wfs(ns).type = wfs_type;
}
// Sets the Influence function file name if not set:
for (nm=1;nm<=ndm;nm++) {
if (dm(nm).iffile == "") {
dm(nm).iffile = parprefix+swrite(format="-if%d",nm)+".fits";
write,format="dm(%d).iffile set to %s\n",nm,dm(nm).iffile;
}
if (dm(nm).ecmatfile == "") {
dm(nm).ecmatfile = parprefix+swrite(format="-ecmat%d",nm)+".fits";
}
if ((dm(nm).type != "stackarray") && (dm(nm).elt == 1)) {
exit,swrite(format="DM %d: parameter dm.elt only used with stackarray mirrors\n",nm);
}
// Are we using a DM we know?
dm_type = strtolower(dm(nm).type);
if ( (dm_type != "bimorph") && (dm_type != "stackarray") &&
(dm_type != "zernike") && (dm_type != "kl") &&
(dm_type != "segmented") && (dm_type != "dh") &&
(dm_type != "tiptilt") && (dm_type != "aniso") ) {
// check if this is a user supplied function
cmd = swrite(format="totype = typeof(%s)",dm(nm).type);
include,[cmd],1;
if ( totype != "function") {
error,swrite(format="dm(%d).type : Unknown value or non-existing function \"%s\"",
nm,dm(nm).type)
}
} else dm(nm).type = dm_type;
dm(nm)._eiffile = parprefix+swrite(format="-if%d",nm)+"-ext.fits";
}
for (i=1;i<=max(_(wfs.subsystem,dm.subsystem));i++) {
if (noneof(wfs.subsystem == i)) {
exit,swrite(format="There is no WFS in subsystem %d\n",i);
}
if (noneof(dm.subsystem == i)) {
exit,swrite(format="There is no DM in subsystem %d\n",i);
}
}
// Sets the interaction/command matrix file name if not set:
if (mat.file == "") {mat.file = parprefix+"-mat.fits";}
// # of targets (stars at which the performance are evaluated):
target.xposition = &(float(*target.xposition));
target.yposition = &(float(*target.yposition));
target._ntarget = numberof(*target.xposition);
target._nlambda = numberof(*target.lambda);
if (anyof((*target.lambda) < 0.1)) {
write,"Some or all target.lambda < 0.1. That seems weird.";
write,"Remember: lambda should be in microns";
exit;
}
if (opt!=[]) {
for (no=1;no<=noptics;no++) {
if (opt(no).misreg==[]) {
noptics = numberof(opt(no).phasemaps);
for (i=1;i<=noptics;i++) opt(no).misreg = [0.,0.];
}
opt(no).misreg= float(opt(no).misreg);
if ((opt(no).path_type=="wfs")&&(opt.path_which==[])) opt.path_which = &indgen(nwfs);
if ((opt(no).path_type=="target")&&(opt.path_which==[])) opt.path_which = &indgen(ntarget);
if ((opt(no).path_type)&&(opt(no).path_type=="")) opt(no).path_type="common";
if ((opt(no).path_type)&&(opt(no).path_type!="wfs")&&\
(opt(no).path_type!="target")&&(opt(no).path_type!="common")) \
error,swrite(format="Unknown optics path \"%s\".\n It should be \"common\", \"wfs\" or \"target\" (default=\"common\")",opt(no).path_type);
}
}
if (sim.verbose) write,format="%s\n","Check parameters: OK";
}
//----------------------------------------------------
func disp2d(ar,xpos,ypos,area,zoom=,power=,init=,nolimits=)
/* DOCUMENT func disp2d(arrayptr,xpos,ypos,area,zoom=,power=,init=)
display several images in the same plsys, at position given
by xpos and ypos.
ar: ar can be either an array of pointers or an image cube
xpos,ypos: the (X,Y) positions, in arbitrary coordinates
area: plsys number
zoom: keyword (vector, dim nim), additional zooming factor (on top of default)
power: *arrayptr^power is displayed
init: initialize, precompute stuff
SEE ALSO:
*/
{
extern basezoomptr;
plsys,area;
if (typeof(ar) == "pointer") {
cas = "ptr";
nim = numberof(ar);
earlyExit = (*ar(1) == []); // in case we call init with ar undefined (legitimate)
} else {
cas = "cube";
tmp = dimsof(ar);
if (tmp(1) == 2) {nim=1;} else {nim=tmp(4);}
}
if (is_set(init)) {
if (basezoomptr == []) {
basezoomptr=array(pointer,10);
}
if ((zoom != []) && (numberof(zoom) != nim)) {zoom = array(zoom,nim);}
if (zoom == []) {zoom = array(1.,nim);}
xd = abs(xpos-xpos(-,));
yd = abs(ypos-ypos(-,));
di = sqrt(xd^2.+yd^2.);
di = di+unit(nim)*max(di);
w = where(zoom!=0);
basezoom = (1.+0.9*min(di(w))/2.)*zoom;
basezoomptr(area) = &basezoom;
if (!is_set(nolimits)) {
limits,min(xpos(w)-basezoom(w)),max(xpos(w)+basezoom(w)),
min(ypos(w)-basezoom(w)),max(ypos(w)+basezoom(w)),square=1;
}
if (earlyExit) return;
}
basezoom = *basezoomptr(area);
if ( cas=="ptr") {
if (!is_set(power)) {
for (i=1;i<=nim;i++) {
if (basezoom(i)==0) continue;
off = basezoom(i)/(dimsof(*ar(1))(2));
pli,bytscl(*ar(i),cmin=0),xpos(i)-basezoom(i)-off,ypos(i)-basezoom(i)-off,
xpos(i)+basezoom(i)-off,ypos(i)+basezoom(i)-off;
}
} else {
for (i=1;i<=nim;i++) {
if (basezoom(i)==0) continue;
off = basezoom(i)/(dimsof(*ar(1))(2));
pli,bytscl(*ar(i)^power,cmin=0),xpos(i)-basezoom(i)-off,ypos(i)-basezoom(i)-off,
xpos(i)+basezoom(i)-off,ypos(i)+basezoom(i)-off;
}
}
} else {
if (!is_set(power)) {
for (i=1;i<=nim;i++) {
if (basezoom(i)==0) continue;
off = basezoom(i)/(dimsof(ar(,,1))(2));
pli,bytscl(ar(,,i),cmin=0),xpos(i)-basezoom(i)-off,ypos(i)-basezoom(i)-off,
xpos(i)+basezoom(i)-off,ypos(i)+basezoom(i)-off;
}
} else {
for (i=1;i<=nim;i++) {
if (basezoom(i)==0) continue;
off = basezoom(i)/(dimsof(ar(,,1))(2));
pli,bytscl(ar(,,i)^power,cmin=0),xpos(i)-basezoom(i)-off,ypos(i)-basezoom(i)-off,
xpos(i)+basezoom(i)-off,ypos(i)+basezoom(i)-off;
}
}
}
if ((is_set(init)) && (!is_set(nolimits))) {
limits,square=1;
limits;
}
}
//--------------------------------------------------------------------------
func modal_gain_optimization(disp=,update=)
/* DOCUMENT func modal_gain_optimization(disp=,update=)
NOT UPGRADED TO VERSION 2.
DO NOT USE.
This routine optimizes the modal gains.
Keywords:
disp: set to display stuff
update: set if this is a gain update (opposite to the first time run)
This routine uses:
- the saved error circular buffer (cberr.fits)
This routine calls:
...
This routine sets:
...
SEE ALSO:
*/
{
cberr = yao_fitsread("cberr.fits"); // CB of actuator error
nGoodSamples = long(2^floor(log(ao.LoopNIter-ao.LoopStartSkip)/log(2)));
cbmoderr = atm(,+)*cberr(+,1-nGoodSamples:);// CB of mode coef errors