-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakerecords.js
More file actions
129 lines (114 loc) · 3.88 KB
/
makerecords.js
File metadata and controls
129 lines (114 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Usage eg:
// node makerecords.js GB 10000 500 largetest/testdata.csv afixedseedifyouwantit
import fs from 'fs'
import path from 'path'
import wordListPath from 'word-list'
import seedrandom from 'seedrandom'
import { fileURLToPath } from 'url'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
let gridtype = false
let desiredRecordCount = 0
let speciesCount = 0
console.log('Make test records in CSV format')
const GBletters = [
'SV', 'SW', 'SX', 'SY', 'SZ',
'TV',
'SR', 'SS', 'ST', 'SU',
'TQ', 'TR',
'SM', 'SN', 'SO', 'SP',
'TL', 'TM',
'SH', 'SJ', 'SK',
'TF', 'TG',
'SC', 'SD', 'SE',
'TA',
'NW', 'NX', 'NY', 'NZ',
'OV',
'NR', 'NS', 'NT', 'NU',
'NL', 'NM', 'NN', 'NO', 'NP',
'NF', 'NG', 'NH', 'NI', 'NJ',
'NA', 'NB', 'NC', 'ND', 'NE',
'HW', 'HX', 'HY', 'HZ',
'HT', 'HU',
'HO', 'HP'
]
const IEletters = [
'B', 'C', 'D',
'F', 'G', 'H', 'J',
'L', 'M', 'N', 'O',
'Q', 'R', 'S', 'T',
'V', 'W', 'X'
]
const GBIEletters = [...GBletters, ...IEletters]
export async function run (argv) {
try {
if (argv.length < 6) {
console.error('usage: node makerecords.js GB|IE|GBIE <desired-record-count> <species-count> <output-csv-file> <random-seed>')
return 0
}
gridtype = argv[2]
if (String(gridtype) !== 'GB' && String(gridtype) !== 'IE' && String(gridtype) !== 'GBIE') {
console.error('duff gridtype', gridtype)
return 0
}
desiredRecordCount = parseInt(argv[3])
speciesCount = parseInt(argv[4])
console.log('makerecord', gridtype, desiredRecordCount, speciesCount, argv[5])
console.log('Species list obtained from', wordListPath)
const speciesArray = fs.readFileSync(wordListPath, 'utf8').split('\n')
if (speciesCount > speciesArray.length) {
console.error('Not enough species names in list', speciesArray.length)
return 0
}
// Make linear distribution curve
const distribution = []
for (let speciesno = 0; speciesno < speciesCount; speciesno++) {
for (let probability = 0; probability < speciesno; probability++) {
distribution.push(speciesno)
}
}
console.log('distribution spread', distribution.length)
console.log('Least likely', speciesArray[distribution[0]])
console.log('Most likely', speciesArray[distribution[distribution.length - 1]])
let randomseed = null
if (argv.length >= 6) randomseed = argv[6]
console.log('randomseed', randomseed)
const myrng = seedrandom(randomseed)
const outpath = path.resolve(__dirname, argv[5])
const letters = String(gridtype) === 'GBIE' ? GBIEletters : String(gridtype) === 'GB' ? GBletters : IEletters
const letterslen = letters.length
const generateRecords = new Promise((resolve, reject) => {
const stream = fs.createWriteStream(outpath)
stream.on('close', function (fd) {
resolve()
})
stream.on('open', function (fd) {
stream.write('Spatial Reference,Date,Taxon Name\r')
for (let recno = 0; recno < desiredRecordCount; recno++) {
const gr1 = letters[parseInt(myrng() * letterslen)]
const gr2 = String(parseInt(myrng() * 10000)).padStart(4, '0')
const speciesno = distribution[parseInt(myrng() * distribution.length)]
const speciesname = speciesArray[speciesno]
stream.write(gr1 + gr2 + ',01/04/2020,' + speciesname + '\r')
}
stream.end()
})
stream.on('error', function (err) {
console.error('Write error', err.message)
console.log(err.stack)
reject('FILE ERROR')
})
})
await generateRecords
console.error('DONE')
return 1
} catch (e) {
console.error('FAILED', e)
return 2
}
}
/// ////////////////////////////////////////////////////////////////////////////////////
// If called from command line, then run now.
// If testing, then don't.
if (process.env.JEST_WORKER_ID === undefined) {
run(process.argv)
}