# 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
import sys
sys.path.append("..")
from healpix_analyse.make_rectangle import make_healpix_rectangle_from_lonlat
from healpix_analyse.resample import resample_to_latlon_grid
from healpix_analyse.ps import ps
level = 12
ellipsoid = 'WGS84'

# Coverage box
lon_min, lon_max = 2.0, 10.0
lat_min, lat_max = 30.0, 32.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)

def mock_data_function(lon, lat):
    definition = 20
    return np.cos(1024*(lon/definition + .1)) * np.sin(256*(lat/definition+.06)) + 0.5

data = mock_data_function(lon, lat)

plt.scatter(lon, lat, c=data, s=1)
<matplotlib.collections.PathCollection at 0x11bec51e0>
../_images/e685116a867941222fb6c89a43e48b152d912ea688ae28bccd21855b693fc872.png
cell_ids_2D_array
array([[50503771, 50503772, 50503773, ..., 50504133, 50504134, 50504135],
       [50487387, 50487388, 50487389, ..., 50487749, 50487750, 50487751],
       [50471003, 50471004, 50471005, ..., 50471365, 50471366, 50471367],
       ...,
       [47505499, 47505500, 47505501, ..., 47505861, 47505862, 47505863],
       [47489115, 47489116, 47489117, ..., 47489477, 47489478, 47489479],
       [47472731, 47472732, 47472733, ..., 47473093, 47473094, 47473095]],
      shape=(186, 365), dtype=uint64)
# reshape lat, lon, data as 2D array like cell_ids_2D_array
lat = lat.reshape(cell_ids_2D_array.shape)
lon = lon.reshape(cell_ids_2D_array.shape)
data = data.reshape(cell_ids_2D_array.shape)
data_resampled = resample_to_latlon_grid(lat, lon, data, method='cubic')
lon_grid, lat_grid = np.meshgrid(np.linspace(lon.min(), lon.max(), data.shape[1]),
                                 np.linspace(lat.min(), lat.max(), data.shape[0]))

data_truth = mock_data_function(lon_grid, lat_grid)
plt.imshow(data_resampled)
plt.colorbar()
plt.show()

plt.imshow(data_resampled - data_truth)
plt.colorbar()
plt.show()
../_images/4955b5edb0484cba1b970d18e80e5a18ed90cf7a732c2b0f62cf90fd74b7a97d.png ../_images/dfb7d43302851868a64aa57c7173cfb3fab66ada5982488c6747d6bbe5fd52e4.png
plt.imshow(np.fft.fftshift(np.abs(np.fft.fft2(data_truth))), norm='log', vmin=1e1)
plt.colorbar()
plt.show()

plt.imshow(np.fft.fftshift(np.abs(np.fft.fft2(data_resampled))), norm='log', vmin=1e1)
plt.colorbar()
plt.show()
../_images/e4dc569ac501e5125bba698563f53e8071b0d0d0bf749000fb89fc8e9f7a4a06.png ../_images/20d7d07cb8f6cfb2a4daae1d114f9491c2e9c684add2cbcf48c567b5738f4228.png
nu_truth, cl_truth = ps(data_truth)
nu, cl = ps(data_resampled)
nu_cross, cl_cross = ps(data=data_resampled, data_cross=data_truth)
plt.plot(nu, cl, label='PS with resampling')
plt.plot(nu_truth, cl_truth, label='PS of truth')
plt.plot(nu_cross, cl_cross, label='cross PS truth and resampled')
plt.yscale('log')
plt.xscale('log')
plt.legend()
<matplotlib.legend.Legend at 0x11c64c640>
../_images/1e70ced0995bfea99e1f2e3405a80320ed6812fa5575e685f8270e1fd1d5ffa6.png