|
POST
|
This is my code. I got it to loop through every file in a folder. The problem is that it only reads the first value. I haven't figured out how to loop through each individual value in the netCDF file yet. I have been trying to modify the code from this forum: http://forums.arcgis.com/threads/32361-using-arcpy-to-export-separate-netcdf-dimension-values-as-a-raster so it reads individual value. # Import system modules
import arcpy
from arcpy import env
from arcpy.sa import *
# Input data source
arcpy.env.workspace = "S:/PM_data/Climate_Idaho/Raw/pr"
arcpy.env.overwriteOutput = True
# Set output folder
OutputFolder = "S:/PM_data/Climate_Idaho/Output/pr_raster"
# Loop through a list of files in the workspace
NCfiles = arcpy.ListFiles("*.nc")
for filename in NCfiles:
print("Processing: " + filename)
inNCfiles = arcpy.env.workspace + "/" + filename
fileroot = filename[0:(len(filename)-3)]
TempLayerFile = "precipitation_amount"
outRaster = OutputFolder + "/" + fileroot
# Process: Make NetCDF Raster Layer
arcpy.MakeNetCDFRasterLayer_md(inNCfiles, "precipitation_amount", "lon", "lat", TempLayerFile, "", "", "BY_VALUE")
# Process: Copy Raster
arcpy.CopyRaster_management(TempLayerFile, outRaster + ".tif", "", "", "", "NONE", "NONE", "")
print "***DONE!!!"
print arcpy.GetMessages() Note that you need to call arcpy.MakeNetCDFRasterLayer_md() for each time step in each netCDF file. You're looping through your files fine, but you need to add another 'inner' loop to go through an individual file. I would try to use "BY_INDEX" instead of "BY_VALUE". Let's assume that your data are in time series, so the "dimension value" is "TIME": # this will convert the FIRST time step in your netCDF file to a raster layer
arcpy.MakeNetCDFRasterLayer_md(inNCfiles, "precipitation_amount", "lon", "lat", TempLayerFile1, "#", "TIME 0", "BY_INDEX")
# this will convert the SECOND time step in your netCDF file to a raster layer
arcpy.MakeNetCDFRasterLayer_md(inNCfiles, "precipitation_amount", "lon", "lat", TempLayerFile2, "#", "TIME 1", "BY_INDEX")
# this will convert the THIRD time step in your netCDF file to a raster layer
arcpy.MakeNetCDFRasterLayer_md(inNCfiles, "precipitation_amount", "lon", "lat", TempLayerFile3, "#", "TIME 2", "BY_INDEX")
#and so on... Obviously you can't just copy-and-paste this code, but this should give you an idea of how to set it up.
... View more
06-18-2012
02:02 PM
|
0
|
0
|
4323
|
|
POST
|
Not sure what's causing the problem exactly. But a couple of things could be cleaned up, and that might straighten out weird behavior. I most often get this same error when one of my lists unexpectedly contains zero items. First, I've always seen/used the mode type declared when creating a file object: readTable = open("C:/Users/atimpson/Desktop/ScriptTable/BPWorksLayers.csv", "r") # 'r' is for 'read only' Second, you've got some extra brackets, which means you're actually appending a list to a list. I'm guessing that's not what you intended to do: for line in readTable.readlines():
segmentedLine = line.split(",")
baseFeature.append(segmentedLine[fcPos]) # I removed an extra bracket here
baseLayer.append(segmentedLine[lyrPos]) # and here And finally, troubleshooting is a lot easier when you have line number and traceback information:
# I wrap this around all of my scripts, pretty much.
import traceback
import sys
try:
# all of your code here
except:
tbinfo = traceback.format_tb(sys.exc_info()[2])
print "Traceback Info:\n"
for item in tbinfo:
print item + "\n"
print "Error Info:\n{0}: {1}\n".format(sys.exc_type, sys.exc_value)
... View more
06-05-2012
09:37 AM
|
0
|
0
|
860
|
|
POST
|
This type of task is very common in GIS work and, fortunately, requires only a very simple script. Your first step should be to read the documentation and make sure you understand what information you need to run the MakeNetCDFRasterLayer tool. Then start with this: import arcpy as ap
import glob
# this will give you a Python list object that you can use to batch process all of your files
# just insert the path to your folder holding the netCDF files
cdfList = glob.glob('C:\\examplefolder\\*.nc')
# now you can loop through your list and process each file one at a time
for cdf in cdfList:
print "Now processing: " + cdf
ap.md.MakeNetCDFRasterLayer(cdf, [fill in the other variables here])
print "Done!" Note that this is only the start of the script. For example, you'll need to make sure each output raster is given a unique, meaningful name. But we should leave some of the fun stuff for you. 🙂
... View more
05-25-2012
05:22 AM
|
1
|
0
|
4323
|
|
POST
|
if you're running directly from a python script independant of ArcGIS, then yes, it works just fine. That was actually my problem. I was running my script as a stand-alone using the IDLE window to process everything. I got tired of having to navigate to the file everytime though, so I tried to add it as a tool within Arctoolbox. If you process it from the Arcpy window within ArcMap, or as a tool script that requires much closer interaction with ArcGIS, then set( ) will not work. Open up the Python window within ArcMap and try it. That's probably the best way to see it. I'm still not seeing any errors. The set() class is working normally for me in the ArcMap Python window (see attached). I'm using AGD 10, service pack 4, with the spatial join patch. Are you importing any other libraries like this: from (some Python library) import * The only thing I can think of is the name space might be polluted and you're calling a different set function or class. You mentioned a thread in a forum that discussed this issue. Do you still have a link to that? A screenshot of the error would be helpful too.
... View more
05-09-2012
11:13 AM
|
0
|
0
|
2208
|
|
POST
|
My guess is you won't get many helpful responses here since your question is, by all appearances, completely unrelated to ArcGIS. But you might start with this: http://stackoverflow.com/questions/8067979/using-plink-putty-to-ssh-through-python
... View more
05-09-2012
05:37 AM
|
0
|
0
|
8479
|
|
POST
|
Will return: set(['a']) Only because everything is wrapped in the set() Python class.
>>>myList = list(set(['a','b','c','f','h','a','c','c','c','t']))
>>>myList[0]
'a'
>>>for item in set(['a','b','c','f','h','a','c','c','c','t']):
... print item
a
c
b
f
h
t An important point is that set() objects are unordered, so if order is important one should also wrap everything in sorted(). Please let me know if I'm not understanding the issue correctly.
... View more
05-08-2012
11:57 AM
|
0
|
0
|
2208
|
|
POST
|
I'm a little confused by this question. In what way are Python set objects incompatible with arcpy?
... View more
05-08-2012
09:51 AM
|
0
|
0
|
2208
|
|
POST
|
Or, just to be lazy: if not filename.endswith("*.14.tif"):
Touché. Though you could run into trouble if some other raster is laying around in there. And also, the endswith method doesn't use asterisks as wildcards. It will evaluate the string literally and look for asterisks in the file name. >>>filename = 'ppt_1970.14.tif'
>>>filename.endswith('14.tif')
True
>>>filename.endswith('*.14.tif')
False
... View more
05-07-2012
12:32 PM
|
0
|
0
|
1463
|
|
POST
|
Dear Darren, Thank you very much for your reply. Maybe I said something wrong at the beginning. I want to include the files that ends with *.01 to *.12 but exclude *.14. You can do that if you want. But as I pointed out earlier, the data you are working with already include the annual averages: the files ending in "14". If you're just going to average all months together, you can make things simpler and faster by just using the "14" files. But, this is the change you should make to average 12 files from each year:
inRaster = [] # you need to do this outside of the loop
for filename in rasterFiles:
print("Processing: " + filename)
if filename[-6:] in [str(x).zfill(2) + '.tif' for x in xrange(1, 13)]:
rasterFiles.append(filename)
... View more
05-07-2012
10:18 AM
|
0
|
0
|
1463
|
|
POST
|
There are a couple of problems that need to be addressed. I put in some comments to help explain. # Import system modules
import arcpy
from arcpy import env
from arcpy.sa import *
# Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")
# Input data source
arcpy.env.workspace = "S:/Work/Risa/Trial_and_Error/input" # don't use spaces or special characters in path names
arcpy.env.overwriteOutput = True
# Set output folder
OutputFolder = "S:/Work/Risa/Trial_and_Error/output/"
# Loop through a list of files in the workspace
rasterFiles = arcpy.ListRasters()
# The loop does nothing else but build the list
inRasters = [] # you need to do this outside of the loop
for filename in rasterFiles:
print("Processing: " + filename)
if filename.endswith("14.tif"):
inRasters.append(filename)
# Now we have a list of the files we want to average, but you only need to calculate the average once,
# which means this part should not be in the loop, so we remove indentation
outRaster = CellStatistics(inRasters, "MEAN", "DATA")
# Save the output
outRaster.save("S:/Work/Risa/Trial_and_Error/output/AvgPrecip01.tif")
print "Average Calculated!!!" Looping through files one of the most useful and common tasks in GIS. Once you get this mastered you'll be set!
... View more
05-07-2012
06:29 AM
|
0
|
0
|
1463
|
|
POST
|
First, please use CODE tags when you post actual Python code: http://forums.arcgis.com/threads/48475-Please-read-How-to-post-Python-code Second, the "14" file that you want to exclude is actually the annual average. No need to include the other 12 files for each year. Third, in answer to your question you could do something like this: inList = []
for filename in rasterFiles:
if filename.endswith('14'):
inList.append(filename)
arcpy.sa.CellStatistics(inList, outRaster, "MEAN", "DATA") # this line was incorrect in your example
... View more
05-03-2012
05:29 AM
|
0
|
0
|
1463
|
|
POST
|
You just need to add a newline character: "\n". Also, you could rewrite the line in question to make it a little cleaner. # if you're using Python 2.5
outfile.write("%s,%s,%s,%s\n" % (SPECIES, ECO_ID, OCCUR, COUNT))
# if you're using Python 2.6+
outfile.write("{1},{2},{3},{4}\n".format(SPECIES, ECO_ID, OCCUR, COUNT))
# or this...
outfile.write(",".join([SPECIES, ECO_ID, OCCUR, COUNT + "\n"]))
... View more
05-01-2012
05:46 AM
|
0
|
0
|
523
|
|
POST
|
It's preferable that you post your example script here using code blocks. There is a sticky post at the top of this forum that explains how to do that. Your value of fieldList is not a list, it's a string. To make a Python list, enclose a string in brackets: fieldList = ["land_cover"] As somebody else pointed out, your workspace variable is not being set correctly. This is probably the problem.
... View more
04-25-2012
02:02 PM
|
0
|
0
|
955
|
|
POST
|
First things first. When you're asking people to look over code, please use the "CODE" tags. There is a sticky post at the top of this forum called "How to post Python code" that explains how to do this. Also, indentation is very important in Python and if your example isn't displayed correctly, it's impossible for anyone to help you. Is this how your function is supposed to look? def Modelling(inPathNetCDF, inPathCov, outPath, concentration):
netcdfName = os.path.basename(inPathNetCDF).split('.')[0]
covName = os.path.basename(inPathCov)
env.workspace = outPath
netcdfList = netcdfName.split('_')[4]
modelName = (netcdfName.split('_')[2]).lower()
dateForTitle = netcdfList[6:8] + netcdfList[4:6] + netcdfList[:4]
date18Z = netcdfList[6:8] + '/' + netcdfList[4:6] + '/' + netcdfList[:4]
arcpy.MakeNetCDFRasterLayer_md(inPathNetCDF, 'IceConc', 'lon', 'lat', 'tmpNetCDF18', '', '"time" "%s 6:00:00 PM"' %date18Z, 'BY_VALUE')
arcpy.CopyRaster_management('tmpNetCDF18', outPath + os.sep + outNetcdfName18Z)
... View more
04-18-2012
12:44 PM
|
0
|
0
|
329
|
|
POST
|
By default there is no extent set: Ah yes, you're right. I must have set this a while ago.
... View more
04-17-2012
04:54 AM
|
0
|
0
|
693
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 05-25-2012 05:22 AM | |
| 1 | 02-09-2018 01:19 PM | |
| 1 | 09-20-2011 11:59 AM | |
| 1 | 03-27-2013 09:08 AM | |
| 3 | 02-09-2018 06:34 AM |
| Online Status |
Offline
|
| Date Last Visited |
11-11-2020
02:23 AM
|