def get_age_fraction_vegetmax(vegmax): """ Return a dictionary of fractions of different age classes for Bareland,Forest,Grass,Pasture and Crop. Parameters: ---------- vegmax: The 65-length PFT should be the second dimension """ if vegmax.ndim == 4: frac_F = OrderedDict() frac_F['Bareland'] = vegmax[:, 0:1, ...].sum(axis=1) frac_F['Forest_Age_1'] = vegmax[:, np.arange(ia2_1, ia9_6, 6), ...].sum(axis=1) frac_F['Forest_Age_2'] = vegmax[:, np.arange(ia2_2, ia9_6, 6), ...].sum(axis=1) frac_F['Forest_Age_3'] = vegmax[:, np.arange(ia2_3, ia9_6, 6), ...].sum(axis=1) frac_F['Forest_Age_4'] = vegmax[:, np.arange(ia2_4, ia9_6, 6), ...].sum(axis=1) frac_F['Forest_Age_5'] = vegmax[:, np.arange(ia2_5, ia9_6, 6), ...].sum(axis=1) frac_F['Forest_Age_6'] = vegmax[:, np.arange(ia2_6, ia9_6 + 1, 6), ...].sum(axis=1) frac_F['Grassland_Age_1'] = vegmax[:, [ia10_1, ia12_1], ...].sum(axis=1) frac_F['Grassland_Age_2'] = vegmax[:, [ia10_2, ia12_2], ...].sum(axis=1) frac_F['Pasture_Age_1'] = vegmax[:, [ia11_1, ia13_1], ...].sum(axis=1) frac_F['Pasture_Age_2'] = vegmax[:, [ia11_2, ia13_2], ...].sum(axis=1) frac_F['Crop_Age_1'] = vegmax[:, [ia14_1, ia15_1], ...].sum(axis=1) frac_F['Crop_Age_2'] = vegmax[:, [ia14_2, ia15_2], ...].sum(axis=1) return frac_F elif vegmax.ndim == 3: frac_F = get_age_fraction_vegetmax(vegmax[np.newaxis, ...]) frac_F = pb.Dic_Apply_Func(lambda x: x[0], frac_F) return frac_F else: raise ValueError( "Input array can only be 3- or 4-dim with PFT as the 2nd one")
def calc_area_from_LCCmatrix(veg5ini,list_LCCmatrix,area): """ Caculate the time series of area from initial vegetation array (veg5ini) and a list of LCCmatrix. Parameters: ----------- veg5ini: Initial vegetation fraction array, in the sequnce of baresoil, forest, grass, pasture, crop. list_LCCmatrix: list of LCC matrix, each matrix having 12 as the length of first dimension. In the sequence of: f2g=1-1; f2p=2-1; f2c=3-1; g2f=4-1; g2p=5-1; g2c=6-1; p2f=7-1; p2g=8-1; p2c=9-1; c2f=10-1; c2g=11-1; c2p=12-1 area: area used to calcuate from the fraction to absolute area """ #Initial arrays of veg fraction veget_1500 = veg5ini.copy() forest = veget_1500[1] grass = veget_1500[2] pasture = veget_1500[3] crop = veget_1500[4] list_forest = [] list_grass = [] list_pasture = [] list_crop = [] list_forest.append(forest) list_grass.append(grass) list_pasture.append(pasture) list_crop.append(crop) #indices f2g=1-1; f2p=2-1; f2c=3-1; g2f=4-1; g2p=5-1; g2c=6-1; p2f=7-1; p2g=8-1; p2c=9-1; c2f=10-1; c2g=11-1; c2p=12-1 for ind in range(len(list_LCCmatrix)): arr = list_LCCmatrix[ind] forest_new = forest - arr[f2g] - arr[f2p] - arr[f2c] + arr[g2f] + arr[p2f] + arr[c2f] grass_new = grass - arr[g2f] - arr[g2p] - arr[g2c] + arr[f2g] + arr[p2g] + arr[c2g] pasture_new = pasture - arr[p2f] - arr[p2g] - arr[p2c] + arr[f2p] + arr[g2p] + arr[c2p] crop_new = crop - arr[c2f] - arr[c2p] - arr[c2g] + arr[f2c] + arr[p2c] + arr[g2c] list_forest.append(forest_new) list_grass.append(grass_new) list_pasture.append(pasture_new) list_crop.append(crop_new) forest = forest_new.copy() grass = grass_new.copy() pasture = pasture_new.copy() crop = crop_new.copy() baresoil = np.tile(veget_1500[0],(len(list_crop),1,1)) series_arr = map(lambda list_arr:np.rollaxis(np.ma.dstack(list_arr),2,0),[list_forest,list_grass,list_pasture,list_crop]) veg5type = dict(zip(['bareland', 'forest', 'grass', 'pasture', 'crop'],[baresoil] + series_arr)) dic = pb.Dic_Apply_Func(lambda x:np.ma.sum(x*area,axis=(1,2)),veg5type) dft = pa.DataFrame(dic) return dft