/
solar_potential_by_postal_code_dag.py
277 lines (269 loc) · 10.9 KB
/
solar_potential_by_postal_code_dag.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from airflow import DAG
from airflow.providers.cncf.kubernetes.operators import kubernetes_pod
from airflow.providers.google.cloud.transfers import gcs_to_bigquery
default_args = {
"owner": "Google",
"depends_on_past": False,
"start_date": "2021-03-01",
}
with DAG(
dag_id="sunroof_solar.solar_potential_by_postal_code",
default_args=default_args,
max_active_runs=1,
schedule_interval="@daily",
catchup=False,
default_view="graph",
) as dag:
# Run CSV transform within kubernetes pod
transform_csv = kubernetes_pod.KubernetesPodOperator(
task_id="transform_csv",
name="solar_potential_by_postal_code",
namespace="default",
affinity={
"nodeAffinity": {
"requiredDuringSchedulingIgnoredDuringExecution": {
"nodeSelectorTerms": [
{
"matchExpressions": [
{
"key": "cloud.google.com/gke-nodepool",
"operator": "In",
"values": ["pool-e2-standard-4"],
}
]
}
]
}
}
},
image_pull_policy="Always",
image="{{ var.json.sunroof_solar.container_registry.run_csv_transform_kub }}",
env_vars={
"SOURCE_URL": "gs://project-sunroof/csv/latest/project-sunroof-postal_code.csv",
"SOURCE_FILE": "files/data.csv",
"TARGET_FILE": "files/data_output.csv",
"CHUNKSIZE": "750000",
"TARGET_GCS_BUCKET": "{{ var.value.composer_bucket }}",
"TARGET_GCS_PATH": "data/sunroof/solar_potential_by_postal_code/data_output.csv",
},
resources={"limit_memory": "8G", "limit_cpu": "3"},
)
# Task to load CSV data to a BigQuery table
load_to_bq = gcs_to_bigquery.GCSToBigQueryOperator(
task_id="load_to_bq",
bucket="{{ var.value.composer_bucket }}",
source_objects=["data/sunroof/solar_potential_by_postal_code/data_output.csv"],
source_format="CSV",
destination_project_dataset_table="sunroof.solar_potential_by_postal_code",
skip_leading_rows=1,
allow_quoted_newlines=True,
write_disposition="WRITE_TRUNCATE",
schema_fields=[
{
"name": "region_name",
"type": "STRING",
"description": "Census Tract",
"mode": "NULLABLE",
},
{
"name": "state_name",
"type": "STRING",
"description": "Name of the state containing that region",
"mode": "NULLABLE",
},
{
"name": "lat_max",
"type": "FLOAT",
"description": "maximum latitude for that region",
"mode": "NULLABLE",
},
{
"name": "lat_min",
"type": "FLOAT",
"description": "minimum latitude for that region",
"mode": "NULLABLE",
},
{
"name": "lng_max",
"type": "FLOAT",
"description": "maximum longitude for that region",
"mode": "NULLABLE",
},
{
"name": "lng_min",
"type": "FLOAT",
"description": "minimum longitude for that region",
"mode": "NULLABLE",
},
{
"name": "lat_avg",
"type": "FLOAT",
"description": "average latitude for that region",
"mode": "NULLABLE",
},
{
"name": "lng_avg",
"type": "FLOAT",
"description": "average longitude for that region",
"mode": "NULLABLE",
},
{
"name": "yearly_sunlight_kwh_kw_threshold_avg",
"type": "FLOAT",
"description": "75% of the optimimum sunlight in the county containing that zip code",
"mode": "NULLABLE",
},
{
"name": "count_qualified",
"type": "INTEGER",
"description": "# of buildings in Google Maps that are suitable for solar",
"mode": "NULLABLE",
},
{
"name": "percent_covered",
"type": "FLOAT",
"description": "% of buildings in Google Maps covered by Project Sunroof",
"mode": "NULLABLE",
},
{
"name": "percent_qualified",
"type": "FLOAT",
"description": "% of buildings covered by Project Sunroof that are suitable for solar",
"mode": "NULLABLE",
},
{
"name": "number_of_panels_n",
"type": "INTEGER",
"description": "# of solar panels potential for north-facing roof space in that region, assuming 1.650m x 0.992m panels",
"mode": "NULLABLE",
},
{
"name": "number_of_panels_s",
"type": "INTEGER",
"description": "# of solar panels potential for south-facing roof space in that region, assuming 1.650m x 0.992m panels",
"mode": "NULLABLE",
},
{
"name": "number_of_panels_e",
"type": "INTEGER",
"description": "# of solar panels potential for east-facing roof space in that region, assuming 1.650m x 0.992m panels",
"mode": "NULLABLE",
},
{
"name": "number_of_panels_w",
"type": "INTEGER",
"description": "# of solar panels potential for west-facing roof space in that region, assuming 1.650m x 0.992m panels",
"mode": "NULLABLE",
},
{
"name": "number_of_panels_f",
"type": "INTEGER",
"description": "# of solar panels potential for flat roof space in that region, assuming 1.650m x 0.992m panels",
"mode": "NULLABLE",
},
{
"name": "number_of_panels_median",
"type": "INTEGER",
"description": "# of panels that fit on the median roof",
"mode": "NULLABLE",
},
{
"name": "number_of_panels_total",
"type": "INTEGER",
"description": "# of solar panels potential for all roof space in that region, assuming 1.650m 0.992m panels",
"mode": "NULLABLE",
},
{
"name": "kw_median",
"type": "FLOAT",
"description": "kW of solar potential for the median building in that region (assuming 250 watts per panel)",
"mode": "NULLABLE",
},
{
"name": "kw_total",
"type": "FLOAT",
"description": "# of kW of solar potential for all roof types in that region (assuming 250 watts per panel)",
"mode": "NULLABLE",
},
{
"name": "yearly_sunlight_kwh_n",
"type": "FLOAT",
"description": "total solar energy generation potential for north-facing roof space in that region",
"mode": "NULLABLE",
},
{
"name": "yearly_sunlight_kwh_s",
"type": "FLOAT",
"description": "total solar energy generation potential for south-facing roof space in that region",
"mode": "NULLABLE",
},
{
"name": "yearly_sunlight_kwh_e",
"type": "FLOAT",
"description": "total solar energy generation potential for east-facing roof space in that region",
"mode": "NULLABLE",
},
{
"name": "yearly_sunlight_kwh_w",
"type": "FLOAT",
"description": "total solar energy generation potential for west-facing roof space in that region",
"mode": "NULLABLE",
},
{
"name": "yearly_sunlight_kwh_f",
"type": "FLOAT",
"description": "total solar energy generation potential for flat roof space in that region",
"mode": "NULLABLE",
},
{
"name": "yearly_sunlight_kwh_median",
"type": "FLOAT",
"description": "kWh/kw/yr for the median roof, in DC (not AC) terms",
"mode": "NULLABLE",
},
{
"name": "yearly_sunlight_kwh_total",
"type": "FLOAT",
"description": "total solar energy generation potential for all roof space in that region",
"mode": "NULLABLE",
},
{
"name": "install_size_kw_buckets_json",
"type": "STRING",
"description": "# of buildings with potential for various installation size buckets. Format is a JSON array, where each element is a tuple containing (1) lower bound of bucket, in kW, and (2) number of buildings in that bucket.",
"mode": "NULLABLE",
},
{
"name": "carbon_offset_metric_tons",
"type": "FLOAT",
"description": "The potential carbon dioxide abatement of the solar capacity that meets the technical potential criteria. The calculation uses eGRID subregion CO2 equivalent non-baseload output emission rates. https://www.epa.gov/sites/production/files/2015-10/documents/egrid2012_summarytables_0.pdf",
"mode": "NULLABLE",
},
{
"name": "existing_installs_count",
"type": "INTEGER",
"description": "# of buildings estimated to have a solar installation, at time of data collection",
"mode": "NULLABLE",
},
{
"name": "center_point",
"type": "GEOGRAPHY",
"description": "",
"mode": "NULLABLE",
},
],
)
transform_csv >> load_to_bq