Repository navigation
Expand file tree
/
Copy pathVida.py
More file actions
1436 lines (1291 loc) · 77.2 KB
/
Copy pathVida.py
File metadata and controls
1436 lines (1291 loc) · 77.2 KB
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
#!/usr/bin/env python
"""This file is part of Vida.
--------------------------
Copyright 2023, Sean T. Hammond
Vida is experimental in nature and is made available as a research courtesy "AS IS," but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
You should have received a copy of academic software agreement along with Vida. If not, see <https://github.com/seanth/Vida/blob/master/LICENSE.txt>.
"""
vidaVersion = "0.10.0"
import random
import math
import os
import os.path
import glob
import sys
import argparse
import io
# if (sys.version_info.major)==2:
# import ConfigParser
# else:
import configparser as ConfigParser
import pathlib
import copy
import time
import pickle
###append the path to basic data files
sys.path.append("Vida_Data")
import vworldr as worldBasics
import vplantr as defaultSpecies
import vgraphics as outputGraphics
import list_utils as list_utils
import geometry_utils as geometry_utils
from dxfwrite import DXFEngine as dxf #pip install dxfwrite #https://pypi.org/project/dxfwrite/
import yaml #pip install PyYAML #https://pypi.org/project/PyYAML/
import progressBarClass
###append the path to where species are
sys.path.append("Species")
sList=[]
theCLArgs=""
##########################################
#Import the options#
try:
theConfig=ConfigParser.RawConfigParser()
theConfig.optionxform = str
theConfig.read('Vida.ini')
theConfigSection='Vida Options'
except ConfigParser.MissingSectionHeaderError:
print("Warning: Invalid config file, no [%s] section.") % (theConfigSection)
raise
theDefaults={}
for i in theConfig.items(theConfigSection):
theItem=i[0]
try:
theValue=theConfig.getint(theConfigSection, theItem)
except:
try:
theValue=theConfig.getboolean(theConfigSection, theItem)
except:
try:
theValue=theConfig.getfloat(theConfigSection, theItem)
except:
try:
theValue=theConfig.get(theConfigSection, theItem)
if theValue=="None": theValue=None
except:
print("what the...?")
theDefaults[theItem]=theValue
#print theDefaults
#print "#################"
class parseAction(argparse.Action):
def __call__(self,parser,args,theValues,option_string=None):
###reload species option
if self.dest=="resumeSim":
theValues=[theValues, True]
if self.dest=="resumeSimReload":
theValues=[theValues, True]
###video option
if self.dest=="produceVideo":
if theValues==None:
theValues=True
else:
try:
theValues=[True, int(theValues)]
except:
print("\n***Warning: -v [int] must have an integer value\n Setting frames per second to the default value")
theValues=[True, theDefaults['framesPerSecond']]
###graphical option
if self.dest=="produceGraphics":
if theValues==[]:
theValues=[True, theDefaults['graphicalView']]
else:
theValues=[True, theValues]
###seed placement option
if self.dest=="startPopulationSize":
#this adds a second item defining if the placement is
#as a square, hex, defined in a placement file, or random
theOpt=self.option_strings
if theOpt==['-ss']: theOpt='square'
elif theOpt==['-sh']: theOpt='hex'
elif theOpt==['-sf']: theOpt='fromFile'
else: theOpt='random'
if theValues==None:
theValues=[self.default, theOpt]
else:
theValues=[theValues, theOpt]
###archive options
if self.dest=="archive" or self.dest=="saveData":
#theOpt=self.option_strings
theValues=theValues[0]
setattr(args, self.dest, theValues)
##############################################
class Species1(defaultSpecies.genericPlant):
###The routine in defaultSpecies.genericPlant reads in default values from .yml file
def __init__(self):
##super(type, obj) -> bound super object; requires isinstance(obj, type)
super(Species1, self).__init__()
def saveSimulationPoint(theDirectory, theFileName, theGarden):
simulationStateFile =open(theDirectory + theFileName, 'wb')
pickle.dump(theGarden, simulationStateFile)
simulationStateFile.close()
#print "simulation state saved to file"
def saveDataPoint (theDirectory, theFileName, theGarden):
#Added "Area Canopy" at end of list
#Added basal area to the outputs--STH 2019-0404
basicHeaders="Cycle #, Plant Name, Species, Mother Plant Name, X Location, Y Location, Z Location, elevation, elevation above water,\
absHeightStem, is a seed, is mature, Age at Maturity, cycles until germination, Age, Mass of Stem, Mass of Canopy, \
# of Seeds, Mass of all Seeds, Mass Stem+Mass Canopy, Mass Total, Diameter Stem, Radius Canopy, Area covered, \
Height Stem, Maximum Thickness of a Leaf, Height of Plant, Yearly Growth Stem (kg), Yearly Growth Canopy (kg), \
Yearly Growth Stem+Canopy (kg), Yearly Growth Stem diameter (m), Yearly Growth Stem Height (m), Average Growth Stem (kg), \
Average Growth Canopy (kg), Average Growth Stem+Canopy (kg), Average Growth Stem diameter (m), Average Growth Stem Height (m),\
Cause of Death, Functional Area, basal_area \n"
photosynthesisHeaders="leaf is a hemisphere?, area canopy available for photosynthesis, area canopy covered, fraction canopy 100% shaded, canopy transmittance, canopy transmittance impacts photosynthesis?, faction of canopy that needs to be 0% shaded for survival"
reproductionHeaders="make seeds?, age to start making seeds, height to start making seeds, fraction of mass used for seeds, Maximum Seed Mass, Where seeds grow, Seed dispersal method, fraction of seeds which fail to germinate, delay in germination, fraction of maximum seed mass needed to germinate, fraction of seed mass converted to plant mass"
allometryHeaders="B1 in Ms=B1*(Mt^a1), a1 in Ms=B1*(Mt^a1), B2 in Mlyoung=B2*(Ms^a2), a2 in Mlyoung=B2*(Ms^a2), B3 in Mlmature=B3*(Ms^a3), a3 in Mlmature=B3*(Ms^a3), B4 in Ds=B4*(Ms^a4), a4 in Ds=B4*(Ms^a4), B5 in Hs=[B5*(Ds^a5)]-B6, a5 in [B5*(Ds^a5)]-B6, B6 in Hs=[B5*(Ds^a5)]-B6, B7 in Mg=[B7*(Ml^a7)]/ area canopy 100% uncovered, a7 in Mg=[B7*(Ml^a7)]/ area canopy 100% uncovered, B8 in Mpt=B8*(Al^a8), a8 in Mpt=B8*(Al^a8)"
#theHeader="Plant Name, Mother Plant, Species, X Location, Y Location, Mass Stem, Mass Leaf, # Seeds, Mass all Seeds, Radius Stem, Radius Leaf, Height Plant, Cause of Death \n"
theHeader= basicHeaders
thePlantList=[]
theSeedList=[]
theCorpseList=[]
thePlantList.append(theHeader)
theSeedList.append(theHeader)
theCorpseList.append(theHeader)
for plant in theGarden.soil:
if plant.age>0:
theData="%i,%s,%s,%s,%f,%f,%f,%f,%f,%f,%s,%s,%s,%i,%i,%f,%f,%i,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%s,%f,%f \n" % \
(theGarden.cycleNumber,plant.name,plant.nameSpecies,plant.motherPlantName,plant.x,plant.y,plant.z,plant.elevation, (plant.elevation-theGarden.waterLevel),
plant.absHeightStem,plant.isSeed,plant.isMature,plant.matureAge,plant.countToGerm,plant.age,plant.massStem,plant.massLeaf,
len(plant.seedList),plant.massSeedsTotal,plant.massStem+plant.massLeaf,plant.massTotal,plant.radiusStem*2,plant.radiusLeaf,plant.areaCovered,
plant.heightStem,plant.heightLeafMax,plant.z,plant.GMs,plant.GMl,
plant.GMs+plant.GMl,2.0*plant.GRs,plant.GHs,plant.massStem/plant.age,
plant.massLeaf/plant.age,(plant.massStem+plant.massLeaf)/plant.age,(plant.radiusStem*2)/plant.age,plant.heightStem/plant.age,
"na",3.14159*plant.radiusLeaf**2-plant.areaCovered, 3.14159*plant.radiusStem**2)
else:
theData="%i,%s,%s,%s,%f,%f,%f,%f,%f,%f,%s,%s,%s,%i,%i,%f,%f,%i,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%s,%f,%s \n" % \
(theGarden.cycleNumber,plant.name,plant.nameSpecies,plant.motherPlantName,plant.x,plant.y,plant.z,plant.elevation, (plant.elevation-theGarden.waterLevel),
plant.absHeightStem,plant.isSeed,plant.isMature,plant.matureAge,plant.countToGerm,plant.age,plant.massStem,plant.massLeaf,
len(plant.seedList),plant.massSeedsTotal,plant.massStem+plant.massLeaf,plant.massTotal,plant.radiusStem*2,plant.radiusLeaf,plant.areaCovered,
plant.heightStem,plant.heightLeafMax,plant.z,plant.GMs,plant.GMl,
plant.GMs+plant.GMl,2.0*plant.GRs,plant.GHs,0,0,0,0,0,
"na",0,0)
if plant.isSeed:
theSeedList.append(theData)
else:
thePlantList.append(theData)
for plant in theGarden.deathNote:
if plant.age>0:
theData="%i,%s,%s,%s,%f,%f,%f,%f,%f,%f,%s,%s,%s,%i,%i,%f,%f,%i,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%s,%f,%f \n" % \
(theGarden.cycleNumber,plant.name,plant.nameSpecies,plant.motherPlantName,plant.x,plant.y,plant.z,plant.elevation, (plant.elevation-theGarden.waterLevel),
plant.absHeightStem,plant.isSeed,plant.isMature,plant.matureAge,plant.countToGerm,plant.age,plant.massStem,plant.massLeaf,
len(plant.seedList),plant.massSeedsTotal,plant.massStem+plant.massLeaf,plant.massTotal,plant.radiusStem*2,plant.radiusLeaf,plant.areaCovered,
plant.heightStem,plant.heightLeafMax,plant.z,plant.GMs,plant.GMl,
plant.GMs+plant.GMl,2.0*plant.GRs,plant.GHs,plant.massStem/plant.age,
plant.massLeaf/plant.age,(plant.massStem+plant.massLeaf)/plant.age,(plant.radiusStem*2)/plant.age,plant.heightStem/plant.age,
plant.causeOfDeath,3.14159*plant.radiusLeaf**2-plant.areaCovered, 3.14159*plant.radiusStem**2)
else:
theData="%i,%s,%s,%s,%f,%f,%f,%f,%f,%f,%s,%s,%s,%i,%i,%f,%f,%i,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%s,%f,%s \n" % \
(theGarden.cycleNumber,plant.name,plant.nameSpecies,plant.motherPlantName,plant.x,plant.y,plant.z,plant.elevation, (plant.elevation-theGarden.waterLevel),
plant.absHeightStem,plant.isSeed,plant.isMature,plant.matureAge,plant.countToGerm,plant.age,plant.massStem,plant.massLeaf,
len(plant.seedList),plant.massSeedsTotal,plant.massStem+plant.massLeaf,plant.massTotal,plant.radiusStem*2,plant.radiusLeaf,plant.areaCovered,
plant.heightStem,plant.heightLeafMax,plant.z,plant.GMs,plant.GMl,
plant.GMs+plant.GMl,2.0*plant.GRs,plant.GHs,0,0,0,0,0,
plant.causeOfDeath,0,0)
theCorpseList.append(theData)
if len(thePlantList)>1:
saveDataFile =open(theDirectory+"Plants/"+ theFileName, 'w')
saveDataFile.writelines(thePlantList)
saveDataFile.close()
if len(theSeedList)>1:
saveDataFile =open(theDirectory+"Seeds/"+ theFileName, 'w')
saveDataFile.writelines(theSeedList)
saveDataFile.close()
if len(theCorpseList)>1:
saveDataFile =open(theDirectory+"Corpses/"+ theFileName, 'w')
saveDataFile.writelines(theCorpseList)
saveDataFile.close()
thePlantList=[]
theSeedList=[]
theCorpseList=[]
#print "simulation data saved to file
def makeDirectory(theDirectory):
if not os.path.exists(theDirectory):
os.mkdir(theDirectory)
else:
###all this adds an increasing numberic suffix to the folders so
###one doesn't overwrite things
while os.path.exists(theDirectory):
basicPath=os.path.split(theDirectory)[0]
i=0
for n in basicPath:
i=i-1
if n=="/": break
if not basicPath[i].isdigit():break
theSuffix=""
if i<-1:
theSuffix=int(basicPath[i+1:])+1
basicPath=basicPath[0:i+1]
else:
theSuffix=1
basicPath=basicPath+str(theSuffix)
theDirectory=basicPath+"/"
os.mkdir(theDirectory)
return theDirectory
# def dirPath(thePath):
# return thePath
def correctType(theItem):
returnValue="na"
if theItem.isdigit():
returnValue=int(theItem)
else:
try:
returnValue=float(theItem)
except ValueError:
try:
returnValue=str(theItem)
except ValueError:
returnValue="na"
return returnValue
def checkSeedPlacementList(seedPlacementList):
###################
######STH 2022-1105
###remove trailing "\n"
seedPlacementList = [aLine.rstrip() for aLine in seedPlacementList]
###remove blank lines
seedPlacementList = [aLine for aLine in seedPlacementList if aLine!=""]
###break the CSV on each line into a list
seedPlacementList = [aLine.split(",") for aLine in seedPlacementList]
i=0
for aLine in seedPlacementList:
aLine = [x.strip() for x in aLine] #remove any leading or trailing white spaces
theLength=len(aLine)
if theLength>4: del aLine[4:theLength] #if it is too long, just chop it
aLine = aLine+["0.0"]*(4-theLength) #if too short, make it longer
#check item in the list and convert from string to correct type
aLine = [correctType(x) for x in aLine]
seedPlacementList[i]=aLine
i=i+1
i=0
printErrorMessage=0
for aLine in seedPlacementList:
if type(aLine[3])==float:
aLine[3]=int(aLine[3])
if (type(aLine[0])!=str) or (type(aLine[1])!=float) or (type(aLine[2])!=float) or (type(aLine[3])!=int):
printErrorMessage=1
del seedPlacementList[i]
i=i+1
if printErrorMessage:
print("***Improper seeding file format...")
print(" Questionable lines will be ignored.")
return seedPlacementList
def correctSList(sList):
startPopulationSize=thePrefs.startPopulationSize
###this is probably a list###
sList=sList[1:-1]
sList=sList.split(",")
sList=map(correctType, sList)
i=0
showSAlert1=0
showSAlert2=0
showSAlert3=0
for x in sList:
if i==0 and type(x)==int:
#the first item is a number. This is not correct
#insert the default species here
showSAlert1=1
showSAlert2=1
sList.insert(0, "_random_")
if i+1==len(sList) and type(sList[i])==str:
#the last item in the list is a string
#put a int after it
showSAlert1=1
showSAlert3=1
sList.insert(i+1, thePrefs.startPopulationSize)
elif type(sList[i])==str and type(sList[i+1])==str:
#this is probably a two species names next to one another
#insert a int between them
showSAlert1=1
showSAlert3=1
sList.insert(i+1, thePrefs.startPopulationSize)
elif type(sList[i])==int and type(sList[i-1])==int:
showSAlert1=1
showSAlert2=1
sList.insert(i, "_random_")
i=i+1
if showSAlert1: print("***Improper seeding [list] format...")
if showSAlert2: print(" Inserting random species...")
if showSAlert3: print(" Inserting default starting population size...")
if showSAlert1: print(" input set to: %s." % (sList))
return sList
def main():
#why do only these need to be reminded they are global?
#YUP. Confirmed that old me was smarter 2 October 2020 STH
global simulationFile
global theWorldSize
global startPopulationSize
global seedPlacement
global sList
global theCLArgs
global absMin
global absMax
global waterLevel
global terrainScale
print("*********Vida version: %s *********" % (vidaVersion))
CFDGtext=""
CFDGtextDict={}
if debug==1: print("***debug is on***")
theGarden= worldBasics.garden()
#####
#I am not thrilled with how this works. waterLevel
#is property that can be enetered from the command
#line. So it has a default value in the .ini file.
#BUT it's also a property of the garden that can be
#altered by event files. This means it can also be
#in the Vida World Preferences.yml file. The command
#line value overrides any value in the Vida World
#Preferences.yml file, but then the event file value
#if any exists, should be the one used.
#I don't like that waterLevel exists in multiple places
#STH 2021-0306
theGarden.waterLevel = waterLevel
theGarden.platonicSeeds={}
theGarden.theRegions=[]
####################################
###experiments in importing events
#if eventFile!=None and os.path.exists(eventFile):
if eventFile!=None:
#if type(eventFile)==file:
print("***Loading event file: %s***" % (eventFile.name))
#theFile=open(eventFile)
#eventData=yaml.load(theFile)
eventData=yaml.load(eventFile, Loader=yaml.FullLoader)
#theFile.close
eventTimes=eventData.keys()
else:
eventTimes=[]
#####################################
####################################
###experiments in importing a terrain file
if terrainFile!=None:
print("***Checking for tif terrain image...***")
if os.path.isfile(terrainFile) == True:
theTerrainFile = terrainFile
tiffFound = True
print(" Tif file found")
#reworked checking for file types in a directory
#STH 23 Sept 2020
elif os.path.isdir(terrainFile) == True:
print("***Checking directory for tif terrain image...***")
tmpPath = os.path.join(terrainFile,'*.tif') #assumes file suffix is 'tif'
matchFiles = glob.glob(tmpPath)
#print matchFiles
if not matchFiles:
#no matching files found
tiffFound = False
else:
theTerrainFile = matchFiles[0] #no matter what, grab the first item in the list
if len(matchFiles)==1:
print(" Tif file found")
else:
print(" Multiple tif files found. Using:")
print(" %s" % os.path.basename(theTerrainFile))
tiffFound = True
else:
print(" Tiff terrain image not found. Skipping terrain import")
tiffFound = False
if tiffFound == True:
#########################################################################
from PIL import Image, ImageOps
#if type(eventFile)==file:
print("***Loading terrain file:\n %s***" % (theTerrainFile))
#theImage = Image.open(terrainFile)
tmp=Image.open(theTerrainFile)
tmp=ImageOps.flip(tmp)
#format of terrainImage is [mode, size tuple, image as bytes] STH 0212-2020
#make sure the list is the correct length
if len(theGarden.terrainImage)<3:
theGarden.terrainImage=[0]*3
#store the image mode
theGarden.terrainImage[0]=tmp.mode
#the imported image might not be a square.
#If it is not a square, trim it
#STH 2021-06-28
if tmp.size[0]!=tmp.size[1]:
if tmp.size[0]<tmp.size[1]:
smallestDim = tmp.size[0]
else:
smallestDim = tmp.size[1]
print("***WARNING: imported image is not square.")
print(" Size is: %s x %s" % (tmp.size[0], tmp.size[1]))
print(" Truncating image to %ix%i..." % (smallestDim, smallestDim))
tmp=tmp.crop((0, 0, smallestDim, smallestDim))
#if the imported image is not the same size as the world size, change it
#this assumes the image is a square
#STH 2021-0628
if tmp.size[1] != theWorldSize:
print("***WARNING: imported image is not the same size as the world space.")
print(" Size is: %s x %s" % (tmp.size[0], tmp.size[1]))
print(" Resizing image to %ix%i..." % (theWorldSize, theWorldSize))
tmp=tmp.resize((theWorldSize,theWorldSize))
#store the image size
theGarden.terrainImage[1]=tmp.size
#store the image as bytes
theGarden.terrainImage[2]=tmp.tobytes()
tmp.close()
tmp=None
#########################################################################
if os.path.isdir(terrainFile) == True:
#Now look for a .xlsx file
#.xlsx files have headers of six lines, followed by tabular data
#ET addition 9-15-2020
print("***Checking directory for xlsx terrain data...***")
tmpPath = os.path.join(terrainFile,'*.xlsx') #assumes file suffix is 'xlsx'
matchFiles = glob.glob(tmpPath)
if matchFiles:
#9/28/2020 ET-test of default absMax and absMin values from vida.ini
theExcelFile = matchFiles[0] #no matter what, grab the first item in the list
if len(matchFiles)==1:
print(" xlsx file found")
else:
print(" Multiple xlsx files found. Using:")
print(" %s" % os.path.basename(theExcelFile))
import pandas
fileData = pandas.read_excel(theExcelFile, header=None)
#each row in the header will have a single value
#but we're not entirely sure exactly what cell the value will be in
#STH 2023-0302
#this too a while to figure out, so I'm explaining to future me:
#I'm getting the row (index starts 0), and then excluding the
#first value on the row. Then getting the max value in that
#remaining row. STH 2023-0302
theNCols = fileData.iloc[1][1:].max()
theNRows = fileData.iloc[2][1:].max()
theCellSize = fileData.iloc[5][1:].max()
print(" ncols: %s" % theNCols)
print(" nrows: %s" % theNRows)
print(" cellsize: %s" % theCellSize)
#use the smaller axis to calculate the scaling based on
#number of cells and cellsize
if theNCols<theNRows:
theScaledDistance = theCellSize*theNCols
else:
theScaledDistance = theCellSize*theNRows
terrainScale=theWorldSize/theScaledDistance
#sometimes there columns of NaN beyond the defined column max
fileData = fileData.drop(theNCols, axis=1)
#trim off those first few rows
fileData = fileData.tail(-7)
#make sure all the values are numbers
fileData = fileData.astype(float)
allMaxForEachColumn = fileData.max()
absMax = allMaxForEachColumn.max()
allMinForEachColumn = fileData.min()
absMin = allMinForEachColumn.min()
else:
print("***xlsx terrain data terrain data not found. Using default values***")
theMaxElevation = absMax-absMin
theMaxElevation = theMaxElevation*terrainScale
theGarden.maxElevation = theMaxElevation
print(" absMax: %s" % absMax)
print(" absMin: %s" % absMin)
print(" terrainScale: %s" % terrainScale)
print(" therefore max: %s" % (theMaxElevation))
####################################
#########Check for multiple species. If none, use default
fileList=os.listdir("Species")
ymlList=[]
pythonList=[]
useDefaultYml=True
print("***Checking for species...***")
for file in fileList:
theExtension=os.path.splitext(file)[1]
if theExtension==".yml":
#add this file to the list of yaml files
ymlList.append(file)
useDefaultYml=False
elif theExtension==".py":
#this might be override info for a species
#add this file to the list of species python files
#this isn't implemented
pythonList.append(file)
fileList=[]
##########
if (resumeSim==True or resumeSimReload==True) and not simulationFile=="":
print("***Loading simulation: %s...***" % (simulationFile.name))
#simulationFile=open(simulationFile, 'r')
theGarden=pickle.load(simulationFile)
#simulationFile.close()
theWorldSize=theGarden.theWorldSize
print("***Resuming Simulation: %s as %s***" % (theGarden.name, simulationName))
theGarden.name=simulationName
startPopulationSize=theGarden.numbPlants
if resumeSimReload==True:
print("***Reloading Vida World Preferences...***")
#reload Vida World Preferences.yml in case there were changes
fileLoc="Vida World Preferences.yml"
theGarden.importPrefs(fileLoc)
##this should reload species data.
###Important if you want to compare runs
# if reloadSpeciesData==True:
# # print "***Reloading species data...***"
# # ###fileLoc will be different for each species eventually
# # fileLoc="Vida_Data/Default_species.yml"
# # for item in theGarden.soil:
# # item.importPrefs(fileLoc)
else:
theGarden.makePlatonicSeedDict(ymlList, Species1)
print("***Species loaded.***")
theGarden.name=simulationName
theGarden.theWorldSize=theWorldSize
print("***Beginning Simulation: %s***" % (simulationName))
theGarden.showProgressBar=showProgressBar
print(" World size: %ix%i" % (theWorldSize, theWorldSize))
if (terrainFile!=None and tiffFound == True):
print(" Terrain file: %s" % (theTerrainFile))
print(" Terrain scaling: %f" % (terrainScale))
print(" Maximum population size will be: %i" % (maxPopulation))
print(" and")
print(" Running simulation for %i cycles" % (maxCycles))
print(" (whichever comes first)")
print(" Starting population size: %i" % (startPopulationSize))
# if theGarden.carbonAllocationMethod==0:
# print(" Plants will allocate carbon to stem and leaf using methods defined by the species.")
# else:
# print(" All plants will allocate carbon to stem and leaf using method %i" % (theGarden.carbonAllocationMethod))
print("")
if produceGraphics==True:
print(" Graphical output will be produced.")
for aView in graphicalView:
if aView=="3d":
print(" Graphical output will be 3d.")
if aView==1:
print(" Graphical output will be a bottom-up view.")
if aView==2:
print(" Graphical output will be a top-down view.")
if aView==3:
print(" Graphical output will be a side-view.")
if aView==12:
print(" Graphical output will be a combination bottom-up and top-down view.")
if aView==21:
print(" Graphical output will be a combination top-down and bottom-up view.")
if aView==13 or aView==31:
print(" Graphical output will be a combined top-down and side view.")
if aView==23:
print(" Graphical output will be a combined bottom-up and side view.")
if aView==123:
print(" Graphical output will be a combination bottom-up, top-down and side view.")
if produceVideo==True:
print(" Graphical output will include a %s frame/second video." % (framesPerSecond))
if saveData=="a":
print("\n All simulation data will be saved")
if saveData=="e":
print("\n End simulation data will be saved")
if saveData=="n":
print("\n No simulation data will be saved")
if saveData=="s":
print("\n Starting simulation data will be saved")
if archive=="a":
print(" All simulation time points will be saved\n")
if archive=="e":
print(" End simulation time point will be saved\n")
if archive=="n":
print(" No simulation time point will be saved\n")
if archive=="s":
print(" Starting simulation time point will be saved\n")
###I think this is where to start the times to repeat bit
for x in range(timesToRepeat):
###make necessary directories
outputDirectory="Output-"+simulationName+"/"
outputDirectory=makeDirectory(outputDirectory)
###save the command line arguments
theCLArgs=" ".join(map(str, theCLArgs))
theCLIfile=open(outputDirectory+"CLI_arguments.txt", 'w')
theCLIfile.writelines(theCLArgs)
theCLIfile.close()
#if produceGraphics==1 or produceDXFGraphics==1:
if produceGraphics==True:
baseGraphicsDirectory = outputDirectory +"Graphics/"
makeDirectory(baseGraphicsDirectory)
outputGraphicsDirectoryDict={}
for aView in graphicalView:
if aView=='3d':
outputGraphicsDirectory = baseGraphicsDirectory +"DXF/"
if aView==1:
outputGraphicsDirectory = baseGraphicsDirectory +"bottom-up/"
if aView==2:
outputGraphicsDirectory = baseGraphicsDirectory +"top-down/"
if aView==3:
outputGraphicsDirectory = baseGraphicsDirectory +"side/"
if aView==12:
outputGraphicsDirectory = baseGraphicsDirectory +"combined-bottom-top/"
if aView==21:
outputGraphicsDirectory = baseGraphicsDirectory +"combined-top-bottom/"
if aView==13 or aView==31:
outputGraphicsDirectory = baseGraphicsDirectory +"combined-bottom-side/"
if aView==23:
outputGraphicsDirectory = baseGraphicsDirectory +"combined-top-side/"
if aView==123:
outputGraphicsDirectory = baseGraphicsDirectory +"combined-bottom-top-side/"
outputGraphicsDirectoryDict[aView]=outputGraphicsDirectory
makeDirectory(outputGraphicsDirectory)
if not archive=="n":
saveDirectory = outputDirectory+"Save_points/"
makeDirectory(saveDirectory)
if not saveData=="n":
dataDirectory = outputDirectory+"Simulation_data/"
makeDirectory(dataDirectory)
makeDirectory(dataDirectory+"Seeds/")
makeDirectory(dataDirectory+"Plants/")
makeDirectory(dataDirectory+"Corpses/")
if resumeSim==None and resumeSimReload==None:
#2008.11.06 Moved a huge block of code related to placing seeds to vworldr.py
theGarden.placeSeed(seedPlacement, sList, startPopulationSize, useDefaultYml, ymlList)
################
#if there is a terrain file and water level then the initial placement of seeds
#should be checked to see if any of them are below water
#STH 0328-2021
if(theGarden.terrainImage != []):
theGarden.checkSubmergedMortality()
################
if produceGraphics==True and CFDGtextDict=={}:
for aView in graphicalView:
if aView!="3d":
#Only call this once to save time in making 2d graphics
CFDGtextDict[aView]=outputGraphics.initCFDGText(theGarden, aView, percentTimeStamp, 50.0)
else:
#Only call this once to save time in making 3d graphics
DXFBlockDefs = vdxfGraphics.initDXFBlocks(theGarden)
#######
cycleNumber=0
print("\n***Running simulation.***")
if not showProgressBar:
theProgressBar= progressBarClass.progressbarClass(maxCycles,"*") #why -1? because index 0. So if total=100, 0-99.
while (theGarden.numbPlants<=maxPopulation and cycleNumber<=maxCycles) and (theGarden.numbPlants+theGarden.numbSeeds)>0:
###################################################################################
####Experimental scripting event stuff #
if cycleNumber in eventTimes: #
for aItem in eventData[cycleNumber]: #
for aKey in aItem.keys(): #
if aKey=="Garden": #
if debug==1: print("debug: A garden related event has been triggered.") #
theDict=aItem[aKey][0] #
gardenAttrs=theDict.keys() #
for theGardenAttr in gardenAttrs: #
setattr(theGarden, theGardenAttr, theDict[theGardenAttr]) #
gardenAttrs=""
elif aKey=="Killzone" or aKey=="Safezone":
if debug==1: print("debug: generation of a zone event has been triggered.")
theDict=aItem[aKey][0]
#the atrributes in a killzone are: X & Ythe SHAPE(circle or square) is centered on, the SIZE of the shape, and what is the TARGET
#(plants or seeds). You can also define a SPECIES_NAME to target
zoneAttrs=theDict.keys()
zoneX=float(theDict['x'])
zoneY=float(theDict['y'])
zoneSize=float(theDict['size'])
zoneShape=theDict['shape']
zoneTarget=theDict['target']
if 'species_name' in theDict:
zoneSpecies=theDict['species_name']
#student requested addition to accept list of species. 2023-0323
zoneSpecies=zoneSpecies.split(",")
#print(zoneSpecies)
else:
zoneSpecies = 'all'
###############
#Adding in ability to have chance of what happens in a zone
#STH 2026-0908
#Percent is a percent chance the event will happen to the targeted thing
if 'percent' in theDict:
zonePercent=theDict['percent']
if zonePercent>1.0:
zonePercent=1.0
else:
zonePercent=1.0
###########
#Adding in ability to be able to target plants based on an attribute the plant has
#STH 2026-0909
if "selection" in theDict:
theSelectionDict=theDict['selection'][0]
#make sure the selection array has all three needed elements
if not ("attribute" and "logic" and "value") in theSelectionDict:
print("\n***WARNING: Selection array needs exactly three elements: attribute, logic, and value")
print("\n***Ignoring selection criteria")
zoneSelAttribute = "none"
else:
zoneSelAttribute = theSelectionDict['attribute']
zoneSelLogic = theSelectionDict['logic']
zoneSelValue = theSelectionDict['value']
#some checks on data type
# if not isinstance(zoneSelValue, (int, float)):
# print("***WARNING: value element must be a number")
# print("***Ignoring selection criteria")
# zoneSelAttribute = "none"
if zoneSelLogic not in ['<', '=', '==', '>']:
print("\n***WARNING: logic element must be <, =, ==, or >")
print("***Ignoring selection criteria")
zoneSelAttribute = "none"
if (zoneSelLogic in ['<', '>']) and (isinstance(zoneSelValue, (str))):
print("\n***WARNING: can't use '<' or '>' on a value element that is a string")
print("***Ignoring selection criteria")
zoneSelAttribute = "none"
else:
zoneSelAttribute = "none"
if zoneShape not in ['circle','square']:
print("\n***WARNING: improper zone shape defined. Defaulting to square.***")
zoneShape='square'
zoneTarget=theDict['target']
if zoneTarget not in ['all','plants','seeds']:
print("\n***WARNING: improper zone target defined. Defaulting to all.***")
zoneTarget='all'
killThese=[]
if zoneShape=='circle':
for theObject in theGarden.soil:
if theObject.isSeed:
r=theObject.radiusSeed
else:
r=theObject.radiusStem
theResult=geometry_utils.checkOverlap(theObject.x, theObject.y, r, zoneX, zoneY, zoneSize)
if theResult>0 and aKey=='Killzone':
#if (zoneSpecies == 'all') or (theObject.nameSpecies == zoneSpecies):
#student requested addition to accept list of species. 0323-2023
#NOTE: this section is exact code in non-circle. Can be abstracted out into its own routine probably
if (zoneSpecies == 'all') or (theObject.nameSpecies in zoneSpecies):
if zoneTarget=='all' or (theObject.isSeed and zoneTarget=='seeds') or (not theObject.isSeed and zoneTarget=='plants'):
#Adding in ability to have chance of what happens in a zone
#STH 2026-0908
theChance=random.random()
if theChance<=zonePercent:
#this means the object is a target
if zoneSelAttribute != "none":
#check whether the provided attribute is present on the object(plant/seed)
if not hasattr(theObject, zoneSelAttribute):
print("***WARNING: attribute does not exist on targeted species")
print("***Ignoring selection criteria")
zoneSelAttribute = "none"
else:
#print(getattr(theObject, zoneSelAttribute))
if zoneSelLogic in ['=', '==']:
if getattr(theObject, zoneSelAttribute) == zoneSelValue:
#print("equal to")
killThese.append(theObject)
if zoneSelLogic == '<':
if getattr(theObject, zoneSelAttribute) < zoneSelValue:
#print("less than")
killThese.append(theObject)
if zoneSelLogic in '>':
if getattr(theObject, zoneSelAttribute) > zoneSelValue:
#print("greater than")
killThese.append(theObject)
else:
#print("it will die")
killThese.append(theObject)
elif aKey=='Safezone':
if theResult==0:
killThese.append(theObject)
elif theResult>0:
if (theObject.isSeed and zoneTarget=='plants') or (not theObject.isSeed and zoneTarget=='seeds'):
killThese.append(theObject)
else:
for theObject in theGarden.soil:
objectX=theObject.x
objectY=theObject.y
theResult=geometry_utils.pointInsideSquare(zoneX, zoneY, zoneSize, objectX, objectY)
if theResult>0 and aKey=='Killzone':
#if (zoneSpecies == 'all') or (theObject.nameSpecies == zoneSpecies):
#student requested addition to accept list of species. 0323-2023
#NOTE: this section is exact code in circle. Can be abstracted out into its own routine probably
if (zoneSpecies == 'all') or (theObject.nameSpecies in zoneSpecies):
if zoneTarget=='all' or (theObject.isSeed and zoneTarget=='seeds') or (not theObject.isSeed and zoneTarget=='plants'):
#Adding in ability to have chance of what happens in a zone
#STH 2026-0908
theChance=random.random()
if theChance<=zonePercent:
#this means the object is a target
if zoneSelAttribute != "none":
#check whether the provided attribute is present on the object(plant/seed)
if not hasattr(theObject, zoneSelAttribute):
print("***WARNING: attribute does not exist on targeted species")
print("***Ignoring selection criteria")
zoneSelAttribute = "none"
else:
#print(getattr(theObject, zoneSelAttribute))
if zoneSelLogic in ['=', '==']:
if getattr(theObject, zoneSelAttribute) == zoneSelValue:
#print("equal to")
killThese.append(theObject)
if zoneSelLogic == '<':
if getattr(theObject, zoneSelAttribute) < zoneSelValue:
#print("less than")
killThese.append(theObject)
if zoneSelLogic in '>':
if getattr(theObject, zoneSelAttribute) > zoneSelValue:
#print("greater than")
killThese.append(theObject)
else:
#print("it will die")
killThese.append(theObject)
elif aKey=='Safezone':
if theResult==0:
killThese.append(theObject)
elif theResult>0:
if (theObject.isSeed and zoneTarget=='plants') or (not theObject.isSeed and zoneTarget=='seeds'):
killThese.append(theObject)
for theObject in killThese:
theObject.causeOfDeath="killzone"
theGarden.kill(theObject)
elif aKey=="Seed":
if debug==1: print("debug: A seeding related event has been triggered.") #
theDict=aItem[aKey][0] #
seedingInfo=theDict.keys() #
for infoItem in seedingInfo:
if infoItem=="number" and not seedPlacement=="fromFile": addPopulationSize=theDict[infoItem]
if infoItem=="species_file":
sList=theDict[infoItem]
if sList=="random": sList=[]
if infoItem=="placement": seedPlacement=theDict[infoItem]
if seedPlacement=="hexagon": seedPlacement="hex" #just make sure it is consistant
if os.path.isfile(seedPlacement):
if debug == 1: print("debug: Confirming placement file exists....")
theFile=open(seedPlacement)
try:
sList=theFile.readlines()
finally:
theFile.close()
sList=checkSeedPlacementList(sList)
addPopulationSize=len(sList)
seedPlacement="fromFile"
if debug ==1: print("debug: Will place seeds from a file")
###if a simulation is being reloaded from a pickle, that sim might not have saved
###data on a new species being introduced. Load the new species and add it to the platonic list
###so it can be added to theGarden
for j in sList:
jj=j[0]
#has_key was depreciated and removed from python 3
#STH 2026-0908
# if (sys.version_info.major)==2:
# if not theGarden.platonicSeeds.has_key(jj):
# speciesIsMissing==True
# else:
# if not jj in theGarden.platonicSeeds:
# speciesIsMissing==True
if not jj in theGarden.platonicSeeds:
speciesIsMissing==True
if speciesIsMissing==True:
if debug == 1: print("debug: Desired species missing from loaded simulation")
if debug == 1: print("debug: Adding species %s" % (jj))
theSeed=Species1()
fileLoc= "Species/"+jj
theSeed.importPrefs(fileLoc)
theSeed.name="Platonic %s" % jj
theGarden.platonicSeeds[jj]=theSeed
if not seedPlacement=="fromFile" and not sList==[]:
newList=[]
for j in range(addPopulationSize):
newList.append(sList)
###if a simulation is being reloaded from a pickle, that sim might not have saved
###data on a new species being introduced. Load the new species and add it to the platonic list
###so it can be added to theGarden
sList=newList
newList=list(set(sList))
for j in newList:
# if not theGarden.platonicSeeds.has_key(j):
#has_key was depreciated and removed from python 3
#STH 2026-0908
if not j in theGarden.platonicSeeds:
#print ("adding %s" % (j))
theSeed=Species1()
fileLoc= "Species/"+j
theSeed.importPrefs(fileLoc)
theSeed.name="Platonic %s" % j
theGarden.platonicSeeds[j]=theSeed
theGarden.placeSeed(seedPlacement, sList, addPopulationSize, useDefaultYml, ymlList)
################
#if there is a terrain file and water level then the initial placement of seeds
#should be checked to see if any of them are below water
#STH 0328-2021
if(theGarden.terrainImage != []):
theGarden.checkSubmergedMortality()
################
sList= [] #reset the sList to what it was when we started
elif aKey=="Region":
if debug: print("debug: Region event detected...")
theDict=aItem[aKey][0]
regionAttrs=theDict.keys()
theRegionName=str(theDict['name'])
if debug: print("debug: Region %s event detected." % (theRegionName))
regionNames=[]
for i in theGarden.theRegions:
regionNames.append(i.name)
if theRegionName in regionNames:
for j in theGarden.theRegions:
if j.name==theRegionName:
theRegion=j
break
for aAttr in regionAttrs:
if not getattr(theRegion,aAttr,"does not exist")==theDict[aAttr]:
if debug: print("debug: Region %s has had a change in one or more attributes." % (theRegionName))
updatePlants=True
break
#if (not theRegion.size==theDict["size"]) or (not theRegion.x==theDict["x"]) or (not theRegion.y==theDict["y"]) or (not theRegion.shape==theDict["shape"]):