I am dabbling with the resolution of the error says:
RuntimeError: ERROR 010240: Could not save raster dataset to
as attached as a screen shot. I am working with a DEM file and trying to generate raster based on the conditional values from an excel file. As the excel file has about 15000 row, I need to generate about 15000 raster. I am very much disappointed when the script takes long time and at last it fails every time at different point and this has been happening from the yesterday.
I am just reading date and associated 3 temperature values from the excel and applying con operation on DEM argis grid raster to generate another raster. This process is repeated for all the dates i.e. rows in the excel file as attached.
My script is a below-
import arcpy,os,sys,shutil
from arcpy.sa import Con
from arcpy.sa import Raster
from openpyxl import load_workbook
arcpy.env.overwriteOutput = True
arcpy.CheckOutExtension("spatial")
INPUT_TEMP_EXCEL_PATH = (#arcpy.GetParameterAsText(0)
r"C:\Users\Me\Desktop\Ryan\Sept02ModularApproach\Temperature Model Data.xlsx"
)
INPUT_DEM_RASTER_PATH = (#arcpy.GetParameterAsText(1)
r"C:\Users\Me\Desktop\Ryan\Sept02ModularApproach\DEM\dem_clip_11"
)
Second_Discrete_variable = (#arcpy.GetParameterAsText(2)
10
)
OUTPUT_TEMP_RASTER_FOLDER = (#arcpy.GetParameterAsText(3)
r"C:\Users\Me\Desktop\Ryan\Sept02ModularApproach\OutputRaster"
)
TEMP_FOLDER_PATH = (#arcpy.GetParameterAsText(4)
r"C:\Users\Me\Desktop\Ryan\Sept02ModularApproach\Temp"
)
Second_Discrete_variable = float(Second_Discrete_variable)
Temp_Data = []
#Loading temperature data
temp_wb = load_workbook(filename=INPUT_TEMP_EXCEL_PATH, read_only=True)
temp_ws = temp_wb[temp_wb.sheetnames[0]]
for row in temp_ws.rows:
rw = [cell.value for cell in row]
Temp_Data.append(rw)
Temp_Data= Temp_Data[1:]
#Folder content deleter
def folder_content_deleter(folder_path):
for the_file in os.listdir(folder_path):
file_path = os.path.join(folder_path, the_file)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path): shutil.rmtree(file_path)
except Exception as e:
pass
#set temporary places, grid format needs a gdb for placing intermediate data
folder_content_deleter(TEMP_FOLDER_PATH)
arcpy.CreateFileGDB_management(out_folder_path=TEMP_FOLDER_PATH, out_name="ScratchData_solRaster", out_version="CURRENT")
arcpy.env.workspace = arcpy.env.scratchWorkspace = os.path.join(TEMP_FOLDER_PATH,"ScratchData_solRaster.gdb")
#processing Second part
for tdata in Temp_Data:
T1 = tdata[1]
T2 = tdata[2]
T3 = tdata[3]
output_second = Con(Raster(INPUT_DEM_RASTER_PATH)<2573,T1,Con(Raster(INPUT_DEM_RASTER_PATH)<=2754,T2,T3))
final_temp_raster = output_second+Second_Discrete_variable
#save
out_path = os.path.join(OUTPUT_TEMP_RASTER_FOLDER,str(tdata[0]))
final_temp_raster.save(out_path)
#Cleaning
if arcpy.Exists("in_memory"):
arcpy.Delete_management("in_memory")
folder_content_deleter(TEMP_FOLDER_PATH)
My error in gist is-
RuntimeError: ERROR 010240: Could not save raster dataset to C:\Users\Winrock\Desktop\Ryan\Sept02ModularApproach\Temp\ScratchData_solRaster.gdb\ifthe_ras with output format FGDBR.
The excel file I am using is https://www.dropbox.com/s/qacfhipo4ry7o2b/Temperature%20Model%20Data.xlsx?dl=0
My error-
N.B. I tried several thread here and in arcgis sites some of them are-
- What causes RuntimeError: ERROR 010240 saving after CellStatistics?
- Why does CON statement give ERROR 010240: Could not save raster dataset to (value) with output format GRID?
- https://gis.stackexchange.com/questions/124511/error-010240-with-output-format-grid
Update:
*I tried with different scratch and current workspace
*I tried with arcpy.gp.Times_sa(it stops after processing almost 3000 rasters) and arcpy.sa.Times(it stops after processing almost 1070 rasters)
*I tried with arcpy.TestSchemaLock
*I tried with setting output to tif format too.
I see that it stops and raises error when it processed and output exact number of 1070 grid files. After some time wasting I came with the following-
import arcpy,os,sys,shutil,time,csv
from arcpy.sa import Con
from arcpy.sa import Raster
from openpyxl import load_workbook
arcpy.env.overwriteOutput = True
arcpy.CheckOutExtension("spatial")
INPUT_TEMP_EXCEL_PATH = (#arcpy.GetParameterAsText(0)
r"C:\test\Temprt.xlsx"
)
INPUT_DEM_RASTER_PATH = (#arcpy.GetParameterAsText(1)
r"C:\test\dem_clip_11"
)
Second_Discrete_variable = (#arcpy.GetParameterAsText(2)
1
)
OUTPUT_TEMP_RASTER_FOLDER = (#arcpy.GetParameterAsText(3)
r"C:\test\myout"
)
TEMP_FOLDER_PATH = (#arcpy.GetParameterAsText(4)
r"C:\test\mytemp"
)
Second_Discrete_variable = float('%.6f'%Second_Discrete_variable)
Temp_Data = [] # it is as list like [[19120515L, 5.673994, 4.899673, 2.826565], [19120516L, 7.585234, 6.915178, 4.927008],..............]
#Loading temperature data
temp_wb = load_workbook(filename=INPUT_TEMP_EXCEL_PATH, read_only=True)
temp_ws = temp_wb[temp_wb.sheetnames[0]]
for row in temp_ws.rows:
d = []
for cell in row:
if cell.value == None:
pass
elif cell.value == 0:
d.append(0.000000)
elif isinstance(cell.value, float):
d.append(round(cell.value,6))
else:
d.append(cell.value)
Temp_Data.append(d)
#process collected excel data
Temp_Data= Temp_Data[1:]
seen = set()
Temp_Data = [x for x in Temp_Data if x[0] not in seen and not seen.add(x[0])]# removing duplicate date
#Folder content deleter
def folder_content_deleter(folder_path):
for the_file in os.listdir(folder_path):
file_path = os.path.join(folder_path, the_file)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path): shutil.rmtree(file_path)
except Exception as e:
pass
#gdb content deleter
def gdb_content_deleter(wrkspc):
for r,d,fls in arcpy.da.Walk(wrkspc, datatype="FeatureClass"):
for f in fls:
print f
try:
arcpy.Delete_management(os.path.join(r,f))
print "deleted scratch"
except:
pass
#set temporary places, grid format needs a gdb for placing intermediate data
folder_content_deleter(TEMP_FOLDER_PATH)
arcpy.CreateFileGDB_management(out_folder_path=TEMP_FOLDER_PATH, out_name="SDB_SR", out_version="CURRENT")
scr_db = os.path.join(TEMP_FOLDER_PATH,"SDB_SR.gdb")
arcpy.env.scratchWorkspace = scr_db
#processing Second part
counter = 0
for tdata in Temp_Data:
T1 = float('%.6f'%tdata[1])
T2 = float('%.6f'%tdata[2])
T3 = float('%.6f'%tdata[3])
out_folder_name = str(tdata[0])[0:4]
out_folder = os.path.join(OUTPUT_TEMP_RASTER_FOLDER,out_folder_name)
if not os.path.exists(out_folder):os.mkdir(out_folder)
arcpy.env.workspace = out_folder
out_path = os.path.join(out_folder,str(tdata[0])+'.tif') #like C:\\test\\myout\\1912\\19120515.tif'
output_second = Con(Raster(INPUT_DEM_RASTER_PATH)<2573,T1,Con(Raster(INPUT_DEM_RASTER_PATH)<=2754,T2,T3))
#apply map algebra if feature exists
try:
final_temp_raster = arcpy.gp.Times_sa(output_second, str(Second_Discrete_variable), "in_memory\\test_"+str(counter))
qq = final_temp_raster.getoutput(0)
qq=Raster(qq)
final_temp_raster = qq
except:
pass
try:
#check if wait is of use
time.sleep(15)
final_temp_raster = arcpy.gp.Times_sa(output_second, str(Second_Discrete_variable), "in_memory\\test_"+str(counter))
qq = final_temp_raster.getoutput(0)
qq=Raster(qq)
final_temp_raster = qq
except:
pass
#write the errors into csv
with open(os.path.join(TEMP_FOLDER_PATH,'error.csv'), 'ab') as error_file:
writr = csv.writer(error_file)
writr.writerow(tdata)
counter+=1
#save
final_temp_raster.save(out_path)
#cleaning
if arcpy.Exists("in_memory"):
arcpy.Delete_management("in_memory")
gdb_content_deleter(scr_db)
#Cleaning againg completely
if arcpy.Exists("in_memory"):
arcpy.Delete_management("in_memory")
folder_content_deleter(TEMP_FOLDER_PATH)
It is giving me pain for several days- please help!
