Spaces:
Running
Running
construire un outil de calcul des émissions de gaz à effets de serre. Pouvoir saisir toutes les interventions, les intrants pour toutes les cultures d'une exploitation agricoles. ensuite analyser ses données avec un calcul issu de la méthode grande culture du labal bas carbone V2 et des données du giec. pouvoir exporter ces données sous excel et faire à la fin une notice des données de refences du LBC et du giec
8722bff verified Download script.js from LOLIVEJ/agricarbon-tracker: direct link, hf CLI and curl.
- Browser
- Download file 6.7 kB
-
https://huggingface.co/spaces/LOLIVEJ/agricarbon-tracker/resolve/main/script.js
- Command line
-
hf download hf://spaces/LOLIVEJ/agricarbon-tracker/script.js
-
curl -L -o script.js https://huggingface.co/spaces/LOLIVEJ/agricarbon-tracker/resolve/main/script.js
6.7 kB
| class DataManager { | |
| constructor() { | |
| this.interventions = JSON.parse(localStorage.getItem('interventions')) || []; | |
| this.cultures = JSON.parse(localStorage.getItem('cultures')) || []; | |
| this.parametres = JSON.parse(localStorage.getItem('parametres')) || this.getDefaultParams(); | |
| } | |
| getDefaultParams() { | |
| return { | |
| lbc: { | |
| n2o_direct: 0.01, | |
| n2o_indirect: 0.01, | |
| co2_engrais: 0.6, | |
| co2_pesticides: 3.0 | |
| }, | |
| giec: { | |
| n2o_direct: 0.01, | |
| n2o_indirect: 0.0075, | |
| co2_engrais: 1.3, | |
| co2_pesticides: 5.0 | |
| } | |
| }; | |
| } | |
| addIntervention(intervention) { | |
| this.interventions.push({ | |
| ...intervention, | |
| id: Date.now().toString(), | |
| createdAt: new Date().toISOString() | |
| }); | |
| this.saveData(); | |
| } | |
| addCulture(culture) { | |
| this.cultures.push({ | |
| ...culture, | |
| id: Date.now().toString(), | |
| createdAt: new Date().toISOString() | |
| }); | |
| this.saveData(); | |
| } | |
| removeIntervention(id) { | |
| this.interventions = this.interventions.filter(i => i.id !== id); | |
| this.saveData(); | |
| } | |
| removeCulture(id) { | |
| this.cultures = this.cultures.filter(c => c.id !== id); | |
| this.saveData(); | |
| } | |
| calculateEmissions() { | |
| const results = { | |
| lbc: this.calculateWithMethod('lbc'), | |
| giec: this.calculateWithMethod('giec'), | |
| total: { | |
| lbc: 0, | |
| giec: 0 | |
| } | |
| }; | |
| results.total.lbc = Object.values(results.lbc).reduce((sum, val) => sum + val, 0); | |
| results.total.giec = Object.values(results.giec).reduce((sum, val) => sum + val, 0); | |
| return results; | |
| } | |
| calculateWithMethod(method) { | |
| const params = this.parametres[method]; | |
| let emissions = { | |
| n2o_direct: 0, | |
| n2o_indirect: 0, | |
| co2_engrais: 0, | |
| co2_pesticides: 0, | |
| co2_carburant: 0 | |
| }; | |
| this.interventions.forEach(intervention => { | |
| if (intervention.type === 'engrais') { | |
| emissions.n2o_direct += intervention.quantite * params.n2o_direct; | |
| emissions.n2o_indirect += intervention.quantite * params.n2o_indirect; | |
| emissions.co2_engrais += intervention.quantite * params.co2_engrais; | |
| } else if (intervention.type === 'pesticide') { | |
| emissions.co2_pesticides += intervention.quantite * params.co2_pesticides; | |
| } else if (intervention.type === 'travail_sol') { | |
| emissions.co2_carburant += intervention.quantite * 2.68; // kg CO2/L de gasoil | |
| } | |
| }); | |
| return emissions; | |
| } | |
| exportToExcel() { | |
| const data = { | |
| interventions: this.interventions, | |
| cultures: this.cultures, | |
| emissions: this.calculateEmissions(), | |
| calcul: new Date().toISOString() | |
| }; | |
| const blob = new Blob([JSON.stringify(data, null, 2)], { | |
| type: 'application/json' | |
| }); | |
| const url = URL.createObjectURL(blob); | |
| const a = document.createElement('a'); | |
| a.href = url; | |
| a.download = `emissions_agricoles_${new Date().toISOString().split('T')[0]}.json`; | |
| document.body.appendChild(a); | |
| a.click(); | |
| document.body.removeChild(a); | |
| URL.revokeObjectURL(url); | |
| } | |
| saveData() { | |
| localStorage.setItem('interventions', JSON.stringify(this.interventions)); | |
| localStorage.setItem('cultures', JSON.stringify(this.cultures)); | |
| localStorage.setItem('parametres', JSON.stringify(this.parametres)); | |
| } | |
| clearAllData() { | |
| this.interventions = []; | |
| this.cultures = []; | |
| this.saveData(); | |
| } | |
| } | |
| // Initialisation globale | |
| window.dataManager = new DataManager(); | |
| // Fonctions utilitaires | |
| function formatNumber(number, decimals = 2) { | |
| return new Intl.NumberFormat('fr-FR', { | |
| minimumFractionDigits: decimals, | |
| maximumFractionDigits: decimals | |
| }).format(number); | |
| } | |
| function showAlert(message, type = 'info') { | |
| const alert = document.createElement('div'); | |
| alert.className = `alert alert-${type} fixed top-4 right-4 z-50`; | |
| alert.innerHTML = ` | |
| <div class="flex items-center"> | |
| <i data-feather="${type === 'success' ? 'check-circle' : type === 'error' ? 'alert-circle' : 'info'}" | |
| class="w-5 h-5 mr-2"></i> | |
| <span>${message}</span> | |
| </div> | |
| `; | |
| document.body.appendChild(alert); | |
| feather.replace(); | |
| setTimeout(() => { | |
| alert.remove(); | |
| }, 5000); | |
| } | |
| // Gestion des événements | |
| document.addEventListener('DOMContentLoaded', function() { | |
| // Initialisation des tooltips | |
| const tooltips = document.querySelectorAll('[data-tooltip]'); | |
| tooltips.forEach(tooltip => { | |
| tooltip.addEventListener('mouseenter', function(e) { | |
| const tooltipText = this.getAttribute('data-tooltip'); | |
| const tooltipEl = document.createElement('div'); | |
| tooltipEl.className = 'absolute z-50 px-3 py-2 text-sm text-white bg-gray-900 rounded-md shadow-lg'; | |
| tooltipEl.textContent = tooltipText; | |
| tooltipEl.style.top = (e.clientY - 40) + 'px'; | |
| tooltipEl.style.left = (e.clientX + 10) + 'px'; | |
| document.body.appendChild(tooltipEl); | |
| this.addEventListener('mouseleave', function() { | |
| tooltipEl.remove(); | |
| }); | |
| }); | |
| }); | |
| // Gestion des formulaires | |
| const forms = document.querySelectorAll('form[data-ajax]'); | |
| forms.forEach(form => { | |
| form.addEventListener('submit', async function(e) { | |
| e.preventDefault(); | |
| const formData = new FormData(this); | |
| const submitBtn = this.querySelector('button[type="submit"]'); | |
| const originalText = submitBtn.textContent; | |
| submitBtn.disabled = true; | |
| submitBtn.innerHTML = '<div class="loading-spinner"></div>'; | |
| try { | |
| // Simulation de traitement | |
| await new Promise(resolve => setTimeout(resolve, 1000)); | |
| const data = Object.fromEntries(formData.entries()); | |
| if (this.id === 'intervention-form') { | |
| window.dataManager.addIntervention(data); | |
| showAlert('Intervention ajoutée avec succès', 'success'); | |
| } else if (this.id === 'culture-form |