using arcpy to export separate netcdf dimension values as a raster

5653
7
06-09-2011 03:04 PM
SarahBurns
New Contributor
Hi All,
I would like to open a netcdf file which has a months worth of data and then export each day as a separate raster. I can run arcpy to get a particular day if I enter the name of the day in the dimension field (i.e 2/02/1999) but how can I get it to find them without me having to enter each one and then export each one.
I have made an attempt at the code using acrpy.MakeNetCDFRasterLayer_md and arcpy.CopyRaster_managment but I don't understand how to work loop through the dimension values and how to work with files that go to memory (which is what happens to the netcdf files).
Any feedback will be greatly appreciated.
See code below
thanks

#Make a separate raster *.img file for each dimension value (time) in a netcdf file
import arcpy
from arcpy import env
from arcpy.sa import *

# Set local variables
inNetCDFFile = "D:/data/netcdffiles/SURFACEWEATHERDATA.nc"
variable = "TEMPERATURE"
XDimension = "longitude"
YDimension = "latitude"
bandDimmension = ""
dimensionValues = "time"
valueSelectionMethod = ""
InMemory_netcdf_raster = "InMemory_netCDF_File"
outRasterFolder = "D:/data/netcdffiles/OutputRasters/"

DimensionValues_List=[]
for DimensionValuesofTime in arcpy.ListDimensionValues():
    DimensionValues_List.append(DimensionValues.time)
    # Execute MakeNetCDFRasterLayer
    arcpy.MakeNetCDFRasterLayer_md(inNetCDFFile, variable, XDimension, YDimension,
                               InMemory_netCDF_File, bandDimension, dimensionValues,
                               valueSelectionMethod)

Rasters_List=[]
for Rasters in arcpy.ListRasters():
    Rasters_List.append(Rasters)
    #export raster layer (from scratch workspace?) to raster output folder as *.img
    arcpy.CopyRaster_management(InMemory_netCDF_File, OutputRasterFolder, "", "", "NONE", "NONE", "")
Tags (2)
0 Kudos
7 Replies
AndrewChapkowski
Esri Regular Contributor
Use the arcpy.NetCDFFileProperties() to examine the NetCDF's properties and dimension values.

Here is some code that will examine the netcdf's properties and display all the values for each dimension.  You can easily modify the code to you liking to only look the dimension value you specified in your code.

import arcpy

InNetCDF = r"C:\temp\tos_O1_2001-2002.nc"
try:
    ncFP = arcpy.NetCDFFileProperties(InNetCDF)
    ncDim = ncFP.getDimensions()
    # loop through all dimension and show the value
    for dim in ncDim:
        print dim
        top =  ncFP.getDimensionSize(dim)
        for i in range(0,top):
            print ncFP.getDimensionValue(dim,i)
except:
    print arcpy.GetMessages(2)
0 Kudos
SarahBurns
New Contributor
excellent, thank you.
0 Kudos
SarahBurns
New Contributor
So I can now loop through all dimension and show the values (thanks andrewchap)
but how can I loop through a particular dimensions values and save those so that they can be imported into the next step (which is into arcpy.MakeNetCDFRASTERLayer_md) see code attached
Any feedback will be greatly appreciated
0 Kudos
SarahBurns
New Contributor
Hello,
I am still having trouble running makenetcdfrasterlayer and then exporting as a raster in python.
My main problem is working out how to run makenetcdfrasterlayer on each dimension value (for time) and being able to export these as rasters.
This is the code I have so far.
Any feedback will be greatly appreciated!!
#Make a separate raster *.img file for each dimension value (time) in a netcdf file

import arcpy
from arcpy import env
from arcpy.sa import *
import os

# Set local variables
inNetCDF = r"D:/DSE_work/FFDInetcdf/FEB2009FFDI/IDZ00026_VIC_ADFD_FFDI_SFC.nc"
env.workspace="D:/DSE_work/FFDInetcdf/FEB2009FFDI"
outRasterFolder = "D:/data/FFDInetcdf/OutputRasters"
variable = "FFDI_SFC"
XDimension = "longitude"
YDimension = "latitude"
bandDimension = ""
dimensionValues = "time"
valueSelectionMethod = "BY_VALUE"

# loop through all dimension and show the values
ncFP = arcpy.NetCDFFileProperties(inNetCDF)
ncDim = ncFP.getDimensions()
for dim in ncDim:
    print "dimension name is:", dim
    dimsize =  ncFP.getDimensionSize(dim)
    for i in range(0,dimsize):
        dimvalues=ncFP.getDimensionValue(dim,i)
        print dimvalues
