Created
November 3, 2016 23:33
-
-
Save MattReimer/5b5283c5f2b17f97edfd9ffe707d6317 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Line # Hits Time Per Hit % Time Line Contents | |
============================================================== | |
44 @profile | |
45 def getVolumeAndArea(arSurvey, arMinimum, fLowerElevation, fUpperElevation, fCellSize, templateRasterPath): | |
46 | |
47 428 2277 5.3 0.0 assert arSurvey.size == arMinimum.size, "The two arrays are not the same size!" | |
48 | |
49 428 775 1.8 0.0 if fLowerElevation is None: | |
50 assert fUpperElevation is not None, "An upper elevation must be provided if the lower elevation is not provided." | |
51 else: | |
52 428 601 1.4 0.0 assert fLowerElevation >=0, "The lower elevation ({0}) must be greater than or equal to zero.".format(fLowerElevation) | |
53 428 445 1.0 0.0 if fUpperElevation is not None: | |
54 assert fLowerElevation < fUpperElevation, "The lower elevation ({0}) must be less than the upper elevation ({1}).".format(fLowerElevation, fUpperElevation) | |
55 | |
56 428 464 1.1 0.0 if fUpperElevation is not None: | |
57 assert fUpperElevation >=0, "The upper elevation ({0}) must be greater than or equal to zero.".format(fUpperElevation) | |
58 | |
59 # Mask the survey and min surface where they are NaN | |
60 428 9146511 21370.4 8.3 maSurvey = np.ma.masked_invalid(arSurvey) | |
61 428 9226549 21557.4 8.3 maMinimu = np.ma.masked_invalid(arMinimum) | |
62 | |
63 # Mask the survey and minimum surface where they are negative (-9999 used for raster NoData) | |
64 427 22776800 53341.5 20.6 maSurvey = np.ma.masked_less(maSurvey, 0) | |
65 427 21005375 49192.9 19.0 maMinimu = np.ma.masked_less(maMinimu, 0) | |
66 | |
67 # Update each mask with the other's mask | |
68 427 1110389 2600.4 1.0 maSurvey = np.ma.masked_array(maSurvey, maMinimu.mask) | |
69 427 1051001 2461.4 1.0 maMinimu = np.ma.masked_array(maMinimu, maSurvey.mask) | |
70 | |
71 427 1600 3.7 0.0 if fUpperElevation is not None: | |
72 # analysis below upper elevation | |
73 maSurvey = np.ma.masked_greater(maSurvey, fUpperElevation) | |
74 | |
75 427 532 1.2 0.0 if fLowerElevation is not None: | |
76 # analysis above lower elevation | |
77 427 22056246 51654.0 19.9 maSurvey = np.ma.masked_less(maSurvey, fLowerElevation) | |
78 | |
79 # try: | |
80 # array2raster_template(maSurvey, "D:\Temp\GCMRC\survey_{0}.tif".format(time.strftime("%Y%m%d%H%M%S")), templateRasterPath) | |
81 # array2raster_template(maMinimu, "D:\Temp\GCMRC\minimum_post_mask_{0}.tif".format(time.strftime("%Y%m%d%H%M%S")), templateRasterPath) | |
82 # array2raster_template(arMinimum, "D:\Temp\GCMRC\minimum_pre_mask_{0}.tif".format(time.strftime("%Y%m%d%H%M%S")), templateRasterPath) | |
83 # except RuntimeError as e: | |
84 # print e | |
85 | |
86 # Abort if the the entire survey is masked. | |
87 427 2440068 5714.4 2.2 if maSurvey.size == np.ma.count_masked(maSurvey): | |
88 #print "Survey entirely below analysis elevation {0} - {1}".format(fLowerElevation, fUpperElevation) | |
89 return (0.0, 0.0) | |
90 | |
91 # Calculation | |
92 427 9343547 21881.8 8.5 maDiff = maSurvey - maMinimu | |
93 | |
94 #try: | |
95 # array2raster_template(maDiff, "D:\Temp\GCMRC\diff.tif", templateRasterPath) | |
96 #except RuntimeError as e: | |
97 # print e | |
98 | |
99 427 9903772 23193.8 9.0 fVolume = maDiff.sum() * fCellSize**2 | |
100 427 2492499 5837.2 2.3 fArea = maDiff.count() * fCellSize**2 | |
101 | |
102 # if fVolume < 0: | |
103 # array2raster_template(maSurvey, "D:\\Temp\\GCMRC\\0003L\\temp_survey.tif", templateRasterPath) | |
104 # array2raster_template(maMinimu, "D:\\Temp\\GCMRC\\0003L\\temp_min.tif", templateRasterPath) | |
105 # array2raster_template(maDiff, "D:\\Temp\\GCMRC\\0003L\\temp_diff.tif", templateRasterPath) | |
106 | |
107 427 2400 5.6 0.0 assert fVolume >= 0, "The volume must be greater than or equal to zero." | |
108 427 861 2.0 0.0 assert fArea >=0, "The area must be greater than or equal to zero." | |
109 | |
110 427 539 1.3 0.0 return (fArea, fVolume) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment