0

Can you please write me sample loop that iterate through prepared earlier values and automatically changes number of output raster e.g. from outDirectRad1 to outDirectRad2.

Parameters that will change on each iteration:

e.g. first iteration

timeConfig = TimeMultipleDays(2015, 2, 32)

diffuseProp = 0.6

transmittivity = 0.2

e.g. second iteration

timeConfig = TimeMultipleDays(2015, 1, 31)

diffuseProp = 0.4

transmittivity = 0.3

**

  • Rest parameters are the same in each iteration

**

Here is my code:

import arcpy
from arcpy import env
from arcpy.sa import *

env.workspace = "C:/xxx/output"

arcpy.CheckOutExtension("Spatial")

inRaster = "C:/xxx/output/Test"
latitude = 47.04
skySize = 200
timeConfig = TimeMultipleDays(2015, 2, 32)
dayInterval = 14
hourInterval = 1
zFactor = 1
calcDirections = 32
zenithDivisions = 8
azimuthDivisions = 8
diffuseProp = 0.4
transmittivity = 0.6
outDirectRad = "C:/xxx/output/outDirectRad1"
outDiffuseRad = "C:/xxx/output/outDiffuseRad1"
outDirectDur = "C:/xxx/output/dir_dur1"

globRad1 = AreaSolarRadiation(inRaster, latitude, skySize, timeConfig,
dayInterval, hourInterval, "NOINTERVAL", zFactor, "FROM_DEM",
calcDirections, zenithDivisions, azimuthDivisions, "UNIFORM_SKY",
diffuseProp, transmittivity, outDirectRad, outDiffuseRad, outDirectDur)

globRad1.save("C:/xxx/output")
  • 1
    Welcome to GIS SE! A good question should include some degree of research and attempt. Include what you've tried and details of what happens when you try it, including any error messages. We are volunteers, not a code writing service. I have given you a start in my answer below. – Midavalo Mar 8 '17 at 16:56
2

Put your values into lists, loop through those lists:

# Static values go here
inRaster = "C:/xxx/output/Test"
latitude = 47.04
skySize = 200
# etc.

params = [[TimeMultipleDays(2015, 2, 32), 0.6, 0.2], [TimeMultipleDays(2015, 1, 31), 0.4, 0.3]]

for param in params:
    timeConfig = param[0]
    diffuseProp = param[1]
    transmittivity = param[2]

    # Rest of your code goes here

To break down the params list:

params = [
          [                                # List of parameters - First Iteration
           TimeMultipleDays(2015, 2, 32),  # First Iteration param[0]
           0.6,                            # First Iteration param[1]
           0.2                             # First Iteration param[2]
          ], 
          [                                # List of parameters - Second Iteration
           TimeMultipleDays(2015, 1, 31),  # Second Iteration param[0]
           0.4,                            # Second Iteration param[1]
           0.3                             # Second Iteration param[2]
          ]
         ]

import arcpy
from arcpy import env
from arcpy.sa import *

env.workspace = "C:/xxx/output"
arcpy.CheckOutExtension("Spatial")

inRaster = "C:/xxx/output/Test"
latitude = 47.04
skySize = 200
dayInterval = 14
hourInterval = 1
zFactor = 1
calcDirections = 32
zenithDivisions = 8
azimuthDivisions = 8
outDirectRad_pre = "C:/xxx/output/outDirectRad"
outDiffuseRad_pre = "C:/xxx/output/outDiffuseRad"
outDirectDur_pre = "C:/xxx/output/dir_dur"
i = 0 # suffix for output filenames

params = [[TimeMultipleDays(2015, 2, 32), 0.6, 0.2], [TimeMultipleDays(2015, 1, 31), 0.4, 0.3]]

for param in params:
    timeConfig = param[0]
    diffuseProp = param[1]
    transmittivity = param[2]

    i += 1
    outDirectRad = "{0}_{1}".format(outDirectRad_pre, i)
    outDiffuseRad = "{0}_{1}".format(outDiffuseRad_pre, i)
    outDirectDur = "{0}_{1}".format(outDirectDur_pre, i)

    globRad1 = AreaSolarRadiation(inRaster, latitude, skySize, timeConfig, dayInterval, hourInterval, "NOINTERVAL", zFactor, "FROM_DEM", calcDirections, zenithDivisions, azimuthDivisions, "UNIFORM_SKY", diffuseProp, transmittivity, outDirectRad, outDiffuseRad, outDirectDur)
    globRad1.save("C:/xxx/output")
  • I've created quite long list with all values that I need. How to create loop that automatically goes through next values? When I start function first time it should be like you wrote earlier. But on the second time it should be: for param in params: <br></br> 'timeConfig = param[3]' <br></br> 'diffuseProp = param[4]' <br></br> 'transmittivity = param[5]' – haroldkumar1 Mar 8 '17 at 18:06
  • 1
    @haroldkumar1 I have added a break-down of the parameters list to show how the list of lists work – Midavalo Mar 8 '17 at 19:34
  • I am not sure if you know what I mean. I think I wasn't preceise enough. After first iteration with this list like you wrote above function AreaSolar should produce 4 rasters: globRad1,outDirectRad1,outDiffuseRad1,outDirectDur1. Then comes second iteration like you wrote earlier and function AreaSolar should produce again 4 rasters but with parameters from second list. Output should be:globRad2,outDirectRad2,outDiffuseRad2,outDirectDur2. – haroldkumar1 Mar 8 '17 at 20:34
  • @haroldkumar1 put all your code into the loop, then each time the loop runs over your different parameters it will run the tools to output your rasters. Have you tried it? – Midavalo Mar 8 '17 at 20:37
  • @haroldkumar1 I have updated my answer to include a full example of looping your script. Please note that I am not familiar with AreaSolarRadiation() tool, so the code (particularly for the ourDirectRad etc. files) may need to be tweaked to get it to work, but hopefully now you'll get an idea of how the looping/iterating actually works using that list of lists. If there are values that change every time, put them in the params for each iteration, and refer to them with newVar = param[3] etc. – Midavalo Mar 9 '17 at 5:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.