# Execute MakeNetCDFRasterLayer
        arcpy.MakeNetCDFRasterLayer_md(inNetCDF,
                                   variable,
                                   XDimension,
                                   YDimension,
                                   (outRasterFolder,dimvalues), #output folder named by dimension value 
                                   bandDimension,
                                   (dimensionValues,dimvalues), #need to feed in each day (value from the time dimension e.g time 2/02/2009)
                                   valueSelectionMethod)

 #export raster layer (from scratch workspace) to raster output folder as *.img
        Rasters_List=[]
        for Rasters in arcpy.ListRasters():
            Rasters_List.append(Rasters)
            arcpy.CopyRaster_management(outRasterFolder, ((os.path.join(outRasterFolder, "netcdf_"+rasters))), "", "", "NONE", "NONE", "")

print "end of processing"

0 Kudos
JasonGeck
New Contributor
All - Here is python code to parse out a NetCDF file to individual rasters  (.img).
Hope it helps -
Jason
0 Kudos
JustinKoppa
New Contributor
I have been working at this for awhile and struggling to iterate through rainfall data and extract to raster for the entire year by each day.  I have started with Jason's script above but I am getting an error with dimension value, so I think my error is comes early on.  Any feedback would be greatly appreciated.
Thanks, Justin

import arcpy, os, time, datetime, calendar

# Set local variables
arcpy.env.overwriteOutput = True
arcpy.env.scratchWorkspace = "C:\\temp\\"


#Year info
Yr = 2010
#Month info
allmnths = range(1,12)
for mnths in allmnths:
    Lastday = calendar.monthrange(Yr, mnths)[1]
    MRange = range(1,Lastday+1)
    for dyy in MRange:
        dyys =int(dyy)
        mnthsss = int(mnths)
        a = str(mnthsss)+ "/" + str(dyys) +"/"+str(Yr)
        b = str(mnthsss)+ "_" + str(dyys) +"_"+str(Yr)
        inDate = "time " + a
        outTmpRaster = "precip_" + b
        outImagine = "precip_" + b + ".img"

        precip_2010_nc = "H:\\ArcGIS\\Rainfall Index\\precip.V1.0.2010.nc"
        precip_Layer = "precip_Layer"+ b
        precip_layer_lyr = "C:\\temp\\precip_" + b + ".lyr"
        test_img = "C:\\temp\\precip_" + b + ".img"

        # Process: Make NetCDF Raster Layer
        arcpy.MakeNetCDFRasterLayer_md(precip_2010_nc, "precip", "lat", "long", precip_Layer, "", inDate, "BY_VALUE")
        print "Created NetCDF Layer for " + precip_Layer
       
        # Process: Save To Layer File
        arcpy.SaveToLayerFile_management(precip_Layer, precip_layer_lyr, "")
        print "Created Layer for " + precip_layer_lyr
       
        # Process: Copy Raster
        arcpy.CopyRaster_management(precip_layer_lyr, test_img, "", "", "", "NONE", "NONE", "")
        print "Created Raster for " + precip_layer_lyr
0 Kudos
MuhammadUmair
New Contributor

Hy. How are you guys?

I am also using the same code to import .nc files to raster (.tif). But when I open the converted raster in ENVI, its spatial reference is distorted. Its longitude (x-axis) is from 0 to 360. which should be -180 to 180.

However, the latitudes are ok and showing data from -50 to 50.

I think I need to georefrence the raster. But i am not sure.

Below is my code. "I AM REALLY IN A TROUBLE. ANY HELP WOULD REALLY BE APPRECIATED."

import arcpy, os, time, datetime, calendar, glob

from arcpy import env

from arcpy.sa import *

arcpy.env.workspace = "C:/Users/UmairPC/Desktop/TRMM"

arcpy.env.overwriteOutput = True

arcpy.env.scratchWorkspace = "C:/Users/UmairPC/Desktop/TRMM"

InMemory_netcdf_raster = "r"

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

for filename in NetCDFfiles:

    print("Processing: " + filename)

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

    # Process: Make NetCDF Raster Layer 

    arcpy.MakeNetCDFRasterLayer_md(inNetCDF, "r", "longitude", "latitude", InMemory_netcdf_raster, "", "", "BY_VALUE")

    outRasterFolder = "C:/Users/UmairPC/Desktop/TRMM/check1"

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

    outTIFF = outRasterFolder + "/" + filename + ".tif"

    # Process: Copy Raster

    arcpy.CopyRaster_management(InMemory_netcdf_raster, outTIFF, "", "", "", "NONE", "NONE", "")

print "DONE"

0 Kudos