summaryrefslogtreecommitdiffstats
path: root/abs/core-testing/LinHES-timezone/create_map_include.c
blob: dbdbb52c458c1d09a516111485c0358015715f82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

int main ()
{
  char line_in[100];
  FILE *zones;
  FILE *boundary;
  char boundary_name[100];
  zones = fopen ("maps/zones.sorted", "r");
  int zone_num = 0;
 
  printf ("typedef struct {double x;double y;double z;} vect_type;\n");
  while (!feof (zones))
  {
    char coords[100];
    int points_in_vector = 0;
    if (fgets (line_in, sizeof (line_in), zones))
    {
    //Strip the newline off the end.
    line_in[strlen(line_in) - 1] = 0;
    
    sprintf (boundary_name, "maps/boundary%s", line_in);

//printf ("%s<\n", boundary_name);
    
    boundary = fopen (boundary_name, "r");
    
    if (!boundary)
    {
      fprintf (stderr, "%s ", boundary_name);
      perror ("fopen");
      exit (1);
    }

    printf ("vect_type boundary_%d[] = {\n", zone_num);

    while (!feof (boundary))
    {
      int num_scanned;
      int id,code,point;
      double lon,lat,x,y,z;    
      
      memset (coords, 0, sizeof (coords));
      fgets (coords, sizeof (coords), boundary);
//printf ("scanning %s\n", coords); 

      num_scanned = sscanf (coords, "%d,%d,%d,%lf,%lf", &id,&code,&point,&lon,&lat);

//printf ("num scanned%d\n", num_scanned);
      
      if (num_scanned <= 0)
      {
        // EOF coming.
      }
      else if (num_scanned == 5)
      {
      lon *= M_PI / 180.0;
      lat *= M_PI / 180.0;
      
      x = cos (lon) * cos (lat);
      y = sin (lon) * cos (lat);
      z = sin (lat);
      
        points_in_vector++;
        printf ("{%.5lf, %.5lf, %.5lf},\n", x, y, z);
      }
      else
      {
//printf ("points in = %d\n", points_in_vector);

        // If there was only a single point in the vector duplicate it.
        // This will allow ust to use xdrawlines to draw it.
        if (points_in_vector == 1)
        printf ("{%.5lf, %.5lf, %.5lf},\n", x, y, z);
        
        //Print an end vector delimiter.
        printf ("{-10.0,-10.0,-10.0},\n");
        points_in_vector = 0;
      }
    }
    printf ("};\n");
    
    printf ("#define num_boundary_points_%d XtNumber (boundary_%d)\n", zone_num, zone_num);
    fclose (boundary);
    zone_num++;
    }
  } 

  fclose (zones);


  printf ("typedef struct {\n"
          "  char *zonename;\n"
          "  vect_type v;\n"
          "} place_info_type;\n");

  zones = fopen ("maps/zones.sorted", "r");
  zone_num = 0;
 
  while (!feof (zones))
  {
    FILE *places;
    char place_name[100];
    
    if (fgets (line_in, sizeof (line_in), zones))
    {
      char place_info[100];
      double lat,lon;
      char zonename[100];

      //Strip the newline off the end.
      line_in[strlen(line_in) - 1] = 0;
    
      printf ("#define zone_offset_%d \"%s%s\"\n", zone_num, (line_in[0] == '-') ? "" : "+", line_in);

      sprintf (place_name, "maps/places%s", line_in);

//printf ("%s<\n", place_name);
    
      places = fopen (place_name, "r");

      if (!places)
      {
        fprintf (stderr, "%s ", place_name);
        perror ("fopen");
        exit (1);
      }    

      printf ("place_info_type places_%d[] =\n{\n", zone_num);

      memset (place_info, 0, sizeof (place_info));

      while (fgets (place_info, sizeof (place_info), places))
      {
//printf (place_info);

        if (sscanf (place_info, "%lf %lf %s", &lat, &lon, zonename) == 3)
        {
          lat *= M_PI / 180.0;
          lon *= M_PI / 180.0;

          printf ("  {\"%s\", {%.5lf, %.5lf, %.5lf}},\n", zonename, 
                  
            cos (lon) * cos (lat),
            sin (lon) * cos (lat),
            sin (lat)
                  );
        }
      }
      printf ("};\n");

      printf ("#define num_places_%d XtNumber (places_%d)\n", zone_num, zone_num);
      zone_num++;
    } 
  }
  
  printf ("#define NUM_ZONES %d\n", zone_num);

  printf ("struct {\n"
          "  char *offset;\n"
          "  int num_boundary_points;\n"
          "  vect_type *boundary_points;\n"
          "  int num_places;\n"
          "  place_info_type *place_info;\n"
          "} zone_data[] =\n"
          "{\n");

  for (int zone = 0; zone < zone_num; zone++)
  {
    printf ("  {\n"
            "    zone_offset_%d,\n"
            "    num_boundary_points_%d,\n"
            "    boundary_%d,\n"
            "    num_places_%d,\n"
            "    places_%d\n"
            "  },\n", zone, zone, zone, zone, zone);
  }

  printf ("};\n");

  exit (0);
}