├── Adj_RUSLE.py ├── Input_File.py ├── Output_File.py ├── README.md ├── The RUSLE model on Google Earth Engine.pdf ├── erosivity.f90 ├── erosivity.so ├── gee.py ├── gsmap_precip_proc.py ├── scaling_func.py └── sourcegrid.txt /Adj_RUSLE.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import numpy as np 3 | from netCDF4 import Dataset 4 | from sys import argv 5 | 6 | #code Adjusted RUSLE model as presented in Naipal et al., 2015., 2016 7 | 8 | def K_factor(fname,llx,urx,lly,ury,lat_h,lon_h): 9 | 10 | ''' 11 | This function calculates the K factor based on the method of Torri et al., 1997 12 | at 1 km spatial resolution and then upscales the result to 30m 13 | 14 | ''' 15 | from Input_File import input_K 16 | from scaling_func import upscaling 17 | #read data 18 | sand_t,silt_t,clay_t,oc_t,su_code90,lat_c,lon_c = input_K(fname,llx,urx,lly,ury) 19 | #calculate Dg & K 20 | Dg = (sand_t*np.log((2.*0.05)**0.5))+(silt_t*np.log((0.05*0.002)**0.5))+(clay_t*np.log((0.002*0.00005)**0.5)) 21 | K = 0.0293*(0.65-Dg+0.24*Dg**2.)*np.exp((-0.0021*(oc_t/clay_t))-(0.00037*(oc_t/clay_t)**2.)-(4.02*clay_t)+(1.72*clay_t**2.)) 22 | #Volcanic soils K = 0.08 (Andisols) 23 | #volcanic soils are andisols numbers 20-27 from Taxousda 24 | su_code90[su_code90>28]=0;su_code90[su_code90<20]=0 25 | k_volc=np.zeros((sand_t.shape)) 26 | k_volc[su_code90!=0]=0.08 27 | k_volc[su_code90==0]=K[su_code90==0] 28 | #rescale K factor to 30m 29 | r_sel,k_sel=upscaling(lat_h,lon_h,lat_c,lon_c) 30 | K_out1=k_volc[r_sel[:]];K_out=K_out1[:,k_sel[:]] 31 | 32 | return K_out 33 | 34 | def C_factor(fname,year_start,year_end,llx,urx,lly,ury): 35 | 36 | ''' 37 | This function calculates the C factor based on ndvi at monthly resolution 38 | and adjusts maximum values for forest and grassland 39 | ''' 40 | from Input_File import input_lc,input_C 41 | from scaling_func import upscaling 42 | #read data 43 | ndvi=input_C(fname,year_start,year_end,llx,urx,lly,ury) 44 | treefrac,grassfrac,cropfrac=input_lc(fname,llx,urx,lly,ury) 45 | rows=treefrac.shape[0];kols=treefrac.shape[1] 46 | #calculate monthly C factor 47 | ndvi[ndvi<0.] = np.nan 48 | ndvi=ndvi.reshape(12,rows*kols) 49 | c = np.exp(-2*(ndvi/(1-ndvi))) 50 | tree_h = np.ravel(treefrac) 51 | grass_h = np.ravel(grassfrac) 52 | #define maximum c values for grass and tree 53 | for i in range(12): 54 | c_temp_tree=np.zeros((rows*kols)) 55 | c_temp_tree[tree_h>0.7]=c[i,tree_h>0.7] 56 | c_temp_tree[c_temp_tree>0.01]=0.01 57 | 58 | c_temp_grass=np.zeros((rows*kols)) 59 | c_temp_grass[grass_h>0.7]=c[i,grass_h>0.7] 60 | c_temp_grass[c_temp_grass>0.05]=0.05 61 | 62 | c[i,tree_h>0.7]=c_temp_tree[tree_h>0.7] 63 | c[i,grass_h>0.7]=c_temp_grass[grass_h>0.7] 64 | 65 | C_all = np.asarray(c).reshape(12,rows,kols) 66 | C_all[C_all==0.]=np.nan 67 | C_out=np.nanmean(C_all,axis=0) 68 | C_out[np.isnan(C_out)==True]=0. 69 | 70 | return C_out 71 | 72 | def LS_factor(fname,llx,urx,lly,ury): 73 | 74 | ''' 75 | This function calculates the S factor based on Nearing (1997) and 76 | the L factor is derived with the method of Desmet and Govers (1996) 77 | ''' 78 | 79 | from Input_File import input_LS 80 | #read data 81 | slope,aspect,A_contr=input_LS(fname,llx,urx,lly,ury) #in degrees and m2 82 | rows=slope.shape[0] 83 | kols=slope.shape[1] 84 | #make correction for A_contr shape 85 | A_contr=A_contr[:-1,:] 86 | A_contr[A_contr>10000.]=10000. #max slope length for RUSLE is 305m 87 | #calculate slope length 88 | slope_rad=np.radians(slope) 89 | aspect_rad=np.radians(aspect) 90 | F=(np.sin(slope_rad)/0.0896)/(3*np.sin(slope_rad)**0.8+0.56) #slope in degrees 91 | m=F/(1+F) 92 | X=np.sin(aspect_rad)+np.cos(aspect_rad) 93 | D=30. 94 | L=((A_contr+D**2)**(m+1)-A_contr**(m+1))/(D**(m+2)*X**m*22.13**m) 95 | L[L<0.] = 0.; 96 | L[np.isnan(L)==True] = 0. 97 | # calculate S factor 98 | S=-1.5+(17./(1+np.exp(2.3-6.1*np.sin(slope_rad)))) #slope in degrees 99 | S[S<0.] = 0. 100 | S[np.isnan(S)==True] = 0. 101 | LS_out=S*L 102 | 103 | return LS_out 104 | 105 | def R_factor(fname,llx,urx,lly,ury,lat_h,lon_h): 106 | 107 | ''' 108 | This function calculates the erosivity factor at 5 arcmin resolution using the erosivity.f90 script 109 | erosivity.f90 has to be wrapped to be able to import as a function in python using f2py 110 | Before executing this function run in your terminal: f2py -c -m erosivity erosivity.f90 111 | ''' 112 | 113 | from Input_File import input_R 114 | import sys 115 | sys.path.insert(0,'/home/wieka20/Dokumente/Adj_RUSLE') 116 | import erosivity 117 | from scaling_func import upscaling 118 | #read data input for erosivity 119 | z,p,k,sdii,rows_sel,kols_sel,lat_c,lon_c=input_R(fname,llx,urx,lly,ury) 120 | #calculate R factor at 10km res 121 | r=erosivity.erosivity.main(z,p,k,sdii,len(rows_sel),len(kols_sel)) #shape(kols*rows) 122 | r[r==-9999.]=np.nan 123 | R=r.reshape(len(rows_sel),len(kols_sel)) 124 | #rescale R factor to 30m 125 | r_sel,k_sel=upscaling(lat_h,lon_h,lat_c,lon_c) 126 | R_out1=R[r_sel[:]];R_out=R_out1[:,k_sel[:]] 127 | 128 | return R_out 129 | 130 | def P_factor(fname,llx,urx,lly,ury): 131 | 132 | ''' 133 | This function calculates the P factor for areas under terracing according to Haidong et al, 2016 134 | ''' 135 | from Input_File import input_P 136 | 137 | terrace_frac=input_P(fname,llx,urx,lly,ury) 138 | P_out =0.12*terrace_frac 139 | P_out[P_out==0.]=1. #if no terracing then no reduction in erosion 140 | 141 | return P_out 142 | 143 | 144 | -------------------------------------------------------------------------------- /Input_File.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import numpy as np 3 | from netCDF4 import Dataset 4 | import sys 5 | import os.path 6 | import gdal 7 | #import ee 8 | #ee.Initialize() 9 | #import richdem as rd 10 | 11 | def input_LS(fname,llx,urx,lly,ury): 12 | 13 | ''' 14 | Derive slope, aspect, and contributing area from a 30m DEM on GEE platform 15 | Here I only derived slope and aspect from GEE and exported that as Gtiff images 16 | ''' 17 | 18 | ##################GEE part######################## 19 | #region=ee.Geometry.Rectangle([llx,lly,urx,ury]) 20 | #image = ee.Image('USGS/SRTMGL1_003') #30m resolution DEM SRTM void-filled 21 | ##print(image.getInfo()) 22 | #Project the image to Asia_Lambert_Conformal_Conic 23 | #wkt = 'PROJCS["Asia_Lambert_Conformal_Conic", \ 24 | # GEOGCS["GCS_WGS_1984", \ 25 | # DATUM["WGS_1984", \ 26 | # SPHEROID["WGS_1984",6378137,298.257223563]], \ 27 | # PRIMEM["Greenwich",0], \ 28 | # UNIT["Degree",0.017453292519943295]], \ 29 | # PROJECTION["Lambert_Conformal_Conic_2SP"], \ 30 | # PARAMETER["False_Easting",0], \ 31 | # PARAMETER["False_Northing",0],\ 32 | # PARAMETER["Central_Meridian",105],\ 33 | # PARAMETER["Standard_Parallel_1",30],\ 34 | # PARAMETER["Standard_Parallel_2",62],\ 35 | # PARAMETER["Latitude_Of_Origin",0],\ 36 | # UNIT["Meter",1], AUTHORITY["EPSG","102012"]]' 37 | 38 | #proj_Asia_Lambert = ee.Projection(wkt); 39 | #image_Asia_Lambert = image.reproject(crs=proj_Asia_Lambert,scale= 30); 40 | 41 | #elevation = image_Asia_Lambert.select('elevation') 42 | #slope = ee.Terrain.slope(elevation) 43 | #aspect = ee.Terrain.aspect(elevation) 44 | ################################################## 45 | 46 | # read slope data 47 | file_in_slope = '%s/Data_input_test/topo/Slope_China_30m_test.tif' % (fname) 48 | # file_out_slope= '%s/topo/Slope_China_30m_xxx.nc' % (fname) 49 | # !gdal_translate -of 'netCDF' file_in_slope file_out_slope 50 | ds = gdal.Open(file_in_slope) 51 | band = ds.GetRasterBand(1) 52 | slope = band.ReadAsArray() 53 | band=None 54 | ds=None 55 | # read aspect data 56 | file_in_aspect = '%s/Data_input_test/topo/Aspect_China_30m_test.tif' % (fname) 57 | ds = gdal.Open(file_in_aspect) 58 | band = ds.GetRasterBand(1) 59 | aspect = band.ReadAsArray() 60 | band=None 61 | ds=None 62 | # read contributing area data 63 | file_in_A_contr = '%s/Data_input_test/topo/A_contr_China_30m_test.tif' % (fname) 64 | ds = gdal.Open(file_in_A_contr) 65 | band = ds.GetRasterBand(1) 66 | A_contr = band.ReadAsArray() 67 | band=None 68 | ds=None 69 | 70 | return slope,aspect,A_contr 71 | 72 | def input_K(fname,llx,urx,lly,ury): 73 | ''' 74 | Read the soil input data for the calculation of the K factor at 1 km spatial resolution from GSDE (Shangguan et al., 2014) 75 | Extract data for test region 76 | ''' 77 | 78 | from scaling_func import select_region 79 | data = Dataset('%s/Data_input_test/soil/SAND1.nc' % (fname),'r') 80 | lats=data.variables['lat'][:] 81 | lons=data.variables['lon'][:] 82 | rows_sel,kols_sel=select_region(lats,lons,llx,urx,lly,ury) 83 | lat_c=lats[rows_sel[0]:rows_sel[-1]+1] 84 | lon_c=lons[kols_sel[0]:kols_sel[-1]+1] 85 | 86 | sand = data.variables['SAND'][3,rows_sel[0]:rows_sel[-1]+1,kols_sel[0]:kols_sel[-1]+1] #select topsoil layer for all, units % 87 | sand_t = sand.astype(float) 88 | sand_t[sand_t<0.] = np.nan 89 | sand_t = sand_t*0.01 # now fraction 90 | data.close() 91 | 92 | data = Dataset('%s/Data_input_test/soil/SILT1.nc' % (fname) ,'r') 93 | silt = data.variables['SILT'][3,rows_sel[0]:rows_sel[-1]+1,kols_sel[0]:kols_sel[-1]+1] 94 | silt_t = silt.astype(float) 95 | silt_t[silt_t<0.] = np.nan 96 | silt_t = silt_t*0.01 97 | data.close() 98 | 99 | data = Dataset('%s/Data_input_test/soil/CLAY1.nc' % (fname),'r') 100 | clay = data.variables['CLAY'][3,rows_sel[0]:rows_sel[-1]+1,kols_sel[0]:kols_sel[-1]+1] 101 | clay_t = clay.astype(float) 102 | clay_t[clay_t<0.] = np.nan 103 | clay_t = clay_t*0.01 104 | data.close() 105 | 106 | data = Dataset('%s/Data_input_test/soil/OC1.nc' % (fname),'r') 107 | oc = data.variables['OC'][3,rows_sel[0]:rows_sel[-1]+1,kols_sel[0]:kols_sel[-1]+1] 108 | oc_t = oc.astype(float) 109 | oc_t[oc_t<0.] = np.nan 110 | oc_t = oc_t*0.01 # to have % weight 111 | oc_t = oc_t*0.01 # to have fractions! 112 | data.close() 113 | 114 | #soil type classification: Taxo USDA 2014 from soil grids at 1km; 115 | file_in = '%s/Data_input_test/soil/TAXOUSDA_China_1km_test.tif' % (fname) 116 | ds = gdal.Open(file_in) 117 | band = ds.GetRasterBand(1) 118 | su_code90 = band.ReadAsArray() 119 | band=None 120 | ds=None 121 | 122 | return sand_t,silt_t,clay_t,oc_t,su_code90,lat_c,lon_c 123 | 124 | def input_grav(fname,llx,urx,lly,ury,lat_h,lon_h): 125 | ''' 126 | Read the gravel data at 1 km spatial resolution from GSDE (Shangguan et al., 2014) 127 | then upscale to 30m for Eros function in Adj_RUSLE.py 128 | Extract data for test region 129 | ''' 130 | 131 | from scaling_func import select_region,upscaling 132 | 133 | data = Dataset('%s/Data_input_test/soil/GRAV1.nc' % (fname),'r') 134 | lats=data.variables['lat'][:] 135 | lons=data.variables['lon'][:] 136 | rows_sel,kols_sel=select_region(lats,lons,llx,urx,lly,ury) 137 | lat_c=lats[rows_sel[0]:rows_sel[-1]+1] 138 | lon_c=lons[kols_sel[0]:kols_sel[-1]+1] 139 | 140 | gravel = data.variables['GRAV'][3,rows_sel[0]:rows_sel[-1]+1,kols_sel[0]:kols_sel[-1]+1] # units % 141 | gravel=gravel.astype(float) 142 | gravel[gravel<0.] = 0. 143 | gravel[np.isnan(gravel)==True] = 0. 144 | data.close() 145 | #rescale gravel to 30m 146 | r_sel,k_sel=upscaling(lat_h,lon_h,lat_c,lon_c) 147 | gravel_out1=gravel[r_sel[:]];gravel_out=gravel_out1[:,k_sel[:]] 148 | 149 | return gravel_out 150 | 151 | def input_lc(fname,llx,urx,lly,ury): 152 | ''' 153 | Derives landcover at 100m res on GEE platform 154 | Outputs landcover data test region as Gtiff images 155 | Landcover fractions are then rescaled to 30m for calculation of the C factor 156 | and as input for the Eros function in Adj_RUSLE.py 157 | 158 | ''' 159 | from scaling_func import upscaling 160 | 161 | ##################GEE part######################## 162 | #region=ee.Geometry.Rectangle([llx,lly,urx,ury]) 163 | 164 | ##landcover data 165 | #imagecol=ee.ImageCollection("COPERNICUS/Landcover/100m/Proba-V/Global").filterDate('2015-01-01') 166 | #imagereg=imagecol.clip(region) 167 | #crops = imagereg.select(['crops-coverfraction']) 168 | #tree= imagereg.select(['tree-coverfraction']) 169 | #grass=imagereg.select(['grass-coverfraction']) 170 | #baresoil=imagereg.select(['baresoil-coverfraction']) 171 | #urban=imagereg.select(['urban-coverfraction']) 172 | 173 | ################################################### 174 | 175 | # read ndvi data to derive high-res lat and lon (lat_h,lon_h) 176 | file_in = '%s/Data_input_test/ndvi/NDVI_China_30m_test_1_2012-2017.tif' % (fname) 177 | ds = gdal.Open(file_in) 178 | width = ds.RasterXSize 179 | height = ds.RasterYSize 180 | gt = ds.GetGeoTransform() 181 | minx = gt[0] 182 | miny = gt[3] + width*gt[4] + height*gt[5] 183 | maxx = gt[0] + width*gt[1] + height*gt[2] 184 | maxy = gt[3] 185 | lat_h=np.arange(miny,maxy,gt[5]*-1) 186 | lon_h=np.arange(minx,maxx,gt[1]) 187 | band=None 188 | ds=None 189 | # read landcover data 190 | file_in_tree = '%s/Data_input_test/landcover/Tree_China_100m_test_2015.tif' % (fname) 191 | ds = gdal.Open(file_in_tree) 192 | band = ds.GetRasterBand(1) 193 | treefrac_c = band.ReadAsArray()*0.01 194 | width = ds.RasterXSize 195 | height = ds.RasterYSize 196 | gt = ds.GetGeoTransform() 197 | minx = gt[0] 198 | miny = gt[3] + width*gt[4] + height*gt[5] 199 | maxx = gt[0] + width*gt[1] + height*gt[2] 200 | maxy = gt[3] 201 | lat_c=np.arange(miny,maxy,gt[5]*-1) 202 | lon_c=np.arange(minx,maxx,gt[1]) 203 | band=None 204 | ds=None 205 | #rescale tree to 30m 206 | r_sel,k_sel=upscaling(lat_h,lon_h,lat_c,lon_c) 207 | treefrac1=treefrac_c[r_sel[:]];treefrac=treefrac1[:,k_sel[:]] 208 | 209 | file_in_crop = '%s/Data_input_test/landcover/Crops_China_100m_test_2015.tif' % (fname) 210 | ds = gdal.Open(file_in_crop) 211 | band = ds.GetRasterBand(1) 212 | cropfrac_c = band.ReadAsArray()*0.01 213 | band=None 214 | ds=None 215 | #rescale crop to 30m 216 | cropfrac1=cropfrac_c[r_sel[:]];cropfrac=cropfrac1[:,k_sel[:]] 217 | 218 | file_in_grass = '%s/Data_input_test/landcover/Grass_China_100m_test_2015.tif' % (fname) 219 | ds = gdal.Open(file_in_grass) 220 | band = ds.GetRasterBand(1) 221 | grassfrac_c = band.ReadAsArray()*0.01 222 | band=None 223 | ds=None 224 | #rescale grass to 30m 225 | grassfrac1=grassfrac_c[r_sel[:]];grassfrac=grassfrac1[:,k_sel[:]] 226 | 227 | return treefrac,grassfrac,cropfrac 228 | 229 | def input_C(fname,year_start,year_end,llx,urx,lly,ury): 230 | ''' 231 | Derives ndvi (monthly) at 30m resolution on GEE platform 232 | Outputs ndvi test region as Gtiff images 233 | ''' 234 | from scaling_func import upscaling 235 | 236 | ##################GEE part######################## 237 | #region=ee.Geometry.Rectangle([llx,lly,urx,ury]) 238 | #months=np.array(['01','02','03','04','05','06','07','08','09','10','11','12']) 239 | #enddate=np.array(['31','28','31','30','31','30','31','31','30','31','30','31']) 240 | 241 | ##ndvi data 242 | #for m in range(len(months)): 243 | #imagecol = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA') 244 | #img = imagecol.filter(ee.Filter.calendarRange(%i,%i,'year') %(year_start,year_end))\ 245 | # .filter(ee.Filter.calendarRange(m+1,m+1,'month')) 246 | #imagereg=img.filterBounds(region) 247 | ##print('spatialFiltered', imagereg.getInfo()) 248 | #median = imagereg.median() 249 | #ndvi = median.normalizedDifference(['B5', 'B4']).rename('NDVI') 250 | ################################################### 251 | 252 | # read ndvi data 253 | file_in = '%s/Data_input_test/ndvi/NDVI_China_30m_test_1_2012-2017.tif' % (fname) 254 | ds = gdal.Open(file_in) 255 | width = ds.RasterXSize 256 | height = ds.RasterYSize 257 | gt = ds.GetGeoTransform() 258 | minx = gt[0] 259 | miny = gt[3] + width*gt[4] + height*gt[5] 260 | maxx = gt[0] + width*gt[1] + height*gt[2] 261 | maxy = gt[3] 262 | lat_h=np.arange(miny,maxy,gt[5]*-1) 263 | lon_h=np.arange(minx,maxx,gt[1]) 264 | band=None 265 | ds=None 266 | ndvi_all=[] 267 | for m in range(1,13): 268 | file_in = '%s/Data_input_test/ndvi/NDVI_China_30m_test_%i_2012-2017.tif' % (fname,m) 269 | ds = gdal.Open(file_in) 270 | band = ds.GetRasterBand(1) 271 | ndvi = band.ReadAsArray() 272 | band=None 273 | ds=None 274 | ndvi_all.append(ndvi) 275 | ndvi_all=np.asarray(ndvi_all).reshape(12,ndvi.shape[0],ndvi.shape[1]) 276 | 277 | return ndvi_all 278 | 279 | def input_R(fname,llx,urx,lly,ury): 280 | 281 | ''' 282 | Derives readily available climate and elevation data for calculation of erosivity 283 | Total yearly precipitation is derived from GSMaP on GEE 284 | SDII is calculated from the daily precip exported from GEE on local machine 285 | high-resolution (0.005deg) precip data is the interpolated to 10km with bilinear method using cdo's 286 | ''' 287 | 288 | from scaling_func import select_region 289 | 290 | ##################GEE part######################## 291 | #months=np.array(['01','02','03','04','05','06','07','08','09','10','11','12']) 292 | #for m in range(len(months)): 293 | # if m==0 or m==2 or m==4 or m==6 or m==7 or m==9 or m==11: 294 | # days=np.arange(1,32) 295 | # elif m==1: 296 | # days=np.arange(1,29) 297 | # else: 298 | # days=np.arange(1,31) 299 | # #loop over days 300 | # inidate = ee.Date.fromYMD(2012,m+1,1) 301 | # if m<11: 302 | # enddate = ee.Date.fromYMD(2012,m+2,1) 303 | # else: 304 | # enddate = ee.Date.fromYMD(2013,1,1) 305 | # #Import GSMaP data 306 | # gsmap = ee.ImageCollection('JAXA/GPM_L3/GSMaP/v6/reanalysis')\ 307 | # .filterDate(inidate, enddate).select('hourlyPrecipRateGC'); 308 | # gsmap_reg=gsmap.filterBounds(region) 309 | # for d in days: 310 | # if d<10: 311 | # day=ee.Date('2012-%s-0%i' % (months[m],d)) 312 | # print(day) 313 | # else: 314 | # day=ee.Date('2012-%s-%i' % (months[m],d)) 315 | # print(day) 316 | # day_collection = gsmap_reg.filterDate(day, day.advance(1 , 'day')) 317 | # sum_d = ee.Image(day_collection.sum()) 318 | # 319 | # task_config = { 'description' : 'ImagetoDriveM%sD%i' %(months[m],d), 'folder' : 'GEE', \ 320 | # 'fileNamePrefix' : 'PrM%sD%i' %(months[m],d)\ 321 | # , 'dimensions' : 400, 'region' : geometry } 322 | # 323 | # task = ee.batch.Export.image.toDrive(sum_d, **task_config) 324 | # 325 | # task.start() 326 | ################################################## 327 | 328 | #input readily calculated coarse res precip data rescaled to 10km resolution 329 | data = Dataset('%s/Data_input_test/erosivity/climate_classification_5m.nc' % (fname),'r') #koppen-geiger climate classification 330 | lats=data.variables['lat'][:] 331 | lons=data.variables['lon'][:] 332 | rows_sel,kols_sel=select_region(lats,lons,llx,urx,lly,ury) 333 | lat_c=lats[rows_sel[0]:rows_sel[-1]+1] 334 | lon_c=lons[kols_sel[0]:kols_sel[-1]+1] 335 | k = data.variables['climate'][rows_sel[0]:rows_sel[-1]+1,kols_sel[0]:kols_sel[-1]+1] 336 | k[k<0.]=np.nan 337 | k = np.ravel(k) 338 | data.close() 339 | data = Dataset('%s/Data_input_test/erosivity/P_total_2012_5m.nc' % (fname),'r') #total yearly 50 year mean precipitation data 340 | precip = data.variables['P'][:] 341 | precip[precip<=0.]=np.nan 342 | precip = np.ravel(precip) 343 | data.close() 344 | # SDII (simple precipitation index data) is the sum of precip on wet days (precip>1mm) divided by the number of wet days in a period 345 | data = Dataset('%s/Data_input_test/erosivity/SDII_2012_5m.nc' % (fname),'r') 346 | sdii = data.variables['SDII'][:] 347 | sdii[sdii<0.]=np.nan 348 | sdii = np.ravel(sdii) 349 | data.close() 350 | data = Dataset('%s/Data_input_test/erosivity/etopo5_5m.nc' % (fname),'r') #ETOPO 5 arcmin elevation data 351 | elev = data.variables['elev'][rows_sel[0]:rows_sel[-1]+1,kols_sel[0]:kols_sel[-1]+1] 352 | elev[elev<0.]=-9999. 353 | elev = np.ravel(elev) 354 | data.close() 355 | # remove too small elevation and precip values and zeros 356 | z = [] 357 | for i in range(len(elev)): 358 | if 0.<=elev[i]<0.001: 359 | z.append(0.001) 360 | elif elev[i]<0.: 361 | z.append(np.nan) 362 | else: 363 | z.append(elev[i]) 364 | z = np.asarray(z) 365 | p = [] 366 | for i in range(len(precip)): 367 | if 0.100.]=0. 102 | band=None 103 | ds=None 104 | file_in_R = '%s/Data_output_test/R_factor_China_30m.tif' % (fname) 105 | ds = gdal.Open(file_in_R) 106 | band = ds.GetRasterBand(1) 107 | R_out = band.ReadAsArray() 108 | R_out[R_out>1000000.]=0. 109 | band=None 110 | ds=None 111 | file_in_K = '%s/Data_output_test/K_factor_China_30m.tif' % (fname) 112 | ds = gdal.Open(file_in_K) 113 | band = ds.GetRasterBand(1) 114 | K_out = band.ReadAsArray() 115 | K_out[K_out>100.]=0. 116 | band=None 117 | ds=None 118 | file_in_tree = '%s/Data_output_test/treefrac_China_30m.tif' % (fname) 119 | ds = gdal.Open(file_in_tree) 120 | band = ds.GetRasterBand(1) 121 | treefrac = band.ReadAsArray() 122 | treefrac[treefrac>100.]=0. 123 | band=None 124 | ds=None 125 | file_in_crop = '%s/Data_output_test/cropfrac_China_30m.tif' % (fname) 126 | ds = gdal.Open(file_in_crop) 127 | band = ds.GetRasterBand(1) 128 | cropfrac = band.ReadAsArray() 129 | cropfrac[cropfrac>100.]=0. 130 | band=None 131 | ds=None 132 | file_in_grass = '%s/Data_output_test/grassfrac_China_30m.tif' % (fname) 133 | ds = gdal.Open(file_in_grass) 134 | band = ds.GetRasterBand(1) 135 | grassfrac = band.ReadAsArray() 136 | grassfrac[grassfrac>100.]=0. 137 | band=None 138 | ds=None 139 | file_in_grav = '%s/Data_output_test/gravel_China_30m.tif' % (fname) 140 | ds = gdal.Open(file_in_grav) 141 | band = ds.GetRasterBand(1) 142 | gravel = band.ReadAsArray() 143 | gravel[gravel>100.]=0. 144 | band=None 145 | ds=None 146 | #This function calculates the annual average soil erosion rate in t/ha/yr at 30m spatial resolution 147 | E=LS_out*K_out[:,:-1]*R_out[:,:-1]*C_out[:,:-1]*P_out 148 | E_CG=1.*E;E_rest=1.*E 149 | E_CG[(cropfrac[:,:-1]+grassfrac[:,:-1])<1.]=0. 150 | E_CG[gravel[:,:-1]>12.]=E_CG[gravel[:,:-1]>12.]-(0.8*E_CG[gravel[:,:-1]>12.]) 151 | E_rest[(cropfrac[:,:-1]+grassfrac[:,:-1])>=1.]=0. 152 | E_rest[gravel[:,:-1]>30.]=E_rest[gravel[:,:-1]>30.]-(0.3*E_rest[gravel[:,:-1]>30.]) 153 | E_out=E_CG+E_rest #yearly average soil erosion rates in tonne/ha/year 154 | E_out[E_out>100.]=100. #limit soil erosion to 100t/ha/y 155 | #output erosion 156 | output = Dataset('%s/Data_output_test/Erosion_China_30m.nc' % (fname),'w') 157 | output.createDimension('lat',E_out.shape[0]) 158 | output.createDimension('lon',E_out.shape[1]) 159 | output.createVariable('E','d',('lat','lon',)) 160 | output.variables['E'][:] = E_out 161 | output.close() 162 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RUSLE_GEE 2 | The RUSLE model on Google Earth Engine 3 | 4 | This model is entirely written in Python code that needs to be transformed to make the model run on GEE with the help of Python API for GEE. The model consists out of 3 scripts: 5 | Input_File.py : derives input data from GEE and exports them to tif files which are then read and output is transformed to arrays. The needed processing is done (reprojection, selection of test region etc.) 6 | Adj_RUSLE.py: This is the main model, where the RUSLE factors are calculated based on different methods from literature 7 | Output_File.py: The run script 8 | 9 | In addition the erosion model has 2 functions that need to be imported to make the model run: 10 | erosivity.so and erosivity.f90 11 | scaling_func.py 12 | -------------------------------------------------------------------------------- /The RUSLE model on Google Earth Engine.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wieka29/RUSLE_GEE/93b02dd3da4c103b538da63820098ad9b1f5e79e/The RUSLE model on Google Earth Engine.pdf -------------------------------------------------------------------------------- /erosivity.f90: -------------------------------------------------------------------------------- 1 | module erosivity 2 | implicit none 3 | 4 | contains 5 | 6 | subroutine main(z,p,k,sdii,rows,kols,r_out) !input should be p[t],sdii[t],k[t]; -9999 for r_out should be defined as nan in python after 7 | !use ieee_arithmetic 8 | 9 | implicit none 10 | 11 | real(8),dimension(kols*rows),intent(in)::sdii,p,z 12 | integer,intent(in):: kols, rows 13 | integer:: i 14 | integer,dimension(kols*rows),intent(in)::k 15 | real(8),dimension(kols*rows),intent(out):: r_out 16 | 17 | do i=1,size(z) 18 | if (p(i)==-9999.) then 19 | r_out(i)=-9999. 20 | else 21 | if (k(i)==5) then!BWk 22 | if (sdii(i)==-9999.) then 23 | if (p(i)<=850.) then 24 | r_out(i)=0.0483*p(i)**1.61 25 | else 26 | r_out(i)=587.8-1.219*p(i)+0.004105*p(i)**2 27 | endif 28 | else 29 | r_out(i)=0.809*p(i)**0.957 + 0.000189*sdii(i)**6.285 30 | endif 31 | elseif (k(i)==6) then!BSh 32 | if (sdii(i)==-9999.) then 33 | r_out(i)=exp(-8.164+2.455*log(p(i))) 34 | else 35 | r_out(i)=exp(-7.72+1.595*log(p(i))+2.068*log(sdii(i))) 36 | endif 37 | elseif (k(i)==7) then!BSk 38 | if (z(i)>=200.) then 39 | if (sdii(i)==-9999.) then 40 | r_out(i)=exp(5.52+1.33*log(p(i))-(0.977*log(z(i)))) 41 | else 42 | r_out(i)=exp(0.0793+0.887*log(p(i)) + 1.892*log(sdii(i))-(0.429*log(z(i)))) 43 | endif 44 | else 45 | if (sdii(i)==-9999.) then 46 | r_out(i)=exp(5.52+1.33*log(p(i))-(0.977*log(200.))) 47 | else 48 | r_out(i)=exp(0.0793+0.887*log(p(i)) + 1.892*log(sdii(i))-(0.429*log(200.))) 49 | endif 50 | endif 51 | elseif (k(i)==9) then!Csb 52 | r_out(i)=98.35+0.0003549*p(i)**1.987 53 | elseif (k(i) == 14) then!Cfa 54 | if (z(i) >= 4.5) then 55 | if (sdii(i)==-9999.) then 56 | r_out(i)=exp(3.378+0.852*log(p(i))-(0.191*log(z(i)))) 57 | else 58 | r_out(i)=exp(0.524+0.462*log(p(i))-(0.106*log(z(i)))+1.97*log(sdii(i))) 59 | endif 60 | else 61 | if (sdii(i)==-9999.) then 62 | r_out(i)=exp(3.378+0.852*log(p(i))-(0.191*log(4.5))) 63 | else 64 | r_out(i)=exp(0.524+0.462*log(p(i))-(0.106*log(4.5))+1.97*log(sdii(i))) 65 | endif 66 | endif 67 | elseif (k(i)==15) then!Cfb 68 | if (sdii(i)==-9999.) then 69 | if (z(i) >= 75.) then 70 | r_out(i)=exp(5.267+0.839*log(p(i))-(0.635*log(z(i)))) 71 | else 72 | r_out(i)=exp(5.267+0.839*log(p(i))-(0.635*log(75.))) 73 | endif 74 | else 75 | r_out(i)=exp(-4.853+0.676*log(p(i))+3.34*log(sdii(i))) 76 | endif 77 | elseif (k(i) == 17) then!Dsa 78 | if (sdii(i)==-9999.) then 79 | r_out(i)=exp(7.49+-0.0512*log(p(i))-(0.272*log(z(i)))) 80 | else 81 | r_out(i)=exp(8.602+-0.963*log(sdii(i))-(0.247*log(z(i)))) 82 | endif 83 | elseif (k(i) == 18) then!Dsb 84 | r_out(i)=exp(2.166+0.494*log(p(i))) 85 | elseif (k(i) == 19) then!Dsc 86 | if (sdii(i)==-9999.) then 87 | r_out(i)=exp(4.416+0.0594*log(p(i))) 88 | else 89 | r_out(i)=exp(6.236-(0.869*log(sdii(i)))) 90 | endif 91 | elseif (k(i) == 21) then!Dwa 92 | r_out(i)=exp(-0.572+1.238*log(p(i))) 93 | elseif (k(i) == 22) then!Dwb 94 | if (sdii(i)==-9999.) then 95 | r_out(i)=exp(1.882+0.819*log(p(i))) 96 | else 97 | r_out(i)=exp(-1.7+0.788*log(p(i))+1.824*log(sdii(i))) 98 | endif 99 | elseif (k(i) == 25) then!Dfa 100 | if (sdii(i)==-9999.) then 101 | r_out(i)=exp(-2.396+1.5*log(p(i))) 102 | else 103 | r_out(i)=exp(-1.99+0.737*log(p(i))+2.033*log(sdii(i))) 104 | endif 105 | elseif (k(i) == 26) then!Dfb 106 | if (z(i) >= 65.) then 107 | if (sdii(i)==-9999.) then 108 | r_out(i)=exp(1.96+1.084*log(p(i))-(0.34*log(z(i)))) 109 | else 110 | r_out(i)=exp(-0.5+0.266*log(p(i))-(0.131*log(z(i)))+3.1*log(sdii(i))) 111 | endif 112 | else 113 | if (sdii(i)==-9999.) then 114 | r_out(i)=exp(1.96+1.084*log(p(i))-(0.34*log(65.))) 115 | else 116 | r_out(i)=exp(-0.5+0.266*log(p(i))-(0.131*log(65.))+3.1*log(sdii(i))) 117 | endif 118 | endif 119 | elseif (k(i) == 27) then!Dfc 120 | if (sdii(i)==-9999.) then 121 | r_out(i)=exp(-3.263+1.576*log(p(i))) 122 | else 123 | r_out(i)=exp(-1.259+3.862*log(sdii(i))) 124 | endif 125 | elseif (k(i) == 29) then!ET 126 | r_out(i)=exp(-3.945+1.54*log(p(i))) 127 | elseif (k(i) == 30) then!EF 128 | r_out(i)=exp(16.39-1.286*log(p(i))) 129 | elseif (k(i) == 31) then!ETH 130 | if (sdii(i)==-9999.) then 131 | r_out(i)=exp(-10.66+2.43*log(p(i))) 132 | else 133 | r_out(i)=exp(21.44+1.293*log(p(i))-(10.579*log(sdii(i)))) 134 | endif 135 | elseif (k(i) == 32) then!EFH 136 | r_out(i)=exp(16.39-1.286*log(p(i))) 137 | else !Af(1),Am(2),Aw(3),BWh(4),Csa(8),Csc(10),Cwa(11),Cwb(12),Cwc(13),Cfc(16),Dsd(20),Dwc(23),Dwd(24),Dfd(28) 138 | if (p(i) <= 850.) then 139 | r_out(i)=0.0483*p(i)**1.61 140 | else 141 | r_out(i)=587.8-1.219*p(i)+0.004105*p(i)**2 142 | endif 143 | endif 144 | endif 145 | enddo 146 | end subroutine main 147 | end module erosivity 148 | 149 | !f2py -c -m routing_sub routing_sub.f90 150 | -------------------------------------------------------------------------------- /erosivity.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wieka29/RUSLE_GEE/93b02dd3da4c103b538da63820098ad9b1f5e79e/erosivity.so -------------------------------------------------------------------------------- /gee.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | test GEE 4 | """ 5 | import ee 6 | import numpy as np 7 | 8 | ee.Initialize() 9 | 10 | image = ee.Image('USGS/SRTMGL1_003') #30m resolution DEM SRTM void-filled 11 | #print(image.getInfo()) 12 | #image.projection().nominalScale().getInfo() 13 | #Project the image to Asia_Lambert_Conformal_Conic 14 | wkt = 'PROJCS["Asia_Lambert_Conformal_Conic", \ 15 | GEOGCS["GCS_WGS_1984", \ 16 | DATUM["WGS_1984", \ 17 | SPHEROID["WGS_1984",6378137,298.257223563]], \ 18 | PRIMEM["Greenwich",0], \ 19 | UNIT["Degree",0.017453292519943295]], \ 20 | PROJECTION["Lambert_Conformal_Conic_2SP"], \ 21 | PARAMETER["False_Easting",0], \ 22 | PARAMETER["False_Northing",0],\ 23 | PARAMETER["Central_Meridian",105],\ 24 | PARAMETER["Standard_Parallel_1",30],\ 25 | PARAMETER["Standard_Parallel_2",62],\ 26 | PARAMETER["Latitude_Of_Origin",0],\ 27 | UNIT["Meter",1], AUTHORITY["EPSG","102012"]]' 28 | 29 | proj_Asia_Lambert = ee.Projection(wkt); 30 | image_Asia_Lambert = image.reproject(crs=proj_Asia_Lambert,scale= 30); 31 | 32 | elevation = image_Asia_Lambert.select('elevation') 33 | slope = ee.Terrain.slope(elevation) 34 | aspect = ee.Terrain.aspect(elevation) 35 | 36 | 37 | ''' 38 | Test some calculations 39 | 40 | xy = ee.Geometry.Point([86.9250, 27.9881]) 41 | elev = image.sample(xy, 30).first().get('elevation').getInfo() 42 | print('Mount Everest elevation (m):', elev) 43 | 44 | slope_sel=slope.sample(xy,30).first().getInfo() 45 | print('Mount Everest slope (degree):', slope_sel) 46 | 47 | region=ee.Geometry.Rectangle([86.76,34.69,88.5,35.52]) #minLon, minLat, maxLon, maxLat 48 | affine = [0.0002777777777777778, 0, -180.0001388888889, 0, -0.0002777777777777778, 60.00013888888889] 49 | 50 | meanElevation = elevation.reduceRegion(reducer=ee.Reducer.mean(),\ 51 | geometry=region,crs='EPSG:4326',\ 52 | crsTransform=affine,\ 53 | maxPixels=1e9) 54 | print('Mean elevation:', meanElevation.getInfo()) 55 | ''' 56 | 57 | # Export image/data 58 | llx = 106. 59 | lly = 36. 60 | urx = 108. 61 | ury = 38. 62 | geometry = ee.Geometry.Rectangle([llx,lly,urx,ury]) 63 | geometry = geometry['coordinates'][0] 64 | 65 | task_config_elev = { 'description' : 'imageToDriveDEM', 'folder' : 'GEE', \ 66 | 'fileNamePrefix' : 'DEMTest', 'scale' : 30, 'region' : geometry } 67 | 68 | task_elev = ee.batch.Export.image.toDrive(elevation, **task_config_elev) 69 | 70 | task_elev.start() 71 | 72 | task_config_slope = { 'description' : 'imageToDriveSlope', 'folder' : 'GEE', \ 73 | 'fileNamePrefix' : 'SlopeTest', 'scale' : 30, 'region' : geometry } 74 | 75 | task_slope = ee.batch.Export.image.toDrive(slope, **task_config_slope) 76 | 77 | task_slope.start() 78 | 79 | task_config_aspect = { 'description' : 'imageToDriveAspect', 'folder' : 'GEE', \ 80 | 'fileNamePrefix' : 'AspectTest', 'scale' : 30, 'region' : geometry } 81 | 82 | task_aspect = ee.batch.Export.image.toDrive(slope, **task_config_aspect) 83 | 84 | task_aspect.start() 85 | 86 | """ 87 | Test NDVI calculation 88 | """ 89 | region=ee.Geometry.Rectangle([llx,lly,urx,ury]) 90 | months=np.array(['01','02','03','04','05','06','07','08','09','10','11','12']) 91 | enddate=np.array(['31','28','31','30','31','30','31','31','30','31','30','31']) 92 | for m in range(len(months)): 93 | imagecol = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA') 94 | #filterDate('2017-%s-01' % (months[m]), '2017-%s-%s' % (months[m],enddate[m])) 95 | img = imagecol.filter(ee.Filter.calendarRange(2012,2017,'year'))\ 96 | .filter(ee.Filter.calendarRange(m+1,m+1,'month')) 97 | imagereg=img.filterBounds(region) 98 | #print('spatialFiltered', imagereg.getInfo()) 99 | median = imagereg.median() 100 | ndvi = median.normalizedDifference(['B5', 'B4']).rename('NDVI') 101 | #export ndvi image 102 | task_config_ndvi = { 'description' : 'imageToDrivendvi%s' % (months[m]), 'folder' : 'GEE', \ 103 | 'fileNamePrefix' : 'ndviTest%s' % (months[m]), 'scale' : 30, 'region' : geometry } 104 | 105 | task_ndvi = ee.batch.Export.image.toDrive(ndvi, **task_config_ndvi) 106 | 107 | task_ndvi.start() 108 | 109 | """ 110 | Derive landcover 111 | """ 112 | imagecol=ee.ImageCollection("COPERNICUS/Landcover/100m/Proba-V/Global").filterDate('2015-01-01').first() 113 | imagereg=imagecol.clip(region) 114 | crops = imagereg.select(['crops-coverfraction']) 115 | tree= imagereg.select(['tree-coverfraction']) 116 | grass=imagereg.select(['grass-coverfraction']) 117 | baresoil=imagereg.select(['bare-coverfraction']) 118 | urban=imagereg.select(['urban-coverfraction']) 119 | #discrete=imagereg.select(['discrete_classification']) 120 | #palette = ee.List(discrete.get('discrete_classification_class_palette')) 121 | 122 | 123 | 124 | task_config_tree = { 'description' : 'imageToDriveTree', 'folder' : 'GEE', \ 125 | 'fileNamePrefix' : 'TreeTest', 'scale' : 100, 'region' : geometry } 126 | 127 | task_tree = ee.batch.Export.image.toDrive(tree, **task_config_tree) 128 | 129 | task_tree.start() 130 | 131 | task_config_crops = { 'description' : 'imageToDriveCrops', 'folder' : 'GEE', \ 132 | 'fileNamePrefix' : 'CropsTest', 'scale' : 100, 'region' : geometry } 133 | 134 | task_crops = ee.batch.Export.image.toDrive(crops, **task_config_crops) 135 | 136 | task_crops.start() 137 | 138 | task_config_grass = { 'description' : 'imageToDriveGrass', 'folder' : 'GEE', \ 139 | 'fileNamePrefix' : 'GrassTest', 'scale' : 100, 'region' : geometry } 140 | 141 | task_grass = ee.batch.Export.image.toDrive(grass, **task_config_grass) 142 | 143 | task_grass.start() 144 | 145 | task_config_baresoil = { 'description' : 'imageToDrivebaresoil', 'folder' : 'GEE', \ 146 | 'fileNamePrefix' : 'baresoilTest', 'scale' : 100, 'region' : geometry } 147 | 148 | task_baresoil = ee.batch.Export.image.toDrive(baresoil, **task_config_baresoil) 149 | 150 | task_baresoil.start() 151 | 152 | task_config_urban = { 'description' : 'imageToDriveurban', 'folder' : 'GEE', \ 153 | 'fileNamePrefix' : 'urbanTest', 'scale' : 100, 'region' : geometry } 154 | 155 | task_urban = ee.batch.Export.image.toDrive(urban, **task_config_urban) 156 | 157 | task_urban.start() 158 | 159 | """ 160 | Derive precipitation 161 | """ 162 | llx = 106. 163 | lly = 36. 164 | urx = 108. 165 | ury = 38. 166 | geometry = ee.Geometry.Rectangle([llx,lly,urx,ury]) 167 | geometry = geometry['coordinates'][0] 168 | region=ee.Geometry.Rectangle([llx,lly,urx,ury]) 169 | 170 | ''' 171 | #test daily precipitation (working code) 172 | inidate = ee.Date.fromYMD(2012,12,1) 173 | enddate = ee.Date.fromYMD(2013,1,1) 174 | #Import GSMaP data 175 | gsmap = ee.ImageCollection('JAXA/GPM_L3/GSMaP/v6/reanalysis')\ 176 | .filterDate(inidate, enddate).select('hourlyPrecipRateGC'); 177 | gsmap_reg=gsmap.filterBounds(region) 178 | #lapse = ee.List.sequence(0, difdate.subtract(1)) 179 | #inidate = ee.Date('2014-01-01') 180 | #days = ee.List.sequence(1, 365) 181 | #Filter the collection in one day (24 images) 182 | day=ee.Date('2012-12-31') 183 | day_collection = gsmap_reg.filterDate(day, day.advance(1, 'day')) 184 | sum_d = ee.Image(day_collection.sum()) 185 | #month_collection = gsmap_reg.filterDate(inidate,enddate) 186 | #Get the sum of all 24 images into one Image 187 | #sum_m = ee.Image(month_collection.sum()) 188 | 189 | task_config = { 'description' : 'ImagetoDriveM12D31', 'folder' : 'GEE', \ 190 | 'fileNamePrefix' : 'PrM12D31', 'dimensions' : 400, 'region' : geometry } 191 | 192 | task = ee.batch.Export.image.toDrive(sum_d, **task_config) 193 | 194 | task.start() 195 | 196 | 197 | ''' 198 | 199 | ''' 200 | # The code below does not work 201 | 202 | #test 203 | inidate = ee.Date.fromYMD(2014,12,1) 204 | enddate = ee.Date.fromYMD(2014,12,31) 205 | 206 | #Difference between start and end in days 207 | difdate = enddate.difference(inidate, 'day') 208 | 209 | def function(day): 210 | return inidate.advance(day, 'day') 211 | listdates=lapse.map(function(day)) 212 | 213 | #Iterate over the list of dates 214 | def function2(day): 215 | #cast 216 | day = ee.Date(day) 217 | 218 | #Filter the collection in one day (24 images) 219 | day_collection = gsmap.filterDate(day, day.advance(1, 'day')) 220 | 221 | #Get the sum of all 24 images into one Image 222 | sum = ee.Image(day_collection.sum()) 223 | 224 | value = sum.reduceRegion(ee.Reducer.first(), geometry, 30).get('hourlyPrecipRateGC') 225 | 226 | return value 227 | ''' 228 | 229 | months=np.array(['01','02','03','04','05','06','07','08','09','10','11','12']) 230 | for m in range(len(months)): 231 | if m==0 or m==2 or m==4 or m==6 or m==7 or m==9 or m==11: 232 | days=np.arange(1,32) 233 | elif m==1: 234 | days=np.arange(1,29) 235 | else: 236 | days=np.arange(1,30) 237 | #loop over days 238 | inidate = ee.Date.fromYMD(2012,m+1,1) 239 | if m<11: 240 | enddate = ee.Date.fromYMD(2012,m+2,1) 241 | else: 242 | enddate = ee.Date.fromYMD(2013,1,1) 243 | #Import GSMaP data 244 | gsmap = ee.ImageCollection('JAXA/GPM_L3/GSMaP/v6/reanalysis')\ 245 | .filterDate(inidate, enddate).select('hourlyPrecipRateGC'); 246 | gsmap_reg=gsmap.filterBounds(region) 247 | for d in days: 248 | if d<10: 249 | day=ee.Date('2012-%s-0%i' % (months[m],d)) 250 | print(day) 251 | else: 252 | day=ee.Date('2012-%s-%i' % (months[m],d)) 253 | print(day) 254 | day_collection = gsmap_reg.filterDate(day, day.advance(1 , 'day')) 255 | sum_d = ee.Image(day_collection.sum()) 256 | 257 | task_config = { 'description' : 'ImagetoDriveM%sD%i' %(months[m],d), 'folder' : 'GEE', \ 258 | 'fileNamePrefix' : 'PrM%sD%i' %(months[m],d)\ 259 | , 'dimensions' : 400, 'region' : geometry } 260 | 261 | task = ee.batch.Export.image.toDrive(sum_d, **task_config) 262 | 263 | task.start() 264 | 265 | """ 266 | Export terracing map 267 | """ 268 | llx = 106. 269 | lly = 36. 270 | urx = 108. 271 | ury = 38. 272 | geometry = ee.Geometry.Rectangle([llx,lly,urx,ury]) 273 | geometry = geometry['coordinates'][0] 274 | region=ee.Geometry.Rectangle([llx,lly,urx,ury]) 275 | 276 | ChinaTerrace = ee.Image("users/cbw/TerraceResults/China_terrace_1"); 277 | 278 | wkt = 'PROJCS["Asia_Lambert_Conformal_Conic", \ 279 | GEOGCS["GCS_WGS_1984", \ 280 | DATUM["WGS_1984", \ 281 | SPHEROID["WGS_1984",6378137,298.257223563]], \ 282 | PRIMEM["Greenwich",0], \ 283 | UNIT["Degree",0.017453292519943295]], \ 284 | PROJECTION["Lambert_Conformal_Conic_2SP"], \ 285 | PARAMETER["False_Easting",0], \ 286 | PARAMETER["False_Northing",0],\ 287 | PARAMETER["Central_Meridian",105],\ 288 | PARAMETER["Standard_Parallel_1",30],\ 289 | PARAMETER["Standard_Parallel_2",62],\ 290 | PARAMETER["Latitude_Of_Origin",0],\ 291 | UNIT["Meter",1], AUTHORITY["EPSG","102012"]]' 292 | 293 | proj_Asia_Lambert = ee.Projection(wkt); 294 | image_Asia_Lambert = ChinaTerrace.reproject(crs=proj_Asia_Lambert,scale= 30); 295 | 296 | imagereg=image_Asia_Lambert.clip(region) 297 | 298 | task_config = { 'description' : 'imageToDriveTer', 'folder' : 'GEE', \ 299 | 'fileNamePrefix' : 'Chinaterrace', 'scale' : 30, 'region' : geometry } 300 | 301 | task_terrace = ee.batch.Export.image.toDrive(imagereg, **task_config) 302 | 303 | task_elev.start() -------------------------------------------------------------------------------- /gsmap_precip_proc.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | """ 4 | Created on Thu Mar 12 16:17:52 2020 5 | 6 | @author: wieka20 7 | """ 8 | import gdal 9 | import numpy as np 10 | from netCDF4 import Dataset 11 | 12 | year=2012 13 | months=np.array(['01','02','03','04','05','06','07','08','09','10','11','12']) 14 | fname='/home/wieka20/Dokumente/Adj_RUSLE/Data_input_test/erosivity' 15 | # derive rows kols of precip file 16 | file_in= '%s/PrM01D1.tif' % (fname) 17 | ds = gdal.Open(file_in) 18 | band = ds.GetRasterBand(1) 19 | P = band.ReadAsArray() 20 | rows=P.shape[0] 21 | kols=P.shape[1] 22 | band=None 23 | ds=None 24 | 25 | P_monthly=[] 26 | wetdays=np.zeros((rows,kols)) 27 | P_wetdays=np.zeros((rows,kols)) 28 | for m in range(len(months)): 29 | if m==0 or m==2 or m==4 or m==6 or m==7 or m==9 or m==11: 30 | dd=31 31 | elif m==1: 32 | dd=28 33 | else: 34 | dd=30 35 | days=np.arange(1,dd+1) 36 | P_daily=[] 37 | for d in days: 38 | # read daily precip data from gsmap 39 | file_in= '%s/PrM%sD%i.tif' % (fname,months[m],d) 40 | ds = gdal.Open(file_in) 41 | band = ds.GetRasterBand(1) 42 | P = band.ReadAsArray() 43 | wetdays[P>=1.]+=1 44 | P_wetdays[P>=1.]+=P[P>=1.] 45 | band=None 46 | ds=None 47 | P_daily.append(P) 48 | P_daily=np.asarray(P_daily).reshape(dd,rows,kols) 49 | P_monthly.append(np.nansum(P_daily,axis=0)) #monthly sum 50 | P_monthly=np.asarray(P_monthly).reshape(12,rows,kols) 51 | P_yearly=np.nansum(P_monthly,axis=0) #yearly sum 52 | SDII=P_wetdays/wetdays 53 | SDII[np.isfinite(SDII)==False]=0. 54 | SDII[np.isnan(SDII)==True]=0. 55 | #output 56 | output = Dataset('%s/P_total_%i.nc' % (fname,year),'w') 57 | output.createDimension('lat',rows) 58 | output.createDimension('lon',kols) 59 | output.createVariable('P','d',('lat','lon',)) 60 | output.variables['P'][:] = P_yearly 61 | output.close() 62 | 63 | output = Dataset('%s/SDII_%i.nc' % (fname,year),'w') 64 | output.createDimension('lat',rows) 65 | output.createDimension('lon',kols) 66 | output.createVariable('SDII','d',('lat','lon',)) 67 | output.variables['SDII'][:] = SDII 68 | output.close() -------------------------------------------------------------------------------- /scaling_func.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python2 2 | # -*- coding: utf-8 -*- 3 | """ 4 | Created on Tue Mar 3 15:14:48 2020 5 | 6 | @author: wieka20 7 | """ 8 | import numpy as np 9 | from netCDF4 import Dataset 10 | 11 | def upscaling(lat_h,lon_h,lat_c,lon_c): 12 | r_sel=[] 13 | for i in range(len(lat_h)): 14 | dlat_min=1000. 15 | row_min=0 16 | for j in range(len(lat_c)): 17 | dlat=abs(lat_h[i]-lat_c[j]) 18 | if dlat=lly and lats[i]<=ury: 41 | rows_sel.append(i) 42 | kols_sel=[] 43 | for j in range(len(lons)): 44 | if lons[j]>=llx and lons[j]<=urx: 45 | kols_sel.append(j) 46 | rows_sel=np.asarray(rows_sel) 47 | kols_sel=np.asarray(kols_sel) 48 | return rows_sel,kols_sel 49 | -------------------------------------------------------------------------------- /sourcegrid.txt: -------------------------------------------------------------------------------- 1 | gridtype = lonlat 2 | xsize = 7423 3 | ysize = 7422 4 | xname = lon 5 | xlongname = "longitude" 6 | xunits = "degrees_east" 7 | yname = lat 8 | ylongname = "latitude" 9 | yunits = "degrees_north" 10 | xfirst = 105.9999117143227636 11 | xinc = 0.000269494585 12 | yfirst = 38.0002179177449904 13 | yinc = -0.000269494585 14 | 15 | --------------------------------------------------------------------------------