| 1 |
#include <math.h> |
| 2 |
|
| 3 |
#include "interpolation.h" |
| 4 |
#include "healpix.h" |
| 5 |
|
| 6 |
// Old versions of MSVC do not support C99 and therefore |
| 7 |
// do not define NAN in math.h. |
| 8 |
#ifndef NAN |
| 9 |
static const union { |
| 10 |
unsigned long integer; |
| 11 |
float value; |
| 12 |
} type_punned_nan = {0xFFFFFFFFFFFFFFFFul}; |
| 13 |
#define NAN (type_punned_nan.value) |
| 14 |
#endif |
| 15 |
|
| 16 |
#ifndef M_PI |
| 17 |
#define M_PI 3.14159265358979323846 |
| 18 |
#endif |
| 19 |
|
| 20 |
void interpolate_weights(double lon, double lat, int64_t *ring_indices, |
| 21 |
double *weights, int Nside) { |
| 22 |
|
| 23 |
// Given a longitude and a latitude, Nside, and pre-allocated arrays of 4 |
| 24 |
// elements ring_indices and weights, find the ring index of the four nearest |
| 25 |
// neighbours and the weights to use for each neighbour to interpolate. |
| 26 |
|
| 27 |
int64_t xy_index, npix; |
| 28 |
int64_t ring1, ring2, ring3, ring4; |
| 29 |
double lon1, lat1, lon2, lat2; |
| 30 |
double lon3, lat3, lon4, lat4; |
| 31 |
double xfrac1, xfrac2, yfrac, lon_frac; |
| 32 |
int ring_number, longitude_index, n_in_ring; |
| 33 |
|
| 34 |
// Find the xy index of the pixel in which the coordinates fall |
| 35 |
xy_index = radec_to_healpixl(lon, lat, Nside); |
| 36 |
|
| 37 |
// Find the lon/lat of the center of that pixel |
| 38 |
healpixl_to_radec(xy_index, Nside, 0.5, 0.5, &lon1, &lat1); |
| 39 |
|
| 40 |
// Take into account possible wrapping so that the pixel longitude/latitude |
| 41 |
// are close to the requested longitude/latitude |
| 42 |
if (lon - lon1 > M_PI) |
| 43 |
lon1 += 2 * M_PI; |
| 44 |
if (lon1 - lon > M_PI) |
| 45 |
lon1 -= 2 * M_PI; |
| 46 |
|
| 47 |
// Convert to a ring index and decompose into ring number and longitude index |
| 48 |
ring1 = healpixl_xy_to_ring(xy_index, Nside); |
| 49 |
if (ring1 < 0) |
| 50 |
{ |
| 51 |
int i; |
| 52 |
for (i = 0; i < 4; i ++) |
| 53 |
{ |
| 54 |
ring_indices[i] = -1; |
| 55 |
weights[i] = NAN; |
| 56 |
} |
| 57 |
return; |
| 58 |
} |
| 59 |
healpixl_decompose_ring(ring1, Nside, &ring_number, &longitude_index); |
| 60 |
|
| 61 |
// Figure out how many pixels are in the ring |
| 62 |
if (ring_number < Nside) { |
| 63 |
n_in_ring = 4 * ring_number; |
| 64 |
} else if (ring_number < 3 * Nside) { |
| 65 |
n_in_ring = 4 * Nside; |
| 66 |
} else { |
| 67 |
n_in_ring = (int)(4 * (4 * (int64_t)Nside - (int64_t)ring_number)); |
| 68 |
} |
| 69 |
|
| 70 |
// We now want to find the next index in the ring so that the point to |
| 71 |
// interpolate is between the two. First we check what direction to look in by |
| 72 |
// finding the longitude/latitude of the center of the HEALPix pixel. |
| 73 |
|
| 74 |
if (lon < lon1) { // Go to the left |
| 75 |
if (longitude_index == 0) { |
| 76 |
ring2 = ring1 + n_in_ring - 1; |
| 77 |
} else { |
| 78 |
ring2 = ring1 - 1; |
| 79 |
} |
| 80 |
} else { // Go to the right |
| 81 |
if (longitude_index == n_in_ring - 1) { |
| 82 |
ring2 = ring1 - n_in_ring + 1; |
| 83 |
} else { |
| 84 |
ring2 = ring1 + 1; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
// Find the lon/lat of the new pixel |
| 89 |
xy_index = healpixl_ring_to_xy(ring2, Nside); |
| 90 |
healpixl_to_radec(xy_index, Nside, 0.5, 0.5, &lon2, &lat2); |
| 91 |
|
| 92 |
// Take into account possible wrapping so that the pixel longitude/latitude |
| 93 |
// are close to the requested longitude/latitude |
| 94 |
if (lon - lon2 > M_PI) |
| 95 |
lon2 += 2 * M_PI; |
| 96 |
if (lon2 - lon > M_PI) |
| 97 |
lon2 -= 2 * M_PI; |
| 98 |
|
| 99 |
// Now check whether we are moving up or down in terms of ring index |
| 100 |
|
| 101 |
if (lat > lat1) { // Move up (0 index is at the top) |
| 102 |
ring_number -= 1; |
| 103 |
} else { // Move down |
| 104 |
ring_number += 1; |
| 105 |
} |
| 106 |
|
| 107 |
if (ring_number > 0 && ring_number < 4 * Nside) { |
| 108 |
|
| 109 |
// Now figure out again how many pixels are in the ring |
| 110 |
|
| 111 |
if (ring_number < Nside) { |
| 112 |
n_in_ring = 4 * ring_number; |
| 113 |
} else if (ring_number < 3 * Nside) { |
| 114 |
n_in_ring = 4 * Nside; |
| 115 |
} else { |
| 116 |
n_in_ring = (int)(4 * (4 * (int64_t)Nside - (int64_t)ring_number)); |
| 117 |
} |
| 118 |
|
| 119 |
// Now determine the longitude index in which the requested longitude falls. |
| 120 |
|
| 121 |
// In all regions, the longitude elements are spaced by 360 / n_in_ring. For |
| 122 |
// convenience we convert the longitude index so that the spacing is 1. |
| 123 |
lon_frac = lon * n_in_ring / (2 * M_PI); |
| 124 |
|
| 125 |
// In the equatorial region, the first ring starts at 0.5 and the second at |
| 126 |
// 0 (in lon_frac space). The ring number is 1-based and the first ring in |
| 127 |
// the equatorial region is even. In this ring we can simply take |
| 128 |
// int(lon_frac) to get the longitude index but in the odd rings we need to |
| 129 |
// adjust lon_frac |
| 130 |
if (n_in_ring == 4 * Nside && ring_number % 2 == 1) { // Equatorial region |
| 131 |
lon_frac += 0.5; |
| 132 |
} |
| 133 |
|
| 134 |
// Find the longitude index of the closest pixel |
| 135 |
longitude_index = (int)lon_frac; |
| 136 |
if (longitude_index == n_in_ring) { |
| 137 |
longitude_index -= 1; |
| 138 |
} |
| 139 |
|
| 140 |
// Find the longitude/latitude and ring index of this pixel |
| 141 |
ring3 = healpixl_compose_ring(ring_number, longitude_index, Nside); |
| 142 |
xy_index = healpixl_ring_to_xy(ring3, Nside); |
| 143 |
healpixl_to_radec(xy_index, Nside, 0.5, 0.5, &lon3, &lat3); |
| 144 |
|
| 145 |
// Take into account possible wrapping so that the pixel longitude/latitude |
| 146 |
// are close to the requested longitude/latitude |
| 147 |
if (lon - lon3 > M_PI) |
| 148 |
lon3 += 2 * M_PI; |
| 149 |
if (lon3 - lon > M_PI) |
| 150 |
lon3 -= 2 * M_PI; |
| 151 |
|
| 152 |
// Finally we can find the fourth pixel as before |
| 153 |
|
| 154 |
if (lon < lon3) { // Go to the left |
| 155 |
if (longitude_index == 0) { |
| 156 |
ring4 = ring3 + n_in_ring - 1; |
| 157 |
} else { |
| 158 |
ring4 = ring3 - 1; |
| 159 |
} |
| 160 |
} else { // Go to the right |
| 161 |
if (longitude_index == n_in_ring - 1) { |
| 162 |
ring4 = ring3 - n_in_ring + 1; |
| 163 |
} else { |
| 164 |
ring4 = ring3 + 1; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
xy_index = healpixl_ring_to_xy(ring4, Nside); |
| 169 |
|
| 170 |
healpixl_to_radec(xy_index, Nside, 0.5, 0.5, &lon4, &lat4); |
| 171 |
|
| 172 |
// Take into account possible wrapping so that the pixel longitude/latitude |
| 173 |
// are close to the requested longitude/latitude |
| 174 |
if (lon - lon4 > M_PI) |
| 175 |
lon4 += 2 * M_PI; |
| 176 |
if (lon4 - lon > M_PI) |
| 177 |
lon4 -= 2 * M_PI; |
| 178 |
|
| 179 |
// Determine the interpolation weights |
| 180 |
|
| 181 |
xfrac1 = (lon - lon1) / (lon2 - lon1); |
| 182 |
xfrac2 = (lon - lon3) / (lon4 - lon3); |
| 183 |
yfrac = (lat - lat1) / (lat3 - lat1); |
| 184 |
|
| 185 |
weights[0] = (1 - xfrac1) * (1 - yfrac); |
| 186 |
weights[1] = xfrac1 * (1 - yfrac); |
| 187 |
weights[2] = (1 - xfrac2) * yfrac; |
| 188 |
weights[3] = xfrac2 * yfrac; |
| 189 |
|
| 190 |
} else { |
| 191 |
|
| 192 |
// In the case where we are inside the four top/bottom-most |
| 193 |
// values, we effectively place a value at the pole that |
| 194 |
// is the average of the four values, and the interpolation |
| 195 |
// is the weighted average of this polar value and the |
| 196 |
// value interpolated along the ring. |
| 197 |
|
| 198 |
xfrac1 = (lon - lon1) / (lon2 - lon1); |
| 199 |
yfrac = (lat - lat1) / (0.5 * M_PI - lat1); |
| 200 |
|
| 201 |
if (ring_number == 0) { |
| 202 |
ring3 = (ring1 + 2) % 4; |
| 203 |
ring4 = (ring2 + 2) % 4; |
| 204 |
yfrac = (lat - lat1) / (0.5 * M_PI - lat1); |
| 205 |
} else { |
| 206 |
npix = 12 * (int64_t)Nside * (int64_t)Nside; |
| 207 |
ring3 = ((ring1 - (npix - 4)) + 2) % 4 + npix - 4; |
| 208 |
ring4 = ((ring2 - (npix - 4)) + 2) % 4 + npix - 4; |
| 209 |
yfrac = (lat - lat1) / (-0.5 * M_PI - lat1); |
| 210 |
} |
| 211 |
|
| 212 |
weights[0] = (1 - xfrac1) * (1 - yfrac) + 0.25 * yfrac; |
| 213 |
weights[1] = xfrac1 * (1 - yfrac) + 0.25 * yfrac; |
| 214 |
weights[2] = 0.25 * yfrac; |
| 215 |
weights[3] = 0.25 * yfrac; |
| 216 |
} |
| 217 |
|
| 218 |
ring_indices[0] = ring1; |
| 219 |
ring_indices[1] = ring2; |
| 220 |
ring_indices[2] = ring3; |
| 221 |
ring_indices[3] = ring4; |
| 222 |
} |