Search code examples
pythonarcgisarcpy

How to convert multiple Raster to NetCDF using ArcPy


I am trying to convert multiple raster files to NetCDF files using an ArcPy script. When I run the below script, I get the following error message:

Message File Name   Line    Position    
Traceback               
    <module>    <module1>   19      
    RasterToNetCDF  C:\Program Files\ArcGIS\Desktop10.3\ArcPy\arcpy\md.py   253     
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000840: The value is not a Raster Layer.
ERROR 000840: The value is not a Raster Catalog.
Failed to execute (RasterToNetCDF).

Python script:

# Import system modules
import arcpy
from arcpy import env

# Set environment settings
env.workspace = r"D:\2012A"

# Set local variables
inRaster = r"D:\2012A"
outNetCDFFile = r"D:\2012A/nppnetcdf.nc"
variable = "elevation"
units = "meter"
XDimension = "x"
YDimension = "y"
bandDimension = ""

# Process: RasterToNetCDF
arcpy.RasterToNetCDF_md(inRaster, outNetCDFFile, variable, units,
                        XDimension, YDimension, bandDimension)

Solution

  • @Erica answered why you are receiving an error, though if you want to perform your conversion for each raster dataset within a directory, you'll have to first create a list of rasters within it.This can be done with something like this:

    rasterlist = arcpy.ListRasters()
    ## other variables, as you have already defined them in your code
    for raster in rasterlist:
        RastertoNetCDF_md(variables)
    

    So to implement this:

    # Set environment settings
    env.workspace = r"D:\2012A"
    
    # Set local variables
    inRaster = r"D:\2012A"
    outNetCDFFile = r"D:\2012A\nppnetcdf.nc"
    variable = "elevation"
    units = "meter"
    XDimension = "x"
    YDimension = "y"
    bandDimension = ""
    rasterlist = arcpy.ListerRasters()
    
    # Process: RasterToNetCDF
    for raster in rasterlist:
        arcpy.RasterToNetCDF_md(inRaster, outNetCDFFile, variable, units,
                        XDimension, YDimension, bandDimension)