Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. import os
  2. import xarray as xr
  3. import glob
  4.  
  5. # Navigate to directory with .nc files
  6. directory = '/Users/geel5095/Desktop/obs-le/seawifs/'
  7. # Identify new directory to store .nc files with time stamp
  8. output_directory = '/Users/geel5095/Desktop/obs-le/seawifs_post_processed/'
  9. # This is an arbitrary order
  10. filelist = glob.glob(os.path.join(directory, '*.nc'))
  11.  
  12. # enumerate also prints a counter (i) along
  13. for i, file in enumerate(sorted(filelist)):
  14. print(f'{file}...')
  15. ds = xr.open_dataset(file)
  16. # Add time dimension
  17. ds = ds.expand_dims('time')
  18. # Make each time coordinate an interger that increases monotonically with each file
  19. ds['time'] = [i]
  20. # Save out with same file name to new folder
  21. # file.split() command puls file name and discards original directory
  22. filename = file.split('/')[-1]
  23. # `ncrcat` can only concatenate over unlimited, or "record" variables.
  24. # This means you expect this variable to expand, such as time.
  25. # The alternative is a "fixed" variable such as lat/lon.
  26. # This ensures that time is saved out as a record variable.
  27. ds.to_netcdf(path=f'{output_directory}/{filename}', unlimited_dims='time',
  28. engine='scipy')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement