简体   繁体   English

新手Python:如何与嵌套循环交互图层

[英]Rookie python: how to interact layers with a nested loop

I've dealing with the code below with such a long time. 我已经处理了很长时间的以下代码。 I have to show in a raster image the result of calculating the cost of the electricity for a solar plant. 我必须在光栅图像中显示计算太阳能发电厂的电费的结果。 The thing is that I have to interact a layer "DNI" (direct normal irradiation) with the formula, and the value of the formula changes with a factor for each year (costReductionFactorPlant) during the 30 years of the life plant. 问题是我必须使“ DNI”层(直接法线照射)与该公式交互作用,并且该公式的值在生命植物的30年中每年以一个因子(costReductionFactorPlant)变化。 The factors are shown below. 这些因素如下所示。

When I run the code, I receive the following error: 运行代码时,出现以下错误:

Runtime error 
Traceback (most recent call last):
  File "<string>", line 56, in <module>
  File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\sa\Functions.py", line 4049, in Times
    in_raster_or_constant2)
  File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\sa\Utils.py", line 47, in swapper
    result = wrapper(*args, **kwargs)
  File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\sa\Functions.py", line 4046, in wrapper
    return _wrapLocalFunctionRaster(u"Times_sa", ["Times", in_raster_or_constant1, in_raster_or_constant2])
RuntimeError: ERROR 000732: Input Raster: Dataset D:_ArcGIS\Python\LCOE_test does not exist or is not supported
>>> 

I'm afraid that there is something wrong when calling the layer "DNI". 恐怕在调用“ DNI”层时出了点问题。 But no clue. 但是没有头绪。 Maybe there is also an error with the loop... I've tryed to look at it also, for me looks good, but I'm not an expert. 也许循环也有错误...我也尝试过看它,对我来说看起来不错,但我不是专家。

I'm running ArcGis 10.1. 我正在运行ArcGis 10.1。 All suggestions are more than welcome. 所有建议都值得欢迎。

CODE: 码:

import arcpy, os, sys
from arcpy import env
from arcpy.sa import *

# Set Workspace
env.workspace = "D:\02_ArcGIS\Python\LCOE_test"

# Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")

# Variables definition
capacity = 100000
actualOutput = .94 * capacity
storageHours = 16
loadFactor = (.57 * storageHours + 7.1)/24
costReductionFactorPlant = [0.4790, 0.4533, 0.4297, 0.4080, 0.3880, 0.3696, 0.3526, 0.3368, 0.3222, 0.3087, 0.2961, 0.2844, 0.2735, 0.2633, 0.2538, 0.2449, 0.2366, 0.2288, 0.2215, 0.2146, 0.2081, 0.2021, 0.1964, 0.1910, 0.1859, 0.1811, 0.1765, 0.1723, 0.1682, 0.1643, 0.1607]
efficiency = [0.1822, 0.1827, 0.1831, 0.1834, 0.1837, 0.1840, 0.1841, 0.1843, 0.1844, 0.1845, 0.1846, 0.1847, 0.1847, 0.1848, 0.1848, 0.1849, 0.1849, 0.1849, 0.1849, 0.1849, 0.1849, 0.1850, 0.1850, 0.1850, 0.1850, 0.1850, 0.1850, 0.1850, 0.1850, 0.1850, 0.1850]
years = 30
i = .1
ir = .01
lcoePlantTotal = 0
productionPlant = loadFactor * actualOutput * 8760
DNI =  "D:\02_ArcGIS\Python\LCOE_test" #layer#

Count=len(costReductionFactorPlant)+ 1
counter=1

while counter < Count:
    for i in range(counter,(counter+1)):

            # Set the cell size environment using a keyword.
            arcpy.env.cellSize = "MAXOF"

            # Set the extent environment using a keyword
            arcpy.env.extent = "MAXOF"

            #intermediate formula plant

            # size = (capacity * 8760 * loadFactor)/(DNI * efficiency[counter-1])

            size1 = (capacity * 8760 * loadFactor)
            timesConstant = efficiency[counter-1]
            size2 = Times(3, timesConstant)
            print size2
            size = Divide(size1, size2)

            # Calculate power block & storage              
            powerBlock = capacity * 1484.9918 * costReductionFactorPlant[counter-1]
            storage = storageHours * capacity * 39.45759 * costReductionFactorPlant[counter-1]

            # Calculate mirrowField 
            # mirrorField = size * 155.6975 * costReductionFactorPlant[cont-1]
            timesConstant2 = costReductionFactorPlant[counter-1]
            inconstant = Times(155.6975, timesConstant2)
            mirrorField = Times(size, inconstant)

            # calculate PowerTower
            powerTower = ((capacity * 121.03883) + (capacity * 128.50378)) * costReductionFactorPlant[counter-1]

            # Calculate investmentPlant
            investmentPlant = powerBlock + storage + mirrorField + powerTower

            # Calculate omPlant
            omPlant = Times(investmentPlant, 0.048)

            #formula: lcoePlant = ((investmentPlant * (((i * (1 + i)^counter)/((1 + i)^counter-1))+ ir))+omPlant)/productionPlant*100
            # Formula Part 1 = (investmentPlant * (((i * (1 + i)^counter) Power symbol = **
            InConstant3= i * ((1 + i)**counter)
            FormulaPart1 = Times(investmentPlant, InConstant3)

            # Formula Part 2 = ((1 + i)^counter-1))+ ir))+omPlant)/productionPlant*100
            FormulaPart2 =(((1 + i)**(counter-1)+ ir)+omPlant)/productionPlant*100

            # Formula
            lcoePlant = Divide(FormulaPart1, FormulaPart2)

            lcoePlantTotal = lcoePlantTotal + lcoePlant

            lcoePlantTotal.save = "D:\\02_ArcGIS\\Python" + str(lcoePlantTotal) + ".img"

print "lcoePlanttotal calculated"

You need to escape your backslashes, you can see how it thinks you are saying hex-2. 您需要转义反斜杠,您可以看到它说十六进制2的方式。 If you look at the final line in your error msg, it's telling you the file doesn't exist and it's showing that it has a different file name from what you had given. 如果您在错误消息中查看最后一行,则表明文件不存在,并且表明文件名与给定的文件名不同。

>>> DNI =  "D:\02_ArcGIS\Python\LCOE_test"
>>> DNI
'D:\x02_ArcGIS\\Python\\LCOE_test'

>>> DNI =  "D:\\02_ArcGIS\\Python\\LCOE_test"
>>> DNI
'D:\\02_ArcGIS\\Python\\LCOE_test'
>>> print DNI
D:\02_ArcGIS\Python\LCOE_test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM