Skip to content

Commit

Permalink
Merge pull request #28 from gemini-hlsw/plot-localtime
Browse files Browse the repository at this point in the history
Added function to plot time depending on timezone
  • Loading branch information
stroncod authored Feb 14, 2024
2 parents 87cfdd0 + 4d9ffeb commit f5d3964
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 62 deletions.
11 changes: 6 additions & 5 deletions src/components/Results/EntryBySite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ export default function EntryBySite({
entryBySite.timeEntries.map((en: TimeEntryType, idx: number) => {
timeLine.push(
<div
className={`${
JSON.stringify(en) === JSON.stringify(selectedEntry) ? "active" : ""
} event-bullet`}
className={`${JSON.stringify(en) === JSON.stringify(selectedEntry) ? "active" : ""
} event-bullet`}
key={`timeEntry${idx}`}
onClick={() => setSelectedEntry(en)}
>
Expand All @@ -36,8 +35,10 @@ export default function EntryBySite({
<h4 className="title">Timeline</h4>
<div className="timeline">{timeLine}</div>
<TimeEntry timeEntry={selectedEntry}
eveTwilight={entryBySite.eveTwilight}
mornTwilight={entryBySite.mornTwilight}/>
eveTwilight={entryBySite.eveTwilight}
mornTwilight={entryBySite.mornTwilight}
site={entryBySite.site}
/>
</div>
);
}
61 changes: 31 additions & 30 deletions src/components/Results/TimeEntry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import { Tag } from 'primereact/tag';
import { ProgressBar } from 'primereact/progressbar';


export default function TimeEntry({timeEntry, mornTwilight, eveTwilight}: {timeEntry: TimeEntryType,
mornTwilight: string,
eveTwilight: string}) {
export default function TimeEntry({ timeEntry, mornTwilight, eveTwilight, site }: {
timeEntry: TimeEntryType,
mornTwilight: string,
eveTwilight: string,
site: string
}) {
function parseToVisitForPlot(visits: Visit[]) {
console.log(visits[0].startTime)
console.log( moment.utc(visits[0].startTime).tz('Pacific/Honolulu').toDate())
return visits.map((visit: Visit) => ({
startDate: new Date(visit.startTime),
endDate: new Date(visit.endTime),
Expand All @@ -40,10 +41,10 @@ export default function TimeEntry({timeEntry, mornTwilight, eveTwilight}: {timeE
throw new Error('Denominator cannot be zero');
}
const percentage = (numerator / denominator) * 100;

return percentage;
}


const obsClassBodyTemplate = (visit: Visit) => {
return <Tag value={visit.obsClass} severity={getSeverity(visit)}></Tag>;
Expand All @@ -60,30 +61,30 @@ export default function TimeEntry({timeEntry, mornTwilight, eveTwilight}: {timeE
return `${visit.completion} (${fractionToPercentage(visit.completion).toFixed(0)}%)`
}

const getSeverity = (visit: Visit )=> {
const getSeverity = (visit: Visit) => {
switch (visit.obsClass) {
case 'SCIENCE':
return 'success';

case 'PROGCAL':
return 'warning';

case 'PARTNERCAL':
return 'danger';
case 'ACQ':
return 'info';
case 'ACQCAL':
return 'info';
case 'DAYCAL':
return 'info';

default:
return null;
case 'SCIENCE':
return 'success';

case 'PROGCAL':
return 'warning';

case 'PARTNERCAL':
return 'danger';

case 'ACQ':
return 'info';

case 'ACQCAL':
return 'info';

case 'DAYCAL':
return 'info';

default:
return null;
}
};
};

return (
<Accordion className="time-entry">
Expand All @@ -92,7 +93,7 @@ export default function TimeEntry({timeEntry, mornTwilight, eveTwilight}: {timeE
nightState={timeEntry.plan.nightStats}
nightTitle={timeEntry.plan.startTime.substring(0, timeEntry.plan.startTime.indexOf('T'))} />}
>
<AltAzPlot data={parseToVisitForPlot(timeEntry.plan.visits)} eveTwilight={eveTwilight} mornTwilight={mornTwilight} />
<AltAzPlot data={parseToVisitForPlot(timeEntry.plan.visits)} eveTwilight={eveTwilight} mornTwilight={mornTwilight} site={site} />
<DataTable value={timeEntry.plan.visits} tableStyle={{ minWidth: '50rem' }}>
<Column field="obsId" header="Observation ID"> </Column>
<Column header="Observation Class" body={obsClassBodyTemplate}></Column>
Expand Down
64 changes: 37 additions & 27 deletions src/components/SchedulerPlot/SchedulerPlot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,22 @@ interface Visit {

interface AltAzPlotProps {
data: Visit[];
eveTwilight: string,
mornTwilight: string
eveTwilight: string,
mornTwilight: string,
site: string
}

const AltAzPlot: React.FC<AltAzPlotProps> = ({ data, eveTwilight, mornTwilight, site }) => {
const getOffset = (timeZone = 'UTC', date = new Date()) => {
const utcDate = new Date(date.toLocaleString('en-US', { timeZone: 'UTC' }));
const tzDate = new Date(date.toLocaleString('en-US', { timeZone }));
return (tzDate.getTime() - utcDate.getTime()) / 6e4;
}
const INITIAL_TIMEZONE = site === "GN" ? getOffset("Pacific/Honolulu") : getOffset("America/Santiago")

const AltAzPlot: React.FC<AltAzPlotProps> = ({ data, eveTwilight, mornTwilight}) => {
function getTzTime(date: Date) {
return date.getTime() + (INITIAL_TIMEZONE) * 60 * 1000
}

// Get theme context to modify chart values
const { theme } = useContext(ThemeContext);
Expand Down Expand Up @@ -54,8 +64,8 @@ const AltAzPlot: React.FC<AltAzPlotProps> = ({ data, eveTwilight, mornTwilight})
return map;
};
const colorMap = createMap(instruments, colors);
const eveTwiDate = new Date (eveTwilight)
const mornTwiDate = new Date (mornTwilight)
const eveTwiDate = new Date(eveTwilight)
const mornTwiDate = new Date(mornTwilight)

const seriesData: Array<SeriesArearangeOptions> = data.map((d: any, index: number) => {
const yMinArray = d.yPoints.map((y: number) => 0);
Expand All @@ -64,7 +74,7 @@ const AltAzPlot: React.FC<AltAzPlotProps> = ({ data, eveTwilight, mornTwilight})
type: "arearange",
data: d.yPoints.map((y: any, i: number) => {
return {
x: d.startDate.getTime() + i * 60 * 1000,
x: getTzTime(d.startDate) + i * 60 * 1000,
low: yMinArray[i],
high: y,
};
Expand All @@ -77,10 +87,10 @@ const AltAzPlot: React.FC<AltAzPlotProps> = ({ data, eveTwilight, mornTwilight})
enabled: false,
},
events: {
legendItemClick: function() {
return false; // Prevents the default action, which is toggling visibility
legendItemClick: function () {
return false; // Prevents the default action, which is toggling visibility
}
},
},
showInLegend: true, // Hide this series in the legend
};
});
Expand Down Expand Up @@ -111,7 +121,7 @@ const AltAzPlot: React.FC<AltAzPlotProps> = ({ data, eveTwilight, mornTwilight})

// Render custom labels for each section
data.forEach((d, index) => {
const x = (d.startDate.getTime() + d.endDate.getTime()) / 2;
const x = (getTzTime(d.startDate) + getTzTime(d.endDate)) / 2;
const y = Math.max(...d.yPoints) / 2;

const xPos = chart.xAxis[0].toPixels(x, false);
Expand All @@ -133,7 +143,7 @@ const AltAzPlot: React.FC<AltAzPlotProps> = ({ data, eveTwilight, mornTwilight})

const options: Highcharts.Options = {
time: {
timezone: 'Pacific/Honolulu'
timezone: "Pacific/Honolulu",
},
chart: {
type: "arearange",
Expand All @@ -143,13 +153,13 @@ const AltAzPlot: React.FC<AltAzPlotProps> = ({ data, eveTwilight, mornTwilight})
// Use the shared tooltip to show information for the entire area
shared: true,
formatter: function () {
if (this.points) {
var points = this.points;
// Assuming the first point is representative for the area
var point = this.series.name;
return point;
}
return false; // No tooltip for individual points
if (this.points) {
var points = this.points;
// Assuming the first point is representative for the area
var point = this.series.name;
return point;
}
return false; // No tooltip for individual points
}
},
title: {
Expand All @@ -165,16 +175,16 @@ const AltAzPlot: React.FC<AltAzPlotProps> = ({ data, eveTwilight, mornTwilight})
color: textColor, // Change the color of y-axis tick labels
},
},
min: eveTwiDate.getTime(),
max: mornTwiDate.getTime(),
min: getTzTime(eveTwiDate),
max: getTzTime(mornTwiDate),
tickPositioner: function () {
var positions = []
var interval = 1 * 60 * 60 * 1000
var positions = []
var interval = 1 * 60 * 60 * 1000

for (var i = eveTwiDate.getTime(); i <= mornTwiDate.getTime(); i+=interval){
positions.push(i);
}
return positions;
for (var i = getTzTime(eveTwiDate); i <= getTzTime(mornTwiDate); i += interval) {
positions.push(i);
}
return positions;
}

},
Expand Down Expand Up @@ -203,7 +213,7 @@ const AltAzPlot: React.FC<AltAzPlotProps> = ({ data, eveTwilight, mornTwilight})

return (
<div className='scheduler-plot'>

<HighchartsReact highcharts={Highcharts} options={options} ref={chartRef} />
</div>
);
Expand Down

0 comments on commit f5d3964

Please sign in to comment.