getAreaWeatherData.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. const fs = require('fs');
  2. const path = require('path');
  3. const dayjs = require('dayjs');
  4. const { execSync } = require('child_process');
  5. const { v4: uuidv4 } = require('uuid');
  6. const { getSubdirectories, getFilesInDirectory } = require('./folder_reader.js');
  7. const { createCloudImage } = require("../../schema/createCloudImage_old.js");
  8. const { createCloudImageDeep } = require("../../schema/createCloudImage.js");
  9. async function getLastNewModel(degree) {
  10. let gfsList = null;
  11. let hourList = null;
  12. gfsList = await getSubdirectories(path.join(__dirname, `../../model/grib2/${degree}`));
  13. if (gfsList?.length) {
  14. let dates = [];
  15. for (let i = 0; i < gfsList.length; i++) {
  16. const filePath = gfsList[i].split("\\");
  17. dates.push({
  18. fileName: filePath[filePath.length - 1],
  19. path: gfsList[i],
  20. date: Number(filePath[filePath.length - 1].split(".")[1])
  21. });
  22. }
  23. dates.sort((a, b) => {
  24. return b.date - a.date;
  25. });
  26. hourList = await getSubdirectories(path.join(__dirname, `../../model/grib2/${degree}/${dates[0].fileName}`));
  27. if (hourList?.length) {
  28. let hours = [];
  29. for (let i = 0; i < hourList.length; i++) {
  30. const filePath = hourList[i].split("\\");
  31. hours.push({
  32. fileName: filePath[filePath.length - 1],
  33. path: hourList[i],
  34. date: Number(filePath[filePath.length - 1])
  35. });
  36. }
  37. hours.sort((a, b) => {
  38. return b.date - a.date;
  39. });
  40. const fileList = await getFilesInDirectory(hours[0].path);
  41. // return global.downloadManager.manager.getStatus();
  42. return {
  43. utcData: convertUtcDateForChinaDate(hours[0].path.split("gfs.")[1].split("\\")[0], hours[0].date - 8),
  44. chinaData: convertUtcDateForChinaDate(hours[0].path.split("gfs.")[1].split("\\")[0], hours[0].date),
  45. ...hours[0],
  46. list: fileList?.length ? fileList : []
  47. };
  48. }
  49. return [];
  50. }
  51. return [];
  52. }
  53. // utc 时间转中国时间
  54. function convertUtcDateForChinaDate(day, hour) {
  55. return dayjs(`${day} ${hour}:00:00`).add(8, 'hour').format("YYYY-MM-DD HH:mm:ss");
  56. }
  57. exports.getAreaWeatherData = async (req, res) => {
  58. const fileList = await getLastNewModel("gfs_1p00");
  59. const uniqueId = uuidv4();
  60. const tempTxtFilePath = path.join(__dirname, `../../model/model_${uniqueId}.txt`);
  61. // const regionCommand=`-small_grib ${CHINA_REGION.minLon}:${CHINA_REGION.maxLon} ${CHINA_REGION.minLat}:${CHINA_REGION.maxLat} ${tempTxtFilePath}`;
  62. const type = "tmp";
  63. let stdout = null;
  64. if (!fs.existsSync(path.join(__dirname, `../../tempDir`))) {
  65. fs.mkdirSync(path.join(__dirname, `../../tempDir`), { recursive: true });
  66. }
  67. if (type === "cloud") {
  68. // 云层图
  69. const command = `"${path.join(__dirname, "../../wgrib2/wgrib2.exe")}" "${fileList.list[0]}" -match "TCDC:surface" -text ${tempTxtFilePath}`;
  70. stdout = execSync(command);
  71. await createCloudImage(tempTxtFilePath, path.join(__dirname, `../../tempDir/cloud.png`), { r: 255, g: 255, b: 255 }, "cloud");
  72. } else if (type === "rain") {
  73. // 降雨图
  74. const command = `"${path.join(__dirname, "../../wgrib2/wgrib2.exe")}" "${fileList.list[0]}" -match "RWMR:50 mb" -text ${tempTxtFilePath}`;
  75. stdout = execSync(command);
  76. await createCloudImage(tempTxtFilePath, path.join(__dirname, `../../tempDir/rain.png`), { r: 64, g: 158, b: 255 }, "rain");
  77. } else if (type === "tmp") {
  78. // 降雨图
  79. const command = `"${path.join(__dirname, "../../wgrib2/wgrib2.exe")}" "${fileList.list[0]}" -match "TMP:2 m above ground" -text ${tempTxtFilePath}`;
  80. stdout = execSync(command);
  81. await createCloudImage(tempTxtFilePath, path.join(__dirname, `../../tempDir/tmp.png`), { r: 0, g: 0, b: 0 }, "tmp");
  82. }
  83. // await createCloudImageDeep(tempTxtFilePath);
  84. // 将数据返回给前端
  85. res.rescc("成功", 200, { stdout, tempTxtFilePath });
  86. }