1
+ #script to extract max/min precipitation from each basin. Used to calculate gradient
2
+ import matplotlib
3
+
4
+ matplotlib .use ("Agg" )
5
+ import pandas as pd
6
+ import csv
7
+ import os
8
+ from matplotlib import pyplot as plt
9
+
10
+ target = '/exports/csce/datastore/geos/users/s1134744/LSDTopoTools/Topographic_projects/Himalayan_front/'
11
+ #name = 'himalaya_processed.csv'
12
+
13
+ def getMaxMin (dataFrame ,basin_key ):
14
+ basinDataFrame = dataFrame .loc [dataFrame ["basin_key" ] == basin_key ]
15
+ try :
16
+ precipitationSeries = basinDataFrame ["precipitation" ]
17
+
18
+ except :
19
+ precipitationSeries = basinDataFrame ["secondary_burned_data" ]
20
+ #print precipitationSeries
21
+
22
+ #maxPrecip = precipitationSeries.max()
23
+ #minPrecip = precipitationSeries.min()
24
+ maxPrecip = precipitationSeries .first_valid_index ()
25
+ minPrecip = precipitationSeries .last_valid_index ()
26
+
27
+
28
+ return maxPrecip , minPrecip
29
+
30
+
31
+ def pathCollector (path ,name ):
32
+ #returns lists of paths and names
33
+ with open (path + name + '.csv' ,'r' ) as csvfile :
34
+ csvReader = csv .reader (csvfile ,delimiter = ',' )
35
+ next (csvReader )
36
+ full_paths = []
37
+ dem_names = []
38
+ write_names = []
39
+ for row in csvReader :
40
+ max_basin = (int (row [6 ])/ 2 )+ int (row [5 ])
41
+ full_path = path + str (row [0 ])+ '/' + ("%.2f" % float (row [2 ]))+ '_' + ("%.2f" % float (row [3 ]))+ '_' + str (row [0 ])+ '_' + str (row [1 ])+ '/' + str (row [5 ])+ '/'
42
+ dem_name = str (row [0 ])+ '_' + str (row [1 ])
43
+ write_name = str (row [1 ])+ str (row [5 ])+ '_' + str ((int (row [6 ])/ 2 )+ int (row [5 ]))
44
+ full_paths .append (full_path )
45
+ dem_names .append (dem_name )
46
+ write_names .append (write_name )
47
+ return full_paths ,dem_names ,write_names
48
+
49
+ def basinLists (path ):
50
+ with open (path + '_AllBasinsInfo.csv' ,'r' ) as basincsv :
51
+ basinPandas = pd .read_csv (basincsv ,delimiter = ',' )
52
+ basins = basinPandas ["basin_key" ]
53
+ basin_list = basins .tolist ()
54
+ return basin_list
55
+
56
+ def getMChiSegmentedPandas (path ):
57
+ with open (path + '_MChiSegmented_burned.csv' ) as mChiSource :
58
+ pandasDF = pd .read_csv (mChiSource , delimiter = ',' )
59
+ return pandasDF
60
+
61
+ def mainOperation (full_paths ,dem_names ,write_names ,dem_record ,basins ,maxs ,mins ):
62
+
63
+ #looping through each tile
64
+ for x ,y ,z in zip (full_paths ,dem_names ,write_names ):
65
+ #getting basin list for tile
66
+ try :
67
+ basin_list = basinLists (x + z )
68
+ pandasDF = getMChiSegmentedPandas (x + z )
69
+ for a in basin_list :
70
+ max_precip ,min_precip = getMaxMin (pandasDF ,a )
71
+ dem_record .append (y )
72
+ basins .append (a )
73
+ maxs .append (max_precip )
74
+ mins .append (min_precip )
75
+ except (IOError ):
76
+ print ("IOError, some info does not exist %s" % (y ))
77
+
78
+ return dem_record ,basins ,maxs ,mins
79
+
80
+ def scatterPlot (dataFrame ):
81
+
82
+ # Create a figure
83
+ fig = plt .figure (1 , figsize = (18 ,9 ))
84
+
85
+ # Create an axes
86
+ ax = fig .add_subplot (111 )
87
+ #plt.ylabel("", fontsize = 24)
88
+ plt .title (("precip_gradient" ), fontsize = 32 )
89
+
90
+ # Create the boxplot
91
+ #bp = ax.boxplot(data_to_plot, labels=header_list, showfliers=False)
92
+ bp = dataFrame .plot .scatter (x = [2 ],y = [3 ],c = 'DarkBlue' )
93
+
94
+ plt .tick_params (axis = 'both' , which = 'major' , labelsize = 18 )
95
+ # Save the figure
96
+ fig .savefig (target + 'precip_gradient_scatter.png' , bbox_inches = 'tight' )
97
+ #required to clear the axes. Each call of this function wouldn't do that otherwise.
98
+ plt .cla ()
99
+
100
+ #getting base lists
101
+
102
+ full_paths ,dem_names ,write_names = pathCollector (target ,'himalaya_processed' )
103
+ full_paths_b ,dem_names_b ,write_names_b = pathCollector (target ,'himalaya_b_processed' )
104
+
105
+ dem_record = []
106
+ basins = []
107
+ maxs = []
108
+ mins = []
109
+
110
+ dem_record ,basins ,maxs ,mins = mainOperation (full_paths ,dem_names ,write_names ,dem_record ,basins ,maxs ,mins )
111
+
112
+ dem_record ,basins ,maxs ,mins = mainOperation (full_paths_b ,dem_names_b ,write_names_b ,dem_record ,basins ,maxs ,mins )
113
+
114
+
115
+
116
+ demDF = pd .Series (dem_record )
117
+ basinDF = pd .Series (basins )
118
+ maxDF = pd .Series (maxs )
119
+ minDF = pd .Series (mins )
120
+
121
+ print demDF ,basinDF ,maxDF ,minDF
122
+ export_DF = pd .concat ([demDF ,basinDF ,maxDF ,minDF ],axis = 1 )
123
+ export_DF .to_csv (target + 'precip_gradient_data_first_last.csv' ,mode = 'w' ,header = True ,index = False )
124
+ scatterPlot (export_DF )
0 commit comments