def stats(output, date, fd): fd.write('-' * 80) fd.write(os.linesep) fd.write('NDVI class statistics ({0}: {1})'.format(output, date)) fd.write(os.linesep) fd.write('-' * 80) fd.write(os.linesep) from subprocess import PIPE ret = Module('v.report', map=output, option='area', stdout_=PIPE) for line in ret.outputs.stdout.splitlines( )[1:]: # skip first line (cat|label|area) # parse line (eg. 1||2712850) data = line.split('|') cat = data[0] area = float(data[-1]) fd.write('NDVI class {0}: {1:.1f} ha'.format(cat, area / 1e4)) fd.write(os.linesep) data = vector_db_select(output) for vals in data['values'].values(): # unfortunately we need to cast values by float fd.write('NDVI class {0}: {1:.4f} (min) {2:.4f} (max) {3:.4f} (mean)'. format(vals[0], float(vals[2]), float(vals[3]), float(vals[4]))) fd.write(os.linesep)
def stats(): print('-' * 80) print('NDVI class statistics') print('-' * 80) from subprocess import PIPE ret = Module('v.report', map=options["output"], option='area', stdout_=PIPE) for line in ret.outputs.stdout.splitlines( )[1:]: # skip first line (cat|label|area) # parse line (eg. 1||2712850) data = line.split('|') cat = data[0] area = float(data[-1]) print('NDVI class {0}: {1:.1f} ha'.format(cat, area / 1e4)) # v.to.rast: use -c flag for updating statistics if exists Module('v.rast.stats', flags='c', map=options["output"], raster='ndvi', column_prefix='ndvi', method=['minimum', 'maximum', 'average']) data = vector_db_select(options["output"]) for vals in data['values'].itervalues(): # unfortunately we need to cast values by float print('NDVI class {0}: {1:.4f} (min) {2:.4f} (max) {3:.4f} (mean)'. format(vals[0], float(vals[2]), float(vals[3]), float(vals[4])))
def basin_discharge_plots(self, save=False, show=True, legend=True, bigtitle=False, alltimes=False, onefig=True, ICE=None): from grass.script import vector as vect import matplotlib.pyplot as plt Qmodern = dict(Mississippi=18430., Mackenzie=9910., Colorado=665., Rio_Grande=150., Hudson=620., Columbia=7500., Susquehanna=1082., Saint_Lawrence=12000., Hudson_Strait=30900., Yukon=6428.) if ICE: pass else: try: ICE = self.ICE except: # hack-ey solution here to plotting ICE = grass.parse_command('g.gisenv')['LOCATION_NAME'] ICE = re.findall("[a-zA-Z0-9]+", ICE)[0] self.ICE = ICE # River list self.riversAll = np.array(list(set(list(grass.parse_command('v.db.select', map='river_mouth_regions', columns='river', flags="c"))))) self.rnumAll = np.arange(1,len(self.riversAll)+1) # Is this OK? what about database table rnum? # Create dicts to hold lists of Q_i, Q_m, Q_t # WHICH RIVERS WE USE ARE PRE-DEFINED HERE; CLUNKY TO CHANGE! self.Q_i = dict(Mississippi=[], Mackenzie=[], Colorado=[], Rio_Grande=[], Hudson=[], Columbia=[], Susquehanna=[], Saint_Lawrence=[], Hudson_Strait=[], Yukon=[]) self.Q_m = dict(Mississippi=[], Mackenzie=[], Colorado=[], Rio_Grande=[], Hudson=[], Columbia=[], Susquehanna=[], Saint_Lawrence=[], Hudson_Strait=[], Yukon=[]) self.Q_t = dict(Mississippi=[], Mackenzie=[], Colorado=[], Rio_Grande=[], Hudson=[], Columbia=[], Susquehanna=[], Saint_Lawrence=[], Hudson_Strait=[], Yukon=[]) #shortRivers = np.array(['Mississippi River', 'Susquehanna River', 'Hudson River', 'Saint Lawrence River', 'Hudson Strait', 'Mackenzie River', 'Columbia River', 'Colorado River', 'Rio Grande']) #srnu = np.array(['Mississippi', 'Susquehanna', 'Hudson', 'Saint_Lawrence', 'Hudson_Strait', 'Mackenzie', 'Columbia', 'Colorado', 'Rio_Grande']) #self.Q_i = dict(Mississippi=[], Mackenzie=[], Columbia=[], Saint_Lawrence=[], Hudson_Strait=[], Other_North=[], Volga=[], White_Sea = [], Daugava_and_Baltic=[]) #self.Q_m = dict(Mississippi=[], Mackenzie=[], Columbia=[], Saint_Lawrence=[], Hudson_Strait=[], Other_North=[], Volga=[], White_Sea = [], Daugava_and_Baltic=[]) #self.Q_t = dict(Mississippi=[], Mackenzie=[], Columbia=[], Saint_Lawrence=[], Hudson_Strait=[], Other_North=[], Volga=[], White_Sea = [], Daugava_and_Baltic=[]) srnu = self.Q_t.keys() shortRivers = [] for i in range(len(srnu)): # space for list of names shortRivers.append(srnu[i].replace('_', ' ')) # Update rivers and rnum to include only these desired outputs self.rivers = np.array(self.Q_t.keys()) for i in range(len(self.rivers)): # space for list of names self.rivers[i] = self.rivers[i].replace('_', ' ') self.rnum = [] for river in self.rivers: self.rnum.append(self.rnumAll[self.riversAll == river][0]) for age in self.ages: print age drainage_basins = 'drainage_basins_' + age try: drainage_basins_cols = vect.vector_db_select(drainage_basins)['columns'] drainage_basins_lol = vect.vector_db_select(drainage_basins)['values'].values() river_name_column = (np.array(drainage_basins_cols) == 'river').nonzero()[0][0] meltwater_discharge_column = (np.array(drainage_basins_cols) == 'Q_ice').nonzero()[0][0] meteoric_discharge_column = (np.array(drainage_basins_cols) == 'Q_meteoric_uncorrected').nonzero()[0][0] #total_discharge_column = (np.array(drainage_basins_cols) == 'Q_total').nonzero()[0][0] except: print 'Error at', age drainage_basins_cols = None for river in self.rivers: rnu = river.replace(' ', '_') # Underscore for dict # Some rivers not appearing in database table, so have to do this as a quick fix! # Maybe clashing rules files from running many of these at once? self.Q_i[rnu].append(np.nan) self.Q_m[rnu].append(np.nan) self.Q_t[rnu].append(np.nan) if drainage_basins_cols: for river in self.rivers: rnu = river.replace(' ', '_') # Underscore for dict # Some rivers not appearing in database table, so have to do this as a quick fix! # Maybe clashing rules files from running many of these at once? self.Q_i[rnu].append(np.nan) self.Q_m[rnu].append(np.nan) self.Q_t[rnu].append(np.nan) for row in drainage_basins_lol: if row[river_name_column] == river: #print row #if row[total_discharge_column] != '': self.Q_i[rnu][-1] = row[meltwater_discharge_column] self.Q_m[rnu][-1] = row[meteoric_discharge_column] """ else: print age, '!!!', river self.Q_i[rnu].append(np.nan) self.Q_m[rnu].append(np.nan) self.Q_t[rnu].append(np.nan) if river == 'Hudson': print age, row[total_discharge_column] """ self.Q_meteoric_simulated_now = [] for river in self.rivers: rnu = river.replace(' ', '_') # Underscore for dict self.Q_m[rnu] = np.array(self.Q_m[rnu]).astype(float) self.Q_t[rnu] = np.array(self.Q_i[rnu]).astype(float) try: self.Q_meteoric_simulated_now.append(self.Q_m[rnu][-1]) self.Q_m[rnu] *= float(Qmodern[rnu]) / np.mean(np.array(self.Q_m[rnu]).astype(float)[self.ages_numeric < 3000]) self.Q_t[rnu] += np.array(self.Q_m[rnu]).astype(float) # now forced to match! except: self.Q_meteoric_simulated_now.append(np.nan) self.output.HighDischargeTimes(self) outvars = {'ages': self.ages, 'ages_numeric': self.ages_numeric, \ 'Q_i': self.Q_i, 'Q_m': self.Q_m, 'Q_t': self.Q_t} np.save(self.ICE+'.npy', outvars) sys.exit("Mostly, need to fix long vs. short names, but just use Qhist_all.py after running this") if alltimes: agei=0 if onefig: print "OneFig doesn't function currently for all times" #for age in self.ages: for i in range(len(self.rivers)): river = self.rivers[i] print river rnu = river.replace(' ', '_') # Underscore for dict plt.figure(i, figsize=(8,5)) if bigtitle: plt.plot(self.ages_numeric/1000., self.Q_t[rnu],'k-', linewidth=10, label='Total') plt.plot(self.ages_numeric/1000., self.Q_m[rnu],'g-', linewidth=4, label='Meteoric') plt.plot(self.ages_numeric/1000., self.Q_i[rnu], 'b--', linewidth=4, label='Melt') plt.plot(self.ages_numeric/1000.[1+agei], self.Q_t[rnu][agei], 'ro', markersize=20) elif onefig: pass else: plt.plot(self.ages_numeric/1000., self.Q_t[rnu],'k-', linewidth=6, label='Total') plt.plot(self.ages_numeric/1000., self.Q_m[rnu],'g-', linewidth=2, label='Meteoric') plt.plot(self.ages_numeric/1000., self.Q_i[rnu], 'b--', linewidth=2, label='Melt') plt.plot(self.ages_numeric/1000.[1+agei], self.Q_t[rnu][agei], 'ro', markersize=12) if legend: plt.legend(loc='upper left') elif onefig: pass if bigtitle: plt.title(river, fontsize=48, fontweight='bold') plt.xlabel('Age [ka]', fontsize=24) plt.ylabel('Discharge [m$^3$/s]', fontsize=24) elif onefig: pass else: plt.title(river, fontsize=24, fontweight='bold') plt.xlabel('Age [ka]', fontsize=16) plt.ylabel('Discharge [m$^3$/s]', fontsize=16) fig = plt.gcf() if bigtitle: fig.subplots_adjust(top=0.84) for label in plt.gca().get_xticklabels() + plt.gca().get_yticklabels(): label.set_fontsize(20) fig.subplots_adjust(left=0.22) fig.subplots_adjust(bottom=0.15) elif onefig: pass else: fig.subplots_adjust(left=0.16) fig.subplots_adjust(bottom=0.12) if save: if bigtitle: plt.savefig('alltimes/dischargefigs_bigtitles/'+rnu+'_'+age) elif legend: plt.savefig('alltimes/dischargefigs_legends/'+rnu+'_'+age) else: plt.savefig('alltimes/dischargefigs/'+rnu+'_'+age) plt.clf() agei+=1 if show: print "Not allowing hundreds of figures to be shown - poor computer!" else: if onefig: print "onefig ignores bigtitle" fig = plt.figure(figsize = (10, 14)) fig.text((1+.17-.1)/2., 0.04, 'Age [ka]', ha='center', va='center', fontsize=24, fontweight='bold') fig.text(0.06, 0.5, 'Discharge [m$^3$s$^{-1}$]', ha='center', va='center', rotation='vertical', fontsize=24, fontweight='bold') #ax0 = fig.add_subplot(111) #ax0.spines['top'].set_color('none') #ax0.spines['bottom'].set_color('none') #ax0.spines['left'].set_color('none') #ax0.spines['right'].set_color('none') #ax0.tick_params(labelcolor='w', top='off', bottom='off', left='off', right='off') #ax0.set_xlabel('Age [ka]', fontsize=16) #ax0.set_ylabel('Discharge [m$^3$s$^{-1}$]', fontsize=16) iof = 1 for i in range(len(shortRivers)): print shortRivers[i] rnu = srnu[i].replace(' ', '_') # Underscore for dict ax = fig.add_subplot(5, 2, iof) try: for row in self.EnhancedQ[shortRivers[i]]: ax.axvspan(row[0], row[1], facecolor='0.8', linewidth='0') except: print "No geological constraints entered for", shortRivers[i] ax.plot(self.ages_numeric/1000., self.Q_t[rnu],'k-', linewidth=6, label='Total discharge') ax.plot(self.ages_numeric/1000., self.Q_i[rnu], 'b--', linewidth=2, label='Meltwater discharge') ax.plot(self.ages_numeric/1000., self.Q_m[rnu],'g-', linewidth=2, label='Meteoric water\n(P-ET) discharge') ax.set_title(shortRivers[i], fontsize=16, fontweight='bold') ax.set_xlim((0, 20)) ax.set_ylim((0, ax.get_ylim()[-1])) try: plt.plot(self.ages_numeric[-1]/1000., self.Q_meteoric_simulated_now[(self.rivers == rnu).nonzero()[0][0]], 'go', markersize=10, zorder=-100) except: print "No discharge constraints entered for", shortRivers[i] # set y-lim based on model outputs """ if rnu == 'Mississippi': ax.set_ylim((0, 200000)) elif rnu == 'Susquehanna': ax.set_ylim((0, 40000)) elif rnu == 'Hudson': ax.set_ylim((0, 120000)) elif rnu == 'Saint_Lawrence': ax.set_ylim((0, 160000)) elif rnu == 'Hudson_Strait': ax.set_ylim((0, 250000)) elif rnu == 'Mackenzie': ax.set_ylim((0, 160000)) elif rnu == 'Columbia': ax.set_ylim((0, 18000)) elif rnu == 'Colorado': ax.set_ylim((0, 2000)) elif rnu == 'Rio_Grande': ax.set_ylim((0, 600)) """ if iof == 9: ax.legend(loc = 'center', bbox_to_anchor = (1.71, 0.5), fontsize=14) iof += 1 fig.tight_layout() fig.subplots_adjust(left=.17, bottom=.08, right=None, top=None, wspace=None, hspace=.4) if save: plt.savefig('all_rivers_'+ICE+'.png') plt.savefig('all_rivers_'+ICE+'.pdf') else: for i in range(len(self.rivers)): river = self.rivers[i] rnu = river.replace(' ', '_') # Underscore for dict plt.figure(i, figsize=(8,5)) if bigtitle: plt.plot(self.ages_numeric/1000., self.Q_t[rnu],'k-', linewidth=10, label='Total') plt.plot(self.ages_numeric/1000., self.Q_m[rnu],'g-', linewidth=4, label='Meteoric') plt.plot(self.ages_numeric/1000., self.Q_i[rnu], 'b--', linewidth=4, label='Melt') else: plt.plot(self.ages_numeric/1000., self.Q_t[rnu],'k-', linewidth=6, label='Total') plt.plot(self.ages_numeric/1000., self.Q_m[rnu],'g-', linewidth=2, label='Meteoric') plt.plot(self.ages_numeric/1000., self.Q_i[rnu], 'b--', linewidth=2, label='Melt') if legend: plt.legend(loc='upper left') if bigtitle: plt.title(river, fontsize=48, fontweight='bold') plt.xlabel('Age [ka]', fontsize=24) plt.ylabel('Discharge [m$^3$/s]', fontsize=24) else: plt.title(river, fontsize=24, fontweight='bold') plt.xlabel('Age [ka]', fontsize=16) plt.ylabel('Discharge [m$^3$/s]', fontsize=16) fig = plt.gcf() if bigtitle: fig.subplots_adjust(top=0.84) for label in plt.gca().get_xticklabels() + plt.gca().get_yticklabels(): label.set_fontsize(20) fig.subplots_adjust(left=0.22) fig.subplots_adjust(bottom=0.15) else: fig.subplots_adjust(left=0.16) fig.subplots_adjust(bottom=0.12) if save: if bigtitle: plt.savefig('dischargefigs_bigtitles/'+rnu) elif legend: plt.savefig('dischargefigs_legends/'+rnu) else: plt.savefig('dischargefigs/'+rnu) if show: plt.show() else: if onefig: plt.close() else: for i in range(len(self.rivers)): plt.figure(i) #plt.clf() plt.close()
#---------------------------------------------------- # 9.2 - perform some maintenance. remove lines that have NULL entries colList = ['orig', 'r10pa', 'r15pa', 'r20pa', 'r25pa', 'r30pa', \ 'r35pa','r40pa', 'r45pa', 'r50pa', 'r55pa', 'r01pa', \ 'r10ap', 'r15ap', 'r20ap', 'r25ap', 'r30ap', 'r35ap', \ 'r40ap', 'r45ap', 'r50ap', 'r55ap', 'r01ap'] region = ['amazon','qf','andes'] for reg in region: for param in ['aspect_compass','slope']: vect = reg + '_' + param + '_random' print 'maintenance for vector ' + vect for col in colList: # get column from db as a dict col_dict = gvect.vector_db_select(map=vect, columns=col)['values'] # get cats of NULL entries null_list = [int(i[1]) for i in col_dict.values() if i[0]==''] print 'removing NULL entries...' for n in null_list: grass.write_command("db.execute", \ stdin="DELETE FROM %s WHERE cat = %d" % (vect,n)) grass.db_describe(vect) #---------------------------------------------------- # 9.3 - correlation for slope (linear) # files for results os.chdir('/Volumes/HDD/Users/guano/Dropbox/artigos/derivadas_dem/stats') fileOut = open('correlacao_PA_AP_original_slope.txt', 'w')