Skip to content

Commit

Permalink
Merge pull request #537 from NREL/feb_29_hacky_fix
Browse files Browse the repository at this point in the history
Enable leap day to be included in PV simulations using weather data
  • Loading branch information
janinefreeman authored Feb 24, 2021
2 parents f2d3405 + 0fb63c1 commit ee9e787
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 8 deletions.
10 changes: 7 additions & 3 deletions shared/lib_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -902,15 +902,19 @@ size_t util::hour_of_year(size_t month, size_t day, size_t hour)
size_t h = 0;
bool ok = true;
std::vector<size_t> days_in_months = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
//first add the days from all the preceding completed months
if (month >= 1 && month <= 12)
{
for (size_t m = 0; m < (month - 1); m++)
h += days_in_months[m] * 24;
}
else ok = false;
if (day >= 1 && day <= days_in_months[month - 1])
h += (day - 1) * 24;
else ok = false;
//then add days in the current month up to the current day
if (day >= 1 && day <= days_in_months[month - 1])
h += (day - 1) * 24;
else if (month == 2 && day == 29) //special check for leap day present in data
h += (27 * 24); //for leap day, repeat Feb 28 in annual indexes, because hour_of_year is used to index 8760 non-leap-year arrays.
else ok = false;
if (hour >= 0 && hour <= 23)
h += hour;
else ok = false;
Expand Down
21 changes: 16 additions & 5 deletions ssc/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1222,28 +1222,39 @@ double shading_factor_calculator::dc_shade_factor()
//HOWEVER, the year value may vary (i.e., TMY) so only checking month, day, hour, minute timesteps, not actual year vector
bool weatherdata::check_continuous_single_year(bool leapyear)
{
//first, set up some timestep variables
int ts_per_hour = 0; //determine the number of timesteps in each hour
if (leapyear)
ts_per_hour = (int)(m_nRecords / 8784);
else
ts_per_hour = (int)(m_nRecords / 8760);
double ts_min = 60. / ts_per_hour; //determine the number of minutes of each timestep

//next, check if the data has leap day (feb 29). need to do this because some tools pass in 8760 data that contains feb 29 and not dec 31
bool has_leapday = false;
int leapDayNoon = 1429 * ts_per_hour; //look for the index of noon on leap day. noon on leap day is hour 1429 of the year
if (this->m_data[leapDayNoon]->month == 2 && this->m_data[leapDayNoon]->day == 29) //check noon on what would be feb 29 if it's in the data
has_leapday = true;

//last, go through each index in order and make sure that the timestamps all correspond to a single, serially complete year with even timesteps
int idx = 0; // index to keep track of where we are in the timestamp vectors
// now, check that the month, hour, day, and minute vectors are consistent with a single, continuous year with an
// even timestep that starts on jan 1 and ends dec 31
for (int m = 1; m <= 12; m++)
{
int daymax = util::days_in_month(m - 1);
if (m == 2 && leapyear) daymax = 29; //make sure to account for leap day in Feb
if (m == 2 && has_leapday) daymax = 29; //make sure to account for leap day in Feb if it's in the data
if (m == 12 && has_leapday && !leapyear) daymax = 30; //if the data is 8760 but has Feb 29, then it won't have Dec 31
for (int d = 1; d <= daymax; d++)
{
for (int h = 0; h < 24; h++)
{
double min = this->m_data[idx]->minute;
for (int tsph = 0; tsph < ts_per_hour; tsph++)
{
min += tsph * ts_min;
//if any of the month, day, hour, or minute don't line up with what we've calculated, then it doesn't fit our criteria for a continuous year
//first check that the index isn't out of bounds
if (idx > m_nRecords - 1)
return false;
//if any of the month, day, hour, or minute don't line up with what we've calculated, then it doesn't fit our criteria for a continuous year
min += tsph * ts_min;
if (this->m_data[idx]->month != m || this->m_data[idx]->day != d || this->m_data[idx]->hour != h
|| this->m_data[idx]->minute != min)
return false;
Expand Down
Loading

0 comments on commit ee9e787

Please sign in to comment.