/
bifacialvf.py
1211 lines (1048 loc) · 50.3 KB
/
bifacialvf.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# This program calculates irradiances on the front and back surfaces
# of bifacial PV modules.
#
# Key dimensions and nomenclature:
# tilt = PV module tilt angle from horizontal, in degrees
# sazm = PV module surface azimuth from north, in degrees
# 1.0 = normalized PV module/panel slant height
# C = ground clearance of PV module, in PV module/panel slant heights
# D = distance between rows, from rear of module to front of module in next
# row, in PV module/panel slant heights
# h = sin(tilt), vertical PV module dimension, in PV module/panel slant
# heights
# x1 = cos(tilt), horizontal PV module dimension, in PV module/panel slant
# heights
# pitch = x1 + D, row-to-row distance, from front of module to front of
# module in next row, in PV module/panel slant heights
# sensorsy = number of horzontal results, usually corresponding to the rows
# of cells in a PV module/panel along the slope of the sampled axis.
# PVfrontSurface = PV module front surface material type, either "glass" or
# "ARglass"
# PVbackSurface = PV module back surfac ematerial type, either "glass" or
# "ARglass"
#
# Program flow consists of:
# a. Calculate irradiance distribution on ground
# b. Calculate AOI corrected irradiance on front of PV module, and
# irradiance reflected from front of PV module
# c. Calculate irradiance on back of PV module
# ensure python3 compatible division and printing
from __future__ import division, print_function, absolute_import
import math
import csv
import pvlib
import os
import numpy as np
import pandas as pd
from tqdm import tqdm
from bifacialvf.vf import getBackSurfaceIrradiances, getFrontSurfaceIrradiances
from bifacialvf.vf import getGroundShadeFactors
from bifacialvf.vf import getSkyConfigurationFactors
from bifacialvf.vf import trackingBFvaluescalculator, rowSpacing
from bifacialvf.vf import getSkyConfigurationFactors2, getGroundShadeFactors2
from bifacialvf.sun import perezComp, sunIncident
from bifacialvf.sun import sunrisecorrectedsunposition # hrSolarPos, solarPos
# Electrical Mismatch Calculation
from bifacialvf.analysis import analyseVFResultsBilInterpol
from bifacialvf.analysis import analyseVFResultsPVMismatch
# import bifacialvf.analysis as analysis
def readWeatherFile(weatherFile=None, source=None):
'''
## Read Weatherfile data using pvlib.
Parameters
----------
weatherFile : str
File containing the weather information. EPW, TMY3, SAM/PSM3 accepted.
source : str
To help identify different types of .csv files. If None, it assumes
it is a SAM-style formated data. Current options: 'TMY3',
'EPW', 'SAM' or 'PSM3' (SAM and PSM3 are same format)
Returns
dataframe, meta
'''
import pandas as pd
# TODO: Completely deprecate/remove the readtmy3 graphical file picker
if weatherFile is None:
print("No weather file passed. Use our graphical file picker to ",
"select a TMY3 fie-type. This function will be deprecated next",
" release. ")
(WeatherDF, meta) = pvlib.iotools.read_tmy3(weatherFile)
if source is None:
if weatherFile[-3:].lower() == 'epw':
source = 'EPW'
else:
print('Warning: CSV file passed for input. Assuming it is SAM' +
'style format. Otherwise, use input `source` to specify.' +
'options: EPW, SAM, TMY3.')
source = 'SAM'
if source == 'EPW':
(WeatherDF, meta) = pvlib.iotools.read_epw(weatherFile)
# rename different field parameters to match dni, dhi, Tdry, Wspd
# pvlib uses -1hr offset that needs to be un-done.
WeatherDF.index = WeatherDF.index+pd.Timedelta(hours=1)
WeatherDF.rename(columns={'temp_air': 'Tdry',
'wind_speed': 'Wspd',
'albedo': 'Albedo'}, inplace=True)
elif source == 'SAM' or source == 'PSM3':
(WeatherDF, meta) = pvlib.iotools.read_psm3(weatherFile,
map_variables=True)
elif source == 'TMY3':
(WeatherDF, meta) = pvlib.iotools.read_tmy3(weatherFile)
WeatherDF.rename(columns={'DNI': 'dni',
'GHI': 'ghi',
'DHI': 'dhi',
'DryBulb': 'Tdry',
'Alb': 'Albedo'}, inplace=True)
else:
raise Exception('Incorrect extension for Weatherfile to read. ')
return WeatherDF, meta
def fixintervalTMY(WeatherDF, meta):
'''
If data is passed in TMY3 format but has a interval smaller than 1 HR, this
function fixes the timestamps from the already imported TMY3 data with
readInputTMY. It assume there is column labeld 'Time (HH:MM)' in WeatherDF
'''
import pandas as pd
WeatherDF['Datetime'] = pd.to_datetime(WeatherDF['Date (MM/DD/YYYY)'] +
' ' + WeatherDF['Time (HH:MM)'])
WeatherDF = WeatherDF.set_index('Datetime').tz_localize(
int(meta['TZ'] * 3600))
return WeatherDF, meta
def getEPW(lat=None, lon=None, GetAll=False, path=None):
"""
Subroutine to download nearest epw files to latitude and long. provided,
into the directory `EPWs`
Code based on github/aahoo.
.. warning::
verify=false is required to operate within NREL's network.
to avoid annoying warnings, insecurerequestwarning is disabled
currently this function is not working within NREL's network. Annoying!
Parameters
----------
lat : decimal
Used to find closest EPW file.
lon : decimal
Longitude value to find closest EPW file.
GetAll : boolean
Download all available files. Note that no epw file will be loaded
into memory.
"""
import requests
import re
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
hdr = {'User-Agent': "Magic Browser",
'Accept':
'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
}
def _setPath(path):
"""
setPath - move path and working directory
"""
path = os.path.abspath(path)
print('path = ' + path)
try:
os.chdir(path)
except:
print("Error on Path passed")
# check for path in the new Radiance directory:
def _checkPath(path): # create the file structure if it doesn't exist
if not os.path.exists(path):
os.makedirs(path)
print('Making path: '+path)
_checkPath('EPWs')
if path is None:
_setPath(os.getcwd())
else:
_setPath(path)
# create a directory and write the name of directory here
path_to_save = os.path.join('EPWs')
if not os.path.exists(path_to_save):
os.makedirs(path_to_save)
def _returnEPWnames():
''' return a DF with the name, lat, lon, url of available files'''
r = requests.get(
('https://github.com/NREL/EnergyPlus/raw/develop/' +
'weather/master.geojson'),
verify=False)
data = r.json() # metadata for available files
# download lat/lon and url details for each .epw file into a dataframe
df = pd.DataFrame({'url': [], 'lat': [], 'lon': [], 'name': []})
for location in data['features']:
match = re.search(r'href=[\'"]?([^\'" >]+)',
location['properties']['epw'])
if match:
url = match.group(1)
name = url[url.rfind('/') + 1:]
lontemp = location['geometry']['coordinates'][0]
lattemp = location['geometry']['coordinates'][1]
dftemp = pd.DataFrame({'url': [url], 'lat': [lattemp],
'lon': [lontemp], 'name': [name]})
# df = df.append(dftemp, ignore_index=True)
df = pd.concat([df, dftemp], ignore_index=True)
return df
def _findClosestEPW(lat, lon, df):
# locate the record with the nearest lat/lon
errorvec = np.sqrt(np.square(df.lat - lat) + np.square(df.lon - lon))
index = errorvec.idxmin()
url = df['url'][index]
name = df['name'][index]
return url, name
def _downloadEPWfile(url, path_to_save, name):
r = requests.get(url, verify=False, headers=hdr)
if r.ok:
filename = os.path.join(path_to_save, name)
# py2 and 3 compatible: binary write, encode text first
with open(filename, 'wb') as f:
f.write(r.text.encode('ascii', 'ignore'))
print(' ... OK!')
else:
print(' connection error status code: %s' % (r.status_code))
r.raise_for_status()
# Get the list of EPW filenames and lat/lon
df = _returnEPWnames()
# find the closest EPW file to the given lat/lon
if (lat is not None) & (lon is not None) & (GetAll is False):
url, name = _findClosestEPW(lat, lon, df)
# download the EPW file to the local drive.
print('Getting weather file: ' + name)
_downloadEPWfile(url, path_to_save, name)
# self.epwfile = os.path.join('EPWs', name)
epwfile = os.path.join('EPWs', name)
elif GetAll is True:
if input('Downloading ALL EPW files available. OK? [y/n]') == 'y':
# get all of the EPW files
for index, row in df.iterrows():
print('Getting weather file: ' + row['name'])
_downloadEPWfile(row['url'], path_to_save, row['name'])
# self.epwfile = None
epwfile = None
else:
print('Nothing returned. Proper usage: epwfile = getEPW(lat,lon)')
# self.epwfile = None
epwfile = None
# return self.epwfile
return epwfile
def simulate(WeatherDF, meta, writefiletitle=None, tilt=0, sazm=180,
clearance_height=None, hub_height=None,
pitch=None, rowType='interior', transFactor=0.01, sensorsy=6,
PVfrontSurface='glass', PVbackSurface='glass', albedo=None,
tracking=False, backtrack=True, limit_angle=45,
calculatePVMismatch=False, cellsnum=72,
portraitorlandscape='landscape', bififactor=1.0,
calculateBilInterpol=False, BilInterpolParams=None,
deltastyle='SAM', agriPV=False):
'''
Description
-----------
Main function to run the bifacialvf routines
Parameters
----------
WeatherDF (pd.DataFrame): A pandas DataaFrame containing for each timestep
columns: dni, dhi, it can also have Tdry, Wspd, zenith, azimuth,
meta (dict): A dictionary conatining keys: 'latitude', 'longitude', 'TZ',
'Name'
writefiletitle: name of output file
tilt: tilt angle in degrees. Not used for tracking
sazm: surface azimuth orientation in degrees east of north. For
tracking this is the tracker axis orientation
C: normalized ground clearance. For trackers, this is the module
height at zero tilt
pitch: row-to-row normalized distance. = 1/GCR
transFactor: PV module transmission fraction. Default 1% (0.01)
sensorsy: Number of points along the module chord to return
irradiance values. Default 6 (1-up landscape module)
limit_angle: 1-axis tracking maximum limits of rotation
tracking, backtrack: boolean to enable 1-axis tracking and pvlib
backtracking algorithm, respectively
albedo: If a value is passed, that value will be used for all the
simulations. If None is passed (or albedo argument is not passed),
program will search the input weather dataframe for 'Albedo' column and
use those values.
New Parameters:
# Dictionary input example:
# calculateBilInterpol = {'interpolA':0.005, 'IVArray':None,
'beta_voc_all':None, 'm_all':None, 'bee_all':None}
Returns
-------
none
'''
if (clearance_height is None) & (hub_height is not None):
clearance_height = hub_height
if tracking is False:
print('Warning: hub_height passed and is being used as ',
'clearance_height for the fixed_tilt routine.')
elif (clearance_height is None) & (hub_height is None):
raise Exception('No row distance specified in either D or pitch')
elif (clearance_height is not None) & (hub_height is None):
if tracking is True:
print('Warning: clearance_height passed and is being used as ',
'hub_height for the tracking routine')
else:
print('Warning: clearance_height and hub_height passed in. Using '
+ ('hub_height' if tracking else 'clearance_height'))
if tracking is True:
clearance_height = hub_height
C = clearance_height
heightlabel = 'Clearance_Height'
if tracking is True:
axis_tilt = 0 # only allows for zero north-south tilt with SAT
# limit_angle = 45 # maximum tracker rotation
axis_azimuth = sazm # axis_azimuth is degrees east of North
tilt = 0 # start with tracker tilt = 0
hub_height = C # Ground clearance at tilt = 0. C >= 0.5
stowingangle = 90
if hub_height < 0.5:
print('Warning: tracker hub height C < 0.5 may result in ground ' +
'clearance errors')
heightlabel = 'Hub_Height'
D = pitch - math.cos(tilt / 180.0 * math.pi)
if writefiletitle is None:
writefiletitle = "data/Output/TEST.csv"
noRows, noCols = WeatherDF.shape
lat = meta['latitude']
lng = meta['longitude']
if 'TZ' in meta:
tz = meta['TZ']
if 'Time Zone' in meta:
tz = meta['Time Zone']
meta['TZ'] = tz
# TODO: Make this part of weatherfile reading/input needs
if 'City' in meta:
name = meta['City']
if 'Name' in meta:
name = meta['Name']
if 'city' in meta:
name = meta['city']
# infer the data frequency in minutes
dataInterval = (WeatherDF.index[1]-WeatherDF.index[0]).total_seconds()/60
if not (('azimuth' in WeatherDF) and ('zenith' in WeatherDF) and
('elevation' in WeatherDF)):
solpos, sunup = sunrisecorrectedsunposition(WeatherDF, meta,
deltastyle=deltastyle)
WeatherDF['zenith'] = np.radians(solpos['zenith'])
WeatherDF['azimuth'] = np.radians(solpos['azimuth'])
WeatherDF['elevation'] = np.radians(solpos['elevation'])
if tracking is True:
if not (('trackingdata_surface_tilt' in WeatherDF) and
('trackingdata_surface_azimuth' in WeatherDF)):
gcr = 1/pitch
trackingdata = (
pvlib.tracking.singleaxis(np.degrees(WeatherDF['zenith']),
np.degrees(WeatherDF['azimuth']),
axis_tilt, axis_azimuth,
limit_angle, backtrack, gcr))
trackingdata.surface_tilt.fillna(stowingangle, inplace=True)
WeatherDF['trackingdata_surface_tilt'] = trackingdata[
'surface_tilt']
WeatherDF['trackingdata_surface_azimuth'] = (
trackingdata['surface_azimuth'])
[WeatherDF['C'], WeatherDF['D']] = trackingBFvaluescalculator(
WeatherDF['trackingdata_surface_tilt'], hub_height, pitch)
# Check what Albedo to use:
if albedo is None:
if 'Albedo' in WeatherDF:
print("Using albedo from Weather File file.")
print("Note that at the moment, no validation check is done",
"in the albedo data, so we assume it's correct and valid.\n")
useTMYalbedo = True
else:
print("No albedo value set or 'Albedo' column in DF",
"Setting albedo default to 0.2\n ")
albedo = 0.2
useTMYalbedo = False
else:
if 'Albedo' in WeatherDF:
print("Albedo value passed, but also present in Weather File.",
"Using albedo value passed. To use the ones in Weather File",
" re-run simulation with albedo=None\n")
useTMYalbedo = False
# Distance between rows for no shading on Dec 21 at 9 am
print(" ")
print("********* ")
print("Running Simulation for Weather File: ")
print("Location: ", name)
print("Lat: ", lat, " Long: ", lng, " Tz ", tz)
print("Parameters: tilt: ", tilt, " Sazm: ", sazm, " ",
heightlabel, ": ", C, " Pitch: ", pitch, " Row type: ", rowType,
" Albedo: ", albedo)
print("Saving into", writefiletitle)
print(" ")
print(" ")
# Distance between rows for no shading on Dec 21 at 9 am
DD = rowSpacing(tilt, sazm, lat, lng, tz, 9, 0.0)
print("Distance between rows for no shading on Dec 21 at 9 am " +
"solar time = ", DD)
print("Actual distance between rows = ", D)
print(" ")
if tracking is False:
# Sky configuration factors are the same for all times, only based on
# geometry and row type
[rearSkyConfigFactors, frontSkyConfigFactors] = (
getSkyConfigurationFactors(rowType, tilt, C, D))
# Create WriteFile and write labels at this time
# Check that the save directory exists, unless it's in root
savedirectory = os.path.dirname(writefiletitle)
if ((not os.path.exists(savedirectory)) and (savedirectory != '')):
os.makedirs(savedirectory)
with open(writefiletitle, 'w') as csvfile:
sw = csv.writer(csvfile, delimiter=',', quotechar='|',
quoting=csv.QUOTE_MINIMAL, lineterminator='\n')
# Write Simulation Parameters (from setup file)
if tracking is False and backtrack is True:
print("Warning: tracking=False, but backtracking=True. ",
"Setting backtracking=False because it doesn't make ",
"sense to backtrack on fixed tilt systems.")
backtrack = False
outputheader = ['Latitude(deg)', 'Longitude(deg)', 'Time Zone',
'Tilt(deg)', 'PV Azimuth(deg)', heightlabel, 'Pitch',
'RowType(first interior last single)',
'TransmissionFactor(open area fraction)',
'sensorsy(# hor rows in panel)',
'PVfrontSurface(glass or ARglass)',
'PVbackSurface(glass or ARglass)',
'Albedo', 'Tracking', 'backtracking']
outputheadervars = [lat, lng, tz, tilt, sazm, clearance_height, pitch,
rowType, transFactor, sensorsy, PVfrontSurface,
PVbackSurface, albedo, tracking, backtrack]
sw.writerow(outputheader)
sw.writerow(outputheadervars)
# Write Results names"
allrowfronts = []
allrowbacks = []
for k in range(0, sensorsy):
allrowfronts.append("No_"+str(k+1)+"_RowFrontGTI")
allrowbacks.append("No_"+str(k+1)+"_RowBackGTI")
outputtitles = ['date', 'dni', 'dhi',
'albedo', 'decHRs', 'ghi', 'inc', 'zen', 'azm',
'pvFrontSH', 'aveFrontGroundGHI', 'GTIfrontBroadBand',
'pvBackSH', 'aveBackGroundGHI', 'GTIbackBroadBand',
'maxShadow', 'Tamb', 'VWind']
outputtitles += allrowfronts
outputtitles += allrowbacks
if tracking is True:
print(" *** IMPORTANT --> THIS SIMULATION Has Tracking Activated")
print("Backtracking Option is set to: ", backtrack)
outputtitles += ['tilt']
outputtitles += ['sazm']
outputtitles += ['height']
outputtitles += ['D']
if agriPV:
print("Saving Ground Irradiance Values for AgriPV Analysis. ")
outputtitles += ['Ground Irradiance Values']
sw.writerow(outputtitles)
# Loop through file.
rl = 0 # TODO: this is not needed I think?
for rl in tqdm(range(noRows)):
myTimestamp = WeatherDF.index[rl]
hour = myTimestamp.hour
minute = myTimestamp.minute
dni = WeatherDF.dni[rl]
dhi = WeatherDF.dhi[rl]
if 'Tdry' in WeatherDF:
Tamb = WeatherDF.Tdry[rl]
else:
Tamb = 0
if 'Wspd' in WeatherDF:
VWind = WeatherDF.Wspd[rl]
else:
VWind = 0
if useTMYalbedo:
albedo = WeatherDF.Albedo[rl]
zen = WeatherDF['zenith'][rl]
azm = WeatherDF['azimuth'][rl]
elv = WeatherDF['elevation'][rl]
if (zen < 0.5 * math.pi): # If daylight hours
# a. CALCULATE THE IRRADIANCE DISTRIBUTION ON THE GROUND
# ********************************************************
# double[] rearGroundGHI = new double[100],
# frontGroundGHI = new double[100]
# For global horizontal irradiance for each of 100 ground
# segments, to the rear and front of front of row edge
# Determine where on the ground the direct beam is shaded for
# a sun elevation and azimuth
# int[] rearGroundSH = new int[100],
# frontGroundSH = new int[100]
# Front and rear row-to-row spacing divided into 100 segments,
# (later becomes 1 if direct beam is shaded, 0 if not shaded)
# double pvFrontSH = 0.0, pvBackSH = 0.0, maxShadow
# Initialize fraction of PV module front and back surfaces
# that are shaded to zero (not shaded), and maximum shadow
# projected from front of row.
# TRACKING ROUTINE CALULATING GETSKYCONFIGURATION FACTORS
if tracking is True:
tilt = WeatherDF['trackingdata_surface_tilt'][rl]
sazm = WeatherDF['trackingdata_surface_azimuth'][rl]
C = WeatherDF['C'][rl]
D = WeatherDF['D'][rl]
# Sky configuration factors are the same for all times,
# only based on geometry and row type
[rearSkyConfigFactors, frontSkyConfigFactors] = (
getSkyConfigurationFactors(rowType, tilt, C, D))
rearGroundGHI = []
frontGroundGHI = []
pvFrontSH, pvBackSH, maxShadow, rearGroundSH, frontGroundSH = (
getGroundShadeFactors(rowType, tilt, C, D, elv, azm, sazm))
# Sum the irradiance components for each of the ground
# segments, to the front and rear of the front of the PV row
# double iso_dif = 0.0, circ_dif = 0.0, horiz_dif = 0.0,
# grd_dif = 0.0, beam = 0.0 # For calling PerezComp to break
# diffuse into components for zero tilt (horizontal)
ghi, iso_dif, circ_dif, horiz_dif, grd_dif, beam = (
perezComp(dni, dhi, albedo, zen, 0.0, zen))
for k in range(0, 100):
# Add diffuse sky component viewed by ground
rearGroundGHI.append(iso_dif * rearSkyConfigFactors[k])
# Add beam and circumsolar component if not shaded
if (rearGroundSH[k] == 0):
rearGroundGHI[k] += beam + circ_dif
# Add beam and circumsolar component transmitted thru
# module spacing if shaded
else:
rearGroundGHI[k] += (beam + circ_dif) * transFactor
# Add diffuse sky component viewed by ground
frontGroundGHI.append(iso_dif * frontSkyConfigFactors[k])
# Add beam and circumsolar component if not shaded
if (frontGroundSH[k] == 0):
frontGroundGHI[k] += beam + circ_dif
# Add beam and circumsolar component transmitted thru
# module spacing if shaded
else:
frontGroundGHI[k] += (beam + circ_dif) * transFactor
# b. CALCULATE THE AOI CORRECTED IRRADIANCE ON THE FRONT OF
# THE PV MODULE, AND IRRADIANCE REFLECTED FROM FRONT OF PV
# MODULE ***************************
# double[] frontGTI = new double[sensorsy],
# frontReflected = new double[sensorsy]
# double aveGroundGHI = 0.0
# Average GHI on ground under PV array
aveGroundGHI, frontGTI, frontReflected = (
getFrontSurfaceIrradiances(rowType, maxShadow,
PVfrontSurface, tilt, sazm, dni,
dhi, C, D, albedo, zen, azm,
sensorsy, pvFrontSH,
frontGroundGHI))
# For calling PerezComp to break diffuse into components for
inc, tiltr, sazmr = sunIncident(0, tilt, sazm, 45.0, zen, azm)
save_inc = inc
# Call to get components for the tilt
gtiAllpc, iso_dif, circ_dif, horiz_dif, grd_dif, beam = (
perezComp(dni, dhi, albedo, inc, tiltr, zen))
save_gtiAllpc = gtiAllpc
# CALCULATE THE AOI CORRECTED IRRADIANCE ON PV MODULE'S BACK
# double[] backGTI = new double[sensorsy]
backGTI, aveGroundGHI = getBackSurfaceIrradiances(
rowType, maxShadow, PVbackSurface, tilt, sazm, dni, dhi,
C, D, albedo, zen, azm, sensorsy, pvBackSH, rearGroundGHI,
frontGroundGHI, frontReflected, offset=0)
# For calling PerezComp to break diffuse into components for
inc, tiltr, sazmr = sunIncident(0, 180.0-tilt, sazm-180.0,
45.0, zen, azm)
# Call to get components for the tilt
gtiAllpc, iso_dif, circ_dif, horiz_dif, grd_dif, beam = (
perezComp(dni, dhi, albedo, inc, tiltr, zen))
# Write output
decHRs = hour - 0.5 * dataInterval / 60.0 + minute / 60.0
ghi_calc = dni * math.cos(zen) + dhi
incd = save_inc * 180.0 / math.pi
zend = zen * 180.0 / math.pi
azmd = azm * 180.0 / math.pi
outputvalues = [myTimestamp, dni, dhi, albedo, decHRs,
ghi_calc, incd, zend, azmd, pvFrontSH,
aveGroundGHI, save_gtiAllpc, pvBackSH,
aveGroundGHI, gtiAllpc, maxShadow, Tamb, VWind]
frontGTIrow = []
backGTIrow = []
# INVERTING Sensor measurements for tracking when tracker
# facing the west side.
# TODO: Modify so it works with axis_azm different of 0
# (sazm = 90 or 270 only)
if tracking is True:
if sazm == 270.0:
rangestart = sensorsy-1
rangeend = -1
steprange = -1
rearGroundGHI.reverse()
else:
rangestart = 0
rangeend = sensorsy
steprange = 1
else:
rangestart = 0
rangeend = sensorsy
steprange = 1
for k in range(rangestart, rangeend, steprange):
frontGTIrow.append(frontGTI[k])
backGTIrow.append(backGTI[k])
outputvalues += frontGTIrow
outputvalues += backGTIrow
if tracking is True:
outputvalues.append(tilt)
outputvalues.append(sazm)
outputvalues.append(C)
outputvalues.append(D)
if agriPV:
outputvalues.append(str(rearGroundGHI).replace(',', ''))
sw.writerow(outputvalues)
# End of daylight if loop
# End of WeatherDF rows of data
if calculateBilInterpol is True:
analyseVFResultsBilInterpol(filename=writefiletitle,
portraitorlandscape=portraitorlandscape,
bififactor=bififactor,
writefilename=writefiletitle)
if calculatePVMismatch is True:
analyseVFResultsPVMismatch(filename=writefiletitle,
portraitorlandscape=portraitorlandscape,
bififactor=bififactor, numcells=cellsnum,
writefilename=writefiletitle)
print("Finished")
return
def simulate2(WeatherDF, meta, writefiletitle=None, tilt=0, sazm=180,
clearance_height=None, hub_height=None,
pitch=None, rowType='interior', transFactor=0.01, sensorsy=6,
PVfrontSurface='glass', PVbackSurface='glass', albedo=None,
tracking=False, backtrack=True, limit_angle=60,
calculatePVMismatch=False, cellsnum=72,
portraitorlandscape='landscape', bififactor=1.0,
calculateBilInterpol=False, BilInterpolParams=None,
deltastyle='SAM', agriPV=False):
'''
Description
-----------
Main function to run the bifacialvf routines
Parameters
----------
WeatherDF : pd.DataFrame)
A pandas DataaFrame containing for each timestep columns:
dni, dhi, it can also have Tdry, Wspd, zenith, azimuth,
meta : (dict)
A dictionary conatining keys: 'latitude', 'longitude', 'TZ', 'Name'
writefiletitle : str
Name of output file
tilt : float
Tilt angle in degrees. Not used for tracking
sazm : float
Surface azimuth orientation in degrees east of north. For tracking this
is the tracker axis orientation
C : float
Normalized ground clearance. For trackers, this is the module height at
zero tilt
pitch : float
Row-to-row normalized distance. = 1/GCR
transFactor : float
PV module transmission fraction. Default 1% (0.01)
sensorsy : int
Number of points along the module chord to return irradiance values.
Default 6 (1-up landscape module)
limit_angle : float
1-axis tracking maximum limits of rotation
tracking : BOOL
Boolean to enable 1-axis tracking and pvlib
backtrack : BOOL
Enables backtracking algorithm from pvlib.
albedo : float or None
If a value is passed, that value will be used for all the simulations.
If None is passed (or albedo argument is not passed), program will
search the dataframe for the "Albedo" column and use those values
Bilinear Interpolation Parameters:
# calculateBilInterpol = {'interpolA':0.005, 'IVArray':None,
'beta_voc_all':None, 'm_all':None, 'bee_all':None}
Returns
-------
none
'''
if (clearance_height is None) & (hub_height is not None):
clearance_height = hub_height
if tracking is False:
print('Warning: hub_height passed and is being used as ',
'clearance_height for the fixed_tilt routine.')
elif (clearance_height is None) & (hub_height is None):
raise Exception('No row distance specified in either D or pitch')
elif (clearance_height is not None) & (hub_height is None):
if tracking is True:
print('Warning: clearance_height passed and is being used as ',
'hub_height for the tracking routine')
else:
print('Warning: clearance_height and hub_height passed in. Using '
+ ('hub_height' if tracking else 'clearance_height'))
if tracking is True:
clearance_height = hub_height
C = clearance_height
heightlabel = 'Clearance_Height'
if tracking is True:
axis_tilt = 0 # only allows for zero north-south tilt with SAT
# limit_angle = 45 # maximum tracker rotation
axis_azimuth = sazm # axis_azimuth is degrees east of North
tilt = 0 # start with tracker tilt = 0
hub_height = C # Ground clearance at tilt = 0. C >= 0.5
stowingangle = 90
if hub_height < 0.5:
print('Warning: tracker hub height C < 0.5 may result in ground ' +
'clearance errors')
heightlabel = 'Hub_Height'
D = pitch - math.cos(tilt / 180.0 * math.pi)
if writefiletitle is None:
writefiletitle = "data/Output/TEST.csv"
noRows, noCols = WeatherDF.shape
lat = meta['latitude']
lng = meta['longitude']
if 'TZ' in meta:
tz = meta['TZ']
if 'Time Zone' in meta:
tz = meta['Time Zone']
meta['TZ'] = tz
# TODO: Make this part of weatherfile reading/input needs
if 'City' in meta:
name = meta['City']
if 'Name' in meta:
name = meta['Name']
if 'city' in meta:
name = meta['city']
# infer the data frequency in minutes
dataInterval = (WeatherDF.index[1]-WeatherDF.index[0]).total_seconds()/60
if not (('azimuth' in WeatherDF) and ('zenith' in WeatherDF) and
('elevation' in WeatherDF)):
solpos, sunup = sunrisecorrectedsunposition(WeatherDF, meta,
deltastyle=deltastyle)
WeatherDF['zenith'] = np.radians(solpos['zenith'])
WeatherDF['azimuth'] = np.radians(solpos['azimuth'])
WeatherDF['elevation'] = np.radians(solpos['elevation'])
if tracking is True:
# If Tracker's tilt and surface azimuth are not in Weather File,
# it calculates them.
if not (('tilt' in WeatherDF) and ('sazm' in WeatherDF)):
gcr = 1/pitch
trackingdata = (
pvlib.tracking.singleaxis(np.degrees(WeatherDF['zenith']),
np.degrees(WeatherDF['azimuth']),
axis_tilt, axis_azimuth,
limit_angle, backtrack, gcr))
trackingdata.surface_tilt.fillna(stowingangle, inplace=True)
WeatherDF['tilt'] = trackingdata['surface_tilt']
WeatherDF['sazm'] = trackingdata['surface_azimuth']
[WeatherDF['C'], WeatherDF['D']] = trackingBFvaluescalculator(
WeatherDF['tilt'], hub_height, pitch)
else: # Fixed itlt
WeatherDF['C'] = C
WeatherDF['D'] = D
WeatherDF['sazm'] = sazm
WeatherDF['tilt'] = tilt
# Check what Albedo to se:
if albedo is None:
if 'Albedo' in WeatherDF:
print("Using albedo from Weather File file.")
print("Note that at the moment, no validation check is done",
"in the albedo data, so we assume it's correct and valid.\n")
useTMYalbedo = True
else:
print("No albedo value set or included in Weatehr dataframe ",
"as 'Albedo' column. Setting albedo default to 0.2\n ")
albedo = 0.2
useTMYalbedo = False
else:
if 'Albedo' in WeatherDF:
print("Albedo value passed, but also present in Weather ",
"dataframe. Using albedo value passed. To use the ",
"Weather dataframe's value, re-run the sim with input ",
"albedo=None\n")
useTMYalbedo = False
# Distance between rows for no shading on Dec 21 at 9 am
print(" ")
print("********* ")
print("Running Simulation for Weather File: ")
print("Location: ", name)
print("Lat: ", lat, " Long: ", lng, " Tz ", tz)
print("Parameters: tilt: ", tilt, " Sazm: ", sazm, " ",
heightlabel, ": ", C, " Pitch: ", pitch, " Row type: ", rowType,
" Albedo: ", albedo)
print("Saving into", writefiletitle)
print(" ")
print(" ")
# Distance between rows for no shading on Dec 21 at 9 am
DD = rowSpacing(tilt, sazm, lat, lng, tz, 9, 0.0)
print("Distance between rows for no shading on Dec 21 at 9 am " +
"solar time = ", DD)
print("Actual distance between rows = ", D)
print(" ")
# Create WriteFile and write labels at this time
# Check that the save directory exists, unless it's in root
savedirectory = os.path.dirname(writefiletitle)
if ((not os.path.exists(savedirectory)) and (savedirectory != '')):
os.makedirs(savedirectory)
with open(writefiletitle, 'w') as csvfile:
sw = csv.writer(csvfile, delimiter=',', quotechar='|',
quoting=csv.QUOTE_MINIMAL, lineterminator='\n')
# Write Simulation Parameters (from setup file)
if tracking is False and backtrack is True:
print("Warning: tracking=False, but backtracking=True. ",
"Setting backtracking=False because it doesn't make ",
"sense to backtrack on fixed tilt systems.")
backtrack = False
outputheader = ['Latitude(deg)', 'Longitude(deg)', 'Time Zone',
'Tilt(deg)', 'PV Azimuth(deg)', heightlabel, 'Pitch',
'RowType(first interior last single)',
'TransmissionFactor(open area fraction)',
'sensorsy(# hor rows in panel)',
'PVfrontSurface(glass or ARglass)',
'PVbackSurface(glass or ARglass)',
'Albedo', 'Tracking', 'backtracking']
outputheadervars = [lat, lng, tz, tilt, sazm, clearance_height, pitch,
rowType, transFactor, sensorsy, PVfrontSurface,
PVbackSurface, albedo, tracking, backtrack]
sw.writerow(outputheader)
sw.writerow(outputheadervars)
# Write Results names"
allrowfronts = []
allrowbacks = []
for k in range(0, sensorsy):
allrowfronts.append("No_"+str(k+1)+"_RowFrontGTI")
allrowbacks.append("No_"+str(k+1)+"_RowBackGTI")
outputtitles = ['date', 'dni', 'dhi',
'albedo', 'decHRs', 'ghi', 'inc', 'zen', 'azm',
'pvFrontSH', 'aveFrontGroundGHI', 'GTIfrontBroadBand',
'pvBackSH', 'aveBackGroundGHI', 'GTIbackBroadBand',
'maxShadow', 'Tamb', 'VWind']
outputtitles += allrowfronts
outputtitles += allrowbacks
if tracking is True:
print(" *** IMPORTANT --> THIS SIMULATION Has Tracking Activated")
print("Backtracking Option is set to: ", backtrack)
outputtitles += ['tilt']
outputtitles += ['sazm']
outputtitles += ['height']
outputtitles += ['D']
if agriPV:
print("Saving Ground Irradiance Values for AgriPV Analysis. ")
outputtitles += ['Ground Irradiance Values']
sw.writerow(outputtitles)
myTimestamp = WeatherDF.index
hour = myTimestamp.hour
minute = myTimestamp.minute
dni = WeatherDF.dni
dhi = WeatherDF.dhi
if 'Tdry' in WeatherDF:
Tamb = WeatherDF.Tdry
else:
Tamb = 0
if 'Wspd' in WeatherDF:
VWind = WeatherDF.Wspd
else:
VWind = 0
if useTMYalbedo:
albedo = WeatherDF.Alb
zen = WeatherDF['zenith']
azm = WeatherDF['azimuth']
elv = WeatherDF['elevation']
daylighthours = WeatherDF[WeatherDF['zenith'] < 0.5 * math.pi]
C = WeatherDF['C']
D = WeatherDF['D']
df = WeatherDF[['Date (MM/DD/YYYY)', 'Time (HH:MM)', 'ghi', 'dni',
'dhi', 'Tdry', 'RHum', 'Pressure', 'Wdir', 'Wspd',
'zenith', 'azimuth', 'elevation', 'dni', 'dhi', 'C',
'D', 'sazm', 'tilt']]
df['albedo'] = 0.3
df['pitch'] = 1.5
# a. CALCULATE THE IRRADIANCE DISTRIBUTION ON THE GROUND
# ********************************************************
# double[] rearGroundGHI = new double[100],
# frontGroundGHI = new double[100]
# For global horizontal irradiance for each of 100 ground
# segments, to the rear and front of front of row edge
# Determine where on the ground the direct beam is shaded for
# a sun elevation and azimuth
# int[] rearGroundSH = new int[100],
# frontGroundSH = new int[100]
# Front and rear row-to-row spacing divided into 100 segments,
# (later becomes 1 if direct beam is shaded, 0 if not shaded)
# double pvFrontSH = 0.0, pvBackSH = 0.0, maxShadow
# Initialize fraction of PV module front and back surfaces
# that are shaded to zero (not shaded), and maximum shadow
# projected from front of row.
# Sky configuration factors are based on geometry and row type
# Maybe speed up by not calculating full array but rather filling if
# tracking == False?
rearSkyConfigFactors, frontSkyConfigFactors = (
getSkyConfigurationFactors2(rowType, df, pitch))
for rl in tqdm(range(noRows)):