tideFormated = {
const startDates = [
new Date(new Date().setDate(new Date().getDate() - 7)),
new Date()
];
const startDatesFormat = startDates.map(day1 => {
return {
date: day1,
year: day1.toLocaleDateString("en-GB", {
year: "numeric"
}),
month: day1.toLocaleDateString("en-GB", {
month: "2-digit"
}),
day: day1.toLocaleDateString("en-GB", {
day: "2-digit"
})
};
});
const fetchData = startDatesFormat.map(x =>
tabletojson
.convertUrl(
"https://mb-hakai.herokuapp.com/http://tides.gc.ca/eng/station?type=0&date=" +
x.year +
"%2F" +
x.month +
"%2F" +
x.day +
"&sid=7955&tz=PST&pres=1"
)
.then(function(tablesAsJson) {
const weekly = tablesAsJson[7];
const tides = [];
weekly.forEach((w, i) => {
i === 0
? (w.date = x.date)
: (w.date = new Date(new Date().setDate(x.date.getDate() + i)));
tides.push(w);
});
return tides;
})
);
return Promise.all(fetchData).then(values => {
const result = values[0].concat(values[1]);
const hours = [
"11",
"12",
"13",
"14",
"15",
"16",
"17",
"18",
"19",
"20",
"21",
"22",
"23",
"00",
"01",
"02",
"03",
"04",
"05",
"06",
"07",
"08",
"09",
"10"
];
const outdata = [];
result.forEach(d => {
let temp = hours.forEach((h, i) => {
let obj = {};
let time = new Date(new Date(d.date).setHours(h, 0, 0, 0));
const sun = suncalc.getTimes(new Date(time), 51.5529, -128.0056);
if (d3.isoParse(sun.sunrise) < time && time < d3.isoParse(sun.sunset)) {
d.day = 0;
} else {
d.day = 10;
}
obj["day"] = d.day;
obj["date"] = time;
obj["sea_surface_elevation"] = Number(Object.values(d)[i]);
outdata.push(obj);
});
});
outdata.sort((a, b) => a.date - b.date);
return outdata;
});
}