2

I am creating a script that uses AreaSolarRadiation. Currently arcpy gives a cryptic error during the night hours.

arcgisscripting.ExecuteError: ERROR 999999: Error executing function.
Failed to open raster dataset
Failed to execute (AreaSolarRadiation)

Is there a way to simply check to know when it is night hours so I can skip running the tool? Currently I just catch the cryptic error and skip it. The script calculates Solar Radiation every hour over several days. It would be nice to have a quick check to save computation time during night hours.

import arcpy
from arcpy.sa import *
import datetime

arcpy.env.workspace = r'C:\Workspace'
arcpy.CheckOutExtension('Spatial')

print('Begin Script')

date_time = datetime.datetime.strptime('2014-01-01', '%Y-%m-%d')
day_of_year = date_time.timetuple().tm_yday #(Jan 1 = 1)
start = 12 # Hour to start calculation
end = 13 # Hour to end
twd = TimeWithinDay(day_of_year, start, end) 
sky_size = 200 
dem = r'C:\Workspace\dem.tif' # Any DEM file (use a small one for testing)

# Works during the day
out_solar_radiation = AreaSolarRadiation(dem, '', sky_size, twd)
print(out_solar_radiation)

start = 2
end = 3
twd = TimeWithinDay(day_of_year, start, end) 

# Does not work at night
out_solar_radiation = AreaSolarRadiation(dem, '', sky_size, twd)
print out_solar_radiation
print('End Script')

Using ArcGIS 10.3

When the workspace is a place arcpy can write to I get a different error

arcgisscripting.ExecuteError: ERROR 010036: Error in creating the sun map.
ERROR 010003: The time range is at night when sun light is not available
Failed to execute (AreaSolarRadiation)

Esri help pages don't have very helpful information though.

http://resources.arcgis.com/en/help/main/10.1/index.html#//00vq0000000n010036 http://resources.arcgis.com/en/help/main/10.1/index.html#//00vq00000002010003

Tool dialogue and error screenshots

Tool Dialogue box

Error screenshot

6
  • 1
    Welcome to GIS SE. As a new user please be sure to take the Tour. Please edit this question to specify the version of ArcGIS in use, and contain the complete command that leads to that error message, with a text description of the parameters.
    – Vince
    Commented Oct 20, 2015 at 18:02
  • en.wikipedia.org/wiki/Sunrise_equation
    – FelixIP
    Commented Oct 20, 2015 at 19:34
  • @FelixIP I'm checking this out right now. It might be what I was looking for. But I was hoping there would be an arcpy function that returned a boolean. There must be something in arcpy that is making this check.
    – chaptuck
    Commented Oct 20, 2015 at 21:14
  • @TuckerChapman The formulae in wiki is way to complicated, can be reduced to latitude and Julian day and provide result that is accurate enough for your purpose
    – FelixIP
    Commented Oct 20, 2015 at 22:15
  • e.g. stackoverflow.com/questions/2538190/sunrise-set-calculations
    – FelixIP
    Commented Oct 20, 2015 at 22:22

1 Answer 1

1

What about using datetime? Like:

from datetime import datetime as dt
now_in_hours = dt.now().time().hour
if now_in_hours in range(6, 18):
    do process...

Update: You can retrieve the current sunrise and sunset times based on location from a web service like: http://sunrise-sunset.org/ and then use the result in the above if statement:

import urllib
import json

def sun (lat, lon):
    url = 'http://api.sunrise-sunset.org/json?lat=%f&lng=%f' % (lat, lon)
    result= json.loads(urllib.urlopen(url).read())

    sunrise = result['results']['sunrise']
    sunset = result['results']['sunset']
    return (sunrise, sunset)

lat, lon =  46.05145, -14.506053
print sun(lat,lon)   ## (u'7:22:07 AM', u'6:03:26 PM')
10
  • If the OP is really looking for times when the sun is actually up (or has been up in the past hour), the if statement would need to be far more complex, depending on both latitude and longitude of the observation point.
    – Vince
    Commented Oct 20, 2015 at 20:03
  • Yes, at first I thought this might work but the AreaSolarRadiation tool also adjusts to the time of year (shorter days in the winter).
    – chaptuck
    Commented Oct 20, 2015 at 20:18
  • Latitude, longitude and day of the year
    – FelixIP
    Commented Oct 20, 2015 at 20:36
  • @Matej If I was doing this for my own project a web service would be okay. But my boss most likely won't agree to it. Its a good idea though.
    – chaptuck
    Commented Oct 20, 2015 at 21:19
  • In that case, you'll have to implement your own logic. There are several resources out there: williams.best.vwh.net/sunrise_sunset_algorithm.htm
    – Matej
    Commented Oct 20, 2015 at 21:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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