library("dismo")
## Loading required package: raster
## Warning: package 'raster' was built under R version 4.0.5
## Loading required package: sp
library(EcoHydRology)
## Loading required package: operators
## 
## Attaching package: 'operators'
## The following objects are masked from 'package:base':
## 
##     options, strrep
## Loading required package: topmodel
## Loading required package: DEoptim
## Warning: package 'DEoptim' was built under R version 4.0.4
## Loading required package: parallel
## 
## DEoptim package
## Differential Evolution algorithm in R
## Authors: D. Ardia, K. Mullen, B. Peterson and J. Ulrich
## Loading required package: XML
## Warning: package 'XML' was built under R version 4.0.5
path<- "~/geoserver/data_dir/rasters/worldclim"
tmin<-getData(name = "worldclim", var = "tmin", res = 10, path=path)
tmax<-getData(name = "worldclim", var = "tmax", res = 10, path=path)
prec<-getData(name = "worldclim", var = "prec", res = 10, path=path)
clim<-stack(tmin,tmax,prec)
clim <- brick(clim)
names(clim) <- c(paste("mint", 1:12, sep = ""), paste("maxt", 1:12, sep = ""), 
    paste("prec", 1:12, sep = ""))
lat_rad <- coordinates(clim)[, 2] * pi/180
for (i in 1:12) {
    evap <- raster(clim, 1)
    Tmax <- values(subset(clim, i + 12))/10
    Tmin <- values(subset(clim, i))/10
    d <- data.frame(day = (30 * i) - 15, Tmin, Tmax, lat_rad)
    d[is.na(d)] <- 0
    Es_PET <- PET_fromTemp(Jday = d$day, Tmax_C = d$Tmax, Tmin_C = d$Tmin, lat_radians = d$lat_rad) * 
        1000
    values(evap) <- Es_PET
    if (i == 1) {
        PET <<- brick(evap)
    }
    if (i > 1) {
        PET <<- addLayer(PET, evap)
    }
}
## Warning in if ((abs(lat) > pi/2 & latUnits == "unknown") | latUnits ==
## "degrees") {: the condition has length > 1 and only the first element will be
## used
## Warning in acos(-tan(dec) * tan(lat)): NaNs produced
## Warning in acos(tan(dec) * tan(lat)): NaNs produced
## Warning in if ((abs(lat) > pi/2 & latUnits == "unknown") | latUnits ==
## "degrees") {: the condition has length > 1 and only the first element will be
## used
## Warning in acos(-tan(dec) * tan(lat)): NaNs produced
## Warning in acos(tan(dec) * tan(lat)): NaNs produced
## Warning in if ((abs(lat) > pi/2 & latUnits == "unknown") | latUnits ==
## "degrees") {: the condition has length > 1 and only the first element will be
## used
## Warning in acos(-tan(dec) * tan(lat)): NaNs produced
## Warning in acos(tan(dec) * tan(lat)): NaNs produced
## [1] "Warning, Tn larger than Tx and generated NAs in the following rows:"
## [1] 476310 476311 478469 478470 480629 480630 482789
## Warning in if ((abs(lat) > pi/2 & latUnits == "unknown") | latUnits ==
## "degrees") {: the condition has length > 1 and only the first element will be
## used
## [1] "Warning, Tn larger than Tx and generated NAs in the following rows:"
## [1] 476310 476311 478469 478470 480629 480630 482789
## Warning in acos(-tan(dec) * tan(lat)): NaNs produced

## Warning in acos(-tan(dec) * tan(lat)): NaNs produced
## [1] "Warning, Tn larger than Tx and generated NAs in the following rows:"
## [1] 1032905
## Warning in if ((abs(lat) > pi/2 & latUnits == "unknown") | latUnits ==
## "degrees") {: the condition has length > 1 and only the first element will be
## used
## [1] "Warning, Tn larger than Tx and generated NAs in the following rows:"
## [1] 1032905
## Warning in acos(-tan(dec) * tan(lat)): NaNs produced

## Warning in acos(-tan(dec) * tan(lat)): NaNs produced
## [1] "Warning, Tn larger than Tx and generated NAs in the following rows:"
## [1]  927032 1032905
## Warning in if ((abs(lat) > pi/2 & latUnits == "unknown") | latUnits ==
## "degrees") {: the condition has length > 1 and only the first element will be
## used
## [1] "Warning, Tn larger than Tx and generated NAs in the following rows:"
## [1]  927032 1032905
## Warning in acos(-tan(dec) * tan(lat)): NaNs produced

