Select to view content in your preferred language

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

3190
5
03-23-2017 07:31 PM
RafaelMagris
Deactivated User

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
DanPatterson_Retired
MVP Emeritus

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

https://community.esri.com/thread/191339-arcpy-error-date-is-not-in-list-while-extracting-monthly-da...

RafaelMagris
Deactivated User
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 arcpy.sa 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\md.py", line 172, in MakeNetCDFRasterLayer
    raise e
RuntimeError: Object: Error in executing tool".

Would anyone have any guess?

0 Kudos
DanPatterson_Retired
MVP Emeritus

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
RafaelMagris
Deactivated User
# Import system modules  
import arcpy  
from arcpy import env  
from arcpy.sa 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\md.py", line 172, in MakeNetCDFRasterLayer
    raise e
RuntimeError: Object: Error in executing tool"

curtvprice
MVP Esteemed Contributor