Skip to content

Commit a6795da

Browse files
authored
Merge pull request #179 from NREL/SAM_289
Address SAM issue 289 - ignore leap years in timeseries axis
2 parents dadaba4 + 9f31b48 commit a6795da

File tree

1 file changed

+40
-16
lines changed

1 file changed

+40
-16
lines changed

src/plot/plaxis.cpp

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -669,14 +669,25 @@ void wxPLTimeAxis::RecalculateTicksAndLabel() {
669669
//We need to figure out whether we are looking at hours, days, or months, and label the graph appropriately.
670670
wxDateTime timeKeeper(1, wxDateTime::Jan, 1971, 0, 0, 0); // works all time zones
671671

672+
672673
double world_len = m_max - m_min;
673674
double time = m_min;
675+
676+
// to skip leap years (SAM issue 289) - scale to first year
677+
int m_min_offset = m_min / 8760;
678+
double m_min_scaled = (double)((int)m_min % 8760); // m_min and m_max always based on 8760 (365 days with no leap years)
679+
double m_max_scaled = (double)((int)m_max % 8760); // m_min and m_max always based on 8760 (365 days with no leap years)
680+
if (m_max_scaled <= m_min_scaled) m_max_scaled += 8760; // handle year end points
681+
682+
time = m_min_scaled;
683+
674684
timeKeeper.Add(wxTimeSpan::Minutes(60 * time));
675685

676686
// Handle DST.
677687
if (timeKeeper.IsDST())
678688
timeKeeper.Subtract(wxTimeSpan::Hour());
679689

690+
680691
if (world_len <= 72) {
681692
if (floor(time) != time) {
682693
timeKeeper.Add(wxTimeSpan::Minutes(60 * (floor(time) + 2 - time)));
@@ -700,11 +711,12 @@ void wxPLTimeAxis::RecalculateTicksAndLabel() {
700711
timeKeeper.Add(wxTimeSpan::Hour());
701712
timeKeeper2.Add(wxTimeSpan::Hour());
702713
}
703-
704714
if (timeKeeper.GetDay() == timeKeeper2.GetDay())
705715
m_timeLabel = timeKeeper.Format("%b %d");
706-
else
716+
else if (timeKeeper.GetMonth() == timeKeeper2.GetMonth())
707717
m_timeLabel = timeKeeper.Format("%b %d") + "-" + timeKeeper2.Format("%d");
718+
else
719+
m_timeLabel = timeKeeper.Format("%b %d") + "-" + timeKeeper2.Format("%b %d");
708720

709721
do {
710722
m_tickList.push_back(TickData(time, timeKeeper.Format("%H"), TickData::LARGE));
@@ -741,13 +753,18 @@ void wxPLTimeAxis::RecalculateTicksAndLabel() {
741753
}
742754

743755
do {
744-
m_tickList.push_back(TickData(time, timeKeeper.Format("%b %d"), TickData::NONE));
756+
auto str = timeKeeper.Format("%b %d");
757+
// m_tickList.push_back(TickData(time, str, TickData::NONE));
758+
m_tickList.push_back(TickData(time + m_min_offset*8760, str, TickData::NONE));
745759
time += 12;
746-
if (time < m_max)
747-
m_tickList.push_back(TickData(time, wxEmptyString, TickData::LARGE)); // midnight
760+
// if (time < m_max)
761+
if (time < m_max_scaled)
762+
m_tickList.push_back(TickData(time + m_min_offset * 8760, wxEmptyString, TickData::LARGE)); // midnight
763+
// m_tickList.push_back(TickData(time, wxEmptyString, TickData::LARGE)); // midnight
748764
time += 12;
749765
timeKeeper.Add(wxTimeSpan::Hours(24));
750-
} while (time < m_max);
766+
} while (time < m_max_scaled);
767+
// } while (time < m_max);
751768
} else {
752769
//Assume day endpoints.
753770
//Only Label Months
@@ -763,13 +780,16 @@ void wxPLTimeAxis::RecalculateTicksAndLabel() {
763780
timeKeeper2.Add(wxTimeSpan::Day());
764781
}
765782

766-
m_tickList.push_back(TickData(time + daysVisibleInMonth * 24.0f, wxEmptyString, TickData::LARGE));
767-
int endMonthHours = time + daysVisibleInMonth * 24.0f; //00:00 on first of next month.
783+
// m_tickList.push_back(TickData(time + daysVisibleInMonth * 24.0f, wxEmptyString, TickData::LARGE));
784+
m_tickList.push_back(TickData(time + daysVisibleInMonth * 24.0f + m_min_offset * 8760, wxEmptyString, TickData::LARGE));
785+
int endMonthHours = time + m_min_offset * 8760 + daysVisibleInMonth * 24.0f; //00:00 on first of next month.
768786

769787
if (daysVisibleInMonth >= 7) {
770788
//Label the month if it has more than seven days visible.
789+
// m_tickList.push_back(
790+
// TickData(time + (daysVisibleInMonth * 24.0f / 2.0f), timeKeeper.Format("%b"), TickData::NONE));
771791
m_tickList.push_back(
772-
TickData(time + (daysVisibleInMonth * 24.0f / 2.0f), timeKeeper.Format("%b"), TickData::NONE));
792+
TickData(time + (daysVisibleInMonth * 24.0f / 2.0f) + m_min_offset * 8760, timeKeeper.Format("%b"), TickData::NONE));
773793
}
774794

775795
timeKeeper2.SetDay(wxDateTime::GetNumberOfDays(timeKeeper2.GetMonth()) / 2); //Middle of the second month.
@@ -779,24 +799,27 @@ void wxPLTimeAxis::RecalculateTicksAndLabel() {
779799
//Loop, labeling months
780800
//While end of month is visible.
781801
do {
782-
m_tickList.push_back(TickData(time, timeKeeper2.Format("%b"), TickData::NONE));
802+
// m_tickList.push_back(TickData(time, timeKeeper2.Format("%b"), TickData::NONE));
803+
m_tickList.push_back(TickData(time + m_min_offset * 8760, timeKeeper2.Format("%b"), TickData::NONE));
783804
timeKeeper.Set(timeKeeper2.GetTicks()); //timeKeeper is position of last label.
784805
timeKeeper2.Add(wxDateSpan::Month());
785806
timeKeeper2.SetDay(1); //timeKeeper2 is day 1 of next month.
786807

787-
m_tickList.push_back(TickData(time + timeKeeper2.Subtract(timeKeeper).GetHours(), wxEmptyString,
788-
TickData::LARGE)); //Adds tick at this pos. (start-month)
808+
// m_tickList.push_back(TickData(time + timeKeeper2.Subtract(timeKeeper).GetHours(), wxEmptyString,
809+
// TickData::LARGE)); //Adds tick at this pos. (start-month)
810+
m_tickList.push_back(TickData(time + timeKeeper2.Subtract(timeKeeper).GetHours() + m_min_offset * 8760, wxEmptyString,
811+
TickData::LARGE)); //Adds tick at this pos. (start-month)
789812
timeKeeper2.SetDay(wxDateTime::GetNumberOfDays(timeKeeper2.GetMonth()) / 2); //timeKeeper2 mid month
790813
time += timeKeeper2.Subtract(timeKeeper).GetHours(); //hours midMonth.
791814
endMonthHours += 24 * wxDateTime::GetNumberOfDays(timeKeeper2.GetMonth());
815+
// } while (endMonthHours < m_max_scaled);
792816
} while (endMonthHours < m_max);
793817

794818
//timeKeeper holds the middle of last month we actually labelled.
795819
//We still need to label the last month if it has more than 7 days showing.
796820
timeKeeper.Add(wxDateSpan::Month());
797821
timeKeeper.SetDay(1); // First not-yet-labeled month
798-
time = endMonthHours -
799-
24 * wxDateTime::GetNumberOfDays(timeKeeper.GetMonth()); //00:00 on first of not-yet-labeled month.
822+
time = endMonthHours - 24 * wxDateTime::GetNumberOfDays(timeKeeper.GetMonth()); //00:00 on first of not-yet-labeled month.
800823

801824
//Take care of fractional days at the max.
802825
timeKeeper2 = wxDateTime(01, wxDateTime::Jan, 1970, 00, 00, 00);
@@ -808,15 +831,16 @@ void wxPLTimeAxis::RecalculateTicksAndLabel() {
808831
time += daysVisibleInMonth * 24.0f;
809832
timeKeeper2.Add(wxTimeSpan::Minutes(daysVisibleInMonth * 24.0f * 60.0f));
810833
while (time < m_max && timeKeeper2.GetMonth() == timeKeeper.GetMonth()) {
834+
// while (time < m_max_scaled && timeKeeper2.GetMonth() == timeKeeper.GetMonth()) {
811835
daysVisibleInMonth += 1;
812836
timeKeeper2.Add(wxTimeSpan::Day());
813837
time += 24;
814838
}
815839

816840
if (daysVisibleInMonth >= 7) {
817841
//Label the month if it has more than seven days visible.
818-
m_tickList.push_back(
819-
TickData(time - (daysVisibleInMonth * 24.0f / 2.0f), timeKeeper.Format("%b"), TickData::NONE));
842+
m_tickList.push_back(TickData(time - (daysVisibleInMonth * 24.0f / 2.0f), timeKeeper.Format("%b"), TickData::NONE));
843+
// m_tickList.push_back(TickData(time - (daysVisibleInMonth * 24.0f / 2.0f) + m_min_offset * 8760, timeKeeper.Format("%b third"), TickData::NONE));
820844
}
821845
}
822846

0 commit comments

Comments
 (0)