Select to view content in your preferred language

Conversion multiple Raster to NetCDF using Arcpy

6029
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
24 Replies
DanPatterson_Retired
MVP Emeritus
0 Kudos
ShouvikJha
Occasional Contributor III

Dan_Patterson‌, I am trying to find the solution, following by above comments but its not solved yet. 

0 Kudos
DanPatterson_Retired
MVP Emeritus

Hmmm it seems you haven't responded to iamurray‌ comments or implemented or commented on previous suggestions. Perhaps you could show what you have tried in more detail then since the repetition of the error message really isn't providing any new information to help solve your problem.  Also, maybe you will get some suggestions over on Stack Overflow, that may help people here or even prevent them from reinventing a suggestion.

ShouvikJha
Occasional Contributor III

I have posted my updated code reply with @Lan Murry comment. and that Stake overflow code is not posted by me , I mostly using geonet. My one of research junior also trying to solve the same issue. Thank you.  

0 Kudos
ShouvikJha
Occasional Contributor III

Dan_Pattersonxander_bakker‌ @Lan Murry , i have tried in my best to do it but i won't understanding the which way i have to solve the issue to make the time series NetCDF 

0 Kudos
ShouvikJha
Occasional Contributor III

Dan_Pattersonxander_bakker‌, can it be make a time series NetCDF (using my 12 months raster) using arcPy???

0 Kudos
DanPatterson_Retired
MVP Emeritus
# 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")

c = 0
for filename in rasters:
    variable = "NPP"
    units = "gcsqm"
    XDimension = "x"
    YDimension = "y"
    bandDimension = ""
    # Process: RasterToNetCDF
    out = "{}{}.nc".format(r"c:\test\file_0", c)
    arcpy.RasterToNetCDF_md(filename, out, variable, units,XDimension,
                             YDimension, bandDimension)
    c += 1‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Despite the path being named... r"c:\test\file_0", c) ...  this is totally untested

RebeccaStrauch__GISP
MVP Emeritus

I'm coming late to the party , but doesn't the ListRaster command need/like 2args to use it this way?  I think both are optional, but maybe try

rasters = arcpy.ListRasters("*", "TIF")

and  then print or make sure it is returning something to rasters

ListRasters—Help | ArcGIS Desktop 

I'm on an iPad so can't test anything, but help sample...

import arcpy

# Set the current workspace
arcpy.env.workspace = "c:/data/DEMS"

# Get and print a list of GRIDs from the workspace
rasters = arcpy.ListRasters("*", "GRID")
for raster in rasters:
   print(raster)

Change "GRID" to "TIF"

DanPatterson_Retired
MVP Emeritus

fixed... we need to pool our resources and get ArcThingy for our iPads

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: