Extracting rasters from NetCDF files based on time dimension

2303
13
09-11-2016 10:18 PM
ShouvikJha
Occasional Contributor III

I have single netCDF file which contain monthly data for different year. (Format of date e.g 2001-1-1, 2001-2-1, 2001-3-1…….2002-12-1...............2015-0-1......2015-12-1). 

Here-with i have attached a snapshot of data format from Arc GIS multidimension tool 

I trying to extract each month raster for selected year (e.g only 2002). Save it as 01-2002, 02-2002, 03-2002…..12-2002.

I appreciate any help you can provide on the crucial bottom code.

Here is what I have so far:

 

# Import system modules
import arcpy
from arcpy import env
from arcpy.sa import *

#Workspace
outLoc = r"D:\MODIS_NPP_02_15"
inNetCDF = r"D:\MODIS_NPP_02_15\NPP_2001_15.nc"

#Veriable
variable = "npp"
x_dimension = "lon"
y_dimension = "lat"
band_dimension = ""
dimension = "time"
valueSelectionMethod = "BY_VALUE"

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 2002
                if int(nowFile[-12]) > 6:

                    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, +".tif","", "", "", "NONE", "NONE", "")


                else: print "DATA OUT OF RANGE"‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
13 Replies
NeilAyres
MVP Frequent Contributor

So, what happens at the moment?

What errors are you getting or are you asking to validate this code prior to actually trying it?

I can see one problem with this :

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

Shouldn't you add the "*.tif" to outname in the line above. Here the "*.tif" is the 3rd parameter. Which I am sure will not work.

ShouvikJha
Occasional Contributor III

Thank you. Output raster i am trying to save in tif format. I am getting following error massage. 

Message File Name Line Position 
Traceback 
 <module> D:\Arc-GIS-Python-Script\NetCDF_RASTER_TIME_SERIES.py 30 
IndexError: string index out of range 
DanPatterson_Retired
MVP Esteemed Contributor

you had better throw a print statement in there to ensure that the negative slice doe indeed return the proper formatted year prior to testing.  you might want to split on "/" and return the last slice instead to get the year itself, rather than relying on a position count.

NeilAyres
MVP Frequent Contributor

Never dealt with this sort of data before, but that error is obviously your indexing of the value in Nowfile.

>>> n = "dngthrysdfg"
>>> n[-12]
Traceback (most recent call last):
 File "<pyshell#5>", line 1, in <module>
 n[-12]
IndexError: string index out of range
>>>

So, as Dan says, perhaps have a look at that variable before this step.

NeilAyres
MVP Frequent Contributor

And, not sure what that [-12] is doing.

>>> n = "fkghthsyfgtrekh"
>>> n[-12]
'h'
>>> n[:-12]
'fkg'
DanPatterson_Retired
MVP Esteemed Contributor
>>> a ='abcdefghijklmnopqrstuvwxyz'
>>> a[:-12]        # doubt it
'abcdefghijklmn'
>>> a[-12:]        # this one perhaps???????????
'opqrstuvwxyz'
>>> a[-12]         # nope
'o'
DanPatterson_Retired
MVP Esteemed Contributor

Any more info? or is this closed?

0 Kudos
ShouvikJha
Occasional Contributor III

Dan , Neil, i extremely sorry for the late reply, here holiday was going on . Problem still persist. by this command i am trying to extract selected time spam images. 

n[:-12]
0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

so you did see my example and you definitely want the first 12 characters of the string (line 4 in my example) and not the last 12 (ie line 4 in my example).  You need to print the string prior to extracting it so we know what is it is