Guest User

Untitled

a guest
Nov 14th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. # Manual Coarsen
  2. # --------------
  3. # Author : Riley X. Brady
  4. # Date : 11/14/2018
  5. # --------------
  6. # The pyAMG method of coarsening our particles traverses in 3D, rather than
  7. # just removing full 2D columns. This method simply strides through the
  8. # lat and lon list, removing full columns.
  9. #
  10. # NOTE: Run make_particle_file.py first without any coarsening turned on.
  11. # This will take in that particles.nc file and coarsen it.
  12. import numpy as np
  13. import xarray as xr
  14. import argparse
  15.  
  16.  
  17. def xyz_to_lat_lon(x, y, z):
  18. x, y, z = np.asarray(x), np.asarray(y), np.asarray(z)
  19. plat = np.arcsin(z / np.sqrt(x**2 + y**2 + z**2))
  20. plong = np.arctan2(y, x)
  21. # longitude adjustment
  22. plong[plong < 0.0] = 2*np.pi + plong[plong < 0.0]
  23. return plong * 180./np.pi, \
  24. plat * 180./np.pi
  25.  
  26.  
  27. def main(init, stride, output):
  28. ds = xr.open_dataset(init)
  29. plong, plat = xyz_to_lat_lon(ds.xParticle, ds.yParticle, ds.zParticle)
  30. ds.coords['particleLat'] = (('Time', 'nParticles'), plat)
  31. ds.coords['particleLon'] = (('Time', 'nParticles'), plong)
  32. un_lon, un_lat = np.unique(ds.particleLon), np.unique(ds.particleLat)
  33. # lat filter
  34. latlist = un_lat[0::stride]
  35. particleList = []
  36. for i, lat in enumerate(ds.squeeze().particleLat.values):
  37. if lat in latlist:
  38. particleList.append(i)
  39. ds_lat_trimmed = ds.sel(nParticles=particleList)
  40. # lon filter
  41. lonlist = un_lon[0::stride]
  42. particleList = []
  43. for i, lon in enumerate(ds_lat_trimmed.squeeze().particleLon.values):
  44. if lon in lonlist:
  45. particleList.append(i)
  46. ds_full_trimmed = ds_lat_trimmed.sel(nParticles=particleList)
  47. ds_full_trimmed.to_netcdf(output)
  48.  
  49.  
  50. if __name__ == '__main__':
  51. ap = argparse.ArgumentParser(
  52. description="Coarsens particle init file horizontally by a determined stride.")
  53. ap.add_argument('-i', '--init', required=True, type=str,
  54. help="Particle init file (netCDF)")
  55. ap.add_argument('-s', '--stride', required=True, type=int,
  56. help="Number of particles to skip in lat and lon")
  57. ap.add_argument('-o', '--output', required=True, type=str,
  58. help="Coarsened particle output file (.nc at end)")
  59. args = vars(ap.parse_args())
  60. init = args['init']
  61. stride = args['stride']
  62. output = args['output']
  63. main(init, stride, output)
Add Comment
Please, Sign In to add comment