Conversion multiple Raster to NetCDF using Arcpy

5576
24
Jump to solution
01-18-2017 10:24 PM
ShouvikJha
Occasional Contributor III

Dear all,

I have multiples raster files, i am trying to convert it in NetCDF file using Arcpy script . below script i am using for conversion. While  i ran the below script , i am getting the error, 

Below i have attached my raster file also, which i trying to convert in NetCDF format. 

Error Code 

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).‍‍‍‍

Below script i am using for conversion of raster to NetCDF. 

import arcpy
from arcpy import env

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

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

# Process: RasterToNetCDF
arcpy.RasterToNetCDF_md(inRaster, outNetCDFFile, variable, units,
                        XDimension, YDimension, bandDimension)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

I took the code provided by Dan Patterson and changed it a bit for the file naming part. However it threw me an error. Doing a manual conversion threw the error 000243:

000243: The units value is either invalid or not supported.

Description
The specified unit is either invalid or not supported by the UDUNITS library

You can find the valid UDUNITS here: http://www.unidata.ucar.edu/software/udunits/udunits.txt 

When I clear the units variable (line 10) it runs without problems. Are you sure the units are OK (I'm running an old 10.3.1 version of ArcMap). 

# Import system modules
import arcpy
import os

# Set local variables
ws_in = r"C:\GeoNet\NPP_2010"
ws_out = r"C:\GeoNet\NPP_2010\results"

variable = "NPP"
units = "" # gcsqm seems to be unvalid (ERROR: 000243)
XDimension = "x"
YDimension = "y"
bandDimension = ""

arcpy.env.workspace = ws_in
rasters = arcpy.ListRasters("*","TIF")

for rasname in rasters:
    raspath = os.path.join(ws_in, rasname)
    name, ext = os.path.splitext(rasname)
    netcdf = os.path.join(ws_out, name)

    # Process: RasterToNetCDF
    print "Exporting {0} to {1}".format(rasname, netcdf)
    arcpy.RasterToNetCDF_md(raspath, netcdf, variable, units, XDimension,
                             YDimension, bandDimension)

Prints:

Exporting r001_NPP.TIF to C:\GeoNet\NPP_2010\results\r001_NPP
Exporting r002_NPP.TIF to C:\GeoNet\NPP_2010\results\r002_NPP
Exporting r003_NPP.TIF to C:\GeoNet\NPP_2010\results\r003_NPP
Exporting r004_NPP.TIF to C:\GeoNet\NPP_2010\results\r004_NPP
Exporting r005_NPP.TIF to C:\GeoNet\NPP_2010\results\r005_NPP

Content of output folder:

View solution in original post

24 Replies
DanPatterson_Retired
MVP Emeritus

you have set your environment workspace (ie a folder or a geodatabase) as D:\2012A  then you reassigned it as a raster.  This won't work.  And you have to start using raw formatting in any situation ... with the 'r' prefix to paths

r"c:\yourfolder\yoursubfolder\yourgdb.gdb"  for example

ShouvikJha
Occasional Contributor III

Dan Patterson‌, Thank you. I have updated my code by the add of r comment. and my raster file in .tif format. but unfortunately i am getting the error massage, below is error code, 

can you please explain, why i have to add .gdb  and is it possible to run without  gdb file ?

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).
                    
‍‍‍‍‍‍‍‍‍
0 Kudos
ShouvikJha
Occasional Contributor III

Xander Bakker‌, I have posted one of my problem, Please cooperate with us to solve the issues.  

0 Kudos
DanPatterson_Retired
MVP Emeritus

line 5 and 8 are wrong... read my post, putting an r in front of those won't work. Post your new code

XanderBakker
Esri Esteemed Contributor

As Dan Patterson mentions, revise the definition of the input data. If the "raster" is indeed a raster (Esri grid format with the name "2012A" in the root of drive D) then you should rename the raster (since you should not start the name of an Esri grid with a number).

Another general advise; run the tool manually with the data, check the result and if correct, copy the Python snippet to verify the syntax of the python code that you should use. 

ShouvikJha
Occasional Contributor III

Dan PattersonXander Bakker‌, Thank you very much all of you. I have updated my code, still problem persist, and and my raster name , r001_NPP.tif, r002_NPP.tif ........... r012_NPP.tif , and i checked one raster to converted in  NetCDF using Arc GIS tool, i got perfect output from there, but i want to convert by the script , i have many of raster to convert. 

# Import system modules
import arcpy
from arcpy import env

# Set environment settings
env.workspace = r"D:\NPP_GUJARAT_NEW\MODIS_BKU_DATA"

# Set local variables
inRaster = r"D:\NPP_GUJARAT_NEW\MODIS_BKU_DATA\NPP_2012A"
outNetCDFFile = r"D:\NPP_GUJARAT_NEW\MODIS_BKU_DATA\NC_FILE\nppnetcdf.nc"
variable = "NPP"
units = "gcsqm"
XDimension = "x"
YDimension = "y"
bandDimension = ""

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

your inRaster variable is currently a folder I believe and not the actual raster file.  The input for the tool has to be a raster file(in your case it looks like .tif).  If you have multiple rasters in that directory, you will need to make a list of them all and iterate through them, using each individual raster as input for the Raster to NetCDF Tool(See the link about the ListRasters).   Please also note you will need unique output names for each output NetCDF file, or you will just be overwriting the prior file.

http://pro.arcgis.com/en/pro-app/arcpy/functions/listrasters.htm

IanMurray
Frequent Contributor

Just as a note, I would make sure to run the ListRasters and print all the raster names as the example states, to make sure that the tool is using the correct files for input into the Raster to NetCDF.  Also if the rasters are in the NPP_2012A folder as I think they are from your code, you will need to make that your workspace when you use ListRasters.

ShouvikJha
Occasional Contributor III

Dan Patterson, @Lan Murray, Xander Bakker  i have tried using below command , but wont understand how to complete this code, and i want to take all 12 rasters in time series NetCDF file. Please guide on same 

# Import system modules
import arcpy
from arcpy import env

# Set environment settings
env.workspace = r"D:\NPP_GUJARAT_NEW\MODIS_BKU_DATA"

# Set local variables
inRaster = r"D:\NPP_GUJARAT_NEW\MODIS_BKU_DATA\NPP_2012A"
outNetCDFFile = r"D:\NPP_GUJARAT_NEW\MODIS_BKU_DATA\NC_FILE"
rasters = arcpy.ListRasters("*.tif")

for filename in rasters:
     variable = "NPP"
     units = "gcsqm"
     XDimension = "x"
     YDimension = "y"
     bandDimension = ""

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