{"id":88,"date":"2012-02-26T00:00:00","date_gmt":"2012-02-25T16:00:00","guid":{"rendered":"http:\/\/xuguang.info\/blog\/2012\/02\/a-program-compute-ndvi-from-modishdf4-file"},"modified":"2014-01-27T23:37:11","modified_gmt":"2014-01-27T15:37:11","slug":"a-program-compute-ndvi-from-modishdf4-file-2","status":"publish","type":"post","link":"https:\/\/xg1990.com\/blog\/en\/archives\/88","title":{"rendered":"A program compute NDVI from MODIS\/HDF4 file"},"content":{"rendered":"<p>Just finished a c++ program to compute NDVI from MODIS\/HDF4 file<\/p>\n<p>Written in c++, depends on <a href=\"http:\/\/www.gdal.org\">GDAL<\/a>(Geospatial Data Abstraction Library)<\/p>\n<p>Source code can be downloaded <a href=\"http:\/\/xg1990.com\/blog\/wp-content\/uploads\/2012\/02\/hdf2ndvi.zip\">HERE<\/a><br \/>\n<!--more--><\/p>\n<h1 id=\"compile\">Compile<\/h1>\n<p>Before compiling, make sure you have <code>make<\/code>, <code>g++<\/code>, <code>gdal<\/code> installed.<\/p>\n<p>go to the hdf2ndvi directory, type:<\/p>\n<pre><code>make\r\n<\/code><\/pre>\n<p>you\u2019ll see<\/p>\n<pre><code>g++  -c hdf2ndvi.cpp\r\ng++  -c enhance_gdal.cpp\r\ng++  -o hdf2ndiv hdf2ndvi.o enhance_gdal.o -lgdal\r\n<\/code><\/pre>\n<p>and then it\u2019s successfully complied.<\/p>\n<h1 id=\"usage\">Usage<\/h1>\n<p>##dealing with a single file<br \/>\ntype:<\/p>\n<pre><code>.\/hdf2ndiv filename.hdf\r\n<\/code><\/pre>\n<p>and you\u2019ll see:<\/p>\n<pre><code>computing filename.hdf...\r\n<\/code><\/pre>\n<p>after finished, you\u2019ll get <em>filename.hdf_NDVI.tiff<\/em> in the same folder<\/p>\n<h2 id=\"dealing-with-multiple-files\">dealing with multiple files<\/h2>\n<p>If the files to be computed in the same folder.<br \/>\nYou can type:<\/p>\n<pre><code>.\/hdf2ndvi *.hdf\r\n<\/code><\/pre>\n<p>If you want to compute all the files in the folders and subfolders,<br \/>\nyou can use <code>xargs<\/code>:<\/p>\n<pre><code>find . -name \".hdf\" -print0 | xargs -0 .\/hdf2ndvi\r\n<\/code><\/pre>\n<h2 id=\"change-band-used\">change band used<\/h2>\n<p>This program defaultly regard band0(band is counted from 0) as Red Band, and regard band1 as NIR(near infrared red) band. If you want to change the default value, for instance, set band-4 as NIR band and band-0 as Red Band, you can do like this:<\/p>\n<pre><code>.\/hdf2ndvi filename.hdf -red 0 -nir 4\r\n<\/code><\/pre>\n<p>and you\u2019ll get file: <em>filename.hdf_NDVI.tiff<\/em> computed from band4 and band0<\/p>\n<h1 id=\"algorithm\">Algorithm<\/h1>\n<p>The NDVI is computed as following:<\/p>\n<p><script type=\"math\/tex; mode=display\">\/\/ <![CDATA[\n\\frac{B_{NIR}-B_{Red}}{B_{NIR}+B_{Red}}\n\/\/ ]]><\/script><\/p>\n<p>but some pixel value of MODIS data is negative,<br \/>\nso when meeting negative value in<br \/>\n<script type=\"math\/tex\">\/\/ <![CDATA[\nB_{NIR}\n\/\/ ]]><\/script><br \/>\nor<br \/>\n<script type=\"math\/tex\">\/\/ <![CDATA[\nB_{Red}\n\/\/ ]]><\/script>,<br \/>\nif<script type=\"math\/tex\">\/\/ <![CDATA[\nB_{NIR}>B_{Red}\n\/\/ ]]><\/script>the program set NDVI = 1,<br \/>\nelse if<script type=\"math\/tex\">\/\/ <![CDATA[\n% &lt;![CDATA[\nB_{NIR}<b_{Red} %]]&gt;\n\/\/ ]]><\/script>the program set NDVI = -1.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just finished a c++ program to compute NDVI from MODIS\/HDF4 file Written in c++, depends on GDAL(Geospatial Data Abstraction Library) Source code can be downloaded HERE<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":""},"categories":[4,3],"tags":[],"translation":{"provider":"WPGlobus","version":"3.0.1","language":"en","enabled_languages":["zh","en"],"languages":{"zh":{"title":true,"content":true,"excerpt":false},"en":{"title":false,"content":false,"excerpt":false}}},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v18.4.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>A program compute NDVI from MODIS\/HDF4 file - xg1990<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/xg1990.com\/blog\/archives\/88\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"A program compute NDVI from MODIS\/HDF4 file - xg1990\" \/>\n<meta property=\"og:url\" content=\"https:\/\/xg1990.com\/blog\/archives\/88\" \/>\n<meta property=\"og:site_name\" content=\"xg1990\" \/>\n<meta property=\"article:published_time\" content=\"2012-02-25T16:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2014-01-27T15:37:11+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"xg1990\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/xg1990.com\/blog\/#website\",\"url\":\"https:\/\/xg1990.com\/blog\/\",\"name\":\"xg1990\",\"description\":\"\u5730\u7406\u5708\u7684\u7801\u519c\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/xg1990.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/xg1990.com\/blog\/archives\/88#webpage\",\"url\":\"https:\/\/xg1990.com\/blog\/archives\/88\",\"name\":\"A program compute NDVI from MODIS\/HDF4 file - xg1990\",\"isPartOf\":{\"@id\":\"https:\/\/xg1990.com\/blog\/#website\"},\"datePublished\":\"2012-02-25T16:00:00+00:00\",\"dateModified\":\"2014-01-27T15:37:11+00:00\",\"author\":{\"@id\":\"https:\/\/xg1990.com\/blog\/#\/schema\/person\/774c05a86b0a61bb8ff633749c59fa8f\"},\"breadcrumb\":{\"@id\":\"https:\/\/xg1990.com\/blog\/archives\/88#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/xg1990.com\/blog\/archives\/88\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/xg1990.com\/blog\/archives\/88#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/xg1990.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"A program compute NDVI from MODIS\/HDF4 file\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/xg1990.com\/blog\/#\/schema\/person\/774c05a86b0a61bb8ff633749c59fa8f\",\"name\":\"xg1990\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/xg1990.com\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/2d326d725f277a38a498575f23200141?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/2d326d725f277a38a498575f23200141?s=96&d=mm&r=g\",\"caption\":\"xg1990\"},\"url\":\"https:\/\/xg1990.com\/blog\/en\/archives\/author\/xg1990\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"A program compute NDVI from MODIS\/HDF4 file - xg1990","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/xg1990.com\/blog\/archives\/88","og_locale":"en_US","og_type":"article","og_title":"A program compute NDVI from MODIS\/HDF4 file - xg1990","og_url":"https:\/\/xg1990.com\/blog\/archives\/88","og_site_name":"xg1990","article_published_time":"2012-02-25T16:00:00+00:00","article_modified_time":"2014-01-27T15:37:11+00:00","twitter_misc":{"Written by":"xg1990","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/xg1990.com\/blog\/#website","url":"https:\/\/xg1990.com\/blog\/","name":"xg1990","description":"\u5730\u7406\u5708\u7684\u7801\u519c","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/xg1990.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/xg1990.com\/blog\/archives\/88#webpage","url":"https:\/\/xg1990.com\/blog\/archives\/88","name":"A program compute NDVI from MODIS\/HDF4 file - xg1990","isPartOf":{"@id":"https:\/\/xg1990.com\/blog\/#website"},"datePublished":"2012-02-25T16:00:00+00:00","dateModified":"2014-01-27T15:37:11+00:00","author":{"@id":"https:\/\/xg1990.com\/blog\/#\/schema\/person\/774c05a86b0a61bb8ff633749c59fa8f"},"breadcrumb":{"@id":"https:\/\/xg1990.com\/blog\/archives\/88#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/xg1990.com\/blog\/archives\/88"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/xg1990.com\/blog\/archives\/88#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/xg1990.com\/blog\/"},{"@type":"ListItem","position":2,"name":"A program compute NDVI from MODIS\/HDF4 file"}]},{"@type":"Person","@id":"https:\/\/xg1990.com\/blog\/#\/schema\/person\/774c05a86b0a61bb8ff633749c59fa8f","name":"xg1990","image":{"@type":"ImageObject","@id":"https:\/\/xg1990.com\/blog\/#personlogo","inLanguage":"en-US","url":"https:\/\/secure.gravatar.com\/avatar\/2d326d725f277a38a498575f23200141?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2d326d725f277a38a498575f23200141?s=96&d=mm&r=g","caption":"xg1990"},"url":"https:\/\/xg1990.com\/blog\/en\/archives\/author\/xg1990"}]}},"_links":{"self":[{"href":"https:\/\/xg1990.com\/blog\/en\/wp-json\/wp\/v2\/posts\/88"}],"collection":[{"href":"https:\/\/xg1990.com\/blog\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xg1990.com\/blog\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xg1990.com\/blog\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xg1990.com\/blog\/en\/wp-json\/wp\/v2\/comments?post=88"}],"version-history":[{"count":3,"href":"https:\/\/xg1990.com\/blog\/en\/wp-json\/wp\/v2\/posts\/88\/revisions"}],"predecessor-version":[{"id":143,"href":"https:\/\/xg1990.com\/blog\/en\/wp-json\/wp\/v2\/posts\/88\/revisions\/143"}],"wp:attachment":[{"href":"https:\/\/xg1990.com\/blog\/en\/wp-json\/wp\/v2\/media?parent=88"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xg1990.com\/blog\/en\/wp-json\/wp\/v2\/categories?post=88"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xg1990.com\/blog\/en\/wp-json\/wp\/v2\/tags?post=88"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}