def boost(): buf = 20 tree = GIS.read('MOD44B.tif') p_wet = GIS.read('wetSum.tropics.tif') alp = GIS.read('wetMean.tropics.tif') tree_po = tree.copy() mar = ((90-23.25)*4,(90+23.25)*4) limits = {'america':(0,600),'africa':(600,980),'asiaPaci':(0,1440)} for block in limits: para = paraLoad(block) s = para[0]+para[1]*alp[:,limits[block][0]:limits[block][1]] p = para[2]+para[3]*alp[:,limits[block][0]:limits[block][1]] tree_regPo = grow(p_wet[:,limits[block][0]:limits[block][1]],p,s,para[4]) #regional potential tree cover #####Gradient patch bufNorth = tree_po[mar[0]:mar[0]+buf,limits[block][0]:limits[block][1]] bufSouth = tree_po[mar[1]-buf:mar[1],limits[block][0]:limits[block][1]] grad = np.repeat(np.linspace(0,1,buf+2)[1:-1].reshape((-1,1)),tree_regPo.shape[1],axis=1) tree_regPo[:buf,:] = grad*tree_regPo[:buf,:] + (1-grad)*bufNorth tree_regPo[-buf:,:] = (1-grad)*tree_regPo[-buf:,:] + grad*bufSouth tree_po[mar[0]:mar[1],limits[block][0]:limits[block][1]] = tree_regPo tree_po[np.isnan(tree)] = np.nan tree_po[:,0:240][tree[:,0:240]==0] = np.nan #fix missing tile GIS.write(tree_po, 'MOD44B.boost.tif','MOD44B.tif') plot.mapDraw('MOD44B.boost.tif', 'Potential Tree Cover (%)', vMin=0, vMax=80, cmNm='terrain_r') os.system('gdalwarp -te -180 -90 180 90 -tr 1.25 0.9375 -r average -overwrite MOD44B.boost.tif MOD44B.boost.lowRe.tif')
def knife(source, region, target=None, method='average', draw=True, scale=1, vMin=0, vMax=100, titlTxt=None, areaWeight=False, bound=[-180, 90, 180, -90]): if target is None: target = source.rsplit('/', 1)[-1].rsplit('.', 1)[0] + '.' + region + '.tif' cutFile = 'vector/' + region + '.shp' if not os.path.isfile(target): os.system( 'gdalwarp -ot Float32 -wt Float32 -dstnodata 255 -overwrite -t_srs WGS84 -cutline %s -crop_to_cutline -r %s -tr 0.25 0.25 %s %s' % (cutFile, method, source, target)) if draw: from pyEarth import plot plot.mapDraw(target, scale=scale, vMin=vMin, vMax=vMax, titlTxt=titlTxt) if areaWeight: from pyEarth import GIS data = GIS.read(target) area = GIS.areaGrid(res=[0.25, 0.25], bound=bound) GIS.write( data * area / 100.0 * scale, target.rsplit('/', 1)[-1].rsplit('.', 1)[0] + '.weighted.tif', target)
def knife(source, region, target=None, method='average', draw=True, scale=1, vMin=0, vMax=100, titlTxt=None, areaWeight=False, bound = [-180,90,180,-90]): if target is None: target = source.rsplit('/',1)[-1].rsplit('.',1)[0]+'.'+region+'.tif' cutFile = 'vector/'+region+'.shp' if not os.path.isfile(target): os.system('gdalwarp -ot Float32 -wt Float32 -dstnodata 255 -overwrite -t_srs WGS84 -cutline %s -crop_to_cutline -r %s -tr 0.25 0.25 %s %s' %(cutFile, method, source, target)) if draw: from pyEarth import plot plot.mapDraw(target, scale=scale, vMin=vMin, vMax=vMax, titlTxt=titlTxt) if areaWeight: from pyEarth import GIS data = GIS.read(target) area = GIS.areaGrid(res=[0.25, 0.25], bound=bound) GIS.write(data*area/100.0*scale, target.rsplit('/',1)[-1].rsplit('.',1)[0]+'.weighted.tif', target)
def cover2score(FilePath, DataName, TypeNameList = TypeNameList, decompose=False): import pandas as pd origin = FilePath if not os.path.isfile(origin): GIS.resamp(FilePath, origin, method='near',resol='origin') if not os.path.isfile(modeCover): GIS.resamp(FilePath, modeCover, method='mode') raw = GIS.read(origin) ref = pd.read_excel(outPath+'LandTypeScore/LandScoreRule.xls', DataName, index_col=None, na_values=['NaN']) if decompose: TypeNameList = list(ref['ShortName']) #decompose to seperate PFTs for TypeName in TypeNameList: if TypeName=='Ignore': continue RAWName = outPath+'LandTypeScore/'+DataName+TypeName+'RAW.tif' outName = outPath+'LandTypeScore/'+DataName+TypeName+'.tif' if True:#not os.path.isfile(RAWName): ScoreMat = np.zeros(raw.shape) if decompose: ScoreTable = ref[['Label','Value']] ScoreTable[TypeName] = ref['ShortName'].map(lambda x: 100 if x==TypeName else np.nan if x=='Ignore' else 0) else: ScoreTable = ref[['Label','Value',TypeName]] for ValSco in ScoreTable.iterrows(): score = ValSco[1][TypeName] if score!=0: print 'cover2score:', DataName+',', TypeName+'.', ValSco[1]['Label']+':', ValSco[1][TypeName] ScoreMat[raw==ValSco[1]['Value']]=ValSco[1][TypeName] GIS.write(ScoreMat, RAWName, origin) del ScoreMat if not os.path.isfile(outName): os.system('gdalwarp -ot Float32 -wt Float32 -overwrite -r average -tr 0.25 0.25 %s %s' %(inFile, outFile)) GIS.resamp(RAWName, outName) plot.mapDraw(outName, outName+'.png',DataName+', '+TypeName, vMin=0, vMax=100)
return np.sum(np.roll(data, i - 4, axis=2) * weight[i] for i in range(9)) #################################################################################################### #dailyAve() #data=np.dstack(GIS.read('3B42_cat.nc',num) for num in range(1,366)) SIcal(filter(data)) #data=GIS.read('3B42_MAP.nc') #TRMMwrite(data,'3B42_MAP.tif') from pyEarth import plot plot.mapDraw('wetPeak.tif', 'Maxiumum Rainfall Day', cmNm='hsv', vMin=1, vMax=365, lut=366) plot.mapDraw('wetSum.tif', r'$P_w$ (mm), Mean Wet season Precipitation', cmNm='rainbow_r', vMin=0, vMax=2000) plot.mapDraw('wetMean.tif', r'$\alpha_w$ (mm), Mean Wet season Rainfall Depth', cmNm='rainbow_r', vMin=0, vMax=18) plot.mapDraw('wetLength.tif', 'Rainfall Season Length', cmNm='rainbow_r')
from pyEarth import plot plot.mapDraw('MOD44B.tropics.boost.tif', 'Potential Tree Cover (%)', vMin=0, vMax=80, cmNm='terrain_r') plot.mapDraw('MOD44B.tropics.tif', 'MOD44 Tree Cover (%)', vMin=0, vMax=80, cmNm='terrain_r') plot.mapDraw('/data8/data/guol3/TRMM/3B42/wetPeak.tif', 'Maxiumum Rainfall Day', cmNm='hsv',vMin=1, vMax=365, lut=366) plot.mapDraw('/data8/data/guol3/TRMM/3B42/wetLength.tif', 'Rainfall Season Length', cmNm='rainbow_r') plot.mapDraw('/data8/data/guol3/TRMM/3B42/3B42_MAP.tif', 'Mean Annual Precipitation', cmNm='rainbow_r', vMin=0, vMax=4000) plot.mapDraw('wetSum.tropics.tif', r'$P_w$ (mm), Mean Wet season Precipitation', cmNm='rainbow_r',vMin=0, vMax=2000) plot.mapDraw('wetMean.tropics.tif', r'$\alpha_w$ (mm), Mean Wet season Rainfall Depth', cmNm='rainbow_r', vMin=0, vMax=18) plot.mapDraw('/data8/data/guol3/TRMM/3B42/wetSum.tif', r'$P_w$ (mm), Mean Wet season Precipitation', cmNm='rainbow_r',vMin=0, vMax=2000) plot.mapDraw('/data8/data/guol3/TRMM/3B42/wetMean.tif', r'$\alpha_w$ (mm), Mean Wet season Rainfall Depth', cmNm='rainbow_r', vMin=0, vMax=18)
seaSum[sumMask]+=data[:,:,z][sumMask] occSum[sumMask]+=(data[:,:,z][sumMask]>0).astype(np.int) occRat = occSum*1.0/np.ceil(length) seaAve = seaSum/occSum TRMMwrite(occRat, 'wetRatio.tif') TRMMwrite(seaAve, 'wetMean.tif') TRMMwrite(peak, 'wetPeak.tif') TRMMwrite(length, 'wetLength.tif') TRMMwrite(seaSum, 'wetSum.tif') return None #################################################################################################### def filter(data, weight=[0.1,0.1,0.1,0.1,0.2,0.1,0.1,0.1,0.1]): return np.sum(np.roll(data,i-4,axis=2)*weight[i] for i in range(9)) #################################################################################################### #dailyAve() #data=np.dstack(GIS.read('3B42_cat.nc',num) for num in range(1,366)) SIcal(filter(data)) #data=GIS.read('3B42_MAP.nc') #TRMMwrite(data,'3B42_MAP.tif') from pyEarth import plot plot.mapDraw('wetPeak.tif', 'Maxiumum Rainfall Day', cmNm='hsv',vMin=1, vMax=365, lut=366) plot.mapDraw('wetSum.tif', r'$P_w$ (mm), Mean Wet season Precipitation', cmNm='rainbow_r',vMin=0, vMax=2000) plot.mapDraw('wetMean.tif', r'$\alpha_w$ (mm), Mean Wet season Rainfall Depth', cmNm='rainbow_r', vMin=0, vMax=18) plot.mapDraw('wetLength.tif', 'Rainfall Season Length', cmNm='rainbow_r')
from pyEarth import plot plot.mapDraw('MOD44B.tropics.boost.tif', 'Potential Tree Cover (%)', vMin=0, vMax=80, cmNm='terrain_r') plot.mapDraw('MOD44B.tropics.tif', 'MOD44 Tree Cover (%)', vMin=0, vMax=80, cmNm='terrain_r') plot.mapDraw('/data8/data/guol3/TRMM/3B42/wetPeak.tif', 'Maxiumum Rainfall Day', cmNm='hsv', vMin=1, vMax=365, lut=366) plot.mapDraw('/data8/data/guol3/TRMM/3B42/wetLength.tif', 'Rainfall Season Length', cmNm='rainbow_r') plot.mapDraw('/data8/data/guol3/TRMM/3B42/3B42_MAP.tif', 'Mean Annual Precipitation', cmNm='rainbow_r', vMin=0, vMax=4000) plot.mapDraw('wetSum.tropics.tif', r'$P_w$ (mm), Mean Wet season Precipitation', cmNm='rainbow_r', vMin=0,
def layerMap(self): try: if self.pftPo is None: self.boost() except AttributeError: self.boost() from pyEarth import plot nameList = ['BareLand','NeedleEverTemp','NeedleEverBoreal','NeedleDeciduBoreal','BroadEverTrop','BroadEverTemp','BroadDeciduTrop','BroadDeciduTemp','BroadDeciduBoreal','BroadEverShrubTemp','BroadDeciduShrubTemp','BroadDeciduShrubBoreal','C3Arctic','C3','C4','C3Crop'] for i in range(len(nameList)): plot.mapDraw(self.pft[i,...], 'Original '+nameList[i], nameList[i]+'Old.png') plot.mapDraw(self.pftPo[i,...], 'New '+nameList[i], nameList[i]+'New.png') plot.mapDraw(self.pft[:len(nameList),...].sum(axis=0), 'Original PFT sum', 'PFTsumOld.png') plot.mapDraw(self.pftPo[:len(nameList),...].sum(axis=0), 'New PFT sum', 'PFTsumNew.png') plot.mapDraw(self.tree, 'Original Tree Cover', 'treeOld.png') plot.mapDraw(self.treePo, 'New Tree Cover', 'treeNew.png')
from pyEarth import plot import os ''' plot.mapDraw('MCD12Q1.2009.cerealCrop.tropics.tif', scale=100, vMin=0, vMax=100, titlTxt='MCD12 PFT: Cereal Crop') plot.mapDraw('MCD12Q1.2009.broadCrop.tropics.tif', scale=100, vMin=0, vMax=100, titlTxt='MCD12 PFT: Broad-leaf Crops') os.system('gdal_calc.py -A GLOBCOVER_L4_200901_200912_V2.3.rainCrop.tropics.tif -B GLOBCOVER_L4_200901_200912_V2.3.mosaVege.tropics.tif -C GLOBCOVER_L4_200901_200912_V2.3.mosaCrop.tropics.tif -D GLOBCOVER_L4_200901_200912_V2.3.irrigCrop.tropics.tif --outfile=%s --calc="A+B*0.35+C*0.6+D"' %('GLOBCOVER_L4_200901_200912_V2.3.totaCrop.tropics.tif')) ''' os.system( 'gdal_calc.py -A MCD12Q1.2009.cerealCrop.TRMM.tif -B MCD12Q1.2009.broadCrop.TRMM.tif --outfile=%s --calc="A+B"' % ('MCD12Q1.2009.totaCrop.TRMM.tif')) plot.mapDraw('MCD12Q1.2009.totaCrop.TRMM.tif', scale=100, vMin=0, vMax=100, titlTxt='MCD12 PFT: Total Crops') plot.mapDraw('GLOBCOVER_L4_200901_200912_V2.3.totaCrop.world.tif', scale=100, vMin=0, vMax=100, titlTxt='GLC2009: Total Crops')
def layerMap(self): try: if self.pftPo is None: self.boost() except AttributeError: self.boost() from pyEarth import plot nameList = [ 'BareLand', 'NeedleEverTemp', 'NeedleEverBoreal', 'NeedleDeciduBoreal', 'BroadEverTrop', 'BroadEverTemp', 'BroadDeciduTrop', 'BroadDeciduTemp', 'BroadDeciduBoreal', 'BroadEverShrubTemp', 'BroadDeciduShrubTemp', 'BroadDeciduShrubBoreal', 'C3Arctic', 'C3', 'C4', 'C3Crop' ] for i in range(len(nameList)): plot.mapDraw(self.pft[i, ...], 'Original ' + nameList[i], nameList[i] + 'Old.png') plot.mapDraw(self.pftPo[i, ...], 'New ' + nameList[i], nameList[i] + 'New.png') plot.mapDraw(self.pft[:len(nameList), ...].sum(axis=0), 'Original PFT sum', 'PFTsumOld.png') plot.mapDraw(self.pftPo[:len(nameList), ...].sum(axis=0), 'New PFT sum', 'PFTsumNew.png') plot.mapDraw(self.tree, 'Original Tree Cover', 'treeOld.png') plot.mapDraw(self.treePo, 'New Tree Cover', 'treeNew.png')