forked from stuporglue/solar_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batchSolarAnalyst.py
executable file
·262 lines (216 loc) · 9.91 KB
/
batchSolarAnalyst.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# batchSolarAnalyst.py
# Created on: 2014-16-08
# Description: Generate solar radiation tiles for all areas in a shapefile
# ---------------------------------------------------------------------------
# Calculate solar radiation over a very large area by using a Raster Dataset as the source
# and using a database with a fishnet of polygons to choose which areas to use as extents.
#
# Also takes a buffer size which will be applied to each polygon for use in the extent so that
# output rasters will have edges that match
# The final image output will be clipped back to the size of the input polygon
# Pseudo Code!
# For eachPolygon in fishnet where state is 0:
# bufferedPollygon = buffer polygon by bufferAmount
# set extent env to bufferedPolygon
# outputRaster = AreaSolarRadiation(rasterDataset)
# clippedOutput = clip outputRaster back to polygon size
# write clippedOutput file to designated directory
print "Batch Solar Analyst, reporting for duty"
import sys
import os
import time
import dbconn_quick
import shutil
import datetime
import dbconn
from config import *
print "Importing arcpy"
import arcpy
print "Everything imported"
# Define workspace and input datasets
ws = config.get('paths','workspace')
in_surface_raster = config.get('paths','dem_mosaic')
out_path = config.get('paths','solar_raster_output_dir')
try:
tmpfile = (os.environ.get('PBS_NODENAME') or os.environ.get('HOSTNAME') or 'nohostname') + str(time.time()) + str(os.getpid())
except:
print os.environ
raise
# Check out spatial analyst and set ArcGIS environment settings
print "Checking out the spatial extension"
arcpy.CheckOutExtension("spatial")
print "Making temp paths"
temp_dir = config.get("paths",'temp_dir')
# Check for existence of directories and create as needed
if not os.path.isdir(temp_dir):
try:
os.mkdir(temp_dir)
except:
pass
if not os.path.isdir(temp_dir):
print "ERROR! Couldn't create temporary directory", temp_dir
exit()
workspace = config.get('paths','temp_dir') + os.sep + tmpfile + '_workspace'
if not os.path.isdir(workspace):
try:
os.mkdir(workspace)
except:
pass
if not os.path.isdir(workspace):
print "ERROR! Couldn't create workspace directory " + workspace
exit()
arcpy.env.workspace = workspace
# Test read/write privledges
try:
ret = os.access(out_path, os.R_OK)
except:
print 'You do not have read access to', out_path + '. Please contact the network admin for access.'
raw_input('Please press enter to exit.')
exit()
try:
ret = os.access(out_path, os.W_OK)
except:
print 'You do not have write access to', out_path + '. Please contact the network admin for access.'
raw_input('Please press enter to exit.')
exit()
print "Making scratch workspace"
# Make a temp directory to avoid the FATAL ERROR (INFADI) MISSING DIRECTORY error
# NOTE: Delete this later (last line of script)
# Seems like dir should be made in here, but it's not working.... tempfile.gettempdir()
scratchdir = config.get('paths','temp_dir') + os.sep + tmpfile + '_temp'
if not os.path.isdir(scratchdir):
try:
os.mkdir(scratchdir)
except:
pass
arcpy.env.scratchWorkspacea = scratchdir
print "Setting loghistory to false"
arcpy.SetLogHistory(False)
# buffer distance for input processing extent
buff = config.getint('buffers','sa_processing_buffer')
# Define Solar analyst variables
latitude = '' # generate in for loop from feature attribute
sky_size = int(config.get('solar_analyst','sky_size')) # min is 100
# TODO: Change 2014 to current year
print "Setting SA settings"
time_configuration = arcpy.sa.TimeWholeYear(datetime.date.today().year) # arcgis time object set for all of 2014
day_interval = '' # with whole year analysis default is calendar month
hour_interval = '' # default is 0.5
each_interval = '' # default is no interval (i.e. a single band for whole year)
z_factor = '' # meters for both units, defaults to 1
slope_aspect_input_type = 'FROM_DEM' # how slope/aspect are determined
calculation_directions = int(config.get('solar_analyst','calculation_directions')) # number of directions used when calculating viewshed (multiple of 8 only)
zenith_divisions = int(config.get('solar_analyst','zenith_divisions')) # number of divisions to create sky sectors in sky map
azimuth_divisions = int(config.get('solar_analyst','azimuth_divisions')) # number of divisions to create sky sectors in sky map
diffuse_model_type = 'UNIFORM_SKY' # type of diffuse radiation model
diffuse_proportion = float(config.get('solar_analyst','diffuse_proportion')) # proportion of radiation that is diffuse
transmissivity = float(config.get('solar_analyst','transmissivity')) # fraction of radiation passing through atmosphere (0.5 for generally clear sky)
out_direct_radiation_raster = '' # optional output - direct radiation
out_diffuse_radiation_raster = '' # optional output - diffuse radiation
out_direct_duration_raster = '' # optional output - direct duration
hostname = os.getenv('HOSTNAME') or os.getenv('COMPUTERNAME') or 'Unknown host'
# Database queries to manage processing
reserveQuery = """
UPDATE """ + config.get('postgres','schema') + "." + config.get('postgres','sa_fishnet_table') + """ sa
SET state=1,
hostname='""" + hostname + """'
WHERE sa.id in (
SELECT id FROM """ + config.get('postgres','sa_fishnet_table') + """ WHERE state=0
ORDER BY
--- id
--- RANDOM()
ST_Distance(the_geom,ST_SetSrid(ST_MakePoint(""" + config.get('processing','starting_x') + """,""" + config.get('processing','starting_y') + """),""" + config.get('projection','srid') + """))
LIMIT 1 FOR UPDATE
)
RETURNING
id,
ST_XMin(the_geom) as xmin,
ST_YMin(the_geom) as ymin,
ST_XMax(the_geom) as xmax,
ST_YMax(the_geom) as ymax,
ST_Y(ST_Transform(ST_CENTROID(the_geom),4326)) AS lat
"""
completeQuery = """
UPDATE """ + config.get('postgres','schema') + "." + config.get('postgres','sa_fishnet_table') + """ AS sa
SET
state = c.newstate::integer,
time = c.runtime::float,
hostname='""" + hostname + """'
FROM (values
('UPDATEVALUES')) AS c(updateid,newstate,runtime)
WHERE
sa.id=c.updateid::integer
"""
# connect to database using dbconn connection script
print "Reserving first job"
res = dbconn_quick.run_query(reserveQuery)
print str(len(res)) + " jobs reserved"
count = 0
average = 0
while len(res) > 0:
resultsToSubmit = []
for row in res:
count += 1
print "Running Area Solar Radiation for row " + str(row['id'])
start_time = time.clock()
# add buffer/subtract distance to extent boundaries
x_min = int(row['xmin']) - buff
y_min = int(row['ymin']) - buff
x_max = int(row['xmax']) + buff
y_max = int(row['ymax']) + buff
latitude = float(row['lat'])
# set processing environment
arcpy.env.extent = arcpy.sa.Extent(x_min, y_min, x_max, y_max)
# lookup dsm tile id
demno = 0
q = """
SELECT d.id FROM dem_fishnets d,sa_fishnets s WHERE ST_WITHIN(s.the_geom,d.the_geom) AND s.id=""" + str(row['id']) + """
"""
t = dbconn.run_query(q)
for s in t:
demno = str(s['id'])
#clipped_solar_raster_dir = out_path + os.sep + 'SRR_' + str(row['id'] / 1000 * 1000).zfill(4) + os.sep
clipped_solar_raster_dir = out_path + os.sep + demno + os.sep
clipped_solar_raster = clipped_solar_raster_dir + str(row['id']) + '.img' # what raster format do we want???
if not os.path.exists(clipped_solar_raster_dir):
os.mkdir(clipped_solar_raster_dir)
if os.path.isfile(clipped_solar_raster):
print "WARNING: Overwriting existing solar raster image at " + clipped_solar_raster
os.unlink(clipped_solar_raster)
# run solar analyst
try:
solar_raster = arcpy.sa.AreaSolarRadiation(in_surface_raster, latitude, sky_size, time_configuration, day_interval, hour_interval, each_interval, z_factor, slope_aspect_input_type, calculation_directions, zenith_divisions, azimuth_divisions, diffuse_model_type, diffuse_proportion, transmissivity, out_direct_radiation_raster, out_diffuse_radiation_raster, out_direct_duration_raster)
# clip to feature extent and saves output
envelope = "{0} {1} {2} {3}".format(int(row['xmin']), int(row['ymin']), int(row['xmax']), int(row['ymax']))
if not os.path.isfile(clipped_solar_raster):
clipped = arcpy.Clip_management(solar_raster, envelope, clipped_solar_raster) #, '', '-3.402823e+038', '', True)
if os.path.isfile(clipped_solar_raster):
solarWorked = True
except 'arcgisscripting.ExecuteError':
print "\t\t\t" + arcpy.GetMessages(3)
print sys.exc_info()
solarWorked = False
except:
print sys.exc_info()
e = sys.exc_info()[0]
print "\t\t\t" + str(e)
solarWorked = False
# calculate average tile calculation time
end_time = time.clock()
time_run = end_time - start_time
average = (average * (count - 1) + time_run) / count
if solarWorked:
print "DONE! (" + str((time_run)) + " seconds, running avg:" + str(average) + ")"
resultsToSubmit.append([str(row['id']),'2',str(time_run)])
else:
print "Error! (" + str((time_run)) + " seconds, running avg:" + str(average) + ")"
resultsToSubmit.append([str(row['id']),'-3','-1'])
cq = completeQuery.replace('UPDATEVALUES',"'),('".join(["','".join(x) for x in resultsToSubmit]))
res = dbconn_quick.send_query(cq)
res = dbconn_quick.run_query(reserveQuery)
# We have to manually unlink temp dirs created by mkdtemp
shutil.rmtree(arcpy.env.scratchWorkspacea)
shutil.rmtree(arcpy.env.workspace)