## Warning in acos(-tan(dec) * tan(lat)): NaNs produced
## [1] "Warning, Tn larger than Tx and generated NAs in the following rows:"
## [1] 1032905
## Warning in if ((abs(lat) > pi/2 & latUnits == "unknown") | latUnits ==
## "degrees") {: the condition has length > 1 and only the first element will be
## used
## [1] "Warning, Tn larger than Tx and generated NAs in the following rows:"
## [1] 1032905
## Warning in acos(-tan(dec) * tan(lat)): NaNs produced

## Warning in acos(-tan(dec) * tan(lat)): NaNs produced
## Warning in if ((abs(lat) > pi/2 & latUnits == "unknown") | latUnits ==
## "degrees") {: the condition has length > 1 and only the first element will be
## used
## Warning in acos(-tan(dec) * tan(lat)): NaNs produced
## Warning in acos(tan(dec) * tan(lat)): NaNs produced
## Warning in if ((abs(lat) > pi/2 & latUnits == "unknown") | latUnits ==
## "degrees") {: the condition has length > 1 and only the first element will be
## used
## Warning in acos(-tan(dec) * tan(lat)): NaNs produced
## Warning in acos(tan(dec) * tan(lat)): NaNs produced
## [1] "Warning, Tn larger than Tx and generated NAs in the following rows:"
##    [1] 208204 208208 208212 208213 208216 210361 210363 210364 210365 210366
##   [11] 210367 210368 210369 210370 210371 210372 210373 210374 210375 210376
##   [21] 210377 210379 210380 210381 210385 210387 212519 212521 212522 212523
##   [31] 212524 212525 212526 212527 212528 212529 212530 212531 212532 212533
##   [41] 212534 212535 212536 212537 212538 212539 212540 212541 212542 212543
##   [51] 212544 212545 212546 212547 212548 212549 212550 212552 214676 214677
##   [61] 214679 214680 214681 214682 214683 214684 214685 214686 214687 214688
##   [71] 214689 214690 214691 214692 214693 214694 214695 214696 214697 214698
##   [81] 214699 214700 214701 214702 214703 214704 214705 214706 214707 214708
##   [91] 214709 214710 214711 214712 214713 214715 214716 214720 214723 214724
##  [101] 216834 216835 216837 216838 216839 216840 216841 216842 216843 216844
##  [111] 216845 216846 216847 216848 216849 216850 216851 216852 216853 216854
##  [121] 216855 216856 216857 216858 216859 216860 216861 216862 216863 216864
##  [131] 216865 216866 216867 216868 216869 216870 216871 216872 216873 216874
##  [141] 216875 216876 216877 216878 216879 216880 216881 216882 216883 216884
##  [151] 216885 216886 216887 216888 218992 218993 218994 218995 218996 218997
##  [161] 218998 218999 219000 219001 219002 219003 219004 219005 219006 219007
##  [171] 219008 219009 219010 219011 219012 219013 219014 219015 219016 219017
##  [181] 219018 219019 219020 219021 219022 219023 219024 219025 219026 219027
##  [191] 219028 219029 219030 219031 219032 219033 219034 219035 219036 219037
##  [201] 219038 219039 219040 219041 219042 219043 219044 219045 219046 219047
##  [211] 219048 219049 219050 221150 221152 221153 221154 221155 221156 221157
##  [221] 221158 221159 221160 221161 221162 221163 221164 221165 221166 221167
##  [231] 221168 221169 221170 221171 221172 221173 221174 221175 221176 221177
##  [241] 221178 221179 221180 221181 221182 221183 221184 221185 221186 221187
##  [251] 221188 221189 221190 221191 221192 221193 221194 221195 221196 221197
##  [261] 221198 221199 221200 221201 221202 221203 221204 221205 221206 221207
##  [271] 221208 221209 223309 223310 223312 223313 223314 223315 223316 223317
##  [281] 223318 223319 223320 223321 223322 223323 223324 223325 223326 223327
##  [291] 223328 223329 223330 223331 223332 223333 223334 223335 223336 223337
##  [301] 223338 223339 223340 223341 223342 223343 223344 223345 223346 223347
##  [311] 223348 223349 223350 223351 223352 223353 223354 223355 223356 223357
##  [321] 223358 223359 223360 223361 223362 223363 223364 223365 223366 223367
##  [331] 223368 223369 225470 225472 225473 225474 225475 225476 225477 225478
##  [341] 225479 225480 225481 225482 225483 225484 225485 225486 225487 225488
##  [351] 225489 225490 225491 225492 225493 225494 225495 225496 225497 225498
##  [361] 225499 225500 225501 225502 225503 225504 225505 225506 225507 225508
##  [371] 225509 225510 225511 225512 225513 225514 225515 225516 225517 225518
##  [381] 225519 225520 225521 225522 225523 225524 225525 225526 225527 225528
##  [391] 227637 227638 227639 227640 227641 227642 227643 227644 227645 227646
##  [401] 227647 227648 227649 227650 227651 227652 227653 227654 227655 227656
##  [411] 227657 227658 227659 227660 227661 227662 227663 227664 227665 227666
##  [421] 227667 227668 227669 227670 227671 227672 227673 227674 227675 227676
##  [431] 227677 227678 227679 227680 227681 227682 227683 227684 227685 227686
##  [441] 229798 229799 229800 229801 229802 229803 229804 229805 229806 229807
##  [451] 229808 229809 229810 229811 229812 229813 229814 229815 229816 229817
##  [461] 229818 229819 229820 229821 229822 229823 229824 229825 229826 229827
##  [471] 229828 229829 229830 229831 229832 229833 229834 229835 229836 229837
##  [481] 229838 229839 229840 229841 229842 231961 231962 231963 231964 231965
##  [491] 231966 231967 231968 231969 231970 231971 231972 231973 231974 231975
##  [501] 231976 231977 231978 231979 231980 231981 231982 231983 231984 231985
##  [511] 231986 231987 231988 231989 231990 231991 231992 231993 231994 231995
##  [521] 231996 231997 231998 231999 232000 232001 232002 232003 234122 234123
##  [531] 234124 234125 234126 234127 234128 234129 234130 234131 234132 234133
##  [541] 234134 234135 234136 234137 234138 234139 234140 234141 234142 234143
##  [551] 234144 234145 234146 234147 234148 234149 234150 234151 234152 234153
##  [561] 234154 234155 234156 234157 234158 234159 234160 234161 234162 236281
##  [571] 236283 236284 236285 236286 236287 236288 236289 236290 236291 236292
##  [581] 236293 236294 236295 236296 236297 236298 236299 236300 236301 236302
##  [591] 236303 236304 236305 236306 236307 236308 236309 236310 236311 236312
##  [601] 236313 236314 236315 236316 236317 236318 236319 236320 236321 236322
##  [611] 236323 236324 238442 238444 238445 238446 238447 238448 238449 238450
##  [621] 238451 238452 238453 238454 238455 238456 238457 238458 238459 238460
##  [631] 238461 238462 238463 238464 238465 238466 238467 238468 238469 238470
##  [641] 238471 238472 238473 238474 238475 238476 238477 238478 238479 238480
##  [651] 238481 238482 238483 238484 238485 238486 238487 238488 238489 240601
##  [661] 240603 240604 240605 240606 240607 240608 240609 240610 240611 240612
##  [671] 240613 240614 240615 240616 240617 240618 240619 240620 240621 240622
##  [681] 240623 240624 240625 240626 240627 240628 240629 240630 240631 240632
##  [691] 240633 240634 240635 240636 240637 240638 240639 240640 240641 240642
##  [701] 240643 240644 240645 240646 240647 240648 240649 240650 240651 240652
##  [711] 240653 242762 242763 242764 242765 242766 242767 242768 242769 242770
##  [721] 242771 242772 242773 242774 242775 242776 242777 242778 242779 242780
##  [731] 242781 242782 242783 242784 242785 242786 242787 242788 242789 242790
##  [741] 242791 242792 242793 242794 242795 242796 242797 242798 242799 242800
##  [751] 242801 242802 242803 242804 242805 242806 242807 242808 242809 242810
##  [761] 242811 242812 242813 244921 244923 244924 244925 244926 244927 244928
##  [771] 244929 244930 244931 244932 244933 244934 244935 244936 244937 244938
##  [781] 244939 244940 244941 244942 244943 244944 244945 244946 244947 244948
##  [791] 244949 244950 244951 244952 244953 244954 244955 244956 244957 244958
##  [801] 244959 244960 244961 244962 244963 244964 244965 244966 244967 244968
##  [811] 244969 244970 244971 244972 244973 247082 247083 247084 247085 247086
##  [821] 247087 247088 247089 247090 247091 247092 247093 247094 247095 247096
##  [831] 247097 247098 247099 247100 247101 247102 247103 247104 247105 247106
##  [841] 247107 247108 247109 247110 247111 247112 247113 247114 247115 247116
##  [851] 247117 247118 247119 247120 247121 247122 247123 247124 247125 247126
##  [861] 247127 247128 247129 247130 247131 247132 249244 249245 249246 249247
##  [871] 249248 249249 249250 249251 249252 249253 249254 249255 249256 249257
##  [881] 249258 249259 249260 249261 249262 249263 249264 249265 249266 249267
##  [891] 249268 249269 249270 249271 249272 249273 249274 249275 249276 249277
##  [901] 249278 249279 249280 249281 249282 249283 249284 249285 249286 249287
##  [911] 249288 249289 249290 249291 251404 251405 251406 251407 251408 251409
##  [921] 251410 251411 251412 251413 251414 251415 251416 251417 251418 251419
##  [931] 251420 251421 251422 251423 251424 251425 251426 251427 251428 251429
##  [941] 251430 251431 251432 251433 251434 251435 251436 251437 251438 251439
##  [951] 251440 251441 251442 251443 251444 251445 251446 251447 251448 251449
##  [961] 251450 253563 253564 253565 253566 253567 253568 253569 253570 253571
##  [971] 253572 253573 253574 253575 253576 253577 253578 253579 253580 253581
##  [981] 253582 253583 253584 253585 253586 253587 253588 253589 253590 253591
##  [991] 253592 253593 253594 253595 253596 253597 253598 253599 253600 253601
## [1001] 253602 253603 253604 253605 253606 253607 253608 253609 253610 255723
## [1011] 255724 255725 255726 255727 255728 255729 255730 255731 255732 255733
## [1021] 255734 255735 255736 255737 255738 255739 255740 255741 255742 255743
## [1031] 255744 255745 255746 255747 255748 255749 255750 255751 255752 255753
## [1041] 255754 255755 255756 255757 255758 255759 255760 255761 255762 255763
## [1051] 255764 255765 255766 255767 255768 255769 255770 257884 257885 257886
## [1061] 257887 257888 257889 257890 257891 257892 257893 257894 257895 257896
## [1071] 257897 257898 257899 257900 257901 257902 257903 257904 257905 257906
## [1081] 257907 257908 257909 257910 257911 257912 257913 257914 257915 257916
## [1091] 257917 257918 257919 257920 257921 257922 257923 257924 257925 257926
## [1101] 257927 257928 257929 257930 257931 257932 260045 260046 260047 260048
## [1111] 260049 260050 260051 260052 260053 260054 260055 260056 260057 260058
## [1121] 260059 260060 260061 260062 260063 260064 260065 260066 260067 260068
## [1131] 260069 260070 260071 260072 260073 260074 260075 260076 260077 260078
## [1141] 260079 260080 260081 260082 260083 260084 260085 260086 260087 260088
## [1151] 260089 260090 260091 260092 260093 262205 262206 262207 262208 262209
## [1161] 262210 262211 262212 262213 262214 262215 262216 262217 262218 262219
## [1171] 262220 262221 262222 262223 262224 262225 262226 262227 262228 262229
## [1181] 262230 262231 262232 262233 262234 262235 262236 262237 262238 262239
## [1191] 262240 262241 262242 262243 262244 262245 262246 262247 262248 262249
## [1201] 262250 262251 262252 262253 262254 262255 262256 264366 264367 264368
## [1211] 264369 264370 264371 264372 264373 264374 264375 264376 264377 264378
## [1221] 264379 264380 264381 264382 264383 264384 264385 264386 264387 264388
## [1231] 264389 264390 264391 264392 264393 264394 264395 264396 264397 264398
## [1241] 264399 264400 264401 264402 264403 264404 264405 264406 264407 264408
## [1251] 264409 264410 264411 264412 264413 264414 264415 266524 266525 266526
## [1261] 266527 266528 266529 266530 266531 266532 266533 266534 266535 266536
## [1271] 266537 266538 266539 266540 266541 266542 266543 266544 266545 266546
## [1281] 266547 266548 266549 266550 266551 266552 266553 266554 266555 266556
## [1291] 266557 266558 266559 266560 266561 266562 266563 266564 266565 266566
## [1301] 266567 266568 266569 266570 268684 268685 268686 268687 268688 268689
## [1311] 268690 268691 268692 268693 268694 268695 268696 268697 268698 268699
## [1321] 268700 268701 268702 268703 268704 268705 268706 268707 268708 268709
## [1331] 268710 268711 268722 268724 268725 268726 268727 268728 268729 268730
## [1341] 268731 270845 270846 270847 270848 270849 270850 270851 270852 270853
## [1351] 270854 270855 270856 270857 270858 270859 270860 270861 270862 270863
## [1361] 270864 270865 270866 270867 270868 270869 270888 270889 270890 270891
## [1371] 270892 270893 270894 270895 273004 273005 273006 273007 273008 273009
## [1381] 273010 273011 273012 273013 273014 273015 273016 273017 273018 273019
## [1391] 273020 273021 273022 273023 273024 273025 273026 273027 273028 273049
## [1401] 273051 273052 273053 273054 273055 275165 275166 275167 275168 275169
## [1411] 275170 275171 275172 275173 275174 275175 275176 275177 275178 275179
## [1421] 275180 275181 275182 275183 275184 275185 275186 275187 275188 277325
## [1431] 277326 277327 277328 277329 277330 277331 277332 277333 277334 277335
## [1441] 277336 277337 277338 277339 277340 277341 277342 277343 277344 277345
## [1451] 277346 277347 277348 279485 279486 279487 279488 279489 279490 279491
## [1461] 279492 279493 279494 279495 279496 279497 279498 279499 279500 279501
## [1471] 279502 279503 279504 279505 279506 279511 279512 281649 281650 281651
## [1481] 281652 281653 281654 281655 281656 281657 281658 281659 281660 281661
## [1491] 281662 281663 281664 281665 281671 283808 283809 283810 283812 283813
## [1501] 283814 283815 283816 283817 283818 283819 283820 283821 283822 283823
## [1511] 283824 285971 285972 285973 285974 285975 285976 285977 285978 285979
## [1521] 285980 285981 285982 285983 285984 288132 288133 288134 288135 288136
## [1531] 288137 288138 288139 288140 288141 288142 288143 290297 290298 290299
## [1541] 290300 290301 290302 290303 292458 292459 292460 292461 292462 292463
## [1551] 292464 292465 294618 294619 294620 294621 294622 294623 294624
## Warning in if ((abs(lat) > pi/2 & latUnits == "unknown") | latUnits ==
## "degrees") {: the condition has length > 1 and only the first element will be
## used
## [1] "Warning, Tn larger than Tx and generated NAs in the following rows:"
##    [1] 208204 208208 208212 208213 208216 210361 210363 210364 210365 210366
##   [11] 210367 210368 210369 210370 210371 210372 210373 210374 210375 210376
##   [21] 210377 210379 210380 210381 210385 210387 212519 212521 212522 212523
##   [31] 212524 212525 212526 212527 212528 212529 212530 212531 212532 212533
##   [41] 212534 212535 212536 212537 212538 212539 212540 212541 212542 212543
##   [51] 212544 212545 212546 212547 212548 212549 212550 212552 214676 214677
##   [61] 214679 214680 214681 214682 214683 214684 214685 214686 214687 214688
##   [71] 214689 214690 214691 214692 214693 214694 214695 214696 214697 214698
##   [81] 214699 214700 214701 214702 214703 214704 214705 214706 214707 214708
##   [91] 214709 214710 214711 214712 214713 214715 214716 214720 214723 214724
##  [101] 216834 216835 216837 216838 216839 216840 216841 216842 216843 216844
##  [111] 216845 216846 216847 216848 216849 216850 216851 216852 216853 216854
##  [121] 216855 216856 216857 216858 216859 216860 216861 216862 216863 216864
##  [131] 216865 216866 216867 216868 216869 216870 216871 216872 216873 216874
##  [141] 216875 216876 216877 216878 216879 216880 216881 216882 216883 216884
##  [151] 216885 216886 216887 216888 218992 218993 218994 218995 218996 218997
##  [161] 218998 218999 219000 219001 219002 219003 219004 219005 219006 219007
##  [171] 219008 219009 219010 219011 219012 219013 219014 219015 219016 219017
##  [181] 219018 219019 219020 219021 219022 219023 219024 219025 219026 219027
##  [191] 219028 219029 219030 219031 219032 219033 219034 219035 219036 219037
##  [201] 219038 219039 219040 219041 219042 219043 219044 219045 219046 219047
##  [211] 219048 219049 219050 221150 221152 221153 221154 221155 221156 221157
##  [221] 221158 221159 221160 221161 221162 221163 221164 221165 221166 221167
##  [231] 221168 221169 221170 221171 221172 221173 221174 221175 221176 221177
##  [241] 221178 221179 221180 221181 221182 221183 221184 221185 221186 221187
##  [251] 221188 221189 221190 221191 221192 221193 221194 221195 221196 221197
##  [261] 221198 221199 221200 221201 221202 221203 221204 221205 221206 221207
##  [271] 221208 221209 223309 223310 223312 223313 223314 223315 223316 223317
##  [281] 223318 223319 223320 223321 223322 223323 223324 223325 223326 223327
##  [291] 223328 223329 223330 223331 223332 223333 223334 223335 223336 223337
##  [301] 223338 223339 223340 223341 223342 223343 223344 223345 223346 223347
##  [311] 223348 223349 223350 223351 223352 223353 223354 223355 223356 223357
##  [321] 223358 223359 223360 223361 223362 223363 223364 223365 223366 223367
##  [331] 223368 223369 225470 225472 225473 225474 225475 225476 225477 225478
##  [341] 225479 225480 225481 225482 225483 225484 225485 225486 225487 225488
##  [351] 225489 225490 225491 225492 225493 225494 225495 225496 225497 225498
##  [361] 225499 225500 225501 225502 225503 225504 225505 225506 225507 225508
##  [371] 225509 225510 225511 225512 225513 225514 225515 225516 225517 225518
##  [381] 225519 225520 225521 225522 225523 225524 225525 225526 225527 225528
##  [391] 227637 227638 227639 227640 227641 227642 227643 227644 227645 227646
##  [401] 227647 227648 227649 227650 227651 227652 227653 227654 227655 227656
##  [411] 227657 227658 227659 227660 227661 227662 227663 227664 227665 227666
##  [421] 227667 227668 227669 227670 227671 227672 227673 227674 227675 227676
##  [431] 227677 227678 227679 227680 227681 227682 227683 227684 227685 227686
##  [441] 229798 229799 229800 229801 229802 229803 229804 229805 229806 229807
##  [451] 229808 229809 229810 229811 229812 229813 229814 229815 229816 229817
##  [461] 229818 229819 229820 229821 229822 229823 229824 229825 229826 229827
##  [471] 229828 229829 229830 229831 229832 229833 229834 229835 229836 229837
##  [481] 229838 229839 229840 229841 229842 231961 231962 231963 231964 231965
##  [491] 231966 231967 231968 231969 231970 231971 231972 231973 231974 231975
##  [501] 231976 231977 231978 231979 231980 231981 231982 231983 231984 231985
##  [511] 231986 231987 231988 231989 231990 231991 231992 231993 231994 231995
##  [521] 231996 231997 231998 231999 232000 232001 232002 232003 234122 234123
##  [531] 234124 234125 234126 234127 234128 234129 234130 234131 234132 234133
##  [541] 234134 234135 234136 234137 234138 234139 234140 234141 234142 234143
##  [551] 234144 234145 234146 234147 234148 234149 234150 234151 234152 234153
##  [561] 234154 234155 234156 234157 234158 234159 234160 234161 234162 236281
##  [571] 236283 236284 236285 236286 236287 236288 236289 236290 236291 236292
##  [581] 236293 236294 236295 236296 236297 236298 236299 236300 236301 236302
##  [591] 236303 236304 236305 236306 236307 236308 236309 236310 236311 236312
##  [601] 236313 236314 236315 236316 236317 236318 236319 236320 236321 236322
##  [611] 236323 236324 238442 238444 238445 238446 238447 238448 238449 238450
##  [621] 238451 238452 238453 238454 238455 238456 238457 238458 238459 238460
##  [631] 238461 238462 238463 238464 238465 238466 238467 238468 238469 238470
##  [641] 238471 238472 238473 238474 238475 238476 238477 238478 238479 238480
##  [651] 238481 238482 238483 238484 238485 238486 238487 238488 238489 240601
##  [661] 240603 240604 240605 240606 240607 240608 240609 240610 240611 240612
##  [671] 240613 240614 240615 240616 240617 240618 240619 240620 240621 240622
##  [681] 240623 240624 240625 240626 240627 240628 240629 240630 240631 240632
##  [691] 240633 240634 240635 240636 240637 240638 240639 240640 240641 240642
##  [701] 240643 240644 240645 240646 240647 240648 240649 240650 240651 240652
##  [711] 240653 242762 242763 242764 242765 242766 242767 242768 242769 242770
##  [721] 242771 242772 242773 242774 242775 242776 242777 242778 242779 242780
##  [731] 242781 242782 242783 242784 242785 242786 242787 242788 242789 242790
##  [741] 242791 242792 242793 242794 242795 242796 242797 242798 242799 242800
##  [751] 242801 242802 242803 242804 242805 242806 242807 242808 242809 242810
##  [761] 242811 242812 242813 244921 244923 244924 244925 244926 244927 244928
##  [771] 244929 244930 244931 244932 244933 244934 244935 244936 244937 244938
##  [781] 244939 244940 244941 244942 244943 244944 244945 244946 244947 244948
##  [791] 244949 244950 244951 244952 244953 244954 244955 244956 244957 244958
##  [801] 244959 244960 244961 244962 244963 244964 244965 244966 244967 244968
##  [811] 244969 244970 244971 244972 244973 247082 247083 247084 247085 247086
##  [821] 247087 247088 247089 247090 247091 247092 247093 247094 247095 247096
##  [831] 247097 247098 247099 247100 247101 247102 247103 247104 247105 247106
##  [841] 247107 247108 247109 247110 247111 247112 247113 247114 247115 247116
##  [851] 247117 247118 247119 247120 247121 247122 247123 247124 247125 247126
##  [861] 247127 247128 247129 247130 247131 247132 249244 249245 249246 249247
##  [871] 249248 249249 249250 249251 249252 249253 249254 249255 249256 249257
##  [881] 249258 249259 249260 249261 249262 249263 249264 249265 249266 249267
##  [891] 249268 249269 249270 249271 249272 249273 249274 249275 249276 249277
##  [901] 249278 249279 249280 249281 249282 249283 249284 249285 249286 249287
##  [911] 249288 249289 249290 249291 251404 251405 251406 251407 251408 251409
##  [921] 251410 251411 251412 251413 251414 251415 251416 251417 251418 251419
##  [931] 251420 251421 251422 251423 251424 251425 251426 251427 251428 251429
##  [941] 251430 251431 251432 251433 251434 251435 251436 251437 251438 251439
##  [951] 251440 251441 251442 251443 251444 251445 251446 251447 251448 251449
##  [961] 251450 253563 253564 253565 253566 253567 253568 253569 253570 253571
##  [971] 253572 253573 253574 253575 253576 253577 253578 253579 253580 253581
##  [981] 253582 253583 253584 253585 253586 253587 253588 253589 253590 253591
##  [991] 253592 253593 253594 253595 253596 253597 253598 253599 253600 253601
## [1001] 253602 253603 253604 253605 253606 253607 253608 253609 253610 255723
## [1011] 255724 255725 255726 255727 255728 255729 255730 255731 255732 255733
## [1021] 255734 255735 255736 255737 255738 255739 255740 255741 255742 255743
## [1031] 255744 255745 255746 255747 255748 255749 255750 255751 255752 255753
## [1041] 255754 255755 255756 255757 255758 255759 255760 255761 255762 255763
## [1051] 255764 255765 255766 255767 255768 255769 255770 257884 257885 257886
## [1061] 257887 257888 257889 257890 257891 257892 257893 257894 257895 257896
## [1071] 257897 257898 257899 257900 257901 257902 257903 257904 257905 257906
## [1081] 257907 257908 257909 257910 257911 257912 257913 257914 257915 257916
## [1091] 257917 257918 257919 257920 257921 257922 257923 257924 257925 257926
## [1101] 257927 257928 257929 257930 257931 257932 260045 260046 260047 260048
## [1111] 260049 260050 260051 260052 260053 260054 260055 260056 260057 260058
## [1121] 260059 260060 260061 260062 260063 260064 260065 260066 260067 260068
## [1131] 260069 260070 260071 260072 260073 260074 260075 260076 260077 260078
## [1141] 260079 260080 260081 260082 260083 260084 260085 260086 260087 260088
## [1151] 260089 260090 260091 260092 260093 262205 262206 262207 262208 262209
## [1161] 262210 262211 262212 262213 262214 262215 262216 262217 262218 262219
## [1171] 262220 262221 262222 262223 262224 262225 262226 262227 262228 262229
## [1181] 262230 262231 262232 262233 262234 262235 262236 262237 262238 262239
## [1191] 262240 262241 262242 262243 262244 262245 262246 262247 262248 262249
## [1201] 262250 262251 262252 262253 262254 262255 262256 264366 264367 264368
## [1211] 264369 264370 264371 264372 264373 264374 264375 264376 264377 264378
## [1221] 264379 264380 264381 264382 264383 264384 264385 264386 264387 264388
## [1231] 264389 264390 264391 264392 264393 264394 264395 264396 264397 264398
## [1241] 264399 264400 264401 264402 264403 264404 264405 264406 264407 264408
## [1251] 264409 264410 264411 264412 264413 264414 264415 266524 266525 266526
## [1261] 266527 266528 266529 266530 266531 266532 266533 266534 266535 266536
## [1271] 266537 266538 266539 266540 266541 266542 266543 266544 266545 266546
## [1281] 266547 266548 266549 266550 266551 266552 266553 266554 266555 266556
## [1291] 266557 266558 266559 266560 266561 266562 266563 266564 266565 266566
## [1301] 266567 266568 266569 266570 268684 268685 268686 268687 268688 268689
## [1311] 268690 268691 268692 268693 268694 268695 268696 268697 268698 268699
## [1321] 268700 268701 268702 268703 268704 268705 268706 268707 268708 268709
## [1331] 268710 268711 268722 268724 268725 268726 268727 268728 268729 268730
## [1341] 268731 270845 270846 270847 270848 270849 270850 270851 270852 270853
## [1351] 270854 270855 270856 270857 270858 270859 270860 270861 270862 270863
## [1361] 270864 270865 270866 270867 270868 270869 270888 270889 270890 270891
## [1371] 270892 270893 270894 270895 273004 273005 273006 273007 273008 273009
## [1381] 273010 273011 273012 273013 273014 273015 273016 273017 273018 273019
## [1391] 273020 273021 273022 273023 273024 273025 273026 273027 273028 273049
## [1401] 273051 273052 273053 273054 273055 275165 275166 275167 275168 275169
## [1411] 275170 275171 275172 275173 275174 275175 275176 275177 275178 275179
## [1421] 275180 275181 275182 275183 275184 275185 275186 275187 275188 277325
## [1431] 277326 277327 277328 277329 277330 277331 277332 277333 277334 277335
## [1441] 277336 277337 277338 277339 277340 277341 277342 277343 277344 277345
## [1451] 277346 277347 277348 279485 279486 279487 279488 279489 279490 279491
## [1461] 279492 279493 279494 279495 279496 279497 279498 279499 279500 279501
## [1471] 279502 279503 279504 279505 279506 279511 279512 281649 281650 281651
## [1481] 281652 281653 281654 281655 281656 281657 281658 281659 281660 281661
## [1491] 281662 281663 281664 281665 281671 283808 283809 283810 283812 283813
## [1501] 283814 283815 283816 283817 283818 283819 283820 283821 283822 283823
## [1511] 283824 285971 285972 285973 285974 285975 285976 285977 285978 285979
## [1521] 285980 285981 285982 285983 285984 288132 288133 288134 288135 288136
## [1531] 288137 288138 288139 288140 288141 288142 288143 290297 290298 290299
## [1541] 290300 290301 290302 290303 292458 292459 292460 292461 292462 292463
## [1551] 292464 292465 294618 294619 294620 294621 294622 294623 294624
## Warning in acos(-tan(dec) * tan(lat)): NaNs produced

