methane_super_emitters.windfield
1import cdsapi 2import netCDF4 3import click 4import numpy as np 5from scipy.ndimage import zoom 6import uuid 7import os 8import tempfile 9import glob 10from pathlib import Path 11 12 13def download_windfield(input_file, output_file, tmp_dir): 14 c = cdsapi.Client() 15 data = np.load(input_file, allow_pickle=True) 16 latitude = data["lat"] 17 longitude = data["lon"] 18 area = [ 19 float(latitude.min()), 20 float(longitude.min()), 21 float(latitude.max()), 22 float(longitude.max()), 23 ] 24 time = data["time"].min() 25 tmp_file = os.path.join(tmp_dir, str(uuid.uuid4()) + ".nc") 26 c.retrieve( 27 "reanalysis-era5-single-levels", 28 { 29 "product_type": "reanalysis", 30 "variable": ["10m_u_component_of_wind", "10m_v_component_of_wind"], 31 "year": [f"{time.year}"], 32 "month": [f"{time.month:02d}"], 33 "day": [f"{time.day:02d}"], 34 "time": [f"{time.hour:02d}:00"], 35 "area": area, 36 "format": "netcdf", 37 }, 38 tmp_file, 39 ) 40 with netCDF4.Dataset(tmp_file, "r") as fd: 41 u = fd["u10"][:][0] 42 v = fd["v10"][:][0] 43 zoom_factors = (32 / u.shape[0], 32 / u.shape[1]) 44 u_ups = zoom(u, zoom_factors, order=3) 45 v_ups = zoom(v, zoom_factors, order=3) 46 np.savez( 47 output_file, 48 methane=data["methane"], 49 lat=data["lat"], 50 lon=data["lon"], 51 qa=data["qa"], 52 time=data["time"], 53 mask=data["mask"], 54 non_destriped=data["non_destriped"], 55 u=u_ups, 56 v=v_ups, 57 ) 58 59 60@click.command() 61@click.option("-i", "--input-dir", help="Input directory") 62@click.option("-o", "--output-dir", help="Output directory") 63def main(input_dir, output_dir): 64 tmp_dir_ = tempfile.TemporaryDirectory() 65 tmp_dir = tmp_dir_.name 66 for input_file in glob.glob(os.path.join(input_dir, "*.npz")): 67 stem = Path(input_file).stem 68 output_file = os.path.join(output_dir, stem + "_wf.npz") 69 if os.path.isfile(output_file): 70 continue 71 download_windfield(input_file, output_file, tmp_dir) 72 tmp_dir_.cleanup() 73 74 75if __name__ == "__main__": 76 main()
def
download_windfield(input_file, output_file, tmp_dir):
14def download_windfield(input_file, output_file, tmp_dir): 15 c = cdsapi.Client() 16 data = np.load(input_file, allow_pickle=True) 17 latitude = data["lat"] 18 longitude = data["lon"] 19 area = [ 20 float(latitude.min()), 21 float(longitude.min()), 22 float(latitude.max()), 23 float(longitude.max()), 24 ] 25 time = data["time"].min() 26 tmp_file = os.path.join(tmp_dir, str(uuid.uuid4()) + ".nc") 27 c.retrieve( 28 "reanalysis-era5-single-levels", 29 { 30 "product_type": "reanalysis", 31 "variable": ["10m_u_component_of_wind", "10m_v_component_of_wind"], 32 "year": [f"{time.year}"], 33 "month": [f"{time.month:02d}"], 34 "day": [f"{time.day:02d}"], 35 "time": [f"{time.hour:02d}:00"], 36 "area": area, 37 "format": "netcdf", 38 }, 39 tmp_file, 40 ) 41 with netCDF4.Dataset(tmp_file, "r") as fd: 42 u = fd["u10"][:][0] 43 v = fd["v10"][:][0] 44 zoom_factors = (32 / u.shape[0], 32 / u.shape[1]) 45 u_ups = zoom(u, zoom_factors, order=3) 46 v_ups = zoom(v, zoom_factors, order=3) 47 np.savez( 48 output_file, 49 methane=data["methane"], 50 lat=data["lat"], 51 lon=data["lon"], 52 qa=data["qa"], 53 time=data["time"], 54 mask=data["mask"], 55 non_destriped=data["non_destriped"], 56 u=u_ups, 57 v=v_ups, 58 )