fork download
  1. <?php
  2. $page_title = 'Run temp 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="map_figure.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>draw map</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. if(isset($_POST['go'])) {
  23. $t0 = microtime(true);
  24.  
  25. $q_time_scale = "SELECT interval_name, age_bottom, age_top, intervals.id FROM intervals
  26. WHERE interval_name IN ('Whiterock', 'Chazyan', 'Blackriverian', 'Shermanian', 'Richmondian', 'Hirnantian', 'Rhuddanian', 'Aeronian', 'Telychian', 'Wenlock')
  27. ORDER BY age_bottom";
  28. $time_scale = mysqli_query($connect_macrostrat, $q_time_scale);
  29. $n_bin = mysqli_num_rows($time_scale);
  30.  
  31. while($row = mysqli_fetch_assoc($time_scale)) {
  32. if($row['interval_name'] == 'Blackriverian') {
  33. $int_name[] = 'Blackriveran-Kirkfield';
  34. $int_bottom[] = $row['age_bottom'];
  35. $int_top[] = 457.0000;
  36. } else if($row['interval_name'] == 'Shermanian') {
  37. $int_name[] = 'Shermanian-Maysvillian';
  38. $int_bottom[] = $row['age_bottom'];
  39. $int_top[] = 450.0000;
  40. } else {
  41. $int_name[] = $row['interval_name'];
  42. $int_bottom[] = $row['age_bottom'];
  43. $int_top[] = $row['age_top'];
  44. }
  45. $int_id[] = $row['id'];
  46. }
  47. mysqli_free_result($time_scale);
  48. $n_int = count($int_name);
  49.  
  50. $base_ordovician = 488.3;
  51. $study_max = max($int_bottom);
  52. $study_min = min($int_top);
  53.  
  54.  
  55. //////////////
  56. //
  57. // Column data saved as columns.csv
  58. //
  59. //////////////
  60. $q = "SELECT cols.id FROM cols
  61. INNER JOIN col_groups ON col_groups.id=col_group_id
  62. INNER JOIN projects ON projects.id=project_id
  63. WHERE project LIKE 'North America' AND col_group NOT LIKE 'Mexico' AND cols.id NOT IN (34, 111, 139, 218, 586, 587) AND status_code='active'";
  64. $columns = mysqli_query($connect_macrostrat, $q);
  65. while($row = mysqli_fetch_assoc($columns)) $my_cols[] = $row['id'];
  66. mysqli_free_result($columns);
  67.  
  68.  
  69. //////////////
  70. //
  71. // get packages
  72. //
  73. //////////////
  74. $temp_pkg = split_packages('marine');
  75. for($i=0; $i<count($temp_pkg); ++$i) {
  76. $q = "SELECT col_id, max(b.age_bottom) bottom, min(t.age_top) top, count(distinct units.id) n_unit FROM units
  77. INNER JOIN intervals b ON b.id=FO
  78. INNER JOIN intervals t ON t.id=LO
  79. WHERE units.id IN ($temp_pkg[$i])";
  80. $result=mysqli_query($connect_macrostrat, $q);
  81. $row=mysqli_fetch_assoc($result);
  82. if(in_array($row['col_id'], $my_cols)===TRUE && $row['bottom']>$study_min && $row['top']<$study_max) $my_pkg[] = $temp_pkg[$i];
  83. }
  84. $all_units = implode(",",$my_pkg);
  85.  
  86.  
  87. //////////////
  88. //
  89. // units saved as O_S_units.csv
  90. //
  91. //////////////
  92. $units_file = "O_S_units.csv";
  93. $fh = fopen($units_file, 'w') or die("can't open file");
  94. chmod($units_file, 0777);
  95. $file_row = "unit_id, col_id, age_bottom, age_top, color\n";
  96. fwrite($fh, $file_row);
  97. $q = "SELECT units.id, col_id, b.age_bottom, t.age_top, color FROM units
  98. INNER JOIN intervals b ON b.id=FO
  99. INNER JOIN intervals t ON t.id=LO
  100. WHERE units.id IN (".$all_units.") and b.age_bottom>$study_min AND t.age_top<$study_max"; //echo $q."<br>";
  101. $result=mysqli_query($connect_macrostrat, $q);
  102. while($row=mysqli_fetch_assoc($result)) {
  103. $file_row = $row['id'].",".$row['col_id'].",".$row['age_bottom'].",".$row['age_top'].",".$row['color']."\n";
  104. fwrite($fh, $file_row);
  105. }
  106. fclose($fh);
  107. echo 'File <a href="'.$units_file.'">'.$units_file.'</a> saved<BR>';
  108.  
  109.  
  110. //////////////
  111. //
  112. // pbdb_collections saved as O_S_collections.csv
  113. //
  114. //////////////
  115. $fh = fopen("collections.csv", "r");
  116. $my_collections = fgetcsv($fh, 100000, ",")
  117. echo "There are ".count($data)." collections<br>";
  118. fclose($fh);
  119.  
  120. $pbdb_file = "O_S_collections.csv";
  121. $fh = fopen($pbdb_file, 'w') or die("can't open file");
  122. chmod($pbdb_file, 0777);
  123. $file_row = "collection_no, unit_id, lat, lng\n";
  124. fwrite($fh, $file_row);
  125. $q = "SELECT collection_no, unit_id, lat, lng FROM pbdb_matches
  126. WHERE collection_no IN (".implode(",",$my_collections).")";
  127. $result=mysqli_query($connect_macrostrat, $q);
  128. while($row=mysqli_fetch_assoc($result)) {
  129. $file_row = $row['collection_no'].",".$row['unit_id'].",".$row['lat'].",".$row['lng']."\n";
  130. fwrite($fh, $file_row);
  131. }
  132. fclose($fh);
  133. echo 'File <a href="'.$pbdb_file.'">'.$pbdb_file.'</a> saved<BR>';
  134.  
  135.  
  136. //////////////
  137. //
  138. // draw map in R
  139. //
  140. //////////////
  141.  
  142. $r_file = "temp.r";
  143. $fh = fopen($r_file, 'w') or die("can't open file");
  144. chmod($r_file, 0777);
  145.  
  146. $r_script = "
  147. source('myFunctions.r')
  148. library(maps)
  149. library(mapproj)
  150.  
  151. time.scale <- read.csv(file='time_scale.csv', header=TRUE); n.bins <- nrow(time.scale)
  152. units <- read.csv(file='O_S_units.csv', header=TRUE)
  153. polygons <- read.csv(file='column_polygons.csv', header=TRUE)
  154. pbdb <- read.csv(file='O_S_collections.csv', header=TRUE)
  155.  
  156. ## make maps
  157. lon.limit <- c(-172,-50)
  158. lat.limit <- c(25,83)
  159.  
  160. map.orient <- c(54,-103,0)
  161.  
  162. map.data <- map('world', c('USA', 'Canada'), xlim=lon.limit, ylim=lat.limit, proj='azequalarea',orient=map.orient, plot=FALSE, interior=FALSE)
  163.  
  164. map.color <- 'gray25' # solid dark gray
  165. pbdb.color <- rgb(1,0,0,0.3) # transparent red
  166.  
  167. rock.colors <- matrix(NA, nrow=15, ncol=3)
  168. rock.colors[,1] <- c('blue','gray light','tan','orange','yellow','blue green','pink','brown dark','green dark','brown','green light','brown light','blue dark','gray','green')
  169. rock.colors[,2] <- c('#6BA1FF','#7C7C7C','#9A9469','#FF8400','#FFFF00','#00FFFF','#FF80A8','#55432F','#008800','#55432F','#008800','#55432F','#3838FF','#7C7C7C','#008800')
  170. rock.colors[,3] <- c('carbonate','fine clastics','mixed clastics','coarse clastics','sands','chert','evaporite','metamorphic','plutonic','metamorphic','volcanic','metamorphic','mixed carb/clast','fine clastics','volcanics')
  171. rock.colors <- rock.colors[order(rock.colors[,1]),]
  172.  
  173. top.units <- vector(mode='numeric', length=nrow(units)); counter <- 1
  174. pdf(file='map_figure.pdf', height=20, width=50)
  175. par(mfrow=c(2,5), mar=c(0,0,10,0)+0.1, pch=16, cex.main=7)
  176.  
  177. for(i in n.bins:1) {
  178. unit.temp <- units[units[,3]>time.scale[i,3] & units[,4]<time.scale[i,2],]
  179. cols.temp <- unique(unit.temp\$col_id)
  180. pbdb.temp <- unique(pbdb[is.element(pbdb\$unit_id, unit.temp\$unit_id),3:4])
  181.  
  182. plot(map.data, main=time.scale[i,1], axes=FALSE, frame.plot=TRUE, type='n', xlab='', ylab='')
  183.  
  184. # plot macrostrat units
  185. for(j in 1:length(cols.temp)) {
  186. top.units[counter] <- unit.temp[unit.temp\$col_id==cols.temp[j],1][1]
  187. color.temp <- rock.colors[match(unit.temp[unit.temp\$col_id==cols.temp[j],5][1], rock.colors[,1]),2]
  188.  
  189. poly.temp <- polygons[polygons[,1]==cols.temp[j],2:3]
  190. poly.proj <- mapproject(list(x=poly.temp[,1], y=poly.temp[,2]))
  191. polygon(poly.proj\$x, poly.proj\$y, col=paste(color.temp), border=paste(color.temp), lwd=0.5)
  192. counter <- counter + 1
  193. }
  194.  
  195. # plot pbdb
  196. if(nrow(pbdb.temp) > 0) {
  197. pbdb.proj <- mapproject(list(x=pbdb.temp[,2], y=pbdb.temp[,1]))
  198. points(pbdb.proj\$x, pbdb.proj\$y, cex=3, col=pbdb.color)
  199. }
  200. lines(map.data\$x, map.data\$y, col=map.color)
  201. }
  202. dev.off()
  203.  
  204. top.units <- top.units[top.units>0]
  205. rock.colors2 <- rock.colors[match(unique(units[is.element(units[,1], top.units), 5]),rock.colors[,1]),]
  206.  
  207. ## make legend
  208. pdf(file='map_legend.pdf', height=6, width=10)
  209. layout(matrix(1:15, nrow=3, ncol=5, byrow=TRUE))
  210. for(i in 1:nrow(rock.colors2)) {
  211. par(mar=c(0.1,0.1,0.1,0.1))
  212. plot(1:10, xlim=c(0,10), ylim=c(0,10), type='n', axes=FALSE, frame.plot=TRUE, xlab='', ylab='')
  213. rect(1,1,9,4, col=paste(rock.colors2[i,2]))
  214. text(5,8, labels=rock.colors2[i,1], cex=1.5)
  215. text(5,6, labels=rock.colors2[i,3], cex=1.5)
  216. }
  217. dev.off()
  218. ";
  219.  
  220. fwrite($fh, $r_script);
  221.  
  222. $cmd = "echo 'rscript <- \"$r_file\"; source(rscript)' | " . "/usr/bin/R --slave 2>&1";
  223. exec($cmd, $r_ran);
  224. if(count($r_ran)>0) {
  225. print_r($r_ran); echo "<br>";
  226. }
  227.  
  228. echo 'File <a href="map_figure.pdf" target="blank">map_figure.pdf</a> saved<BR>';
  229. echo 'File <a href="map_legend.pdf" target="blank">map_legend.pdf</a> saved<BR>';
  230.  
  231. }
  232.  
  233.  
  234. //////////////
  235. //
  236. // stuff at the end
  237. //
  238. //////////////
  239. if(isset($_POST['submit'])) {
  240. $t1 = microtime(true);
  241. $time_diff = $t1 - $t0;
  242. if ($time_diff/60 < 1) {
  243. $diff_seconds = round($time_diff, 3);
  244. echo "<br>This process took ".$diff_seconds." seconds";
  245. } else if ($time_diff>=1 & $time_diff<3600) {
  246. $diff_min = round($time_diff/60, 3);
  247. echo "<br>This process took ".$diff_min." minutes";
  248. } else {
  249. $diff_hour = round($time_diff/3600, 3);
  250. echo "<br>This process took ".$diff_hour." hours";
  251. }
  252. }
  253. ?>
  254.  
  255.  
  256.  
  257. <?php
  258. require_once('../footer.html');
  259. exit();
  260. ?>
Runtime error #stdin #stdout 0.02s 13664KB
stdin
Standard input is empty
stdout
Parse error: syntax error, unexpected T_ECHO in /home/g8NE3L/prog.php on line 119