| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- 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 { createCloudImage } = require("../../schema/createCloudImage_old.js");
- const { createCloudImageDeep } = require("../../schema/createCloudImage.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.getAreaWeatherData = async (req, res) => {
- const fileList = await getLastNewModel("gfs_1p00");
- 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 type = "tmp";
- let stdout = null;
- if (!fs.existsSync(path.join(__dirname, `../../tempDir`))) {
- fs.mkdirSync(path.join(__dirname, `../../tempDir`), { recursive: true });
- }
- if (type === "cloud") {
- // 云层图
- const command = `"${path.join(__dirname, "../../wgrib2/wgrib2.exe")}" "${fileList.list[0]}" -match "TCDC:surface" -text ${tempTxtFilePath}`;
- stdout = execSync(command);
- await createCloudImage(tempTxtFilePath, path.join(__dirname, `../../tempDir/cloud.png`), { r: 255, g: 255, b: 255 }, "cloud");
- } else if (type === "rain") {
- // 降雨图
- const command = `"${path.join(__dirname, "../../wgrib2/wgrib2.exe")}" "${fileList.list[0]}" -match "RWMR:50 mb" -text ${tempTxtFilePath}`;
- stdout = execSync(command);
- await createCloudImage(tempTxtFilePath, path.join(__dirname, `../../tempDir/rain.png`), { r: 64, g: 158, b: 255 }, "rain");
- } else if (type === "tmp") {
- // 降雨图
- const command = `"${path.join(__dirname, "../../wgrib2/wgrib2.exe")}" "${fileList.list[0]}" -match "TMP:2 m above ground" -text ${tempTxtFilePath}`;
- stdout = execSync(command);
- await createCloudImage(tempTxtFilePath, path.join(__dirname, `../../tempDir/tmp.png`), { r: 0, g: 0, b: 0 }, "tmp");
- }
- // await createCloudImageDeep(tempTxtFilePath);
- // 将数据返回给前端
- res.rescc("成功", 200, { stdout, tempTxtFilePath });
- }
|