# command to auto-reload modules when they are edited (easier for testing and debugging)
%load_ext autoreload
%autoreload 2
import healpix_geo
import numpy as np
import matplotlib.pyplot as plt
from healpix_analyse.make_rectangle import make_healpix_rectangle_from_lonlat
level = 12
ellipsoid = 'WGS84'

# Coverage box
lon_min, lon_max = 20.0, 40.0
lat_min, lat_max = 40.0, 45.0

cell_ids_2D_array = make_healpix_rectangle_from_lonlat(bbox=(lon_min, lat_min, lon_max, lat_max), 
                                                       level=level, 
                                                       ellipsoid=ellipsoid)

lon, lat = healpix_geo.ring.healpix_to_lonlat(cell_ids_2D_array.flatten(), level, ellipsoid=ellipsoid)

definition = 200
data = np.cos(1024*lon/definition) * np.sin(256*lat/definition) + 1

plt.scatter(lon, lat, c=data, s=1)
<matplotlib.collections.PathCollection at 0x108e7fa30>
../_images/3d62bbfc9036e5541ef8afb1beb119cef99880481eeb1da4af79243dab50d2b3.png
cell_ids_2D_array
array([[386418, 386419, 386420, ..., 386514, 386515, 386516],
       [384661, 384662, 384663, ..., 384757, 384758, 384759],
       [382909, 382910, 382911, ..., 383005, 383006, 383007],
       ...,
       [ 62342,  62341,  62340, ...,  62410,  62411,  62412],
       [ 61638,  61637,  61636, ...,  61706,  61707,  61708],
       [ 60938,  60937,  60936, ...,  61005,  61006,  61007]],
      shape=(266, 99), dtype=uint64)
level = 12
ellipsoid = 'WGS84'
indexing_scheme = 'ring' # 'ring' or 'nested'
definition = 20

# Coverage box
lon_min, lon_max = 2.0, 6.0
lat_min, lat_max = 30.0, 32.0

cell_ids, _, _ = healpix_geo.nested.zone_coverage(bbox=(lon_min,lat_min,lon_max,lat_max),
                                                  depth=level, 
                                                  ellipsoid=ellipsoid)

if indexing_scheme == 'ring': 
    cell_ids = to_ring(cell_ids, depth=level)
    lon, lat = healpix_geo.ring.healpix_to_lonlat(cell_ids, level, ellipsoid=ellipsoid)
elif indexing_scheme == 'nested':
    lon, lat = healpix_geo.nested.healpix_to_lonlat(cell_ids, level, ellipsoid=ellipsoid)
else:
    raise NotImplementedError("For now, indexing_scheme must be 'ring' or 'nested'")

data = np.cos(1024*lon/definition) * np.sin(256*lat/definition) + 1

plt.scatter(lon, lat, c=data, s=1)
<matplotlib.collections.PathCollection at 0x303c02770>
../_images/284a582fa9360cc1c491659573dfee74c1a1bf9b3fd30bc70dac76843536896e.png
nu, cl = powerspectra(cell_ids, level, data, indexing_scheme=indexing_scheme, plot_2D_fft=True)
../_images/148975331fea17898fd13962dbda259efd0172bd8708f4c52d541c807f6663c4.png ../_images/28945848d1fa47dd89358b3feb44c66993f21dd36dadea89479140277ce6e76f.png
plt.plot(nu, cl)
plt.yscale('log')
plt.xscale('log')
../_images/8ecca3cd20d4604c540060770ee3ee48d6f7e037793648c7348838ae96de86eb.png
lats = np.unique(lat)
lons = np.unique(lon)
plt.plot(np.diff(lats), label='lat spacing')
plt.plot(np.diff(lons), label='lon spacing')
plt.legend()
plt.show()
../_images/1c0b031fa628c4a8e8bb68309b50aa5af1fe3884ac7ac31b3b182d7dfdec95d8.png