weather-prism
Version:
A node module for retrieving PRISM daily weather data for an arbitrary location (lat-lon)
158 lines • 4.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const axios = require('axios');
const md5 = require('md5');
const moment_1 = tslib_1.__importDefault(require("moment"));
const _ = require('lodash');
let Promise = require('bluebird');
const debug = require('debug');
const info = debug(`weather-prism:info`);
async function fetch({ years, lat, lon }) {
if (!lat || !lon) {
throw new Error(`Latitude ("lat") ${lat} or Longitude ("lon"): ${lon} value missing.`);
}
if (!years) {
let end = (0, moment_1.default)().year() + 1;
let start = end - 21;
years = _.range(start, end);
info(`Missing parameter "years". Defaulting to 20 years of data, i.e., years:${years}.`);
}
let data = {};
let template = {
pcpn: {
units: "in"
},
maxt: {
units: "degreeF"
},
mint: {
units: "degreeF"
},
avgt: {
units: "degreeF"
},
ggd: {
units: "GDD Heat Units",
base: {
value: 50,
units: "degreeF"
}
}
};
let tid = md5(JSON.stringify(template));
template.id = tid;
await Promise.each(years.sort(), async (year) => {
info(`Processing year ${year}`);
let response = await axios({
method: "post",
url: "https://data.rcc-acis.org/GridData",
headers: {
"Content-Type": "application/json"
},
data: {
loc: `${lon}, ${lat}`,
"sdate": `${year}-01-01`,
"edate": `${year}-12-31`,
"grid": "21",
"elems": [{
"name": "pcpn",
"interval": "dly",
"duration": 1,
"smry": "sum",
"units": "inch"
}, {
"name": "maxt",
"interval": "dly",
"duration": 1,
"smry": "sum",
"units": "degreeF"
}, {
"name": "mint",
"interval": "dly",
"duration": 1,
"smry": "sum",
"units": "degreeF"
}, {
"name": "avgt",
"interval": "dly",
"duration": 1,
"smry": "sum",
"units": "degreeF"
}, {
"name": "gdd",
"interval": "dly",
"duration": 1,
"smry": "sum",
"units": "degreeF"
}]
}
}).then(r => r.data.data);
if (response) {
response.forEach(item => {
let day = item[0];
data[day] = {
pcpn: item[1],
maxt: item[2],
mint: item[3],
avgt: item[4],
gdd: item[5],
time: (0, moment_1.default)(day).format(),
template: tid
};
});
}
});
return { data, template };
}
async function get(lon, lat, sdate, edate) {
await axios({
method: "post",
url: "https://data.rcc-acis.org/GridData",
headers: {
"Content-Type": "application/json"
},
data: {
loc: `${lon}, ${lat}`,
sdate,
edate,
"grid": "21",
"elems": [{
"name": "pcpn",
"interval": "dly",
"duration": 1,
"smry": "sum",
"units": "inch"
}, {
"name": "maxt",
"interval": "dly",
"duration": 1,
"smry": "sum",
"units": "degreeF"
}, {
"name": "mint",
"interval": "dly",
"duration": 1,
"smry": "sum",
"units": "degreeF"
}, {
"name": "avgt",
"interval": "dly",
"duration": 1,
"smry": "sum",
"units": "degreeF"
}, {
"name": "gdd",
"interval": "dly",
"duration": 1,
"smry": "sum",
"units": "degreeF"
}]
}
}).then(r => r.data.data);
}
module.exports = {
fetch,
get
};
//# sourceMappingURL=index.js.map