def fromLocationAndHoys(cls, location, hoys, pointGroups, vectorGroups=[], timestep=1, hbObjects=None, subFolder='sunlighthour'): """Create sunlighthours recipe from Location and hours of year.""" sp = Sunpath.fromLocation(location) suns = (sp.calculateSunFromHOY(HOY) for HOY in hoys) sunVectors = tuple(s.sunVector for s in suns if s.isDuringDay) analysisGrids = cls.analysisGridsFromPointsAndVectors(pointGroups, vectorGroups) return cls(sunVectors, hoys, analysisGrids, timestep, hbObjects, subFolder)
def fromLocationAndAnalysisPeriod( cls, location, analysisPeriod, pointGroups, vectorGroups=None, hbObjects=None, subFolder='sunlighthour' ): """Create sunlighthours recipe from Location and analysis period.""" vectorGroups = vectorGroups or () sp = Sunpath.fromLocation(location) suns = (sp.calculateSunFromHOY(HOY) for HOY in analysisPeriod.floatHOYs) sunVectors = tuple(s.sunVector for s in suns if s.isDuringDay) hoys = tuple(s.hoy for s in suns if s.isDuringDay) analysisGrids = cls.analysisGridsFromPointsAndVectors(pointGroups, vectorGroups) return cls(sunVectors, hoys, analysisGrids, analysisPeriod.timestep, hbObjects, subFolder)
def fromJson(cls, recJson): """Create the solar access recipe from json. { "id": 0, // do NOT overwrite this id "location": null, // a honeybee location - see below "hoys": [], // list of hours of the year "surfaces": [], // list of honeybee surfaces "analysis_grids": [] // list of analysis grids "sun_vectors": [] // list of sun vectors if location is not provided } """ hoys = recJson["hoys"] if 'sun_vectors' not in recJson or not recJson['sun_vectors']: loc = Location.fromJson(recJson['location']) sp = Sunpath.fromLocation(loc) suns = (sp.calculateSunFromHOY(HOY) for HOY in hoys) sunVectors = tuple(s.sunVector for s in suns if s.isDuringDay) else: sunVectors = recJson['sun_vectors'] analysisGrids = \ tuple(AnalysisGrid.fromJson(ag) for ag in recJson["analysis_grids"]) hbObjects = tuple(HBSurface.fromJson(srf) for srf in recJson["surfaces"]) return cls(sunVectors, hoys, analysisGrids, 1, hbObjects)
# pass # # def tearDown(self): # """Nothing to tear down as nothing gets written to file.""" # pass # # def test_default_values(self): # """Test if the command correctly creates a wea file name as output.""" # self.assertEqual(self.epw2Wea.outputWeaFile, self.testWea) # # # if __name__ == "__main__": # unittest.main() # test sunpath # test defualt inputs sunpath = Sunpath() sun = sunpath.calculateSun(month=12, day=21, hour=23) print round(sun.sunVector.x, 2) == 0.23 print round(sun.sunVector.y, 2) == 0.40 print round(sun.sunVector.z, 2) == 0.89 # test sunpath from location l = Location() sunpath = Sunpath.fromLocation(l) sun = sunpath.calculateSun(month=12, day=21, hour=23) print round(sun.sunVector.x, 2) == 0.23 print round(sun.sunVector.y, 2) == 0.40 print round(sun.sunVector.z, 2) == 0.89
def execute(self, workingDir, reuse=True): """Generate sun matrix. Args: workingDir: Folder to execute and write the output. reuse: Reuse the matrix if already existed in the folder. Returns: Full path to analemma, sunlist and sunmatrix. """ fp = os.path.join(workingDir, self.analemmafile) lfp = os.path.join(workingDir, self.sunlistfile) mfp = os.path.join(workingDir, self.sunmtxfile) hrf = os.path.join(workingDir, self.name + '.hrs') outputType = self.skyType if reuse: if self.hoursMatch(hrf): for f in (fp, lfp, mfp): if not os.path.isfile(f): break else: return fp, lfp, mfp with open(hrf, 'wb') as outf: outf.write(','.join(str(h) for h in self.hoys) + '\n') wea = self.wea monthDateTime = (DateTime.fromHoy(idx) for idx in self.hoys) latitude, longitude = wea.location.latitude, -wea.location.longitude sp = Sunpath.fromLocation(wea.location, self.north) solarradiances = [] sunValues = [] sunUpHours = [] # collect hours that sun is up solarstring = \ 'void light solar{0} 0 0 3 {1} {1} {1} ' \ 'solar{0} source sun 0 0 4 {2:.6f} {3:.6f} {4:.6f} 0.533' # use gendaylit to calculate radiation values for each hour. print('Calculating sun positions and radiation values.') count = 0 for timecount, timeStamp in enumerate(monthDateTime): month, day, hour = timeStamp.month, timeStamp.day, timeStamp.hour + 0.5 dnr, dhr = int(wea.directNormalRadiation[timeStamp.intHOY]), \ int(wea.diffuseHorizontalRadiation[timeStamp.intHOY]) if dnr == 0: continue count += 1 sun = sp.calculateSun(month, day, hour) if sun.altitude < 0: continue x, y, z = sun.sunVector solarradiance = \ int(gendaylit(sun.altitude, month, day, hour, dnr, dhr, outputType)) curSunDefinition = solarstring.format(count, solarradiance, -x, -y, -z) solarradiances.append(solarradiance) sunValues.append(curSunDefinition) # keep the number of hour relative to hoys in this sun matrix sunUpHours.append(timecount) sunCount = len(sunUpHours) assert sunCount > 0, ValueError('There is 0 sun up hours!') print('# Number of sun up hours: %d' % sunCount) print('Writing sun positions and radiation values to {}'.format(fp)) # create solar discs. with open(fp, 'w') as annfile: annfile.write("\n".join(sunValues)) annfile.write('\n') print('Writing list of suns to {}'.format(lfp)) # create list of suns. with open(lfp, 'w') as sunlist: sunlist.write("\n".join( ("solar%s" % (idx + 1) for idx in xrange(sunCount)))) sunlist.write('\n') # Start creating header for the sun matrix. fileHeader = ['#?RADIANCE'] fileHeader += ['Sun matrix created by Honeybee'] fileHeader += ['LATLONG= %s %s' % (latitude, -longitude)] fileHeader += ['NROWS=%s' % sunCount] fileHeader += ['NCOLS=%s' % len(self.hoys)] fileHeader += ['NCOMP=3'] fileHeader += ['FORMAT=ascii'] print('Writing sun matrix to {}'.format(mfp)) # Write the matrix to file. with open(mfp, 'w') as sunMtx: sunMtx.write('\n'.join(fileHeader) + '\n' + '\n') for idx, sunValue in enumerate(solarradiances): sunRadList = ['0 0 0'] * len(self.hoys) sunRadList[sunUpHours[idx]] = '{0} {0} {0}'.format(sunValue) sunMtx.write('\n'.join(sunRadList) + '\n\n') sunMtx.write('\n') return fp, lfp, mfp