const fs = require('fs'); const path = require('path'); const dayjs = require('dayjs'); const { execSync } = require('child_process'); const { v4: uuidv4 } = require('uuid'); const { getSubdirectories, getFilesInDirectory } = require('./folder_reader.js'); const { createWindData } = require("../../schema/createWindData.js"); async function getLastNewModel(degree) { let gfsList = null; let hourList = null; gfsList = await getSubdirectories(path.join(__dirname, `../../model/grib2/${degree}`)); if (gfsList?.length) { let dates = []; for (let i = 0; i < gfsList.length; i++) { const filePath = gfsList[i].split("\\"); dates.push({ fileName: filePath[filePath.length - 1], path: gfsList[i], date: Number(filePath[filePath.length - 1].split(".")[1]) }); } dates.sort((a, b) => { return b.date - a.date; }); hourList = await getSubdirectories(path.join(__dirname, `../../model/grib2/${degree}/${dates[0].fileName}`)); if (hourList?.length) { let hours = []; for (let i = 0; i < hourList.length; i++) { const filePath = hourList[i].split("\\"); hours.push({ fileName: filePath[filePath.length - 1], path: hourList[i], date: Number(filePath[filePath.length - 1]) }); } hours.sort((a, b) => { return b.date - a.date; }); const fileList = await getFilesInDirectory(hours[0].path); // return global.downloadManager.manager.getStatus(); return { utcData: convertUtcDateForChinaDate(hours[0].path.split("gfs.")[1].split("\\")[0], hours[0].date - 8), chinaData: convertUtcDateForChinaDate(hours[0].path.split("gfs.")[1].split("\\")[0], hours[0].date), ...hours[0], list: fileList?.length ? fileList : [] }; } return []; } return []; } // utc 时间转中国时间 function convertUtcDateForChinaDate(day, hour) { return dayjs(`${day} ${hour}:00:00`).add(8, 'hour').format("YYYY-MM-DD HH:mm:ss"); } exports.getWeatherWindData = async (req, res) => { const fileList = await getLastNewModel("gfs_1p00"); const uniqueId = uuidv4(); const uTempTxtFilePath = path.join(__dirname, `../../model/model_u_${uniqueId}.csv`); const vTempTxtFilePath = path.join(__dirname, `../../model/model_v_${uniqueId}.csv`); // const command = `"${path.join(__dirname, "../../wgrib2/wgrib2.exe")}" "${fileList.list[0]}" -match "TCDC:entire atmosphere" -text ${tempTxtFilePath}`; const uCommand = `"${path.join(__dirname, "../../wgrib2/wgrib2.exe")}" "${fileList.list[1]}" -match "UGRD:10 m above ground" -csv ${uTempTxtFilePath}`; const vCommand = `"${path.join(__dirname, "../../wgrib2/wgrib2.exe")}" "${fileList.list[1]}" -match "VGRD:10 m above ground" -csv ${vTempTxtFilePath}`; const u = execSync(uCommand); const v = execSync(vCommand); createWindData(uTempTxtFilePath, vTempTxtFilePath); // 将数据返回给前端 res.rescc("成功", 200, { u, v }); }