-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimesr.pro
executable file
·1856 lines (1507 loc) · 59.4 KB
/
timesr.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
;+
; NAME:
; HANTS
;
; PURPOSE:
; This programme is based on the 2005 fortran version of HANTS programme by Wout Verhoef.
;
; INPUT:
; ni: Number of images = length of time series, such as 46 for MODIS 8day LST product
; per: Period that input time series covered, such as 365 for one year
; ts: Day number of each element in array y, such as [1,9,17,...]
; nf: Number of frequency, such as 3 (generally 3 or 4. Larger nf is, the reconstructed curve turns to include more noise)
; pa: Period, which is corresponding to nf. If nf is 3, pa = [361 180 120]
; HiLo: High or low outliers. (To decide higher values or lower values will be treated as unreasonable values)
; low: Minimum of valid range
; high: Maximum of valid range
; fet: Fit error tolerance
; dod: Degree of overdetermined
; y: Input time series curve.
; delta = small positive number to suppress high amplitudes
;
;OUTPUT:
; yr: Reconstructed time series
; amp: Array of amplitudes, first element is the average of the curve
; phi: Array of phases, first element is zero
;
;AUTHOR:
; Jing Li, Ph.D
; E-mail: [email protected]
; TIME:
; September, 2008
FUNCTION HANTS,ni,per,ts,nf,pa,HiLo,low,high,fet,dod,y,looplim,delta,$
POLYNOMIAL=polynomial
ON_ERROR,0
;catch, theError
;if theError ne 0 then begin
; catch, /cancel
; ok = dialog_message(!error_state.msg, /error)
; return, {yr:yr, amp:amp, phi:phi}
;endif
;delta = 0.1
;output
amp = FLTARR(nf+1)
phi = FLTARR(nf+1)
yr = UINTARR(ni)
tagdiff = STRCMP(HiLo,'High',/FOLD_CASE)
IF tagdiff EQ 1 THEN BEGIN
sHiLo = -1
ENDIF ELSE BEGIN
sHiLo = 1
ENDELSE
nr = 2*nf+1
IF N_ELEMENTS(polynomial) NE 0 THEN nr=nr+3
IF nr GT ni THEN BEGIN
nr = ni
ENDIF
noutmax = ni-nr-dod
pi = 4*ATAN(1)
rd = pi/180
dg = 180/pi
mat = FLTARR(nr,ni)+1
ang = FINDGEN(per)
ang = 2*pi*ang/per
cs = COS(ang)
sn = SIN(ang)
;"mat": Dimensions is ni*(2*nf+1)
FOR i=0,nf-1 DO BEGIN
ifr = FIX(FLOAT(per)/pa[i])
index = ifr*(ts-1) MOD per
;angles(i,*) = index
mat[2*i+1,*] = cs[index]
mat[2*i+2,*] = sn[index]
ENDFOR
IF N_ELEMENTS(polynomial) NE 0 THEN BEGIN
;ifr = fix(float(per)/pa[i])
index = cgSCALEVECTOR(ts,0,per-1)
mat[nr-2,*]=index*0.05;/(2*pi)
mat[nr-1,*]=LONG64(index*0.05)^2
mat[nr-1,*]=LONG64(index*0.05)^3
ENDIF
;'p': weight value!
p = INTARR(1,ni)+1
pos = WHERE(y GT high OR y LT low,nout)
IF nout NE 0 THEN BEGIN
p[pos] = 0
ENDIF
IF nout GT noutmax THEN BEGIN
;result = dialog_message('Samples in series are lower than needed! Reconstruction failed!',/error)
RETURN, 0
ENDIF
nloop = 0
;nloopmax = 10
ready = 0
mat = TRANSPOSE(mat)
;cgoplot,y
;����ȥ�����������
;while ready eq 0 and nloop lt nloopmax and nloop lt looplim do begin
WHILE ready EQ 0 AND nloop LT looplim DO BEGIN
nloop = nloop + 1
za = mat ## (p*y)
pmatrix = p
FOR ii = 1,nr-1 DO BEGIN
pmatrix = [[pmatrix],[p]]
ENDFOR
A = mat*pmatrix##TRANSPOSE(mat)
ADim = SIZE(A,/dimensions)
Adelta = FLTARR(ADim)
Adelta[INDGEN(ADim[0]) * (ADim[0]+1)] = 1
Adelta = Adelta*delta
Adelta[0,0] = 0
A = A + Adelta
B = INVERT(A)
zr = B##za
;rewrite by wwj
;yr = transpose(zr)##mat
yr = TRANSPOSE(mat)##zr
diff = sHiLo*(yr-y)
err = TRANSPOSE(p)*diff
errsort_sub = SORT(err)
maxerr=diff[errsort_sub[ni-1]]
ready = (maxerr LE fet) OR (nout EQ noutmax)
IF ready EQ 0 THEN BEGIN
; i=ni-1
; j=errsort_sub[i]
; while p[j]*diff[j] gt maxerr*0.5 and nout lt noutmax do begin
;
; p(j)=0
; nout=nout+1
; i=i-1
; j=errsort_sub(i)
; endwhile
index=WHERE(err GT fet, count,COMPLEMENT=c_index)
p[index]=p[index]-err[index]
p_index=WHERE(p LT 0,count)
IF count NE 0 THEN BEGIN
p[p_index]=0
nout=count
ENDIF
ENDIF
;index=WHERE(p LT 1, count,COMPLEMENT=c_index)
;y[index]=INTERPOL(y[c_index],c_index,index,/spline)
; yr[index]=y[index]
;cgoplot,y
ENDWHILE
ra = zr[INDGEN(nf)*2+1]
rb = zr[INDGEN(nf)*2+2]
amp[1:*] = TRANSPOSE(SQRT(ra*ra+rb*rb))
phi[1:*] = TRANSPOSE(ATAN(rb,ra)*dg)
amp[0] = zr[0]
phi[0] = 0
pos = WHERE(phi LT 0, npos)
IF npos NE 0 THEN BEGIN
phi[pos] = phi[pos] + 360
ENDIF
RETURN, {yr:yr, amp:amp, phi:phi,valid_i:WHERE(p EQ 1)}
END
FUNCTION DIFF,A,Deg
;A=shift(D,0,1)-A
IF N_PARAMS() EQ 1 THEN deg=1
IF deg EQ 1 THEN BEGIN
RETURN,(SHIFT(A,0,1)-A)[*,1:*]
END
RETURN,DIFF((SHIFT(A,0,1)-A)[*,1:*],deg-1)
END
FUNCTION WHITSMW,y, WEIGHT=w, LAMBDA=lambda, DEGREE=degree
catch,err
if err ne 0 then begin
catch,/cancel
print,y,w
return,w*y
endif
IF N_ELEMENTS(degree) EQ 0 THEN degree=2
IF N_ELEMENTS(w) EQ 0 THEN w=FLTARR(N_ELEMENTS(y))+1
IF N_ELEMENTS(lambda) EQ 0 THEN lambda=0.5
;Smoothing
m = n_elements(y);
;E = SPRSIN(LINDGEN(m), LINDGEN(m), REPLICATE(1.0,m), m)
;W = SPRSIN(LINDGEN(m), LINDGEN(m), w, m);
wa=DIAG_MATRIX(w)
D = IDENTITY(m)
D=DIFF(D,degree)
;C = chol(W + lambda * transpose(D) * D);
;z = INVERT((wa + lambda * TRANSPOSE(D)## D))# (wa#y)
A=(wa + lambda * TRANSPOSE(D)## D)
CHOLDC, A, P
RETURN,CHOLSOL(A, P, wa#y)
END
FUNCTION ddmat,x,d
m=N_ELEMENTS(x)
IF d EQ 0 THEN BEGIN
DD=identity(m)
;DD=SPRSIN(indgen(m),indgen(m),REPLICATE(1.0,m),m)
ENDIF ELSE BEGIN
dx=x[d:m-1]-x[0:m-d-1]
V=DIAG_MATRIX(1.0/dx)
;V=SPRSIN(indgen(m-d),indgen(m-d),1.0/dx,m-d)
DD=V##diff(ddmat(x,d-1))
; DD=SPRSAB(V##(diff(ddmat(x,d-1))))
ENDELSE
RETURN,DD
END
FUNCTION whitsmddw,x, y, WEIGHT=weight, LAMBDA=lambda, DEGREE=d
IF N_ELEMENTS(degree) EQ 0 THEN degree=2
IF N_ELEMENTS(weight) EQ 0 THEN weight=FLTARR(N_ELEMENTS(y))+1
IF N_ELEMENTS(lambda) EQ 0 THEN lambda=10
m=N_ELEMENTS(y)
e=identity(m)
DD=ddmat(x,d)
w=DIAG_MATRIX(weight)
z = INVERT((W + lambda * TRANSPOSE(DD)## DD))# (w#y)
RETURN,z
END
FUNCTION R_ha,o_s,WEIGHT=weight, PARA_INFO=para_info,$
PER=per,TS=ts,DELTA=delta,PARR=pa,OTS=ots,OUT=out,_Extra=extrakeywords
ON_ERROR,0
; if n_elements(batch) ne 0 then begin
; CASE (batch) OF
; 0: BEGIN ;all params hold the same except o_s
;
; END
; ELSE: BEGIN
; END
; ENDCASE
;
; endif
ni=N_ELEMENTS(o_s)
IF N_ELEMENTS(delta) EQ 0 THEN delta=0.5
IF N_ELEMENTS(weight) EQ 0 THEN weight=FLTARR(ni)+1
pi = 4*ATAN(1)
nf=N_ELEMENTS(pa)
nr=2*nf+1
mat = FLTARR(nr,ni)+1
pha_arr=2*!PI*per/pa#ts/per
mat[1:nr-1:2,*]=COS(pha_arr)
mat[2:nr-1:2,*]=SIN(pha_arr)
pos = WHERE(weight NE 0)
; p[pos] = 1
w=BYTARR(ni,ni)
w[pos,pos]=1
A = TRANSPOSE(w##mat)##(mat)
ADim = SIZE(A,/dimensions)
Adelta = FLTARR(ADim)
Adelta[INDGEN(ADim[0]) * (ADim[0]+1)] = 1
Adelta = Adelta*delta
Adelta[0,0] = 0
A = A + Adelta
B = INVERT(A)
yr = mat##B##TRANSPOSE(w##mat)##o_s
; M=w##mat
; LA_SVD, M, S, U, V
;
; N = N_ELEMENTS(S)
; WP = FLTARR(N, N)
; index=where(abs(S) ge 0.1,count)
; ;if count ne -1 then wp[index,index]=1.0/s[index]
; wp[indgen(N),indgen(N)]=s/(s+0.1)^2
;t=reform(w##o_s)
;if count ne -1 then s[index]=0
; yr1=mat##SVSOL(U, S, V, t)
; A=TRANSPOSE(w##mat)##(mat)
; CHOLDC,A,P
; B=reform(transpose(mat)##w##o_s)
; yr1=mat##CHOLSOL(A, P, B)
IF N_ELEMENTS(out) ne 0 THEN BEGIN
no=N_ELEMENTS(ots)
mat_o = FLTARR(nr,no)+1
pha_arr=2*!PI*per/pa#ots/per
mat_o[1:nr-1:2,*]=COS(pha_arr)
mat_o[2:nr-1:2,*]=SIN(pha_arr)
yr=mat_o##B##TRANSPOSE(w##mat)##o_s
;yr= mat_o## V ## WP ## TRANSPOSE(U)##w##o_s
ENDIF else begin
;yr= mat## V ## WP ## TRANSPOSE(U)##w##o_s
yr=mat##B##TRANSPOSE(w##mat)##o_s
Endelse
IF ARG_PRESENT(para_info) EQ 1 THEN BEGIN
zr = B##TRANSPOSE(w##mat)##o_s
amp = FLTARR(nf+1)
phi = FLTARR(nf+1)
ra = zr[INDGEN(nf)*2+1]
rb = zr[INDGEN(nf)*2+2]
amp[1:*] = TRANSPOSE(SQRT(ra*ra+rb*rb))
phi[1:*] = TRANSPOSE(ATAN(rb,ra))
amp[0] = zr[0]
phi[0] = 0
pos = WHERE(phi LT 0, npos)
IF npos NE 0 THEN BEGIN
phi[pos] = phi[pos] + 2*!PI
ENDIF
para_info={amp:amp,phi:phi}
ENDIF
RETURN,REFORM(yr);
END
FUNCTION GENERRIZED_RCON_MULTI,y,$
TS=ts,OTS=ots,PER=per,DELTA=delta,PARR=pa,$
HILO=HiLo,LOW=low,HIGH=high,FET=fet,DOD=dod,LOOPLIM=looplim,_Extra=extrakeywords
ON_ERROR,0
sHiLo = STRCMP(HiLo,'High',/FOLD_CASE) EQ 1?-1:1
;if the all the value is too small, then we don't do reconstruction.
if max(y) le low+fet then return,1
y=float(y)
ni=N_ELEMENTS(y)
noutmax = (ni-dod)
p = fltARR(1,ni)+1
pos = WHERE(y GT high OR y LT low,nout)
if nout ge noutmax then return, 0
IF nout NE 0 THEN p[pos] = 0
;tmp_y=y
ready=0
nloop=0
IF N_ELEMENTS(delta) EQ 0 THEN delta=0.5
IF N_ELEMENTS(weight) EQ 0 THEN weight=FLTARR(ni)+1
pi = 4*ATAN(1)
nf=N_ELEMENTS(pa)
nr=2*nf+1
pos = WHERE(weight NE 0)
; p[pos] = 1
w=BYTARR(ni,ni)
w[pos,pos]=1
mat = FLTARR(nr,ni)+1
pha_arr=2*!PI*per/pa#ts/per
mat[1:nr-1:2,*]=COS(pha_arr)
mat[2:nr-1:2,*]=SIN(pha_arr)
WHILE ready EQ 0 AND nloop LT looplim DO BEGIN
;**********************
A = TRANSPOSE(w##mat)##(mat)
ADim = SIZE(A,/dimensions)
Adelta = FLTARR(ADim)
Adelta[INDGEN(ADim[0]) * (ADim[0]+1)] = 1
Adelta = Adelta*delta
Adelta[0,0] = 0
A = A + Adelta
B = INVERT(A)
yr=mat##B##TRANSPOSE(w##mat)##y
;************************************
;yr=WHITSMW(tmp_y,WEIGHT=p,_Extra=extrakeywords)
; M=w##mat
;
; LA_SVD, M, S, U, V , STATUS=status
; if status gt 0 then return,0
; RES=SVSOL(U, S, V, reform(w##y))
;
; N = N_ELEMENTS(S)
; WP = FLTARR(N, N)
; index=where(abs(S) ge 0.0000001,count)
; wp[indgen(n),indgen(n)]=1.0/s
;if count ne -1 then wp[index,index]=1.0/s[index]
;wp[indgen(N),indgen(N)]=s/(s+0.1)^1
;yr=R_ha(y,WEIGHT=w,_Extra=extrakeywords,PARR=pa,PER=per)
;yr= W##mat## V ## WP ## TRANSPOSE(U)##float(y)
; yr=mat##res
diff = sHiLo*(yr-y)
err = w[findgen(ni),findgen(ni)]*diff
e_index=WHERE(err GE fet, count)
nout=nout+count
ready = (count EQ 0) OR (nout EQ noutmax)
IF ready EQ 1 THEN BREAK
w[e_index,e_index]=0
ENDWHILE
IF N_ELEMENTS(OTS) ne 0 THEN BEGIN
no=N_ELEMENTS(ots)
mat_o = FLTARR(nr,no)+1
pha_arr=2*!PI*per/pa#ots/per
mat_o[1:nr-1:2,*]=COS(pha_arr)
mat_o[2:nr-1:2,*]=SIN(pha_arr)
yr=mat_o##B##TRANSPOSE(w##mat)##y
;yr= mat_o## V ## WP ## TRANSPOSE(U)##w##y
; yr=mat##res
ENDIF
RETURN, low>yr<high
END
;for debug use.
FUNCTION GENERRIZED_HANTS,y,$
ITS=its,OTS=ots,$
HILO=HiLo,LOW=low,HIGH=high,FET=fet,DOD=dod,LOOPLIM=looplim,$
PLOT=plot,MULTI=multi,$
SMOOTHER=smoother,_Extra=extrakeywords
ON_ERROR,0
;catch, theError
;if theError ne 0 then begin
; catch, /cancel
; ok = dialog_message(!error_state.msg, /error)
; return, {yr:yr, amp:amp, phi:phi}
;endif
;delta = 0.1
IF N_ELEMENTS(y) EQ 0 THEN MESSAGE,'please provide the series to be reconstructed.'
IF N_ELEMENTS(Hilo) EQ 0 THEN Hilo='High'
IF N_ELEMENTS(low) EQ 0 THEN low =MIN(y)
IF N_ELEMENTS(high) EQ 0 THEN high =MAX(y)
IF N_ELEMENTS(dod) EQ 0 THEN dod =N_ELEMENTS(y)/5
IF N_ELEMENTS(looplim) EQ 0 THEN looplim =10
sHiLo = STRCMP(HiLo,'High',/FOLD_CASE) EQ 1?-1:1
ni=N_ELEMENTS(y)
noutmax = N_ELEMENTS(multi) NE 0 ? 3*(ni-dod):ni-dod
tmp_y=N_ELEMENTS(multi) NE 0 ?[y,y,y]:y
tmp_ni=N_ELEMENTS(multi) NE 0 ? 3*ni:ni
p = INTARR(1,tmp_ni)+1
pos = WHERE(tmp_y GT high OR tmp_y LT low,nout)
IF nout NE 0 THEN p[pos] = 0
;tmp_y=y
ready=0
nloop=0
WHILE ready EQ 0 AND nloop LT looplim DO BEGIN
;yr=CURVEFIT(ts,tmp_y,p
;pp=IMSL_CSSMOOTH(ts,y,weights=reform(p))
;yr=IMSL_SPVALUE(ts, pp)
;yr=SPlINEFIT(ts,Y,reform(p),ts[0:22:2],y[0:22:2],SIGYS,1.0)
; X=ts
; coeff = SPLINECOEFF(ts, y, lambda = 1000,sigm=reform(p))
; yr = FLTARR(N_ELEMENTS(y) - 1)
; x1 = ts[0:N_ELEMENTS(y)-2]+16
; FOR i = 0, N_ELEMENTS(y)-2 DO yr[i] = coeff.d[I] + $
; coeff.c[I] * (x[I+1]-x[I]) + $
; coeff.b[I] * (x[I+1]-x[I])^2 + $
; coeff.a[I] * (x[I+1]-x[I])^3
; savgolFilter = SAVGOL(4, 4, 0, 4)
; yr=CONVOL(y, savgolFilter, /EDGE_TRUNCATE)
CASE (smoother) OF
'HANTS': BEGIN
yr=r_ha(tmp_y,WEIGHT=p,_Extra=extrakeywords)
END
'WHITSMW': BEGIN
yr=WHITSMW(tmp_y,WEIGHT=p,_Extra=extrakeywords)
END
ELSE: BEGIN
END
ENDCASE
diff = sHiLo*(yr-tmp_y)
err = TRANSPOSE(p)*diff
;errsort_sub = SORT(err)
e_index=WHERE(err GE fet, count)
;maxerr=max(err,max_sub)
nout=nout+count
ready = (count EQ 0) OR (nout EQ noutmax)
IF ready EQ 1 THEN BREAK
p[e_index]=0
IF N_ELEMENTS(plot) NE 0 THEN cgplot,INDGEN(tmp_ni),yr,linestyle=1,/overplot
IF N_ELEMENTS(plot) NE 0 THEN cgplot,(INDGEN(tmp_ni))[e_index],tmp_y[e_index],psym=21,/overplot
ENDWHILE
; IF N_ELEMENTS(combine) NE 0 THEN BEGIN
; index=WHERE(p EQ 0, count)
; IF count NE 0 THEN tmp_y[index]=yr[index]
; yr[ni:2*ni-1]=R_ha(tmp_y[ni:2*ni-1],-3000)
; ENDIF ELSE BEGIN
;
; p[*]=1
; yr=WHITSMW(yr,REFORM(p),0.5,DEGREE=3)
; ENDELSE
IF N_ELEMENTS(plot) NE 0 THEN cgplot,INDGEN(tmp_ni),yr,linestyle=0,/overplot,color='blue'
IF N_ELEMENTS(multi) NE 0 THEN yr=yr[ni:2*ni-1]
IF N_ELEMENTS(ots) NE 0 THEN BEGIN
mi=N_ELEMENTS(its)
mo=N_ELEMENTS(ots)
values1=REPLICATE({y:0.,w:0.,yr:0.},mo)
values2=REPLICATE({y:0.,w:0.,yr:0.},mi)
values2.y=yr
values2.w=FLTARR(mi)+1
obj_hash=HASH(ots,values1,its,values2)
;obj_hash[its]=values2
x=(obj_hash.keys()).toarray()
index=SORT(x)
values_arr=((obj_hash.values()).toarray())[index]
values_arr.yr=whitsmddw(x[index], values_arr.y, WEIGHT=values_arr.w, LAMBDA=1, DEGREE=2)
;values_arr.yr=r_ha(values_arr.y,WEIGHT=values_arr.w,_Extra=extrakeywords)
obj_hash[x[index]]=values_arr
yr_hash=obj_hash[ots]
yr=(((yr_hash.values())[SORT((yr_hash.keys()).toarray())]).toarray()).yr
ENDIF
RETURN, yr
END
FUNCTION _conbine_ts,ots,its,I2O=i2o,O2O=o2o
ni=N_ELEMENTS(its)
no=N_ELEMENTS(ots)
IF ARG_PRESENT (i2o) THEN BEGIN
values1=INTARR(no)
values2=INTARR(ni)+1
obj_hash=HASH(ots,values1,its,values2)
;obj_hash[its]=values2
x=(obj_hash.keys()).toarray()
index=SORT(x)
values_arr=((obj_hash.values()).toarray())[index]
orts=x[index]
i2o=WHERE(values_arr EQ 1)
ENDIF
IF ARG_PRESENT (o2o) THEN BEGIN
values1=INTARR(ni)
values2=INTARR(no)+1
obj_hash=HASH(its,values1,ots,values2)
;obj_hash[its]=values2
x=(obj_hash.keys()).toarray()
index=SORT(x)
values_arr=((obj_hash.values()).toarray())[index]
orts=x[index]
o2o=WHERE(values_arr EQ 1)
ENDIF
RETURN, orts
END
PRO TSR_SESSION,imgsetfile,ni,start_l_arr,end_l_arr,$
tmp_packed_file,sample,line,datatype,its,ots,otype,$
flag_mask,cur_session,n_session,pref
on_error,0
catch,Error_status
if Error_status ne 0 then begin
catch,/cancel
PRINT, 'Error index: ', Error_status
PRINT, 'Error message: ', !ERROR_STATE.MSG
if n_elements(cur_line) gt 1 then begin
cur_line=0
shmunmap,'cur_line'
endif
MESSAGE, /REISSUE_LAST
endif
HILO='Low';'High';
LOW=0;11000;0;
HIGH=10;17500;10000;1000;100;
FET=0.5;250.0;500.0;50;5;
DOD=10
LOOPLIM= 10
fillvalue=0;-3000;-3000;32767;255;
if n_elements(otype) eq 0 then otype=datatype
print,systime(),'Begin session'
SHMMAP,'cur_line',n_session,/long
cur_line=SHMVAR('cur_line')
if flag_mask ne 0 then begin
; mask=read_tiff(maskfile)
SHMMAP,'mask_arr',DIMENSION=[sample,line],/byte
mask_arr=shmvar('mask_arr')
; help,mask_arr
;mask_arr[0]=mask
; flag_mask=1
endif
;get the # of sub task
n_sub_task=n_elements(start_l_arr)
;get the byte length of the datatype
nblen=N_TAGS({t:MAKE_ARRAY(1,type=datatype)},/data_length)
;open the temperal output file
openu, o_lun,tmp_packed_file,/get_lun
;constructe algrithm object
;intep_obj=obj_new('RECON_HA',TS=its,OTS=ots,/IOD,per=365,parr=[720,360,180,120,90],BATCH=1)
for i_sub_task=0,n_sub_task-1 do begin
start_line=start_l_arr[i_sub_task]
end_line=end_l_arr[i_sub_task]
;read image data
nsubline=end_line-start_line+1
img_data_arr=MAKE_ARRAY(sample,nsubline,ni,type=datatype)
img_data=MAKE_ARRAY(sample,nsubline,type=datatype)
OPENR,i_lun,imgsetfile,/get_lun
POINT_LUN,i_lun,start_line*nblen
FOR i_ni=0,ni-1 DO BEGIN
; print,line,i_ni,start_line,sample,nblen
; print,(LONG64(line)*i_ni+start_line)*sample*nblen
POINT_LUN,i_lun,(LONG64(line)*i_ni+start_line)*sample*nblen
;print,nblen,(LONG64(line)*i_ni+start_line)*sample*nblen
READU,i_lun,img_data
img_data_arr[*,*,i_ni]=img_data
ENDFOR
;create the output array
no=N_ELEMENTS(ots)
o_img_data_arr=MAKE_ARRAY(sample,nsubline,no,type=otype)+fillvalue
flag_arr=bytarr(sample,nsubline)
;reconstruction process
;PROFILER
;ROFILER, /SYSTEM
print,systime(),'Begin reconstruction',i_sub_task
FOR i_line=0,nsubline-1 DO BEGIN
;print,reform(mask_arr[*,start_line+i_line])
FOR i_sample=0,sample-1 DO BEGIN
if flag_mask ne 0 then begin
if mask_arr[i_sample,start_line+i_line] eq 0 then continue
endif
res=GENERRIZED_RCON_MULTI(HILO=HiLo,LOW=low,HIGH=high,FET=fet,DOD=dod,$
float(REFORM(img_data_arr[i_sample,i_line,*])),LOOPLIM=looplim,$
per=365,parr=[720,360,180,120],TS=its,OTS=ots)
; res=1
if n_elements(res) eq 1 then begin
o_img_data_arr[i_sample,i_line,*]= fillvalue
continue
;flag_arr[i_sample,i_line]=1
endif else begin
o_img_data_arr[i_sample,i_line,*]= res
endelse
;PRINT,i_sample,i_line
flag_arr[i_sample,i_line]=1
ENDFOR
;PRINT,i_line
cur_line[cur_session]=start_line+i_line
ENDFOR
print,systime(),'end reconstruction',i_sub_task
;print a test series to check the vilidation of the reconstruction
v_index=where(flag_arr eq 1,count) ;get all index of valid pixeles
;get a random index
if count ne 0 then begin
rt_index=v_index[0>fix(randomu(SYSTIME(1),1)*count)<(count-1)]
;t_sample=rt_index-rt_index/sample
t_line=rt_index/sample
t_sample=rt_index-t_line*sample
;t_sample=0>fix(randomu(SYSTIME(1),1)*sample)<(sample-1)
;t_line=0>fix(randomu(SYSTIME(1),1)*(end_line-start_line))<(end_line-start_line-1)
iseries=REFORM(img_data_arr[t_sample,t_line,*])
oseries=REFORM(o_img_data_arr[t_sample,t_line,*])
tfile=string(pref,t_sample,start_line+t_line,format='(A,".S",I05,"L",I05,".txt")')
openw,t_lun,tfile,/get_lun
printf,t_lun,'start line: ',start_line
printf,t_lun,'end line: ', end_line
printf,t_lun,'series for :', t_sample,start_line+t_line
printf,t_lun,'input time tag ','series value'
printf,t_lun,transpose([[its],[iseries]])
printf,t_lun,'output time tag ','series value'
printf,t_lun,transpose([[ots],[oseries]])
close,t_lun
;plot the sample series
pixw=cgpixmap()
ptitle=string(t_sample,start_line+t_line,format='("series for ","S",I05,"L",I05)')
cgplot,its,iseries,color='red',psym=2,YRANGE=[low,high],title=ptitle,/window
cgplot,ots,oseries,color='dodger blue',/addcmd,/overplot
cd,cur=cur
pngfile=filepath(strcompress(string(pref,t_sample,start_line+t_line,$
randomu(systime(1))*long64(2)^20,format='(A,"-S",I05,"L",I05,"-",I08,".png")'),/remove_all),root_dir=cur)
print,pngfile
pixw->output,pngfile
print,pngfile
cgdelete,pixw
endif
img_data_arr=0
;cd
;cgplot,ts,REFORM(img_data_arr[t_sample,t_line,*]),psym=3,output=string(t_sample,start_line+t_line,format='("S",I,"L",I,".ps")')
;cgplot,ots,REFORM(o_img_data_arr[t_sample,t_line,*]),psym=-5
;write the reconstruction result to temparal file
print,systime(),'Begin write file',i_sub_task
;get the byte length of the otype
noblen=N_TAGS({t:MAKE_ARRAY(1,type=otype)},/data_length)
for i=0,no-1 do begin
point_lun,o_lun,sample*(long64(line)*i+start_line)*noblen
writeu,o_lun,o_img_data_arr[*,*,i]
endfor
print,systime(),'end write file',i_sub_task
o_img_data_arr=0
endfor
;obj_destroy,intep_obj
; PROFILER,/report
cur_line=0
shmunmap,'cur_line'
if flag_mask ne 0 then begin
; mask=read_tiff(maskfile)
shmunmap,'mask_arr'
endif
print,systime(),'end session'
END
PRO imgset_recon, IMGSETFILE=imgsetfile,NI=ni, ITS=its, OTS=ots,OIMGSETFILES=oimgsetfiles,ODIR=odir,$
DATATYPE=dtype,OTYPE=otype,SAMPLE=sample,LINE=line,GEOTIFF=geotiff,MASKFILE=maskfile,PBAR=pbar,$
PREF=pref
; ON_ERROR,2
CATCH,Error_status
IF Error_status NE 0 THEN BEGIN
CATCH,/cancel
PRINT, 'Error index: ', Error_status
PRINT, 'Error message: ', !ERROR_STATE.MSG
IF N_ELEMENTS(bridge_arr) NE 0 THEN begin
for i=0,N_ELEMENTS(bridge_arr)-1 do begin
if bridge_arr[i_session]->Status() EQ 1 then bridge_arr[i_session]->abort
endfor
OBJ_DESTROY,bridge_arr
endif
IF N_ELEMENTS(o_lun) NE 0 THEN FREE_LUN,o_lun
IF N_ELEMENTS(tmp_lun) NE 0 THEN FREE_LUN,tmp_lun
if n_elements(cur_line) gt 1 then begin
cur_line=0
shmunmap,'cur_line'
endif
MESSAGE, /REISSUE_LAST
ENDIF
;ni=n_elements(its)
IF N_ELEMENTS(ni) EQ 0 THEN MESSAGE,'please give the # of images in the packed file.'
;if total(file_test(imglist,/regular)) le ni then message,'some of the image files do not exist. Please check again.'
IF N_ELEMENTS(ots) NE 0 THEN BEGIN
IF N_ELEMENTS(its) NE ni THEN MESSAGE,'The its should has the same length as image list.'
ENDIF
IF N_ELEMENTS(oimgsetfiles) EQ 0 THEN MESSAGE,'please specific a full path for output files.'
if N_elements(otype) eq 0 then otype=dtype
if n_elements(pref) eq 0 then pref='Unknown'
;IF N_ELEMENTS(oprefix) EQ 0 THEN oprefix='ReconPrefix'
;line=208 & sample=400000 & ni =23 & no =365
;first determine the # of procesessors to be used based on the total task
n_session=line/50+1 LE !CPU.HW_NCPU-1 ? line/50+1:!CPU.HW_NCPU-1
;Then determine the optimal subtasks for each processors consider the # of processor and aviliable physical memory
;The total pysical memory used by all processor can not exceed 70% of mochine's physical memory and each processor should hold equivalent task
Machine_mem=long64(2)^34
MEM_for_IDL_SESSION=Machine_mem*0.7/n_session
no=n_elements(ots)
;get the byte length of the datatype
nblen=N_TAGS({t:MAKE_ARRAY(1,type=4)},/data_length)
max_sub_task_line=fix(MEM_for_IDL_SESSION/(sample*nblen*(ni+no)))
sub_task_line=max_sub_task_line
n_session_sub_task=(line / (sub_task_line*n_session))
n_sub_task=n_session_sub_task*n_session
if n_sub_task lt n_session then begin
n_sub_task=n_session
n_session_sub_task=1
sub_task_line=line/n_session
endif
sub_task_line=line/n_sub_task
start_l_arr=indgen(n_sub_task)*sub_task_line
end_l_arr=start_l_arr+sub_task_line-1
r_line=line mod (n_sub_task*sub_task_line)
if r_line ne 0 then begin
start_l_arr[n_sub_task-r_line:n_sub_task-1]=start_l_arr[n_sub_task-r_line:n_sub_task-1]+indgen(r_line)
end_l_arr[n_sub_task-r_line:n_sub_task-1]=end_l_arr[n_sub_task-r_line:n_sub_task-1]+indgen(r_line)+1
endif
start_l_arr=reform(start_l_arr,n_session_sub_task,n_session)
end_l_arr=reform(end_l_arr,n_session_sub_task,n_session)
;print,transpose([[start_l_arr],[end_l_arr]])
;segemente the image for each session
;line=330
;n_session=1
;seg_line=line/n_session
;start_l_arr=BINDGEN(n_session)*seg_line
;end_l_arr=(BINDGEN(n_session)+1)*seg_line-1
;r_line=line MOD n_session
;start_l_arr[0:r_line]=start_l_arr[0:r_line]+1
;end_l_arr[n_session-1]=end_l_arr[n_session-1]+line MOD n_session
;print,n_session,start_l_arr,end_l_arr
cur=odir
tmpdir=FILEPATH('tmp',root_dir=cur)
IF ~file_test(tmpdir,/dir) THEN FILE_MKDIR,tmpdir
;restore save file
savfile=FILEPATH('TSR_SESSION.sav',ROOT_dir=cur,sub='tmp')
SAVE,/ROUTINES,FILENAME=savfile
;log file for each session
fname=pref+'.session'+string(INDGEN(n_session),format='(I02,".")')+string(randomu(systime(1),n_session)*long64(2)^20,format='(I08)')+'.log'
bridge_log_arr=FILEPATH(fname,ROOT_dir=cur,sub='tmp')
;temporal output file
;bridge_tmpo_arr=FILEPATH(STRING(INDGEN(n_session),FORMAT='("SESSION",I02,".dat")'),ROOT_dir=cur,sub='tmp')
tmp_packed_file=FILEPATH('recon_packed.dat',ROOT_dir=cur,sub='tmp')
;create the temperal output file
if ~file_test(tmp_packed_file) then begin
openw,1,tmp_packed_file
close,1
endif
;goto,tt
bridge_arr=OBJARR(n_session)
;create a shared memory to monitoring current processing state of of each session
SHMMAP,'cur_line',n_session,/long
cur_line=SHMVAR('cur_line')
cur_line[0]=reform(start_l_arr[0,*])
;if a maskfile avaliable then share the mask array between sessions
flag_mask=0
if n_elements(maskfile) ne 0 then begin
mask=read_tiff(maskfile)
SHMMAP,'mask_arr',DIMENSION=[sample,line],/byte
mask_arr=shmvar('mask_arr')
mask_arr[*,*]=mask
flag_mask=1
endif
;;create a shared memory to retrive the sample series from each sub session
;SHMMAP,'sample_series',dimention=[ni+no,n_session], type=dtype
;sample_series=shmvar('sample_series')
FOR i_session =0, n_session-1 DO BEGIN
bridge_arr[i_session]=OBJ_NEW('IDL_IDLBridge',$
OUTPUT=bridge_log_arr[i_session])
bridge_arr[i_session]->SetVar,'imgsetfile',imgsetfile
bridge_arr[i_session]->SetVar,'ni',ni
bridge_arr[i_session]->SetVar,'start_l_arr',start_l_arr[*,i_session]
bridge_arr[i_session]->SetVar,'end_l_arr',end_l_arr[*,i_session]
;bridge_arr[i_session]->SetVar,'tmpo',bridge_tmpo_arr[i_session]
bridge_arr[i_session]->SetVar,'tmp_packed_file',tmp_packed_file
bridge_arr[i_session]->SetVar,'sample',sample
bridge_arr[i_session]->SetVar,'line',line
bridge_arr[i_session]->SetVar,'datatype',dtype
bridge_arr[i_session]->SetVar,'its',its
bridge_arr[i_session]->SetVar,'ots',ots
bridge_arr[i_session]->SetVar,'otype',otype
bridge_arr[i_session]->SetVar,'flag_mask',flag_mask
bridge_arr[i_session]->SetVar,'cur_session',i_session
bridge_arr[i_session]->SetVar,'n_session',n_session
bridge_arr[i_session]->SetVar,'pref',pref
bridge_arr[i_session]->Execute,'COMPILE_OPT IDL2'
bridge_arr[i_session]->Execute,'cd,"'+cur+'"'
bridge_arr[i_session]->Execute,"restore,'"+savfile+"'"
;bridge_arr[i_session]->Execute,"help, /ROUTINES"
cmdStr='TSR_SESSION,imgsetfile,ni,start_l_arr,end_l_arr,'+ $
'tmp_packed_file,sample,line,datatype,its,ots,otype,'+ $
'flag_mask,cur_session,n_session,pref'
bridge_arr[i_session]->Execute,cmdStr,/NOWAIT
ENDFOR
;TSR_SESSION,imgsetfile,ni,start_l_arr[i_session],end_l_arr[i_session],bridge_tmpo_arr[i_session],sample,line,dtype,its,ots
flag=1
WHILE (flag) DO BEGIN
flag=0
FOR i_session=0,n_session-1 DO BEGIN
; if ~ obj_valid( bridge_arr[i_session]) then continue
; if bridge_arr[i_session]->Status() EQ 0 then OBJ_DESTROY,bridge_arr[i_session] & continue
IF (bridge_arr[i_session]->Status() EQ 3) THEN MESSAGE,STRING(i_session,FORMAT='("Session",I1,"failed!")')
flag=(bridge_arr[i_session]->Status() EQ 1) OR flag
; cur_line=bridge_arr[i_session]->getvar('cur_line')
;print,i_session,cur_line
ENDFOR
;print,cur_line
IF pbar -> CheckCancel() THEN BEGIN
ok = Dialog_Message('The user cancelled operation.')
FOR i_session=0,n_session-1 DO res=bridge_arr[i_session]->abort()
;destroy objects
OBJ_DESTROY,bridge_arr
cur_line=0
SHMUNMAP,'cur_line'
if flag_mask ne 0 then begin