1 /*
2  * Copyright (c) 2015-2021, The OSKAR Developers.
3  * See the LICENSE file at the top-level directory of this distribution.
4  */
5 
6 #include "vis/private_vis_header.h"
7 #include "vis/oskar_vis_header.h"
8 #include <stdlib.h>
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
oskar_vis_header_create(int amp_type,int coord_precision,int max_times_per_block,int num_times_total,int max_channels_per_block,int num_channels_total,int num_stations,int write_autocorr,int write_crosscor,int * status)14 oskar_VisHeader* oskar_vis_header_create(int amp_type, int coord_precision,
15         int max_times_per_block, int num_times_total,
16         int max_channels_per_block, int num_channels_total, int num_stations,
17         int write_autocorr, int write_crosscor, int* status)
18 {
19     int i = 0, j = 0;
20     oskar_VisHeader* hdr = 0;
21     if (*status) return 0;
22 
23     /* Check type. */
24     if (!oskar_type_is_complex(amp_type))
25     {
26         *status = OSKAR_ERR_BAD_DATA_TYPE;
27         return 0;
28     }
29 
30     /* Allocate the structure. */
31     hdr = (oskar_VisHeader*) calloc(1, sizeof(oskar_VisHeader));
32     if (!hdr)
33     {
34         *status = OSKAR_ERR_MEMORY_ALLOC_FAILURE;
35         return 0;
36     }
37     hdr->amp_type = amp_type;
38     hdr->coord_precision = coord_precision;
39 
40     /* Set number of tags per block in the binary file. */
41     /* This must be updated if the number of fields written to file from
42      * the oskar_VisBlock structure is changed. */
43     hdr->num_tags_per_block = 1;
44     if (write_crosscor) hdr->num_tags_per_block += 4;
45     if (write_autocorr) hdr->num_tags_per_block += 1;
46 
47     /* Set dimensions. */
48     if (max_channels_per_block <= 0)
49     {
50         max_channels_per_block = num_channels_total;
51     }
52     if (max_times_per_block <= 0)
53     {
54         max_times_per_block = num_times_total;
55     }
56     hdr->max_times_per_block    = max_times_per_block;
57     hdr->num_times_total        = num_times_total;
58     hdr->max_channels_per_block = max_channels_per_block;
59     hdr->num_channels_total     = num_channels_total;
60     hdr->num_stations           = num_stations;
61     hdr->write_autocorr         = write_autocorr;
62     hdr->write_crosscorr        = write_crosscor;
63 
64     /* Set default polarisation type. */
65     if (oskar_type_is_matrix(amp_type))
66     {
67         oskar_vis_header_set_pol_type(hdr,
68                 OSKAR_VIS_POL_TYPE_LINEAR_XX_XY_YX_YY, status);
69     }
70     else
71     {
72         oskar_vis_header_set_pol_type(hdr,
73                 OSKAR_VIS_POL_TYPE_STOKES_I, status);
74     }
75 
76     /* Initialise arrays. */
77     hdr->telescope_path = oskar_mem_create(OSKAR_CHAR, OSKAR_CPU, 0, status);
78     hdr->settings = oskar_mem_create(OSKAR_CHAR, OSKAR_CPU, 0, status);
79     for (i = 0; i < 3; ++i)
80     {
81         hdr->station_offset_ecef_metres[i] = oskar_mem_create(coord_precision,
82                 OSKAR_CPU, num_stations, status);
83         hdr->element_enu_metres[i] = (oskar_Mem**) calloc(
84                 num_stations, sizeof(oskar_Mem*));
85         for (j = 0; j < num_stations; ++j)
86         {
87             hdr->element_enu_metres[i][j] = oskar_mem_create(coord_precision,
88                     OSKAR_CPU, 0, status);
89         }
90     }
91 
92     return hdr;
93 }
94 
95 #ifdef __cplusplus
96 }
97 #endif
98