#!/usr/bin/env python3 """ Script to make sure that lon and lat values of Scarpelli Mexico data files are rounded to 5 decimal places. """ from os.path import basename import numpy as np import xarray as xr import gcpy from glob import glob # Get the proper reader reader = gcpy.util.dataset_reader( multi_files=False, verbose=False ) # Keep xarray attributes with xr.set_options(keep_attrs=True): # Loop over each file for ifile in glob("../*nc*"): print(f"Fixing {ifile}") # Read data into an xr.Dataset object dset = reader( ifile, drop_variables=gcpy.constants.skip_these_vars ).load() # Round lon & lat up to 5 decimal places dset.coords["lon"] = np.round(dset.coords["lon"], 5) dset.coords["lat"] = np.round(dset.coords["lat"], 5) # Output file name dset.to_netcdf(f"../fixed.{basename(ifile)}") # Free memory del dset