I am working on a project for my beginners' Python class and have gotten a little stuck. I have three .tif files that I want to do Zonal Statistics for, but I am getting an error. Here is my script:
import arcpy
import os
from arcpy import env
from arcpy.sa import *
env.workspace = r'C:\Users\alvaremi\Documents\Final Project_Python'
path = r'C:\Users\alvaremi\Documents\Final Project_Pythonn'
env.overwriteOutput = 1
arcpy.CheckOutExtension('Spatial')
in_zone_data = 'counties_in_cog.shp'
zone_field = 'NAME'
impervious = os.listdir(env.workspace + '\ImpvClipped')
print impervious
for year in impervious:
if year.endswith(".tif"):
outZonalStatistics = ZonalStatistics(in_zone_data, zone_field, year, "MEAN", "NODATA")
outZonalStatistics.save(year[:8] + 'zonalstats')
print 'Done'
When I run it, I get this error:
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000865: Input value raster: 2001impvclipped.tif does not exist.
Failed to execute (ZonalStatistics).
I am also unsure of how to save the new files so that they keep the date on them. The files I want to run the Zonal Stats on are "2001impclipped", "2006impclipped", and "2011impclipped".
Thanks!
You need to add the full directory path to the filename in order for Python to find it.
fileName = env.workspace + '\ImpvClipped\' + year
ZonalStatistics(in_zone_data, zone_field, fileName, "MEAN", "NODATA")
Related
My target is to create a script tool using python where I will select location by area and export the area in the directory. To do so I have used this script
import arcpy
arcpy.env.overwriteOutput = True
try:
inPath = arcpy.GetParameterAsText(0)
inpath2 = arcpy.GetParameterAsText(1)
outPath = arcpy.GetParameterAsText(2)
selection = arcpy.SelectLayerByLocation_management("inPath", "WITHIN_A_DISTANCE", "inpath2", "50 meter","NEW_SELECTION","NOT_INVERT")
copySelection = arcpy.CopyFeatures_management(selection,outPath)
arcpy.GetMessage("All Done")
except:
arcpy.AddError("Could not colplete the process")
arcpy.AddMessage(arcpy.GetMessages())
but running this code in scripting tool toll look like thisgive this error messageError given after running the tool. :Please help me to solve this issue.
Thanks in advance.
I belive you are mistaking the syntax, although the data you are working with is not clear to me, and you may need to provide more details. Anyway try this code out and let me know if it helped you with your project.
import arcpy
arcpy.env.overwriteOutput = True
try:
inPath = arcpy.GetParameterAsText(0)
inpath2 = arcpy.GetParameterAsText(1)
outPath = arcpy.GetParameterAsText(2)
selection = arcpy.management.SelectLayerByLocation("inPath", "WITHIN_A_DISTANCE", "inpath2", "50 meter","NEW_SELECTION","NOT_INVERT")
copySelection = arcpy.management.CopyFeatures(selection,outPath)
arcpy.GetMessage("All Done")
except:
arcpy.AddError("Could not colplete the process")
arcpy.AddMessage(arcpy.GetMessages())
if it didn't work for you your problem is copying the features and instead of copying just try
arcpy.conversion.FeatureClassToFeatureClass(in_features, out_path, out_name, {where_clause}, {field_mapping}, {config_keyword})
That will solve your error.
I am trying to learn the use of gravityai and frankly i am a bit new to this. For that i followed https://www.youtube.com/watch?v=i6qL3NqFjs4 from Ania Kubow. When i do this, at the end i encounter the error message. This message appears in gravity ai, when trying to run the job, i.e. after uploading all zipped files three .pkl files, one .py file, one .txt file, one .json file), after docker is initialized and run:
Error running executable: usage: classify_financial_articles.py [-h] {run,serve} ... classify_financial_articles.py: error: argument subcommand: invalid choice: '/tmp/gai_temp/0675f15ca0b04cf98071474f19e38f3c/76f5cdc86a1241af8c01ce1b4d441b0c' (choose from 'run', 'serve').
I do not understand the error message and therefore cannot fix it. Is it an error in the code? or in the configuration on the gravityai platform? At no point do i run the .py file explicitly so i conclude, that it must be from the gravityai. Yet i dont get the error. Can anyone help me?
i added the .py file, as it is the one throwing the error
from gravityai import gravityai as grav
import pickle
import pandas as pd
model = pickle.load(open('financial_text_classifier.pkl', 'rb'))
tfidf_vectorizer = pickle.load(open('financial_text_vectorizer.pkl','rb'))
label_encder = pickle.load(open('financial_text_encoder.pkl', 'rb'))
def process(inPath, outPath):
# read csv input file
input_df = pd.read_csv(inPath)
# read the data
features = tfidf_vectorizer.transform(input_df['body'])
# predict classes
predictions = model.predict(features)
#convert outpulabels to categories
input_df['category'] = label_encder.inverse_transform(predictions)
#save results to csv
output_df = input_df(['id', 'category'])
output_df.csv(outPath, index=False)
grav.wait_for_requests(process)
I can't find any errors in the .py file
The error you got comes from the line that imports gravityai library:
from gravityai import gravityai as grav
I believe you need to upload your projet to the gravityai platform and then you will be able to test it
To test localy you need to:
1)Add line"import sys". 2)Comment or delete line"grav.wait_for_requests(process)" 3)Add line: "process(inPath=sys.argv[2], outPath=sys.argv[3])". 4)Run from command line "python classify_financial_articles.py run test_set.csv test_set_out.csv"
Example of code:
from gravityai import gravityai as grav
import pickle
import pandas as pd
import sys
model = pickle.load(open('financial_text_clasifier.pkl', 'rb'))
tfidf_vectorizer = pickle.load(open('financial_text_vectorizer.pkl', 'rb'))
label_encoder = pickle.load(open('financial_text_encoder.pkl', 'rb'))
def process(inPath, outPath):
input_df = pd.read_csv(inPath)
features = tfidf_vectorizer.transform(input_df['body'])
predictions = model.predict(features)
input_df['category'] = label_encoder.inverse_transform(predictions)
output_df = input_df[['id', 'category']]
output_df.to_csv(outPath, index=False)
process(inPath=sys.argv[2], outPath=sys.argv[3])
I am trying to create a python def to unzip a few .gz files within a folder. I know that the main script works if it is not in the def. The script I have created is similar to others I have done but this one give me the error
File "unzip.py", line 24, in
decompressed_files(input_folder)
NameError: name 'input_folder' is not defined
I copied the script below so someone can help me to see where the error is. I haven't done any BioInformatics for the last couple of years and I am a bit rusty.
import glob
import sys
import os
import argparse
import subprocess
import gzip
def decompressed_files(input_folder):
print ('starting decompressed_files')
output_folder=input_folder + '/fasta_files'
if os.path.exists(output_folder):
print ('folder already exists')
else:
os.makedirs(output_folder)
for f in input_folder:
fastqs=glob.glob(input_folder + '/*.fastq.gz')
cmd =[gunzip, -k, fastqs, output_folder]
my_file=subprocess.Popen(cmd)
my_file.wait
print ('The programme has finished doing its job')
decompressed_files(input_folder)
This is done for python 2.7, I know that is old but it is the one that it is installed in my work server.
That's why when you call decompressed_files(input_folder) in the last line, you didn't define input_folder before. you should do it like this :
input_folder = 'C:/Some Address/'
decompressed_files(input_folder)
I am trying to convert multiple raster files to NetCDF files using an ArcPy script. When I run the below script, I get the following error message:
Message File Name Line Position
Traceback
<module> <module1> 19
RasterToNetCDF C:\Program Files\ArcGIS\Desktop10.3\ArcPy\arcpy\md.py 253
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000840: The value is not a Raster Layer.
ERROR 000840: The value is not a Raster Catalog.
Failed to execute (RasterToNetCDF).
Python script:
# Import system modules
import arcpy
from arcpy import env
# Set environment settings
env.workspace = r"D:\2012A"
# Set local variables
inRaster = r"D:\2012A"
outNetCDFFile = r"D:\2012A/nppnetcdf.nc"
variable = "elevation"
units = "meter"
XDimension = "x"
YDimension = "y"
bandDimension = ""
# Process: RasterToNetCDF
arcpy.RasterToNetCDF_md(inRaster, outNetCDFFile, variable, units,
XDimension, YDimension, bandDimension)
#Erica answered why you are receiving an error, though if you want to perform your conversion for each raster dataset within a directory, you'll have to first create a list of rasters within it.This can be done with something like this:
rasterlist = arcpy.ListRasters()
## other variables, as you have already defined them in your code
for raster in rasterlist:
RastertoNetCDF_md(variables)
So to implement this:
# Set environment settings
env.workspace = r"D:\2012A"
# Set local variables
inRaster = r"D:\2012A"
outNetCDFFile = r"D:\2012A\nppnetcdf.nc"
variable = "elevation"
units = "meter"
XDimension = "x"
YDimension = "y"
bandDimension = ""
rasterlist = arcpy.ListerRasters()
# Process: RasterToNetCDF
for raster in rasterlist:
arcpy.RasterToNetCDF_md(inRaster, outNetCDFFile, variable, units,
XDimension, YDimension, bandDimension)
Two problems stand out to me.
First, your file path here outNetCDFFile = r"D:\2012A/nppnetcdf.nc" is invalid. Both should be backslashes \ to get a valid path.
Second, and what is more likely causing the error -- inRaster = r"D:\2012A" appears to be a directory. You can't pass just a directory to the RasterToNetCDF_md tool -- the input parameter has to be a raster layer. Run the MakeRasterLayer_management tool on a raster file (not on a directory!) to create a raster layer, and pass that result to RasterToNetCDF_md.
I am new to python and I'm trying to create a program that creates a directory with todays date, create a sandbox into that directory and run the make file in the sandbox. I am having trouble getting the variables to be picked up in the os.path lines. The code is posted below:
#!/usr/bin/python
import mks_function
from mks_function import mks_create_sandbox
import sys, os, time, datetime
import os.path
today = datetime.date.today() # get today's date as a datetime type
todaystr = today.isoformat() # get string representation: YYYY-MM-DD
# from a datetime type.
if not os.path.exists('/home/build/test/sandboxes/'+todaystr):
os.mkdir(todaystr)
else:
pass
if not os.path.exists('/home/build/test/sandboxes/'+todaystr+'/new_sandbox/project.pj'):
mks_create_sandbox()
else:
pass
if os.path.exists('/home/build/test/sandboxes/'+todaystr+'/new_sandbox/Makefile'):
os.system("make >make_results.txt 2>&1")
Any help would be appreciated,
Thanks
a couple of notes:
#!/usr/bin/env python
# import mks_function .. you won't need this ...
from mks_function import mks_create_sandbox
import os, datetime
# import time, sys .. these aren't used in this snippet
# import os.path .. just refer to os.path, since os is already imported
# get today's date as a datetime type
todaystr = datetime.date.today().isoformat()
# .. use os.path.join()
if not os.path.exists(os.path.join('/home/build/test/sandboxes/', todaystr)):
os.mkdir(os.path.join('/home/build/test/sandboxes/', todaystr))
# .. 'else: pass' is unnecessary
if not os.path.exists(os.path.join(
'/home/build/test/sandboxes/', todaystr, '/new_sandbox/project.pj')):
# i'm not seen, that the sandbox is created in the right directory here
# maybe you should change the working directory via ..
# os.chdir(os.path.join('/home/build/test/sandboxes/', todaystr))
mks_create_sandbox()
if os.path.exists(os.path.join(
'/home/build/test/sandboxes/', todaystr, '/new_sandbox/Makefile')):
# .. change to the right directory
os.chdir(os.path.join(
'/home/build/test/sandboxes/', todaystr, '/new_sandbox/'))
os.system("make > make_results.txt 2>&1")
Please try adding chdir code before you call make
if os.path.exists('/home/build/test/sandboxes/'+todaystr+'/new_sandbox/Makefile'):
os.chdir('/home/build/test/sandboxes/'+todaystr+'/new_sandbox/')
os.system("make >make_results.txt 2>&1")
I think you want to change a few things:
def makeSandbox():
sbdir = os.path.join('/home/build/test/sandboxes/',todaystr)
if not os.path.exists(sbdir):
os.mkdir(sbdir) # <- fully qualified path
else:
pass
And I don't really see what variables need to be picked up, seems fine to me.
Not sure what the module mks_function does. But I see one issue with your code.
For example,
if not os.path.exists('/home/build/test/sandboxes/'+todaystr):
os.mkdir(todaystr)
In the above chunk you check if the directory "/home/build/test/sandboxes/+'todaystr'"
exists and a create a directory by name "value contained in todaystr" (say 2009-12-21). This creates directory by name '2009-12-21' in the current working directory, rather than under : /home/build/test/sandboxes
which is what you intended I guess. So change to the above directory before the call to mkdir. Also it is good to check the return status of mkdir to verify if the directory creation succeeded.
path module might help in this case:
#!/usr/bin/env python
from mks_function import mks_create_sandbox
import os, datetime
from path import path
sandboxes = path('/home/build/test/sandboxes/')
today = sandboxes / datetime.date.today().isoformat()
today.mkdir() # create directory if it doesn't exist
project = today / "new_sandbox/project.pj"
project.parent.mkdir() # create sandbox directory if it doesn't exist
if not project.isfile():
mks_create_sandbox()
makefile = project.parent / "Makefile"
if makefile.isfile():
os.chdir(makefile.parent)
os.system("make >make_results.txt 2>&1")