Skip to content

Commit

Permalink
chore: move rpi tool from ca-tools (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnkenny54 authored Dec 24, 2024
1 parent 6858faf commit 78b69d0
Show file tree
Hide file tree
Showing 9 changed files with 470 additions and 41 deletions.
27 changes: 1 addition & 26 deletions lib/csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,6 @@ import { parse } from "csv-parse";
import { stringify } from "csv-stringify/sync";

class CSV {
/**
* @param {string} dir
* @param {string} fileName
*/
static getMap(dir, fileName) {
/** @type {string[]} */
let headers = [];

/**
* @param {string[]} h
*/
function setHeaders(h) {
headers = h;
return h;
}

const csv = this.parseFile(dir, fileName, setHeaders);
/** @type {Object<string,string[]>} */
const map = {};
for (const row of csv) {
map[row[headers[0]]] = row[headers[1]];
}

return map;
}

/**
* @param {string} fileName
* @param {import("csv-parse").ColumnOption[]|boolean|function (string[]):string[]} columns
Expand Down Expand Up @@ -102,6 +76,7 @@ class CSV {
* @param {string} fileName
* @param {boolean|import("csv-parse").ColumnOption[]|function (string[]):string[]} [columns]
* @param {string} [delimiter]
* @returns {Object<string,string>[]}
*/
static readFile(fileName, columns = true, delimiter) {
const content = fs.readFileSync(fileName);
Expand Down
3 changes: 2 additions & 1 deletion lib/exceptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Config } from "./config.js";
import { Files } from "./files.js";

class Exceptions {
/** @type {Object<string,Object<string,Object<string,{}>>>} */
/** @type {Object<string,Object<string,Object<string,string>>>} */
#exceptions = {};

/**
Expand Down Expand Up @@ -39,6 +39,7 @@ class Exceptions {
* @param {string} cat
* @param {string} subcat
* @param {string} [defaultValue]
* @returns {string|undefined}
*/
getValue(name, cat, subcat, defaultValue) {
const taxonData = this.#exceptions[name];
Expand Down
12 changes: 8 additions & 4 deletions lib/program.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Command } from "commander";
import { Files } from "./files.js";
import { CSV } from "./csv.js";
import path from "node:path";

class Program {
/**
Expand All @@ -14,7 +15,10 @@ class Program {
console.log(includeFilePath + " not found; loading all taxa");
return true;
}
const includeCSV = CSV.parseFile(dataDir, includeFileName);

/** @type {TaxonData[]} */
// @ts-ignore
const includeCSV = CSV.readFile(path.join(dataDir, includeFileName));
/** @type {Object<string,TaxonData>} */
const include = {};
for (const row of includeCSV) {
Expand All @@ -29,16 +33,16 @@ class Program {
.option(
"-d, --datadir <dir>",
"The directory containing plant data.",
"./data"
"./data",
)
.option(
"-f, --show-flower-errors",
"Include missing flower color/flowering time in the error log when loading taxa."
"Include missing flower color/flowering time in the error log when loading taxa.",
)
.option(
"-o, --outputdir <dir>",
"The directory to which output should be written.",
"./output"
"./output",
);
return program;
}
Expand Down
3 changes: 3 additions & 0 deletions lib/taxa.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ class Taxa {
this.#loadInatPhotos(dataDir);
}

/** @type {SynonymData[]} */
// @ts-ignore
const synCSV = CSV.parseFile(dataDir, "synonyms.csv");
this.#loadSyns(synCSV, inclusionList);
this.#loadSyns(extraSynonyms, inclusionList);
Expand All @@ -107,6 +109,7 @@ class Taxa {
const photosFileName = "inattaxonphotos.csv";
if (fs.existsSync(path.join(dataDir, photosFileName))) {
/** @type {InatCsvPhoto[]} */
// @ts-ignore
const csvPhotos = CSV.parseFile(dataDir, photosFileName);
for (const csvPhoto of csvPhotos) {
const taxon = this.getTaxon(csvPhoto.name);
Expand Down
5 changes: 5 additions & 0 deletions lib/tools/calflora.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const CALFLORA_URL_COUNTY =

/**
* @typedef {{
* Taxon:string,
* "Native Status":string,
* TJMTID:string
* "Active in Calflora?":string
Expand Down Expand Up @@ -59,9 +60,13 @@ class Calflora {
calfloraDataFileNameCounties,
);

/** @type {CalfloraData[]} */
// @ts-ignore
const csvActive = CSV.readFile(
path.join(toolsDataPath, calfloraDataFileNameActive),
);
/** @type {CalfloraData[]} */
// @ts-ignore
const csvCounties = CSV.readFile(
path.join(toolsDataPath, calfloraDataFileNameCounties),
);
Expand Down
Loading

0 comments on commit 78b69d0

Please sign in to comment.