1 /*
2  * Copyright (c) 2012-2021, The OSKAR Developers.
3  * See the LICENSE file at the top-level directory of this distribution.
4  */
5 
6 #include "telescope/private_telescope.h"
7 #include "telescope/oskar_telescope.h"
8 
9 #include <stdio.h>
10 
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14 
oskar_telescope_save_layout(const oskar_Telescope * telescope,const char * filename,int * status)15 void oskar_telescope_save_layout(const oskar_Telescope* telescope,
16         const char* filename, int* status)
17 {
18     int i = 0, type = 0, location = 0, num_stations = 0;
19     FILE* file = 0;
20     const oskar_Mem *x_weights = 0, *y_weights = 0, *z_weights = 0;
21     const oskar_Mem *x_signal = 0, *y_signal = 0, *z_signal = 0;
22 
23     /* Check if safe to proceed. */
24     if (*status) return;
25 
26     /* Check type and location. */
27     type = oskar_telescope_precision(telescope);
28     location = oskar_telescope_mem_location(telescope);
29     num_stations = oskar_telescope_num_stations(telescope);
30     if (type != OSKAR_SINGLE && type != OSKAR_DOUBLE)
31     {
32         *status = OSKAR_ERR_BAD_DATA_TYPE;
33         return;
34     }
35     if (location != OSKAR_CPU)
36     {
37         *status = OSKAR_ERR_BAD_LOCATION;
38         return;
39     }
40 
41     /* Get pointers to the arrays. */
42     x_weights = telescope->station_measured_enu_metres[0];
43     y_weights = telescope->station_measured_enu_metres[1];
44     z_weights = telescope->station_measured_enu_metres[2];
45     x_signal = telescope->station_true_enu_metres[0];
46     y_signal = telescope->station_true_enu_metres[1];
47     z_signal = telescope->station_true_enu_metres[2];
48 
49     /* Open the file. */
50     file = fopen(filename, "w");
51     if (!file)
52     {
53         *status = OSKAR_ERR_FILE_IO;
54         return;
55     }
56 
57     /* Save the position of each station. */
58     fprintf(file, "# Number of stations  = %i\n", num_stations);
59     fprintf(file, "# Longitude [radians] = %f\n", telescope->lon_rad);
60     fprintf(file, "# Latitude [radians]  = %f\n", telescope->lat_rad);
61     fprintf(file, "# Altitude [metres]   = %f\n", telescope->alt_metres);
62     fprintf(file, "# Local horizontal x (east), y (north), z (up) [metres], "
63             "delta x, delta y, delta z [metres]\n");
64     if (type == OSKAR_DOUBLE)
65     {
66         const double *x_ = 0, *y_ = 0, *z_ = 0, *xs_ = 0, *ys_ = 0, *zs_ = 0;
67         x_ = oskar_mem_double_const(x_weights, status);
68         y_ = oskar_mem_double_const(y_weights, status);
69         z_ = oskar_mem_double_const(z_weights, status);
70         xs_ = oskar_mem_double_const(x_signal, status);
71         ys_ = oskar_mem_double_const(y_signal, status);
72         zs_ = oskar_mem_double_const(z_signal, status);
73 
74         for (i = 0; i < num_stations; ++i)
75         {
76             double x = 0.0, y = 0.0, z = 0.0;
77             x = x_[i]; y = y_[i]; z = z_[i];
78             fprintf(file, "% 14.6f % 14.6f % 14.6f % 14.6f % 14.6f % 14.6f\n",
79                     x, y, z, (xs_[i] - x), (ys_[i] - y), (zs_[i] - z));
80         }
81     }
82     else if (type == OSKAR_SINGLE)
83     {
84         const float *x_ = 0, *y_ = 0, *z_ = 0, *xs_ = 0, *ys_ = 0, *zs_ = 0;
85         x_ = oskar_mem_float_const(x_weights, status);
86         y_ = oskar_mem_float_const(y_weights, status);
87         z_ = oskar_mem_float_const(z_weights, status);
88         xs_ = oskar_mem_float_const(x_signal, status);
89         ys_ = oskar_mem_float_const(y_signal, status);
90         zs_ = oskar_mem_float_const(z_signal, status);
91 
92         for (i = 0; i < num_stations; ++i)
93         {
94             float x = 0.0, y = 0.0, z = 0.0;
95             x = x_[i]; y = y_[i]; z = z_[i];
96             fprintf(file, "% 14.6f % 14.6f % 14.6f % 14.6f % 14.6f % 14.6f\n",
97                     x, y, z, (xs_[i] - x), (ys_[i] - y), (zs_[i] - z));
98         }
99     }
100 
101     /* Close the file. */
102     fclose(file);
103 }
104 
105 #ifdef __cplusplus
106 }
107 #endif
108