Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import xarray as xr
- import glob
- # Navigate to directory with .nc files
- directory = '/Users/geel5095/Desktop/obs-le/seawifs/'
- # Identify new directory to store .nc files with time stamp
- output_directory = '/Users/geel5095/Desktop/obs-le/seawifs_post_processed/'
- # This is an arbitrary order
- filelist = glob.glob(os.path.join(directory, '*.nc'))
- # enumerate also prints a counter (i) along
- for i, file in enumerate(sorted(filelist)):
- print(f'{file}...')
- ds = xr.open_dataset(file)
- # Add time dimension
- ds = ds.expand_dims('time')
- # Make each time coordinate an interger that increases monotonically with each file
- ds['time'] = [i]
- # Save out with same file name to new folder
- # file.split() command puls file name and discards original directory
- filename = file.split('/')[-1]
- # `ncrcat` can only concatenate over unlimited, or "record" variables.
- # This means you expect this variable to expand, such as time.
- # The alternative is a "fixed" variable such as lat/lon.
- # This ensures that time is saved out as a record variable.
- ds.to_netcdf(path=f'{output_directory}/{filename}', unlimited_dims='time',
- engine='scipy')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement