{"id":1771,"date":"2018-07-02T12:50:01","date_gmt":"2018-07-02T10:50:01","guid":{"rendered":"https:\/\/blogs.fu-berlin.de\/reseda\/?page_id=1771"},"modified":"2018-09-25T11:09:37","modified_gmt":"2018-09-25T09:09:37","slug":"landsat-8-bulk-process","status":"publish","type":"page","link":"https:\/\/blogs.fu-berlin.de\/reseda\/landsat-8-bulk-process\/","title":{"rendered":"Landsat 8 Bulk Preprocessing"},"content":{"rendered":"<p>Imagine you have 50 Landsat 8 records. Of course, it would be very tedious to modify and start the script 50 times. That&#8217;s why there is now an automated solution for any number of data products!<\/p>\n<p>Again, the prerequisite is that you have the L8 Level-2 datasets downloaded to a folder (&#8220;\/media\/sf_exchange\/landsatdata&#8221;  in our example), in which only all of your Landsat 8 scenes are stored &#8211; no other files! Of course you should replace the file path according to your storage location and create a character variable:<\/p>\n<pre class=\"theme:amityreseda\">\r\npathToFiles &lt;- &quot;\/media\/sf_exchange\/landsatdata&quot;\r\n\r\ndir(pathToFiles)\r\n## [1] &quot;LC081920232017083001T1-SC20180613163601.tar.gz&quot;\r\n## [2] &quot;LC081930232017060201T1-SC20180613160412.tar.gz&quot;\r\n<\/pre>\n<p>You can check the files inside <span class=\"crayon-inline theme:amityreseda\">pathToFiles <\/span> with the <span class=\"crayon-inline theme:amityreseda\">dir()<\/span> function. It should list all your Landsat 8 files. If that is not the case, make sure you did not mess up the file path name.<br \/>\nWe can write all the products that exist in the folder in a vector <span class=\"crayon-inline theme:amityreseda\">products<\/span> using the <span class=\"crayon-inline theme:amityreseda\">list.files()<\/span> function:<\/p>\n<pre class=\"theme:amityreseda\">\r\nproducts &lt;- list.files(pathToFiles, full.names = TRUE)\r\n\r\nproducts\r\n## [1] &quot;\/media\/sf_exchange\/landsatdata\/LC081920232017083001T1-SC20180613163601.tar.gz&quot;\r\n## [2] &quot;\/media\/sf_exchange\/landsatdata\/LC081930232017060201T1-SC20180613160412.tar.gz&quot;\r\n<\/pre>\n<p>To go through all the steps we saw in the previous section above for each of the scenes in <span class=\"crayon-inline theme:amityreseda\">products<\/span>, we use a for-loop. Conceptually, the for-loop does the following:<\/p>\n<pre class=\"theme:amityreseda\">\r\nfor (i in products) {\r\n  print(i)\r\n  print(\"do all the preprocessing stuff\")\r\n}\r\n## [1] \"\/media\/sf_exchange\/landsatdata\/LC081920232017083001T1-SC20180613163601.tar.gz\"\r\n## [1] \"do all the preprocessing stuff\"\r\n## [1] \"\/media\/sf_exchange\/landsatdata\/LC081930232017060201T1-SC20180613160412.tar.gz\"\r\n## [1] \"do all the preprocessing stuff\"\r\n<\/pre>\n<p>For all landsat scenes <span class=\"crayon-inline theme:amityreseda\">i<\/span> in <span class=\"crayon-inline theme:amityreseda\">products<\/span>, it will do all the preprocessing stuff. The variable, or iterator, <span class=\"crayon-inline theme:amityreseda\">i<\/span> is just a placeholder, which is sequentially occupied with the file names of the Landsat products.<br \/>\nThus, you can just imagine any filename (as a string) every time you see the iterator i.<br \/>\nThe remaining steps are then identical to those described above.<\/p>\n<p>Here is the complete code. Just adjust your <span class=\"crayon-inline theme:amityreseda\">pathToFiles <\/span> and run it in R-Studio to preprocess all of your Landsat 8 Level-2 files!<\/p>\n<p><a name=\"3\"><\/a><\/p>\n<pre class=\"theme:amityreseda\">\r\nlibrary(raster)\r\n\r\npathToFiles &lt;- &quot;\/media\/sf_exchange\/landsatdata&quot;\r\n\r\nproducts &lt;- list.files(pathToFiles, full.names = TRUE)\r\n\r\nfor (i in products) {\r\n  print( paste0(&quot;processing: &quot;, i) )\r\n  \r\n  productname &lt;- substr(i, 1, nchar(i) - 7) \r\n\r\n  untar(i, exdir = productname)\r\n  \r\n  productfiles &lt;- list.files(productname, full.names = TRUE)\r\n  \r\n  bands &lt;- c(grep(&#039;band1&#039;, productfiles, value=TRUE),\r\n             grep(&#039;band2&#039;, productfiles, value=TRUE),\r\n             grep(&#039;band3&#039;, productfiles, value=TRUE),\r\n             grep(&#039;band4&#039;, productfiles, value=TRUE),\r\n             grep(&#039;band5&#039;, productfiles, value=TRUE),\r\n             grep(&#039;band6&#039;, productfiles, value=TRUE),\r\n             grep(&#039;band7&#039;, productfiles, value=TRUE)\r\n            ) \r\n  \r\n  writeRaster(stack(bands), \r\n              paste0(productname, &quot;.tif&quot;), \r\n              format = &quot;GTiff&quot;\r\n              )\r\n  \r\n  unlink(productname, recursive=TRUE)\r\n\r\n  makeOVR &lt;- paste0(&quot;gdaladdo -ro &quot;, productname, &quot;.tif 2 4 8 16&quot;)\r\n  system( makeOVR )\r\n  }\r\n<\/pre>\n<p>What does the script for Level-1 data look like? Get an answer and check your knowledge during the following tasks!<\/p>\n<p><a target=\"_blank\" href=\"https:\/\/blogs.fu-berlin.de\/reseda\/preprocess-landsat-exercise\/\"><br \/>\n<button style=\"width:100%;text-align:center;padding: 0;background-color:#6b9e1f;color: white\"><\/p>\n<div style=\"font-family: 'Noto Sans',sans-serif\"><span style=\"font-size: 30px\"><strong>Prove your knowledge<\/strong><\/span><\/div>\n<p><\/button><\/a><\/p>\n<hr style=\"height:4px;background-color:#6b9e1f\">\n<a href=\"https:\/\/blogs.fu-berlin.de\/reseda\/sentinel-2-preprocessing\/\"><br \/>\n<button style=\"width:100%;text-align:right;padding: 10 0;background-color:white;margin:-55px 0 0 0\"><\/p>\n<div style=\"font-family: 'Noto Sans',sans-serif;line-height: 1.2\">\n<span style=\"font-size: 12px;color:#bfbfbf\"><strong><em>NEXT<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 30px;color:#6b9e1f\"><strong><em>Sentinel 2 Preprocessing<\/em><\/strong><\/span>\n<\/div>\n<p><\/button><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Imagine you have 50 Landsat 8 records. Of course, it would be very tedious to modify and start the script 50 times. That&#8217;s why there is now an automated solution for any number of data products! Again, the prerequisite is that you have the L8 Level-2 datasets downloaded to a folder (&#8220;\/media\/sf_exchange\/landsatdata&#8221; in our example), &hellip; <a href=\"https:\/\/blogs.fu-berlin.de\/reseda\/landsat-8-bulk-process\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Landsat 8 Bulk Preprocessing&#8221;<\/span><\/a><\/p>\n","protected":false},"author":3237,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1771","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages\/1771","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/users\/3237"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/comments?post=1771"}],"version-history":[{"count":4,"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages\/1771\/revisions"}],"predecessor-version":[{"id":2501,"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages\/1771\/revisions\/2501"}],"wp:attachment":[{"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/media?parent=1771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}