7

I'm having trouble using the Dimension Values (Optional) argument in arcpy.MakeNetCDFRasterLayer_md.

Whatever I seem to do the output raster is always the first layer which is the default value.

Below is my attempt to export all layers to raster in the time dimension.

Code:

def extractAllNetCDF():

    variable = "RRt_10m"
    x_dimension = "lon"
    y_dimension = "lat"
    band_dimension = ""
    dimension = "time"
    valueSelectionMethod = "BY_VALUE"

    outLoc = "E:/New Folder/"
    inNetCDF = "E:/netCDFFiles/RRt.nc"    

    nc_FP = arcpy.NetCDFFileProperties(inNetCDF)   
    nc_Dim = nc_FP.getDimensions()

    for dimension in nc_Dim:

        top = nc_FP.getDimensionSize(dimension)

        for i in range(0, top):

            if dimension == "time":                
                dimension_values = nc_FP.getDimensionValue(dimension, i)
                nowFile = str(dimension_values)

                arcpy.MakeNetCDFRasterLayer_md(inNetCDF, variable, x_dimension, y_dimension, nowFile, band_dimension, dimension_values, valueSelectionMethod)
                arcpy.CopyRaster_management(nowFile, outLoc + nowFile + ".img", "", "", "", "NONE", "NONE", "")

            print dimension_values, i

The print method at the end will show the dates as they should be and the index i is also moving alon so there is no reason to think that there are other problems with the code other than the Dimension Value being incorrect and reverting to the default.

Does anyone have any idea how to get the subsequent layers to export?

Is there any code online that has a specific example of this using the Dimension Value argument other than empty quotes?

4
  • Do you have much experience in Python? Without having a serious play myself, you could try looking at the netcdf file using the netcdf4-python library to explore the data. Potentially you could even save the layers as NumPy arrays, and then import them into ArcGIS from there.
    – om_henners
    Commented Nov 14, 2011 at 2:21
  • I think that using numpy arrays may not be such a bad idea as then I can run it without using a licence. Thanks for the pointer to netcdf4-python, I will need it when I remove the hardwired code it has for testing so it will come in handy.
    – EnE_
    Commented Nov 14, 2011 at 5:07
  • I think it will be easier if use gdal_translate to do this!
    – user12739
    Commented Nov 13, 2012 at 17:06
  • I have placed the NetCDF files in the below Folder as following: ![enter image description here](i.sstatic.net/f4y7o.png) I want to convert the NetCDF files to Tif and place in the below directory: C:\output plus (I have extracted this information from ArcMAP and "Make NetCDF to Raster" tool): ![enter image description here](i.sstatic.net/VWgCw.png) Could you please modify the code for me?
    – ali
    Commented Dec 15, 2019 at 11:41

2 Answers 2

6

Okay, so this was a little tricky as there seemed to be a few different ways of using what is descibed as a Value Table on the ESRI Help Page:

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//004300000006000000.htm

There are also a few uses of brackets that could be tricky if you are not really sure of the syntax so hopefully if you can just use the example below it will work.

So what you really want is answers so here it is with a fix

def extractAllNetCDF():

    variable = "RRt_10m"
    x_dimension = "lon"
    y_dimension = "lat"
    band_dimension = ""
    dimension = "time"
    valueSelectionMethod = "BY_VALUE"

    outLoc = "E:/New Folder/"
    inNetCDF = "E:/netCDFFiles/RRt.nc"

    nc_FP = arcpy.NetCDFFileProperties(inNetCDF)
    nc_Dim = nc_FP.getDimensions()

    for dimension in nc_Dim:

        top = nc_FP.getDimensionSize(dimension)

        for i in range(0, top):

            if dimension == "time":

                dimension_values = nc_FP.getDimensionValue(dimension, i)
                nowFile = str(dimension_values)

                #THIS IS THE NEW CODE HERE
                dv1 = ["time", dimension_value]
                dimension_values = [dv1]
                #END NEW CODE

                arcpy.MakeNetCDFRasterLayer_md(inNetCDF, variable, x_dimension, y_dimension, nowFile, band_dimension, dimension_values, valueSelectionMethod)
                arcpy.CopyRaster_management(nowFile, outLoc + nowFile + ".img", "", "", "", "NONE", "NONE", "")
                print dimension_values, i 

So that's it essentially. There is no need to create an instance of 'Value Table' type e.g.

vtab = arcpy.ValueTable(2)

as is seemed to be implied by the fact the argument was labelled 'Value Table'.

There is no need to use all of the brackets that they show in the examples whether curly, round or otherwise. Follow the above and it should work.

0
3

Just improving the efficiency of the above answer by pulling the if dimension == "time": out of the loop so it doesn't evaluate needlessly again and again. Also pulled the forward slashes out of the output filenames, to address an error in executing MakeNetCDFRasterLayer_md.

 def extractAllNetCDF():

    variable = "precip"
    x_dimension = "lon"
    y_dimension = "lat"
    band_dimension = ""
    dimension = "time"
    valueSelectionMethod = "BY_VALUE"

    outLoc = r"C:\Users\jkadmin\Downloads\Shirin\a"
    inNetCDF = r"C:\Users\jkadmin\Downloads\Shirin\precip.mon.total.v7.nc"

    nc_FP = arcpy.NetCDFFileProperties(inNetCDF)
    nc_Dim = nc_FP.getDimensions()

    for dimension in nc_Dim:
        if dimension == "time":
            top = nc_FP.getDimensionSize(dimension)
            for i in range(0, top):

                dimension_values = nc_FP.getDimensionValue(dimension, i)
                nowFile = str(dimension_values)
                nowFile = nowFile.translate(None, '/')
                # I needed only the years 1950 onward
                if int(nowFile[-2]) > 4:

                    dv1 = ["time", dimension_values]
                    dimension_values = [dv1]

                    arcpy.MakeNetCDFRasterLayer_md(inNetCDF, variable, x_dimension, y_dimension, nowFile, band_dimension, dimension_values, valueSelectionMethod)
                    print "success"
                    outname = outLoc + nowFile

                    arcpy.CopyRaster_management(nowFile, outname, "", "", "", "NONE", "NONE", "")


                else: print "before 1950"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.