1 /*
2  * Copyright (c) 2013-2015, The University of Oxford
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * 1. Redistributions of source code must retain the above copyright notice,
8  *    this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  *    this list of conditions and the following disclaimer in the documentation
11  *    and/or other materials provided with the distribution.
12  * 3. Neither the name of the University of Oxford nor the names of its
13  *    contributors may be used to endorse or promote products derived from this
14  *    software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <gtest/gtest.h>
30 
31 #include "convert/oskar_convert_healpix_ring_to_theta_phi.h"
32 #include "convert/oskar_convert_galactic_to_fk5.h"
33 #include "math/oskar_evaluate_image_lon_lat_grid.h"
34 
35 #include "binary/oskar_binary.h"
36 #include "mem/oskar_binary_write_mem.h"
37 #include "utility/oskar_timer.h"
38 
39 #include "math/oskar_cmath.h"
40 #include <cstdio>
41 #include <iostream>
42 
TEST(beam_pattern_coordinates,generate_lon_lat_grid)43 TEST(beam_pattern_coordinates, generate_lon_lat_grid)
44 {
45     int status = 0;
46 
47     double lon0 = 0.0 * (M_PI/180.0);
48     double lat0 = 90.0 * (M_PI/180.0);
49     int image_size = 1024;
50     double fov = 180.0 * (M_PI/180.0);
51 
52     int num_pixels = image_size * image_size;
53     int type = OSKAR_DOUBLE;
54     int loc = OSKAR_CPU;
55 
56     oskar_Mem* lon = oskar_mem_create(type, loc, num_pixels, &status);
57     oskar_Mem* lat = oskar_mem_create(type, loc, num_pixels, &status);
58     ASSERT_EQ(0, status);
59 
60     // ##### Generates a grid of pixels centred on ra0, dec0 ##################
61     oskar_Timer* timer = oskar_timer_create(OSKAR_TIMER_NATIVE);
62     oskar_timer_start(timer);
63     oskar_evaluate_image_lon_lat_grid(image_size, image_size,
64             fov, fov, lon0, lat0, lon, lat, &status);
65     ASSERT_EQ(0, status);
66     std::cout << "Grid generation: " << oskar_timer_elapsed(timer)/1000.0
67             << " ms" << std::endl;
68     oskar_timer_free(timer);
69     // ########################################################################
70 
71     // Write to OSKAR binary file (for manual inspection in MATLAB).
72     const char* filename = "coords1.dat";
73     const char* group = "coords";
74     oskar_Binary* h = oskar_binary_create(filename, 'w', &status);
75     oskar_binary_write_mem_ext(h, lon, group, "RA", 0, num_pixels, &status);
76     ASSERT_EQ(0, status);
77     oskar_binary_write_mem_ext(h, lat, group, "Dec", 0, num_pixels, &status);
78     ASSERT_EQ(0, status);
79     oskar_binary_free(h);
80 
81     // Clean up.
82     oskar_mem_free(lon, &status);
83     oskar_mem_free(lat, &status);
84     ASSERT_EQ(0, status);
85 }
86 
87 
TEST(beam_pattern_coordinates,HEALPix_horizontal)88 TEST(beam_pattern_coordinates, HEALPix_horizontal)
89 {
90     int status = 0;
91 
92     int nside = 12;
93     int num_pixels = 12 * nside * nside;
94     int loc = OSKAR_CPU;
95     int type = OSKAR_DOUBLE;
96 
97     // galactic longitude = l, latitude = b
98     oskar_Mem* b = oskar_mem_create(type, loc, num_pixels, &status);
99     oskar_Mem* l = oskar_mem_create(type, loc, num_pixels, &status);
100     oskar_Mem* RA = oskar_mem_create(type, loc, num_pixels, &status);
101     oskar_Mem* Dec = oskar_mem_create(type, loc, num_pixels, &status);
102     double* b_ = oskar_mem_double(b, &status);
103     double* l_ = oskar_mem_double(l, &status);
104     double* RA_ = oskar_mem_double(RA, &status);
105     double* Dec_ = oskar_mem_double(Dec, &status);
106 
107     for (int i = 0; i < num_pixels; ++i)
108     {
109         oskar_convert_healpix_ring_to_theta_phi_d(nside, i, &b_[i], &l_[i]);
110         b_[i] = (M_PI / 2.0) - b_[i]; /* Co-latitude to latitude. */
111         // Convert from galactic lon, lat to J2000 RA,Dec
112         oskar_convert_galactic_to_fk5_d(1, &l_[i], &b_[i], &RA_[i], &Dec_[i]);
113     }
114 
115     const char* filename = "test_healpix_coords.dat";
116     oskar_Binary* h = oskar_binary_create(filename, 'w', &status);
117     oskar_binary_write_mem_ext(h, l, "healpix", "phi", 0, num_pixels, &status);
118     oskar_binary_write_mem_ext(h, b, "healpix", "theta", 0, num_pixels, &status);
119     oskar_binary_write_mem_ext(h, RA, "healpix", "RA", 0, num_pixels, &status);
120     oskar_binary_write_mem_ext(h, Dec, "healpix", "Dec", 0, num_pixels, &status);
121     oskar_binary_free(h);
122 
123     oskar_mem_free(b, &status);
124     oskar_mem_free(l, &status);
125     oskar_mem_free(RA, &status);
126     oskar_mem_free(Dec, &status);
127     ASSERT_EQ(0, status);
128 }
129