fork download
  1. <?php
  2. $page_title = 'Run LOME selectivity analysis';
  3. require('../directoryinfo.php');
  4. include('../connect.php');
  5. include('functions.php');
  6. require_once('../header.html');
  7. ?>
  8.  
  9. <!-- Form For Executing Code -->
  10. <form action="index.php" method="post" name="file_select">
  11. <fieldset>
  12.  
  13. <table align="left" cellspacing=15>
  14. <tr><td colspan="2"><input type="checkbox" checked name="go" value="yes"><STRONG>Run Full Analysis</STRONG></td></tr>
  15. <tr><td colspan=2><input type="submit" name="submit" value="Submit"></td></tr>
  16. </table>
  17. </fieldset>
  18. </form>
  19.  
  20. <?php
  21. error_reporting(E_ALL);
  22.  
  23. if(isset($_POST['go'])) {
  24. $t0 = microtime(true);
  25.  
  26. $carb_colors = array('blue','blue dark');
  27. $clast_colors = array('yellow','orange','tan','gray dark','gray light','gray');
  28.  
  29. //////////////
  30. //
  31. // Time scale saved as time_scale.csv
  32. //
  33. //////////////
  34. $q_time_scale = "SELECT interval_name, age_bottom, age_top, intervals.id FROM intervals
  35. WHERE interval_name IN ('Whiterock', 'Chazyan', 'Blackriverian', 'Shermanian', 'Richmondian', 'Hirnantian', 'Rhuddanian', 'Aeronian', 'Telychian', 'Wenlock')
  36. ORDER BY age_bottom";
  37. $time_scale = mysqli_query($connect_macrostrat, $q_time_scale);
  38. $n_bin = mysqli_num_rows($time_scale);
  39.  
  40. $time_scale_file = "time_scale.csv";
  41. $fh = fopen($time_scale_file, 'w') or die("can't open file");
  42. //chmod($time_scale_file, 0666);
  43. $file_row = "interval_name, age_bottom, age_top, id\n";
  44. fwrite($fh, $file_row);
  45.  
  46. while($row = mysqli_fetch_assoc($time_scale)) {
  47. if($row['interval_name'] == 'Blackriverian') {
  48. $file_row = "Blackriveran-Kirkfield,".$row['age_bottom'].",457.0000,".$row['id']."\n";
  49. $int_name[] = 'Blackriveran-Kirkfield';
  50. $int_bottom[] = $row['age_bottom'];
  51. $int_top[] = 457.0000;
  52. } else if($row['interval_name'] == 'Shermanian') {
  53. $file_row = "Shermanian-Maysvillian,".$row['age_bottom'].",450.0000,".$row['id']."\n";
  54. $int_name[] = 'Shermanian-Maysvillian';
  55. $int_bottom[] = $row['age_bottom'];
  56. $int_top[] = 450.0000;
  57. } else {
  58. $file_row = $row['interval_name'].",".$row['age_bottom'].",".$row['age_top'].",".$row['id']."\n";
  59. $int_name[] = $row['interval_name'];
  60. $int_bottom[] = $row['age_bottom'];
  61. $int_top[] = $row['age_top'];
  62. }
  63. $int_id[] = $row['id'];
  64. fwrite($fh, $file_row);
  65. }
  66. fclose($fh);
  67. echo 'File <a href="'.$time_scale_file.'">'.$time_scale_file.'</a> saved<BR>';
  68. mysqli_free_result($time_scale);
  69. $n_int = count($int_name);
  70.  
  71. $base_ordovician = 488.3;
  72. $study_max = max($int_bottom);
  73. $study_min = min($int_top);
  74.  
  75.  
  76. //////////////
  77. //
  78. // Column data saved as columns.csv
  79. //
  80. //////////////
  81. $cols_file = "columns.csv";
  82. $fh = fopen($cols_file, 'w') or die("can't open file");
  83. chmod($cols_file, 0777);
  84. $file_row = "col_id, lat, lng, sheet, col, col_area, project_id\n";
  85. fwrite($fh, $file_row);
  86.  
  87. $q = "SELECT cols.id, lat, lng, project_id, col_group, col, col_area FROM cols
  88. INNER JOIN col_groups ON col_groups.id=col_group_id
  89. INNER JOIN projects ON projects.id=project_id
  90. WHERE project = 'North America' AND col_group != 'Mexico' AND cols.id NOT IN (34, 111, 139, 218, 586, 587) AND status_code = 'active'"; //echo $q."<br>";
  91.  
  92. //(456,443,633,499)
  93. $columns = mysqli_query($connect_macrostrat, $q);
  94. while($row = mysqli_fetch_assoc($columns)) {
  95. $file_row = $row['id'].",".$row['lat'].",".$row['lng'].",".$row['col_group'].",".$row['col'].",".$row['col_area'].",".$row['project_id']."\n";
  96. fwrite($fh, $file_row);
  97. $my_cols[] = $row['id'];
  98. }
  99. fclose($fh);
  100. mysqli_free_result($columns);
  101. echo 'File <a href="'.$cols_file.'">'.$cols_file.'</a> saved<BR>';
  102.  
  103. // get column polygons
  104. $poly_file = "column_polygons.csv";
  105. $fh = fopen($poly_file, 'w') or die("can't open file");
  106. chmod($poly_file, 0777);
  107. $file_row = "col_id, lat, lng\n";
  108. fwrite($fh, $file_row);
  109.  
  110. $q = "SELECT col_id, AsText(col_area) coords FROM col_areas
  111. WHERE col_id IN (".implode(",", $my_cols).")";
  112. $columns = mysqli_query($connect_macrostrat, $q);
  113.  
  114. while($row = mysqli_fetch_assoc($columns)) {
  115. $my_coord = ltrim($row['coords'], 'POLYGON((');
  116. $my_coord = rtrim($my_coord, '))');
  117. $my_coord = explode(",", $my_coord);
  118.  
  119. for($i=0; $i<count($my_coord); ++$i) {
  120. $temp_coord = explode(" ", $my_coord[$i]);
  121. $file_row = $row['col_id'].",".$temp_coord[0].",".$temp_coord[1]."\n";
  122. fwrite($fh, $file_row);
  123. }
  124. }
  125. fclose($fh);
  126. mysqli_free_result($columns);
  127. echo 'File <a href="'.$poly_file.'">'.$poly_file.'</a> saved<BR>';
  128.  
  129. // get obselete columns to exclude
  130. $obsolete_cols = array();
  131. $q = "SELECT cols.id FROM cols
  132. INNER JOIN col_groups ON col_groups.id=col_group_id
  133. INNER JOIN projects ON projects.id=project_id
  134. WHERE project = 'North America' AND status_code = 'obsolete'"; //echo $q."<br>";
  135. $columns = mysqli_query($connect_macrostrat, $q);
  136. while($row = mysqli_fetch_assoc($columns)) {
  137. $obsolete_cols[] = $row['id'];
  138. }
  139. mysqli_free_result($columns);
  140.  
  141.  
  142.  
  143. //////////////
  144. //
  145. // get packages
  146. //
  147. //////////////
  148. $counter = 0;
  149. $temp_pkg = split_packages('marine');
  150.  
  151. // set package crossers
  152. $pkg_file = "ordovician_packages.csv";
  153. $fh = fopen($pkg_file, 'w') or die("can't open file");
  154. chmod($pkg_file, 0777);
  155. $file_row = "package_id, age_bottom, age_top, col_id\n";
  156. fwrite($fh, $file_row);
  157.  
  158. for($i=0; $i<count($temp_pkg); ++$i) {
  159. $q = "SELECT col_id, max(b.age_bottom) bottom, min(t.age_top) top, count(distinct units.id) n_unit, FO_h FROM units
  160. INNER JOIN intervals b ON b.id=FO
  161. INNER JOIN intervals t ON t.id=LO
  162. WHERE units.id IN ($temp_pkg[$i])";
  163. $result=mysqli_query($connect_macrostrat, $q);
  164. $row=mysqli_fetch_assoc($result);
  165. if(in_array($row['col_id'], $my_cols)===TRUE && $row['bottom']>$study_min && $row['top']<$study_max) {
  166. $my_pkg[$counter] = $temp_pkg[$i];
  167. $my_pkg_fo[$counter] = $row['bottom'];
  168. $my_pkg_lo[$counter] = $row['top'];
  169. $my_pkg_n_units[$counter] = $row['n_unit'];
  170. $my_pkg_col_id[$counter] = $row['col_id'];
  171.  
  172. $temp_units = explode(",",$temp_pkg[$i]);
  173. for($j=0; $j<count($temp_units); ++$j) $unit_packages[$temp_units[$j]]=$counter; // unit_id is key, package number is value
  174.  
  175. // get fo_h and lo_h for packages
  176. $q = "SELECT units.id, FO_h, (b.age_bottom-b.age_top)/2 half_duration, b.age_bottom FROM units
  177. INNER JOIN intervals b ON b.id=FO
  178. WHERE units.id IN ($temp_pkg[$i])
  179. ORDER BY b.age_bottom DESC, FO_h
  180. LIMIT 1";
  181. $fo_result = mysqli_query($connect_macrostrat, $q);
  182. $fo_row = mysqli_fetch_assoc($fo_result);
  183. $my_temp_fo = $fo_row['FO_h'];
  184. mysqli_free_result($fo_result);
  185.  
  186. $q = "SELECT units.id, LO_h, (t.age_bottom-t.age_top)/2 half_duration, t.age_top FROM units
  187. INNER JOIN intervals t ON t.id=LO
  188. WHERE units.id IN ($temp_pkg[$i])
  189. ORDER BY t.age_top, FIELD(LO_h,0,6,5,4,3,2,1)
  190. LIMIT 1"; //echo $q."<br>";
  191. $lo_result = mysqli_query($connect_macrostrat, $q);
  192. $lo_row = mysqli_fetch_assoc($lo_result);
  193. $my_temp_lo = $lo_row['LO_h'];
  194. mysqli_free_result($lo_result);
  195.  
  196. if($my_temp_fo==0) $my_pkg_fo_h[$counter] = $fo_row['age_bottom'];
  197. else $my_pkg_fo_h[$counter] = $fo_row['age_bottom'] - $fo_row['half_duration'];
  198.  
  199. if($my_temp_lo==0) $my_pkg_lo_h[$counter] = $lo_row['age_top'];
  200. else $my_pkg_lo_h[$counter] = $lo_row['age_top'] + $lo_row['half_duration'];
  201.  
  202. $file_row=$counter.",".$fo_row['age_bottom'].",".$lo_row['age_top'].",".$row['col_id']."\n";
  203. // $file_row=$counter.",".$my_pkg_fo_h[$counter].",".$my_pkg_lo_h[$counter]."\n";
  204. fwrite($fh, $file_row);
  205.  
  206. ++$counter;
  207. }
  208.  
  209. // get packages for testing gaps
  210. if(in_array($row['col_id'], $my_cols)===TRUE) {
  211. // get fo_h and lo_h for packages
  212. $q = "SELECT units.id, FO_h, (b.age_bottom-b.age_top)/2 half_duration, b.age_bottom FROM units
  213. INNER JOIN intervals b ON b.id=FO
  214. INNER JOIN intervals t ON t.id=LO
  215. WHERE units.id IN ($temp_pkg[$i])
  216. ORDER BY b.age_bottom DESC, FO_h, t.age_top DESC, field(LO_h, 1,2,3,4,5,6,0)
  217. LIMIT 1";
  218. $fo_result = mysqli_query($connect_macrostrat, $q);
  219. $fo_row = mysqli_fetch_assoc($fo_result);
  220. if($fo_row['FO_h']==0) $my_gap_fo_h[] = $fo_row['age_bottom'];
  221. else $my_gap_fo_h[] = $fo_row['age_bottom']-$fo_row['half_duration'];
  222. if($fo_row['age_bottom']!=$row['bottom']) echo "bad base calc<br>";
  223. mysqli_free_result($fo_result);
  224.  
  225. $my_gap_pkg[] = $temp_pkg[$i];
  226. $my_gap_fo[] = $row['bottom'];
  227. $my_gap_col_id[] = $row['col_id'];
  228. }
  229. }
  230. $all_units = implode(",",$my_pkg);
  231. $all_units_array = explode(",", $all_units);
  232. $total_units = implode(",",$my_gap_pkg);
  233. unset($temp_units, $temp_pkg, $counter);
  234. echo "There are ".(count(explode(",",$total_units)))." units.<br><br>";
  235. fclose($fh);
  236. echo 'File <a href="'.$pkg_file.'">'.$pkg_file.'</a> saved<BR>';
  237.  
  238. /*
  239. // calculate package crossers
  240. $crosser_file = 'package_crossers.csv';
  241. $fh = fopen($crosser_file,'w');
  242. chmod($crosser_file, 0666);
  243. fclose($fh);
  244.  
  245. $file_name_r = 'temp.r';
  246. $fh = fopen($file_name_r, 'w') or die("can't open file");
  247. chmod($file_name_r, 0666);
  248. $r_script = "
  249. source('myFunctions.r')
  250. time.scale <- read.csv(file='$time_scale_file', header=TRUE)
  251. packages <- read.csv(file='$pkg_file', header=TRUE)
  252.  
  253. cross.temp <- crossers(packages[,2:3], time.scale[,2:3])
  254. write.csv(cbind(time.scale[,c(4,1:3)],cross.temp), file='$crosser_file', quote=FALSE, row.names=FALSE)
  255. ";
  256. fwrite($fh, $r_script);
  257. fclose($fh);
  258. $cmd = "echo 'rscript <- \"$file_name_r\"; source(rscript)' | " . "/usr/bin/R --slave 2>&1";
  259. exec($cmd, $r_ran);
  260. if(count($r_ran)>0) {
  261. echo "R (Package Crossers): ";
  262. print_r($r_ran); echo "<br>";
  263. }
  264. echo 'File <a href="'.$crosser_file.'">'.$crosser_file.'</a> saved<BR>';
  265.  
  266.  
  267.  
  268. //////////////
  269. //
  270. // calculate environmental gaps for every unit
  271. //
  272. //////////////
  273. $env_gap = array();
  274. $env_unit_top = array();
  275. $all_unit_array = explode(",",$all_units);
  276. for($i=0; $i<count($all_unit_array); ++$i) {
  277. // get information for focal unit
  278. $q = "SELECT units.id, color, col_id, t.age_bottom top_bottom, t.age_top, LO_h, LO, FO_h, b.age_bottom FROM units
  279. INNER JOIN intervals b ON b.id=FO
  280. INNER JOIN intervals t ON t.id=LO
  281. WHERE units.id=".$all_unit_array[$i];
  282. $result = mysqli_query($connect_macrostrat, $q);
  283. $focal_unit = mysqli_fetch_assoc($result);
  284. mysqli_free_result($result);
  285. if($focal_unit['LO_h'] == 0) {
  286. $gap_bottom = $focal_unit['age_top'];
  287. } else {
  288. $gap_bottom = $focal_unit['age_top'] + ($focal_unit['top_bottom']-$focal_unit['age_top'])/2;
  289. }
  290.  
  291. // get information on next youngest unit with same color
  292. $q = "SELECT units.id, b.age_bottom, b.age_top bottom_top, FO_h, FO, color, t.age_top, LO_h FROM units
  293. INNER JOIN intervals b ON b.id=FO
  294. INNER JOIN intervals t ON t.id=LO
  295. WHERE col_id=".$focal_unit['col_id']." AND units.id IN (".$total_units.") AND units.id != ".$focal_unit['id']."
  296. AND ((FO_h!=0 AND b.age_bottom-(b.age_bottom-b.age_top)/2 <=".$gap_bottom.") OR (FO_h=0 AND b.age_bottom<=".$gap_bottom."))
  297. AND color='".$focal_unit['color']."'
  298. ORDER BY b.age_bottom DESC, FO_h, t.age_top DESC, field(LO_h, 1,2,3,4,5,6,0)
  299. LIMIT 1"; //echo $q."<br>";
  300. $result = mysqli_query($connect_macrostrat, $q);
  301. if(mysqli_num_rows($result)==1) {
  302. $test_unit = mysqli_fetch_assoc($result);
  303. if($test_unit['FO_h'] == 0) {
  304. $gap_top = $test_unit['age_bottom'];
  305. } else {
  306. $gap_top = $test_unit['age_bottom'] - ($test_unit['age_bottom']-$test_unit['bottom_top'])/2;
  307. }
  308. } else {
  309. $gap_top = 0;
  310. }
  311. mysqli_free_result($result);
  312.  
  313. if(round($gap_bottom,5)==round($gap_top,5)) {
  314. $env_gap[$all_unit_array[$i]]=0;
  315. } else {
  316. $env_gap[$all_unit_array[$i]] = log($gap_bottom-$gap_top);
  317. }
  318. if($gap_bottom<$gap_top) {
  319. echo $focal_unit['id']." (".$test_unit['id']."): ".$gap_bottom." - ".$gap_top." = ".$env_gap[$all_unit_array[$i]]."<br>";
  320. }
  321. $env_unit_top[$all_unit_array[$i]] = $gap_bottom;
  322. }
  323. // echo "environmental gap:<br>";print_r($env_gap); echo "<br><br>";
  324.  
  325.  
  326. //////////////
  327. //
  328. // get duration of gaps above packages
  329. //
  330. //////////////
  331. for($i=0; $i<count($my_pkg); ++$i) {
  332. $temp_max=0;
  333. $temp_fo=0;
  334. for($j=0; $j<count($my_gap_pkg); ++$j) {
  335. if($my_gap_pkg[$j]!=$my_pkg[$i] && $my_pkg_col_id[$i]==$my_gap_col_id[$j] && $my_pkg_lo[$i] >= $my_gap_fo[$j] && $my_gap_fo[$j]>$temp_max) {
  336. $temp_max = $my_gap_fo[$j];
  337. }
  338. if($my_gap_pkg[$j]!=$my_pkg[$i] && $my_pkg_col_id[$i]==$my_gap_col_id[$j] && $my_pkg_lo_h[$i] >= $my_gap_fo_h[$j] && $my_gap_fo_h[$j]>$temp_fo) {
  339. $temp_fo = $my_gap_fo_h[$j];
  340. }
  341. }
  342. if($my_pkg_lo[$i] != $temp_max) {
  343. $my_pkg_gap[$i] = log($my_pkg_lo[$i] - $temp_max);
  344. } else {
  345. $my_pkg_gap[$i] = 0;
  346. }
  347.  
  348. if(round($my_pkg_lo_h[$i],5) != round($temp_fo,5)) {
  349. $my_pkg_gap2[$i] = log($my_pkg_lo_h[$i] - $temp_fo);
  350. } else {
  351. $my_pkg_gap2[$i] = 0;
  352. }
  353. }
  354.  
  355. */
  356. //////////////
  357. //
  358. // get matched ordovician genera and references
  359. //
  360. //////////////
  361.  
  362. // get all appropriate taxa
  363. $my_genera = array();
  364. $this_genus = array();
  365. $my_genus_age = array();
  366. $my_occurrences = array();
  367.  
  368. $q = "SELECT occurrences_temp2.taxon_no, genus_name, class, taxon_order, family, min(ta.top_age) pbdb_min,
  369. occurrences_temp2.taxon_environment, occurrences_temp2.locomotion, occurrences_temp2.life_habit, occurrences_temp2.diet1, occurrences_temp2.diet2
  370. FROM occurrences_temp2
  371. INNER JOIN intervals b ON b.id=occurrences_temp2.FO
  372. INNER JOIN intervals t ON t.id=occurrences_temp2.LO
  373. INNER JOIN pbdb.interval_lookup ba ON ba.interval_no=occurrences_temp2.max_interval_no
  374. INNER JOIN pbdb.interval_lookup ta ON ta.interval_no=occurrences_temp2.min_interval_no
  375. LEFT OUTER JOIN pbdb.ecotaph ON ecotaph.taxon_no=occurrences_temp2.taxon_no
  376. LEFT OUTER JOIN pbdb.authorities ON authorities.taxon_no=occurrences_temp2.taxon_no
  377. WHERE occurrences_temp2.unit_id > 0 AND occurrences_temp2.unit_id IN (".$all_units.") AND (occurrences_temp2.extant = 'no' OR occurrences_temp2.extant IS NULL)
  378. AND occurrences_temp2.col_id NOT IN (".implode(",",$obsolete_cols).")
  379. AND ((b.age_bottom > ".$study_min." AND t.age_top < ".$study_max.") OR ((b.age_bottom IS NULL AND t.age_top IS NULL) AND (ba.base_age > ".$study_min." AND ta.top_age < ".$study_max.")))
  380. AND (occurrences_temp2.release_date <= now() OR occurrences_temp2.release_date IS NULL)
  381. AND (form_taxon = 'no' OR form_taxon IS NULL) AND (preservation != 'trace' OR preservation IS NULL)
  382. GROUP BY occurrences_temp2.taxon_no"; //echo $q."<br><br>";
  383. $result = mysqli_query($connect_macrostrat, $q);
  384. echo "There are ".mysqli_num_rows($result)." queried genera<br>";
  385. while($row=mysqli_fetch_assoc($result)) {
  386. $my_genera[] = $row;
  387. $this_genus[] = $row['taxon_no'];
  388. $my_genus_age[$row['taxon_no']]['pbdb_min'] = $row['pbdb_min'];
  389. }
  390.  
  391. // get global and NoAm ranges of taxa - even outside study interval
  392.  
  393. $q = "SELECT occurrence_no, taxon_no, ba.base_age, ta.top_age, FO_h, LO_h, unit_id,
  394. b.age_bottom-((b.age_bottom-b.age_top)/2*LEAST(1,FO_h)) bottom, t.age_bottom+((t.age_bottom-t.age_top)/2*LEAST(1,LO_h)) top,
  395. unit_id, col_id, occurrences_temp2.collection_no,
  396. original_taxon_no, original_taxon_rank, paleolat, paleolng
  397. FROM occurrences_temp2
  398. INNER JOIN pbdb.interval_lookup ba ON ba.interval_no=occurrences_temp2.max_interval_no
  399. INNER JOIN pbdb.interval_lookup ta ON ta.interval_no=occurrences_temp2.min_interval_no
  400. LEFT OUTER JOIN intervals b ON b.id=FO
  401. LEFT OUTER JOIN intervals t ON t.id=LO
  402. WHERE taxon_no IN (".implode(",", $this_genus).")
  403. AND (occurrences_temp2.unit_id=0 OR occurrences_temp2.unit_id IN (".$all_units."))
  404. AND (occurrences_temp2.col_id IS NULL OR occurrences_temp2.col_id NOT IN (".implode(",",$obsolete_cols)."))"; //echo $q."<br><br>";
  405. $result = mysqli_query($connect_macrostrat, $q);
  406. echo "There are ".mysqli_num_rows($result)." queried occurrences<br>";
  407. $counter = 0;
  408. $int_occurrences = array();
  409. $int_paleolat = array();
  410. while($row=mysqli_fetch_assoc($result)) {
  411. if($row['unit_id']==0 || in_array($row['unit_id'], $all_units_array)===FALSE) { // not matched to north america
  412. $occurrence_bottom = $row['base_age'];
  413. $occurrence_top = $row['top_age'];
  414. } else {
  415. if($row['base_age']>$row['top'] && $row['top_age']<$row['bottom']) {// pbdb and unit ages overlap: use youngest bottom and oldest top
  416. $occurrence_bottom = min($row['base_age'], $row['bottom']);
  417. $occurrence_top = max($row['top_age'], $row['top']);
  418. } else { // pbdb and unit ages DO NOT overlap: use macrostrat
  419. $occurrence_bottom = $row['bottom'];
  420. $occurrence_top = $row['top'];
  421. }
  422. }
  423.  
  424. $my_occurrences[] = array(
  425. "occurrence_no" => $row['occurrence_no'],
  426. "taxon_no" => $row['taxon_no'],
  427. "bottom" => $occurrence_bottom,
  428. "top" => $occurrence_top,
  429. "unit_id" => $row['unit_id'],
  430. "col_id" => $row['col_id'],
  431. "collection_no" => $row['collection_no'],
  432. "original_taxon_no" => $row['original_taxon_no'],
  433. "original_taxon_rank" => $row['original_taxon_rank'],
  434. "paleolat" => $row['paleolat'],
  435. "paleolng" => $row['paleolng']
  436. );
  437. // get occurrence interval combinations
  438. for($i=0; $i<$n_int; ++$i) {
  439. if($occurrence_bottom>$int_top[$i] && $occurrence_top<$int_bottom[$i]) $int_occurrences[$i][] = $counter;
  440. if($occurrence_bottom>$int_top[$i] && $occurrence_top<$study_max && is_numeric($row['paleolat'])) $int_paleolat[$i][$row['taxon_no']][] = $row['paleolat'];
  441. }
  442. ++$counter;
  443.  
  444. if(isset($my_genus_age[$row['taxon_no']]['global_max'])===FALSE) $my_genus_age[$row['taxon_no']]['global_max'] = $occurrence_bottom;
  445. else if($my_genus_age[$row['taxon_no']]['global_max'] < $row['base_age']) $my_genus_age[$row['taxon_no']]['global_max'] = $occurrence_bottom;
  446.  
  447. if(isset($my_genus_age[$row['taxon_no']]['global_min'])===FALSE) $my_genus_age[$row['taxon_no']]['global_min'] = $occurrence_top;
  448. else if($my_genus_age[$row['taxon_no']]['global_min'] > $row['top_age']) $my_genus_age[$row['taxon_no']]['global_min'] = $occurrence_top;
  449.  
  450. if($row['unit_id']>0 && in_array($row['unit_id'], $all_units_array) && (($row['top'] < 443.7 && $my_genus_age[$row['taxon_no']]['pbdb_min'] < 443.7) || $row['top'] >= 443.7)) {
  451. if(isset($my_genus_age[$row['taxon_no']]['noam_max'])===FALSE) $my_genus_age[$row['taxon_no']]['noam_max'] = $occurrence_bottom;
  452. else if($my_genus_age[$row['taxon_no']]['noam_max'] < $row['base_age']) $my_genus_age[$row['taxon_no']]['noam_max'] = $occurrence_bottom;
  453.  
  454. if(isset($my_genus_age[$row['taxon_no']]['noam_min'])===FALSE) $my_genus_age[$row['taxon_no']]['noam_min'] = $occurrence_top;
  455. else if($my_genus_age[$row['taxon_no']]['noam_min'] > $row['top_age']) $my_genus_age[$row['taxon_no']]['noam_min'] = $occurrence_top;
  456. }
  457. }
  458.  
  459.  
  460. // $pbdb_file = "ordovician_genera.csv";
  461. $pbdb_file = "ordovician_genera_culled.csv";
  462. $fh = fopen($pbdb_file, 'w') or die("can't open file");
  463. chmod($pbdb_file, 0666);
  464. $file_row = "interval_id, interval_name, age_bottom, age_top, taxon_no, genus, class, order, family, life_habit, locomotion, taxon_environment, diet1, diet2, n_species_NoAm, n_species_global, n_collections_NoAm, n_occur_NoAm, global_endemic_to_interval, NoAm_FAD, NoAm_LAD, global_FAD, global_LAD, max_N_paleolat_during_prior, max_S_paleolat_during_prior, n_matched_units_greater_n_45, n_matched_units_greater_s_45, max_great_circle_NoAm, max_great_circle_Global, n_packages_occupied, n_packages_total, n_units_occupied, n_units_total,n_carb_units, n_carb_units_occupied, n_siliciclastic_units, n_siliciclastic_units_occupied, n_truncating_packages_total, n_truncating_packages_occupied, n_env_truncation, n_env_truncation_double, n_units_entirely_within_interval, min_gap, max_gap, mean_gap, median_gap, min_env_gap, max_env_gap, mean_env_gap, median_env_gap, n_env_gap, n_columns_occupied, n_columns_total, n_trunc_columns_occupied\n";
  465. fwrite($fh, $file_row);
  466.  
  467. $my_collections = array();
  468. for($i=0; $i < 1; ++$i) {
  469. // for($i=0; $i<$n_int; ++$i) {
  470. // get total units and packages
  471. $my_packages = array();
  472. $my_columns = array();
  473. $my_carb = array();
  474. $my_clast = array();
  475. $q = "SELECT units.id, color, col_id FROM units
  476. INNER JOIN intervals b ON b.id=FO
  477. INNER JOIN intervals t ON t.id=LO
  478. WHERE units.id IN (".$all_units.") AND b.age_bottom>$int_top[$i] AND t.age_top<$int_bottom[$i]";
  479. $result = mysqli_query($connect_macrostrat, $q);
  480. while($row = mysqli_fetch_assoc($result)) {
  481. $my_packages[] = $unit_packages[$row['id']];
  482. if(in_array($row['color'], $carb_colors)) $my_carb[] = $row['id'];
  483. if(in_array($row['color'], $clast_colors)) $my_clast[] = $row['id'];
  484. $my_columns[] = $row['col_id'];
  485. }
  486. $n_units_total = mysqli_num_rows($result);
  487. $n_packages_total = count(array_unique($my_packages));
  488. $n_columns_total = count(array_unique($my_columns));
  489. unset($my_packages, $my_columns);
  490.  
  491. // loop through each package and count truncations
  492. $truncating_packages = array();
  493. $spanning_packages = array();
  494. for($k=0; $k<count($my_pkg); ++$k) {
  495. // the packages that truncate in this interval
  496. if($my_pkg_lo[$k]<$int_bottom[$i] && $my_pkg_lo[$k]>=$int_top[$i]) {
  497. $truncating_packages[] = $k;
  498. // non-truncating packages
  499. } else if($my_pkg_lo[$k]<$int_bottom[$i] && $my_pkg_fo[$k]>$int_top[$i]) {
  500. $spanning_packages[] = $k;
  501. }
  502. }
  503.  
  504. // loop through each occurrence in this interval and get those that occur in this interval
  505. $interval_genus = array();
  506. for($k=0; $k<count($int_occurrences[$i]); ++$k) {
  507. $interval_genus[$my_occurrences[$k]['taxon_no']][] = $my_occurrences[$k];
  508. }
  509. $this_genus = array_keys($interval_genus);
  510.  
  511. // for($j=0; $j < 100; ++$j) {
  512. for($j=0; $j<count($interval_genus); ++$j) {
  513.  
  514. $noam_occurrences = array();
  515. $noam_collections = array();
  516. $noam_units = array();
  517. $noam_columns = array();
  518. $noam_packages = array();
  519. $noam_species = array();
  520. $noam_paleolat = array();
  521. $noam_paleolng = array();
  522. $matched_unit_n_45 = array();
  523. $matched_unit_s_45 = array();
  524. $unit_paleolat = array();
  525. $unit_paleolng = array();
  526.  
  527. $global_occurrences = array();
  528. $global_collections = array();
  529. $global_species = array();
  530. $global_paleolat = array();
  531. $global_paleolng = array();
  532.  
  533. if($my_genus_age[$this_genus[$j]]['global_max']<=$int_bottom[$i] && $my_genus_age[$this_genus[$j]]['global_min']>$int_top[$i]) $global_endemic_to_interval = 1;
  534. else $global_endemic_to_interval = 0;
  535.  
  536. for($k=0; $k<count($interval_genus[$this_genus[$j]]); ++$k) {
  537. $row = $interval_genus[$this_genus[$j]][$k];
  538.  
  539. if($row['unit_id']==0 || in_array($row['unit_id'], $all_units_array)===FALSE || ($row['unit_id']>0 && ($my_genus_age[$row['taxon_no']]['pbdb_min'] < 443.7 && $row['top'] < 443.7) || $row['top'] >= 443.7)) {
  540. $global_occurrences[] = $row['occurrence_no'];
  541. $global_collections[] = $row['collection_no'];
  542. if(is_numeric($row['paleolat']) && is_numeric($row['paleolng'])) {
  543. $global_paleolat[] = $row['paleolat'];
  544. $global_paleolng[] = $row['paleolng'];
  545. }
  546. $my_collections[] = $row['collection_no'];
  547.  
  548. if($row['original_taxon_rank']=='species') $global_species[] = $row['original_taxon_no'];
  549.  
  550. if($row['unit_id']>0 && in_array($row['unit_id'], $all_units_array)) {
  551. $noam_occurrences[] = $row['occurrence_no'];
  552. $noam_collections[] = $row['collection_no'];
  553. $noam_units[] = $row['unit_id'];
  554. $noam_columns[] = $row['col_id'];
  555. if($row['original_taxon_rank']=='species') $noam_species[] = $row['original_taxon_no'];
  556. $noam_paleolat[] = $row['paleolat'];
  557. $noam_paleolng[] = $row['paleolng'];
  558. if($row['paleolat']>= 45) $matched_unit_n_45[] = $row['unit_id'];
  559. if($row['paleolat']<= -45) $matched_unit_s_45[] = $row['unit_id'];
  560.  
  561. if(is_numeric($row['paleolat']) && is_numeric($row['paleolng'])) {
  562. $unit_paleolat[$row['unit_id']][] = $row['paleolat']; //echo $row['paleolat']."<br>";
  563. $unit_paleolng[$row['unit_id']][] = $row['paleolng'];
  564. }
  565.  
  566. $noam_packages[] = $unit_packages[$row['unit_id']];
  567. }
  568. }
  569. }
  570. $n_occur = count(array_unique($noam_occurrences));
  571. $n_coll = count(array_unique($noam_collections));
  572. $noam_units = array_unique($noam_units);
  573. $n_units = count($noam_units);
  574.  
  575. if($n_units>0) {
  576. $noam_units = array_combine(range(0,$n_units-1,1), $noam_units);
  577. $n_cols = count(array_unique($noam_columns));
  578. $n_species = count(array_unique($noam_species));
  579. $n_packages = count(array_unique($noam_packages));
  580. // print_r($noam_columns); echo " - $n_cols<br><br>";
  581.  
  582. if(count($my_carb)>0) $n_carb_units_occupied = count(array_unique(array_intersect($my_carb, $noam_units)));
  583. else $n_carb_units_occupied = 0;
  584. if(count($my_clast)>0) $n_siliciclastic_units_occupied = count(array_unique(array_intersect($my_clast, $noam_units)));
  585. $n_siliciclastic_units_occupied = 0;
  586.  
  587. $n_occur_global = count(array_unique($global_occurrences));
  588. $n_coll_global = count(array_unique($global_collections));
  589. $n_species_global = count(array_unique($global_species));
  590. $n_matched_units_greater_n_45 = count(array_unique($matched_unit_n_45));
  591. $n_matched_units_greater_s_45 = count(array_unique($matched_unit_s_45));
  592.  
  593. $temp_taxon_no = array_keys($int_paleolat[$i]);
  594. $max_paleolat_N = 'NA';
  595. max_paleolat_S = 'NA';
  596. for($k=0; $k<count($int_paleolat[$i]); ++$k) {
  597. if($temp_taxon_no[$k] == $row['taxon_no']) {
  598. $max_paleolat_N = max($int_paleolat[$i][$row['taxon_no']]);
  599. $max_paleolat_S = min($int_paleolat[$i][$row['taxon_no']]);
  600. }
  601. }
  602.  
  603. // get matched great circle distance
  604. // set up file for sending to R for great circle analysis
  605. $circle_file = "noam_great_circle.csv";
  606. $fh2 = fopen($circle_file, 'w') or die("can't open file");
  607. chmod($circle_file, 0666);
  608. $file_row = "plat, plng, unit_id\n";
  609. fwrite($fh2, $file_row);
  610. for($k=0; $k<count($unit_paleolat); ++$k) {
  611. if(isset($unit_paleolat[$noam_units[$k]]) && isset($unit_paleolng[$noam_units[$k]])) {
  612. $unit_mean_lat = array_sum($unit_paleolat[$noam_units[$k]])/count($unit_paleolat[$noam_units[$k]]);
  613. $unit_mean_lng = array_sum($unit_paleolng[$noam_units[$k]])/count($unit_paleolng[$noam_units[$k]]);
  614. if(is_null($unit_mean_lat)===FALSE && is_null($unit_mean_lng)===FALSE) {
  615. $file_row="$unit_mean_lat,$unit_mean_lng,$noam_units[$k]\n";
  616. fwrite($fh2, $file_row);
  617. }
  618. } else {
  619. echo "paleo coords not properly set<br>";
  620. }
  621. }
  622. fclose($fh2);
  623.  
  624. if($n_units>1) {
  625. $file_name_r = 'temp.r';
  626. $fh3 = fopen($file_name_r, 'w') or die("can't open file");
  627. chmod($file_name_r, 0666);
  628. $r_script = "
  629. source('myFunctions.r')
  630. coords <- read.csv(file='$circle_file', header=TRUE)
  631. if(nrow(unique(coords[,1:2]))>1) {
  632. max.dist <- max(great.circle2(coords[,1:2]), na.rm=TRUE)
  633. } else {
  634. max.dist <- 'NA'
  635. }
  636. write.table(max.dist, file='temp_dist.csv', quote=FALSE, row.names=FALSE, col.names=FALSE, sep=',')
  637. ";
  638. fwrite($fh3, $r_script);
  639. fclose($fh3);
  640. $cmd = "echo 'rscript <- \"$file_name_r\"; source(rscript)' | " . "/usr/bin/R --slave 2>&1";
  641. exec($cmd, $r_ran);
  642. if(count($r_ran)>0) {
  643. echo "R (Matched great circle): ";
  644. print_r($r_ran); echo "<br>";
  645. }
  646. $handle = fopen("temp_dist.csv", "r");
  647. while ($row = fgetcsv($handle, 1000, ",")) $max_dist_noam = $row[0];
  648. fclose($handle);
  649. if($max_dist_noam=='NA') {
  650. $handle = fopen($circle_file, "r");
  651. echo $max_dist_noam."<br>";
  652. while ($file_row = fgetcsv($handle, 1000, ",")) {
  653. print_r($file_row); echo "<br>";
  654. }
  655. print_r($noam_units);echo "<br>";
  656. print_r($noam_collections);echo "<br><br>";
  657. fclose($handle);
  658. }
  659. } else {
  660. $max_dist_noam='NA';
  661. }
  662.  
  663. // get global great circle distance
  664. if(count($global_paleolat)>1 && count($global_paleolng)==count($global_paleolat)) {
  665. // set up file for sending to R for great circle analysis
  666. $circle_file = "global_great_circle.csv";
  667. $fh2 = fopen($circle_file, 'w') or die("can't open file");
  668. chmod($circle_file, 0666);
  669. $file_row = "plat, plng\n";
  670. fwrite($fh2, $file_row);
  671.  
  672. for($k=0; $k<count($global_paleolat); ++$k) {
  673. $file_row=$global_paleolat[$k].",".$global_paleolng[$k]."\n";
  674. fwrite($fh2, $file_row);
  675. }
  676. fclose($fh2);
  677.  
  678. $file_name_r = 'temp.r';
  679. $fh3 = fopen($file_name_r, 'w') or die("can't open file");
  680. chmod($file_name_r, 0666);
  681. $r_script = "
  682. source('myFunctions.r')
  683. coords <- read.csv(file='$circle_file', header=TRUE)
  684. if(nrow(unique(coords))>1) {
  685. max.dist <- max(great.circle2(coords), na.rm=TRUE)
  686. } else {
  687. max.dist <- 'NA'
  688. }
  689. write.table(max.dist, file='temp_dist.csv', quote=FALSE, row.names=FALSE, col.names=FALSE, sep=',')
  690. ";
  691. fwrite($fh3, $r_script);
  692. fclose($fh3);
  693. $cmd = "echo 'rscript <- \"$file_name_r\"; source(rscript)' | " . "/usr/bin/R --slave 2>&1";
  694. exec($cmd, $r_ran);
  695. if(count($r_ran)>0) {
  696. echo "R (Global great circle): ";
  697. print_r($r_ran); echo "<br>";
  698. }
  699. $handle = fopen("temp_dist.csv", "r");
  700. while ($row = fgetcsv($handle, 1000, ",")) $max_dist_global = $row[0];
  701. fclose($handle);
  702. } else {
  703. $max_dist_global = 'NA';
  704. }
  705.  
  706. $n_env_temp = array();
  707. $n_env_temp2 = array();
  708.  
  709. // get stratigraphic gap duration
  710. if($n_packages>0) {
  711. $pkg_occupied = array_combine(range(0,$n_packages-1), array_unique($noam_packages));
  712. $trunc_temp = array_intersect($truncating_packages, array_unique($noam_packages));
  713. $n_trunc_packages_occupied = count($trunc_temp);
  714. if($n_trunc_packages_occupied>0) {
  715. $trunc_temp = array_combine(range(0,$n_trunc_packages_occupied-1,1), $trunc_temp);
  716. // get n truncating columns
  717. $trunc_temp_columns = array();
  718. for($k=0; $k<$n_trunc_packages_occupied; ++$k) {
  719. $trunc_temp_columns[] = $my_pkg_col_id[$trunc_temp[$k]];
  720. }
  721. $n_trunc_columns_occupied = count(array_unique($trunc_temp_columns));
  722. } else {
  723. $n_trunc_columns_occupied = 0;
  724. }
  725.  
  726. if($n_trunc_packages_occupied>1) {
  727. $trunc_packages_occupied = array_combine(range(0,($n_trunc_packages_occupied-1),1), $trunc_temp);
  728. //print_r($trunc_temp); echo "<br>";
  729. //print_r($trunc_packages_occupied); echo "<br>";
  730.  
  731. $temp_gap = array();
  732. for($zz=0; $zz<$n_trunc_packages_occupied; ++$zz) {
  733. $temp_gap[] = $my_pkg_gap2[$trunc_packages_occupied[$zz]];
  734. }
  735.  
  736. sort($temp_gap);
  737. //print_r($temp_gap); echo "<br><br>";
  738. $max_gap = max($temp_gap);
  739. $min_gap = min($temp_gap);
  740. $mean_gap = array_sum($temp_gap)/$n_trunc_packages_occupied;
  741.  
  742. $h = intval($n_trunc_packages_occupied/2);
  743. if($n_trunc_packages_occupied % 2 == 0) {
  744. $median_gap = ($temp_gap[$h] + $temp_gap[$h-1])/2;
  745. } else {
  746. $median_gap = $temp_gap[$h];
  747. }
  748. } else if($n_trunc_packages_occupied==1) {
  749. $trunc_packages_occupied = array_combine(range(0,($n_trunc_packages_occupied-1),1), $trunc_temp); // returns package keys
  750. $max_gap = $my_pkg_gap2[$trunc_packages_occupied[0]];
  751. $min_gap = $max_gap;
  752. $mean_gap = $max_gap;
  753. $median_gap = $max_gap;
  754. } else {
  755. $max_gap = NULL;
  756. $min_gap = NULL;
  757. $mean_gap = NULL;
  758. $median_gap = NULL;
  759. }
  760.  
  761.  
  762. // get environmental truncation
  763. for($k=0; $k<$n_packages; ++$k) {
  764. // get unit of oldest occurrence in interval
  765. $q = "SELECT units.id, b.age_bottom, t.age_top, color FROM occurrences_temp2
  766. INNER JOIN units ON units.id=occurrences_temp2.unit_id
  767. INNER JOIN intervals b ON b.id=units.FO
  768. INNER JOIN intervals t ON t.id=units.LO
  769. WHERE taxon_no=".$this_genus[$j]." AND (occurrences_temp2.release_date <= now() OR occurrences_temp2.release_date IS NULL)
  770. AND units.id IN (".$my_pkg[$pkg_occupied[$k]].")
  771. AND b.age_bottom > ".$int_top[$i]." AND t.age_top < ".$int_bottom[$i]."
  772. GROUP BY units.id
  773. ORDER BY b.age_bottom DESC, units.FO_h, t.age_top DESC, field(units.LO_h, 1,2,3,4,5,0)
  774. LIMIT 1"; //echo $q."<br>";
  775. $result = mysqli_query($connect_macrostrat, $q);
  776. if(mysqli_num_rows($result)==1) {
  777. $max_unit = mysqli_fetch_assoc($result);
  778. $occupied_unit = $max_unit['id'];
  779. $occupied_bottom = $max_unit['age_bottom'];
  780. $occupied_top = $max_unit['age_top'];
  781. $occupied_color = $max_unit['color'];
  782. $next_unit_temp = NULL;
  783.  
  784. // get oldest unit in overlying intervals - may span into focal interval
  785. if($i==0) {
  786. $previous_top = 418.7;
  787. $previous_bottom = $study_max;
  788. } else {
  789. $previous_top = $int_top[$i-1];
  790. $previous_bottom = $int_bottom[$i-1];
  791. }
  792. $q = "SELECT units.id, color, LO_h, FO_h, b.age_bottom, t.age_top FROM units
  793. INNER JOIN intervals b ON b.id=FO
  794. INNER JOIN intervals t ON t.id=LO
  795. WHERE units.id IN (".$my_pkg[$pkg_occupied[$k]].") AND b.age_bottom>".$previous_top." AND t.age_top<".$previous_bottom."
  796. ORDER BY b.age_bottom DESC, FO_h, t.age_top DESC, field(LO_h, 1,2,3,4,5,0)
  797. LIMIT 1";
  798. $temp_pkg_result = mysqli_query($connect_macrostrat, $q);
  799. if(mysqli_num_rows($temp_pkg_result)>0) {
  800. $next_int_unit = mysqli_fetch_assoc($temp_pkg_result);
  801. $next_unit_temp = $next_int_unit['id'];
  802. if($next_int_unit['color'] != $occupied_color) {
  803. $n_env_temp[] = $pkg_occupied[$k];
  804. // echo $occupied_unit.' '.$occupied_color.' - '.$next_int_unit['id'].' '.$next_int_unit['color']."<br>";
  805. }
  806. }
  807. mysqli_free_result($temp_pkg_result);
  808.  
  809.  
  810. // loop through the units within the focal interval - may not span into overlying interval
  811. if(is_null($next_unit_temp)) $temp_exclude = $occupied_unit;
  812. else $temp_exclude = $occupied_unit.",".$next_unit_temp;
  813. $q = "SELECT units.id, color, LO_h, FO_h, b.age_bottom, t.age_top FROM units
  814. INNER JOIN intervals b ON b.id=FO
  815. INNER JOIN intervals t ON t.id=LO
  816. WHERE units.id IN (".$my_pkg[$pkg_occupied[$k]].") AND b.age_bottom > ".$int_top[$i]." AND t.age_top >= ".$int_top[$i]."
  817. AND b.age_bottom<=".$occupied_top." AND units.id NOT IN (".$temp_exclude.")
  818. ORDER BY b.age_bottom DESC, FO_h, t.age_top DESC, field(LO_h, 1,2,3,4,5,0)"; //echo $q."<br>";
  819. $temp_pkg_result = mysqli_query($connect_macrostrat, $q); //if(mysqli_num_rows($temp_pkg_result)>2) echo $q."<br>";
  820. while($pkg_row=mysqli_fetch_assoc($temp_pkg_result)) {
  821. if($pkg_row['color']!=$occupied_color && $pkg_row['age_top']<$occupied_top) {
  822. // echo $occupied_unit.' '.$occupied_color.' - '.$pkg_row['id'].' '.$pkg_row['color']."<br>";
  823. if($pkg_row['age_top']>=$int_top[$i]) { // unit is contained entirely within the interval
  824. $n_env_temp[] = $pkg_occupied[$k]; //echo $n_env_truncation."<br>";
  825. } else if ($pkg_row['age_top']<$int_top[$i] && $pkg_row['age_bottom']>$int_top[$i]) { // units spans interval boundary
  826. $n_env_temp[] = $pkg_occupied[$k]; //echo $n_env_truncation."<br>";
  827. }
  828. }
  829. if($pkg_row['age_top']>=$int_top[$i] && $pkg_row['age_bottom']<=$int_bottom[$i]) $n_env_temp2[] = $pkg_occupied[$k];
  830. }
  831. mysqli_free_result($temp_pkg_result);
  832. if($occupied_top>=$int_top[$i] && $occupied_bottom<=$int_bottom[$i]) $n_env_temp2[] = $pkg_occupied[$k];
  833. }
  834. }
  835. }
  836.  
  837. $n_env_truncation = count(array_unique($n_env_temp));
  838. $n_env_truncation2 = count($n_env_temp);
  839. $n_env_truncation3 = count(array_unique($n_env_temp2));
  840. // if($n_env_truncation>0) {print_r($n_env_temp); echo "<br><br>";}
  841.  
  842. // tabulate environmental gap length
  843. $env_gap_count = array();
  844. if($n_units>1) {
  845. $my_occupied_units = array_combine(range(0,$n_units-1,1), array_unique($noam_units));
  846. for($zzz=0; $zzz<$n_units; ++$zzz) {
  847. if($env_unit_top[$my_occupied_units[$zzz]] >= $int_top[$i] && $env_unit_top[$my_occupied_units[$zzz]] < $int_bottom[$i]) {
  848. $env_gap_count[] = $env_gap[$my_occupied_units[$zzz]];
  849. }
  850. }
  851. unset($my_occupied_units);
  852. } else if($n_units==1) {
  853. $my_occupied_units = array_combine(array(0), array_unique($noam_units));
  854. if($env_unit_top[$my_occupied_units[0]] >= $int_top[$i] && $env_unit_top[$my_occupied_units[0]] < $int_bottom[$i]) {
  855. $env_gap_count[] = $env_gap[$my_occupied_units[0]];
  856. }
  857. unset($my_occupied_units);
  858. }
  859.  
  860. // tabulate stats
  861. $n_env_gap = count($env_gap_count);
  862. if($n_env_gap>1) {
  863. $min_env_gap=min($env_gap_count);
  864. $max_env_gap=max($env_gap_count);
  865. $mean_env_gap=array_sum($env_gap_count)/$n_env_gap;
  866. $h = intval($n_env_gap/2);
  867. if($n_env_gap % 2 == 0) {
  868. $median_env_gap = ($env_gap_count[$h] + $env_gap_count[$h-1])/2;
  869. } else {
  870. $median_env_gap = $env_gap_count[$h];
  871. }
  872. } else if($n_env_gap==1) {
  873. $min_env_gap=$env_gap_count[0];
  874. $max_env_gap=$env_gap_count[0];
  875. $mean_env_gap=$env_gap_count[0];
  876. $median_env_gap=$env_gap_count[0];
  877. } else {
  878. $min_env_gap=NULL;
  879. $max_env_gap=NULL;
  880. $mean_env_gap=NULL;
  881. $median_env_gap=NULL;
  882. }
  883. unset($env_gap_count);
  884. if(isset($file_row)) unset($file_row);
  885. $file_row = $int_id[$i].",".$int_name[$i].",".$int_bottom[$i].",".$int_top[$i].",".
  886. $my_genera[$j]['taxon_no'].",".$my_genera[$j]['genus_name'].",".$my_genera[$j]['class'].",".$my_genera[$j]['taxon_order'].",".$my_genera[$j]['family'].",".
  887. $my_genera[$j]['life_habit'].",".$my_genera[$j]['locomotion'].",".$my_genera[$j]['taxon_environment'].",".$my_genera[$j]['diet1'].",".$my_genera[$j]['diet2'].",".
  888. $n_species.",".$n_species_global.",".$n_coll.",".$n_occur.",".$global_endemic_to_interval.",".
  889. $my_genus_age[$this_genus[$j]]['noam_max'].",".$my_genus_age[$this_genus[$j]]['noam_min'].",".
  890. $my_genus_age[$this_genus[$j]]['global_max'].",".$my_genus_age[$this_genus[$j]]['global_min'].",".
  891. $max_paleolat_N.",".$max_paleolat_S.",".$n_matched_units_greater_n_45.",".$n_matched_units_greater_s_45.",".
  892. $max_dist_noam.",".$max_dist_global.",".
  893. $n_packages.",".$n_packages_total.",".$n_units.",".$n_units_total.",".
  894. count($my_carb).",".count(array_intersect($noam_units, $my_carb)).",".count($my_clast).",".count(array_intersect($noam_units, $my_clast)).",".
  895. count($truncating_packages).",".count(array_intersect($noam_packages, $truncating_packages)).",".
  896. $n_env_truncation.",".$n_env_truncation2.",".$n_env_truncation3.",".
  897. $min_gap.",".$max_gap.",".$mean_gap.",".$median_gap.",".
  898. $min_env_gap.",".$max_env_gap.",".$mean_env_gap.",".$median_env_gap.",".$n_env_gap.",".
  899. $n_cols.",".$n_columns_total.",".$n_trunc_columns_occupied."\n";
  900. fwrite($fh, $file_row);
  901. unset($file_row);
  902. }
  903. }
  904. }
  905. fclose($fh);
  906. $my_collections = implode(",",array_unique($my_collections));
  907.  
  908.  
  909. // get references and authorizers & enterers
  910. $q = "SELECT occurrences_temp2.collection_no, collections.reference_no, collections.authorizer, collections.enterer, collections.modifier,
  911. refs.author1init, refs.author1last, refs.author2init, refs.author2last, refs.otherauthors, refs.pubyr, refs.reftitle, refs.pubtitle,
  912. refs.editors, refs.pubvol, refs.firstpage, refs.lastpage, refs.publication_type
  913. FROM occurrences_temp2
  914. INNER JOIN pbdb.collections ON occurrences_temp2.collection_no=collections.collection_no
  915. INNER JOIN pbdb.refs ON refs.reference_no=collections.reference_no
  916. WHERE collections.collection_no IN (".$my_collections.")
  917. GROUP BY occurrences_temp2.collection_no
  918. ORDER BY occurrences_temp2.collection_no"; //echo $q."<br><br>";
  919. $result = mysqli_query($connect_macrostrat, $q);
  920.  
  921. $data_credits = array();
  922. $ref_credits = array();
  923. while($row=mysqli_fetch_assoc($result)) {
  924.  
  925. if(isset($data_credits[$row['authorizer']])) ++$data_credits[$row['authorizer']]['auth'];
  926. else $data_credits[$row['authorizer']] = array('auth'=> 1, 'enter' => 0, 'modify' => 0);
  927.  
  928. if(isset($data_credits[$row['enterer']])) ++$data_credits[$row['enterer']]['enter'];
  929. else $data_credits[$row['enterer']] = array('auth'=> 0, 'enter' => 1, 'modify' => 0);
  930.  
  931. if(isset($data_credits[$row['modifier']])) ++$data_credits[$row['modifier']]['modify'];
  932. else $data_credits[$row['modifier']] = array('auth'=> 0, 'enter' => 0, 'modify' => 1);
  933.  
  934. if(isset($ref_credits[$row['reference_no']])) {
  935. ++$ref_credits[$row['reference_no']]['count'];
  936. } else {
  937. $author_temp = $row['author1last'].", ".$row['author1init'];
  938. if(is_null($row['otherauthors'])===FALSE) {
  939. $author_temp = $author_temp.' ,'.$row['author2init']." ".$row['author2last'].", ".$row['otherauthors'].".";
  940. } else if(is_null($row['author2last'])===FALSE) {
  941. $author_temp = $author_temp.' and '.$row['author2init']." ".$row['author2last'].".";
  942. }
  943.  
  944. if($row['publication_type']=='journal article' || $row['publication_type']=='serial monograph' || $row['publication_type']=='abstract') {
  945. $full_ref = $author_temp." ".$row['pubyr'].". ".$row['reftitle'].". ".$row['pubtitle']." ".$row['pubvol'].":".$row['firstpage']."-".$row['lastpage'];
  946. } else if ($row['publication_type']=='guidebook' || $row['publication_type']=='book/book chapter' || $row['publication_type']=='Ph.D. thesis' || $row['publication_type']=='M.S. thesis') {
  947. if(is_null($row['editors'])) $editor_temp = '';
  948. else $editor_temp = $row['editors']." (eds.) ";
  949.  
  950. $full_ref = $author_temp." ".$row['pubyr'].". ".$row['reftitle'].". In ".$editor_temp.". ".$row['pubtitle']." ".$row['firstpage']."-".$row['lastpage'];
  951. } else {
  952. if(is_null($row['reftitle']) || $row['reftitle']=='') $temp_pubtitle = '';
  953. else $temp_pubtitle = $row['reftitle'].". ";
  954. $full_ref = $author_temp." ".$row['pubyr'].". ".$temp_pubtitle." Unpublished data";
  955. }
  956. $ref_credits[$row['reference_no']] = array('count'=> 1, 'full' => $full_ref, 'type' => $row['publication_type']);
  957. }
  958. }
  959.  
  960. // write text files
  961. $credit_file = 'collection_credits.txt';
  962. $fh = fopen($credit_file,'w');
  963. chmod($credit_file, 0777);
  964. $file_row = "name\tauthorized\tentered\tmodified\n";
  965. fwrite($fh, $file_row);
  966.  
  967. $my_people = array_keys($data_credits);
  968. for($i=0; $i<count($data_credits); ++$i) {
  969. if(is_null($my_people[$i])===FALSE && $my_people[$i]!='') {
  970. $file_row = $my_people[$i]."\t".$data_credits[$my_people[$i]]['auth']."\t".$data_credits[$my_people[$i]]['enter']."\t".$data_credits[$my_people[$i]]['modify']."\n";
  971. fwrite($fh, $file_row);
  972. }
  973. }
  974. fclose($fh);
  975. echo 'File <a href="'.$credit_file.'">'.$credit_file.'</a> saved<BR>';
  976.  
  977. $ref_file = 'references.txt';
  978. $fh = fopen($ref_file,'w');
  979. chmod($ref_file, 0777);
  980. $file_row = "reference_no\tcitation\tn_collections\n";
  981. fwrite($fh, $file_row);
  982.  
  983. $my_refs = array_keys($ref_credits);
  984. for($i=0; $i<count($ref_credits); ++$i) {
  985. $file_row = $my_refs[$i]."\t".$ref_credits[$my_refs[$i]]['full']."\t".$ref_credits[$my_refs[$i]]['count']."\n";
  986. fwrite($fh, $file_row);
  987. }
  988. fclose($fh);
  989. echo 'File <a href="'.$ref_file.'">'.$ref_file.'</a> saved<BR>';
  990.  
  991. $coll_file = 'collections.csv';
  992. $fh = fopen($coll_file,'w');
  993. chmod($coll_file, 0777);
  994. fwrite($fh, $my_collections);
  995. fclose($fh);
  996. echo 'File <a href="'.$coll_file.'">'.$coll_file.'</a> saved<BR>';
  997. }
  998.  
  999.  
  1000.  
  1001. //////////////
  1002. //
  1003. // stuff at the end
  1004. //
  1005. //////////////
  1006. if(isset($_POST['submit'])) {
  1007. $t1 = microtime(true);
  1008. $time_diff = $t1 - $t0;
  1009. if ($time_diff/60 < 1) {
  1010. $diff_seconds = round($time_diff, 3);
  1011. echo "<br>This process took ".$diff_seconds." seconds";
  1012. } else if ($time_diff>=1 & $time_diff<3600) {
  1013. $diff_min = round($time_diff/60, 3);
  1014. echo "<br>This process took ".$diff_min." minutes";
  1015. } else {
  1016. $diff_hour = round($time_diff/3600, 3);
  1017. echo "<br>This process took ".$diff_hour." hours";
  1018. }
  1019. }
  1020. ?>
  1021.  
  1022.  
  1023. <?php
  1024. require_once('../footer.html');
  1025. exit();
  1026. ?>
Runtime error #stdin #stdout 0.02s 13664KB
stdin
Standard input is empty
stdout
Parse error: syntax error, unexpected '=' in /home/0kSb3h/prog.php on line 599