def processSoilData(AggLevel): '''Calculates average soil characteristics at AggLevel = "Woreda" or "Kebele" and outputs them to WoredaSoilData.csv or KebeleSoilData.csv''' #set the working directory workingDir = os.getcwd() #Download all of the soil data downloadSoilData(workingDir) #Turn on Spatial Statistics package and define field over which ZonalStatisticsAsTable will be calculated (Woreda or Kebele ID) ap.CheckOutExtension("Spatial") if AggLevel == 'Kebele': in_zone_data = os.path.dirname(workingDir) + "\\Shapefiles\\Ethiopia Kebeles without Somali region.shp" in_template_dataset = in_zone_data zone_field = "KebeleID" elif AggLevel == 'Woreda': in_zone_data = os.path.dirname(workingDir) + "\\Shapefiles\\WoredasWGS1984.shp" in_template_dataset = in_zone_data zone_field = "WOREDANO_" #Define the projection and change the working directory to the directory with all of the soil data folders latLongRef = "Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj" os.chdir(workingDir) directories = [f for f in os.listdir(os.getcwd()) if os.path.isfile(f) == False] for i in range(len(directories)): #Find all the tiffs with soil data in each soil characteristic folder os.chdir(workingDir + "\\" + directories[i]) filelist = os.listdir(os.getcwd()) tiffs = [] clipTiffs = [] for j in range(len(filelist)): name = filelist[j] if name[-8::] == '250m.tif': tiffs.append(name) elif name[-9::] == '_Clip.tif': clipTiffs.append(name) for j in range(len(clipTiffs)): clipTiffs[j] = os.getcwd() + "\\" + clipTiffs[j] for j in range(len(tiffs)): in_raster = os.getcwd() + "\\" + tiffs[j] out_raster = os.getcwd() + "\\" + tiffs[j][0:-4] + "_Clip.tif" #Clip the tiffs to Ethiopia if they haven't been already if out_raster not in clipTiffs: ap.Clip_management(in_raster, "#", out_raster, in_template_dataset, "#", 1) #Calculate Zonal Statistics of soil data at AggLevel in_value_raster = out_raster out_table = os.getcwd() + "\\" + tiffs[j][0:-4] + AggLevel + ".dbf" ap.sa.ZonalStatisticsAsTable(in_zone_data, zone_field, in_value_raster, out_table) #Convert the DBFs with all the AggLevel soil data to CSVs #Change the working directory to the directory with all the soil data folders os.chdir(workingDir) directories = [f for f in os.listdir(os.getcwd()) if os.path.isfile(f) == False] for i in range(len(directories)): #Find all the DBFs with soil data in each soil characteristic folder os.chdir(workingDir + "\\" + directories[i]) filelist = os.listdir(os.getcwd()) DBFs = [] for j in range(len(filelist)): name = filelist[j] if name[-10::] == AggLevel + '.dbf': DBFs.append(name) #Convert the DBF to a CSV for j in range(len(DBFs)): convertDBFtoCSV(os.getcwd() + "\\" + DBFs[j]) #Join the CSVs with all the AggLevel soil data into one CSV titled "WoredaSoilData.csv" or "KebeleSoilData.csv" depending on the AggLevel joinSoilCSVs(AggLevel, workingDir) return None
def intersectGrid(AggLevel, workingDir, variable): '''Intersects the NOAA precipitation data grid with the AggLevel = "Woreda" or "Kebele" shapefile''' #create grid shapefile Grid = workingDir + "\\All" + variable + "Grid.shp" if (os.path.exists(Grid) == False): if variable == "Temp": origin_coord = "-180 -90" nrows = "360" ncols = "720" polygon_width = "0.5 degrees" else: origin_coord = "-20.05 -40.05" nrows = "801" ncols = "751" polygon_width = "0.1 degrees" polygon_height = polygon_width ap.GridIndexFeatures_cartography(Grid, "", "", "", "", polygon_width, polygon_height, origin_coord, nrows, ncols) ap.DefineProjection_management( Grid, coor_system="GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',\ SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]" ) #add 3 or 4 fields to grid shapefile: latitude (LAT), longitude (LONG) and #for precipitation, row (row) of text file corresponding to each grid in the shapefile; #for temperature, row (row) and column (col) of netCDF file corresponding to each grid in the shapefile ap.AddField_management(Grid, "LAT", "DOUBLE", 7, 2, "", "", "", "", "") ap.AddField_management(Grid, "LONG", "DOUBLE", 7, 2, "", "", "", "", "") ap.AddField_management(Grid, "row", "SHORT", 6, "", "", "", "", "", "") if variable == "Temp": ap.AddField_management(Grid, "col", "SHORT", 5, "", "", "", "", "", "") #calculate lat and long fields expression1 = "float(!SHAPE.CENTROID!.split()[0])" expression2 = "float(!SHAPE.CENTROID!.split()[1])" ap.CalculateField_management(Grid, "LONG", expression1, "PYTHON") ap.CalculateField_management(Grid, "LAT", expression2, "PYTHON") #calculate row and col fields if variable == "Temp": Grid = calcTempFields(Grid) else: Grid = calcRainFields(Grid) #clip the grid to Ethiopia and convert its .dbf to a .csv for later use GridClip = workingDir + "\\" + variable + "GridClip" + AggLevel + ".shp" if AggLevel == 'Woreda': EthiopiaBorders = os.path.dirname( workingDir) + "\\Shapefiles\\WoredasAdindan.shp" elif AggLevel == 'Kebele': EthiopiaBorders = os.path.dirname( workingDir ) + "\\Shapefiles\\Ethiopia Kebeles without Somali region.shp" ap.Clip_analysis(Grid, EthiopiaBorders, GridClip) dbf = GridClip[0:-4] + ".dbf" GridCSV = convertDBFtoCSV(dbf) #intersect the clipped grid with the woreda or kebele shapefile and project to Adindan GridIntersect = workingDir + "\\" + variable + AggLevel + "Intersect.shp" ap.Intersect_analysis([GridClip, EthiopiaBorders], GridIntersect) GridIntersectProject = GridIntersect[0:-4] + "Project.shp" ap.Project_management( GridIntersect, GridIntersectProject, out_coor_system="PROJCS['Adindan_UTM_Zone_37N',GEOGCS['GCS_Adindan',\ DATUM['D_Adindan',SPHEROID['Clarke_1880_RGS',6378249.145,293.465]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],\ PROJECTION['Transverse_Mercator'],PARAMETER['False_Easting',500000.0],PARAMETER['False_Northing',0.0],\ PARAMETER['Central_Meridian',39.0],PARAMETER['Scale_Factor',0.9996],PARAMETER['Latitude_Of_Origin',0.0],\ UNIT['Meter',1.0]]", transform_method="Adindan_To_WGS_1984_1", in_coor_system="GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',\ SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]" ) #calculate area of intersection between grid and woreda or kebele shapefile after adding a field to store it ap.AddField_management(GridIntersectProject, "PartArea", "DOUBLE", 12, 6, "", "", "", "", "") expression = "float(!SHAPE.AREA@SQUAREKILOMETERS!)" ap.CalculateField_management(GridIntersectProject, "PartArea", expression, "PYTHON") #convert GridIntersect's .dbf to a .csv for later use dbf = GridIntersectProject[0:-4] + ".dbf" intersectCSV = convertDBFtoCSV(dbf) return intersectCSV, GridCSV
def intersectGrid(AggLevel, workingDir, variable): '''Intersects the GHCN temperature data grid with the AggLevel = "Woreda" or "Kebele" shapefile''' #create grid shapefile Grid = workingDir + "\\All" + variable + "Grid.shp" if(os.path.exists(Grid)==False): if variable == "Temp": origin_coord = "-180 -90" nrows = "360" ncols = "720" polygon_width = "0.5 degrees" else: origin_coord = "-20.05 -40.05" nrows = "801" ncols = "751" polygon_width = "0.1 degrees" polygon_height = polygon_width ap.GridIndexFeatures_cartography(Grid, "", "", "", "", polygon_width, polygon_height, origin_coord, nrows, ncols) ap.DefineProjection_management(Grid,coor_system="GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',\ SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]") #add 3 or 4 fields to grid shapefile: latitude (LAT), longitude (LONG) and #for precipitation, row (row) of text file corresponding to each grid in the shapefile; #for temperature, row (row) and column (col) of netCDF file corresponding to each grid in the shapefile ap.AddField_management(Grid, "LAT", "DOUBLE", 7, 2, "", "", "", "", "") ap.AddField_management(Grid, "LONG", "DOUBLE", 7, 2, "", "", "", "", "") ap.AddField_management(Grid, "row", "SHORT", 6, "", "", "", "", "", "") if variable == "Temp": ap.AddField_management(Grid, "col", "SHORT", 5, "", "", "", "", "", "") #calculate lat and long fields expression1 = "float(!SHAPE.CENTROID!.split()[0])" expression2 = "float(!SHAPE.CENTROID!.split()[1])" ap.CalculateField_management(Grid, "LONG", expression1, "PYTHON") ap.CalculateField_management(Grid, "LAT", expression2, "PYTHON") #calculate row and col fields if variable == "Temp": Grid = calcTempFields(Grid) else: Grid = calcRainFields(Grid) #clip the grid to Ethiopia and convert its .dbf to a .csv for later use GridClip = workingDir + "\\" + variable + "GridClip" + AggLevel + ".shp" if AggLevel == 'Woreda': EthiopiaBorders = os.path.dirname(workingDir) + "\\Shapefiles\\WoredasAdindan.shp" elif AggLevel == 'Kebele': EthiopiaBorders = os.path.dirname(workingDir) + "\\Shapefiles\\Ethiopia Kebeles without Somali region.shp" ap.Clip_analysis(Grid, EthiopiaBorders, GridClip) dbf = GridClip[0:-4] + ".dbf" GridCSV = convertDBFtoCSV(dbf) #intersect the clipped grid with the woreda or kebele shapefile and project to Adindan GridIntersect = workingDir + "\\" + variable + AggLevel + "Intersect.shp" ap.Intersect_analysis([GridClip, EthiopiaBorders],GridIntersect) GridIntersectProject = GridIntersect[0:-4] + "Project.shp" ap.Project_management(GridIntersect,GridIntersectProject,out_coor_system="PROJCS['Adindan_UTM_Zone_37N',GEOGCS['GCS_Adindan',\ DATUM['D_Adindan',SPHEROID['Clarke_1880_RGS',6378249.145,293.465]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],\ PROJECTION['Transverse_Mercator'],PARAMETER['False_Easting',500000.0],PARAMETER['False_Northing',0.0],\ PARAMETER['Central_Meridian',39.0],PARAMETER['Scale_Factor',0.9996],PARAMETER['Latitude_Of_Origin',0.0],\ UNIT['Meter',1.0]]",transform_method="Adindan_To_WGS_1984_1",in_coor_system="GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',\ SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]") #calculate area of intersection between grid and woreda or kebele shapefile after adding a field to store it ap.AddField_management(GridIntersectProject, "PartArea", "DOUBLE", 12, 6, "", "", "", "", "") expression = "float(!SHAPE.AREA@SQUAREKILOMETERS!)" ap.CalculateField_management(GridIntersectProject, "PartArea", expression, "PYTHON") #convert GridIntersect's .dbf to a .csv for later use dbf = GridIntersectProject[0:-4] + ".dbf" intersectCSV = convertDBFtoCSV(dbf) return intersectCSV, GridCSV
def processSoilData(AggLevel): '''Calculates average soil characteristics at AggLevel = "Woreda" or "Kebele" and outputs them to WoredaSoilData.csv or KebeleSoilData.csv''' #set the working directory workingDir = os.getcwd() #Download all of the soil data downloadSoilData(AggLevel, workingDir) #Turn on Spatial Statistics package and define field over which ZonalStatisticsAsTable will be calculated (Woreda or Kebele ID) ap.CheckOutExtension("Spatial") if AggLevel == 'Kebele': in_zone_data = os.path.dirname( workingDir ) + "\\Shapefiles\\Ethiopia Kebeles without Somali region.shp" in_template_dataset = in_zone_data zone_field = "KebeleID" elif AggLevel == 'Woreda': in_zone_data = os.path.dirname( workingDir) + "\\Shapefiles\\WoredasWGS1984.shp" in_template_dataset = in_zone_data zone_field = "WOREDANO_" #Define the projection and change the working directory to the directory with all of the soil data folders latLongRef = "Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj" os.chdir(workingDir) directories = [ f for f in os.listdir(os.getcwd()) if os.path.isfile(f) == False ] for i in range(len(directories)): #Find all the tiffs with soil data in each soil characteristic folder os.chdir(workingDir + "\\" + directories[i]) filelist = os.listdir(os.getcwd()) tiffs = [] clipTiffs = [] for j in range(len(filelist)): name = filelist[j] if name[-8::] == '250m.tif': tiffs.append(name) elif name[-9::] == '_Clip.tif': clipTiffs.append(name) for j in range(len(clipTiffs)): clipTiffs[j] = os.getcwd() + "\\" + clipTiffs[j] for j in range(len(tiffs)): in_raster = os.getcwd() + "\\" + tiffs[j] out_raster = os.getcwd() + "\\" + tiffs[j][0:-4] + "_Clip.tif" #Clip the tiffs to Ethiopia if they haven't been already if out_raster not in clipTiffs: ap.Clip_management(in_raster, "#", out_raster, in_template_dataset, "#", 1) #Calculate Zonal Statistics of soil data at AggLevel in_value_raster = out_raster out_table = os.getcwd() + "\\" + tiffs[j][0:-4] + AggLevel + ".dbf" ap.sa.ZonalStatisticsAsTable(in_zone_data, zone_field, in_value_raster, out_table) #Convert the DBFs with all the AggLevel soil data to CSVs #Change the working directory to the directory with all the soil data folders os.chdir(workingDir) directories = [ f for f in os.listdir(os.getcwd()) if os.path.isfile(f) == False ] for i in range(len(directories)): #Find all the DBFs with soil data in each soil characteristic folder os.chdir(workingDir + "\\" + directories[i]) filelist = os.listdir(os.getcwd()) DBFs = [] for j in range(len(filelist)): name = filelist[j] if name[-10::] == AggLevel + '.dbf': DBFs.append(name) #Convert the DBF to a CSV for j in range(len(DBFs)): convertDBFtoCSV(os.getcwd() + "\\" + DBFs[j]) #Join the CSVs with all the AggLevel soil data into one CSV titled "WoredaSoilData.csv" or "KebeleSoilData.csv" depending on the AggLevel joinSoilCSVs(AggLevel, workingDir) return None