-
Notifications
You must be signed in to change notification settings - Fork 54
/
netcdf_output.py
executable file
·1398 lines (1168 loc) · 66.5 KB
/
netcdf_output.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 python3
# Yang Lei, Jet Propulsion Laboratory
# November 2017
import datetime
import os
import netCDF4
import numpy as np
import pandas as pd
def get_satellite_attribute(info):
mission_mapping = {
'L': 'Landsat ',
'S': 'Sentinel-',
}
satellite_1 = f'{mission_mapping[info["mission_img1"]]}{info["satellite_img1"]}'
satellite_2 = f'{mission_mapping[info["mission_img2"]]}{info["satellite_img2"]}'
if satellite_1 != satellite_2:
return f'{satellite_1} and {satellite_2}'
return satellite_1
def v_error_cal(vx_error, vy_error):
vx = np.random.normal(0, vx_error, 1000000)
vy = np.random.normal(0, vy_error, 1000000)
v = np.sqrt(vx**2 + vy**2)
return np.std(v)
def netCDF_packaging_intermediate(Dx, Dy, InterpMask, ChipSizeX, GridSpacingX, ScaleChipSizeY, SearchLimitX,
SearchLimitY, origSize, noDataMask, filename='./autoRIFT_intermediate.nc'):
nc_outfile = netCDF4.Dataset(filename, 'w', clobber=True, format='NETCDF4')
# First set global attributes that GDAL uses when it reads netCDF files
nc_outfile.setncattr('date_created', datetime.datetime.now().strftime("%d-%b-%Y %H:%M:%S"))
nc_outfile.setncattr('title', 'autoRIFT intermediate results')
nc_outfile.setncattr('author', 'Alex S. Gardner, JPL/NASA; Yang Lei, GPS/Caltech')
nc_outfile.setncattr('institution', 'NASA Jet Propulsion Laboratory (JPL), California Institute of Technology')
# set dimensions
dimidY, dimidX = Dx.shape
nc_outfile.createDimension('x', dimidX)
nc_outfile.createDimension('y', dimidY)
x = np.arange(0, dimidX, 1)
y = np.arange(0, dimidY, 1)
chunk_lines = np.min([np.ceil(8192/dimidY)*128, dimidY])
ChunkSize = [chunk_lines, dimidX]
nc_outfile.createDimension('x1', noDataMask.shape[1])
nc_outfile.createDimension('y1', noDataMask.shape[0])
var = nc_outfile.createVariable('x', np.dtype('int32'), ('x',), fill_value=None)
var.setncattr('standard_name', 'x_index')
var.setncattr('description', 'x index')
var[:] = x
var = nc_outfile.createVariable('y', np.dtype('int32'), ('y',), fill_value=None)
var.setncattr('standard_name', 'y_index')
var.setncattr('description', 'y index')
var[:] = y
var = nc_outfile.createVariable('Dx', np.dtype('float32'), ('y', 'x'), fill_value=np.nan,
zlib=True, complevel=2, shuffle=True, chunksizes=ChunkSize)
var.setncattr('standard_name', 'x_offset')
var.setncattr('description', 'x offset')
var[:] = Dx.astype(np.float32)
var = nc_outfile.createVariable('Dy', np.dtype('float32'), ('y', 'x'), fill_value=np.nan,
zlib=True, complevel=2, shuffle=True, chunksizes=ChunkSize)
var.setncattr('standard_name', 'y_offset')
var.setncattr('description', 'y offset')
var[:] = Dy.astype(np.float32)
var = nc_outfile.createVariable('InterpMask', np.dtype('uint8'), ('y', 'x'), fill_value=0,
zlib=True, complevel=2, shuffle=True, chunksizes=ChunkSize)
var.setncattr('standard_name', 'interpolated_value_mask')
var.setncattr('description', 'light interpolation mask')
var[:] = np.round(np.clip(InterpMask, 0, 255)).astype('uint8')
var = nc_outfile.createVariable('ChipSizeX', np.dtype('uint16'), ('y', 'x'), fill_value=0,
zlib=True, complevel=2, shuffle=True, chunksizes=ChunkSize)
var.setncattr('standard_name', 'chip_size_x')
var.setncattr('description', 'width of search window')
var[:] = np.round(np.clip(ChipSizeX, 0, 65535)).astype('uint16')
var = nc_outfile.createVariable('SearchLimitX', np.dtype('uint8'), ('y', 'x'), fill_value=0,
zlib=True, complevel=2, shuffle=True, chunksizes=ChunkSize)
var.setncattr('standard_name', 'search_limit_x')
var.setncattr('description', 'search limit x')
var[:] = np.round(np.clip(SearchLimitX, 0, 255)).astype('uint8')
var = nc_outfile.createVariable('SearchLimitY', np.dtype('uint8'), ('y', 'x'), fill_value=0,
zlib=True, complevel=2, shuffle=True, chunksizes=ChunkSize)
var.setncattr('standard_name', 'search_limit_y')
var.setncattr('description', 'search limit y')
var[:] = np.round(np.clip(SearchLimitY, 0, 255)).astype('uint8')
var = nc_outfile.createVariable('noDataMask', np.dtype('uint8'), ('y1', 'x1'), fill_value=0,
zlib=True, complevel=2, shuffle=True, chunksizes=ChunkSize)
var.setncattr('standard_name', 'nodata_value_mask')
var.setncattr('description', 'nodata value mask')
var[:] = np.round(np.clip(noDataMask, 0, 255)).astype('uint8')
var = nc_outfile.createVariable('GridSpacingX', np.dtype('uint16'), (), fill_value=None)
var.setncattr('standard_name', 'GridSpacingX')
var.setncattr('description', 'grid spacing x')
var[0] = GridSpacingX
var = nc_outfile.createVariable('ScaleChipSizeY', np.dtype('float32'), (), fill_value=None)
var.setncattr('standard_name', 'ScaleChipSizeY')
var.setncattr('description', 'scale of chip size in y to chip size in x')
var[0] = ScaleChipSizeY
var = nc_outfile.createVariable('origSizeX', np.dtype('uint16'), (), fill_value=None)
var.setncattr('standard_name', 'origSizeX')
var.setncattr('description', 'original array size x')
var[0] = origSize[1]
var = nc_outfile.createVariable('origSizeY', np.dtype('uint16'), (), fill_value=None)
var.setncattr('standard_name', 'origSizeY')
var.setncattr('description', 'original array size y')
var[0] = origSize[0]
nc_outfile.sync() # flush data to disk
nc_outfile.close()
def netCDF_read_intermediate(filename='./autoRIFT_intermediate.nc'):
inter_file = netCDF4.Dataset(filename, mode='r')
Dx = inter_file.variables['Dx'][:].data
Dy = inter_file.variables['Dy'][:].data
InterpMask = inter_file.variables['InterpMask'][:].data
ChipSizeX = inter_file.variables['ChipSizeX'][:].data
SearchLimitX = inter_file.variables['SearchLimitX'][:].data
SearchLimitY = inter_file.variables['SearchLimitY'][:].data
noDataMask = inter_file.variables['noDataMask'][:].data
noDataMask = noDataMask.astype('bool')
GridSpacingX = inter_file.variables['GridSpacingX'][:].data
ScaleChipSizeY = inter_file.variables['ScaleChipSizeY'][:].data
origSize = (inter_file.variables['origSizeY'][:].data, inter_file.variables['origSizeX'][:].data)
return Dx, Dy, InterpMask, ChipSizeX, GridSpacingX, ScaleChipSizeY, SearchLimitX, SearchLimitY, origSize, noDataMask
def netCDF_packaging(VX, VY, DX, DY, INTERPMASK, CHIPSIZEX, CHIPSIZEY, SSM, SSM1, SX, SY,
offset2vx_1, offset2vx_2, offset2vy_1, offset2vy_2, offset2vr, offset2va, scale_factor_1, scale_factor_2, MM, VXref, VYref,
DXref, DYref, rangePixelSize, azimuthPixelSize, dt, epsg, srs, tran, out_nc_filename, pair_type,
detection_method, coordinates, IMG_INFO_DICT, stable_count, stable_count1, stable_shift_applied,
dx_mean_shift, dy_mean_shift, dx_mean_shift1, dy_mean_shift1, error_vector):
vx_mean_shift = offset2vx_1 * dx_mean_shift + offset2vx_2 * dy_mean_shift
temp = vx_mean_shift
temp[np.logical_not(SSM)] = np.nan
# vx_mean_shift = np.median(temp[(temp > -500)&(temp < 500)])
vx_mean_shift = np.median(temp[np.logical_not(np.isnan(temp))])
vy_mean_shift = offset2vy_1 * dx_mean_shift + offset2vy_2 * dy_mean_shift
temp = vy_mean_shift
temp[np.logical_not(SSM)] = np.nan
# vy_mean_shift = np.median(temp[(temp > -500)&(temp < 500)])
vy_mean_shift = np.median(temp[np.logical_not(np.isnan(temp))])
vx_mean_shift1 = offset2vx_1 * dx_mean_shift1 + offset2vx_2 * dy_mean_shift1
temp = vx_mean_shift1
temp[np.logical_not(SSM1)] = np.nan
vx_mean_shift1 = np.median(temp[np.logical_not(np.isnan(temp))])
vy_mean_shift1 = offset2vy_1 * dx_mean_shift1 + offset2vy_2 * dy_mean_shift1
temp = vy_mean_shift1
temp[np.logical_not(SSM1)] = np.nan
vy_mean_shift1 = np.median(temp[np.logical_not(np.isnan(temp))])
V = np.sqrt(VX**2+VY**2)
if pair_type == 'radar':
dr_2_vr_factor = np.median(offset2vr[np.logical_not(np.isnan(offset2vr))])
SlantRangePixelSize = np.median(offset2vr[np.logical_not(np.isnan(offset2vr))]) * dt/365.0/24.0/3600.0
azimuthPixelSize = np.median(offset2va[np.logical_not(np.isnan(offset2va))]) * dt/365.0/24.0/3600.0
# VR = DX * rangePixelSize / dt * 365.0 * 24.0 * 3600.0
VR = DX * offset2vr
VR = VR.astype(np.float32)
# VA = DY * azimuthPixelSize / dt * 365.0 * 24.0 * 3600.0
VA = DY * offset2va
VA = VA.astype(np.float32)
VRref = DXref * offset2vr
VRref = VRref.astype(np.float32)
VAref = DYref * offset2va
VAref = VAref.astype(np.float32)
# vr_mean_shift = dx_mean_shift * rangePixelSize / dt * 365.0 * 24.0 * 3600.0
vr_mean_shift = dx_mean_shift * offset2vr
vr_mean_shift = np.median(vr_mean_shift[np.logical_not(np.isnan(vr_mean_shift))])
# va_mean_shift = dy_mean_shift * azimuthPixelSize / dt * 365.0 * 24.0 * 3600.0
va_mean_shift = dy_mean_shift * offset2va
va_mean_shift = np.median(va_mean_shift[np.logical_not(np.isnan(va_mean_shift))])
# vr_mean_shift1 = dx_mean_shift1 * rangePixelSize / dt * 365.0 * 24.0 * 3600.0
vr_mean_shift1 = dx_mean_shift1 * offset2vr
vr_mean_shift1 = np.median(vr_mean_shift1[np.logical_not(np.isnan(vr_mean_shift1))])
# va_mean_shift1 = dy_mean_shift1 * azimuthPixelSize / dt * 365.0 * 24.0 * 3600.0
va_mean_shift1 = dy_mean_shift1 * offset2va
va_mean_shift1 = np.median(va_mean_shift1[np.logical_not(np.isnan(va_mean_shift1))])
# create the (slope parallel & reference) flow-based range-projected result
alpha_sp = (DX * scale_factor_1) / (offset2vy_2 / (offset2vx_1 * offset2vy_2 - offset2vx_2 * offset2vy_1) * (-SX) - offset2vx_2 / (offset2vx_1 * offset2vy_2 - offset2vx_2 * offset2vy_1) * (-SY))
alpha_ref = (DX * scale_factor_1) / (offset2vy_2 / (offset2vx_1 * offset2vy_2 - offset2vx_2 * offset2vy_1) * VXref - offset2vx_2 / (offset2vx_1 * offset2vy_2 - offset2vx_2 * offset2vy_1) * VYref)
VXS = alpha_sp * (-SX)
VYS = alpha_sp * (-SY)
VXR = alpha_ref * VXref
VYR = alpha_ref * VYref
zero_flag_sp = (SX == 0) & (SY == 0)
zero_flag_ref = (VXref == 0) & (VYref == 0)
VXS[zero_flag_sp] = np.nan
VYS[zero_flag_sp] = np.nan
VXR[zero_flag_ref] = np.nan
VYR[zero_flag_ref] = np.nan
rngX = offset2vx_1
rngY = offset2vy_1
angle_df_S = np.arccos((-SX * rngX - SY * rngY) / (np.sqrt(SX**2 + SY**2) * np.sqrt(rngX**2+rngY**2)))
angle_df_S = np.abs(np.real(angle_df_S) - np.pi / 2)
angle_df_R = np.arccos((VXref * rngX + VYref * rngY) / (np.sqrt(VXref**2 + VYref**2) * np.sqrt(rngX**2+rngY**2)))
angle_df_R = np.abs(np.real(angle_df_R) - np.pi / 2)
angle_threshold_S = 0.75
angle_threshold_R = 0.75
VXS[angle_df_S < angle_threshold_S] = np.nan
VYS[angle_df_S < angle_threshold_S] = np.nan
VXR[angle_df_R < angle_threshold_R] = np.nan
VYR[angle_df_R < angle_threshold_R] = np.nan
# obsolete fusion routine using the sp mask file to distinguish pure
# smoothed slopes and reference velocity fields
# VXP = VXS
# VXP[MM == 1] = VXR[MM == 1]
# VYP = VYS
# VYP[MM == 1] = VYR[MM == 1]
# FIXME: Switch to using the updated (better) estimates of velocity fields when available
# Use the updated dhdxs and dhdys input files that combine the velocity fields and smoothed slopes
VXP = VXS
VYP = VYS
# use the updated (better) estimates of velocity fields
# VXP = VXR
# VYP = VYR
VXP = VXP.astype(np.float32)
VYP = VYP.astype(np.float32)
VP = np.sqrt(VXP**2+VYP**2)
VXPP = VX.copy()
VYPP = VY.copy()
stable_count_p = np.sum(SSM & np.logical_not(np.isnan(VXP)))
stable_count1_p = np.sum(SSM1 & np.logical_not(np.isnan(VXP)))
vxp_mean_shift = 0.0
vxp_mean_shift1 = 0.0
vyp_mean_shift = 0.0
vyp_mean_shift1 = 0.0
if stable_count_p != 0:
temp = VXP.copy() - VX.copy()
temp[np.logical_not(SSM)] = np.nan
# bias_mean_shift = np.median(temp[(temp > -500)&(temp < 500)])
bias_mean_shift = np.median(temp[np.logical_not(np.isnan(temp))])
vxp_mean_shift = vx_mean_shift + bias_mean_shift / 1
temp = VYP.copy() - VY.copy()
temp[np.logical_not(SSM)] = np.nan
# bias_mean_shift = np.median(temp[(temp > -500)&(temp < 500)])
bias_mean_shift = np.median(temp[np.logical_not(np.isnan(temp))])
vyp_mean_shift = vy_mean_shift + bias_mean_shift / 1
if stable_count1_p != 0:
temp = VXP.copy() - VX.copy()
temp[np.logical_not(SSM1)] = np.nan
# bias_mean_shift1 = np.median(temp[(temp > -500)&(temp < 500)])
bias_mean_shift1 = np.median(temp[np.logical_not(np.isnan(temp))])
vxp_mean_shift1 = vx_mean_shift1 + bias_mean_shift1 / 1
temp = VYP.copy() - VY.copy()
temp[np.logical_not(SSM1)] = np.nan
# bias_mean_shift1 = np.median(temp[(temp > -500)&(temp < 500)])
bias_mean_shift1 = np.median(temp[np.logical_not(np.isnan(temp))])
vyp_mean_shift1 = vy_mean_shift1 + bias_mean_shift1 / 1
if stable_count_p == 0:
if stable_count1_p == 0:
stable_shift_applied_p = 0
else:
stable_shift_applied_p = 2
else:
stable_shift_applied_p = 1
CHIPSIZEX = CHIPSIZEX * rangePixelSize
CHIPSIZEY = CHIPSIZEY * azimuthPixelSize
NoDataValue = -32767
noDataMask = np.isnan(VX) | np.isnan(VY)
# VXref[noDataMask] = NoDataValue
# VYref[noDataMask] = NoDataValue
# if pair_type == 'radar':
# VRref[noDataMask] = NoDataValue
# VAref[noDataMask] = NoDataValue
CHIPSIZEX[noDataMask] = 0
CHIPSIZEY[noDataMask] = 0
INTERPMASK[noDataMask] = 0
title = 'autoRIFT surface velocities'
author = 'Alex S. Gardner, JPL/NASA; Yang Lei, GPS/Caltech'
institution = 'NASA Jet Propulsion Laboratory (JPL), California Institute of Technology'
# VX = np.round(np.clip(VX, -32768, 32767)).astype(np.int16)
# VY = np.round(np.clip(VY, -32768, 32767)).astype(np.int16)
# V = np.round(np.clip(V, -32768, 32767)).astype(np.int16)
# if pair_type == 'radar':
# VR = np.round(np.clip(VR, -32768, 32767)).astype(np.int16)
# VA = np.round(np.clip(VA, -32768, 32767)).astype(np.int16)
# CHIPSIZEX = np.round(np.clip(CHIPSIZEX, 0, 65535)).astype(np.uint16)
# CHIPSIZEY = np.round(np.clip(CHIPSIZEY, 0, 65535)).astype(np.uint16)
# INTERPMASK = np.round(np.clip(INTERPMASK, 0, 255)).astype(np.uint8)
source = f'NASA MEaSUREs ITS_LIVE project. Processed with autoRIFT version ' \
f'{IMG_INFO_DICT["autoRIFT_software_version"]}'
if IMG_INFO_DICT['mission_img1'].startswith('S'):
source += f'. Contains modified Copernicus Sentinel data {IMG_INFO_DICT["date_center"][0:4]}, processed by ESA'
if IMG_INFO_DICT['mission_img1'].startswith('L'):
source += f'. Landsat-{IMG_INFO_DICT["satellite_img1"]} images courtesy of the U.S. Geological Survey'
references = 'When using this data, please acknowledge the source (see global source attribute) and cite:\n' \
'* Gardner, A. S., Moholdt, G., Scambos, T., Fahnestock, M., Ligtenberg, S., van den Broeke, M.,\n' \
' and Nilsson, J., 2018. Increased West Antarctic and unchanged East Antarctic ice discharge over\n' \
' the last 7 years. The Cryosphere, 12, p.521. https://doi.org/10.5194/tc-12-521-2018\n' \
'* Lei, Y., Gardner, A. and Agram, P., 2021. Autonomous Repeat Image Feature Tracking (autoRIFT)\n' \
' and Its Application for Tracking Ice Displacement. Remote Sensing, 13(4), p.749.\n' \
' https://doi.org/10.3390/rs13040749\n' \
'\n' \
'Additionally, a DOI is provided for the software used to generate this data:\n' \
'* autoRIFT: https://doi.org/10.5281/zenodo.4025445\n' \
tran = [tran[0] + tran[1]/2, tran[1], 0.0, tran[3] + tran[5]/2, 0.0, tran[5]]
clobber = True # overwrite existing output nc file
nc_outfile = netCDF4.Dataset(out_nc_filename, 'w', clobber=clobber, format='NETCDF4')
# First set global attributes that GDAL uses when it reads netCFDF files
nc_outfile.setncattr('GDAL_AREA_OR_POINT', 'Area')
nc_outfile.setncattr('Conventions', 'CF-1.8')
nc_outfile.setncattr('date_created', datetime.datetime.now().strftime("%d-%b-%Y %H:%M:%S"))
nc_outfile.setncattr('title', title)
nc_outfile.setncattr('autoRIFT_software_version', IMG_INFO_DICT["autoRIFT_software_version"])
nc_outfile.setncattr('scene_pair_type', pair_type)
nc_outfile.setncattr('satellite', get_satellite_attribute(IMG_INFO_DICT))
nc_outfile.setncattr('motion_detection_method', detection_method)
nc_outfile.setncattr('motion_coordinates', coordinates)
nc_outfile.setncattr('author', author)
nc_outfile.setncattr('institution', institution)
nc_outfile.setncattr('source', source)
nc_outfile.setncattr('references', references)
var = nc_outfile.createVariable('img_pair_info', 'U1', (), fill_value=None)
var.setncattr('standard_name', 'image_pair_information')
for key in IMG_INFO_DICT:
if key == 'autoRIFT_software_version':
continue
var.setncattr(key, IMG_INFO_DICT[key])
# set dimensions
dimidY, dimidX = VX.shape
nc_outfile.createDimension('x', dimidX)
nc_outfile.createDimension('y', dimidY)
x = np.arange(tran[0], tran[0] + tran[1] * dimidX, tran[1])
y = np.arange(tran[3], tran[3] + tran[5] * dimidY, tran[5])
chunk_lines = np.min([np.ceil(8192/dimidY)*128, dimidY])
ChunkSize = [chunk_lines, dimidX]
var = nc_outfile.createVariable('x', np.dtype('float64'), 'x', fill_value=None)
var.setncattr('standard_name', 'projection_x_coordinate')
var.setncattr('description', 'x coordinate of projection')
var.setncattr('units', 'm')
var[:] = x
var = nc_outfile.createVariable('y', np.dtype('float64'), 'y', fill_value=None)
var.setncattr('standard_name', 'projection_y_coordinate')
var.setncattr('description', 'y coordinate of projection')
var.setncattr('units', 'm')
var[:] = y
mapping_var_name = 'mapping' # need to set this as an attribute for the image variables
var = nc_outfile.createVariable(mapping_var_name, 'U1', (), fill_value=None)
if srs.GetAttrValue('PROJECTION') == 'Polar_Stereographic':
var.setncattr('grid_mapping_name', 'polar_stereographic')
var.setncattr('straight_vertical_longitude_from_pole', srs.GetProjParm('central_meridian'))
var.setncattr('false_easting', srs.GetProjParm('false_easting'))
var.setncattr('false_northing', srs.GetProjParm('false_northing'))
var.setncattr('latitude_of_projection_origin', np.sign(srs.GetProjParm('latitude_of_origin'))*90.0) # could hardcode this to be -90 for landsat - just making it more general, maybe...
var.setncattr('latitude_of_origin', srs.GetProjParm('latitude_of_origin'))
var.setncattr('semi_major_axis', float(srs.GetAttrValue('GEOGCS|SPHEROID', 1)))
var.setncattr('scale_factor_at_projection_origin', 1)
var.setncattr('inverse_flattening', float(srs.GetAttrValue('GEOGCS|SPHEROID', 2)))
var.setncattr('spatial_ref', srs.ExportToWkt())
var.setncattr('crs_wkt', srs.ExportToWkt())
var.setncattr('proj4text', srs.ExportToProj4())
var.setncattr('spatial_epsg', epsg)
var.setncattr('GeoTransform', ' '.join(str(x) for x in tran)) # note this has pixel size in it - set explicitly above
elif srs.GetAttrValue('PROJECTION') == 'Transverse_Mercator':
var.setncattr('grid_mapping_name', 'universal_transverse_mercator')
zone = epsg - np.floor(epsg/100)*100
var.setncattr('utm_zone_number', zone)
var.setncattr('longitude_of_central_meridian', srs.GetProjParm('central_meridian'))
var.setncattr('false_easting', srs.GetProjParm('false_easting'))
var.setncattr('false_northing', srs.GetProjParm('false_northing'))
var.setncattr('latitude_of_projection_origin', srs.GetProjParm('latitude_of_origin'))
var.setncattr('semi_major_axis', float(srs.GetAttrValue('GEOGCS|SPHEROID', 1)))
var.setncattr('scale_factor_at_central_meridian', srs.GetProjParm('scale_factor'))
var.setncattr('inverse_flattening', float(srs.GetAttrValue('GEOGCS|SPHEROID', 2)))
var.setncattr('spatial_ref', srs.ExportToWkt())
var.setncattr('crs_wkt', srs.ExportToWkt())
var.setncattr('proj4text', srs.ExportToProj4())
var.setncattr('spatial_epsg', epsg)
var.setncattr('GeoTransform', ' '.join(str(x) for x in tran)) # note this has pixel size in it - set explicitly above
else:
raise Exception('Projection {0} not recognized for this program'.format(srs.GetAttrValue('PROJECTION')))
var = nc_outfile.createVariable('vx', np.dtype('int16'), ('y', 'x'), fill_value=NoDataValue,
zlib=True, complevel=2, shuffle=True, chunksizes=ChunkSize)
var.setncattr('standard_name', 'land_ice_surface_x_velocity')
if pair_type == 'radar':
var.setncattr('description', 'velocity component in x direction from radar range and azimuth measurements')
else:
var.setncattr('description', 'velocity component in x direction')
var.setncattr('units', 'meter/year')
var.setncattr('grid_mapping', mapping_var_name)
if stable_count != 0:
temp = VX.copy() - VXref.copy()
temp[np.logical_not(SSM)] = np.nan
# vx_error_mask = np.std(temp[(temp > -500)&(temp < 500)])
vx_error_mask = np.std(temp[np.logical_not(np.isnan(temp))])
else:
vx_error_mask = np.nan
if stable_count1 != 0:
temp = VX.copy() - VXref.copy()
temp[np.logical_not(SSM1)] = np.nan
# vx_error_slow = np.std(temp[(temp > -500)&(temp < 500)])
vx_error_slow = np.std(temp[np.logical_not(np.isnan(temp))])
else:
vx_error_slow = np.nan
if pair_type == 'radar':
vx_error_mod = (error_vector[0][0]*IMG_INFO_DICT['date_dt']+error_vector[1][0])/IMG_INFO_DICT['date_dt']*365
else:
vx_error_mod = error_vector[0]/IMG_INFO_DICT['date_dt']*365
if stable_shift_applied == 1:
vx_error = vx_error_mask
elif stable_shift_applied == 2:
vx_error = vx_error_slow
else:
vx_error = vx_error_mod
var.setncattr('error', int(round(vx_error*10))/10)
var.setncattr('error_description', 'best estimate of x_velocity error: vx_error is populated '
'according to the approach used for the velocity bias '
'correction as indicated in "stable_shift_flag"')
if stable_count != 0:
var.setncattr('error_stationary', int(round(vx_error_mask*10))/10)
else:
var.setncattr('error_stationary', np.nan)
var.setncattr('error_stationary_description', 'RMSE over stable surfaces, stationary or slow-flowing '
'surfaces with velocity < 15 meter/year identified from an external mask')
var.setncattr('error_modeled', int(round(vx_error_mod*10))/10)
var.setncattr('error_modeled_description', '1-sigma error calculated using a modeled error-dt relationship')
if stable_count1 != 0:
var.setncattr('error_slow', int(round(vx_error_slow*10))/10)
else:
var.setncattr('error_slow', np.nan)
var.setncattr('error_slow_description', 'RMSE over slowest 25% of retrieved velocities')
if stable_shift_applied == 2:
var.setncattr('stable_shift', int(round(vx_mean_shift1*10))/10)
elif stable_shift_applied == 1:
var.setncattr('stable_shift', int(round(vx_mean_shift*10))/10)
else:
var.setncattr('stable_shift', 0)
var.setncattr('stable_shift_flag', stable_shift_applied)
var.setncattr('stable_shift_flag_description', 'flag for applying velocity bias correction: 0 = no correction; '
'1 = correction from overlapping stable surface mask (stationary '
'or slow-flowing surfaces with velocity < 15 meter/year)(top priority); '
'2 = correction from slowest 25% of overlapping velocities '
'(second priority)')
if stable_count != 0:
var.setncattr('stable_shift_stationary', int(round(vx_mean_shift*10))/10)
else:
var.setncattr('stable_shift_stationary', np.nan)
var.setncattr('stable_count_stationary', stable_count)
if stable_count1 != 0:
var.setncattr('stable_shift_slow', int(round(vx_mean_shift1*10))/10)
else:
var.setncattr('stable_shift_slow', np.nan)
var.setncattr('stable_count_slow', stable_count1)
VX[noDataMask] = NoDataValue
var[:] = np.round(np.clip(VX, -32768, 32767)).astype(np.int16)
# var.setncattr('_FillValue', np.int16(FillValue))
var = nc_outfile.createVariable('vy', np.dtype('int16'), ('y', 'x'), fill_value=NoDataValue,
zlib=True, complevel=2, shuffle=True, chunksizes=ChunkSize)
var.setncattr('standard_name', 'land_ice_surface_y_velocity')
if pair_type == 'radar':
var.setncattr('description', 'velocity component in y direction from radar range and azimuth measurements')
else:
var.setncattr('description', 'velocity component in y direction')
var.setncattr('units', 'meter/year')
var.setncattr('grid_mapping', mapping_var_name)
if stable_count != 0:
temp = VY.copy() - VYref.copy()
temp[np.logical_not(SSM)] = np.nan
# vy_error_mask = np.std(temp[(temp > -500)&(temp < 500)])
vy_error_mask = np.std(temp[np.logical_not(np.isnan(temp))])
else:
vy_error_mask = np.nan
if stable_count1 != 0:
temp = VY.copy() - VYref.copy()
temp[np.logical_not(SSM1)] = np.nan
# vy_error_slow = np.std(temp[(temp > -500)&(temp < 500)])
vy_error_slow = np.std(temp[np.logical_not(np.isnan(temp))])
else:
vy_error_slow = np.nan
if pair_type == 'radar':
vy_error_mod = (error_vector[0][1]*IMG_INFO_DICT['date_dt']+error_vector[1][1])/IMG_INFO_DICT['date_dt']*365
else:
vy_error_mod = error_vector[1]/IMG_INFO_DICT['date_dt']*365
if stable_shift_applied == 1:
vy_error = vy_error_mask
elif stable_shift_applied == 2:
vy_error = vy_error_slow
else:
vy_error = vy_error_mod
var.setncattr('error', int(round(vy_error*10))/10)
var.setncattr('error_description', 'best estimate of y_velocity error: vy_error is populated according '
'to the approach used for the velocity bias correction as indicated '
'in "stable_shift_flag"')
if stable_count != 0:
var.setncattr('error_stationary', int(round(vy_error_mask*10))/10)
else:
var.setncattr('error_stationary', np.nan)
var.setncattr('error_stationary_description', 'RMSE over stable surfaces, stationary or slow-flowing surfaces '
'with velocity < 15 meter/year identified from an external mask')
var.setncattr('error_modeled', int(round(vy_error_mod * 10)) / 10)
var.setncattr('error_modeled_description', '1-sigma error calculated using a modeled error-dt relationship')
if stable_count1 != 0:
var.setncattr('error_slow', int(round(vy_error_slow * 10)) / 10)
else:
var.setncattr('error_slow', np.nan)
var.setncattr('error_slow_description', 'RMSE over slowest 25% of retrieved velocities')
if stable_shift_applied == 2:
var.setncattr('stable_shift', int(round(vy_mean_shift1*10))/10)
elif stable_shift_applied == 1:
var.setncattr('stable_shift', int(round(vy_mean_shift*10))/10)
else:
var.setncattr('stable_shift', 0)
var.setncattr('stable_shift_flag', stable_shift_applied)
var.setncattr('stable_shift_flag_description', 'flag for applying velocity bias correction: 0 = no correction; '
'1 = correction from overlapping stable surface mask (stationary '
'or slow-flowing surfaces with velocity < 15 meter/year)(top priority); '
'2 = correction from slowest 25% of overlapping velocities '
'(second priority)')
if stable_count != 0:
var.setncattr('stable_shift_stationary', int(round(vy_mean_shift*10))/10)
else:
var.setncattr('stable_shift_stationary', np.nan)
var.setncattr('stable_count_stationary', stable_count)
if stable_count1 != 0:
var.setncattr('stable_shift_slow', int(round(vy_mean_shift1*10))/10)
else:
var.setncattr('stable_shift_slow', np.nan)
var.setncattr('stable_count_slow', stable_count1)
VY[noDataMask] = NoDataValue
var[:] = np.round(np.clip(VY, -32768, 32767)).astype(np.int16)
# var.setncattr('missing_value', np.int16(NoDataValue))
var = nc_outfile.createVariable('v', np.dtype('int16'), ('y', 'x'), fill_value=NoDataValue,
zlib=True, complevel=2, shuffle=True, chunksizes=ChunkSize)
var.setncattr('standard_name', 'land_ice_surface_velocity')
var.setncattr('description', 'velocity magnitude')
var.setncattr('units', 'meter/year')
var.setncattr('grid_mapping', mapping_var_name)
V[noDataMask] = NoDataValue
var[:] = np.round(np.clip(V, -32768, 32767)).astype(np.int16)
# var.setncattr('missing_value',np.int16(NoDataValue))
var = nc_outfile.createVariable('v_error', np.dtype('int16'), ('y', 'x'), fill_value=NoDataValue,
zlib=True, complevel=2, shuffle=True, chunksizes=ChunkSize)
var.setncattr('standard_name', 'velocity_error')
if pair_type == 'radar':
var.setncattr('description', 'velocity magnitude error from radar range and azimuth measurements')
else:
var.setncattr('description', 'velocity magnitude error')
var.setncattr('units', 'meter/year')
var.setncattr('grid_mapping', mapping_var_name)
v_error = v_error_cal(vx_error, vy_error)
V_error = np.sqrt((vx_error * VX / V)**2 + (vy_error * VY / V)**2)
V_error[V == 0] = v_error
V_error[noDataMask] = NoDataValue
var[:] = np.round(np.clip(V_error, -32768, 32767)).astype(np.int16)
# var.setncattr('missing_value',np.int16(NoDataValue))
if pair_type == 'radar':
var = nc_outfile.createVariable('vr', np.dtype('int16'), ('y', 'x'), fill_value=NoDataValue,
zlib=True, complevel=2, shuffle=True, chunksizes=ChunkSize)
var.setncattr('standard_name', 'range_velocity')
var.setncattr('description', 'velocity in radar range direction')
var.setncattr('units', 'meter/year')
var.setncattr('grid_mapping', mapping_var_name)
if stable_count != 0:
temp = VR.copy() - VRref.copy()
temp[np.logical_not(SSM)] = np.nan
# vr_error_mask = np.std(temp[(temp > -500)&(temp < 500)])
vr_error_mask = np.std(temp[np.logical_not(np.isnan(temp))])
else:
vr_error_mask = np.nan
if stable_count1 != 0:
temp = VR.copy() - VRref.copy()
temp[np.logical_not(SSM1)] = np.nan
# vr_error_slow = np.std(temp[(temp > -500)&(temp < 500)])
vr_error_slow = np.std(temp[np.logical_not(np.isnan(temp))])
else:
vr_error_slow = np.nan
vr_error_mod = (error_vector[0][2]*IMG_INFO_DICT['date_dt']+error_vector[1][2])/IMG_INFO_DICT['date_dt']*365
if stable_shift_applied == 1:
vr_error = vr_error_mask
elif stable_shift_applied == 2:
vr_error = vr_error_slow
else:
vr_error = vr_error_mod
var.setncattr('error', int(round(vr_error*10))/10)
var.setncattr('error_description', 'best estimate of range_velocity error: vr_error is populated '
'according to the approach used for the velocity bias correction '
'as indicated in "stable_shift_flag"')
if stable_count != 0:
var.setncattr('error_stationary', int(round(vr_error_mask*10))/10)
else:
var.setncattr('error_stationary', np.nan)
var.setncattr('error_stationary_description', 'RMSE over stable surfaces, stationary or slow-flowing '
'surfaces with velocity < 15 meter/year identified from an external mask')
var.setncattr('error_modeled', int(round(vr_error_mod*10))/10)
var.setncattr('error_modeled_description', '1-sigma error calculated using a modeled error-dt relationship')
if stable_count1 != 0:
var.setncattr('error_slow', int(round(vr_error_slow*10))/10)
else:
var.setncattr('error_slow', np.nan)
var.setncattr('error_slow_description', 'RMSE over slowest 25% of retrieved velocities')
if stable_shift_applied == 2:
var.setncattr('stable_shift', int(round(vr_mean_shift1*10))/10)
elif stable_shift_applied == 1:
var.setncattr('stable_shift', int(round(vr_mean_shift*10))/10)
else:
var.setncattr('stable_shift', 0)
var.setncattr('stable_shift_flag', stable_shift_applied)
var.setncattr('stable_shift_flag_description', 'flag for applying velocity bias correction: 0 = no correction; '
'1 = correction from overlapping stable surface mask '
'(stationary or slow-flowing surfaces with velocity < 15 meter/year)'
'(top priority); 2 = correction from slowest 25% of overlapping '
'velocities (second priority)')
if stable_count != 0:
var.setncattr('stable_shift_stationary', int(round(vr_mean_shift*10))/10)
else:
var.setncattr('stable_shift_stationary', np.nan)
var.setncattr('stable_count_stationary', stable_count)
if stable_count1 != 0:
var.setncattr('stable_shift_slow', int(round(vr_mean_shift1*10))/10)
else:
var.setncattr('stable_shift_slow', np.nan)
var.setncattr('stable_count_slow', stable_count1)
VR[noDataMask] = NoDataValue
var[:] = np.round(np.clip(VR, -32768, 32767)).astype(np.int16)
# var.setncattr('missing_value', np.int16(NoDataValue))
var = nc_outfile.createVariable('va', np.dtype('int16'), ('y', 'x'), fill_value=NoDataValue,
zlib=True, complevel=2, shuffle=True, chunksizes=ChunkSize)
var.setncattr('standard_name', 'azimuth_velocity')
var.setncattr('description', 'velocity in radar azimuth direction')
var.setncattr('units', 'meter/year')
var.setncattr('grid_mapping', mapping_var_name)
if stable_count != 0:
temp = VA.copy() - VAref.copy()
temp[np.logical_not(SSM)] = np.nan
# va_error_mask = np.std(temp[(temp > -500)&(temp < 500)])
va_error_mask = np.std(temp[np.logical_not(np.isnan(temp))])
else:
va_error_mask = np.nan
if stable_count1 != 0:
temp = VA.copy() - VAref.copy()
temp[np.logical_not(SSM1)] = np.nan
# va_error_slow = np.std(temp[(temp > -500)&(temp < 500)])
va_error_slow = np.std(temp[np.logical_not(np.isnan(temp))])
else:
va_error_slow = np.nan
va_error_mod = (error_vector[0][3]*IMG_INFO_DICT['date_dt']+error_vector[1][3])/IMG_INFO_DICT['date_dt']*365
if stable_shift_applied == 1:
va_error = va_error_mask
elif stable_shift_applied == 2:
va_error = va_error_slow
else:
va_error = va_error_mod
var.setncattr('error', int(round(va_error*10))/10)
var.setncattr('error_description', 'best estimate of azimuth_velocity error: va_error is populated '
'according to the approach used for the velocity bias correction '
'as indicated in "stable_shift_flag"')
if stable_count != 0:
var.setncattr('error_stationary', int(round(va_error_mask*10))/10)
else:
var.setncattr('error_stationary', np.nan)
var.setncattr('error_stationary_description', 'RMSE over stable surfaces, stationary or slow-flowing surfaces with velocity < 15 meter/year identified from an external mask')
var.setncattr('error_modeled', int(round(va_error_mod*10))/10)
var.setncattr('error_modeled_description', '1-sigma error calculated using a modeled error-dt relationship')
if stable_count1 != 0:
var.setncattr('error_slow', int(round(va_error_slow*10))/10)
else:
var.setncattr('error_slow', np.nan)
var.setncattr('error_slow_description', 'RMSE over slowest 25% of retrieved velocities')
if stable_shift_applied == 2:
var.setncattr('stable_shift', int(round(va_mean_shift1*10))/10)
elif stable_shift_applied == 1:
var.setncattr('stable_shift', int(round(va_mean_shift*10))/10)
else:
var.setncattr('stable_shift', 0)
var.setncattr('stable_shift_flag', stable_shift_applied)
var.setncattr('stable_shift_flag_description', 'flag for applying velocity bias correction: 0 = no correction; '
'1 = correction from overlapping stable surface mask '
'(stationary or slow-flowing surfaces with velocity < 15 meter/year)'
'(top priority); 2 = correction from slowest 25% of overlapping '
'velocities (second priority)')
if stable_count != 0:
var.setncattr('stable_shift_stationary', int(round(va_mean_shift*10))/10)
else:
var.setncattr('stable_shift_stationary', np.nan)
var.setncattr('stable_count_stationary', stable_count)
if stable_count1 != 0:
var.setncattr('stable_shift_slow', int(round(va_mean_shift1*10))/10)
else:
var.setncattr('stable_shift_slow', np.nan)
var.setncattr('stable_count_slow', stable_count1)
VA[noDataMask] = NoDataValue
var[:] = np.round(np.clip(VA, -32768, 32767)).astype(np.int16)
# var.setncattr('missing_value', np.int16(NoDataValue))
# fuse the (slope parallel & reference) flow-based range-projected result with the raw observed range/azimuth-based result
if stable_count_p != 0:
temp = VXP.copy() - VXref.copy()
temp[np.logical_not(SSM)] = np.nan
# vxp_error_mask = np.std(temp[(temp > -500)&(temp < 500)])
vxp_error_mask = np.std(temp[np.logical_not(np.isnan(temp))])
temp = VYP.copy() - VYref.copy()
temp[np.logical_not(SSM)] = np.nan
# vyp_error_mask = np.std(temp[(temp > -500)&(temp < 500)])
vyp_error_mask = np.std(temp[np.logical_not(np.isnan(temp))])
if stable_count1_p != 0:
temp = VXP.copy() - VXref.copy()
temp[np.logical_not(SSM1)] = np.nan
# vxp_error_slow = np.std(temp[(temp > -500)&(temp < 500)])
vxp_error_slow = np.std(temp[np.logical_not(np.isnan(temp))])
temp = VYP.copy() - VYref.copy()
temp[np.logical_not(SSM1)] = np.nan
# vyp_error_slow = np.std(temp[(temp > -500)&(temp < 500)])
vyp_error_slow = np.std(temp[np.logical_not(np.isnan(temp))])
vxp_error_mod = (error_vector[0][4]*IMG_INFO_DICT['date_dt']+error_vector[1][4])/IMG_INFO_DICT['date_dt']*365
vyp_error_mod = (error_vector[0][5]*IMG_INFO_DICT['date_dt']+error_vector[1][5])/IMG_INFO_DICT['date_dt']*365
if stable_shift_applied_p == 1:
vxp_error = vxp_error_mask
vyp_error = vyp_error_mask
elif stable_shift_applied_p == 2:
vxp_error = vxp_error_slow
vyp_error = vyp_error_slow
else:
vxp_error = vxp_error_mod
vyp_error = vyp_error_mod
VP_error = np.sqrt((vxp_error * VXP / VP)**2 + (vyp_error * VYP / VP)**2)
VXPP[V_error > VP_error] = VXP[V_error > VP_error]
VYPP[V_error > VP_error] = VYP[V_error > VP_error]
VXP = VXPP.astype(np.float32)
VYP = VYPP.astype(np.float32)
VP = np.sqrt(VXP**2+VYP**2)
stable_count_p = np.sum(SSM & np.logical_not(np.isnan(VXP)))
stable_count1_p = np.sum(SSM1 & np.logical_not(np.isnan(VXP)))
vxp_mean_shift = 0.0
vyp_mean_shift = 0.0
vxp_mean_shift1 = 0.0
vyp_mean_shift1 = 0.0
if stable_count_p != 0:
temp = VXP.copy() - VX.copy()
temp[np.logical_not(SSM)] = np.nan
# bias_mean_shift = np.median(temp[(temp > -500)&(temp < 500)])
bias_mean_shift = np.median(temp[np.logical_not(np.isnan(temp))])
vxp_mean_shift = vx_mean_shift + bias_mean_shift / 1
temp = VYP.copy() - VY.copy()
temp[np.logical_not(SSM)] = np.nan
# bias_mean_shift = np.median(temp[(temp > -500)&(temp < 500)])
bias_mean_shift = np.median(temp[np.logical_not(np.isnan(temp))])
vyp_mean_shift = vy_mean_shift + bias_mean_shift / 1
if stable_count1_p != 0:
temp = VXP.copy() - VX.copy()
temp[np.logical_not(SSM1)] = np.nan
# bias_mean_shift1 = np.median(temp[(temp > -500)&(temp < 500)])
bias_mean_shift1 = np.median(temp[np.logical_not(np.isnan(temp))])
vxp_mean_shift1 = vx_mean_shift1 + bias_mean_shift1 / 1
temp = VYP.copy() - VY.copy()
temp[np.logical_not(SSM1)] = np.nan
# bias_mean_shift1 = np.median(temp[(temp > -500)&(temp < 500)])
bias_mean_shift1 = np.median(temp[np.logical_not(np.isnan(temp))])
vyp_mean_shift1 = vy_mean_shift1 + bias_mean_shift1 / 1
if stable_count_p == 0:
if stable_count1_p == 0:
stable_shift_applied_p = 0
else:
stable_shift_applied_p = 2
else:
stable_shift_applied_p = 1
# var = nc_outfile.createVariable('vxp',np.dtype('int16'),('y', 'x'), fill_value=NoDataValue,
# zlib=True, complevel=2, shuffle=True, chunksizes=ChunkSize)
# var.setncattr('standard_name', 'projected_x_velocity')
# var.setncattr('description', 'x-direction velocity determined by projecting radar range measurements '
# 'onto an a priori flow vector. Where projected errors are larger than those '
# 'determined from range and azimuth measurements, unprojected vx estimates are used')
# var.setncattr('units', 'meter/year')
# var.setncattr('grid_mapping', mapping_var_name)
#
# if stable_count_p != 0:
# temp = VXP.copy() - VXref.copy()
# temp[np.logical_not(SSM)] = np.nan
# # vxp_error_mask = np.std(temp[(temp > -500)&(temp < 500)])
# vxp_error_mask = np.std(temp[np.logical_not(np.isnan(temp))])
# else:
# vxp_error_mask = np.nan
# if stable_count1_p != 0:
# temp = VXP.copy() - VXref.copy()
# temp[np.logical_not(SSM1)] = np.nan
# # vxp_error_slow = np.std(temp[(temp > -500)&(temp < 500)])
# vxp_error_slow = np.std(temp[np.logical_not(np.isnan(temp))])
# else:
# vxp_error_slow = np.nan
# if stable_shift_applied_p == 1:
# vxp_error = vxp_error_mask
# elif stable_shift_applied_p == 2:
# vxp_error = vxp_error_slow
# else:
# vxp_error = vxp_error_mod
# var.setncattr('error', int(round(vxp_error*10))/10)
# var.setncattr('error_description', 'best estimate of projected_x_velocity error: vxp_error is populated '
# 'according to the approach used for the velocity bias correction as '
# 'indicated in "stable_shift_flag"')
#
# if stable_count_p != 0:
# var.setncattr('error_stationary', int(round(vxp_error_mask*10))/10)
# else:
# var.setncattr('error_stationary', np.nan)
# var.setncattr('error_stationary_description', 'RMSE over stable surfaces, stationary or slow-flowing surfaces '
# 'with velocity < 15 meter/year identified from an external mask')
#
# var.setncattr('error_modeled', int(round(vxp_error_mod * 10)) / 10)
# var.setncattr('error_modeled_description', '1-sigma error calculated using a modeled error-dt relationship')
#
# if stable_count1_p != 0:
# var.setncattr('error_slow', int(round(vxp_error_slow * 10)) / 10)
# else:
# var.setncattr('error_slow', np.nan)
# var.setncattr('error_slow_description', 'RMSE over slowest 25% of retrieved velocities')
#
# if stable_shift_applied_p == 2:
# var.setncattr('stable_shift', int(round(vxp_mean_shift1*10))/10)
# elif stable_shift_applied_p == 1:
# var.setncattr('stable_shift', int(round(vxp_mean_shift*10))/10)
# else:
# var.setncattr('stable_shift', 0)
# var.setncattr('stable_shift_flag', stable_shift_applied_p)
# var.setncattr('stable_shift_flag_description', 'flag for applying velocity bias correction: 0 = no correction; '
# '1 = correction from overlapping stable surface mask '
# '(stationary or slow-flowing surfaces with velocity < 15 meter/year)'
# '(top priority); 2 = correction from slowest 25% of overlapping '
# 'velocities (second priority)')
#
# if stable_count_p != 0:
# var.setncattr('stable_shift_stationary',int(round(vxp_mean_shift*10))/10)
# else:
# var.setncattr('stable_shift_stationary',np.nan)
# var.setncattr('stable_count_stationary',stable_count_p)
#
# if stable_count1_p != 0:
# var.setncattr('stable_shift_slow',int(round(vxp_mean_shift1*10))/10)
# else:
# var.setncattr('stable_shift_slow',np.nan)
# var.setncattr('stable_count_slow',stable_count1_p)
#
# VXP[noDataMask] = NoDataValue
# var[:] = np.round(np.clip(VXP, -32768, 32767)).astype(np.int16)
# # var.setncattr('missing_value', np.int16(NoDataValue))
#
#
# var = nc_outfile.createVariable('vyp', np.dtype('int16'), ('y', 'x'), fill_value=NoDataValue,
# zlib=True, complevel=2, shuffle=True, chunksizes=ChunkSize)
# var.setncattr('standard_name', 'projected_y_velocity')
# var.setncattr('description', 'y-direction velocity determined by projecting radar range measurements '
# 'onto an a priori flow vector. Where projected errors are larger than those '
# 'determined from range and azimuth measurements, unprojected vy estimates are used')
# var.setncattr('units', 'meter/year')
# var.setncattr('grid_mapping', mapping_var_name)
#
# if stable_count_p != 0:
# temp = VYP.copy() - VYref.copy()
# temp[np.logical_not(SSM)] = np.nan
# # vyp_error_mask = np.std(temp[(temp > -500)&(temp < 500)])
# vyp_error_mask = np.std(temp[np.logical_not(np.isnan(temp))])
# else:
# vyp_error_mask = np.nan
# if stable_count1_p != 0: