const fs = require('fs'); const path = require('path'); const dayjs = require('dayjs'); const { exec } = require('child_process'); const { v4: uuidv4 } = require('uuid'); const { getSubdirectories, getFilesInDirectory } = require('./folder_reader.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`).add(8, 'hour').format("YYYY-MM-DD HH:mm:ss"); } exports.getGlobalWeatherData = async (req, res) => { const fileList = await getLastNewModel("gfs_1p00"); // 定义中国区域的经纬度范围 const CHINA_REGION = { minLon: 70, // 东经70度 maxLon: 140, // 东经140度 minLat: 15, // 北纬15度 maxLat: 55 // 北纬55度 }; const uniqueId = uuidv4(); const tempTxtFilePath = path.join(__dirname, `../../model/model_${uniqueId}.txt`); const regionCommand=`-small_grib ${CHINA_REGION.minLon}:${CHINA_REGION.maxLon} ${CHINA_REGION.minLat}:${CHINA_REGION.maxLat} ${tempTxtFilePath}`; const command = `"${path.join(__dirname, "../../wgrib2/wgrib2.exe")}" "${fileList.list[0]}" -match "TCDC:1000 mb" -text ${tempTxtFilePath}`; exec(command, (error, stdout, stderr) => { // 读取提取的数据文件 // const readFileRes = fs.readFileSync(tempTxtFilePath, { encoding: "utf-8" }); // 将数据返回给前端 res.rescc("成功", 200, stdout); }); }