Hi shouvik jha ,
Let's start with input 1 and the reason why this throws an error. As I mentioned before and as the error indicates there is something wrong with the field name(s). The error points to line 34 where field names are used in a cursor to create a list of values for the graph. Remember that in the code the names of the fields created in the Extract Multi Values to Points tool are basically the names of the rasters (r001_NPP.TIF and r003_NPP.TIF) where the dot is replaced by and underscore yielding r001_NPP_TIF and r003_NPP_TIF. Both the fields have 12 characters which is longer than the 10 character limit for the fields in a Shapefile (that's just a single reason why I prefer to use featureclasses in a File Geodatabase). ArcGIS will correct the field names, however, the code will still try to access the original (invalid names) when it tries to generate the lists of values for the graph.
As mentioned before your should use ValidateTableName to create valid field names. However, this does not work correctly on shapefiles. It will not take into account the maximum number of characters for the field name in a Shapefile. Take a look at the code below:
def main():
import arcpy
import os
import math
arcpy.env.overwriteOutput = True
ras1 = r'C:\GeoNet\Scatter_Plot_File\NPP_2008\r001_NPP.TIF'
ras2 = r'C:\GeoNet\Scatter_Plot_File\NPP_2008\r003_NPP.TIF'
fc_pnt = r'C:\GeoNet\Scatter_Plot_File\NPP_2008\random_points.shp'
num_points = 1000
min_dist = math.hypot(arcpy.Describe(ras1).meanCellHeight, arcpy.Describe(ras1).meanCellWidth)
ws, fc_name = os.path.split(fc_pnt)
arcpy.CreateRandomPoints_management(out_path=ws, out_name=fc_name,
constraining_extent=ras1,
number_of_points_or_field=num_points,
minimum_allowed_distance=min_dist)
arcpy.CheckOutExtension("Spatial")
ws, ras1_name = os.path.split(ras1)
ws, ras2_name = os.path.split(ras2)
ws_shp, shp_name = os.path.split(fc_pnt)
fld_ras1 = arcpy.ValidateTableName(ras1_name, ws_shp)
fld_ras2 = arcpy.ValidateTableName(ras2_name, ws_shp)
print ras1_name, fld_ras1
print ras2_name, fld_ras2
fld_ras1 = fld_ras1[:10]
fld_ras2 = fld_ras2[:10]
print ras1_name, fld_ras1
print ras2_name, fld_ras2
in_rasters = [[ras1, fld_ras1], [ras2, fld_ras2]]
arcpy.sa.ExtractMultiValuesToPoints(fc_pnt, in_rasters, "NONE")
arcpy.CheckInExtension("Spatial")
print [fld.name for fld in arcpy.ListFields(fc_pnt)]
xs = [r[0] for r in arcpy.da.SearchCursor(fc_pnt, (fld_ras1)) if not r[0] is None]
ys = [r[0] for r in arcpy.da.SearchCursor(fc_pnt, (fld_ras2)) if not r[0] is None]
if __name__ == '__main__':
main()
This will print:
r001_NPP.TIF r001_NPP_TIF
r003_NPP.TIF r003_NPP_TIF
r001_NPP.TIF r001_NPP_T
r003_NPP.TIF r003_NPP_T
[u'FID', u'Shape', u'Id', u'r001_NPP_T', u'r003_NPP_T']
On line 1 and 2 the "validated" field names with a length larger than the allowed 10 characters in a Shapefile. Lines 3 and 4 after cutting off the additional characters and on line 5 the actual fieldnames in the shapefile. Choose your output format wisely!