|
POST
|
Yes, sir, you are correct and you are going in the correct direction. Please, sir, help me to resolve this issue. How do I communicate with Joshua Bixby?
... View more
06-06-2019
01:31 AM
|
0
|
0
|
2157
|
|
POST
|
Thank you, sir, for your given comment. But I cannot clearly understand your suggestion can you elaborate me.
... View more
06-05-2019
01:50 PM
|
0
|
0
|
2157
|
|
POST
|
Thank you for your tricks is working well. Now I want to reduce my code because initially, I write this code for three land_type (Land_10, Land_20, Land_30) but I have 10 land_type under the three category ("AAA","BBB","CCC"). If you have any suggestion regarding this issue then, please suggest me. Next, I want to use the multiprocessing, multithreading function for the whole code because I have a large volume data, it takes much more time in one process. One more thing the line number 126 to 160 I want to define a function add to the main function. Then, the main function should be processed their next function( geoprocesse1,2,3) automatically based on their categories "AAA","BBB","CCC". How to do this? Please look at this matter. import datetime
start = datetime.datetime.now()
print 'start run: %s\n' % (start)
import arcpy ,os ,sys,csv,errno
from arcpy import env
from arcpy.sa import *
import datetime
import re
import glob
import itertools
arcpy.env.overwriteOutput = True
import multiprocessing as mp
print("Number of processors: ", mp.cpu_count())
cellsize = "F:\\DB_test_data\\TEST_RAY\\TEST1\\MOD02HKM_A2017001_0530_NDVI_AA.img"
d1="F:\\DB_test_data\\VAR2\\cldmask\\tt"
CLDMASK = glob.glob(d1 + os.sep + "*.Aerosol_Cldmask_Land_Ocean-Aerosol_Cldmask_Land_Ocean.tif")
CLDMASK.sort()
if CLDMASK is None:
print 'Could not open the CLDMASK raster files'
sys.exit(1)
else:
print 'The CLDMASK raster files was opened successfully'
#print CLDMASK
d2="F:\\DB_test_data\\TEST_RAY\\TEST1"
NDVI = glob.glob(d2 + os.sep + "*A2017001_0530_NDVI_AA.img")
NDVI.sort()
if NDVI is None:
print 'Could not open the NDVI raster files'
sys.exit(1)
else:
print 'The NDVI raster files was opened successfully'
#print NDVI
d3="F:\\DB_test_data\\VAR2\\IDL\\MOD02HKM\\TEST"
BAND1 = glob.glob(d3 + os.sep + "*A2017001_0530_006_BAND_1.img")
BAND1.sort()
if BAND1 is None:
print 'Could not open the BAND1 raster files'
sys.exit(1)
else:
print 'The BAND1 raster files was opened successfully'
#print BAND1
d4="F:\\DB_test_data\\VAR2\\IDL\\MOD02HKM\\TEST"
BAND2 = glob.glob(d4 + os.sep + "*A2017001_0530_006_BAND_2.img")
BAND2.sort()
if BAND2 is None:
print 'Could not open the BAND2 raster files'
sys.exit(1)
else:
print 'The BAND2 raster files was opened successfully'
#print BAND2
d4="F:\\DB_test_data\\VAR2\\IDL\\MOD09\\TEST"
BAND3 = glob.glob(d4 + os.sep + "*A2017001_0530_006_BAND_3.img")
BAND3.sort()
if BAND3 is None:
print 'Could not open the BAND3 raster files'
sys.exit(1)
else:
print 'The BAND3 raster files was opened successfully'
#print BAND3
d5="F:\\DB_test_data\\VAR2\\IDL\\MOD02HKM\\TEST"
BAND4 = glob.glob(d5 + os.sep + "*A2017001_0530_006_BAND_4.img")
BAND4.sort()
if BAND4 is None:
print 'Could not open the BAND4 raster files'
sys.exit(1)
else:
print 'The BAND4 raster files was opened successfully'
#print BAND4
d6 = r"F:\\DB_test_data\\VAR2\\MOD04_l2"
AOD = glob.glob(d6 + os.sep + "*Corrected_Optical_Depth_Land_2-Corrected_Optical_Depth_Land.tif")
AOD.sort()
if AOD is None:
print 'Could not open the AOD raster files'
sys.exit(1)
else:
print 'The AOD raster files was opened successfully'
Scale_factor = float(0.0010000000474974513)
add_offset = float(0.0)
Fill_value = float(-9999)
outdir="F:\\DB_test_data\\VAR4\\"
for a,b,c,d,e,f,g in zip (CLDMASK ,NDVI ,BAND1 ,BAND2 ,BAND3 ,BAND4,AOD):
print ("processing:"+ a)
arcpy.AddMessage("processing:{}".format(b.split('\\')[4][9:30]))
name = c.split("\\")
filename=name[6][9:33]
print filename
arcpy.AddMessage("processing:{}".format(d.split('\\')[6][9:33]))
arcpy.AddMessage("processing:{}".format(e.split('\\')[6][9:30]))
arcpy.AddMessage("processing:{}".format(f.split('\\')[6][9:33]))
print ("processing:"+ g)
#######################################################################
setnull =arcpy.gp.SetNull_sa(g,g, "in_memory/dat", "\"Value\" = -9999")
ras=arcpy.Raster(setnull)
Data=(ras-add_offset)*Scale_factor
da = Con((Data >= 0.0) & (Data <= 0.1),1)
# Process: Extract by Cloud_Mask
tempEnvironment0 = arcpy.env.cellSize
arcpy.env.cellSize = "MAXOF"
ndvi_msk=arcpy.gp.ExtractByMask_sa(b, a)
Band1_mask=arcpy.gp.ExtractByMask_sa(c, a)
Band2_mask=arcpy.gp.ExtractByMask_sa(d, a)
Band3_mask=arcpy.gp.ExtractByMask_sa(e, a)
Band4_mask=arcpy.gp.ExtractByMask_sa(f, a)
arcpy.env.cellSize = tempEnvironment0
# Process: Extract by Mask using AOD less than 0.1 value
tempEnvironment0 = arcpy.env.cellSize
arcpy.env.cellSize = cellsize
ndvi_msk1=arcpy.gp.ExtractByMask_sa(ndvi_msk, da)
Band1_mask1=arcpy.gp.ExtractByMask_sa(Band1_mask, da)
Band2_mask2=arcpy.gp.ExtractByMask_sa(Band2_mask, da)
Band3_mask3=arcpy.gp.ExtractByMask_sa(Band2_mask, da)
Band4_mask4=arcpy.gp.ExtractByMask_sa(Band2_mask, da)
arcpy.env.cellSize = tempEnvironment0
try:
# Raster to point
field ="VALUE"
Point=arcpy.RasterToPoint_conversion(ndvi_msk1,"in_memory/fileroot",field)
shape=arcpy.sa.ExtractMultiValuesToPoints(Point,[[ndvi_msk1,"NDVI"],[Band1_mask1,"Band1"],[Band2_mask2,"Band2"],
[Band3_mask3 ,"Band3"],[Band4_mask4,"Band4"]], "NONE")
#Add Two Fields Name and Name2
[arcpy.AddField_management(shape,field_name, "TEXT", field_length = 50)
for field_name in ["Name", "Name2"]]
arcpy.AddMessage("Successfully Added NAME fields ")
fieldss = ['Band1','Name']
with arcpy.da.UpdateCursor(shape, fieldss) as cursor:
for row in cursor:
if (row[0] > 0.02 and row[0] < 0.06):
row[1] = 'AAA'
print ("processing1:""{0:.8f}, {1}".format(row[0], row[1]))
elif (row[0] >= 0.06 and row[0] < 0.09):
row[1] = 'BBB'
# print ("processing1:""{0:.8f}, {1}".format(row[0], row[1]))
elif (row[0] >= 0.09):
row[1] = 'CCC'
else:
row[1] = 'NA'
# Update the cursor with the updated list
cursor.updateRow(row)
# Process: Select Layer By Attribute for separate the catagories "AAA","BBB,"CCC"
arcpy.MakeFeatureLayer_management(shape,"lyr1")
AAA = arcpy.SelectLayerByAttribute_management("lyr1", "NEW_SELECTION", "\"Name\" = 'AAA'")
print "Successfully Process : Select Layer By Attribute for AAA "
arcpy.MakeFeatureLayer_management(shape,"lyr2")
BBB = arcpy.SelectLayerByAttribute_management("lyr2", "NEW_SELECTION", "\"Name\" = 'BBB'")
print "Successfully Process : Select Layer By Attribute for BBB "
arcpy.MakeFeatureLayer_management(shape,"lyr3")
CCC = arcpy.SelectLayerByAttribute_management("lyr3", "NEW_SELECTION", "\"Name\" = 'CCC'")
print "Successfully Process : Select Layer By Attribute for CCC "
# Writing tables for categories("AAA","BBB,"CCC") and individual category have three Land type
# that will be saved in different folder and their individual name
#------------------First we write category "AAA" and their individual land type--------------------------------------#
category="AAA"
file_name=filename
base_path = r"F:\\DB_test_data\\VAR4\\"
def geoprocess1(base_path,category,file_name):
try:
land_use='LAND_10'
outdir = os.path.join(base_path,category,land_use)
if not os.path.exists(outdir):
try:
print('\nCreating new output directory!\n'+outdir+'\n')
os.makedirs(outdir)
except OSError:
print ("Creation of the directory failed" )
else:
print ("Successfully created the directory" )
ras_name = os.path.join(outdir,("{0}_{1}{2}_{3}".format(category,file_name,land_use,".txt")))
fields = ['grid_code', 'Band1', 'Band2','Band3','Name',"Name2"]
with open(ras_name, 'wb') as txtfile:
arcpy.AddMessage("fields Writing For landuse 10")
txtfile.write(" {0} {1} {2} {3} {4} {5}\n".format(fields[0],fields[1],fields[2],fields[3],fields[4],fields[5]))
with arcpy.da.UpdateCursor(AAA, fields) as cursor:
for row in cursor:
if (row[0] > 0.10 and row[0] < 0.20):
row[5] = 'landuse_10'
print ("processing_A:""{0:.8f} {1:.8f} {2:.8f} {3:.8f} {4} {5}".format(row[0],row[1],row[2], row[3],row[4],row[5]))
txtfile.write(" {0:.8f} {1:.8f} {2:.8f} {3:.8f} {4} {5}\n".format(row[0],row[1],row[2], row[3],row[4],row[5]))
txtfile.close()
print ("Successfully save the landuse 10 " )
print('Sucess: geoprocess1 complete!')
except Exception as err:
print('Error: unable to complete geoprocess1')
print(err)
def geoprocess2(base_path,category,file_name):
try:
land_use='LAND_20'
outdir = os.path.join(base_path,category,land_use)
if not os.path.exists(outdir):
try:
print('\nCreating new output directory!\n'+outdir+'\n')
os.makedirs(outdir)
except OSError:
print ("Creation of the directory failed" )
else:
print ("Successfully created the directory" )
ras_name = os.path.join(outdir,("{0}_{1}{2}_{3}".format(category,file_name,land_use,".txt")))
fields = ['grid_code', 'Band1', 'Band2','Band3','Name',"Name2"]
with open(ras_name, 'wb') as txtfile:
arcpy.AddMessage("fields Writing For landuse 20")
txtfile.write(" {0} {1} {2} {3} {4} {5}\n".format(fields[0],fields[1],fields[2],fields[3],fields[4],fields[5]))
with arcpy.da.UpdateCursor(AAA, fields) as cursor:
for row in cursor:
if (row[0] >= 0.20 and row[0] < 0.50):
row[5] = 'landuse_20'
print ("processing_B:""{0:.8f} {1:.8f} {2:.8f} {3:.8f} {4} {5}".format(row[0],row[1],row[2], row[3],row[4],row[5]))
txtfile.write(" {0:.8f} {1:.8f} {2:.8f} {3:.8f} {4} {5}\n".format(row[0],row[1],row[2], row[3],row[4],row[5]))
txtfile.close()
print ("Successfully save the landuse 20 " )
print('Sucess: geoprocess2 complete!')
except Exception as err:
print('Error: unable to complete geoprocess2')
print(err)
def geoprocess3(base_path,category,file_name):
try:
land_use='LAND_30'
outdir = os.path.join(base_path,category,land_use)
if not os.path.exists(outdir):
try:
print('\nCreating new output directory!\n'+outdir+'\n')
os.makedirs(outdir)
except OSError:
print ("Creation of the directory failed" )
else:
print ("Successfully created the directory" )
ras_name = os.path.join(outdir,("{0}_{1}{2}_{3}".format(category,file_name,land_use,".txt")))
fields = ['grid_code', 'Band1', 'Band2','Band3','Name',"Name2"]
with open(ras_name, 'wb') as txtfile:
arcpy.AddMessage("fields Writing For landuse 30")
txtfile.write(" {0} {1} {2} {3} {4} {5}\n".format(fields[0],fields[1],fields[2],fields[3],fields[4],fields[5]))
with arcpy.da.UpdateCursor(AAA, fields) as cursor:
for row in cursor:
if (row[0] >= 0.50):
row[5] = 'landuse_30'
print ("processing_C:""{0:.8f} {1:.8f} {2:.8f} {3:.8f} {4} {5}".format(row[0],row[1],row[2], row[3],row[4],row[5]))
txtfile.write(" {0:.8f} {1:.8f} {2:.8f} {3:.8f} {4} {5}\n".format(row[0],row[1],row[2], row[3],row[4],row[5]))
txtfile.close()
print ("Successfully save the landuse 30 " )
print('Sucess: geoprocess3 complete!')
except Exception as err:
print('Error: unable to complete geoprocess3')
print(err)
def main(base_path,category,file_name):
geoprocess1(base_path,category,file_name)
geoprocess2(base_path,category,file_name)
geoprocess3(base_path,category,file_name)
if __name__ == "__main__":
main(base_path,category,file_name)
print "Code is OK"
except:
print "Error in Code"
print arcpy.GetMessages()
print 'finished run: %s\n\n' % (datetime.datetime.now() - start)
... View more
06-03-2019
03:53 AM
|
0
|
1
|
2157
|
|
POST
|
Middle section of the code, I separate the attribute value in a different category ("AAA","BBB","CCC") and every category have Ten land-use types, initially, I set Three land use type (LAND_10, LAND_20, LAND_30) for test code. Now I used the update cursor for different land-use type(LAND_10, LAND_20, LAND_30) , and saved the text file to there corresponding Land use folders(Like, Land_10 ....Land_30) under the "AAA" folder. similarly, this function I will use for categories "BBB","CCC" . Initially, I write this code for category "AAA" but I want to use this function for category "BBB","CCC" by defining a function(uncheck all define function), but is not working. Below is my code please check it. First of all, I want to reduce my code (Because I have 10 land type initially I write three) and the whole function I want to use as a defined function for a different category. Another thing is Multiprocessing, Multithreading I want to use this function for the code because of initially my code takes much more time for one processes. In my case how to use the multiprocessing function. please look at this matter and give me a suggestion to manipulate this code. import datetime
start = datetime.datetime.now()
print 'start run: %s\n' % (start)
import arcpy ,os ,sys,csv,errno
from arcpy import env
from arcpy.sa import *
import datetime
import re
import glob
import itertools
arcpy.env.overwriteOutput = True
import multiprocessing as mp
print("Number of processors: ", mp.cpu_count())
cellsize = "F:\\DB_test_data\\TEST_RAY\\TEST1\\MOD02HKM_A2017001_0530_NDVI_AA.img"
d1="F:\\DB_test_data\\VAR2\\cldmask\\tt"
CLDMASK = glob.glob(d1 + os.sep + "*.Aerosol_Cldmask_Land_Ocean-Aerosol_Cldmask_Land_Ocean.tif")
CLDMASK.sort()
if CLDMASK is None:
print 'Could not open the CLDMASK raster files'
sys.exit(1)
else:
print 'The CLDMASK raster files was opened successfully'
#print CLDMASK
d2="F:\\DB_test_data\\TEST_RAY\\TEST1"
NDVI = glob.glob(d2 + os.sep + "*A2017001_0530_NDVI_AA.img")
NDVI.sort()
if NDVI is None:
print 'Could not open the NDVI raster files'
sys.exit(1)
else:
print 'The NDVI raster files was opened successfully'
#print NDVI
d3="F:\\DB_test_data\\VAR2\\IDL\\MOD02HKM\\TEST"
BAND1 = glob.glob(d3 + os.sep + "*A2017001_0530_006_BAND_1.img")
BAND1.sort()
if BAND1 is None:
print 'Could not open the BAND1 raster files'
sys.exit(1)
else:
print 'The BAND1 raster files was opened successfully'
#print BAND1
d4="F:\\DB_test_data\\VAR2\\IDL\\MOD02HKM\\TEST"
BAND2 = glob.glob(d4 + os.sep + "*A2017001_0530_006_BAND_2.img")
BAND2.sort()
if BAND2 is None:
print 'Could not open the BAND2 raster files'
sys.exit(1)
else:
print 'The BAND2 raster files was opened successfully'
#print BAND2
d4="F:\\DB_test_data\\VAR2\\IDL\\MOD09\\TEST"
BAND3 = glob.glob(d4 + os.sep + "*A2017001_0530_006_BAND_3.img")
BAND3.sort()
if BAND3 is None:
print 'Could not open the BAND3 raster files'
sys.exit(1)
else:
print 'The BAND3 raster files was opened successfully'
#print BAND3
d5="F:\\DB_test_data\\VAR2\\IDL\\MOD02HKM\\TEST"
BAND4 = glob.glob(d5 + os.sep + "*A2017001_0530_006_BAND_4.img")
BAND4.sort()
if BAND4 is None:
print 'Could not open the BAND4 raster files'
sys.exit(1)
else:
print 'The BAND4 raster files was opened successfully'
#print BAND4
d6 = r"F:\\DB_test_data\\VAR2\\MOD04_l2"
AOD = glob.glob(d6 + os.sep + "*Corrected_Optical_Depth_Land_2-Corrected_Optical_Depth_Land.tif")
AOD.sort()
if AOD is None:
print 'Could not open the AOD raster files'
sys.exit(1)
else:
print 'The AOD raster files was opened successfully'
Scale_factor = float(0.0010000000474974513)
add_offset = float(0.0)
Fill_value = float(-9999)
outdir="F:\\DB_test_data\\VAR4\\"
for a,b,c,d,e,f,g in zip (CLDMASK ,NDVI ,BAND1 ,BAND2 ,BAND3 ,BAND4,AOD):
print ("processing:"+ a)
arcpy.AddMessage("processing:{}".format(b.split('\\')[4][9:30]))
name = c.split("\\")
filename=name[6][9:33]
print filename
arcpy.AddMessage("processing:{}".format(d.split('\\')[6][9:33]))
arcpy.AddMessage("processing:{}".format(e.split('\\')[6][9:30]))
arcpy.AddMessage("processing:{}".format(f.split('\\')[6][9:33]))
print ("processing:"+ g)
#######################################################################
setnull =arcpy.gp.SetNull_sa(g,g, "in_memory/dat", "\"Value\" = -9999")
ras=arcpy.Raster(setnull)
Data=(ras-add_offset)*Scale_factor
da = Con((Data >= 0.0) & (Data <= 0.1),1)
# Process: Extract by Cloud_Mask
tempEnvironment0 = arcpy.env.cellSize
arcpy.env.cellSize = "MAXOF"
ndvi_msk=arcpy.gp.ExtractByMask_sa(b, a)
Band1_mask=arcpy.gp.ExtractByMask_sa(c, a)
Band2_mask=arcpy.gp.ExtractByMask_sa(d, a)
Band3_mask=arcpy.gp.ExtractByMask_sa(e, a)
Band4_mask=arcpy.gp.ExtractByMask_sa(f, a)
arcpy.env.cellSize = tempEnvironment0
# Process: Extract by Mask using AOD less than 0.1 value
tempEnvironment0 = arcpy.env.cellSize
arcpy.env.cellSize = cellsize
ndvi_msk1=arcpy.gp.ExtractByMask_sa(ndvi_msk, da)
Band1_mask1=arcpy.gp.ExtractByMask_sa(Band1_mask, da)
Band2_mask2=arcpy.gp.ExtractByMask_sa(Band2_mask, da)
Band3_mask3=arcpy.gp.ExtractByMask_sa(Band2_mask, da)
Band4_mask4=arcpy.gp.ExtractByMask_sa(Band2_mask, da)
arcpy.env.cellSize = tempEnvironment0
try:
# Raster to point
field ="VALUE"
Point=arcpy.RasterToPoint_conversion(ndvi_msk1,"in_memory/fileroot",field)
shape=arcpy.sa.ExtractMultiValuesToPoints(Point,[[ndvi_msk1,"NDVI"],[Band1_mask1,"Band1"],[Band2_mask2,"Band2"],
[Band3_mask3 ,"Band3"],[Band4_mask4,"Band4"]], "NONE")
#Add Two Fields Name and Name2
[arcpy.AddField_management(shape,field_name, "TEXT", field_length = 50)
for field_name in ["Name", "Name2"]]
arcpy.AddMessage("Successfully Added NAME fields ")
fieldss = ['Band1','Name']
with arcpy.da.UpdateCursor(shape, fieldss) as cursor:
for row in cursor:
if (row[0] > 0.02 and row[0] < 0.06):
row[1] = 'AAA'
print ("processing1:""{0:.8f}, {1}".format(row[0], row[1]))
elif (row[0] >= 0.06 and row[0] < 0.09):
row[1] = 'BBB'
# print ("processing1:""{0:.8f}, {1}".format(row[0], row[1]))
elif (row[0] >= 0.09):
row[1] = 'CCC'
else:
row[1] = 'NA'
# Update the cursor with the updated list
cursor.updateRow(row)
# Process: Select Layer By Attribute for separate the catagories "AAA","BBB,"CCC"
arcpy.MakeFeatureLayer_management(shape,"lyr1")
AAA = arcpy.SelectLayerByAttribute_management("lyr1", "NEW_SELECTION", "\"Name\" = 'AAA'")
print "Successfully Process : Select Layer By Attribute for AAA "
arcpy.MakeFeatureLayer_management(shape,"lyr2")
BBB = arcpy.SelectLayerByAttribute_management("lyr2", "NEW_SELECTION", "\"Name\" = 'BBB'")
print "Successfully Process : Select Layer By Attribute for BBB "
arcpy.MakeFeatureLayer_management(shape,"lyr3")
CCC = arcpy.SelectLayerByAttribute_management("lyr3", "NEW_SELECTION", "\"Name\" = 'CCC'")
print "Successfully Process : Select Layer By Attribute for CCC "
# Writing tables for categories("AAA","BBB,"CCC") and individual category have three Land type
# that will be save in different folder and their individual name
#------------------First we write category "AAA" and their indiviual land type--------------------------------------#
category="AAA"
file_name=filename
base_path = r"F:\\DB_test_data\\VAR4\\"
#def TableTotext(Input_Data,base_path,category,file_name):
#def Land_10():
land_use='LAND_10'
outdir = os.path.join(base_path,category,land_use)
if not os.path.exists(outdir):
print('\nCreating new output directory!\n'+outdir+'\n')
os.makedirs(outdir)
else:
print ("Directory already exists" )
ras_name = os.path.join(outdir,("{0}_{1}{2}_{3}".format(category,file_name,land_use,".txt")))
fields = ['NDVI', 'Band1', 'Band2','Band3','Name',"Name2"]
with open(ras_name, 'wb') as txtfile:
arcpy.AddMessage("fields Writing For landuse 10")
txtfile.write(" {0} {1} {2} {3} {4} {5}\n".format(fields[0],fields[1],fields[2],fields[3],fields[4],fields[5]))
with arcpy.da.UpdateCursor(AAA, fields) as cursor:
for row in cursor:
if (row[0] > 0.10 and row[0] < 0.20):
row[5] = 'landuse_10'
print ("processing_A:""{0:.8f} {1:.8f} {2:.8f} {3:.8f} {4} {5}".format(row[0],row[1],row[2], row[3],row[4],row[5]))
txtfile.write("{0:.8f} {1:.8f} {2:.8f} {3:.8f} {4} {5}\n".format(row[0],row[1],row[2], row[3],row[4],row[5]))
txtfile.close()
print ("Successfully save the landuse 10 " )
#return Land_10
#def Land_20():
land_use='LAND_20'
outdir = os.path.join(base_path,category,land_use)
if not os.path.exists(outdir):
print('\nCreating new output directory!\n'+outdir+'\n')
os.makedirs(outdir)
else:
print ("Directory already exists" )
ras_name = os.path.join(outdir,("{0}_{1}{2}_{3}".format(category,file_name,land_use,".txt")))
fields = ['NDVI', 'Band1', 'Band2','Band3','Name',"Name2"]
with open(ras_name, 'wb') as txtfile:
arcpy.AddMessage("fields Writing For landuse 20")
txtfile.write(" {0} {1} {2} {3} {4} {5}\n".format(fields[0],fields[1],fields[2],fields[3],fields[4],fields[5]))
with arcpy.da.UpdateCursor(AAA, fields) as cursor:
for row in cursor:
if (row[0] >= 0.20 and row[0] < 0.50):
row[5] = 'landuse_20'
print ("processing_B:""{0:.8f} {1:.8f} {2:.8f} {3:.8f} {4} {5}".format(row[0],row[1],row[2], row[3],row[4],row[5]))
txtfile.write("{0:.8f} {1:.8f} {2:.8f} {3:.8f} {4} {5}\n".format(row[0],row[1],row[2], row[3],row[4],row[5]))
txtfile.close()
print ("Successfully save the landuse 20 " )
#return Land_20
#def Land_30():
land_use='LAND_30'
outdir = os.path.join(base_path,category,land_use)
if not os.path.exists(outdir):
print('\nCreating new output directory!\n'+outdir+'\n')
os.makedirs(outdir)
else:
print ("Directory already exists" )
ras_name = os.path.join(outdir,("{0}_{1}{2}_{3}".format(category,file_name,land_use,".txt")))
fields = ['NDVI', 'Band1', 'Band2','Band3','Name',"Name2"]
with open(ras_name, 'wb') as txtfile:
arcpy.AddMessage("fields Writing For landuse 30")
txtfile.write(" {0} {1} {2} {3} {4} {5}\n".format(fields[0],fields[1],fields[2],fields[3],fields[4],fields[5]))
with arcpy.da.UpdateCursor(AAA, fields) as cursor:
for row in cursor:
if (row[0] >= 0.50):
row[5] = 'landuse_30'
print ("processing_C:""{0:.8f} {1:.8f} {2:.8f} {3:.8f} {4} {5}".format(row[0],row[1],row[2], row[3],row[4],row[5]))
txtfile.write("{0:.8f} {1:.8f} {2:.8f} {3:.8f} {4} {5}\n".format(row[0],row[1],row[2], row[3],row[4],row[5]))
txtfile.close()
print ("Successfully save the landuse 30 " )
#return LAND_30
#------------------Second we write category "BBB" and their indiviual land type--------------------------------------#
print "Code is OK"
except:
print "Error in Code"
print arcpy.GetMessages()
print 'finished run: %s\n\n' % (datetime.datetime.now() - start)
... View more
06-02-2019
07:48 AM
|
0
|
41
|
8573
|
|
POST
|
I am trying to write this code but getting some error in the loop please see the error. If anybody knows how to solve this issue then give me a suggestion to resolve this issue. Traceback (most recent call last):
File "F:\DB_test_data\python_script\DB_TEST2.py", line 117, in <module>
for o,x,y,z in [(o,x,y,z) for o in Band1_mask1 for x in Band2_mask2 for y in Band3_mask3 for z in Band4_mask4]:
TypeError: 'geoprocessing server result object' object is not iterable import datetime
start = datetime.datetime.now()
print 'start run: %s\n' % (start)
import arcpy ,os ,sys
from arcpy import env
from arcpy.sa import *
import datetime
import glob
arcpy.env.overwriteOutput = True
d1="F:\\DB_test_data\\VAR2\\cldmask\\tt"
CLDMASK = glob.glob(d1 + os.sep + "*.Aerosol_Cldmask_Land_Ocean-Aerosol_Cldmask_Land_Ocean.tif")
CLDMASK.sort()
print CLDMASK
d2="F:\\DB_test_data\\TEST_RAY\\TEST1"
NDVI = glob.glob(d2 + os.sep + "*A2017001_0530_NDVI_AA.img")
NDVI.sort()
print NDVI
d3="F:\\DB_test_data\\VAR2\\IDL\\MOD02HKM\\TEST"
BAND1 = glob.glob(d3 + os.sep + "*A2017001_0530_006_BAND_1.img")
BAND1.sort()
print BAND1
d4="F:\\DB_test_data\\VAR2\\IDL\\MOD02HKM\\TEST"
BAND2 = glob.glob(d4 + os.sep + "*A2017001_0530_006_BAND_2.img")
BAND2.sort()
print BAND2
d5="F:\\DB_test_data\\VAR2\\IDL\\MOD09\\TEST"
BAND3 = glob.glob(d5 + os.sep + "*A2017001_0530_006_BAND_3.img")
BAND3.sort()
print BAND3
d6="F:\\DB_test_data\\VAR2\\IDL\\MOD02HKM\\TEST"
BAND4 = glob.glob(d6 + os.sep + "*A2017001_0530_006_BAND_4.img")
BAND4.sort()
print BAND4
for o,x,y,z in [(o,x,y,z)for CL in CLDMASK for ND in NDVI for B1 in BAND1 for B2 in BAND2 for B3 in BAND3 for B4 in BAND4]:
print ("processing:"+ CL)
print ("processing:"+ ND)
print ("processing:"+ B1)
print ("processing:"+ B2)
print ("processing:"+ B3)
print ("processing:"+ B4)
# Process: Extract by Mask
tempEnvironment0 = arcpy.env.cellSize
arcpy.env.cellSize = "MAXOF"
arcpy.gp.ExtractByMask_sa([ND,B1,B2,B3,B4], CL)
arcpy.env.cellSize = tempEnvironment0
print arcpy.GetMessages()
print 'finished run: %s\n\n' % (datetime.datetime.now() - start)
... View more
05-20-2019
06:08 AM
|
0
|
0
|
859
|
|
POST
|
Please help me, anyone, to resolve this matter, I am waiting for Replies.
... View more
05-14-2019
02:37 AM
|
0
|
0
|
859
|
|
POST
|
I have one problem regarding call different file in a single for loop. Basically, I want to mask six images(NDVI, BAND1....BAND5 of Modis sensor) by a single cloud mask file. For every time step, it will be changed (The cloud mask image plus 6 images). But the code is not working, in this case, I am not sure how to write this function but I try to write this code. @ import datetime
start = datetime.datetime.now()
print 'start run: %s\n' % (start)
import arcpy ,os ,sys
from arcpy import env
from arcpy.sa import *
import datetime
import glob
arcpy.env.overwriteOutput = True
d1="F:\\DB_test_data\\VAR2\\cldmask\\tt"
CLDMASK = glob.glob(d1 + os.sep + "*.Aerosol_Cldmask_Land_Ocean-Aerosol_Cldmask_Land_Ocean.tif")
CLDMASK.sort()
print CLDMASK
d2="F:\\DB_test_data\\TEST_RAY\\TEST1"
NDVI = glob.glob(d2 + os.sep + "*A2017001_0530_NDVI_AA.img")
NDVI.sort()
print NDVI
d3="F:\\DB_test_data\\VAR2\\IDL\\MOD02HKM\\TEST"
BAND1 = glob.glob(d3 + os.sep + "*A2017001_0530_006_BAND_1.img")
BAND1.sort()
print BAND1
d4="F:\\DB_test_data\\VAR2\\IDL\\MOD02HKM\\TEST"
BAND2 = glob.glob(d4 + os.sep + "*A2017001_0530_006_BAND_2.img")
BAND2.sort()
print BAND2
d4="F:\\DB_test_data\\VAR2\\IDL\\MOD09\\TEST"
BAND3 = glob.glob(d4 + os.sep + "*A2017001_0530_006_BAND_3.img")
BAND3.sort()
print BAND3
d5="F:\\DB_test_data\\VAR2\\IDL\\MOD02HKM\\TEST"
BAND4 = glob.glob(d5 + os.sep + "*A2017001_0530_006_BAND_4.img")
BAND4.sort()
print BAND4
for CL,ND,B1,B2,B3,B4 in CLDMASK,NDVI,BAND1,BAND2,BAND3,BAND4:
print ("processing:"+ CL)
print ("processing:"+ ND)
print ("processing:"+ B1)
print ("processing:"+ B2)
print ("processing:"+ B3)
print ("processing:"+ B4)
# Process: Extract by Mask
tempEnvironment0 = arcpy.env.cellSize
arcpy.env.cellSize = "MAXOF"
arcpy.gp.ExtractByMask_sa([ND,B1,B2,B3,B4], CL)
arcpy.env.cellSize = tempEnvironment0
print arcpy.GetMessages()
print 'finished run: %s\n\n' % (datetime.datetime.now() - start) After that, I want to extract the pixels wise NDVI value(categorize NDVI range like this(please look below)) and their corresponding band's pixel values and it will be saved in a table(Three tables based on NDVI categories)at a .text format in every time step of for loop. I am confused about how to write this code please help me to resolve this matter. 0.10<NDVI<0.20 (Category 1) NDVI band1 band2 band4 band5 0.190 0.311 2.11601 0.1000 0.3377 0.199 0.312 2.1100 0.1007 0 .3375 . . . . . . . . . . 0.20<NDVI<0.50 (Category 2) NDVI band1 band2 band4 band5 . . . . . 0.50<NDVI (Category 3) NDVI band1 band2 band4 band5 Please look at this matter . . . . .
... View more
05-11-2019
02:56 AM
|
0
|
2
|
1071
|
|
POST
|
Code working well. Thank you so much, sir, for giving me the time to solve this problem.
... View more
10-08-2018
01:09 AM
|
0
|
1
|
716
|
|
POST
|
I mean, I want to do another process that will be day wise system and save those every raster file by their year, month, date, name.
... View more
10-07-2018
01:15 PM
|
0
|
0
|
716
|
|
POST
|
Its ok but my question is how to extract the date from the Julian day that I want to save with raster name.
... View more
10-07-2018
12:59 PM
|
0
|
1
|
2275
|
|
POST
|
Thank you, sir, code working pretty well. I have some question If I run this code from 2015 to 2018 then such kind of modification will work? rasters = { 2015: {1:[],2:[],3:[],4:[],5:[],6:[],7:[],8:[],9:[],10:[],11:[],12:[]},
2016: {1:[],2:[],3:[],4:[],5:[],6:[],7:[],8:[],9:[],10:[],11:[],12:[]}
2017: {1:[],2:[],3:[],4:[],5:[],6:[],7:[],8:[],9:[],10:[],11:[],12:[]}
2018: {1:[],2:[],3:[],4:[],5:[],6:[],7:[],8:[],9:[],10:[],11:[],12:[]}
} Second things I want to use the "Null_sc" function for every raster file and save it by their "YEAR/MONTH/DATE/AOD" name. Then what kind of modification do I do in the code? or your suggestion. def Null_sc(input,output):
for ds in input:
print ds
setnull =arcpy.gp.SetNull_sa(ds,ds, "in_memory/dat", "\"Value\" < 0")
ras=arcpy.Raster(setnull)
scalefactor=float(0.001)
scale=scalefactor*ras
save.scale(output)
... View more
10-07-2018
12:08 AM
|
0
|
1
|
2275
|
|
POST
|
def Null_sc_mean(input,output):
SCM00_000=[]
if len(input)!= 0:
for ds in input:
print ds
dat=str(ds[59:(len(ds)-113)])
setnull =arcpy.gp.SetNull_sa(ds,ds, "in_memory/dat", "\"Value\" < 0")
ras=arcpy.Raster(setnull)
scalefactor=float(0.001)
scale=scalefactor*ras
SCM00_000.append(scale)
if len(SCM00_000)!=0:
arcpy.gp.CellStatistics_sa(SCM00_000,output,"MEAN", "DATA")
extra=1
m3 ="G:\\ANFIS_FINAL\\AOD\\3KM_AOD\\01_18\\"
for ls in M01,M02,M03,M04,M05,M06,M07,M08,M09,M10,M11,M12:
out1= m3+'{0}.img'.format("Month"+ str(extra)+str(yer))
Null_sc_mean(ls,out1)
extra += 1
if extra==13:
yer+=0001 for ls in M01,M02,M03,M04,M05,M06,M07,M08,M09,M10,M11,M12: I want to avoid this function because this is so complex and errors. I have 2015 to 2018 datasets, some month have no data in this situation when I save this data by the loop after complecting Mean I think it will give me the error message. I want to save those months in which they have data. mon1 = Null_sc_mean(v2,out1) mon1 nothing, just silly mistake I will test your code tomorrow.
... View more
10-06-2018
01:04 PM
|
0
|
2
|
2275
|
|
POST
|
Traceback (most recent call last):
File "<module1>", line 33, in <module>
AttributeError: 'module' object has no attribute 'strptime' This code getting me this error. Can you explain line 23 and 30 to 35? Actually, I have 2015 to 2018 datasets I want to calculate the mean of each month and save it by their month and year name. Befor we go to the mean calculation have some additional calculation that I mention it in first.
... View more
10-06-2018
09:50 AM
|
0
|
1
|
2275
|
| Online Status |
Offline
|
| Date Last Visited |
11-11-2020
02:24 AM
|