1- import numpy as np
2- import xarray as xr
3- import fsspec
41import glob
52import os
63
4+ import fsspec
5+ import numpy as np
6+ import xarray as xr
77from torch .utils .data import ConcatDataset , DataLoader , Dataset , random_split
88
99SAT_MEAN = {
4141
4242
4343def _set_sat_coords (dataset : xr .Dataset ) -> xr .Dataset :
44- """Set variables as coordinates"""
44+ """Set variables as coordinates. """
4545 return dataset .set_coords (
4646 ["time_utc" , "channel_name" , "y_osgb" , "x_osgb" , "y_geostationary" , "x_geostationary" ]
4747 )
4848
49+
4950def load_netcdf (filename , engine = "h5netcdf" , * args , ** kwargs ) -> xr .Dataset :
5051 """Load a NetCDF dataset from local file system or cloud bucket."""
5152 with fsspec .open (filename , mode = "rb" ) as file :
@@ -54,7 +55,23 @@ def load_netcdf(filename, engine="h5netcdf", *args, **kwargs) -> xr.Dataset:
5455
5556
5657class Satellite (DataLoader ):
57- def __init__ (self , channels = ["IR_016" , "IR_039" , "IR_087" , "IR_097" , "IR_108" , "IR_120" , "IR_134" , "VIS006" , "VIS008" , "WV_062" , "WV_073" ], data_dir = "./" ):
58+ def __init__ (
59+ self ,
60+ channels = [
61+ "IR_016" ,
62+ "IR_039" ,
63+ "IR_087" ,
64+ "IR_097" ,
65+ "IR_108" ,
66+ "IR_120" ,
67+ "IR_134" ,
68+ "VIS006" ,
69+ "VIS008" ,
70+ "WV_062" ,
71+ "WV_073" ,
72+ ],
73+ data_dir = "./" ,
74+ ):
5875 self .channels = channels
5976 self .data_dir = data_dir
6077 if "HRV" in self .channels :
@@ -114,9 +131,14 @@ def __getitem__(self, item):
114131 std = np .expand_dims (std , axis = [1 , 2 , 3 ])
115132 hrvsatellite = hrvsatellite - mean
116133 hrvsatellite = hrvsatellite / std
117- input_data = hrvsatellite .values [:,:7 ]
118- target_data = hrvsatellite .values [:,7 :]
119- merged_data = (
120- np .concatenate ((input_data , np .expand_dims (dataset ["y_osgb" ].values , axis = [1 ,2 ]),np .expand_dims (dataset ["x_osgb" ].values , axis = [1 ,2 ])), 1 )
121- ) # Now in Batch, Time+Coord, Channel, W, H orderk
134+ input_data = hrvsatellite .values [:, :7 ]
135+ target_data = hrvsatellite .values [:, 7 :]
136+ merged_data = np .concatenate (
137+ (
138+ input_data ,
139+ np .expand_dims (dataset ["y_osgb" ].values , axis = [1 , 2 ]),
140+ np .expand_dims (dataset ["x_osgb" ].values , axis = [1 , 2 ]),
141+ ),
142+ 1 ,
143+ ) # Now in Batch, Time+Coord, Channel, W, H orderk
122144 return np .squeeze (merged_data ), np .squeeze (target_data )
0 commit comments