1 #define _POSIX_C_SOURCE 200112L
12 void dumpgenjson(struct afrhex *map, int gen, int year);
13 void hexlistjson(struct afrhex *map);
15 static void setpop(struct afrhex *map, int hexid, int *pop) {
20 hex = find_hex(map, hexid);
21 for (i=0; i < 2; i++) {
22 hex->freq[i] = pop[i];
26 fprintf(stderr, "allele frequency for hex %d not even\n", hexid);
31 static void setpopstr(struct afrhex *map, char *spec) {
39 hex = strtol(spec, &resume, 10);
40 if (errno) goto error;
42 for (i = 0; i < 2; i++) {
43 if (*resume != ':' && *resume != ',') goto error;
44 start = resume+1; /* skip the colon or comma */
46 freq[i] = strtol(start, &resume, 10);
47 if (errno) goto error;
49 setpop(map, hex, freq);
53 fprintf(stderr, "unable to parse popspec '%s'\n", spec);
54 fprintf(stderr, "format is hex:freq,freq\n");
58 int main (int ac, char *av[]) {
66 unsigned int seed = 0;
72 double capfactor = 0.25;
75 map = map_init(maphexes, 100.0, capfactor);
77 clock_gettime(CLOCK_REALTIME, &ts);
78 seed = ((unsigned long)(ts.tv_sec) ^ (unsigned long)ts.tv_nsec) & 0xffffffff;
80 while ((opt = getopt(ac, av, "f:r:s:ehp:y:g:m:t:d:")) != -1) {
82 case 's': seed = atoi(optarg); break;
83 case 'r': run = atoi(optarg); break;
84 case 'y': year = atoi(optarg); break;
85 case 'g': gens = atoi(optarg); break;
86 case 'm': capfactor = strtod(optarg, 0); climate_init(100.0, capfactor); break;
87 case 't': strcpy(tag, optarg);
88 case 'e': eemian = 1; break;
89 case 'h': holocene = 1; break;
90 case 'p': setpopstr(map, optarg); defaultpop = 0;break;
91 case 'f': if (freopen(optarg, "w", stdout) == NULL) {
97 fprintf(stderr, "Usage: %s [-s seed] [-r run] [-y startyear] [-eh] [-p hex:freq,freq[:freq,freq...]\n",
107 hex = find_hex(map, 4788);
108 hex->freq[0] = hex->freq[1] = 2000;
109 hex->pop = hex->freq[0] + hex->freq[1];
113 r = var_rhyper(1, 5, 5);
114 printf("rhg = %u\n", r);
117 /* dump out the coordinates of the hexes */
119 printf("map init complete (%p)\n", map);
123 for (hex = map; hex; hex = hex->next) {
124 HL_vertices(hex->hex, hexvert);
125 printf("\t{ \"hex\": %lld, \"coords\": [", hex->hex);
126 for (i=0; i<5; i++) {
127 printf("%f,%f, ", hexvert[i*2], hexvert[i*2+1]);
129 printf("%f,%f", hexvert[5*2], hexvert[5*2+1]);
136 printf(" \"run\": %d, \"seed\": %d, \"gens\": %d", run, seed, gens);
137 printf(",\n\"options\": { \"capfactor\": %g }", capfactor);
138 printf(",\n\"gendata\": [\n");
140 dumpgenjson(map, 0, year);
142 for (gen = 1; gen <= gens; gen++) {
144 nextgen(map, year, holocene + eemian > 0);
146 dumpgenjson(map, gen, year);
150 printf(",\n\"hexlist\": ");
158 void dumpgenjson(struct afrhex *map, int gen, int year) {
161 unsigned int maxpop = 0;
163 printf("{ \"gen\": %d, \"year\": %d, \"pop\": %llu, \"hexes\": [\n",
164 gen, year, total_pop(map)
166 for (hex = map; hex; hex = hex->next) {
167 if (hex->pop == 0) continue;
168 if (hex->pop > maxpop) {
172 printf("%s\t{ \"hex\": %lld, \"pop\": %u, \"freq\": [%u, %u], "
173 "\"climate\": \"%s\", \"cap\": %u, \"outpop\": [%u, %u], "
174 "\"inpop\": [%u, %u]"
176 count > 1 ? ",\n" : "",
177 hex->hex, hex->pop, hex->freq[0], hex->freq[1],
178 hex->climate->code, hex->climate->cap,
179 hex->outmigrate[0], hex->outmigrate[1],
180 hex->inmigrate[0], hex->inmigrate[1]
183 printf(" ],\n\t\"maxpop\": %u}", maxpop);
186 void hexlistjson(struct afrhex *map) {
191 for (hex = map; hex; hex = hex->next) {
193 count > 0 ? "," : "",
194 count % 10 == 0 ? "\n" : "",