How to extract NetCDF raster values for a variable at each time step?

03-23-2017 07:31 PM
New Contributor II

I have a single NetCDF file that contains daily precipitation data for several years and would like to extract them in a more automatic way.

I am aware I could do it by creating an model builder and within "make netcdf raster layer" make the dimension values variable. However when I add a quite a lot of dimension values, model builder keeps crashing.

That is a print screen of my model:

I am wondering if someone could help me with a phyton scripts for this.

That would be very much appreciated.

0 Kudos
5 Replies
MVP Legendary Contributor

netCDF has come up recently,  see if there is anything here you can troll

New Contributor II
Apologies guys for asking the same question..

I went through similar examples and I very unfamiliar with Phython scripts, so I tried only the most simple one first (i.e. exctrating the first raster layer):

# Import system modules  

import arcpy  

from arcpy import env  

from import *  


# Input data source  

arcpy.env.workspace = "C:\prec"  

arcpy.env.overwriteOutput = True

# Set output folder  

OutputFolder = "C:\prec\Raster"  


# Loop through a list of files in the workspace  

NCfiles = arcpy.ListFiles("*.nc")

for filename in NCfiles:  

   print("Processing: " + filename)  

   inNCfiles = arcpy.env.workspace + "/" + filename  

   fileroot = filename[0:(len(filename)-3)]  

   TempLayerFile = "prec_amount"  

   outRaster = OutputFolder + "/" + fileroot 



# Process: Make NetCDF Raster Layer  

# this will convert the FIRST time step in your netCDF file to a raster layer  

        arcpy.MakeNetCDFRasterLayer_md(inNCfiles, "prec", "lon", "lat", TempLayerFile, "#", "time 01/01/2000 12:00:00 PM", "BY_VALUE")


# Process: Copy Raster  

        arcpy.CopyRaster_management(TempLayerFile, outRaster + ".tif", "", "", "", "NONE", "NONE", "") 

print "***DONE!!!"  

print arcpy.GetMessages() 

The error message does not have any specific ID so I am not sure why the tool could not succeed:

"Runtime error
Traceback (most recent call last):
  File "<string>", line 14, in <module>
  File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\", line 172, in MakeNetCDFRasterLayer
    raise e
RuntimeError: Object: Error in executing tool".

Would anyone have any guess?

0 Kudos
MVP Legendary Contributor

inNCfiles should be 'filename' since filename is the name used within the loop ie (arcpy.MakeNetCDFRasterLayer_md(filename..... )

use raw formatting for paths ie ... r"c:\whatever" r"c:\whatever\more"

Try to reformat the code using the code syntax highlighter  (click on the ... in your thread but outside of the messages section... click on your thread title to get there)

more later

0 Kudos
New Contributor II
# Import system modules  
import arcpy  
from arcpy import env  
from import *  
# Input data source  
arcpy.env.workspace = r"C:\prec"   

arcpy.env.overwriteOutput = True  

# Set output folder  
OutputFolder = r"C:\prec\Raster"  
# Loop through a list of files in the workspace  
NCfiles = arcpy.ListFiles("*.nc")  

for filename in NCfiles:  
   print("Processing: " + filename)  
   inNCfiles = arcpy.env.workspace + "/" + filename   
   fileroot = filename[0:(len(filename)-3)]  
   TempLayerFile = "prec_amount"  
   outRaster = OutputFolder + "/" + fileroot 

# Process: Make NetCDF Raster Layer  
# this will convert the FIRST time step in your netCDF file to a raster layer  
     arcpy.MakeNetCDFRasterLayer_md(filename, "prec", "lon", "lat", TempLayerFile, "#", "time 01/01/2000 12:00:00 PM", "BY_VALUE")

# Process: Copy Raster  
arcpy.CopyRaster_management(TempLayerFile, outRaster + ".tif", "", "", "", "NONE", "NONE", "") 
print "***DONE!!!"  
print arcpy.GetMessages() 

Thanks Dan. I pasted the code as above.

So, I have replaced inNCfiles either  in both entries (i.e. lines 22 and 30) or only at line 30, but I m still getting the same error and I have no clue the reasons why.

"Runtime error
Traceback (most recent call last):
  File "<string>", line 14, in <module>
  File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\", line 172, in MakeNetCDFRasterLayer
    raise e
RuntimeError: Object: Error in executing tool"

MVP Esteemed Contributor