getGlobalWeatherData.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. const fs = require('fs');
  2. const path = require('path');
  3. const dayjs = require('dayjs');
  4. const { exec } = require('child_process');
  5. const { v4: uuidv4 } = require('uuid');
  6. const { getSubdirectories, getFilesInDirectory } = require('./folder_reader.js');
  7. async function getLastNewModel(degree) {
  8. let gfsList = null;
  9. let hourList = null;
  10. gfsList = await getSubdirectories(path.join(__dirname, `../../model/grib2/${degree}`));
  11. if (gfsList?.length) {
  12. let dates = [];
  13. for (let i = 0; i < gfsList.length; i++) {
  14. const filePath = gfsList[i].split("\\");
  15. dates.push({
  16. fileName: filePath[filePath.length - 1],
  17. path: gfsList[i],
  18. date: Number(filePath[filePath.length - 1].split(".")[1])
  19. });
  20. }
  21. dates.sort((a, b) => {
  22. return b.date - a.date;
  23. });
  24. hourList = await getSubdirectories(path.join(__dirname, `../../model/grib2/${degree}/${dates[0].fileName}`));
  25. if (hourList?.length) {
  26. let hours = [];
  27. for (let i = 0; i < hourList.length; i++) {
  28. const filePath = hourList[i].split("\\");
  29. hours.push({
  30. fileName: filePath[filePath.length - 1],
  31. path: hourList[i],
  32. date: Number(filePath[filePath.length - 1])
  33. });
  34. }
  35. hours.sort((a, b) => {
  36. return b.date - a.date;
  37. });
  38. const fileList = await getFilesInDirectory(hours[0].path);
  39. // return global.downloadManager.manager.getStatus();
  40. return {
  41. utcData: convertUtcDateForChinaDate(hours[0].path.split("gfs.")[1].split("\\")[0], hours[0].date - 8),
  42. chinaData: convertUtcDateForChinaDate(hours[0].path.split("gfs.")[1].split("\\")[0], hours[0].date),
  43. ...hours[0],
  44. list: fileList?.length ? fileList : []
  45. };
  46. }
  47. return [];
  48. }
  49. return [];
  50. }
  51. // utc 时间转中国时间
  52. function convertUtcDateForChinaDate(day, hour) {
  53. return dayjs(`${day} ${hour}:00`).add(8, 'hour').format("YYYY-MM-DD HH:mm:ss");
  54. }
  55. exports.getGlobalWeatherData = async (req, res) => {
  56. const fileList = await getLastNewModel("gfs_1p00");
  57. // 定义中国区域的经纬度范围
  58. const CHINA_REGION = {
  59. minLon: 70, // 东经70度
  60. maxLon: 140, // 东经140度
  61. minLat: 15, // 北纬15度
  62. maxLat: 55 // 北纬55度
  63. };
  64. const uniqueId = uuidv4();
  65. const tempTxtFilePath = path.join(__dirname, `../../model/model_${uniqueId}.txt`);
  66. const regionCommand=`-small_grib ${CHINA_REGION.minLon}:${CHINA_REGION.maxLon} ${CHINA_REGION.minLat}:${CHINA_REGION.maxLat} ${tempTxtFilePath}`;
  67. const command = `"${path.join(__dirname, "../../wgrib2/wgrib2.exe")}" "${fileList.list[0]}" -match "TCDC:1000 mb" -text ${tempTxtFilePath}`;
  68. exec(command, (error, stdout, stderr) => {
  69. // 读取提取的数据文件
  70. // const readFileRes = fs.readFileSync(tempTxtFilePath, { encoding: "utf-8" });
  71. // 将数据返回给前端
  72. res.rescc("成功", 200, stdout);
  73. });
  74. }