ArcPy Error: Date' is not in list, While extracting monthly data from timeseries NetCDF

2933
13
03-07-2017 06:12 AM
ShouvikJha
Occasional Contributor III

Hi All, 

I have one NetCDF file, that contain monthly value for the different year (1910 - 2014). I want to extract few years data (e.g 2000 - 2014), date range reading from a csv file  and save it as a raster map as the  name format of year-month-date.  I am working on below script but I am getting error 

Below i have attached the csv file which i am using for date range purpose. 

Any help would be greatly appreciated! Thanks 

The image showing the format of Year-month-date

Error massage 

Traceback (most recent call last):
  File "<module4>", line 18, in <module>
ValueError: 'Date' is not in list‍‍‍

Here what i have  so far 

import os, sys
import arcpy

# Check out any necessary licenses
arcpy.CheckOutExtension("spatial")
arcpy.env.overwriteOutput = True

# Script arguments
netCDF = "C:\\Users\\Downloads\\SPIE"
rainfall = "C:\\Users\\Downloads\\SPIE\\New folder"

arcpy.env.workspace = netCDF

# Read Date from csv file
eveDate = open ("C:\\Users\\Downloads\\SPIE\\Name1.csv")
headerLine = eveDate.readline()
valueList = headerLine.split(",")
dateValueIndex = valueList.index("Date")
eventList = []
for line in eveDate.readlines():
    segmenLine = line.split(",")
    variable = "spei"
    x_dimension = "lon"
    y_dimension = "lat"
    band_dimension = ""

for year in range(2010):
    nc_name = 'spei.%d.nc' % (year,)
    valueSelectionMethod = "BY_VALUE"
    outFile = "Pre"
    # extract dimensionValues from csv file
    arcpy.MakeNetCDFRasterLayer_md("spei.nc", variable, x_dimension, y_dimension, outFile, band_dimension, segmenLine[dateValueIndex], valueSelectionMethod)
    print "layer done"
    #copy and save as raster tif file
    arcpy.CopyRaster_management(outFile, rainfall + segmenLine[dateValueIndex] + ".tif" , "", "", "", "NONE", "NONE", "")
    print "raster done"‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
13 Replies
DanPatterson_Retired
MVP Emeritus

you should be using colons when you are parsing, inyour case, you want the last two characters and you will be doing an equality check

>>> nowfile = '1989'
>>> whatisit = int(nowfile[-2])
>>> whatisit
8
>>> howabout = int(nowfile[:-2])
>>> howabout == int(nowfile[:-2])
True
>>>
ShouvikJha
Occasional Contributor III

Dan Patterson‌ sorry for late response. I was out of station. 

Here i tried to follow your suggestion. after assign the year (2002), its create only one raster not extracting all month data in 2002.

dimension_values = nc_FP.getDimensionValue(dimension, i)
                nowFile = str(dimension_values)
                nowFile = nowFile.translate(None, '/')
                nowFile = ('2002')
                # I needed only the years 2002
                if int(nowFile[:-2]):
0 Kudos
ShouvikJha
Occasional Contributor III

Dan Patterson‌, Thank you. I just solved the issue, using below command 

if int(nowFile[-4:]) == 2002:

Thank you all for  your kind cooperation. 

DanPatterson_Retired
MVP Emeritus

good, now you know how slicing works

0 Kudos