## Warning in acos(-tan(dec) * tan(lat)): NaNs produced
## Warning in if ((abs(lat) > pi/2 & latUnits == "unknown") | latUnits ==
## "degrees") {: the condition has length > 1 and only the first element will be
## used
## Warning in acos(-tan(dec) * tan(lat)): NaNs produced
## Warning in acos(tan(dec) * tan(lat)): NaNs produced
## Warning in if ((abs(lat) > pi/2 & latUnits == "unknown") | latUnits ==
## "degrees") {: the condition has length > 1 and only the first element will be
## used
## Warning in acos(-tan(dec) * tan(lat)): NaNs produced
## Warning in acos(tan(dec) * tan(lat)): NaNs produced
months <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", 
    "Nov", "Dec")
names(PET) <- months
plot(PET)

Bucket <- raster(PET, 1)

png("world_aet%02d.png", height = 800, width = 900)
for (n in 1:2) {
    for (i in 1:359) {
        mn <- 1 + i%/%30
        NewAET <- raster(PET, 1)
        NewBucket <- values(Bucket)
        rain <- values(subset(clim, 24 + mn))/30
        alpha <- (NewBucket - 200)/300
        evap <- values(subset(PET, mn)) * alpha * 0.8  ##A fudge factor for stomatal control.
        NewBucket <- NewBucket + (rain) - evap
        NewBucket[NewBucket > 500] <- 500
        NewBucket[NewBucket < 200] <- 200
        values(Bucket) <- NewBucket
        values(NewAET) <- evap * (NewBucket > 200)
        if (n > 1 && (i%%30) - 15 == 0) {
            print(plot(NewAET, main = months[mn]))
            if (mn == 1) {
                AET <<- brick(NewAET)
            }
            if (mn > 1) {
                AET <<- addLayer(AET, NewAET)
            }
        }
    }
}
## NULL
## NULL
## NULL
## NULL
## NULL
## NULL
## NULL
## NULL
## NULL
## NULL
## NULL
## NULL
dev.off()
## png 
##   2
plot(AET[[1]],ylim=c(-60,60))

writeRaster(AET,"world_AET.tif")
system("convert -delay 100 world*.png world_aet.gif")