Skip to content

Commit

Permalink
Made dieoff middleware as similar as possible without if statements
Browse files Browse the repository at this point in the history
  • Loading branch information
mollybsmith-noaa committed Oct 18, 2024
1 parent ed0b1f0 commit c0a9287
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class MatsMiddleDailyModelCycle {
this.indVar_Array.push(indVar);
}
}
this.indVar_Array.sort((a, b) => a - b);
this.indVar_Array.sort((a, b) => Number(a) - Number(b));

await this.createObsData();
await this.createModelData();
Expand Down Expand Up @@ -198,11 +198,13 @@ class MatsMiddleDailyModelCycle {
const dataSingleEpoch = {};
const stationsSingleEpoch = {};
for (let i = 0; i < this.stationNames.length; i += 1) {
const varValStation =
fveDataSingleEpoch[this.stationNames[i]] === "NULL"
? null
: fveDataSingleEpoch[this.stationNames[i]];
stationsSingleEpoch[this.stationNames[i]] = varValStation;
if (fveDataSingleEpoch[this.stationNames[i]]) {
const varValStation =
fveDataSingleEpoch[this.stationNames[i]] === "NULL"
? null
: fveDataSingleEpoch[this.stationNames[i]];
stationsSingleEpoch[this.stationNames[i]] = varValStation;
}
}
dataSingleEpoch.stations = stationsSingleEpoch;
this.fveObs[indVarKey][fveDataSingleEpoch.fve] = dataSingleEpoch;
Expand Down Expand Up @@ -279,11 +281,13 @@ class MatsMiddleDailyModelCycle {
const dataSingleEpoch = {};
const stationsSingleEpoch = {};
for (let i = 0; i < this.stationNames.length; i += 1) {
const varValStation =
fveDataSingleEpoch[this.stationNames[i]] === "NULL"
? null
: fveDataSingleEpoch[this.stationNames[i]];
stationsSingleEpoch[this.stationNames[i]] = varValStation;
if (fveDataSingleEpoch[this.stationNames[i]]) {
const varValStation =
fveDataSingleEpoch[this.stationNames[i]] === "NULL"
? null
: fveDataSingleEpoch[this.stationNames[i]];
stationsSingleEpoch[this.stationNames[i]] = varValStation;
}
}
dataSingleEpoch.stations = stationsSingleEpoch;
this.fveModels[indVarKey][fveDataSingleEpoch.fve] = dataSingleEpoch;
Expand All @@ -299,18 +303,20 @@ class MatsMiddleDailyModelCycle {
};

generateCtc = () => {
const { threshold } = this;
const threshold = Number(this.threshold);
const indVarsWithData = _.intersection(
Object.keys(this.fveObs),
Object.keys(this.fveModels)
);
indVarsWithData.sort(function (a, b) {
return Number(a) - Number(b);
});

for (let idx = 0; idx < indVarsWithData.length; idx += 1) {
const ctcStats = {};

const indVar = indVarsWithData[idx];
const indVarKey = indVar.toString();
ctcStats.avtime = indVar;
ctcStats.avtime = Number(indVar);
ctcStats.hit = 0;
ctcStats.miss = 0;
ctcStats.fa = 0;
Expand All @@ -319,7 +325,7 @@ class MatsMiddleDailyModelCycle {
ctcStats.sub_data = [];

// get all the fve for this indVar
const indVarSingle = this.fveModels[indVarKey];
const indVarSingle = this.fveModels[indVar];
const fveArray = Object.keys(indVarSingle);
fveArray.sort();

Expand All @@ -328,7 +334,7 @@ class MatsMiddleDailyModelCycle {
ctcStats.nTimes = fveArray.length;
for (let imfve = 0; imfve < fveArray.length; imfve += 1) {
const fve = fveArray[imfve];
const obsSingleFve = this.fveObs[indVarKey][fve];
const obsSingleFve = this.fveObs[indVar][fve];
const modelSingleFve = indVarSingle[fve];

if (obsSingleFve && modelSingleFve) {
Expand Down Expand Up @@ -357,13 +363,15 @@ class MatsMiddleDailyModelCycle {
Object.keys(this.fveObs),
Object.keys(this.fveModels)
);
indVarsWithData.sort(function (a, b) {
return Number(a) - Number(b);
});

for (let idx = 0; idx < indVarsWithData.length; idx += 1) {
const sumsStats = {};

const indVar = indVarsWithData[idx];
const indVarKey = indVar.toString();
sumsStats.avtime = indVar;
sumsStats.avtime = Number(indVar);
sumsStats.square_diff_sum = 0;
sumsStats.N_sum = 0;
sumsStats.obs_model_diff_sum = 0;
Expand All @@ -374,7 +382,7 @@ class MatsMiddleDailyModelCycle {
sumsStats.sub_data = [];

// get all the fve for this indVar
const indVarSingle = this.fveModels[indVarKey];
const indVarSingle = this.fveModels[indVar];
const fveArray = Object.keys(indVarSingle);
fveArray.sort();

Expand All @@ -383,7 +391,7 @@ class MatsMiddleDailyModelCycle {
sumsStats.nTimes = fveArray.length;
for (let imfve = 0; imfve < fveArray.length; imfve += 1) {
const fve = fveArray[imfve];
const obsSingleFve = this.fveObs[indVarKey][fve];
const obsSingleFve = this.fveObs[indVar][fve];
const modelSingleFve = indVarSingle[fve];

if (obsSingleFve && modelSingleFve) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,10 @@ class MatsMiddleDieoff {
return Number(utc);
});
}
if (singleCycle) {
this.singleCycle = singleCycle;
}

this.singleCycle = singleCycle;
this.filterInfo = filterInfo;

this.conn = await cbPool.getConnection();
Expand All @@ -159,6 +161,7 @@ class MatsMiddleDieoff {
this.fcstValidEpoch_Array[this.fcstValidEpoch_Array.length - 1]
);
this.indVar_Array = this.indVar_Array.filter((fl) => Number(fl) % 3 === 0);
this.indVar_Array.sort((a, b) => Number(a) - Number(b));

await this.createObsData();
await this.createModelData();
Expand Down Expand Up @@ -226,18 +229,20 @@ class MatsMiddleDieoff {
prSlice.then((qr) => {
for (let jmfve = 0; jmfve < qr.rows.length; jmfve += 1) {
const fveDataSingleEpoch = qr.rows[jmfve];
const indVarKey = fveDataSingleEpoch.fcst_lead.toString();
const indVarKey = "0"; // obs don't have a lead time
if (!this.fveObs[indVarKey]) {
this.fveObs[indVarKey] = {};
}
const dataSingleEpoch = {};
const stationsSingleEpoch = {};
for (let i = 0; i < this.stationNames.length; i += 1) {
const varValStation =
fveDataSingleEpoch[this.stationNames[i]] === "NULL"
? null
: fveDataSingleEpoch[this.stationNames[i]];
stationsSingleEpoch[this.stationNames[i]] = varValStation;
if (fveDataSingleEpoch[this.stationNames[i]]) {
const varValStation =
fveDataSingleEpoch[this.stationNames[i]] === "NULL"
? null
: fveDataSingleEpoch[this.stationNames[i]];
stationsSingleEpoch[this.stationNames[i]] = varValStation;
}
}
dataSingleEpoch.stations = stationsSingleEpoch;
this.fveObs[indVarKey][fveDataSingleEpoch.fve] = dataSingleEpoch;
Expand Down Expand Up @@ -266,6 +271,7 @@ class MatsMiddleDieoff {
tmplGetNStationsMfveModel,
"{{vxFCST_LEN}}"
);

let stationNamesModels = "";
for (let i = 0; i < this.stationNames.length; i += 1) {
if (i === 0) {
Expand Down Expand Up @@ -317,11 +323,13 @@ class MatsMiddleDieoff {
const dataSingleEpoch = {};
const stationsSingleEpoch = {};
for (let i = 0; i < this.stationNames.length; i += 1) {
const varValStation =
fveDataSingleEpoch[this.stationNames[i]] === "NULL"
? null
: fveDataSingleEpoch[this.stationNames[i]];
stationsSingleEpoch[this.stationNames[i]] = varValStation;
if (fveDataSingleEpoch[this.stationNames[i]]) {
const varValStation =
fveDataSingleEpoch[this.stationNames[i]] === "NULL"
? null
: fveDataSingleEpoch[this.stationNames[i]];
stationsSingleEpoch[this.stationNames[i]] = varValStation;
}
}
dataSingleEpoch.stations = stationsSingleEpoch;
this.fveModels[indVarKey][fveDataSingleEpoch.fve] = dataSingleEpoch;
Expand All @@ -338,18 +346,17 @@ class MatsMiddleDieoff {
};

generateCtc = () => {
const { threshold } = this;
const indVarsWithData = _.intersection(
Object.keys(this.fveObs),
Object.keys(this.fveModels)
);
const threshold = Number(this.threshold);
const indVarsWithData = Object.keys(this.fveModels);
indVarsWithData.sort(function (a, b) {
return Number(a) - Number(b);
});

for (let idx = 0; idx < indVarsWithData.length; idx += 1) {
const ctcStats = {};

const indVar = indVarsWithData[idx];
const indVarKey = indVar.toString();
ctcStats.fcst_lead = indVar;
ctcStats.fcst_lead = Number(indVar);
ctcStats.hit = 0;
ctcStats.miss = 0;
ctcStats.fa = 0;
Expand All @@ -358,7 +365,7 @@ class MatsMiddleDieoff {
ctcStats.sub_data = [];

// get all the fve for this indVar
const indVarSingle = this.fveModels[indVarKey];
const indVarSingle = this.fveModels[indVar];
const fveArray = Object.keys(indVarSingle);
fveArray.sort();

Expand All @@ -367,7 +374,7 @@ class MatsMiddleDieoff {
ctcStats.nTimes = fveArray.length;
for (let imfve = 0; imfve < fveArray.length; imfve += 1) {
const fve = fveArray[imfve];
const obsSingleFve = this.fveObs[indVarKey][fve];
const obsSingleFve = this.fveObs["0"][fve];
const modelSingleFve = indVarSingle[fve];

if (
Expand Down Expand Up @@ -409,17 +416,16 @@ class MatsMiddleDieoff {
};

generateSums = () => {
const indVarsWithData = _.intersection(
Object.keys(this.fveObs),
Object.keys(this.fveModels)
);
const indVarsWithData = Object.keys(this.fveModels);
indVarsWithData.sort(function (a, b) {
return Number(a) - Number(b);
});

for (let idx = 0; idx < indVarsWithData.length; idx += 1) {
const sumsStats = {};

const indVar = indVarsWithData[idx];
const indVarKey = indVar.toString();
sumsStats.fcst_lead = indVar;
sumsStats.fcst_lead = Number(indVar);
sumsStats.square_diff_sum = 0;
sumsStats.N_sum = 0;
sumsStats.obs_model_diff_sum = 0;
Expand All @@ -430,7 +436,7 @@ class MatsMiddleDieoff {
sumsStats.sub_data = [];

// get all the fve for this indVar
const indVarSingle = this.fveModels[indVarKey];
const indVarSingle = this.fveModels[indVar];
const fveArray = Object.keys(indVarSingle);
fveArray.sort();

Expand All @@ -439,7 +445,7 @@ class MatsMiddleDieoff {
sumsStats.nTimes = fveArray.length;
for (let imfve = 0; imfve < fveArray.length; imfve += 1) {
const fve = fveArray[imfve];
const obsSingleFve = this.fveObs[indVarKey][fve];
const obsSingleFve = this.fveObs["0"][fve];
const modelSingleFve = indVarSingle[fve];

if (
Expand Down
Loading

0 comments on commit c0a9287

Please sign in to comment.