-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathndinterp.py
executable file
·1144 lines (992 loc) · 45.9 KB
/
ndinterp.py
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
#! /usr/bin/env python
#Scale from 0 to 1
#Min ptp for lsq
#Rather than use 1 km v stack, could use same vstack res, then do interp for points at some stride, and map_coord to orig
#Define separate time ranges for interpolation (linearND) and extrapolation (map_coord)
#Moving Window Kriging
#Smoothness constraint in time and space?
#TopoWx has moving window kriging:
#https://github.com/jaredwo/topowx
import os
import sys
import multiprocessing as mp
from datetime import datetime, timedelta
import pickle
from copy import deepcopy
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import scipy.signal
import scipy.interpolate
from sklearn.gaussian_process import GaussianProcess
from scikits import umfpack
from lib import iolib
from lib import malib
from lib import timelib
from lib import geolib
from lib import pltlib
#Specify the input type
vtype = 'dem'
#This will attempt to load cached files on disk
load_existing = True
#Shelf mask
clip_to_shelfmask = False
lsq = True
#Set to solve for both dh/dt and tilt
both = True
#This does the interpolation for a particular time for all points defined by x and y coords
def dto_interp(interpf, x, y, dto):
return (dto, interpf(x, y, dto.repeat(x.size)).T)
def rangenorm(x, offset=None, scale=None):
if offset is None:
offset = x.min()
if scale is None:
scale = x.ptp()
return (x.astype(np.float64) - offset)/scale
#This repeats the first and last array in the stack with a specified time offset
def pad_stack(s, dt_offset=timedelta(365.25)):
o = s.ma_stack.shape
new_ma_stack = np.ma.vstack((s.ma_stack[0:1], s.ma_stack, s.ma_stack[-1:]))
new_date_list = np.ma.hstack((s.date_list[0:1] - dt_offset, s.date_list, s.date_list[-1:] + dt_offset))
new_date_list_o = timelib.dt2o(new_date_list)
return new_ma_stack, new_date_list_o
def sample_lsq(x_lsq_ma, ti):
ti = np.atleast_1d(ti)
zi = np.ma.masked_all((ti.size, x_lsq_ma.shape[1], x_lsq_ma.shape[2]))
order = x_lsq_ma.shape[0] - 1
for n,i in enumerate(ti):
if order == 1:
zi[n] = x_lsq_ma[0] + x_lsq_ma[1]*i
elif order == 2:
zi[n] = x_lsq_ma[0] + x_lsq_ma[1]*i + x_lsq_ma[2]*i**2
elif order == 3:
zi[n] = x_lsq_ma[0] + x_lsq_ma[1]*i + x_lsq_ma[2]*i**2 + x_lsq_ma[3]*i**3
return zi
def apply_mask(a, m):
a[:,m] = np.ma.masked
#dem_stack_fn = '/scr/pig_stack_20151201_tworows_highcount/shelf_clip/20071020_1438_glas_mean-adj_20150223_1531_1040010008556800_104001000855E100-DEM_32m_trans-adj_stack_189.npz'
#dem_stack_fn = '/scr/pig_stack_20151201_tworows_highcount/tworows_clip_goodtrend/20091207_20150406_DG_SPIRIT/20091207_1452_SPI_11-008_PIG_SPOT_DEM_V1_40m_hae_newfltr-trans_source-DEM-adj_20150406_1519_103001003F89B700_1030010040D68600-DEM_32m_trans-adj_stack_290.npz'
#This is trans only, both stereo and mono
#dem_stack_fn = '/scr/pig_stack_20151201_tworows_highcount/wv_stereo_mono_transonly/20101116_1411_1030010007A8AB00_1030010008813A00-DEM_32m_trans-adj_20150406_1519_103001003F89B700_1030010040D68600-DEM_32m_trans-adj_stack_247.npz'
#dem_stack_fn = '/scr/pig_stack_20151201_tworows_highcount/20021204_1925_atm_mean-adj_20150406_1519_103001003F89B700_1030010040D68600-DEM_32m_trans-adj_stack_320.npz'
#dem_stack_fn = '/scr/pig_stack_20151201_tworows_highcount/20080105_1447_SPI_09-009_PIG_SPOT_DEM_V1_40m_hae_newfltr-trans_source-DEM-adj_20150406_1519_103001003F89B700_1030010040D68600-DEM_32m_trans-adj_stack_295.npz'
#WV only
#dem_stack_fn = '/scr/pig_stack_20151201_tworows_highcount/orig_stack_all/20101009_1530_1030010007898D00_103001000799BD00-DEM_32m-adj_20150406_1519_103001003F89B700_1030010040D68600-DEM_32m_trans-adj_stack_328.npz'
dem_stack_fn = '/scr/pig_stack_20160307_tworows_highcount/full_extent/DG_LVIS_ATM_only_fortiltcorr/20091020_1718_lvis_256m-DEM_20150406_1519_103001003F89B700_1030010040D68600-DEM_32m_trans_stack_355.npz'
dem_stack_fn = '/scr/pig_stack_20160307_tworows_highcount/full_extent/DG_LVIS_ATM_only_fortiltcorr/20091020_1718_lvis_256m-DEM_20150406_1519_103001003F89B700_1030010040D68600-DEM_32m_trans_stack_355_nocorr_offset_-3.10m.npz'
#dem_stack_fn = '/scr/pig_stack_20160307_tworows_highcount/full_extent/DG_LVIS_ATM_only_fortiltcorr/20091020_1718_lvis_256m-DEM_20150406_1519_103001003F89B700_1030010040D68600-DEM_32m_trans_stack_249.npz'
#dem_stack_fn = '/scr/pig_stack_20160307_tworows_highcount/full_extent/DG_LVIS_ATM_only_fortiltcorr/20101116_1411_1030010007A8AB00_1030010008813A00-DEM_32m_trans_20150406_1519_103001003F89B700_1030010040D68600-DEM_32m_trans_stack_222.npz'
#vx_stack_fn = '/scr/pig_stack_20151201_tworows_highcount/vel/shelf_clip/20060921_1746_189days_20060521_0950-20061126_0822_alos_mos_Track-Pig06_vx_20150530_1556_22days_20150525_0356-20150605_0356_tsx_mos_track-MayJun15_vx_stack_19_clip.npz'
#vx_stack_fn = '/scr/pig_stack_20151201_tworows_highcount/vel_20160222_500m/shelf_clip/20060921_1746_189days_20060521_0950-20061126_0822_alos_mos_Track-Pig06_vx_20150530_1556_22days_20150525_0356-20150605_0356_tsx_mos_track-MayJun15_vx_stack_20_clip.npz'
#vx_stack_fn = '/scr/pig_stack_20151201_tworows_highcount/vel_20160222_1km/shelf_clip/20060921_1746_189days_20060521_0950-20061126_0822_alos_mos_Track-Pig06_vx_20150530_1556_22days_20150525_0356-20150605_0356_tsx_mos_track-MayJun15_vx_stack_20_clip.npz'
vx_stack_fn = '/scr/pig_stack_20151201_tworows_highcount/vel_20160225_1km/shelf_clip/20060921_1746_189days_20060521_0950-20061126_0822_alos_mos_Track-Pig06_vx_20151109_1349_90days_20151014_0000-20160112_0000_ls8_mos_All_S1516_vx_stack_22_clip.npz'
#vy_stack_fn = '/scr/pig_stack_20151201_tworows_highcount/vel_20160222_500m/shelf_clip/20060921_1746_189days_20060521_0950-20061126_0822_alos_mos_Track-Pig06_vy_20150530_1556_22days_20150525_0356-20150605_0356_tsx_mos_track-MayJun15_vy_stack_20_clip.npz'
#vy_stack_fn = '/scr/pig_stack_20151201_tworows_highcount/vel_20160222_1km/shelf_clip/20060921_1746_189days_20060521_0950-20061126_0822_alos_mos_Track-Pig06_vy_20150530_1556_22days_20150525_0356-20150605_0356_tsx_mos_track-MayJun15_vy_stack_20_clip.npz'
vy_stack_fn = '/scr/pig_stack_20151201_tworows_highcount/vel_20160225_1km/shelf_clip/20060921_1746_189days_20060521_0950-20061126_0822_alos_mos_Track-Pig06_vy_20151109_1349_90days_20151014_0000-20160112_0000_ls8_mos_All_S1516_vy_stack_22_clip.npz'
#vm_stack_fn = '/scr/pig_stack_20151201_tworows_highcount/vel_20160222_500m/shelf_clip/20060921_1746_189days_20060521_0950-20061126_0822_alos_mos_Track-Pig06_vm_20150530_1556_22days_20150525_0356-20150605_0356_tsx_mos_track-MayJun15_vm_stack_20_clip.npz'
#vm_stack_fn = '/scr/pig_stack_20151201_tworows_highcount/vel_20160222_1km/shelf_clip/20060921_1746_189days_20060521_0950-20061126_0822_alos_mos_Track-Pig06_vm_20150530_1556_22days_20150525_0356-20150605_0356_tsx_mos_track-MayJun15_vm_stack_20_clip.npz'
vm_stack_fn = '/scr/pig_stack_20151201_tworows_highcount/vel_20160225_1km/shelf_clip/20060921_1746_189days_20060521_0950-20061126_0822_alos_mos_Track-Pig06_vm_20151109_1349_90days_20151014_0000-20160112_0000_ls8_mos_All_S1516_vm_stack_22_clip.npz'
#This is just full-shelf mosaics
#vm_stack_fn = '/scr/pig_stack_20151201_tworows_highcount/vel_20160225_1km/shelf_clip/20060921_1746_189days_20060521_0950-20061126_0822_alos_mos_Track-Pig06_vm_20151109_1349_90days_20151014_0000-20160112_0000_ls8_mos_All_S1516_vm_stack_8.npz'
#vm_stack = np.ma.sqrt(vx_stack.ma_stack**2 + vy_stack.ma_stack**2)
#These are for 256 m grids
#test = vm_stack.ma_stack[:,230:250,350:370]
#test = vx_stack.ma_stack[:,190:230,110:150]
#test = vm_stack.ma_stack[:,160:180,230:260]
#These are for 500 m grids
#Near GL
#test = vm_stack.ma_stack[:,20:50,190:220]
#N shelf, shear margin
#test = vm_stack.ma_stack[:,133:160,101:135]
if vtype == 'vm':
stack_fn = vm_stack_fn
elif vtype == 'vx':
stack_fn = vx_stack_fn
elif vtype == 'vy':
stack_fn = vy_stack_fn
elif vtype == 'dem':
stack_fn = dem_stack_fn
stack_fn = sys.argv[1]
stack = malib.DEMStack(stack_fn=stack_fn, save=False, trend=True, med=True, stats=True)
#Get times of original obs
t = stack.date_list_o.data
if clip_to_shelfmask:
mask_fn = '/scr/pig_dem_stack/pig_shelf_poly_shean_2011gl.shp'
outermask_fn = '/scr/pig_stack_20151201_tworows_highcount/pig_vel_mask.shp'
m_orig = geolib.shp2array(mask_fn, res=stack.res, extent=stack.extent)
outermask = ~(geolib.shp2array(outermask_fn, res=stack.res, extent=stack.extent))
m = m_orig
#Expand shelf mask 3 km upstream for flux gates
#Should also reduce edge artifacts
import scipy.ndimage
it = int(np.ceil(3000./stack.res))
m = ~(scipy.ndimage.morphology.binary_dilation(~m, iterations=it))
#Need to make sure to sample mask at appropriate res
apply_mask(stack.ma_stack, m)
if vtype == 'vm' or vtype == 'vy' or vtype == 'vx':
apply_mask(stack.ma_stack, outermask)
#This is used frome here on out
test = stack.ma_stack
test_ptp = stack.dt_stack_ptp
test_source = np.array(stack.source)
res = stack.res
gt = np.copy(stack.gt)
if vtype == 'dem':
stride = 4
test = test[:,::stride,::stride]
test_ptp = test_ptp[::stride,::stride]
res *= stride
print "Using a stride of %i (%0.1f m)" % (stride, res)
gt[[1,5]] *= stride
def make_test():
nt = 30
shp = (nt,40,60)
res = 2000
test = np.ma.masked_all(shp)
init = np.full((shp[1], shp[2]), 300)
#Some spatial distribution of dhdt
dhdt = np.full((shp[1], shp[2]), -2/365.25)
ti_min = timelib.dt2o(datetime(2008,1,1))
ti_max = timelib.dt2o(datetime(2015,4,1))
ti = np.sort(np.random.random(nt) * (ti_max - ti_min))
for i,t in enumerate(ti):
test[i] = init + dhdt * ti[i]
tilt_factor = (110000/res)
i = nt/2
dx = 3./tilt_factor
dy = 1./tilt_factor
dz = -0.5
print i, dx, dy, dz
test[i] = apply_tilt(test[i], -dx, -dy, -dz)
i = 2
dx = -1.5/tilt_factor
dy = 0.1/tilt_factor
dz = 0.4
print i, dx, dy, dz
test[i] = apply_tilt(test[i], -dx, -dy, -dz)
i = -2
dx = 0.1/tilt_factor
dy = 0.1/tilt_factor
dz = 2.5
print i, dx, dy, dz
test[i] = apply_tilt(test[i], -dx, -dy, -dz)
#x,y,z = malib.get_xyz(test[nt/2])
#d = (dx*x + dy*y + dz).reshape((shp[1], shp[2]))
#test[nt/2] += d
#Need to add t_min back to ti
return test, (ti + ti_min), res
#def apply_tilt(a, dx, dy, dz):
def apply_tilt(a, xref, yref, dx, dy, dz):
x,y,z = malib.get_xyz(a)
#d = dx*x + dy*y + dz
#xref = np.mean(x)
#yref = np.mean(y)
d = dx*(x-xref) + dy*(y-yref) + dz
#return a - d.reshape((a.shape[0], a.shape[1]))
#out = np.ma.masked_all_like(a)
#out[y.astype(int),x.astype(int)] = z - d
tilt = np.ma.masked_all_like(a)
tilt[y.astype(int),x.astype(int)] = d
out = a - tilt
return out, tilt
def apply_tilt_map(a, gt, dx, dy, dz):
x,y = geolib.get_xy_ma(a, gt)
tilt = dx*x + dy*y + dz
out = a - tilt
return out, tilt
#This is synthetic test case
#test, t, res = make_test()
#test_ptp = np.full_like(test[0], t[-1] - t[0])
print "Orig shape: ", test.shape
#Check to make sure all t have valid data
tcount = test.reshape(test.shape[0], test.shape[1]*test.shape[2]).count(axis=1)
validt_idx = (tcount > 0).nonzero()[0]
test = test[validt_idx]
test_source = test_source[validt_idx]
t = t[validt_idx]
print "New shape: ", test.shape
#Ben suggested running the vx/vy ratio
#Smoothing the scalar magnitude, then recomputing components
"""
#intergrid
#Note: this won't work with different spatial distribution of missing data over time
#Maybe round timestamps to nearest day, then treat time interval as daily with LOTS of missing data
import intergrid
lo=np.array([x.min(), y.min()])
hi=np.array([x.max(), y.max()])
maps = np.array([x, y])
interfunc = intergrid.Intergrid(test, lo=lo, hi=hi)
"""
if not lsq:
#test_med = malib.nanfill(test, np.nanmedian, axis=0)
#x,y,dummy = malib.get_xyz(test_med)
y, x = (test.count(axis=0) > 0).nonzero()
x = x.astype(int)
y = y.astype(int)
#vm_t = test.reshape(test.shape[0], test.shape[1]*test.shape[2])
vm_t = test[:,y,x]
vm_t_flat = vm_t.ravel()
idx = ~np.ma.getmaskarray(vm_t_flat)
#These are values
VM = vm_t_flat[idx]
X = np.tile(x, t.size)[idx]
Y = np.tile(y, t.size)[idx]
T = np.repeat(t, x.size)[idx]
#These are coords
pts = np.vstack((X,Y,T)).T
#Normalized versions
Xn = rangenorm(X)
Yn = rangenorm(Y)
Tn = rangenorm(T)
ptsn = np.vstack((Xn,Yn,Tn)).T
#Interpoalte at these times
#ti = np.arange(t.min(), t.max(), 90.0)
ti_min = timelib.dt2o(datetime(2008,1,1))
#ti_min = timelib.dt2o(datetime(2012,1,1))
#ti_max = timelib.dt2o(datetime(2009,1,1))
#ti_max = timelib.dt2o(datetime(2014,9,1))
#ti_max = timelib.dt2o(datetime(2015,4,1))
ti_max = timelib.dt2o(datetime(2015,6,1))
#ti_dt = 120
#ti_dt = 121.75
ti_dt = 91.3125
#ti_dt = 365.25
#Interpolate at these times
ti = np.arange(ti_min, ti_max, ti_dt)
#ti = t
#Annual - use for discharge analysis?
#ti = timelib.dt2o([datetime(2008,1,1), datetime(2009,1,1), datetime(2010,1,1), datetime(2011,1,1), datetime(2012,1,1), datetime(2013,1,1), datetime(2014,1,1), datetime(2015,1,1)])
if lsq:
#LSQ Polynomial fitting
order = 1
#Unique indices
min_count = 4
min_ptp = 1.5 * 365.25
#This works for stack of coregistered DEMs
#max_std = 3
max_std = 4
#Set this for test case
#max_std = 99
min_z = 10
#y, x = (test.count(axis=0) >= min_count).nonzero()
#validmask = ((test.count(axis=0) >= min_count) & (test_ptp >= min_ptp)).data
print "Applying validmask"
print "min count: ", min_count
print "min ptp (days): ", min_ptp
print "max std (m): ", max_std
print "min z (m): ", min_z
validmask = (test.count(axis=0) >= min_count) & (test_ptp >= min_ptp) & (test.std(axis=0) <= max_std) & (test.mean(axis=0) > min_z)
validmask = validmask.data
#This masks main shelf and area upstream of GL
shp_fn = '/scr/pig_stack_20151201_tworows_highcount/pig_mainshelfmargins_upstreamtrunk_mask_for_tiltcorr.shp'
m = geolib.shp2array(shp_fn, res=res, extent=stack.extent)
validmask = validmask & m
#Exclude pixels over shelf
if False:
shp_fn = '/scr/pig_dem_stack/pig_shelf_poly_shean_2011gl.shp'
m = geolib.shp2array(shp_fn, res=res, extent=stack.extent)
validmask = validmask & m
#Mask areas based on preliminary trend and residuals
if True:
test_linreg = malib.ma_linreg(test, np.ma.array(timelib.o2dt(t)))
#This works for stack of coregistered DEMs
#detrended_thresh = 1.2
detrended_thresh = 3.0
validmask = validmask & (test_linreg[2] < detrended_thresh).data
trend_thresh = 2.0
validmask = validmask & (np.abs(test_linreg[0]) < trend_thresh).data
"""
#Now do this at the end, using original stack
f = malib.iv(test_linreg[0], clim=(-3,1), label='Linear trend (m/yr)')
f_fn = os.path.splitext(stack_fn)[0]+'_tiltcorr_trend_before.png'
f.savefig(f_fn, dpi=300)
f = malib.iv(test_linreg[2], clim=(0,1), label='Detrended std (m)')
f_fn = os.path.splitext(stack_fn)[0]+'_tiltcorr_detrended_std_before.png'
f.savefig(f_fn, dpi=300)
"""
#f = malib.iv(stack.std, clim=(0,5), label='Std (m)')
#f_fn = os.path.splitext(stack_fn)[0]+'_tiltcorr_std_before.png'
#f.savefig(f_fn, dpi=300)
#f = malib.iv(validmask, cmap='gray', clim=(0,1), title='Tilt correction mask')
f = malib.iv(validmask, cmap='gray', clim=(0,1))
f_fn = os.path.splitext(stack_fn)[0]+'_tiltcorr_mask.png'
f.savefig(f_fn, dpi=300)
#Get valid indices
y, x = validmask.nonzero()
#Should probably check to make sure still have nonzero count for each input after validmask
#Should scale everything from 0 to 1
xref = np.mean(x)
yref = np.mean(y)
#t_ref = t.min()
t_ref = t.mean()
#t_ref = 0
#Scale x and y with same factor
#xn = (x - xref)/x.ptp()
#xn = x - xref
xn = x
#yn = (y - yref)/x.ptp()
#yn = y - yref
yn = y
#tn = (t - t_ref)/t.ptp()
tn = t - t_ref
#Remove median from every pixel
#This helps limit the constraint range
test_ref = np.ma.median(test, axis=0)
#Can use 0, but intercept will be 0 to 1200 m
#test_ref = 0
#test_ref = np.ma.median(test)
testn = test - test_ref
#Note: some of the input DEMs may be completely masked
testn[:,~validmask] = np.ma.masked
#Then weight
if both:
N = (order + 1)*x.size + (3 * t.size)
M = testn.count()
A = scipy.sparse.lil_matrix((M,N))
b = np.zeros(M)
Aidx = dict(zip(zip(y,x), range(M)))
#This is minimum width of DEM to allow for tilt correction
#min_width = 40000/res
min_width = 40000
m = 0
print "Populating matrices: A=(%ix%i), b=(%i)" % (M,N,M)
xrefa = []
yrefa = []
for k in range(t.size):
#Check source
#If ATM/LVIS, tilt and offset should be 0, increased weight on dhdt
#Trans should have increased weight on dhdt, not tilt
#Not trans - Remove ASP bias
#Mono reduced weight on tilt
#print k
a = testn[k]
#za should already have ref removed
xa,ya,za = malib.get_xyz(a)
xam,yam = geolib.get_xy_ma(a, gt)
xam = xam.compressed()
yam = yam.compressed()
zam = a.compressed()
#Normalize with global offset and scaling
#xan = (xa - xref)/x.ptp()
#yan = (ya - yref)/x.ptp()
#xan = xa
#yan = ya
#Note: want to use local offset for each DEM
#Constraints are set for individual DEMs
#Proper tilt values can be very high if global offset is used
xref = np.mean(xam)
xrefa.append(xref)
xan = xam - xref
yref = np.mean(yam)
yrefa.append(yref)
yan = yam - yref
if xa.size > 0:
aidx = np.array([Aidx[myxy] for myxy in zip(ya,xa)])
#Intercept
A[np.arange(m,m+aidx.size), aidx] = 1
#Linear trend
A[np.arange(m,m+aidx.size), (x.size)+aidx] = tn[k]
#Add higher order terms
#Planar coefficients
#Only add planar coefficients if DG
#if not bool(re.search('GLAS|LVIS|ATM', test_source[k])):
if 'DG' in test_source[k]:
#This only computes tilt for longer DEMs
#if np.sqrt(xa**2+ya**2).ptp() > min_width:
dist = np.sqrt(xam**2+yam**2)
#Exclude isolated pixels
dist_perc = (5,95)
dist_ptp = np.percentile(dist, dist_perc).ptp()
if dist_ptp > min_width:
A[m:m+aidx.size, ((order+1)*x.size)+k*3+0] = xan[:,np.newaxis]
A[m:m+aidx.size, ((order+1)*x.size)+k*3+1] = yan[:,np.newaxis]
A[m:m+aidx.size, ((order+1)*x.size)+k*3+2] = np.ones((xan.size, 1))
b[m:m+aidx.size] = za
m += aidx.size
else:
print k, t[k], 'No valid pixels remain after masking'
else:
#Compute fit coefficients for each pixel
N = (order + 1) * x.size
#Number of unique timesteps at all locations
M = testn.count(axis=0)[y,x].sum()
print "Populating matrices: A=(%ix%i), b=(%i)" % (M,N,M)
A = scipy.sparse.lil_matrix((M,N))
b = np.zeros(M)
n = 0
m = 0
Aidx = {}
for i,j in zip(y,x):
#Extract values for a single point
#Should have M timestamps
b = testn[:,i,j]
bidx = (~np.ma.getmaskarray(b)).nonzero()[0]
#if bidx.size >= min_count:
bvalid = b[bidx].data
#b0 = np.median(bvalid)
b[n:n+bidx.size] = bvalid
A[n:n+bidx.size, m] = np.ones((bidx.size,1))
if order == 1:
A[n:n+bidx.size, m+1:m+1+order] = tn[bidx, np.newaxis]
elif order == 2:
A[n:n+bidx.size, m+1:m+1+order] = np.array([tn[bidx], tn[bidx]**2]).T
elif order == 3:
A[n:n+bidx.size, m+1:m+1+order] = np.array([tn[bidx], tn[bidx]**2, tn[bidx]**3]).T
#Aidx[(i,j)] = (slice(n,n+bidx.size),slice(m,m+1+order))
Aidx[(i,j)] = slice(m,m+1+order)
n += bidx.size
m += (order + 1)
#Regularize
if True:
print "Adding regularization terms"
#These are expected magnitudes of each parameter, in meters or relevant units
#If using test_ref = 0, this will be ~500-1000
#Eint = 600
#If removing median
Eint = 10
Edhdt = 1
#Ex = (0.3/1E5)*res
#Mapped coord
Ex = (0.2/1E5)
Ey = Ex/3.0
Ez = 0.3
print Eint, Edhdt, Ex, Ey, Ez
E = np.ones(N)
#These are intercepts
E[0:x.size] = Eint
#dh/dt
E[x.size:(order+1)*x.size] = Edhdt
#x
E[(order+1)*x.size::3] = Ex
#y
E[(order+1)*x.size+1::3] = Ey
#Vertical offset of plane
E[(order+1)*x.size+2::3] = Ez
#Increase tolerance for DEMs without ICP co-registration
if True:
import re
nocorr_idx = np.nonzero([bool(re.search('nocorr', i)) for i in test_source])[0]
E[(order+1)*x.size+nocorr_idx+2] = 1.0
#Mono should have relaxed tilt and offset constraints
if True:
import re
mono_idx = np.nonzero([bool(re.search('mono', i)) for i in test_source])[0]
E[(order+1)*x.size+mono_idx] *= 2.0
E[(order+1)*x.size+mono_idx+1] *= 2.0
E[(order+1)*x.size+mono_idx+2] *= 2.0
#Invert
E = 1./E
#Note: just set these coefficients to 0 initially
#Just setting these to 0 still allows tilt for altimetry
if False:
import re
notilt_idx = np.nonzero([bool(re.search('GLAS|LVIS|ATM', i)) for i in test_source])[0]
E[(order+1)*x.size+notilt_idx] = 0.0
E[(order+1)*x.size+notilt_idx+1] = 0.0
E[(order+1)*x.size+notilt_idx+2] = 0.0
rA = scipy.sparse.diags(E, 0)
rb = np.zeros(N)
"""
rA = scipy.sparse.lil_matrix((3*tn.size,N))
rA[np.arange(0,3*tn.size),np.arange(N-3*tn.size,N)] = 100000
rb = np.zeros(3*tn.size)
"""
A = scipy.sparse.vstack([A,rA])
b = np.hstack([b,rb])
if True:
#Add spatial smoothness constraint
#This is pretty inefficient for large systems
print "Preparing Smoothness Constraint"
SC = scipy.sparse.lil_matrix((x.size,N))
SC_b = np.zeros(x.size)
#This sets order of constraint, use 1 for the linear term, 0 for the intercept
i = 1
for n,(key,value) in enumerate(Aidx.iteritems()):
key_up = (key[0]-1,key[1])
key_down = (key[0]+1,key[1])
key_left = (key[0],key[1]-1)
key_right = (key[0],key[1]+1)
ud = False
#The [1] index for dictionary values is when multiple values are written
if (key_up in Aidx) and (key_down in Aidx):
#SC[n,Aidx[key][1]] = 2
#SC[n,Aidx[key_up][1]] = -1
#SC[n,Aidx[key_down][1]] = -1
SC[n,i*x.size+Aidx[key]] = 2
SC[n,i*x.size+Aidx[key_up]] = -1
SC[n,i*x.size+Aidx[key_down]] = -1
ud = True
if (key_left in Aidx) and (key_right in Aidx):
if ud:
#SC[n,Aidx[key][1]] = 4
SC[n,i*x.size+Aidx[key]] = 4
else:
#SC[n,Aidx[key][1]] = 2
SC[n,i*x.size+Aidx[key]] = 2
#SC[n,Aidx[key_left][1]] = -1
#SC[n,Aidx[key_right][1]] = -1
SC[n,i*x.size+Aidx[key_left]] = -1
SC[n,i*x.size+Aidx[key_right]] = -1
print "Combining matrices"
A = scipy.sparse.vstack([A,SC])
b = np.hstack([b,SC_b])
"""
Tikhonov regularization
#http://scicomp.stackexchange.com/questions/10671/tikhonov-regularization-in-the-non-negative-least-square-nnls-pythonscipy
Err = scipy.sparse.eye(M,N)*5.0
Err_b = np.zeros(M)
A = scipy.sparse.vstack([A,Err])
b = np.hstack([b,Err_b])
"""
print "Converting"
A = A.tocsr()
if True:
f = plt.figure(figsize=(3,7.5))
plt.spy(A, precision='present', markersize=1, color='k')
plt.tight_layout()
f_fn = os.path.splitext(stack_fn)[0]+'_sparse_matrix.png'
f.savefig(f_fn, dpi=300)
print "Solving"
#x_lsq = scipy.sparse.linalg.lsqr(A, b, show=True)
AT = A.T
x_lsq = scipy.sparse.linalg.spsolve(AT*A, AT*b, use_umfpack=True)
#x_lsq = umfpack.spsolve(AT*A, AT*b)
x_lsq = x_lsq[np.newaxis,:]
print "Preparing output"
if both:
x_lsq_a = x_lsq[0][0:(order+1)*x.size].reshape((order+1, x.size)).T
x_lsq_ma = np.ma.masked_all((order+1, test.shape[1], test.shape[2]))
for i in range(order+1):
x_lsq_ma[i,y,x] = x_lsq_a[:,i]
#for i in range(order+1):
# for coord,j in Aidx.iteritems():
# x_lsq_ma[i,coord[0],coord[1]] = x_lsq_a[j,i]
tilt = x_lsq[0][(order+1)*x.size:].reshape((t.size, 3))
f = plt.figure()
ax = plt.gca()
plt.title('Tilt correction magnitude')
plt.axhline(0, color='k', linestyle=':')
plt.axvline(0, color='k', linestyle=':')
plt.scatter(tilt[:,0]*1E5, tilt[:,1]*1E5, c=tilt[:,2], s=16, cmap='RdBu', vmin=-2, vmax=2)
plt.xlabel("X-tilt (m / 100 km)")
plt.ylabel("Y-tilt (m / 100 km)")
plt.colorbar(label="Z-offset (m)", extend='both')
ax.set_aspect('equal')
f_fn = os.path.splitext(stack_fn)[0]+'_tiltcorr_scatter.pdf'
f.savefig(f_fn)
ax.set_aspect('auto')
plt.xlim(-2,2)
plt.ylim(-0.1,0.1)
f_fn = os.path.splitext(stack_fn)[0]+'_tiltcorr_scatter_zoom.pdf'
f.savefig(f_fn)
#3D plot of tilt
#f = plt.figure()
#ax = f.add_subplot(111)
#ax = f.add_subplot(111, projection='3d')
#ax.scatter(tilt[:,0], tilt[:,1], tilt[:,2])
else:
#Extract coefficients
x_lsq_a = x_lsq[0].reshape((x.size, order+1))
#Populate original grids
x_lsq_ma = np.ma.masked_all((order+1, test.shape[1], test.shape[2]))
for i in range(order+1):
x_lsq_ma[i,y,x] = x_lsq_a[:,i]
#malib.iv(x_lsq_ma[1]*365.25, clim=(-3,3), cmap='RdBu')
#Add the z offset back to the computed offset of linear dh/dt
x_lsq_ma[0] += test_ref
#malib.iv(x_lsq_ma[0])
#malib.iv(x_lsq_ma[1]*365.25)
#f = malib.iv(x_lsq_ma[1]*365.25, clim=(-3, 1))
"""
#These are
#tilt_idx = np.array([10, 2, -2])
tilt_idx = np.array([3, 2, -2])
for i in tilt_idx:
print tilt[i]
print test[i]
print apply_tilt(test[i], *tilt[i])
print sample_lsq(x_lsq_ma, t[i] - t_ref)
"""
#Some of these are nan
xrefa = np.ma.fix_invalid(xrefa)
yrefa = np.ma.fix_invalid(yrefa)
xrefa_filled = xrefa.filled(0)
yrefa_filled = yrefa.filled(0)
tilt_idx = np.all(tilt == 0, axis=1)
#Convert to original map coordinates
#Add offset back to relative tilt
tilt[:,2] -= tilt[:,0]*xrefa_filled + tilt[:,1]*yrefa_filled
#Update stack source with 'tiltcorr'
test_tiltcorr = np.ma.masked_all_like(test)
test_tiltcorr_source = np.array(test_source, dtype='|S32')
test_tilt = np.ma.masked_all_like(test)
#Apply to the sampled ma
#for i in range(test.shape[0]):
##test_tiltcorr[i] = apply_tilt(test[i], *tilt[i])
#test_tiltcorr[i], test_tilt[i] = apply_tilt_map(test[i], gt, *tilt[i])
#Make sure stride is 1!
#if stride == 1:
if True:
print "Creating copies of input stack"
stack_tiltcorr = deepcopy(stack)
stack_tiltcorr_fn = os.path.splitext(stack_fn)[0] + '_lsq_tiltcorr.npz'
stack_tiltcorr.stack_fn = stack_tiltcorr_fn
stack_tilt = deepcopy(stack)
stack_tilt_fn = os.path.splitext(stack_fn)[0] + '_lsq_tilt.npz'
stack_tilt.stack_fn = stack_tilt_fn
print "Applying correction"
#validt_idx are the times that made it into test array
for i,n in enumerate(validt_idx):
out_tiltcorr, out_tilt = apply_tilt_map(stack.ma_stack[n], stack.gt, *tilt[i])
stack_tiltcorr.ma_stack[n] = out_tiltcorr
stack_tiltcorr.source[n] = stack_tiltcorr.source[n]+'_tiltcorr'
stack_tilt.ma_stack[n] = out_tilt
stack_tilt.source[n] = stack_tilt.source[n]+'_tiltcorr'
print "Writing out corrected stacks"
stack_tiltcorr.compute_stats()
stack_tiltcorr.write_stats()
stack_tiltcorr.compute_trend()
stack_tiltcorr.write_trend()
stack_tiltcorr.savestack()
print "Writing out final corrections to csv"
out_fn = os.path.splitext(stack_tiltcorr_fn)[0] + '.csv'
#out = tilt
#hdr = 'tiltx,tilty,tiltz'
out = zip(np.array(stack.fn_list)[validt_idx], stack.date_list[validt_idx], stack.date_list_o[validt_idx], \
tilt[:,0], tilt[:,1], tilt[:,2])
hdr = 'fn,date,date_o,tiltx,tilty,tiltz'
np.savetxt(out_fn, out, delimiter=',', fmt='%s', header=hdr)
#Should add filename and date fields
print "Writing out tilt stack"
stack_tilt.compute_stats()
#stack_tilt.write_stats()
stack_tilt.savestack()
f = malib.iv(stack_tilt.stack_min, clim=(-5,5), cmap='RdBu', label='Tilt Correction Min (m)')
f_fn = os.path.splitext(stack_fn)[0]+'_tilt_min.png'
f.savefig(f_fn, dpi=300)
f = malib.iv(stack_tilt.stack_max, clim=(-5,5), cmap='RdBu', label='Tilt Correction Max (m)')
f_fn = os.path.splitext(stack_fn)[0]+'_tilt_min.png'
f.savefig(f_fn, dpi=300)
f = malib.iv(stack_tilt.stack_std, clim=(0,5), label='Std (m)')
f_fn = os.path.splitext(stack_fn)[0]+'_tilt_std.png'
f.savefig(f_fn, dpi=300)
#Review tilts
#for i in range(0,50):
# malib.iv(test_tilt[i], clim=(-2,2), cmap='RdBu')
#Original stack stats
f = malib.iv(stack.stack_trend, clim=(-3,1), label='Linear Trend (m/yr)')
f_fn = os.path.splitext(stack_fn)[0]+'_tiltcorr_trend_before.png'
f.savefig(f_fn, dpi=300)
f = malib.iv(stack.stack_detrended_std, clim=(0,1), label='Detrended Std (m)')
f_fn = os.path.splitext(stack_fn)[0]+'_tiltcorr_detrended_std_before.png'
f.savefig(f_fn, dpi=300)
#f = malib.iv(test.std(axis=0), clim=(0,5), label='Std (m)')
f = malib.iv(stack.stack_std, clim=(0,5), label='Std (m)')
f_fn = os.path.splitext(stack_fn)[0]+'_tiltcorr_std_before.png'
f.savefig(f_fn, dpi=300)
#Post-tiltcorr stats
#test_tiltcorr_linreg = malib.ma_linreg(test_tiltcorr, np.ma.array(timelib.o2dt(t)))
#f = malib.iv(test_tiltcorr_linreg[0], clim=(-3,1), label='Linear Trend (m/yr)')
f = malib.iv(stack_tiltcorr.stack_trend, clim=(-3,1), label='Linear Trend (m/yr)')
f_fn = os.path.splitext(stack_fn)[0]+'_tiltcorr_trend_after.png'
f.savefig(f_fn, dpi=300)
#f = malib.iv(test_tiltcorr_linreg[2], clim=(0,1), label='Detrended Std (m)')
f = malib.iv(stack_tiltcorr.stack_detrended_std, clim=(0,1), label='Detrended Std (m)')
f_fn = os.path.splitext(stack_fn)[0]+'_tiltcorr_detrended_std_after.png'
f.savefig(f_fn, dpi=300)
#f = malib.iv(test_tiltcorr.std(axis=0), clim=(0,5), label='Std (m)')
f = malib.iv(stack_tiltcorr.stack_std, clim=(0,5), label='Std (m)')
f_fn = os.path.splitext(stack_fn)[0]+'_tiltcorr_std_after.png'
f.savefig(f_fn, dpi=300)
#Computing tilt stats
print "Computing tilt stats"
r = stack_tilt.ma_stack
r_flat = r.reshape(r.shape[0], r.shape[1]*r.shape[2])
r_stats = np.vstack([r_flat.count(axis=1), r_flat.mean(axis=1), r_flat.std(axis=1), r_flat.ptp(axis=1)]).T
#Split by source
f = plt.figure()
for lbl,m in [('DG_stereo', 's'), ('DG_stereo_nocorr', 'o'), ('DG_mono', 'D'), ('DG_mono_nocorr', 'd')]:
idx = np.nonzero([bool(re.search(lbl+'$', i)) for i in test_source])[0]
plt.errorbar(stack.date_list[idx], r_stats[idx,1], yerr=r_stats[idx,3]/2.0, linestyle='None', marker=m, label=lbl, markersize=5)
plt.ylabel('DEM correction (m)')
plt.title('DEM Offset and Tilt Magnitude')
plt.axhline(0, color='k', linestyle=':')
plt.legend(loc='lower right', prop={'size':10})
ax = plt.gca()
pltlib.fmt_date_ax(ax)
pltlib.pad_xaxis(ax)
plt.draw()
f_fn = os.path.splitext(stack_fn)[0]+'_tiltcorr_offset+tilt.pdf'
f.savefig(f_fn)
#Should write this information out, sorting by biggest tilt/offset
sys.exit()
#malib.iv(np.ma.array(test.std(axis=0), mask=validmask), clim=(0,5))
#malib.iv(np.ma.array(test.std(axis=0), mask=~validmask), clim=(0,5))
#Should sort by size of tilt correction, then plot in ascending order, putting biggest on top
z_tiltcorr = sample_lsq(x_lsq_ma, t - t_ref)
zi = sample_lsq(x_lsq_ma, ti - t_ref)
r = test_tiltcorr - z_tiltcorr
r_flat = r.reshape(r.shape[0], r.shape[1]*r.shape[2])
r_stats = np.vstack([r_flat.count(axis=1), r_flat.mean(axis=1), r_flat.std(axis=1)]).T
r_stats = np.vstack([r_flat.count(axis=1), np.ma.median(r_flat, axis=1), np.ma.fix_invalid([malib.mad(i) for i in r_flat])]).T
r_stats[r_stats[:,0] < 400] = np.ma.masked
#malib.iv(r.std(axis=0))
malib.iv(r.std(axis=0), clim=(0,1))
rnmad = malib.mad_ax0(r)
malib.iv(rnmad)
if False:
zi_origt = sample_lsq(x_lsq_ma, t - t_ref)
zi_origt_r = test - zi_origt
#Look at residuals for bad corrections
zi_origt_r_2d = zi_origt_r.reshape((zi_origt_r.shape[0], zi_origt_r.shape[1]*zi_origt_r.shape[2]))
zi_origt_r_med = np.ma.median(zi_origt_r_2d, axis=1)
zi_origt_r_count = np.ma.count(zi_origt_r_2d, axis=1)
zi_origt_r_nmad = [malib.mad(i) for i in zi_origt_r_2d]
zi_origt_r_idx = np.arange(zi_origt_r_2d.shape[0])
zi_origt_r_val = np.vstack([zi_origt_r_idx, zi_origt_r_count, zi_origt_r_med, zi_origt_r_nmad]).T
#zi_origt_r_val[zi_origt_r_val[:,3].argsort()]
contextfig, contextax = plt.subplots(1)
contextax.imshow(x_lsq_ma[1]*365.25)
random_idx = np.random.random_integers(0,y.shape[0]-1, 8)
for i in random_idx:
yi, xi = y[i],x[i]
fig, ax = plt.subplots(1)
title = 'Sample: (x=%s, y=%s)' % (xi, yi)
#Note: stack slope/intercept are relative to 0 ordinal in years
zi_orig = np.ma.array([test_linreg[0]*(i/365.25) + test_linreg[1] for i in ti])
ax.set_title(title)
ax.plot_date(ti, zi[:,yi,xi], color='b', marker=None, linestyle='--', label='LSQ dh/dt')
ax.plot_date(ti, zi_orig[:,yi,xi], color='r', marker=None, linestyle='--', label='Orig dh/dt')
ax.plot_date(t, test[:,yi,xi], color='r', markersize=5, label='Orig')
ax.plot_date(t, test_tiltcorr[:,yi,xi], color='b', markersize=4, label='Corr')
ax.legend(loc='lower center')
pltlib.fmt_date_ax(ax)
contextax.scatter(xi, yi, color='k')
sys.exit()
#Populate coordinate arrays for each timestep
xi = np.tile(x, ti.size)
yi = np.tile(y, ti.size)
ptsi = np.array((xi, yi, ti.repeat(x.size))).T
xin = rangenorm(xi, X.min(), X.ptp())
yin = rangenorm(yi, Y.min(), Y.ptp())
tin = rangenorm(ti, T.min(), T.ptp())
"""
#Radial basis function interpolation
#Need to normalize to input cube
print "Running Rbf interpolation for %i points" % X.size
rbfi = scipy.interpolate.Rbf(Xn,Yn,Tn,VM, function='linear', smooth=0.1)
#rbfi = scipy.interpolate.Rbf(Xn,Yn,Tn,VM, function='gaussian', smooth=0.000001)
#rbfi = scipy.interpolate.Rbf(Xn,Yn,Tn,VM, function='inverse', smooth=0.00001)
print "Sampling result at %i points" % xin.size
vmi_rbf = rbfi(xin, yin, tin.repeat(x.size))
vmi_rbf_ma[:,y,x] = np.ma.fix_invalid(vmi_rbf.reshape((ti.size, x.shape[0])))
"""
#Attempt to load interpolation function
#int_fn = 'LinearNDint_%i_%i_%i.pck' % (ti.size, test.shape[1], test.shape[2])
#Should add stack_fn here
#int_fn = 'LinearNDint_%s_%i_%i.pck' % (vtype, test.shape[1], test.shape[2])
int_fn = '%s_LinearNDint_%i_%i.pck' % (os.path.splitext(stack_fn)[0], test.shape[1], test.shape[2])
print int_fn
if load_existing and os.path.exists(int_fn):
print "Loading pickled interpolation function: %s" % int_fn
f = open(int_fn, 'rb')
linNDint = pickle.load(f)
else:
#LinearND interpolation
print "Running LinearND interpolation for %i points" % X.size
#Note: this breaks qhull for lots of input points - works for 500m and 1km grids over PIG shelf, not 256m
linNDint = scipy.interpolate.LinearNDInterpolator(pts, VM, rescale=True)
print "Saving pickled interpolation function: %s" % int_fn
f = open(int_fn, 'wb')
pickle.dump(linNDint, f, protocol=2)
f.close()
"""
#NearestND interpolation (fast)
print "Running NearestND interpolation for %i points" % X.size
NearNDint = scipy.interpolate.NearestNDInterpolator(pts, VM, rescale=True)
"""
#vmi_fn = 'LinearNDint_%s_%i_%i_%iday.npy' % (vtype, test.shape[1], test.shape[2], ti_dt)
vmi_fn = '%s_%iday.npy' % (os.path.splitext(int_fn)[0], ti_dt)
if load_existing and os.path.exists(vmi_fn):
print 'Loading existing interpolated stack: %s' % vmi_fn
vmi_ma = np.ma.fix_invalid(np.load(vmi_fn))
else:
#Once tesselation is complete, sample each timestep
#Use multiprocessing here?
#http://stackoverflow.com/questions/18597435/why-does-scipy-interpolate-griddata-hang-when-used-with-multiprocessing-and-open
print "Sampling %i points at %i timesteps" % (x.size, ti.size)
#Prepare array to hold output
vmi_ma = np.ma.masked_all((ti.size, test.shape[1], test.shape[2]))
"""
#This does all points at once
#vmi = linNDint(ptsi)
#vmi_ma[:,y,x] = np.ma.fix_invalid(vmi.reshape((ti.size, x.shape[0])))
#This does interpolation serially by timestep
for n, i in enumerate(ti):
print n, i, timelib.o2dt(i)
vmi_ma[n,y,x] = linNDint(x, y, i.repeat(x.size)).T
"""
#Parallel
pool = mp.Pool(processes=None)
results = [pool.apply_async(dto_interp, args=(linNDint, x, y, i)) for i in ti]
results = [p.get() for p in results]
results.sort()
for n, r in enumerate(results):
print n, r[0], timelib.o2dt(r[0])
vmi_ma[n,y,x] = r[1]
vmi_ma = np.ma.fix_invalid(vmi_ma)
print 'Saving interpolated stack: %s' % vmi_fn
np.save(vmi_fn, vmi_ma.filled(np.nan))
origt = False
if origt:
print "Sampling %i points at %i original timesteps" % (x.size, t.size)
vmi_ma_origt = np.ma.masked_all((t.size, test.shape[1], test.shape[2]))
#Parallel
pool = mp.Pool(processes=None)
results = [pool.apply_async(dto_interp, args=(linNDint, x, y, i)) for i in t]
results = [p.get() for p in results]
results.sort()
for n, r in enumerate(results):
print n, r[0], timelib.o2dt(r[0])
vmi_ma_origt[n,y,x] = r[1]
vmi_ma_origt = np.ma.fix_invalid(vmi_ma_origt)
#print 'Saving interpolated stack: %s' % vmi_fn
#np.save(vmi_fn, vmi_ma.filled(np.nan))
#vmi = scipy.interpolate.griddata(pts, VM, ptsi, method='linear', rescale=True)
"""
#Kriging
#Should explore this more - likely the best option
#http://connor-johnson.com/2014/03/20/simple-kriging-in-python/
#http://resources.esri.com/help/9.3/arcgisengine/java/gp_toolref/geoprocessing_with_3d_analyst/using_kriging_in_3d_analyst.htm
#PyKrige does moving window Kriging, but only in 2D
#https://github.com/bsmurphy/PyKrige/pull/5
#Could do tiled kriging with overlap in parallel
#Split along x and y direction, preserve all t
#Need to generate semivariogram globally though, then pass to each tile
#See malib sliding_window
wx = wy = 30