-
Notifications
You must be signed in to change notification settings - Fork 1
/
m3_smart_mosaic_start.pro
1578 lines (1556 loc) · 41.7 KB
/
m3_smart_mosaic_start.pro
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
pro m3_smart_mosaic_get_mosaic_rule, coefficients
;
; asks the user for the way in which the mosaic is built (which pixels survive in
; cases of multiple coverages)
;
;--------------------------------------------------------------------------------------
compile_opt strictarr, hidden
;
; for now just use the obs band names
;
list= ['To-Sun Azimuth', 'To-Sun Zenith', $
'To-M3 Azimuth', 'To-M3 Zenith', $
'Phase', 'To-Sun Path Length', $
'To-M3 Path Length', 'Facet Slope', $
'Facet Aspect', 'Facet Cos(i)']
nlist=n_elements(list)
coefficients=intarr(nlist)
;
; use auto-managed widget for now
;
tlb=widget_auto_base(title='M3 Smart Mosaic')
sb1=widget_base(tlb,/row)
wsl=widget_slist(tlb,list=list,prompt='Select Rule to use:',uvalue='slist',/auto)
sb2=widget_base(tlb,/row)
text='Then choose to use either minimum or maximum for this rule:'
wtxt=widget_label(sb2,value=text, xsize=225)
sb3=widget_base(tlb,/row)
list=['Minimum','Maximum']
wtog=widget_toggle(sb3,uvalue='toggle',list=list, /auto)
result=auto_wid_mng(tlb)
if result.accept eq 0 then return
;
; set coefficients according to user preference (need to be -1 for max and +1 for min
;
coefficients[result.slist] = -1
if result.toggle eq 0 then coefficients=coefficients * (-1)
;
end
pro m3_smart_mosaic_pick_time_range_and_region_type, region_type, op_selected
;
; 1. Lets user pick an optical period, then time ranges (from index files) in play
; 2. Lets user decide to draw roi or specify lac chart
;
; *note: for now assume #1 is all files in OP1B, so only implement #2
;
;------------------------------------------------------------------------------------
compile_opt strictarr,hidden
;
; use auto-managed widget for now
;
tlb=widget_auto_base(title='M3 Smart Mosaic')
sb=widget_base(tlb,/column)
op_list=['OP1A','OP1B','OP2A','OP2B','OP2C']
wm=widget_menu(sb,prompt='Pick OP to mosaic from:',list=op_list,uvalue='op_sel',/excl,/auto)
sb1=widget_base(tlb,/row)
text='Select the way you want to do your mosaic:'
wtxt=widget_label(sb1,value=text, xsize=225)
sb2=widget_base(tlb,/row)
list=['Draw an ROI','Select a LAC Chart(s)']
wtog=widget_toggle(sb2,uvalue='toggle',list=list, /auto)
result=auto_wid_mng(tlb)
if result.accept eq 0 then return
;
case result.toggle of
0:region_type='user_drawn'
1:region_type='lac_based'
endcase
;
op_selected=op_list[result.op_sel]
;
end
pro m3_smart_mosaic_pick_lacs_event, event
;
; get state structure containing all the info we need
;
widget_control, event.top, get_uvalue=pinfo
info=*pinfo
;
; handle events based on which widget id we receive
;
case event.id of
info.okID: begin
;
; check to make sure at least one lac is on
;
mask=info.mask
idx=where(mask gt 0,count)
if count eq 0 then begin
msg='You did not select any lac charts. Try again or press cancel.'
ok=dialog_message(msg,/error)
return
endif
;
; store current list of selected lac charts in a pointer
;
widget_control, info.bgroup, get_value=bgroup_vector
info.lacs_chosen = bgroup_vector
*pinfo=info
;
; destroy widget
;
widget_control, event.top, /destroy
;
end
info.cancelID: begin
;
; cleanup
;
widget_control, event.top, /destroy
end
info.bgroup: begin
;
; get new list of what's selected
;
widget_control, info.bgroup, get_value=index
selected=where(index eq 1,count)+1
;
; update draw widget
;
if count gt 0 then begin
;
; calculate new mask
;
bp=info.bp
mask=bp*0
for c=0,count-1 do begin
addr=where(bp eq selected[c])
mask[addr]=255
endfor
;
; update state pointer and display
;
info.mask=mask
*pinfo=info
update_lac_display_image, pinfo
;
endif
;
end
info.dw: begin
;
; only process "button up" events
;
if event.type eq 1 then begin
mask=info.mask
x=event.x
y=event.y
;
; turning on or turning off?
;
current=mask[x,y]
if current eq 255 then newval=0
if current eq 0 then newval=1
;
; get LAC clicked on and all pixels to fill
;
lac_clicked=info.bp[x,y]
idx=where(info.bp eq lac_clicked,count)
if count eq 0 then message, 'NO LAC Chart found.'
mask[idx]=255*newval
;
; update the button group
;
widget_control, info.bgroup, get_value=bgroup_vector
bgroup_vector[lac_clicked-1]=newval
widget_control, info.bgroup, set_value=bgroup_vector
;
; update info structure
;
info.mask = temporary(mask)
*pinfo=info
;
; display new image
;
update_lac_display_image, pinfo
;
endif
;
end
else: ;do nothing
endcase
;
end
pro update_lac_display_image, pinfo
;
; handles updating the draw widget
;
; grab state structure
;
info=*pinfo
mask=info.mask
cbg=info.cbg
cbg=cbg[*,*,0]
;
; blend
;
alpha=0.5
red=byte((mask>cbg)*alpha + (1.0-alpha)*cbg)
disp_img=[[[red]],[[cbg]],[[cbg]]]
;
; update info
;
info.disp_img=disp_img
*pinfo=info
;
; display
;
widget_control, info.dw, get_val=draw_wid
wset,draw_wid
tv, disp_img, true=3
;
end
pro m3_smart_mosaic_pick_lacs, lac_list
;
; hardcoded paths to change
;
start_dir=filepath('', root_dir=m3_programrootdir(), subdir=['resources', 'smart_mosaic'])
if strmid(start_dir,0,1,/reverse_offset) ne path_sep() then start_dir=start_dir+path_sep()
cbgf=start_dir+'lac_option_cbg_w_lac_labels'
bpf=start_dir+'lac_option_backplane'
;
; set display size for draw widget
;
xds=720
yds=360
;
; open and display labeled cbg
;
envi_open_file, cbgf, r_fid=cbg_fid, /no_realize
envi_file_query, cbg_fid,ns=cns,nl=cnl,data_type=cdt
envi_file_mng,id=cbg_fid,/remove
cbg_full=make_array(cns,cnl,type=cdt)
openr,lun,cbgf,/get_lun
readu,lun,cbg_full
free_lun,lun
cbg_full=reverse(cbg_full,2) ;reverse image to match IDL Origin
cbg=rebin(cbg_full,xds,yds)
;
; read in lac index backplane
;
envi_open_file, bpf, r_fid=bp_fid, /no_realize
envi_file_query, bp_fid, ns=bpns, nl=bpnl, data_type=bpdt
envi_file_mng,id=bp_fid,/remove
bp_full=make_array(bpns,bpnl,type=bpdt)
openr,lun,bpf,/get_lun
readu,lun,bp_full
free_lun,lun
bp=rebin(bp_full,xds,yds)
;
; lac indices
;
lac_names=strtrim(indgen(144)+1,2)
for i = 0,98 do lac_names[i]='0'+lac_names[i]
for i = 0,8 do lac_names[i]='0'+lac_names[i]
;
; realize widget
;
tlb=widget_base(title='M3 Smart Mosaic',column=1, /base_align_center,xoffset=xoff, yoffset=yoff)
dw=widget_draw(tlb,xsize=xds,ysize=yds, /button_events)
bgroup=cw_bgroup(tlb,lac_names,column=12,/nonexclusive)
;bgroup=widget_menu(tlb,list=lac_names, rows=12)
slb=widget_base(tlb, column=2)
okID=widget_button(slb,value='OK', xsize=45)
cancelID=widget_button(slb,value='Cancel',xsize=45)
widget_control, tlb, /realize
;
disp_img=[[[cbg]],[[cbg]],[[cbg]]]
widget_control,dw,get_value=draw_wid
wset, draw_wid
tv, disp_img, true=3
;
; set up info structure to hold all the state info, register event handler
;
cbg=[[[cbg]],[[cbg]],[[cbg]]]
info= { cbg:cbg, $
bp:bp, $
lac_list:0, $
lacs_chosen:intarr(144),$
bgroup:bgroup, $
dw:dw, $
okID:okID, $
cancelID:cancelID, $
disp_img:disp_img, $
mask:bp*0 $
}
pinfo=ptr_new(info)
widget_control, tlb, set_uvalue=pinfo
xmanager, 'm3_smart_mosaic_pick_lacs', tlb
;
; display list of lac charts selected
;
info=*pinfo
lacs_chosen=info.lacs_chosen
idx=where(lacs_chosen eq 1,count)
if count gt 0 then lacs_in_play=idx+1
lac_list=idx+1
;
ptr_free, pinfo
;
end
pro m3_smart_mosaic_get_user_mask_event, event
compile_opt strictarr, hidden
;
;CATCH, error
;IF (error NE 0) THEN BEGIN
; catch, /cancel
; ok = m3_error_message()
; return
;ENDIF
;
widget_control, event.top, get_uvalue=pressed_cancel
widget_control, event.id, get_value=ButtonValue
if ButtonValue eq 'OK' then widget_control, event.top, /destroy
if ButtonValue eq 'Cancel' then begin
*pressed_cancel=1
widget_control, event.top, /destroy
endif
;
end
pro m3_smart_mosaic_get_user_mask, maskf, roi_id
;
; lets the user draw an ROI based on a CBG display
;
;-----------------------------------------------------------------------------------
compile_opt strictarr, hidden
;
; error handling
;
;CATCH, error
;IF (error NE 0) THEN BEGIN
; catch, /cancel
; ok = m3_error_message()
; return
;ENDIF
;
; hard coded paths to update as necessary
;
start_dir=filepath('', root_dir=m3_programrootdir(), subdir=['resources', 'smart_mosaic'])
cbgf=start_dir + 'raw_cbg_extended_24deg_rebin_10x'
maskf=filepath('smart_mosaic_mask.img', /tmp)
open=0 ;leave the files open or not
;
; open clementine background file
;
envi_open_file, cbgf, r_fid=cbg_fid
envi_file_query, cbg_fid, ns=ns, nl=nl
dims=[-1,0,ns-1,0,nl-1]
map_info=envi_get_map_info(fid=cbg_fid)
mc=map_info.mc
;
; display cbg
;
envi_display_bands, cbg_fid, 0, /new
all_dgn=envi_get_display_numbers()
dgn=all_dgn[n_elements(all_dgn)-1]
;
; draw GUI that gets user instructions and tells us ROI has been drawn
;
cr=string(13b)
text=cr
text=text+' 1. Create on ROI on the display just opened.'+cr
text=text+' Use any of the standard ENVI ROI methods.'+cr+cr
text=text+' 2. Press OK when you are done.'+cr+cr
text=text+' 3. In the next dialog box you will enter the'+cr
text=text+' rest of the mosaic parameters.'+cr+cr+cr
envi_center,xoff,yoff
tlb=widget_base(title='M3 Smart Mosaic',column=1, /base_align_center,xoffset=xoff, yoffset=yoff)
wl=widget_label(tlb,value=text, xsize=250, ysize=150,/align_left, /frame)
slb=widget_base(tlb, column=2)
ok=widget_button(slb,value='OK', xsize=45)
cancel=widget_button(slb,value='Cancel',xsize=45)
widget_control, tlb, /realize
pressed_cancel=ptr_new(0)
widget_control, tlb, set_uvalue=pressed_cancel
xmanager, 'm3_smart_mosaic_get_user_mask', tlb
if *pressed_cancel then goto, cleanup
;
; get the ROI the user just drew
;
roi_ids=envi_get_roi_ids(fid=cbg_fid, roi_names=roi_names)
n_roi=n_elements(roi_ids)
if n_roi gt 1 then begin
;
; they made more than one ROI, ask which to use
;
tlb=widget_auto_base(title='ROI Selection')
prompt='You created more than one ROI. Select the one to use.'
ws=widget_slist(tlb, list=roi_names, prompt=prompt, uvalue='index', /auto)
result=auto_wid_mng(tlb)
roi_id=roi_ids[result.index]
;
endif else roi_id = roi_ids
;
; get roi pixels, check to make sure there are some to use
;
roi_addr=envi_get_roi(roi_id)
if roi_addr[0] eq -1L then goto, cleanup ;no pixels to process
np=n_elements(roi_addr)
;
; get ROI bounding box
;
pts=array_indices([ns,nl],roi_addr,/dimensions)
s=pts[0,*]
l=pts[1,*]
maxs=max(s,imaxs,min=mins,subscript_min=imins)
maxl=max(l,imaxl,min=minl,subscript_min=iminl)
;
; xstart and ystart
;
x0=s[imins]
y0=l[iminl]
;
; create mask
;
mns=maxs-mins+1
mnl=maxl-minl+1
mask=bytarr(mns,mnl)
;
; figure offset and set roi pixels on
;
x=s-x0
y=l-y0
mask[x,y]=1
;
; update UL geo point
;
envi_convert_file_coordinates,cbg_fid,x0,y0,xmap,ymap,/to_map
mc[2]=xmap
mc[3]=ymap
map_info.mc=mc
;
; write mask to file, set up header
;
openw,lun,maskf,/get_lun
writeu,lun,mask
free_lun, lun
def_stretch=envi_default_stretch_create(/linear, val1=0.0, val2=1.0)
envi_setup_head,fname=maskf,ns=mns,nl=mnl,nb=1,offset=0,data_type=1,interleave=0,$
xstart=x0,ystart=y0,def_stretch=def_stretch,map_info=map_info,bnames='Mask Band',$
/write,open=keyword_set(open)
;
; clean up
;
cleanup:
envi_close_display, dgn
if ~keyword_set(open) then envi_file_mng, id=cbg_fid, /remove
ptr_free, pressed_cancel
;
end
pro m3_smart_mosaic_build_glt,igm,lac_sl,lac_el,ns_igm,iproj,oproj,x0_mos,y0_mos,ns_mos,nl_mos,ps,gltf,$
ulo,ss_glt,es_glt,sl_glt,el_glt
;
; build an m3 glt in the grid space of the mosaic
;
; figure how big a glt this file will make in the output grid
;
; get the edge pixels in clockwise order
;
ns=ns_igm
nl_used=lac_el-lac_sl+1
edge_lon=dblarr(2l*ns_igm+(nl_used-2)*2l)
edge_lat=dblarr(2l*ns_igm+(nl_used-2)*2l)
;
ptr=0l
;
t=igm[*,0:1,lac_sl]
edge_lon[ptr]=t[*,0]
edge_lat[ptr]=t[*,1]
ptr=ptr+ns_igm
;
for i=0l,nl_used-3 do begin
;
t=igm[[0,ns_igm-1],0:1,i+1+lac_sl]
edge_lon[ptr]=t[1,0]
edge_lat[ptr]=t[1,1]
edge_lon[2l*ns_igm+(nl_used-2)*2l-1-i]=t[0,0]
edge_lat[2l*ns_igm+(nl_used-2)*2l-1-i]=t[0,1]
ptr=ptr+1
;
endfor
;
t=igm[*,0:1,lac_el]
ptr=ns_igm+nl_used-2
edge_lon[ptr]=t[*,0]
edge_lat[ptr]=t[*,1]
;
; convert them to grid s and l
;
envi_convert_projection_coordinates,edge_lon,edge_lat,iproj,edge_x,edge_y,oproj
edge_s=(edge_x-x0_mos)/ps
edge_l=(y0_mos-edge_y)/ps
;
nso=es_glt-ss_glt+1
nlo=el_glt-sl_glt+1
dxs=ps*lindgen(nso)
dys=dblarr(nso)
dxl=0d
dyl=-ps
x0o=ulo[0]
y0o=ulo[1]
rotation=0d
;
; build the output lookup table images that will be used for
; the creation of the geocoded imagery file
;
luts=intarr(nso,nlo)
lutl=intarr(nso,nlo)
dxim=fltarr(nso,nlo)
dyim=fltarr(nso,nlo)
;
sptr=lindgen(ns)+1
;
for i=lac_sl,lac_el do begin
;
; get the lat and lon for this in line
;
t=igm[*,0:1,i]
lon=reform(t[*,0])
lat=reform(t[*,1])
neg_lon=where(lon gt 180d,n_neg_lon)
if(n_neg_lon gt 0)then lon[neg_lon]=lon[neg_lon]-360d
;
; convert to glt s and l
;
envi_convert_projection_coordinates,lon,lat,iproj,x,y,oproj
t[0,0]=x-x0o
t[0,1]=y-y0o
;
; see which fit
;
nptrs=long(t[*,0]/ps)
nptrl=long(-t[*,1]/ps)
val=where(nptrs ge 0 and nptrs lt nso and nptrl ge 0 and nptrl lt nlo,nval)
;
; place the points if any fit
;
if(nval gt 0)then begin
;
ptr=nptrs[val]+nso*nptrl[val]
luts[ptr]=sptr[[val]]
lutl[ptr]=i+1
dxim[ptr]=float(t[val,0])
dyim[ptr]=float(t[val,1])
;print,i,nval,n_elements(uniq(nptrs,sort(nptrs)))
;
endif
;
endfor
;
; write the luts,lutl and dxim,dyim files to disk
; to get them out of memory, before pixel infilling
;
; write the luts, lutl, and dx and dy files
;
openw,un1,/get,gltf
for i=0l,nlo-1 do writeu,un1,luts[*,i],lutl[*,i]
free_lun,un1
temp_glt=bytarr(nso,nlo)
for i=0l,nlo-1 do temp_glt[0,i]=luts[*,i] gt 0
;
openw,un1,/get,gltf+'_tmp'
;
dxhalf_pixel=(dxs(1)+dxl)/2
dyhalf_pixel=(dys(1)+dyl)/2
;
for i=0l,nlo-1 do begin
;
t=dxim(*,i) ne 0
;
writeu,un1,float(t*(dxim(*,i)-(dxs+i*dxl+dxhalf_pixel))),$
float(t*(dyim(*,i)-(dys+i*dyl+dyhalf_pixel)))
endfor
;
free_lun,un1
;
luts=0b
lutl=0b
dxim=0b
dyim=0b
;
; figure the pixels that make up the boundary or boundaries of the in-image area(s)
;
; loop over output lines and do the pixel infilling
; using a 3*3 mask for nearest neighbor finding,
; on each output line that has data:
; 1) find the gaps that need filling
; 2) loop over gap pixels
; 3) use the mask to find nearest valid pixel
; 4) infill the lut file (with negative flag set)
;
; build the 3*3 masks of rotated integer dx and dy
;
idx=[[-1,0,1],[-1,0,1],[-1,0,1]]*ps
idy=[[1,1,1],[0,0,0],[-1,-1,-1]]*ps
;rdx=float(cos(-rotr)*idx-sin(-rotr)*idy)
;rdy=float(sin(-rotr)*idx+cos(-rotr)*idy)
rdx=float(idx)
rdy=float(idy)
;
; build the 7*7 masks of rotated distances
;
idx7=intarr(7,7)
for i=0,6 do idx7(0,i)=indgen(7)-3
idy7=intarr(7,7)
for i=0,6 do idy7(0,i)=intarr(7)-3+i
idx7=idx7*ps
idy7=idy7*ps
rdx7=idx7
rdy7=idy7
;
openu,un1,/get,gltf
openr,un2,/get,gltf+'_tmp'
bil2=assoc(un1,intarr(nso,2))
bil3=assoc(un2,fltarr(nso,2))
;
; build and fill the 3-line buffers of dx,dy and luts,lutl values
;
lutsbuff=intarr(nso,3,/no)
lutlbuff=intarr(nso,3,/no)
dxbuff=fltarr(nso,3,/no)
dybuff=fltarr(nso,3,/no)
;
; allow for glt images shorter than 3 lines long
;
max_read_buff_lines=min([3,nlo])
;
for i=0,max_read_buff_lines-1 do begin
t=bil2(i)
lutsbuff(0,i)=t(*,0)
lutlbuff(0,i)=t(*,1)
t=bil3(i)
dxbuff(0,i)=t(*,0)
dybuff(0,i)=t(*,1)
endfor
;
; define left and right pointers to data swath, it needs some
; handholding to avoid raggedy edges
;
;
; build the mask for this polygon in the map space
;
out_mask=bytarr(nso,nlo)
oroi=obj_new('idlanroi',edge_s-ss_glt,edge_l-sl_glt)
out_mask=oroi->computemask(mask_rule=2,dimensions=[nso,nlo],pixel_center=[0.5,0.5])
obj_destroy,oroi
out_mask=out_mask gt 0
;
; now scan out_mask and build nump,sno,lno,nseg
;
; first pass through and just get a total segment count
; by filling the nseg vector
;
nseg=intarr(nlo)
for i=0l,nlo-1 do begin
ta=fix(out_mask(*,i) gt 0)
tb=shift(ta,1)
tb(0)=0
tc=ta-tb
n_new=total(tc eq 1)
if(ta(0) eq 1)then n_new=n_new+1
nseg(i)=n_new
endfor
tot_nseg=total(nseg)
;
; build nump,sno,lno for each output segment
;
nump=intarr(tot_nseg)
sno=intarr(tot_nseg)
lno=lonarr(tot_nseg)
ptr=0l
;
for i=0l,nlo-1 do begin
ta=fix(out_mask(*,i) gt 0)
tb=shift(ta,1)
tb(0)=0
tc=ta-tb
seg_start=where(tc eq 1,n_seg_start)
if(n_seg_start gt 0)then begin
if(ta(0) eq 1)then begin
seg_start=[0,seg_start]
n_seg_start=n_seg_start+1
endif
sno(ptr)=seg_start
lno(ptr)=replicate(i,n_seg_start)
for j=0,n_seg_start-1 do begin
td=ta(seg_start(j):*)
temp=where(td ne td(0),n_temp)
if(n_temp eq 0)then begin
nump(ptr)=n_elements(td)
ptr=ptr+1
endif else begin
nump(ptr)=min(temp)
ptr=ptr+1
endelse
endfor
endif else begin
if(ta(0) eq 1)then begin
sno(ptr)=0
lno(ptr)=i
temp=where(ta ne ta(0),n_temp)
if(n_temp eq 0)then begin
nump(ptr)=n_elements(ta)
ptr=ptr+1
endif else begin
nump(ptr)=min(temp)
ptr=ptr+1
endelse
endif
endelse
endfor
;
; loop over output lines, finding gaps and filling them
;
no3by3=0l
no7by7=0l
for i=1l,nlo-2 do begin
;
; skip empty lines
;
if(nseg(i) gt 0)then begin
;
; read in the luts,lutl data for this line
; and find the blanks that need filling
;
t=bil2(i)
segptr=where(lno eq i)
for l=0l,nseg(i)-1 do begin
bs=t(*,0)
lp=sno(segptr(l))
rp=lp+nump(segptr(l))-1
blank=where(bs(lp:rp) eq 0,nblank)+lp
;
; loop over blanks, if any, and fill them
;
if(nblank gt 0)then begin
for j=0l,nblank-1 do begin
b=blank(j)
if(b gt 0 and b lt nso-1)then begin
dx=(rdx+dxbuff(b-1:b+1,*))^2
dy=(rdy+dybuff(b-1:b+1,*))^2
ds=dx+dy
val=where(lutsbuff(b-1:b+1,*) gt 0,nval)
if(nval gt 0)then begin
nnval=min(ds(val),nnptr)
nnptrs=(val(nnptr) mod 3)+b-1
nnptrl=val(nnptr)/3
t(b,*)=-[lutsbuff(nnptrs,nnptrl),lutlbuff(nnptrs,nnptrl)]
endif else begin
no3by3=no3by3+1l
lpz=(b-3)>0
rpz=(b+3)<(nso-1)
tpz=(i-3)>0
bpz=(i+3)<(nlo-1)
ssz=lpz-b+3
slz=tpz-i+3
nsz=rpz-lpz+1
nlz=bpz-tpz+1
zbuffs=intarr(nsz,nlz)
zbuffl=intarr(nsz,nlz)
zbuffdx=fltarr(nsz,nlz)
zbuffdy=fltarr(nsz,nlz)
for k=tpz,bpz do begin
tz=bil2(lpz:rpz,*,k)
zbuffs(0,k-tpz)=tz(*,0)
zbuffl(0,k-tpz)=tz(*,1)
tz=bil3(lpz:rpz,*,k)
zbuffdx(0,k-tpz)=tz(*,0)
zbuffdy(0,k-tpz)=tz(*,1)
endfor
dx=(rdx7(ssz:ssz+nsz-1,slz:slz+nlz-1)+zbuffdx)^2
dy=(rdy7(ssz:ssz+nsz-1,slz:slz+nlz-1)+zbuffdy)^2
ds=dx+dy
val=where(zbuffs gt 0,nval)
if(nval gt 0)then begin
nnval=min(ds(val),nnptr)
nnptrs=(val(nnptr) mod nsz)
nnptrl=val(nnptr)/nsz
t(b,*)=-[zbuffs(nnptrs,nnptrl),zbuffl(nnptrs,nnptrl)]
endif else begin
no7by7=no7by7+1l
endelse
endelse
endif
endfor
endif
endfor
bil2(i)=t
endif
;
; JWB 7/11/5 3
;
if(i lt nlo-2)then begin
lutsbuff(0,0)=lutsbuff(*,1:2)
lutlbuff(0,0)=lutlbuff(*,1:2)
;t2=bil2(i+1)
t2=bil2(i+2)
lutsbuff(0,2)=t2(*,0)
lutlbuff(0,2)=t2(*,1)
dxbuff(0,0)=dxbuff(*,1:2)
dybuff(0,0)=dybuff(*,1:2)
;t2=bil3(i+1)
t2=bil3(i+2)
dxbuff(0,2)=t2(*,0)
dybuff(0,2)=t2(*,1)
endif
endfor
;
free_lun,un1,un2
file_delete,gltf+'_tmp'
;
if(no3by3 gt 0)then begin
;
print,'number of no 3*3 '+strtrim(no3by3,2)
print,'number of no 7*7 '+strtrim(no7by7,2)
;
endif
;
; build ENVI header for GLT file
;
ops=[ps,ps]
mc=[0,0,ulo[0],ulo[1]]
rotation=0
map_info=envi_map_info_create(proj=oproj,datum=datum,ps=ops,mc=mc,rotation=rotation)
bnames=['sample number lookup','line number lookup']
descrip='AIG M3 Ortho GLT'
envi_setup_head,fname=gltf,ns=nso,nl=nlo,nb=2,interleave=1,data_type=2,offset=0,$
bnames=bnames,descrip=descrip,map_info=map_info,/write
;
end
pro m3_smart_mosaic_build_rule,glt,ss_glt,es_glt,sl_glt,el_glt,contrib_line_range,ns_in,$
loc_bil,obs_bil,temps,rule_null,coefs,rule
;
; build a smart mosaic rule image
;
; build the raw format rule for the contributing line range
;
nl_contrib=contrib_line_range[1]-contrib_line_range[0]+1
rule_raw=fltarr(ns_in,nl_contrib)
for i=0l,nl_contrib-1 do rule_raw[0,i]=obs_bil[*,*,i+contrib_line_range[0]]#coefs
;
; build the null mosaic grid rule image
;
ns_glt=es_glt-ss_glt+1
nl_glt=el_glt-sl_glt+1
rule=fltarr(ns_glt,nl_glt)+rule_null
;
; pass the raw rule through the glt to the mosaic grid image
;
buff=fltarr(ns_glt)+rule_null
;
for i=0l,nl_glt-1 do begin
;
raw_s=reform(long(abs(glt[*,0,i])))-1
raw_l=reform(long(abs(glt[*,1,i])))-1-contrib_line_range[0]
val=where(glt[*,0,i] ne 0,n_val)
if(n_val gt 0)then begin
;
buff[val]=rule_raw[raw_s[val]+ulong64(raw_l[val])*ns_in]
rule[0,i]=buff
if(min(buff[val]) eq max(buff[val]) and n_val gt 300)then print,crap
buff[*]=rule_null
;
endif
;
endfor
;
end
pro m3_smart_mosaic_figure_contrib,max_line_range,proj_loc,proj_out,x0_out,y0_out,ps_out,ns_out,nl_out,loc_bil,ns_in,$
contrib_line_range,this_line,ss_glt,es_glt,sl_glt,el_glt,ulo
;
; figure if a loc file contributes to the mosaic
; if it does figfure the line range and the mosaic grid space subset in play
;
; set up variables
;
nl=max_line_range[1]-max_line_range[0]+1
this_line=lonarr(nl)
ss_glt=ns_out+1
es_glt=-1
sl_glt=nl_out+1
el_glt=-1
contrib_line_range=[-1l,-1l]
;
; loop over possible lines in play for this file
;
for i=0,nl-1 do begin
;
; get lon and lat values
;
t=loc_bil[*,0:1,i+max_line_range[0]]
lon=t[*,0]
lat=t[*,1]
;
; convert to +/- 180 lon
;
neg=where(lon gt 180,n_neg)
if(n_neg gt 0)then lon[neg]=lon[neg]-360d
;
; convert them to output s and l
;
envi_convert_projection_coordinates,lon,lat,proj_loc,x,y,proj_out
s=long((x-x0_out)/ps_out)
l=long((y0_out-y)/ps_out)
;
in=where(s ge 0 and s lt ns_out and l ge 0 and l lt nl_out,n_in)
this_line[i]=n_in
;
; drop any out of the mosaic and update ss,es,sl,el
;
if(n_in gt 0)then begin
;
if(n_in ne ns_in)then begin
;
s=s[in]
l=l[in]
;
endif
;
ss_glt=ss_glt<min(s)
es_glt=es_glt>max(s)
sl_glt=sl_glt<min(l)
el_glt=el_glt>max(l)
;
endif
;
endfor
;
; if some contribute figure line range and mosaic grid subset
;
if(total(this_line) gt 0)then begin
;
; clip it to the actual boundary of the mosaic
;
if(ss_glt lt 0)then ss_glt=0
if(es_glt ge ns_out)then es_glt=ns_out-1
if(sl_glt lt 0)then sl_glt=0
if(el_glt ge nl_out)then el_glt=nl_out-1
ulo=[x0_out+ps_out*ss_glt,y0_out-sl_glt*ps_out]
;
contrib_line_range[0]=min(where(this_line gt 0),max=t)+max_line_range[0]
contrib_line_range[1]=t+max_line_range[0]
;
endif
;
end
pro m3_smart_mosaic_sort_filenames_by_time,files,order,files_sorted
;
; sort m3 filename by time and write to a new file
;
; get the number of input files
;
nf=n_elements(files)
;
; sort them based on time
;
short_files=strarr(nf)
for i=0l,nf-1 do begin
;
; strip each file to its date start
;
last_fslash=strpos(files[i],'/',/reverse_search)
last_bslash=strpos(files[i],'\',/reverse_search)
last_slash=max([last_fslash,last_bslash])
;
short_files[i]=strmid(files[i],last_slash+4)
;
endfor
;
order=sort(short_files)
files_sorted=files[order]
;
end
pro smart_mosaic_lac_mask,lac_list,lac_index,mask,mask_map_info
;
; build a mask of one or more lac sheets
;
nlac=n_elements(lac_list)
mask=lac_index eq lac_list[0]
for i=1l,nlac-1 do mask=mask or (lac_index eq lac_list[i])
;
ps=[1d,1]
mc=[0d,0d,-180d,90d]
datum='Moon Sphere 1737.4'
mask_map_info=envi_map_info_create(/geographic,ps=ps,mc=mc,datum=datum)
;
end