#!/usr/bin/env python import numpy as np import xarray as xr import os from climdata import run_shell # country name and lon/lat ranges cntry_name = 'Thailand' cntry_sname = cntry_name.replace(' ', '') # IO files ifile = f'erai.tas.p75.{cntry_sname}.nc' ofile = ifile.replace('p75', 'p05') xname = 'lon' yname = 'lat' # get the lon/lat values ds = xr.open_dataset(ifile) x = ds[xname].values y = ds[yname].values # set the new grid ysign = 1 if y[-1] > y[0] else -1 # 1 if y increases; -1 if y decreases dx, dy = 0.05, 0.05 * ysign xsize, ysize = round( (x[-1] - x[0]) / dx ) + 1, round( (y[-1] - y[0]) /dy + 1 ) with open('newgrid', 'w') as f: f.write('gridtype = lonlat \n') f.write(f'xfirst = {x[0]} \n') f.write(f'xinc = {dx} \n') f.write(f'xsize = {int(xsize)} \n') f.write(f'yfirst = {y[0]} \n') f.write(f'yinc = {dy} \n') f.write(f'ysize = {int(ysize)} \n') run_shell(f'cdo remapbil,newgrid {ifile} {ofile}')