6

I am having trouble saving outputs from CellStatistics but only when I run it on a list of rasters. This was working on python 26 but not 27.

I know this example is nonsense, but:

env.workspace =    "G:\\Faculty\\Mann\\Historic_BCM\\Aggregated1080\\" 
relevant_tifnames_year_i= ['cwd1975jan.tif', 'cwd1975feb.tif', 'cwd1975mar.tif', 'cwd1975apr.tif', 'cwd1975may.tif', 'cwd1975jun.tif', 'cwd1975jul.tif', 'cwd1975aug.tif', 'cwd1975sep.tif', 'cwd1975oct.tif', 'cwd1975nov.tif', 'cwd1975dec.tif']

mean = CellStatistics(relevant_tifnames_year_i, "MEAN", "DATA")
mean.save(str(env.workspace)+"outa.tif")

sd = CellStatistics(relevant_tifnames_year_i, "STD", "DATA")
sd.save(str(env.workspace)+"outb.tif")

Atest= CellStatistics([mean,sd], "MEAN", "DATA")
Atest.save(str(env.workspace)+"outc.tif")

Traceback (most recent call last):
  File "<pyshell#81>", line 1, in <module>
    Atest.save(str(env.workspace)+"out3.tif")
RuntimeError: ERROR 010240: Could not save raster dataset to G:\Faculty\Mann\Historic_BCM\Aggregated1080\Scratch\meanc_ras3 with output format GRID.

Notice that I can save CellStats run on a list of files from my working directory, but not on a list made up of my intermediate inputs "sd" and "mean". I know that I can run it if I pass the file paths, but in my actual code I need to run it off of intermediate inputs. Am I missing something here?

Note: Overwrite = True has been set. There are no files with the same name in the output folder etc.

Here is the actual code (storing rasters to a list and then running CellStats)

for year in base_year:
    # paths for files
    relevant_tifnames_year = [v for v in relevant_tifnames if int(re.sub('[^0-9]','',v) or 0) >= year and int(re.sub('[^0-9]','',v) or 0) <= year+years_to_end ]
    #list to store annual means
    decadal_holder_Amn = [] 

    # for each period of interest
    for year_i in range(year, (year+years_to_end+1)):
       # get relevant paths
       relevant_tifnames_year_i = filter(lambda x: str(year_i) in x,relevant_tifnames_year)
       #calc annual mean
       mean = CellStatistics(relevant_tifnames_year_i, "MEAN", "DATA")
       #append mean mean to list of annual means
       decadal_holder_Amn.append(mean)
    #calc mean of annual means for the decade
    Amean=CellStatistics(decadal_holder_Amn, "MEAN", "DATA")
    Amean.save(str(env.workspace)+"outd.tif")
7
  • It's not the answer, but I guess mean.save(str(env.workspace)+"outa.tif") is not what you want: >>> str(arcpy.env.workspace)+"outa.tif"' results in: 'G:\\Faculty\\Mann\\Historic_BCM\\Aggregated1080outa.tif'. It should be str(arcpy.env.workspace) + "\\" + "outa.tif"' or `str(arcpy.env.workspace) + os.sep + "outa.tif"'
    – Saleika
    Commented Apr 23, 2013 at 5:57
  • What's your arcpy.env.scratchFolder? Is it: G:\Faculty\Mann\Historic_BCM\Aggregated1080\Scratch? And does the specified scratch folder exists in your file System?
    – Saleika
    Commented Apr 23, 2013 at 6:22
  • In the comment above I ment the arcpy.env.scratchWorkspace and not the arcpy.env.scratchFolder
    – Saleika
    Commented Apr 23, 2013 at 6:36
  • yep sorry typo on the '\\'
    – mmann1123
    Commented Apr 23, 2013 at 14:34
  • I have tried using a scratch workspace without any success, same problem
    – mmann1123
    Commented Apr 23, 2013 at 14:36

3 Answers 3

7

Amazingly all my troubles have to do with the lack of gdb use. As soon as I assigned the scratchWorkspace to a .gdb my write errors started going away!

env.scratchWorkspace ="G:\\Faculty\\Mann\\Historic_BCM\\Aggregated1080\\Scratch.gdb"
1
  • worked for me too
    – Doon_Bogan
    Commented Mar 29, 2017 at 18:52
3

You should do this (best practice with rasters to avoid unnecessary copying):

env.workspace = env.scratchWorkspace = "G:\\Faculty\\Mann\\Historic_BCM\\Aggregated1080\\"

then you can run

mean.save("outa.tif")

and it will save to that folder. No need to specify the full path, which may be part of your issue.

3
  • Be careful that it does not help if you save raster in grid format but no problem with saving tif format at least in my case.
    – Learner
    Commented Sep 9, 2016 at 6:40
  • I suggest not to assign workspaces like this since there may problem arise if both workspace and sratchworspace is same as jonah tells at gis.stackexchange.com/questions/15300/…
    – Learner
    Commented Sep 10, 2016 at 9:05
  • @sislam This only applies if you are working with multiple threads at once. When working with raster data in a single thread, setting the current and scratch to the same location is the most efficient way to go. When your processing is all done, you can save the output with .save() in any format or location you wwant. Commented Sep 12, 2016 at 16:32
1

Probably a little late, but my solution to this problem was saving the result in a different folder than the inputs. A temporary file is created in the work folder with a default name, which is erased after the process is finished, but it generates problems when the output is created.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.