|
| 1 | +# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. |
| 2 | + |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | + |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | +# ref: https://github.com/HaxyMoly/Pangu-Weather-ReadyToGo/blob/main/forecast_decode_functions.py |
| 16 | + |
| 17 | +import os |
| 18 | +from os import path as osp |
| 19 | +from typing import Dict |
| 20 | + |
| 21 | +import hydra |
| 22 | +import netCDF4 as nc |
| 23 | +import numpy as np |
| 24 | + |
| 25 | +from ppsci.utils import logger |
| 26 | + |
| 27 | + |
| 28 | +def convert_surface_data_to_nc( |
| 29 | + surface_file: str, file_name: str, output_dir: str |
| 30 | +) -> None: |
| 31 | + surface_data = np.load(surface_file) |
| 32 | + u_component_of_wind_10m = surface_data[0] |
| 33 | + v_component_of_wind_10m = surface_data[1] |
| 34 | + temperature_2m = surface_data[2] |
| 35 | + mean_sea_level_pressure = surface_data[3] |
| 36 | + |
| 37 | + with nc.Dataset( |
| 38 | + os.path.join(output_dir, file_name), "w", format="NETCDF4_CLASSIC" |
| 39 | + ) as nc_file: |
| 40 | + # Create dimensions |
| 41 | + nc_file.createDimension("longitude", 1440) |
| 42 | + nc_file.createDimension("latitude", 721) |
| 43 | + |
| 44 | + # Create variables |
| 45 | + nc_lon = nc_file.createVariable("longitude", np.float32, ("longitude",)) |
| 46 | + nc_lat = nc_file.createVariable("latitude", np.float32, ("latitude",)) |
| 47 | + nc_msl = nc_file.createVariable( |
| 48 | + "mean_sea_level_pressure", np.float32, ("latitude", "longitude") |
| 49 | + ) |
| 50 | + nc_u10 = nc_file.createVariable( |
| 51 | + "u_component_of_wind_10m", np.float32, ("latitude", "longitude") |
| 52 | + ) |
| 53 | + nc_v10 = nc_file.createVariable( |
| 54 | + "v_component_of_wind_10m", np.float32, ("latitude", "longitude") |
| 55 | + ) |
| 56 | + nc_t2m = nc_file.createVariable( |
| 57 | + "temperature_2m", np.float32, ("latitude", "longitude") |
| 58 | + ) |
| 59 | + |
| 60 | + # Set variable attributes |
| 61 | + nc_lon.units = "degrees_east" |
| 62 | + nc_lat.units = "degrees_north" |
| 63 | + nc_msl.units = "Pa" |
| 64 | + nc_u10.units = "m/s" |
| 65 | + nc_v10.units = "m/s" |
| 66 | + nc_t2m.units = "K" |
| 67 | + |
| 68 | + # Write data to variables |
| 69 | + nc_lon[:] = np.linspace(0.125, 359.875, 1440) |
| 70 | + nc_lat[:] = np.linspace(90, -90, 721) |
| 71 | + nc_msl[:] = mean_sea_level_pressure |
| 72 | + nc_u10[:] = u_component_of_wind_10m |
| 73 | + nc_v10[:] = v_component_of_wind_10m |
| 74 | + nc_t2m[:] = temperature_2m |
| 75 | + |
| 76 | + logger.info( |
| 77 | + f"Convert output surface data file {surface_file} as nc format and save to {output_dir}/{file_name}." |
| 78 | + ) |
| 79 | + |
| 80 | + |
| 81 | +def convert_upper_data_to_nc(upper_file: str, file_name: str, output_dir: str) -> None: |
| 82 | + # Load the saved numpy arrays |
| 83 | + upper_data = np.load(upper_file) |
| 84 | + |
| 85 | + # surface data offset |
| 86 | + st = 4 |
| 87 | + level = 13 |
| 88 | + |
| 89 | + geopotential = upper_data[st : st + level] |
| 90 | + specific_humidity = upper_data[st + level : st + 2 * level] |
| 91 | + u_component_of_wind = upper_data[st + 2 * level : st + 3 * level] |
| 92 | + v_component_of_wind = upper_data[st + 3 * level : st + 4 * level] |
| 93 | + temperature = upper_data[st + 4 * level :] |
| 94 | + |
| 95 | + with nc.Dataset( |
| 96 | + os.path.join(output_dir, file_name), "w", format="NETCDF4_CLASSIC" |
| 97 | + ) as nc_file: |
| 98 | + # Create dimensions |
| 99 | + nc_file.createDimension("longitude", 1440) |
| 100 | + nc_file.createDimension("latitude", 721) |
| 101 | + nc_file.createDimension("level", level) |
| 102 | + |
| 103 | + # Create variables |
| 104 | + nc_lon = nc_file.createVariable("longitude", np.float32, ("longitude",)) |
| 105 | + nc_lat = nc_file.createVariable("latitude", np.float32, ("latitude",)) |
| 106 | + nc_geopotential = nc_file.createVariable( |
| 107 | + "geopotential", np.float32, ("level", "latitude", "longitude") |
| 108 | + ) |
| 109 | + nc_specific_humidity = nc_file.createVariable( |
| 110 | + "specific_humidity", np.float32, ("level", "latitude", "longitude") |
| 111 | + ) |
| 112 | + nc_temperature = nc_file.createVariable( |
| 113 | + "temperature", np.float32, ("level", "latitude", "longitude") |
| 114 | + ) |
| 115 | + nc_u_component_of_wind = nc_file.createVariable( |
| 116 | + "u_component_of_wind", np.float32, ("level", "latitude", "longitude") |
| 117 | + ) |
| 118 | + nc_v_component_of_wind = nc_file.createVariable( |
| 119 | + "v_component_of_wind", np.float32, ("level", "latitude", "longitude") |
| 120 | + ) |
| 121 | + |
| 122 | + # Set variable attributes |
| 123 | + nc_lon.units = "degrees_east" |
| 124 | + nc_lat.units = "degrees_north" |
| 125 | + nc_geopotential.units = "m" |
| 126 | + nc_specific_humidity.units = "kg/kg" |
| 127 | + nc_temperature.units = "K" |
| 128 | + nc_u_component_of_wind.units = "m/s" |
| 129 | + nc_v_component_of_wind.units = "m/s" |
| 130 | + # Write data to variables |
| 131 | + nc_lon[:] = np.linspace(0.125, 359.875, 1440) |
| 132 | + nc_lat[:] = np.linspace(90, -90, 721) |
| 133 | + nc_geopotential[:] = geopotential |
| 134 | + nc_specific_humidity[:] = specific_humidity |
| 135 | + nc_temperature[:] = temperature |
| 136 | + nc_u_component_of_wind[:] = u_component_of_wind |
| 137 | + nc_v_component_of_wind[:] = v_component_of_wind |
| 138 | + |
| 139 | + logger.info( |
| 140 | + f"Convert output upper data file {upper_file} as nc format and save to {output_dir}/{file_name}." |
| 141 | + ) |
| 142 | + |
| 143 | + |
| 144 | +def convert(cfg: Dict): |
| 145 | + output_dir = cfg.output_dir |
| 146 | + |
| 147 | + for _, file_name in os.listdir(output_dir): |
| 148 | + if not file_name.endwiths("npy"): |
| 149 | + continue |
| 150 | + |
| 151 | + convert_surface_data_to_nc( |
| 152 | + osp.join(output_dir, file_name), |
| 153 | + osp.basename(file_name) + "_surface.nc", |
| 154 | + output_dir, |
| 155 | + ) |
| 156 | + convert_upper_data_to_nc( |
| 157 | + osp.join(output_dir, file_name), |
| 158 | + osp.basename(file_name) + "_upper.nc", |
| 159 | + output_dir, |
| 160 | + ) |
| 161 | + |
| 162 | + |
| 163 | +@hydra.main(version_base=None, config_path="./conf", config_name="fengwu.yaml") |
| 164 | +def main(cfg: Dict): |
| 165 | + if cfg.mode == "infer": |
| 166 | + convert(cfg) |
| 167 | + else: |
| 168 | + raise ValueError(f"cfg.mode should in ['infer'], but got '{cfg.mode}'") |
| 169 | + |
| 170 | + |
| 171 | +if __name__ == "__main__": |
| 172 | + main() |
0 commit comments