forked from Marxan-source-code/marxan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.c
1154 lines (998 loc) · 43.3 KB
/
input.c
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// functions that read from input files
// read the planning units file pu.dat
int readPlanningUnits(int *puno,struct spustuff *pu[],struct sfname fnames)
{
FILE *fp;
struct pustruct{int id;double cost; int status; double xloc; double yloc; double prob;};
char *readname;
char sLine[600];
char *varlist[6] = {"id","cost","status","xloc","yloc","prob"};
int numvars = 6,ivars,i=0,j;
char *sVarName,*sVarVal;
struct snlink *head = NULL, *temp = NULL;
struct spustuff putemp;
struct spulink{struct spustuff stemp;struct spulink *next;} *spuhead = NULL, *newspulink;
readname = (char *) calloc(strlen(fnames.puname) + strlen(fnames.inputdir)+2, sizeof(char));
#ifdef MEMDEBUG
iMemoryUsed += (strlen(fnames.puname) + strlen(fnames.inputdir)+2) * sizeof(char);
displayProgress1("memory used %i\n",iMemoryUsed);
#endif
strcpy(readname,fnames.inputdir);
strcat(readname,fnames.puname);
if((fp = fopen(readname,"r"))==NULL)
displayErrorMessage("Planning Unit file %s has not been found.\nAborting Program.",readname);
free(readname);
// Scan header
if (fgets(sLine,500-1,fp) == NULL)
displayErrorMessage("Error reading planning units.\n");
sVarName = strtok(sLine," ,;:^*\"/\t\'\\\n");
head = storeFieldName(varlist,numvars,sVarName,head,fnames.puname);
ivars = 1;
temp = head;
while ((sVarName = strtok(NULL," ,;:^*\"/\t\'\\\n")) != NULL)
{
ivars++;
temp->next = storeFieldName(varlist,numvars,sVarName,head,fnames.puname);
temp = temp->next;
} // tokking out all the variable names from the header line. There are numVars of them.
/* While there are still lines left feed information into temporary link list */
while (fgets(sLine,500-1,fp))
{
i++;
putemp.id = -1; /* Set defaults for any missing values */
putemp.cost = 1;
putemp.status = 0;
putemp.xloc = -1;
putemp.yloc = -1;
putemp.prob = 0;
for (temp = head;temp;temp = temp->next)
{
if (temp == head)
sVarVal = strtok(sLine," ,;:^*\"/\t\'\\\n");
else
sVarVal = strtok(NULL," ,;:^*\"/\t\'\\\n");
if (strcmp("id",temp->name)==0)
{
sscanf(sVarVal,"%d",&putemp.id);
}
else
if (strcmp("status",temp->name)==0)
{
sscanf(sVarVal,"%d",&putemp.status);
}
else
if (strcmp("xloc",temp->name)==0)
{
sscanf(sVarVal,"%lf",&putemp.xloc);
}
else
if (strcmp("yloc",temp->name)==0)
{
sscanf(sVarVal,"%lf",&putemp.yloc);
}
else
if (strcmp("cost",temp->name)==0)
{
sscanf(sVarVal,"%lf",&putemp.cost);
}
else
if (strcmp("prob",temp->name)==0)
{
sscanf(sVarVal,"%lf",&putemp.prob);
iProbFieldPresent = 1;
}
} /* looking for ivar different input variables */
if (putemp.id == -1)
displayErrorMessage("ERROR: Missing planning unit id for line %d. \n",i);
/* Stick everything from putemp into link list */
newspulink = (struct spulink *) malloc(sizeof(struct spulink));
newspulink->stemp.id = putemp.id;
newspulink->stemp.status = putemp.status;
newspulink->stemp.cost = putemp.cost;
newspulink->stemp.xloc = putemp.xloc;
newspulink->stemp.yloc = putemp.yloc;
newspulink->stemp.prob = putemp.prob;
newspulink->next = spuhead;
spuhead = newspulink;
} /* while still lines in data file */
fclose(fp);
/* Create array to store the information */
*puno = i;
*pu = (struct spustuff *) calloc(*puno,sizeof(struct spustuff));
#ifdef MEMDEBUG
iMemoryUsed += (*puno) * sizeof(struct spustuff);
displayProgress1("memory used %i\n",iMemoryUsed);
#endif
for (i=*puno-1;i>=0;i--)
{
(* pu)[i].id = spuhead->stemp.id;
(* pu)[i].cost = spuhead->stemp.cost;
(* pu)[i].status = spuhead->stemp.status;
(* pu)[i].xloc = spuhead->stemp.xloc;
(* pu)[i].yloc = spuhead->stemp.yloc;
(* pu)[i].prob = spuhead->stemp.prob;
(* pu)[i].richness = 0;
(* pu)[i].offset = 0;
(* pu)[i].probrichness = 0;
(* pu)[i].proboffset = 0;
newspulink = spuhead;
spuhead = spuhead->next;
free(newspulink);
}
if (iProbFieldPresent == 1)
{
fProb1D = 1;
}
return(*puno);
} // readPlanningUnits
// read species file: spec.dat
int readSpecies(int *spno,struct sspecies *spec[],struct sfname fnames)
{
FILE *fp;
int n=0;
struct snlink *snhead= NULL,*temp;
struct slink{struct sspecies stemp;struct slink *next;} *head = NULL,*newlink;
struct sspecies spectemp;
char *readname;
char speciesname[255];
char sLine[500];
char *varlist[12] = {"id","type","target","spf",
"target2","sepdistance","sepnum","name",
"targetocc","prop","ptarget1d","ptarget2d"};
int numvars = 12,ivars;
char *sVarName,*sVarVal;
readname = (char *) calloc(strlen(fnames.specname) + strlen(fnames.inputdir)+2, sizeof(char));
strcpy(readname,fnames.inputdir);
strcat(readname,fnames.specname);
if((fp = fopen(readname,"r"))==NULL)
displayErrorMessage("Species file %s has not been found.\nAborting Program.",readname);
free(readname);
// Scan header
if (fgets(sLine,500-1,fp) == NULL)
displayErrorMessage("Error reading species.\n");
sVarName = strtok(sLine," ,;:^*\"/\t\'\\\n");
snhead = storeFieldName(varlist,numvars,sVarName,snhead,fnames.specname);
ivars = 1;
temp = snhead;
while ((sVarName = strtok(NULL," ,;:^*\"/\t\'\\\n")) != NULL) {
ivars++;
temp->next = storeFieldName(varlist,numvars,sVarName,snhead,fnames.specname);
temp = temp->next;
} // tokking out all the variable names from the header line. There are numVars of them
// While there are still lines left feed information into temporary link list
while (fgets(sLine,500-1,fp))
{
n++;
// Clear important species stats
spectemp.name = -1;
spectemp.target = -1;
spectemp.type = -1;
spectemp.spf = -1;
spectemp.target2 = -1;
spectemp.targetocc = -1;
spectemp.sepdistance = -1;
spectemp.sepnum = -1;
spectemp.prop = -1;
spectemp.ptarget1d = -1;
spectemp.ptarget2d = -1;
spectemp.rUserPenalty = -1;
speciesname[0] = '\0';
for (temp = snhead;temp;temp = temp->next)
{
if (temp == snhead)
sVarVal = strtok(sLine," ,;:^*\"/\t\'\\\n");
else
sVarVal = strtok(NULL," ,;:^*\"/\t\'\\\n");
if (strcmp("id",temp->name)==0)
{
sscanf(sVarVal,"%d",&spectemp.name);
}
else
if (strcmp("type",temp->name)==0)
{
sscanf(sVarVal,"%d",&spectemp.type);
}
else
if (strcmp("target",temp->name)==0)
{
sscanf(sVarVal,"%lf",&spectemp.target);
}
else
if (strcmp("prop",temp->name)==0)
{
sscanf(sVarVal,"%lf",&spectemp.prop);
if (spectemp.prop > 0)
fSpecPROPLoaded = 1;
}
else
if (strcmp("spf",temp->name)==0)
{
sscanf(sVarVal,"%lf",&spectemp.spf);
}
else
if (strcmp("sepdistance",temp->name)==0)
{
sscanf(sVarVal,"%lf",&spectemp.sepdistance);
}
else
if (strcmp("sepnum",temp->name)==0)
{
sscanf(sVarVal,"%d",&spectemp.sepnum);
}
else
if (strcmp("target2",temp->name)==0)
{
sscanf(sVarVal,"%lf",&spectemp.target2);
}
else
if (strcmp("targetocc",temp->name)==0)
{
sscanf(sVarVal,"%d",&spectemp.targetocc);
}
else
if (strcmp("ptarget1d",temp->name)==0)
{
sscanf(sVarVal,"%lf",&spectemp.ptarget1d);
}
else
if (strcmp("ptarget2d",temp->name)==0)
{
sscanf(sVarVal,"%lf",&spectemp.ptarget2d);
}
} // looking for ivar different input variables
newlink = (struct slink *) malloc(sizeof(struct slink));
newlink->stemp.name = spectemp.name;
newlink->stemp.target = spectemp.target;
newlink->stemp.prop = spectemp.prop;
newlink->stemp.spf = spectemp.spf;
newlink->stemp.type = spectemp.type;
newlink->stemp.targetocc = spectemp.targetocc;
newlink->stemp.rUserPenalty = spectemp.rUserPenalty;
newlink->stemp.target2 = spectemp.target2;
newlink->stemp.sepdistance = spectemp.sepdistance;
newlink->stemp.sepnum = spectemp.sepnum;
newlink->stemp.sname = (char *) calloc(strlen(speciesname)+1,sizeof(char));
strcpy(newlink->stemp.sname,speciesname);
newlink->stemp.ptarget1d = spectemp.ptarget1d;
newlink->stemp.ptarget2d = spectemp.ptarget2d;
newlink->next = head;
head = newlink;
} // Scanning through each line of file
fclose(fp);
// Now do as Name.dat in forming species array
*spno = n;
*spec = (struct sspecies *) calloc(*spno,sizeof(struct sspecies));
// put each link into namelist and free it
n = 0;
//n = *spno-1;
while (head)
{
(* spec)[n].name = head->stemp.name;
(* spec)[n].type = head->stemp.type;
(* spec)[n].target = head->stemp.target;
(* spec)[n].prop = head->stemp.prop;
(* spec)[n].spf = head->stemp.spf;
(* spec)[n].target2 = head->stemp.target2;
(* spec)[n].targetocc = head->stemp.targetocc;
(* spec)[n].sepdistance = head->stemp.sepdistance;
(* spec)[n].rUserPenalty = head->stemp.rUserPenalty;
(* spec)[n].sepnum = head->stemp.sepnum;
(* spec)[n].richness = 0;
(* spec)[n].probability1D = 0;
(* spec)[n].probability2D = 0;
(* spec)[n].Zscore1D = 0;
(* spec)[n].Zscore2D = 0;
(* spec)[n].ptarget1d = head->stemp.ptarget1d;
(* spec)[n].ptarget2d = head->stemp.ptarget2d;
(* spec)[n].sname = (char *) calloc(strlen(head->stemp.sname)+1,sizeof(char));
strcpy((* spec)[n].sname,head->stemp.sname);
n++;
newlink = head;
head = head->next;
free(newlink->stemp.sname);
free(newlink);
}
return(n);
} // readSpecies
// read species block definition file
// allows attribute to be applied to species based upon a type
int readSpeciesBlockDefinition(int *gspno,struct sgenspec *gspec[],struct sfname fnames)
{
FILE *fp;
char *readname;
char sLine[500];
char *varlist[8] = {"type","target","target2","targetocc",
"sepnum","sepdistance","prop","spf"};
int numvars = 8,ivars,i=0;
char *sVarName,*sVarVal;
struct snlink *head = NULL, *temp = NULL;
struct sgenspec gstemp;
struct sgslink{struct sgenspec stemp;struct sgslink *next;} *gshead = NULL, *newgslink;
/* Find and Open File */
readname = (char *) calloc(strlen(fnames.blockdefname) + strlen(fnames.inputdir)+2, sizeof(char));
strcpy(readname,fnames.inputdir);
strcat(readname,fnames.blockdefname);
if((fp = fopen(readname,"r"))==NULL) {
displayWarningMessage("Warning: Block Definition File %s not found.\n",fnames.blockdefname);
free(readname);
return(0);
}
free(readname);
/* Scan header */
if (fgets(sLine,500-1,fp) == NULL)
displayErrorMessage("Error reading block definition.\n");
sVarName = strtok(sLine," ,;:^*\"/\t\'\\\n");
head = storeFieldName(varlist,numvars,sVarName,head,fnames.blockdefname);
ivars = 1;
temp = head;
while ((sVarName = strtok(NULL," ,;:^*\"/\t\'\\\n")) != NULL) {
ivars++;
temp->next = storeFieldName(varlist,numvars,sVarName,head,fnames.blockdefname);
temp = temp->next;
} /* tokking out all the variable names from the header line. There are numVars of them*/
/* While there are still lines left feed information into temporary link list */
while (fgets(sLine,500-1,fp)) {
i++;
gstemp.type = -1; /* Set defaults for any missing values */
gstemp.targetocc = -1;
gstemp.target = -1;
gstemp.target2 = -1;
gstemp.sepnum = -1;
gstemp.sepdistance = -1;
gstemp.prop = -1;
for (temp = head;temp->next;temp = temp->next)
{
if (temp == head)
sVarVal = strtok(sLine," ,;:^*\"/\t\'\\\n");
else
sVarVal = strtok(NULL," ,;:^*\"/\t\'\\\n");
if (strcmp("type",temp->name)==0) {
sscanf(sVarVal,"%d",&gstemp.type);
}
else if (strcmp("targetocc",temp->name)==0){
sscanf(sVarVal,"%d",&gstemp.targetocc);
}
else if (strcmp("target",temp->name)==0){
sscanf(sVarVal,"%lf",&gstemp.target);
}
else if (strcmp("target2",temp->name)==0){
sscanf(sVarVal,"%lf",&gstemp.target2);
}
else if (strcmp("sepnum",temp->name)==0){
sscanf(sVarVal,"%d",&gstemp.sepnum);
}
else if (strcmp("sepdistance",temp->name)==0){
sscanf(sVarVal,"%lf",&gstemp.sepdistance);
}
else if (strcmp("prop",temp->name)==0){
sscanf(sVarVal,"%lf",&gstemp.prop);
}
else if (strcmp("spf",temp->name)==0){
sscanf(sVarVal,"%lf",&gstemp.spf);
}
else
{
displayWarningMessage("Cannot find >%s< \n",temp->name);
displayErrorMessage("Serious error in GenSpecies data reading function.\n");
}
} /* looking for ivar different input variables */
if (gstemp.type== -1)
displayErrorMessage("ERROR: Missing Gen Species type for line %d. \n",i);
/* Stick everything from gstemp into link list */
newgslink = (struct sgslink *) malloc(sizeof(struct sgslink));
newgslink->stemp.type = gstemp.type;
newgslink->stemp.targetocc = gstemp.targetocc;
newgslink->stemp.target = gstemp.target;
newgslink->stemp.target2 = gstemp.target2;
newgslink->stemp.sepnum = gstemp.sepnum;
newgslink->stemp.sepdistance = gstemp.sepdistance;
newgslink->stemp.prop = gstemp.prop;
newgslink->next = gshead;
gshead = newgslink;
} /* while still lines in data file */
fclose(fp);
/* Now do as Name.dat in forming species array */
*gspno = i;
*gspec = (struct sgenspec *) calloc(*gspno,sizeof(struct sgenspec));
/* put each link into namelist and free it */
i = 0;
while (gshead) {
(* gspec)[i].type = gshead->stemp.type;
(* gspec)[i].targetocc = gshead->stemp.targetocc;
(* gspec)[i].target = gshead->stemp.target;
(* gspec)[i].target2 = gshead->stemp.target2;
(* gspec)[i].sepnum = gshead->stemp.sepnum;
(* gspec)[i].sepdistance = gshead->stemp.sepdistance;
(* gspec)[i].prop = gshead->stemp.prop;
i++;
newgslink = gshead;
gshead = gshead->next;
free(newgslink);
}
return(i);
} // readSpeciesBlockDefinition
// read connections file: read bound.dat (boundaries) or connections.dat (connections)
// boundaries are a subset of asymmetric connections
int readConnections(int puno,struct sconnections connections[],struct spustuff pu[],
struct binsearch PULookup[],struct sfname fnames)
{
FILE *fp;
int id1,id2;
double fcost;
int icount = 0,idup = 0;
int bad;
struct sneighbour *p;
char *readname;
int numvars = 3,ivars;
char *sVarName,*sVarVal;
char sLine[500];
readname = (char *) calloc(strlen(fnames.connectionname) + strlen(fnames.inputdir)+2, sizeof(char));
strcpy(readname,fnames.inputdir);
strcat(readname,fnames.connectionname);
fp = fopen(readname,"r");
if (fp==NULL)
{
displayProgress1("Warning: Connection File %s not found ",fnames.connectionname);
free(readname);
return(0);
}
free(readname);
if (fgets(sLine,500-1,fp) == NULL)
displayErrorMessage("Error reading connections.\n");
while (fgets(sLine,500-1,fp))
{
icount++;
sVarVal = strtok(sLine," ,;:^*\"/\t\'\\\n");
sscanf(sVarVal,"%d",&id1);
sVarVal = strtok(NULL," ,;:^*\"/\t\'\\\n");
sscanf(sVarVal,"%d",&id2);
sVarVal = strtok(NULL," ,;:^*\"/\t\'\\\n");
sscanf(sVarVal,"%lf",&fcost);
id1 = binarySearchPuIndex(puno,id1,PULookup);
id2 = binarySearchPuIndex(puno,id2,PULookup);
if (id1==id2)
{ // irremovable connection
if (asymmetricconnectivity)
connections[id1].fixedcost = 0;
else
connections[id1].fixedcost += fcost;
continue;
}
if (id1>=0 && id1<puno)
{ // Is I a sensible number ?
p = connections[id1].first;
bad = 0;
while (p)
{
if (p->nbr == id2)
bad = 1;
p = p->next;
}
if (asymmetricconnectivity)
bad = 0;
if (bad)
displayProgress3("Double connection definition %i %i \n",id1,id2);
else
{
connections[id1].nbrno++;
p = (struct sneighbour *) malloc (sizeof(struct sneighbour));
p->cost = fcost;
p->nbr = id2;
p->next = connections[id1].first;
if (asymmetricconnectivity)
{
p->connectionorigon = 1;
}
else
p->connectionorigon = 1;
connections[id1].first = p;
}
}
else
displayErrorMessage("A connection is out of range %f %i %i \n",fcost,id1,id2);
if (id2>=0 && id2<puno)
{ /* Is I a sensible number ?*/
p = connections[id2].first;
bad = 0;
while (p)
{
if (p->nbr == id1)
bad = 1;
p = p->next;
}
if (asymmetricconnectivity)
bad = 0;
if (bad && verbosity > 4)
displayProgress3("Double connection definition %i %i \n",id1,id2);
if (bad)
idup++;
else
{
connections[id2].nbrno++;
p = (struct sneighbour *) malloc (sizeof(struct sneighbour));
#ifdef MEMDEBUG
iMemoryUsed += sizeof(struct sneighbour);
#endif
p->cost = fcost;
p->nbr = id1;
p->next = connections[id2].first;
p->connectionorigon = 1;
if (asymmetricconnectivity)
p->connectionorigon = 0;
else
p->connectionorigon = 1;
connections[id2].first = p;
}
}
else displayErrorMessage("A connection is out of range %f %i %i \n",fcost,id1,id2);
}
fclose(fp);
if (idup)
displayProgress1("There were %i duplicate connection definitions.\n",idup);
return(icount);
} // readConnections
// read the planning unit versus species sparse matrix: ordered by planning units
void readSparseMatrix(int *iSMSize, struct spu *SM[], int puno, int spno, struct spustuff PU[],
struct binsearch PULookup[],struct binsearch SPLookup[],
struct sfname fnames)
{
FILE *fp;
char *readname,sLine[500],*sVarName,*sVarVal;
int i, _spid, _puid, iInternalSMSize = 0, iBigMatrixSize, iLength;
double amount, rDensity, rInternalSMSize, rBigMatrixSize, rProbability = 1;
int iP, iR, iO, iB, iLastPUID;
char cP, cR, cO, cB;
readname = (char *) calloc(strlen(fnames.puvsprname) + strlen(fnames.inputdir)+2, sizeof(char));
strcpy(readname,fnames.inputdir);
strcat(readname,fnames.puvsprname);
if((fp = fopen(readname,"r"))==NULL)
displayErrorMessage("PU v Species file %s not found\nAborting Program.",readname);
free(readname);
// read through the file first to see how many lines
if (fgets(sLine,500-1,fp) == NULL)
displayErrorMessage("Error reading sparse matrix.\n");
while (fgets(sLine,500-1,fp))
iInternalSMSize++;
rewind(fp);
if (fgets(sLine,500-1,fp) == NULL)
displayErrorMessage("Error reading sparse matrix.\n");
// scan the first line to see if the prob field is tagged on the end
// 3 = regular marxan matrix
// 4 = prob2d marxan matrix
cP = 'p';
cR = 'r';
cO = 'o';
cB = 'b';
iP = cP;
iR = cR;
iO = cO;
iB = cB;
iLength = strlen(sLine);
if ((sLine[iLength-5] == iP) && (sLine[iLength-4] == iR) && (sLine[iLength-3] == iO) && (sLine[iLength-2] == iB))
fProb2D = 1;
*iSMSize = iInternalSMSize;
// create the sparse matrix
*SM = (struct spu *) calloc(iInternalSMSize,sizeof(struct spu));
iLastPUID = -1;
// init with zero values
for (i=0;i<iInternalSMSize;i++)
{
if (fgets(sLine,500-1,fp) == NULL)
displayErrorMessage("Error reading sparse matrix.\n");
sVarVal = strtok(sLine," ,;:^*\"/\t\'\\\n");
sscanf(sVarVal,"%d",&_spid);
sVarVal = strtok(NULL," ,;:^*\"/\t\'\\\n");
sscanf(sVarVal,"%d",&_puid);
sVarVal = strtok(NULL," ,;:^*\"/\t\'\\\n");
sscanf(sVarVal,"%lf",&amount);
if (fProb2D == 1)
{
sVarVal = strtok(NULL," ,;:^*\"/\t\'\\\n");
sscanf(sVarVal,"%lf",&rProbability);
}
if (_puid < iLastPUID)
{
// error condition exists, file is not in ascending order for PUID
appendTraceFile("Error: PU v Species file %s is not in ascending order for PUID at record %i.\nAborting Program.",fnames.puvsprname,i+1);
displayErrorMessage("Error: PU v Species file %s is not in ascending order for PUID at record %i.\nAborting Program.",fnames.puvsprname,i+1);
}
iLastPUID = _puid;
_puid = binarySearchPuIndex(puno,_puid,PULookup);
_spid = binarySearchSpecIndex(spno,_spid,SPLookup);
/* increment richness for planning unit containing this feature */
PU[_puid].richness += 1;
/* if planning units richness is one, set its offset */
if (PU[_puid].richness == 1)
PU[_puid].offset = i;
(* SM)[i].prob = rProbability;
(* SM)[i].amount = amount;
(* SM)[i].clump = 0;
(* SM)[i].spindex = _spid;
}
fclose(fp);
iBigMatrixSize = puno * spno;
rInternalSMSize = iInternalSMSize;
rBigMatrixSize = iBigMatrixSize;
rDensity = rInternalSMSize / rBigMatrixSize * 100;
displayProgress1("%i conservation values counted, %i big matrix size, %g%% density of matrix \n",
iInternalSMSize,iBigMatrixSize,rDensity);
} // readSparseMatrix
void readPenalties(typesp spec[],int spno,struct sfname fnames,struct binsearch SPLookup[])
{
FILE *fp;
char *readname,*sVarVal,sLine[500];
int i,iSPID;
double rPenalty;
readname = (char *) calloc(strlen(fnames.penaltyname) + strlen(fnames.inputdir)+2, sizeof(char));
strcpy(readname,fnames.inputdir);
strcat(readname,fnames.penaltyname);
if((fp = fopen(readname,"r"))==NULL)
displayErrorMessage("Penalty file %s not found\nAborting Program.",readname);
free(readname);
for (i=0;i<spno;i++)
spec[i].rUserPenalty = 0;
if (fgets(sLine,500-1,fp) == NULL)
displayErrorMessage("Error reading penalties.\n");
while (fgets(sLine,500-1,fp))
{
sVarVal = strtok(sLine," ,;:^*\"/\t\'\\\n");
sscanf(sVarVal,"%d",&iSPID);
sVarVal = strtok(NULL," ,;:^*\"/\t\'\\\n");
sscanf(sVarVal,"%lf",&rPenalty);
i = binarySearchSpecIndex(spno,iSPID,SPLookup);
spec[i].rUserPenalty = rPenalty;
appendTraceFile("readPenalties spname %i user penalty %g\n",spec[i].name,rPenalty);
}
fclose(fp);
}
// read the planning unit versus species sparse matrix: ordered by species
void readSparseMatrixSpOrder(int *iSMSize, struct spusporder *SM[], int puno, int spno,
struct binsearch PULookup[],struct binsearch SPLookup[],
struct sfname fnames)
{
FILE *fp;
char *readname,sLine[500],*sVarName,*sVarVal;
int i, _spid,spid, _puid, iInternalSMSize = 0, iBigMatrixSize, iLastSPID;
double amount, rDensity, rInternalSMSize, rBigMatrixSize;
readname = (char *) calloc(strlen(fnames.matrixspordername) + strlen(fnames.inputdir)+2, sizeof(char));
strcpy(readname,fnames.inputdir);
strcat(readname,fnames.matrixspordername);
if((fp = fopen(readname,"r"))==NULL)
displayErrorMessage("PU v Species file %s not found\nAborting Program.",readname);
free(readname);
// read through the file first to see how many lines
if (fgets(sLine,500-1,fp) == NULL)
displayErrorMessage("Error reading sparse matrix sporder.\n");
while (fgets(sLine,500-1,fp))
iInternalSMSize++;
rewind(fp);
if (fgets(sLine,500-1,fp) == NULL)
displayErrorMessage("Error reading sparse matrix sporder.\n");
*iSMSize = iInternalSMSize;
// create the sparse matrix
*SM = (struct spusporder *) calloc(iInternalSMSize,sizeof(struct spusporder));
iLastSPID = -1;
// planning unit richness and offset are already set to zero
// init with zero values
for (i=0;i<iInternalSMSize;i++)
{
if (fgets(sLine,500-1,fp) == NULL)
displayErrorMessage("Error reading sparse matrix sporder.\n");
sVarVal = strtok(sLine," ,;:^*\"/\t\'\\\n");
sscanf(sVarVal,"%d",&_spid);
sVarVal = strtok(NULL," ,;:^*\"/\t\'\\\n");
sscanf(sVarVal,"%d",&_puid);
sVarVal = strtok(NULL," ,;:^*\"/\t\'\\\n");
sscanf(sVarVal,"%lf",&amount);
if (_spid < iLastSPID)
{
// error condition exists, file is not in ascending order for SPID
appendTraceFile("Error: PU v Species file %s is not in ascending order for SPID at record %i.\nAborting Program.",fnames.puvsprname,i+1);
displayErrorMessage("Error: PU v Species file %s is not in ascending order for SPID at record %i.\nAborting Program.",fnames.puvsprname,i+1);
}
iLastSPID = _spid;
_puid = binarySearchPuIndex(puno,_puid,PULookup);
spid = binarySearchSpecIndex(spno,_spid,SPLookup);
// increment richness for planning unit containing this feature
spec[spid].richness += 1;
// if planning units richness is one, set its offset
if (spec[spid].richness == 1)
spec[spid].offset = i;
(* SM)[i].amount = amount;
(* SM)[i].puindex = _puid;
}
fclose(fp);
iBigMatrixSize = puno * spno;
rInternalSMSize = iInternalSMSize;
rBigMatrixSize = iBigMatrixSize;
rDensity = rInternalSMSize / rBigMatrixSize * 100;
displayProgress1("%i conservation values counted, %i big matrix size, %g%% density of matrix \n",
iInternalSMSize,iBigMatrixSize,rDensity);
} // readSparseMatrixSpOrder
// read value for a single parameter specified in input.dat parameter file
void readInputOption(FILE *infile, char varname[], void *address, int parmtype, int crit,int present)
// Reads a variable of parmtype in from infile. Assumes that the next line is the one that has the
// variable in question but will wrap once to find the variable.
//
// I changed this function because it was ignoring the last line of the file (also it was a dogs breakfast).
// it returns the "Final value taken" error message when SAVESOLUTIONSMATRIX parameter is on last line, weird, need to fix this.
{
int foundit, namelen, check1, check2, gotit;
char buffer[255] = "\0"; /* for storing the line found in the file */
namelen = strlen(varname); /* figure out how long the string is */
foundit = 0;
rewind(infile); // search from top of infile
do
{ /* loop through file looking for varname */
if (fgets(buffer,255,infile) == NULL)
//displayErrorMessage("Error reading input parameter %s.\n",varname)
;
check1 = 0;
check2 = 0;
while (buffer[check1++] == varname[check2++])
;
if (check1 > namelen)
{ // varname matches upto namelen
foundit++;
switch (parmtype)
{
case REAL :
gotit = sscanf(&buffer[check1]," %f", (float *) address);
break;
case DOUBLE :
gotit = sscanf(&buffer[check1]," %lf", (double *) address);
break;
case INTEGER :
gotit = sscanf(&buffer[check1]," %d", (int *) address);
break;
case LONGINT :
gotit = sscanf(&buffer[check1]," %ld", (long int *) address);
break;
case STRING :
// trim leading and trailing blanks (allow spaces which are important for directory names
check1 += strspn(&buffer[check1]," ,");
for (check2 = strlen(&buffer[check1])-1;isspace(buffer[check1+check2]) != 0;check2--)
; // Find last non space character
if (strlen(&buffer[check1]) <2)
buffer[check1] = '\0';
buffer[check1 + check2+1] = '\0';
strcpy((char *) address,&buffer[check1]);
gotit = 1; // So that var check works. This needs further consideration
break;
default :
displayErrorMessage("Invalid parameter type request %d: \n",parmtype);
}
if (!gotit)
{
displayWarningMessage("WARNING: found bad value for variable %s. Value ignored\n",varname);
foundit--;
}
}
} while (!(feof(infile)));
if (!foundit)
if (crit)
displayErrorMessage("Unable to find %s in input file.\n",varname);
if (foundit > 1)
displayWarningMessage("WARNING variable: %s appears more than once in the input file. Final value taken\n",varname);
present = foundit;
return;
} // readInputOption
// read values for the parameters specified in input.dat parameter file
void readInputOptions(double *cm,double *prop,struct sanneal *anneal,
int *iseed,
long int *repeats,char savename[],struct sfname *fnames,char filename[],
int *runopts,double *misslevel,int *heurotype,int *clumptype,
int *itimptype, int *verb,
double *costthresh,double *tpf1,double *tpf2)
{
FILE *fp;
double version;
int present;
char stemp[500];
#ifdef DEBUGTRACEFILE
char debugbuffer[200];
#endif
verbosity = 1; /* This enables local warning messages */
/* Setup all of the default parameter variables */
version = 0.1;
*cm = 0;
*prop = 0;
(*anneal).type = 1;
(*anneal).iterations = 0;
(*anneal).Tinit = 1;
(*anneal).Tcool = 0;
(*anneal).Titns = 1;
*iseed = -1;
*costthresh = 0;
*tpf1 = 0;
*tpf2 = 0;
*repeats = 0;
(*fnames).saverun = 0;
(*fnames).savebest = 0;
(*fnames).savesum = 0;
(*fnames).savesen = 0;
(*fnames).savespecies = 0;
(*fnames).savesumsoln = 0;
(*fnames).savepenalty = 0;
(*fnames).savetotalareas = 0;
(*fnames).savedebugtracefile = 0;
(*fnames).saverichness = 0;
(*fnames).savesolutionsmatrix = 0;
(*fnames).solutionsmatrixheaders = 1;
(*fnames).savelog = 0;
(*fnames).saveannealingtrace = 0;
(*fnames).annealingtracerows = 0;
(*fnames).saveitimptrace = 0;
(*fnames).itimptracerows = 0;
(*fnames).savespec = 0;
(*fnames).savepu = 0;
(*fnames).savepuvspr = 0;
(*fnames).savematrixsporder = 0;
(*fnames).rimagetype = 0;
(*fnames).rexecutescript = 0;
(*fnames).rclustercount = 0;
(*fnames).rimagewidth = 0;
(*fnames).rimageheight = 0;
(*fnames).rimagefontsize = 0;
asymmetricconnectivity = 0;
strcpy(savename,"temp");
*misslevel = 1;
*heurotype = 1;
*clumptype = 0;
verbosity = 1;
/* Open file and then feed in each variable type */
if ((fp = fopen(filename,"r"))==NULL)
displayErrorMessage("input file %s not found\nAborting Program.\n\n",filename);
readInputOption(fp,"VERSION",&version,DOUBLE,0,present);
readInputOption(fp,"BLM",cm,DOUBLE,0,present);
if (present == 0)
readInputOption(fp,"CM",cm,DOUBLE,0,present);
readInputOption(fp,"PROP",prop,DOUBLE,0,present);
readInputOption(fp,"RANDSEED",iseed,INTEGER,0,present);
/* Annealing Controls */
readInputOption(fp,"NUMITNS",&(*anneal).iterations,LONGINT,0,present);
readInputOption(fp,"STARTTEMP",&(*anneal).Tinit,DOUBLE,0,present);
readInputOption(fp,"COOLFAC",&(*anneal).Tcool,DOUBLE,0,present);
readInputOption(fp,"NUMTEMP",&(*anneal).Titns,INTEGER,0,present);
(*anneal).type = 1;
if ((*anneal).iterations < 1 )
(*anneal).type = 0;
if ((*anneal).Tinit < 0)
(*anneal).type = (int) (-(*anneal).Tinit) + 1; /* type is negative of Tinit */
if (fscanf(fp,"%i",iseed) == 0) /* The random seed. -1 to set by clock */
displayErrorMessage("Error reading input parameter random seed.\n");
/* Various controls */
readInputOption(fp,"NUMREPS",repeats,LONGINT,0,present);