Skip to content

Instantly share code, notes, and snippets.

@MarketaP
Created November 6, 2018 21:45
Show Gist options
  • Save MarketaP/e6012a6769e952be836858b49913c2e7 to your computer and use it in GitHub Desktop.
Save MarketaP/e6012a6769e952be836858b49913c2e7 to your computer and use it in GitHub Desktop.
import sys, os, glob, winsound
import arcpy
from arcpy.sa import *
from arcpy import env
arcpy.CheckOutExtension('spatial')
arcpy.env.overwriteOutput = True
## List of rasters to project/sample to that of raster list 2
dataPath1 = r"I:\Data\USDM\GRACE_Grid"
dataPath2 = r"I:\Data\NLDAS\Reclassify\Levels\ewp"
outDataPath = r"I:\Data\USDM\NLDAS_Grid\resample/"
arcpy.env.workspace = r"I:\Code\Scratch"
List1 = glob.glob(dataPath1+'\*.tif')
List2 = glob.glob(dataPath2+'\*.tif')
PRJ = List2[0]
inPRJ = List1[0]
print "Raster Set 1 Count = "+str(len(List1))
print "Raster Set 2 Count = "+str(len(List2))
List1 = sorted(List1)
List2 = sorted(List2)
length1 = len(List1)
count = 0
List1 = List1[:]
cellsize1 = arcpy.GetRasterProperties_management(List1[0],"CELLSIZEX")
cellsize2 = arcpy.GetRasterProperties_management(List2[0],"CELLSIZEX")
cs1 = cellsize1.getOutput(0)
cs2 = cellsize2.getOutput(0)
print("Cellsize of List 1: "+ cs1)
print("Cellsize of List 2: "+ cs2)
# Reproject - Resample, Snap, and ?Extract by Mask?
for raster in List1:
print(raster)
arcpy.env.snapRaster = dataPath2+"/"+ List2[0]
## for rtzsm [-34:], for gws [-32:], for NLDAS [-16:], for USDM[-21:]
out = outDataPath + raster[-21:]
arcpy.ProjectRaster_management(raster,out,PRJ,"NEAREST",cellsize2,"#","#","#")
# arcpy.ProjectRaster_management(raster,out,PRJ,"NEAREST",cellsize2,"#","#","#")
out = arcpy.env.snapRaster
count = count + 1
print("Resample " + raster[-21:] + " Complete ----------" + str(count) + "/" + str(length1))
print("Cleaning up work files...")
file_name = os.listdir(outDataPath)
for item in file_name:
if item.endswith(".xml") or item.endswith(".tfw") or item.endswith(".ovr"):
os.remove(os.path.join(outDataPath, item))
winsound.Beep(1000,1000)
print("COMPLETE")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment