technoshop.essais_mesure_fi.../measure_clean_compa.ts
2025-07-21 10:56:11 +02:00

151 lines
3.6 KiB
TypeScript

import { Data as PlotlyData, Plot } from './plot/mod.ts'
type Data = {
timestamp: string //ISO string
volume: number //mL
ipam: number //ipam
record: {
pression: number //bar
mass: number //g
timestamp: number //s
}[]
}
function parseDatas(file: string): Data {
const lines = file.split('\n')
lines.shift() //remove title
const [day, month, year, time] = lines.shift()!.split(' ')
const monthList = [
'_',
'janvier',
'fevrier',
'mars',
'avril',
'mai',
'juin',
'juillet',
'aout',
'septembre',
'decembre',
]
const timestamp = `${year}-${
monthList.indexOf(month).toString().padStart(2, '0')
}-${day.padStart(2, '0')}T${time}`
lines.shift() //remove empty line
const volume = lines.shift()!.split(';').slice(1).map(Number).at(0)!
const ipam = lines.shift()!.split(';').slice(1).map(Number).at(0)!
lines.shift() //remove empty line
lines.shift() //remove title
const record = lines
.map((line) => line.split(';').slice(1).map(Number))
.map(([pression, mass, timestamp]) => ({ pression, mass, timestamp }))
return {
timestamp,
volume,
ipam,
record,
}
}
function filterRecord(record: Data['record'], ceil = 0.4): Data['record'] {
const filteredRecord: Data['record'] = []
record.forEach((value, index) => {
if (index === 0) filteredRecord.push(value)
if (value.mass < 10) filteredRecord.push(value)
const previousMass = filteredRecord.at(-1)?.mass ?? value.mass
const delta = Math.abs(value.mass - previousMass) / value.mass
if (delta >= ceil) filteredRecord.push({ ...value, mass: previousMass })
else filteredRecord.push(value)
})
return filteredRecord
}
function cutRecord(record: Data['record']): Data['record'] {
let minMass = record[0].mass
let minMassIndex = 0
for (const value of record) {
minMassIndex++
if (value.mass < minMass) minMass = value.mass
if (value.mass > minMass) break
}
return record.slice(minMassIndex)
}
function getDatasPlot(datas: Data): Partial<PlotlyData>[] {
const record = cutRecord(filterRecord(datas.record))
// const minMassIndex = record.findIndex((value) => )
// const record2 = record.filter(({mass}) => mass)
const x = record.map((
{ timestamp },
) => (timestamp - datas.record[0].timestamp))
const y = record.map((data) => data.mass).map((_, i, a) =>
(a[i + 1] - a[i]) / (x[i + 1] - x[i])
)
const meta = `${datas.volume}mL ${datas.ipam}IPAM ${
datas.timestamp.split('T')[1]
}`
const { ipam } = datas
const mean = y.filter((v) => !Number.isNaN(v)).reduce(
(p, c) => p + c / y.length,
0,
) //p + c / y.length, 0)
console.log({ mean, ipam, v: mean / 200 })
return [
{
x,
y,
name: `mass (${meta})`,
line: {
color: `rgb(${50 * Math.log(ipam)}, 0, ${255 - 50 * Math.log(ipam)})`,
},
},
]
}
const xTestFit = Array.from({ length: 190 }).map((_, i) => i * 1e3 + 3e3)
const yTestFit = xTestFit
.map((x) => 200 * (1 - Math.exp(-21e-6 * x) + 2e-7 * x))
.map((_, i, a) => (a[i + 1] - a[i]) / (xTestFit[i + 1] - xTestFit[i]))
// 200 * (1 - Math.exp(-19e-6 * x) + 3e-7 * x)
const testFit = {
x: xTestFit,
y: yTestFit,
name: 'test fit',
}
const plot = new Plot()
const plots: Partial<PlotlyData>[] = []
plots.push(testFit)
for await (const file of Deno.readDir('./datas')) {
if (!file.isFile) continue
if (!file.name.match(/Vrr3_\d{2}-\d{2}_\d{2}-\d{2}\.csv/)) continue
const path = `./datas/${file.name}`
const csv = await Deno.readTextFile(path)
const datas = parseDatas(csv)
// if (datas.ipam !== 4) continue
plots.push(...getDatasPlot(datas))
}
plot.plot(plots, {
xaxis: { title: { text: 'timestamp' } },
yaxis: { title: { text: 'mass [g]' } },
yaxis2: { title: { text: 'pression [bar]' }, side: 'right' },
})