forked from tsutterley/read-ICESat-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MPI_ICESat2_ATL03_histogram.py
3097 lines (3006 loc) · 202 KB
/
MPI_ICESat2_ATL03_histogram.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
u"""
MPI_ICESat2_ATL03_histogram.py (06/2020)
Read ICESat-2 ATL03 and ATL09 data files to calculate average segment surfaces
ATL03 datasets: Global Geolocated Photons
ATL09 datasets: Atmospheric Characteristics
Alternative algorithm that uses gaussian/generalized gaussian decomposition to
extract possibly multiple height surfaces from a histogram of photon events
CALLING SEQUENCE:
mpiexec -np 6 python MPI_ICESat2_ATL03_histogram.py ATL03_file ATL09_file
COMMAND LINE OPTIONS:
-O X, --output=X: Name and path of output file
-V, --verbose: Verbose output to track progress
-M X, --mode=X: Permission mode of files created
REQUIRES MPI PROGRAM
MPI: standardized and portable message-passing system
https://www.open-mpi.org/
http://mpitutorial.com/
PYTHON DEPENDENCIES:
numpy: Scientific Computing Tools For Python
https://numpy.org
https://numpy.org/doc/stable/user/numpy-for-matlab-users.html
scipy: Scientific Tools for Python
https://docs.scipy.org/doc/
mpi4py: MPI for Python
http://pythonhosted.org/mpi4py/
http://mpi4py.readthedocs.org/en/stable/
h5py: Python interface for Hierarchal Data Format 5 (HDF5)
https://h5py.org
http://docs.h5py.org/en/stable/mpi.html
scikit-learn: Machine Learning in Python
http://scikit-learn.org/stable/index.html
https://github.com/scikit-learn/scikit-learn
PROGRAM DEPENDENCIES:
convert_julian.py: returns the calendar date and time given a Julian date
count_leap_seconds: determines the number of leap seconds for a GPS time
REFERENCES:
A Chauve, C Mallet, F Bretar, S Durrieu, M P Deseilligny and W Puech.
"Processing full-waveform lidar data: Modelling raw signals"
International Archives of Photogrammetry
Remote Sensing and Spatial Information Sciences, 36, 102--107 (2007)
C Mallet and F Bretar.
"Full-waveform topographic lidar: State-of-the-art"
ISPRS Journal of Photogrammetry and Remote Sensing, 64, 1--16 (2009)
J B Blair, D L Rabine, and M A Hofton.
"The Laser Vegetation Imaging Sensor: A Medium-Altitude, Digitisation-Only,
Airborne Laser Altimeter for Mapping Vegetation and Topography"
ISPRS Journal of Photogrammetry and Remote Sensing, 54, 115--122 (1999)
M A Hofton, J B Blair, J B Minster, J R Ridgway, N P Williams, J L Bufton,
and D L Rabine. "An Airborne Scanning Laser Altimetry Survey of Long Valley,
California" International Journal of Remote Sensing, 21(12), 2413--2437 (2000)
M A Hofton, J B Minster, and J B Blair.
"Decomposition of Laser Altimeter Waveforms"
IEEE Transactions on Geoscience and Remote Sensing, 38(4), 1899--1996 (2000)
M A Hofton, J B Blair, S B Luthcke, and D L Rabine.
"Assessing the Performance of 20-25 m Footprint Waveform Lidar Data
Collected in ICESat Data Corridors in Greenland"
Geophysical Research Letters, 35, L24501 (2008), doi:10.1029/2008GL035774
J R Ridgway, J B Minster, N Williams, J L Bufton and W B Krabill.
"Airborne laser altimeter survey of Long Valley, California"
Geophysical Journal International (1997) 131, 267-280
UPDATE HISTORY:
Updated 06/2020: reduce the maximum number of peaks to fit and reduce threshold
verify that complementary beam pair is in list of beams
set masks of output arrays after reading from HDF5
save histogram fit amplitudes to output HDF5 file
Updated 05/2020: add mean median difference of histogram fit residuals
Updated 10/2019: changing Y/N flags to True/False
Updated 09/2019: adding segment quality summary variable
Updated 04/2019: updated backup algorithm for when the histogram fit fails
estimate both mean and median first photon bias corrections
estimate both mean and median transmit pulse shape corrections
Updated 03/2019: extract a set of ATL09 parameters for each ATL03 segment_ID
Written 05/2017
"""
from __future__ import print_function, division
import sys
import os
import re
import h5py
import getopt
import datetime
import operator
import itertools
import numpy as np
import scipy.stats
import scipy.signal
import scipy.optimize
import scipy.interpolate
import sklearn.neighbors
from mpi4py import MPI
from icesat2_toolkit.convert_julian import convert_julian
from icesat2_toolkit.count_leap_seconds import count_leap_seconds
#-- PURPOSE: keep track of MPI threads
def info(rank, size):
print('Rank {0:d} of {1:d}'.format(rank+1,size))
print('module name: {0}'.format(__name__))
if hasattr(os, 'getppid'):
print('parent process: {0:d}'.format(os.getppid()))
print('process id: {0:d}'.format(os.getpid()))
#-- PURPOSE: try fitting a function to the signal photons with progressively
#-- less confidence if no valid histogram fit is found
def try_histogram_fit(x, y, z, confidence_mask, dist_along, dt,
FIT_TYPE='gaussian', ITERATE=25, BACKGROUND=0, CONFIDENCE=[2,1,0]):
#-- try with progressively less confidence
for i,conf in enumerate(CONFIDENCE):
ind, = np.nonzero(confidence_mask >= conf)
centroid = dict(x=dist_along, y=np.mean(y[ind]))
try:
surf = reduce_histogram_fit(x[ind], y[ind], z[ind], ind,
dt, FIT_TYPE=FIT_TYPE, ITERATE=ITERATE, PEAKS=2,
BACKGROUND=BACKGROUND)
except (ValueError, RuntimeError, SyntaxError):
pass
else:
return (i+1,surf,centroid)
#-- if still no values found: return infinite values
#-- will need to attempt a backup algorithm
surf = dict(error=np.full(1,np.inf))
centroid = None
return (None,surf,centroid)
#-- PURPOSE: iteratively use decomposition fitting to the elevation data to
#-- reduce to within a valid window
def reduce_histogram_fit(x, y, z, ind, dt, FIT_TYPE='gaussian',
ITERATE=25, PEAKS=2, BACKGROUND=0):
#-- speed of light
c = 299792458.0
#-- use same delta time as calculating first photon bias
#-- so that the residuals will be the same
dz = dt*c
#-- number of background photons in each bin
N_BG = dz*BACKGROUND
#-- create a histogram of the heights
zmin,zmax = (z.min(),z.max())
z_full = np.arange(zmin,zmax+dz,dz)
nz = len(z_full)
#-- maximum allowable window size
H_win_max = 20.0
#-- minimum allowable window size
H_win_min = 3.0
#-- set initial window to the full z range
window = zmax - zmin
window_p1 = np.copy(window)
#-- number of data points
n_max = len(z)
#-- number of terms in fit
if (FIT_TYPE == 'gaussian'):#-- gaussian fit
n_terms = 3
elif (FIT_TYPE == 'general'):#-- generalized gaussian fit
n_terms = 4
#-- run only if number of histogram points is above number of terms
FLAG1 = ((nz - n_terms) > 10)
#-- using kernel density functions from scikit-learn neighbors
#-- gaussian kernels will reflect more accurate distributions of the data
#-- with less sensitivity to sampling width than histograms (tophat kernels)
kde = sklearn.neighbors.KernelDensity(bandwidth=dz,kernel='gaussian')
kde.fit(z[:,None])
#-- kde score_samples outputs are normalized log density functions
hist = np.exp(kde.score_samples(z_full[:,None]) + np.log(n_max*dz))
#-- smooth histogram before determining differentials
gw = scipy.signal.gaussian(nz,4)
hist_smooth = scipy.signal.convolve(hist, gw/gw.sum(), mode='same')
#-- First differentials to find zero crossings
#-- histogram 1st differential
dhist = np.zeros((nz))
#-- forward differentiation for starting point
dhist[0] = hist_smooth[1] - hist_smooth[0]
#-- backward differentiation for end point
dhist[-1] = hist_smooth[-1] - hist_smooth[-2]
#-- centered differentiation for all others
dhist[1:-1] = (hist_smooth[2:] - hist_smooth[0:-2])/2.0
#-- find positive peaks above amplitude threshold (percent of max)
#-- by calculating the histogram differentials
#-- signal amplitude threshold greater than 10% of max or 3xbackground rate
AmpThreshold = 0.10
HistThreshold = np.max([3.0*N_BG, AmpThreshold*np.max(hist_smooth)])
n_peaks = np.count_nonzero((np.sign(dhist[0:-1]) >= 0) & (np.sign(dhist[1:]) < 0) &
((hist_smooth[0:-1] > HistThreshold) | (hist_smooth[1:] > HistThreshold)))
n_peaks = np.min([n_peaks,PEAKS])
peak_index, = np.nonzero((np.sign(dhist[0:-1]) >= 0) & (np.sign(dhist[1:]) < 0) &
((hist_smooth[0:-1] > HistThreshold) | (hist_smooth[1:] > HistThreshold)))
#-- initial indices for reducing to window
filt = np.arange(n_max)
filt_p1 = np.copy(filt)
filt_p2 = np.copy(filt_p1)
if FLAG1 and (n_peaks > 0):
#-- save initial indices for fitting all photons for confidence level
indices = ind.copy()
#-- sort peak index by amplitude of peaks (descending from max to min)
#-- and truncate to a finite number of peaks
sorted_peaks = np.argsort(hist[peak_index])[::-1]
peak_index = peak_index[sorted_peaks][:n_peaks]
#-- amplitude of the maximum peak
max_amp = hist[peak_index][0]
#-- estimated mean and standard deviation of peaks
hist_mean = np.sum(hist)/nz
hist_stdev = np.sqrt(np.sum((hist-hist_mean)**2)/nz)
#-- cumulative probability distribution function of initial histogram
hist_cpdf = np.cumsum(hist/np.sum(hist))
#-- IQR: first and third quartiles (25th and 75th percentiles)
#-- RDE: 16th and 84th percentiles
Q1,Q3,P16,P84 = np.interp([0.25,0.75,0.16,0.84],hist_cpdf,z_full)
#-- create priors list
priors = []
lower_bound = []
upper_bound = []
for i,p in enumerate(peak_index):
if (FIT_TYPE == 'gaussian'):
#-- Fit Gaussian functions to photon event histogram
#-- a*: amplitude of waveform
#-- r*: range from differential index
#-- w*: width as 0.75*IQR
priors.append([hist[p],z_full[p],0.75*(Q3-Q1)])
#-- bounds of each parameter
#-- amplitude: 0 to histogram max+3std
#-- range: zmin to zmax
#-- width: sz to half width of z
lower_bound.extend([0,zmin,dz])
upper_bound.extend([max_amp+3*hist_stdev,zmax,(zmax-zmin)/2.0])
elif (FIT_TYPE == 'general'):
#-- Fit Generalized Gaussian functions to photon event histogram
#-- a*: amplitude of waveform
#-- r*: range from differential index
#-- w*: width as 0.75*IQR
#-- p*: shape parameter = gaussian sqrt(2)
priors.append([hist[p],z_full[p],0.75*(Q3-Q1),np.sqrt(2)])
#-- bounds of each parameter
#-- amplitude: 0 to histogram max+3std
#-- range: zmin to zmax
#-- width: sz to half width of z
#-- shape: positive
lower_bound.extend([0,zmin,dz,0])
upper_bound.extend([max_amp+3*hist_stdev,zmax,(zmax-zmin)/2.0,np.inf])
#-- run optimized curve fit with Levenberg-Marquardt algorithm
fit = fit_histogram(z_full,hist,priors,lower_bound,upper_bound,
FIT_TYPE=FIT_TYPE)
#-- number of iterations performed
n_iter = 1
#-- height fits and height fit errors
height = fit['height'].copy()
amplitude = fit['amplitude'].copy()
height_errors = fit['error'].copy()
#-- minimum and maximum heights
min_peak = np.min(fit['height'])
max_peak = np.max(fit['height'])
#-- save MSE and DOF for error analysis
MSE = np.copy(fit['MSE'])
DOF = np.copy(fit['DOF'])
#-- Root mean square error
RMSE = np.sqrt(fit['MSE'])
#-- Normalized root mean square error
NRMSE = RMSE/(zmax-zmin)
#-- histogram fit
model = np.copy(fit['model'])
#-- histogram fit residuals
resid = np.copy(fit['residuals'])
#-- cumulative probability distribution function of initial histogram
cpdf = np.cumsum(fit['residuals']/np.sum(fit['residuals']))
#-- interpolate residuals to percentiles of interest for statistics
Q1,Q3,MEDIAN,P16,P84 = np.interp([0.25,0.75,0.5,0.16,0.84],cpdf,z_full)
#-- IQR: first and third quartiles (25th and 75th percentiles)
#-- RDE: 16th and 84th percentiles
IQR = 0.75*(Q3-Q1)
RDE = 0.50*(P84-P16)
#-- checking if any residuals are outside of the window
window = np.max([H_win_min,6.0*RDE,0.5*window_p1])
filt, = np.nonzero((z > (min_peak-window/2.0)) & (z < (max_peak+window/2.0)))
#-- run only if number of points is above number of terms
n_rem = np.count_nonzero((z > (min_peak-window/2.0)) & (z < (max_peak+window/2.0)))
nz = (np.max(z[filt])-np.min(z[filt]))//dz + 1
FLAG1 = ((nz - n_terms) > 10) & ((n_rem - n_terms) > 10)
#-- maximum number of iterations to prevent infinite loops
FLAG2 = (n_iter <= ITERATE)
#-- compare indices over two iterations to prevent false stoppages
FLAG3 = (set(filt) != set(filt_p1)) | (set(filt_p1) != set(filt_p2))
#-- iterate until there are no additional removed photons
while FLAG1 & FLAG2 & FLAG3:
#-- fit selected photons for window
x_filt,y_filt,z_filt,indices = (x[filt],y[filt],z[filt],ind[filt])
zmin,zmax = (z_filt.min(),z_filt.max())
z_full = np.arange(zmin,zmax+dz,dz)
nz = len(z_full)
#-- using kernel density functions from scikit-learn neighbors
#-- gaussian kernels will reflect more accurate distributions of the data
#-- with less sensitivity to sampling width than histograms (tophat kernels)
kde = sklearn.neighbors.KernelDensity(bandwidth=dz,kernel='gaussian')
kde.fit(z_filt[:,None])
#-- kde score_samples outputs are normalized log density functions
hist = np.exp(kde.score_samples(z_full[:,None]) + np.log(nz*dz))
#-- smooth histogram before determining differentials
gw = scipy.signal.gaussian(nz,4)
hist_smooth = scipy.signal.convolve(hist, gw/gw.sum(), mode='same')
#-- First differentials to find zero crossings
#-- histogram 1st differential
dhist = np.zeros((nz))
#-- forward differentiation for starting point
dhist[0] = hist_smooth[1] - hist_smooth[0]
#-- backward differentiation for end point
dhist[-1] = hist_smooth[-1] - hist_smooth[-2]
#-- centered differentiation for all others
dhist[1:-1] = (hist_smooth[2:] - hist_smooth[0:-2])/2.0
#-- find positive peaks above amplitude threshold (percent of max)
#-- by calculating the histogram differentials
#-- signal amplitude threshold greater than 10% of max or 3xbackground rate
HistThreshold = np.max([3.0*N_BG, AmpThreshold*np.max(hist_smooth)])
n_peaks = np.count_nonzero((np.sign(dhist[0:-1]) >= 0) & (np.sign(dhist[1:]) < 0) &
((hist_smooth[0:-1] > HistThreshold) | (hist_smooth[1:] > HistThreshold)))
n_peaks = np.min([n_peaks,PEAKS])
peak_index, = np.nonzero((np.sign(dhist[0:-1]) >= 0) & (np.sign(dhist[1:]) < 0) &
((hist_smooth[0:-1] > HistThreshold) | (hist_smooth[1:] > HistThreshold)))
#-- sort peak index by amplitude of peaks (descending from max to min)
#-- and truncate to a finite number of peaks
sorted_peaks = np.argsort(hist[peak_index])[::-1]
peak_index = peak_index[sorted_peaks][:n_peaks]
#-- amplitude of the maximum peak
max_amp = hist[peak_index][0]
#-- estimated mean and standard deviation of peaks
hist_mean = np.sum(hist)/nz
hist_stdev = np.sqrt(np.sum((hist-hist_mean)**2)/nz)
#-- cumulative probability distribution function of initial histogram
hist_cpdf = np.cumsum(hist/np.sum(hist))
#-- IQR: first and third quartiles (25th and 75th percentiles)
#-- RDE: 16th and 84th percentiles
Q1,Q3,P16,P84 = np.interp([0.25,0.75,0.16,0.84],hist_cpdf,z_full)
#-- create priors list
priors = []
lower_bound = []
upper_bound = []
for i,p in enumerate(peak_index):
if (FIT_TYPE == 'gaussian'):
#-- Fit Gaussian functions to photon event histogram
#-- a*: amplitude of waveform
#-- r*: range from differential index
#-- w*: width as 0.75*IQR
priors.append([hist[p],z_full[p],0.75*(Q3-Q1)])
#-- bounds of each parameter
#-- amplitude: 0 to histogram max+3std
#-- range: zmin to zmax
#-- width: sz to half width of z
lower_bound.extend([0,zmin,dz])
upper_bound.extend([max_amp+3*hist_stdev,zmax,(zmax-zmin)/2.0])
elif (FIT_TYPE == 'general'):
#-- Fit Generalized Gaussian functions to photon event histogram
#-- a*: amplitude of waveform
#-- r*: range from differential index
#-- w*: width as 0.75*IQR
#-- p*: shape parameter = gaussian sqrt(2)
priors.append([hist[p],z_full[p],0.75*(Q3-Q1),np.sqrt(2)])
#-- bounds of each parameter
#-- amplitude: 0 to histogram max+3std
#-- range: zmin to zmax
#-- width: sz to half width of z
#-- shape: positive
lower_bound.extend([0,zmin,dz,0])
upper_bound.extend([max_amp+3*hist_stdev,zmax,(zmax-zmin)/2.0,np.inf])
#-- run optimized curve fit with Levenberg-Marquardt algorithm
fit = fit_histogram(z_full,hist,priors,lower_bound,upper_bound,
FIT_TYPE=FIT_TYPE)
#-- add to number of iterations performed
n_iter += 1
#-- height fits and height fit errors
height = fit['height'].copy()
amplitude = fit['amplitude'].copy()
height_errors = fit['error'].copy()
#-- minimum and maximum heights
min_peak = np.min(fit['height'])
max_peak = np.max(fit['height'])
#-- save MSE and DOF for error analysis
MSE = np.copy(fit['MSE'])
DOF = np.copy(fit['DOF'])
#-- Root mean square error
RMSE = np.sqrt(fit['MSE'])
#-- Normalized root mean square error
NRMSE = RMSE/(zmax-zmin)
#-- histogram fit
model = np.copy(fit['model'])
#-- histogram fit residuals
resid = np.copy(fit['residuals'])
#-- cumulative probability distribution function of initial histogram
cpdf = np.cumsum(resid/np.sum(resid))
#-- interpolate residuals to percentiles of interest for statistics
Q1,Q3,MEDIAN,P16,P84 = np.interp([0.25,0.75,0.5,0.16,0.84],cpdf,z_full)
#-- IQR: first and third quartiles (25th and 75th percentiles)
#-- RDE: 16th and 84th percentiles
IQR = 0.75*(Q3-Q1)
RDE = 0.50*(P84-P16)
#-- checking if any residuals are outside of the window
window = np.max([H_win_min,6.0*RDE,0.5*window_p1])
#-- filter out using median statistics and refit
filt_p2 = np.copy(filt_p1)
filt_p1 = np.copy(filt)
filt, = np.nonzero((z > (min_peak-window/2.0)) & (z < (max_peak+window/2.0)))
#-- save iteration of window
window_p1 = np.copy(window)
#-- run only if number of points is above number of terms
n_rem = np.count_nonzero((z > (min_peak-window/2.0)) & (z < (max_peak+window/2.0)))
nz = (np.max(z[filt])-np.min(z[filt]))//dz + 1
FLAG1 = ((nz - n_terms) > 10) & ((n_rem - n_terms) > 10)
#-- maximum number of iterations to prevent infinite loops
FLAG2 = (n_iter <= ITERATE)
#-- compare indices over two iterations to prevent false stoppages
FLAG3 = (set(filt) != set(filt_p1)) | (set(filt_p1) != set(filt_p2))
#-- return reduced model fit
FLAG3 = (set(filt) == set(filt_p1))
if FLAG1 & FLAG3 & (window <= H_win_max) & (n_peaks > 0):
#-- calculate time with respect to height of maximum amplitude
iamp = np.argmax(amplitude)
t_full = -2*(z_full-height[iamp])/c
#-- return values
return {'height':height, 'error':height_errors, 'amplitude':amplitude,
'MSE':MSE, 'NRMSE':NRMSE, 'residuals':resid, 'time': t_full,
'model':model, 'DOF':DOF, 'count':n_max, 'indices':indices,
'iterations':n_iter, 'window':window, 'RDE':RDE, 'peaks':n_peaks}
else:
raise ValueError('No valid fit found')
#-- PURPOSE: optimially fit a function to the photon event histogram
#-- with Levenberg-Marquardt algorithm
def fit_histogram(z, hist, priors, lower_bound, upper_bound, FIT_TYPE=None):
#-- create lists for the initial parameters
#-- parameters, and functions for each maximum
plist = []
flist = []
n_peaks = len(priors)
#-- function formatting string and parameter list for each fit type
if (FIT_TYPE == 'gaussian'):
#-- summation of gaussian functions with:
#-- pulse amplitudes a*
#-- pulse ranges r* (mean)
#-- pulse widths w* (standard deviation)
#-- Gaussian function formatting string and parameters
function = 'a{0:d}*np.exp(-(x-r{0:d})**2.0/(2*w{0:d}**2))'
parameters = 'a{0:d}, r{0:d}, w{0:d}'
elif (FIT_TYPE == 'general'):
#-- summation of generalized gaussian functions with:
#-- pulse amplitudes a*
#-- pulse ranges r* (mean)
#-- pulse widths w* (standard deviation)
#-- shape parameter p* (gaussian=sqrt(2))
#-- Generalized Gaussian function formatting string and parameters
function = 'a{0:d}*np.exp(-np.abs(x-r{0:d})**(p{0:d}**2.0)/(2*w{0:d}**2))'
parameters = 'a{0:d}, r{0:d}, w{0:d}, p{0:d}'
#-- fit decomposition functions to photon event histograms
for n,p in enumerate(priors):
#-- parameter list for peak n
plist.append(parameters.format(n))
#-- function definition list for peak n
flist.append(function.format(n))
#-- initial parameters for iteration n
p0 = np.concatenate((priors),axis=0)
#-- variables for iteration n
lambda_parameters = ', '.join([p for p in plist])
#-- full function for iteration n
lambda_function = ' + '.join([f for f in flist])
#-- tuple for parameter bounds (lower and upper)
bounds = (lower_bound, upper_bound)
#-- create lambda function for iteration n
#-- lambda functions are inline definitions
#-- with the parameters, variables and function definition
fsum = eval('lambda x, {0}: {1}'.format(lambda_parameters, lambda_function))
#-- optimized curve fit with Levenberg-Marquardt algorithm
#-- with the initial guess parameters p0 and parameter bounds
popt, pcov = scipy.optimize.curve_fit(fsum,z,hist,p0=p0,bounds=bounds)
#-- modelled histogram fit
model = fsum(z, *popt)
#-- 1 standard deviation errors in parameters
perr = np.sqrt(np.diag(pcov))
#-- number of points for fit and number of terms in fit
n_max = len(hist)
n_terms = len(p0)
#-- extract function outputs
if (FIT_TYPE == 'gaussian'):
#-- Gaussian function outputs
n = np.arange(n_peaks)*3
peak_amplitude = popt[n]
peak_height = popt[n+1]
peak_height_error = perr[n+1]
peak_stdev = popt[n+2]
elif (FIT_TYPE == 'general'):
#-- Generalized Gaussian function outputs
n = np.arange(n_peaks)*4
peak_amplitude = popt[n]
peak_height = popt[n+1]
peak_height_error = perr[n+1]
peak_stdev = popt[n+2]
#-- residual of fit
res = hist - model
#-- nu = Degrees of Freedom = number of measurements-number of parameters
nu = n_max - n_terms
#-- Mean square error
#-- MSE = (1/nu)*sum((Y-X*B)**2)
MSE = np.dot(np.transpose(hist - model),(hist - model))/nu
#-- Default is 95% confidence interval
alpha = 1.0 - (0.95)
#-- Student T-Distribution with D.O.F. nu
#-- t.ppf parallels tinv in matlab
tstar = scipy.stats.t.ppf(1.0-(alpha/2.0),nu)
return {'height':peak_height, 'amplitude':peak_amplitude,
'error':tstar*peak_height_error, 'stdev': peak_stdev,
'model':model, 'residuals':np.abs(res), 'MSE':MSE, 'DOF':nu}
#-- PURPOSE: calculate delta_time, latitude and longitude of the segment center
def fit_geolocation(var, distance_along_X, X_atc):
#-- calculate x relative to centroid point
rel_x = distance_along_X - X_atc
#-- design matrix
XMAT = np.transpose([np.ones_like((distance_along_X)),rel_x])
#-- Standard Least-Squares fitting (the [0] denotes coefficients output)
beta_mat = np.linalg.lstsq(XMAT,var,rcond=-1)[0]
#-- return the fitted geolocation
return beta_mat[0]
#-- PURPOSE: estimate mean and median first photon bias corrections
def calc_first_photon_bias(t_full,hist,n_pulses,n_pixels,dead_time,dt,
METHOD='direct',ITERATE=20):
#-- number of time points
nt = len(t_full)
#-- normalize residual histogram by number of pulses and number of pixels
N0_full = hist/(n_pulses*n_pixels)
#-- centroid of initial histogram
hist_centroid = np.sum(t_full*hist)/np.sum(hist)
#-- cumulative probability distribution function of initial histogram
hist_cpdf = np.cumsum(hist/np.sum(hist))
#-- linearly interpolate to 10th, 50th, and 90th percentiles
H10,hist_median,H90 = np.interp([0.1,0.5,0.9],hist_cpdf,t_full)
#-- calculate moving total of normalized histogram
#-- average number of pixels in the detector that were inactive
P_dead = np.zeros((nt))
#-- dead time as a function of the number of bins
n_dead = np.int(dead_time/dt)
#-- calculate moving total of last n_dead bins
kernel = np.triu(np.tri(nt,nt,0),k=-n_dead)
P_dead[:] = np.dot(kernel,N0_full[:,None]).flatten()
#-- estimate gain directly
if (METHOD == 'direct'):
#-- estimate gain
G_est_full = 1.0 - P_dead
#-- parameters for calculating first photon bias from calibration products
width = np.abs(H90 - H10)
strength = np.sum(N0_full)
#-- calculate corrected histogram of photon events
N_PEcorr = (n_pulses*n_pixels)*N0_full/G_est_full
N_PE = np.sum(N_PEcorr)
N_sigma = np.sqrt(n_pulses*n_pixels*N0_full)/G_est_full
#-- calculate mean corrected estimate
FPB_mean_corr = np.sum(t_full*N_PEcorr)/N_PE
FPB_mean_sigma = np.sqrt(np.sum((N_sigma*(t_full-FPB_mean_corr)/N_PE)**2))
#-- calculate median corrected estimate
PEcorr_cpdf = np.cumsum(N_PEcorr/N_PE)
sigma_cpdf = np.sqrt(np.cumsum(N_sigma**2))/N_PE
#-- calculate median first photon bias correction
#-- linearly interpolate to 40th, 50th and 60th percentiles
PE40,FPB_median_corr,PE60 = np.interp([0.4,0.5,0.6],PEcorr_cpdf,t_full)
FPB_median_sigma = (PE60-PE40)*np.interp(0.5,PEcorr_cpdf,sigma_cpdf)/0.2
elif (METHOD == 'logarithmic') and np.count_nonzero(P_dead > 0.01):
#-- find indices above threshold for computing correction
ii, = np.nonzero(P_dead > 0.01)
#-- complete gain over entire histogram
G_est_full = np.ones((nt))
#-- segment indices (above threshold and +/- dead time)
imin,imax = (np.min(ii)-n_dead,np.max(ii)+n_dead)
#-- truncate values to range of segment
N0 = N0_full[imin:imax+1]
N_corr = np.copy(N0)
nr = len(N0)
#-- calculate gain for segment
gain = np.ones((nr))
gain_prev = np.zeros((nr))
kernel = np.triu(np.tri(nr,nr,0),k=-n_dead)
#-- counter for number of iterations for segment
n_iter = 0
#-- iterate until convergence or until reaching limit of iterations
#-- using matrix algebra to avoid using a nested loop
while np.any(np.abs(gain-gain_prev) > 0.001) & (n_iter <= ITERATE):
gain_prev=np.copy(gain)
gain=np.exp(np.dot(kernel,np.log(1.0-N_corr[:,None]))).flatten()
N_corr=N0/gain
n_iter += 1
#-- add segment to complete gain array
G_est_full[imin:imax+1] = gain[:]
#-- calculate corrected histogram of photon events
N_PEcorr = (n_pulses*n_pixels)*N0_full/G_est_full
N_PE = np.sum(N_PEcorr)
N_sigma = np.sqrt(n_pulses*n_pixels*N0_full)/G_est_full
#-- calculate mean corrected estimate
FPB_mean_corr = np.sum(t_full*N_PEcorr)/N_PE
FPB_mean_sigma = np.sqrt(np.sum((N_sigma*(t_full-FPB_mean_corr)/N_PE)**2))
#-- calculate median corrected estimate
PEcorr_cpdf = np.cumsum(N_PEcorr/N_PE)
sigma_cpdf = np.sqrt(np.cumsum(N_sigma**2))/N_PE
#-- calculate median first photon bias correction
#-- linearly interpolate to 40th, 50th and 60th percentiles
PE40,FPB_median_corr,PE60 = np.interp([0.4,0.5,0.6],PEcorr_cpdf,t_full)
FPB_median_sigma = (PE60-PE40)*np.interp(0.5,PEcorr_cpdf,sigma_cpdf)/0.2
else:
#-- possible that no first photon bias correction is necessary
FPB_mean_corr = 0.0
FPB_mean_sigma = 0.0
FPB_median_corr = 0.0
FPB_median_sigma = 0.0
N_PE = np.sum(hist)
#-- return first photon bias corrections
return {'mean':FPB_mean_corr, 'mean_sigma':np.abs(FPB_mean_sigma),
'median':FPB_median_corr, 'median_sigma':np.abs(FPB_median_sigma),
'width':width, 'strength':strength, 'count':N_PE}
#-- PURPOSE: compress complete list of valid indices into a set of ranges
def compress_list(i,n):
for a,b in itertools.groupby(enumerate(i), lambda v: ((v[1]-v[0])//n)*n):
group = list(map(operator.itemgetter(1),b))
yield (group[0], group[-1])
#-- PURPOSE: centers the transmit-echo-path histogram reported by ATL03
#-- using an iterative edit to distinguish between signal and noise
def extract_tep_histogram(tep_hist_time,tep_hist,tep_range_prim):
#-- ATL03 recommends subset between 15-30 ns to avoid secondary
#-- using primary histogram range values from ATL03 tep attributes
i, = np.nonzero((tep_hist_time >= tep_range_prim[0]) &
(tep_hist_time < tep_range_prim[1]))
t_tx = np.copy(tep_hist_time[i])
n_tx = len(t_tx)
#-- noise samples of tep_hist (first 5ns and last 10 ns)
ns,ne = (tep_range_prim[0]+5e-9,tep_range_prim[1]-10e-9)
noise, = np.nonzero((t_tx <= ns) | (t_tx >= ne))
noise_p1 = []
#-- signal samples of tep_hist
signal = sorted(set(np.arange(n_tx)) - set(noise))
#-- number of iterations
n_iter = 0
while (set(noise) != set(noise_p1)) & (n_iter < 10):
#-- value of noise in tep histogram
tep_noise_value = np.sqrt(np.sum(tep_hist[i][noise]**2)/n_tx)
p_tx = np.abs(np.copy(tep_hist[i]) - tep_noise_value)
#-- calculate centroid of tep_hist
t0_tx = np.sum(t_tx[signal]*p_tx[signal])/np.sum(p_tx[signal])
#-- calculate cumulative distribution function
TX_cpdf = np.cumsum(p_tx[signal]/np.sum(p_tx[signal]))
#-- linearly interpolate to 16th and 84th percentile for RDE
TX16,TX84 = np.interp([0.16,0.84],TX_cpdf,t_tx[signal]-t0_tx)
#-- calculate width of transmitted pulse (RDE)
W_TX = 0.5*(TX84 - TX16)
#-- recalculate noise
noise_p1 = np.copy(noise)
ns,ne = (t0_tx-6.0*W_TX,t0_tx+6.0*W_TX)
noise, = np.nonzero((t_tx <= ns) | (t_tx >= ne))
signal = sorted(set(np.arange(n_tx)) - set(noise))
#-- add 1 to counter
n_iter += 1
#-- valid primary TEP return has full-width at half max < 3 ns
mx = np.argmax(p_tx[signal])
halfmax = np.max(p_tx[signal])/2.0
H1 = np.interp(halfmax,p_tx[signal][:mx],t_tx[signal][:mx])
H2 = np.interp(halfmax,p_tx[signal][:mx:-1],t_tx[signal][:mx:-1])
FWHM = H2 - H1
#-- return values
return (t_tx[signal]-t0_tx,p_tx[signal],W_TX,FWHM,ns,ne)
#-- PURPOSE: Estimate transmit-pulse-shape correction
def calc_transmit_pulse_shape(t_TX,p_TX,W_TX,W_RX,dt_W,SNR,ITERATE=50):
#-- length of the transmit pulse
nt = len(p_TX)
#-- average time step of the transmit pulse
dt = np.abs(t_TX[1]-t_TX[0])
#-- calculate broadening of the received pulse
W_spread = np.sqrt(np.max([W_RX**2 - W_TX**2,1e-22]))
#-- create zero padded transmit and received pulses (by 4*W_spread samples)
dw = np.ceil(W_spread/dt)
wmn = -np.int(np.min([0,np.round((-t_TX[0])/dt)-4*dw]))
wmx = np.int(np.max([nt,np.round((-t_TX[0])/dt)+4*dw])-nt)
t_RX = np.arange(t_TX[0]-wmn*dt,t_TX[-1]+(wmx+1)*dt,dt)
nr = len(t_RX)
TX = np.zeros((nr))
TX[wmn:wmn+nt] = np.copy(p_TX)
#-- smooth the transmit pulse by the spread
gw = scipy.signal.gaussian(nr, W_spread/dt)
RX = scipy.signal.convolve(TX/TX.sum(), gw/gw.sum(), mode='same')
#-- normalize and add a random noise estimate
RX /= np.sum(RX)
RX += (1.0-2.0*np.random.rand(nr))*(dt/dt_W)/SNR
#-- verify that all values of the synthetic received pulse are positive
RX = np.abs(RX)
#-- calculate median estimate of the synthetic received pulse
RX_cpdf = np.cumsum(RX/np.sum(RX))
#-- linearly interpolate to 50th percentile to calculate median
t_synthetic_med = np.interp(0.5,RX_cpdf,t_RX)
#-- calculate centroid for mean of the synthetic received pulse
t_synthetic_mean = np.sum(t_RX*RX)/np.sum(RX)
#-- number of iterations
n_iter = 0
#-- threshold for stopping iteration
threshold = 2e-4/299792458.0
#-- iterate until convergence of both mean and median
FLAG1,FLAG2 = (False,False)
while (FLAG1 | FLAG2) and (n_iter < ITERATE):
#-- copy previous mean and median times
tmd_prev = np.copy(t_synthetic_med)
tmn_prev = np.copy(t_synthetic_mean)
#-- truncate to within window
i,=np.nonzero((t_RX >= (tmn-0.5*dt_W)) & (t_RX <= (tmn+0.5*dt_W)))
#-- linearly interpolate to 50th percentile to calculate median
t_synthetic_med = np.interp(0.5,np.cumsum(RX[i]/np.sum(RX[i])),t_RX[i])
#-- calculate mean time for window
t_synthetic_mean = np.sum(t_RX[i]*RX[i])/np.sum(RX[i])
#-- add to iteration
n_iter += 1
#-- check iteration
FLAG1 = (np.abs(t_synthetic_med - tmd_prev) > threshold)
FLAG2 = (np.abs(t_synthetic_mean - tmn_prev) > threshold)
#-- return estimated transmit pulse corrections corrections
return {'mean':t_synthetic_mean,'median':t_synthetic_med,'spread':W_spread}
#-- PURPOSE: reads ICESat-2 ATL03 and ATL09 HDF5 files
#-- and computes heights over segments using the decomposition of histograms
def main():
#-- start MPI communicator
comm = MPI.COMM_WORLD
#-- get input files and options from system arguments
long_options = ['output=','verbose','mode=']
optlist,arglist = getopt.getopt(sys.argv[1:],'O:VM:',long_options)
#-- If no system arguments: exit with an error
if not arglist:
raise IOError('No input files entered as system arguments')
#-- command line parameters
#-- use default output file name
output_file = None
#-- verbose output of processing run
VERBOSE = False
#-- permissions mode of the output file (number in octal)
MODE = 0o775
for opt, arg in optlist:
if opt in ("-V","--verbose"):
#-- output module information for process
print(arglist[0]) if (comm.rank == 0) else None
info(comm.rank,comm.size)
VERBOSE = True
elif opt in ("-O","--output"):
#-- explicitly define output file
output_file = os.path.expanduser(arg)
elif opt in ("-M","--mode"):
#-- set permission mode of output HDF5 datasets
MODE = int(arg, 8)
#-- list of input files for processing (tilde-expand paths)
#-- first file listed contains the ATL03 file
#-- second file listed is the associated ATL09 file
ATL03_file = os.path.expanduser(arglist[0])
ATL09_file = os.path.expanduser(arglist[1])
#-- directory setup
ATL03_dir = os.path.dirname(ATL03_file)
#-- compile regular expression operator for extracting data from ATL03 files
rx1 = re.compile(r'(processed)?(ATL\d+)_(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})'
r'(\d{2})_(\d{4})(\d{2})(\d{2})_(\d{3})_(\d{2})(.*?).h5$')
#-- universal variables
#-- speed of light
c = 299792458.0
#-- associated beam pairs
associated_beam_pair = dict(gt1l='gt1r',gt1r='gt1l',gt2l='gt2r',gt2r='gt2l',
gt3l='gt3r',gt3r='gt3l')
#-- read ICESat-2 ATL03 HDF5 files (extract base parameters)
SUB,PRD,YY,MM,DD,HH,MN,SS,TRK,CYCL,GRAN,RL,VERS,AUX=rx1.findall(ATL03_file).pop()
#-- Open the HDF5 file for reading
fileID = h5py.File(ATL03_file, 'r', driver='mpio', comm=comm)
#-- read each input beam within the file
IS2_atl03_beams = [k for k in fileID.keys() if bool(re.match(r'gt\d[lr]',k))]
#-- number of GPS seconds between the GPS epoch
#-- and ATLAS Standard Data Product (SDP) epoch
atlas_sdp_gps_epoch = fileID['ancillary_data']['atlas_sdp_gps_epoch'][:]
#-- which TEP to use for a given spot (convert to 0-based index)
tep_valid_spot = fileID['ancillary_data']['tep']['tep_valid_spot'][:] - 1
tep_pce = ['pce1_spot1','pce2_spot3']
#-- valid range of times for each TEP histogram
tep_range_prim = fileID['ancillary_data']['tep']['tep_range_prim'][:]
#-- save tep parameters for a given beam
tep = {}
#-- variables of interest for generating corrected elevation estimates
Segment_ID = {}
Segment_Index_begin = {}
Segment_PE_count = {}
Segment_Distance = {}
Segment_Length = {}
Segment_Background = {}
#-- fit parameters
Segment_delta_time = {}
Segment_Height = {}
Segment_Land_Ice = {}
Segment_Minimum = {}
Segment_Maximum = {}
Segment_Amplitude = {}
Segment_Minimum_Amplitude = {}
Segment_Maximum_Amplitude = {}
Segment_dH_along = {}
Segment_dH_across = {}
Segment_Height_Error = {}
Segment_Land_Ice_Error = {}
Segment_Minimum_Error = {}
Segment_Maximum_Error = {}
Segment_dH_along_Error = {}
Segment_dH_across_Error = {}
Segment_Mean_Median = {}
Segment_X_atc = {}
Segment_X_spread = {}
Segment_Y_atc = {}
Segment_sigma_geo = {}
Segment_Longitude = {}
Segment_Latitude = {}
Segment_N_Fit = {}
Segment_N_Peaks = {}
Segment_Window = {}
Segment_RDE = {}
Segment_SNR = {}
Segment_Summary = {}
Segment_Iterations = {}
Segment_Source = {}
Segment_Pulses = {}
#-- correction parameters
FPB_mean_corr = {}
FPB_mean_sigma = {}
FPB_median_corr = {}
FPB_median_sigma = {}
mean_dead_time = {}
FPB_n_corr = {}
FPB_cal_corr = {}
TPS_mean_corr = {}
TPS_median_corr = {}
#-- for each input beam within the file
for gtx in sorted(IS2_atl03_beams):
print(gtx) if VERBOSE and (comm.rank == 0) else None
#-- beam type (weak versus strong) for time
atlas_beam_type = fileID[gtx].attrs['atlas_beam_type'].decode('utf-8')
n_pixels = 16.0 if (atlas_beam_type == "strong") else 4.0
#-- ATL03 Segment ID
Segment_ID[gtx] = fileID[gtx]['geolocation']['segment_id'][:]
#-- number of valid overlapping ATL03 segments
n_seg = len(Segment_ID[gtx]) - 1
#-- first photon in the segment (convert to 0-based indexing)
Segment_Index_begin[gtx] = fileID[gtx]['geolocation']['ph_index_beg'][:] - 1
#-- number of photon events in the segment
Segment_PE_count[gtx] = fileID[gtx]['geolocation']['segment_ph_cnt'][:]
#-- along-track distance for each ATL03 segment
Segment_Distance[gtx] = fileID[gtx]['geolocation']['segment_dist_x'][:]
#-- along-track length for each ATL03 segment
Segment_Length[gtx] = fileID[gtx]['geolocation']['segment_length'][:]
#-- ocean tide
fv = fileID[gtx]['geophys_corr']['tide_ocean'].attrs['_FillValue']
tide_ocean = np.ma.array(fileID[gtx]['geophys_corr']['tide_ocean'][:],
fill_value=fv)
tide_ocean.mask = tide_ocean.data == tide_ocean.fill_value
#-- interpolate background photon rate based on 50-shot summation
background_delta_time = fileID[gtx]['bckgrd_atlas']['delta_time'][:]
SPL = scipy.interpolate.UnivariateSpline(background_delta_time,
fileID[gtx]['bckgrd_atlas']['bckgrd_rate'][:],k=3,s=0)
Segment_Background[gtx] = SPL(fileID[gtx]['geolocation']['delta_time'][:])
#-- ATLAS spot number for beam in current orientation
spot = np.int(fileID[gtx].attrs['atlas_spot_number'])
#-- get ATLAS impulse response variables for the transmitter echo path (TEP)
tep1,tep2 = ('atlas_impulse_response','tep_histogram')
#-- get appropriate transmitter-echo-path histogram for spot
associated_pce = tep_valid_spot[spot-1]
pce = tep_pce[associated_pce]
#-- delta time of TEP histogram
tep_tod, = fileID[tep1][pce][tep2]['tep_tod'][:]
#-- truncate tep to primary histogram (reflection 43-50 ns)
#-- and extract signal tep from noise tep. calculate width of tep
#-- ATL03 recommends subsetting between 15-30 ns to avoid secondary
tep_hist_time = np.copy(fileID[tep1][pce][tep2]['tep_hist_time'][:])
tep_hist = np.copy(fileID[tep1][pce][tep2]['tep_hist'][:])
t_TX,p_TX,W_TX,FWHM,TXs,TXe = extract_tep_histogram(tep_hist_time,
tep_hist, tep_range_prim)
#-- save tep information and statistics
tep[gtx] = {}
tep[gtx]['pce'] = pce
tep[gtx]['tep_tod'] = tep_tod
tep[gtx]['tx_start'] = TXs
tep[gtx]['tx_end'] = TXe
tep[gtx]['tx_robust_sprd'] = W_TX
tep[gtx]['sigma_tx'] = FWHM
#-- channel dead time and first photon bias table for beam
cal1,cal2 = ('ancillary_data','calibrations')
channel_dead_time = fileID[cal1][cal2]['dead_time'][gtx]['dead_time'][:]
mean_dead_time[gtx] = np.mean(channel_dead_time)
fpb_dead_time = fileID[cal1][cal2]['first_photon_bias'][gtx]['dead_time'][:]
fpb_strength = fileID[cal1][cal2]['first_photon_bias'][gtx]['strength'][:]
fpb_width = fileID[cal1][cal2]['first_photon_bias'][gtx]['width'][:]
fpb_corr = fileID[cal1][cal2]['first_photon_bias'][gtx]['ffb_corr'][:]
#-- calculate first photon bias as a function of strength and width
#-- for the calculated mean dead time of the beam
ndt,ns,nw = np.shape(fpb_corr)
fpb_corr_dead_time = np.zeros((ns,nw))
for s in range(ns):
for w in range(nw):
SPL = scipy.interpolate.UnivariateSpline(fpb_dead_time/1e9,
fpb_corr[:,s,w],k=3,s=0)
fpb_corr_dead_time[s,w] = SPL(mean_dead_time[gtx])
#-- bivariate spline for estimating first-photon bias using CAL-19
CAL19 = scipy.interpolate.RectBivariateSpline(fpb_strength[0,:],
fpb_width[0,:]/1e9, fpb_corr_dead_time/1e12, kx=1, ky=1)
#-- allocate for output segment fit data
fill_value = fileID[gtx]['geolocation']['sigma_h'].attrs['_FillValue']
#-- delta time of fit photons
Distributed_delta_time = np.ma.zeros((n_seg),fill_value=fill_value)
Distributed_delta_time.mask = np.ones((n_seg),dtype=np.bool)
#-- segment fit heights
Distributed_Height = np.ma.zeros((n_seg),fill_value=fill_value)
Distributed_Height.mask = np.ones((n_seg),dtype=np.bool)
Distributed_Minimum = np.ma.zeros((n_seg),fill_value=fill_value)
Distributed_Minimum.mask = np.ones((n_seg),dtype=np.bool)
Distributed_Maximum = np.ma.zeros((n_seg),fill_value=fill_value)
Distributed_Maximum.mask = np.ones((n_seg),dtype=np.bool)
#-- land ice height corrected for first photon bias and transmit-pulse shape
Distributed_Land_Ice = np.ma.zeros((n_seg),fill_value=fill_value)
Distributed_Land_Ice.mask = np.ones((n_seg),dtype=np.bool)
#-- segment fit amplitudes
Distributed_Amplitude = np.ma.zeros((n_seg),fill_value=fill_value)
Distributed_Amplitude.mask = np.ones((n_seg),dtype=np.bool)
Distributed_Minimum_Amplitude = np.ma.zeros((n_seg),fill_value=fill_value)
Distributed_Minimum_Amplitude.mask = np.ones((n_seg),dtype=np.bool)
Distributed_Maximum_Amplitude = np.ma.zeros((n_seg),fill_value=fill_value)
Distributed_Maximum_Amplitude.mask = np.ones((n_seg),dtype=np.bool)
#-- segment fit along-track slopes
Distributed_dH_along = np.ma.zeros((n_seg),fill_value=fill_value)
Distributed_dH_along.mask = np.ones((n_seg),dtype=np.bool)
#-- segment fit height errors
Distributed_Height_Error = np.ma.zeros((n_seg),fill_value=fill_value)
Distributed_Height_Error.mask = np.ones((n_seg),dtype=np.bool)
Distributed_Minimum_Error = np.ma.zeros((n_seg),fill_value=fill_value)
Distributed_Minimum_Error.mask = np.ones((n_seg),dtype=np.bool)
Distributed_Maximum_Error = np.ma.zeros((n_seg),fill_value=fill_value)
Distributed_Maximum_Error.mask = np.ones((n_seg),dtype=np.bool)
#-- land ice height errors (max of fit or first photon bias uncertainties)
Distributed_Land_Ice_Error = np.ma.zeros((n_seg),fill_value=fill_value)
Distributed_Land_Ice_Error.mask = np.ones((n_seg),dtype=np.bool)
#-- segment fit along-track slope errors
Distributed_dH_along_Error = np.ma.zeros((n_seg),fill_value=fill_value)
Distributed_dH_along_Error.mask = np.ones((n_seg),dtype=np.bool)
#-- difference between the mean and median of the residuals from fit height
Distributed_Mean_Median = np.ma.zeros((n_seg),fill_value=fill_value)
Distributed_Mean_Median.mask = np.ones((n_seg),dtype=np.bool)
#-- along-track X coordinates of segment fit
Distributed_X_atc = np.ma.zeros((n_seg),fill_value=fill_value)
Distributed_X_atc.mask = np.ones((n_seg),dtype=np.bool)
#-- along-track X coordinate spread of points used in segment fit
Distributed_X_spread = np.ma.zeros((n_seg),fill_value=fill_value)
Distributed_X_spread.mask = np.ones((n_seg),dtype=np.bool)
#-- along-track Y coordinates of segment fit
Distributed_Y_atc = np.ma.zeros((n_seg),fill_value=fill_value)
Distributed_Y_atc.mask = np.ones((n_seg),dtype=np.bool)
#-- longitude of fit photons
Distributed_Longitude = np.ma.zeros((n_seg),fill_value=fill_value)
Distributed_Longitude.mask = np.ones((n_seg),dtype=np.bool)
#-- latitude of fit photons
Distributed_Latitude = np.ma.zeros((n_seg),fill_value=fill_value)
Distributed_Latitude.mask = np.ones((n_seg),dtype=np.bool)
#-- number of photons in fit
Distributed_N_Fit = np.ma.zeros((n_seg),fill_value=-1,dtype=np.int)
Distributed_N_Fit.mask = np.ones((n_seg),dtype=np.bool)
#-- number of peaks in the final histogram fit
Distributed_N_Peaks = np.ma.zeros((n_seg),fill_value=-1,dtype=np.int)
Distributed_N_Peaks.mask = np.ones((n_seg),dtype=np.bool)
#-- size of the window used in the fit
Distributed_Window = np.ma.zeros((n_seg),fill_value=fill_value)
Distributed_Window.mask = np.ones((n_seg),dtype=np.bool)
#-- robust dispersion estimator