{"id":176,"date":"2007-10-16T07:00:35","date_gmt":"2007-10-16T11:00:35","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2007\/10\/16\/upslope-area-handling-nans\/"},"modified":"2019-10-23T15:21:20","modified_gmt":"2019-10-23T19:21:20","slug":"upslope-area-handling-nans","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2007\/10\/16\/upslope-area-handling-nans\/","title":{"rendered":"Upslope area &#8211; handling NaNs"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>Since I originally posted my <a title=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadFile.do?objectId=15818&amp;objectType=FILE (link no longer works)\">upslope toolbox<\/a> to MATLAB Central back in August, I have heard from some experts about an issue related to NaNs in the DEM data.  Specifically,\r\n      some data sets record elevation only for DEM pixels within a particular catchment basin.  Pixels outside the catchment basin\r\n      typically are NaN. Some of my code doesn't handle NaN values in the DEM.\r\n   <\/p>\r\n   <p>After some e-mail correspondence, I settled on this approach:<\/p>\r\n   <div>\r\n      <ul>\r\n         <li>In the pixel flow direction computation, replace groups of NaN pixels that touch the border by a high value. (You can read\r\n            about the technique for find such pixels in <a href=\"https:\/\/blogs.mathworks.com\/steve\/2007\/09\/04\/clearing-border-components\/\">this post<\/a>.) This replacement makes the flow direction on the watershed pixels point inward, toward the catchment basin.\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <div>\r\n      <ul>\r\n         <li>When solving the flow matrix for upslope area, use 0 instead 1 on the right-hand side for exterior NaN pixels.  This means\r\n            that such pixels don't contribute any flow to the system.\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>Here's a simple example:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[x,y] = meshgrid(-3:3);\r\nE = hypot(x,y);\r\nE(abs(x) + abs(y) &gt; 3) = NaN<\/pre><pre style=\"font-style:oblique\">\r\nE =\r\n\r\n       NaN       NaN       NaN    3.0000       NaN       NaN       NaN\r\n       NaN       NaN    2.2361    2.0000    2.2361       NaN       NaN\r\n       NaN    2.2361    1.4142    1.0000    1.4142    2.2361       NaN\r\n    3.0000    2.0000    1.0000         0    1.0000    2.0000    3.0000\r\n       NaN    2.2361    1.4142    1.0000    1.4142    2.2361       NaN\r\n       NaN       NaN    2.2361    2.0000    2.2361       NaN       NaN\r\n       NaN       NaN       NaN    3.0000       NaN       NaN       NaN\r\n\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">R = dem_flow(E);\r\nT = flow_matrix(E, R);\r\nA = upslope_area(E, T)<\/pre><pre style=\"font-style:oblique\">\r\nA =\r\n\r\n         0         0         0    1.0000         0         0         0\r\n         0         0    1.0000    2.0000    1.0000         0         0\r\n         0    1.0000    1.8112    4.1888    1.8112    1.0000         0\r\n    1.0000    2.0000    4.1888   25.0000    4.1888    2.0000    1.0000\r\n         0    1.0000    1.8112    4.1888    1.8112    1.0000         0\r\n         0         0    1.0000    2.0000    1.0000         0         0\r\n         0         0         0    1.0000         0         0         0\r\n\r\n<\/pre><p>Notice that the upslope area at the lowest pixel is 25.  This is the number of non-NaN pixels in the DEM.<\/p>\r\n   <p>I uploaded version 1.2 of the upslope toolbox, with these NaN-related changes, to the MATLAB Central File Exchange in early\r\n      October.  If you have data sets that use NaNs outside the catchment basin of interest, you might want to <a title=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadFile.do?objectId=15818&amp;objectType=FILE (link no longer works)\">download<\/a> the new version.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_90dc5f5bd09747b582c12754fd2840e2() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='90dc5f5bd09747b582c12754fd2840e2 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 90dc5f5bd09747b582c12754fd2840e2';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Copyright 2007 The MathWorks, Inc.';\r\n        copyright = '';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_90dc5f5bd09747b582c12754fd2840e2()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.5<br><\/p>\r\n<\/div>\r\n<!--\r\n90dc5f5bd09747b582c12754fd2840e2 ##### SOURCE BEGIN #####\r\n%%\r\n% Since I originally posted my \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadFile.do?objectId=15818&objectType=FILE \r\n% upslope toolbox> to MATLAB Central back in\r\n% August, I have heard from some experts about an issue related to NaNs in\r\n% the DEM data.  Specifically, some data sets record elevation only for\r\n% DEM pixels within a particular catchment basin.  Pixels outside the\r\n% catchment basin typically are NaN. Some of my code doesn't handle NaN\r\n% values in the DEM.\r\n%\r\n% After some e-mail correspondence, I settled on this approach:\r\n%\r\n% * In the pixel flow direction computation, replace groups of NaN pixels\r\n% that touch the border by a high value. (You can read about the technique\r\n% for find such pixels in \r\n% <https:\/\/blogs.mathworks.com\/steve\/2007\/09\/04\/clearing-border-components\/ \r\n% this post>.) This replacement makes the flow \r\n% direction on\r\n% the watershed pixels point inward, toward the catchment basin.\r\n%\r\n% * When solving the flow matrix for upslope area, use 0 instead 1 on the\r\n% right-hand side for exterior NaN pixels.  This means that such pixels\r\n% don't contribute any flow to the system.\r\n%\r\n% Here's a simple example:\r\n\r\n[x,y] = meshgrid(-3:3);\r\nE = hypot(x,y);\r\nE(abs(x) + abs(y) > 3) = NaN\r\n\r\n%%\r\n\r\nR = dem_flow(E);\r\nT = flow_matrix(E, R);\r\nA = upslope_area(E, T)\r\n\r\n%%\r\n% Notice that the upslope area at the lowest pixel is 25.  This is the\r\n% number of non-NaN pixels in the DEM.\r\n%\r\n% I uploaded version 1.2 of the upslope toolbox, with these NaN-related\r\n% changes, to the MATLAB Central File Exchange in early October.  If you\r\n% have data sets that use NaNs outside the catchment basin of interest, you\r\n% might want to \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadFile.do?objectId=15818&objectType=FILE \r\n% download> the new version.\r\n\r\n##### SOURCE END ##### 90dc5f5bd09747b582c12754fd2840e2\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   Since I originally posted my upslope toolbox to MATLAB Central back in August, I have heard from some experts about an issue related to NaNs in the DEM data.  Specifically,\r\n      some data sets... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2007\/10\/16\/upslope-area-handling-nans\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[208,334,30],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/176"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/comments?post=176"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/176\/revisions"}],"predecessor-version":[{"id":3568,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/176\/revisions\/3568"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}