FILTER_WATERMASK.sml

  Download

More scripts: Scripts By Jack

Syntax Highlighing:

comments, key words, predefined symbols, class members & methods, functions & classes
            
# ------------------------------------------------------------
# FILTER_WATERMASK.sml
# ------------------------------------------------------------
# SET WARNING LEVEL:
  $warnings 3
# ------------------------------------------------------------
# DEFINE PROCEDURE writeTitle:
# PURPOSE: WRITES TITLE & AUTHOR INFO TO CONSOLE WINDOW.
  proc writeTitle() begin
     printf("FILTER_WATERMASK.sml:\n");
     printf("         VERSION: November 15, 2005\n");
     printf("         PURPOSE: Process WATERMASK Raster\n");
     printf("                  to Convert ISOLATED WATER\n");
     printf("                  PIXELS to LAND PIXELS.\n");
     printf("         DETAILS: FAQs_by_Jack G\n");
     printf("          AUTHOR: Dr. Jack F. Paris\n");
     printf("    CONTACT INFO: [email protected] ");
     printf(" 303-775-1195\n");
     printf("     ALLOWED USE: ONLY NON-COMMERCIAL\n\n");
  end
# ------------------------------------------------------------
  clear(); 
  writeTitle();
# ------------------------------------------------------------
# DECLARE VARIABLES:
  raster WATERMASK;
  numeric nlins,ncols,lin,col,nlinsm1,ncolsm1;
  numeric v1,v2,v3,v4,v5,v6,v7,v8,v9,vtest,change;
  numeric pass,count,lm1,lp1,cm1,cp1;
# ------------------------------------------------------------
# DEFINE STRING VARIABLES FOR POPUP WINDOWS:
  string t$,p$,p1$,p2$,p3$,p4$,p5$,p6$,p7$,p8$,p9$;
  string p10$,p11$,p12$,p13$,p14$,p15$,p16$,p17$,p18$,p19$;
  string p20$,p21$;
# ------------------------------------------------------------
# CLEAR CONSOLE WINDOW & REQUEST REPOSITIONING:
  p1$ = "CONSOLE-WINDOW ADJUSTMENT:\n";
  p2$ = "* REPOSITION the CONSOLE WINDOW.\n";
  p3$ = "* Then, CLICK OK.";
  p$  = p1$ + p2$ + p3$;
  PopupMessage(p$);
# ------------------------------------------------------------
# ISSUE WARNING:
  p1$ = "                  !!! WARNING !!!\n\n";
  p2$ = "ONLY USE a COPY of the WATERMASK Raster as INPUT.\n";
  p3$ = "The Input WATERMASK Raster is ";
  p4$ = "ALTERED by this SML.\n\n";
  p5$ = "Click the Next CANCEL Button ";
  p6$ = "to STOP this Process.";
  p$  = p1$ + p2$ + p3$ + p4$ + p5$ + p6$;
  PopupMessage(p$);
# ------------------------------------------------------------
# DEFINE proc checkHisto(Band):
# PURPOSE: Check that Input Raster has a Full (Unsampled)
#          Histogram.  If Not, Then Recompute Histogram.
  proc checkHisto (raster Band) begin
     local numeric sampleInt;
     sampleInt = HistogramGetSampleInterval(Band);
     if (sampleInt > 1) begin
        DeleteHistogram(Band); CreateHistogram(Band,0);
     end
     else if (sampleInt == -1) then	begin
        CreateHistogram(Band,0);
     end
  end
# ------------------------------------------------------------
# GET INPUT RASTER:
  GetInputRaster(WATERMASK); IgnoreNull(WATERMASK);
  checkHisto(WATERMASK);
  DeleteHistogram(WATERMASK); DeletePyramid(WATERMASK);
  nlins = NumLins(WATERMASK); ncols = NumCols(WATERMASK);
  nlinsm1 = nlins - 1; ncolsm1 = ncols - 1;
# ------------------------------------------------------------
# SCAN INTERIOR REGION TO DETECT WATER PIXELS FOR CONVERSION:
  printf("SEARCH FOR WATER-PIXELS TO CONVERT TO");
  printf(" LAND-PIXELS:\n\n");
  printf("SCANNING INTERIOR PIXELS:\n");
  pass = 0; count = 1;
  while (count > 0) begin
     pass = pass + 1; count = 0;
     printf("Pass %3d ... ",pass);
     for lin=2 to nlinsm1 begin
        lm1 = lin - 1; lp1 = lin + 1;
        for col=2 to ncolsm1 begin
           v5 = WATERMASK[lin,col];
           if (v5 == 1) then begin
              # 28:
              cm1 = col - 1; cp1 = col + 1;
              change = 0;
              v2 = WATERMASK[lm1,col];
              v7 = WATERMASK[lp1,col];
              vtest = SetMax(v2,v7);
              if (vtest == 0) then begin
                 change = 1;
              end
              if (change == 0) then begin
                 # 46:
                 v4  = WATERMASK[lin,cm1];
                 v6  = WATERMASK[lin,cp1];
                 vtest = SetMax(v4,v6);
                 if (vtest == 0) then begin
                    change = 1;
                 end
              end
              if (change == 0) then begin
                 # 19:
                 v1  = WATERMASK[lm1,cm1];
                 v9  = WATERMASK[lp1,cp1];
                 vtest = SetMax(v1,v9);
                 if (vtest == 0) then begin
                    change = 1;
                 end
              end
              if (change == 0) then begin
                 # 37:
                 v3  = WATERMASK[lm1,cp1];
                 v7  = WATERMASK[lp1,cm1];
                 vtest = SetMax(v3,v7);
                 if (vtest == 0) then begin
                    change = 1;
                 end
              end
              if (change == 1) then begin
                 WATERMASK[lin,col] = 0;
                 count = count + 1;
              end
           end
        end
     end
     printf("converted %5d pixels.\n",count);
  end
  printf("\n");
# ------------------------------------------------------------
# SCAN EDGE PIXELS TO FIND WATER PIXELS FOR CONVERSION:
  printf("SCANNING EDGE PIXELS:\n");
  pass = pass + 1;
  printf("Pass %3d ... ",pass);
  count = 1;
  # Top:
  for col = 1 to ncols begin
     v5 = WATERMASK[1,col];
     v8 = WATERMASK[2,col];
     if (v5 == 1) then begin
        if (v8 == 0) then begin
           WATERMASK[1,col] = 0;
           count = count + 1;
        end
     end
  end
  # Bottom:
  for col = 1 to ncols begin
     v5 = WATERMASK[nlins,col];
     v2 = WATERMASK[nlinsm1,col];
     if (v5 == 1) then begin
        if (v2 == 0) then begin
           WATERMASK[nlins,col] = 0;
           count = count + 1;
        end
     end
  end
  # Left:
  for lin = 1 to nlins begin
     v5 = WATERMASK[lin,1];
     v6 = WATERMASK[lin,2];
     if (v5 == 1) then begin
        if (v6 == 0) then begin
           WATERMASK[lin,1] = 0;
           count = count + 1;
        end
     end
  end
  # Right:
  for lin = 1 to nlins begin
     v5 = WATERMASK[lin,ncols];
     v4 = WATERMASK[lin,ncolsm1];
     if (v5 == 1) then begin
        if (v4 == 0) then begin
           WATERMASK[lin,ncols] = 0;
           count = count + 1;
        end
     end
  end
  printf("converted %5d pixels.\n",count);
# ------------------------------------------------------------
# FINISH HOUSEKEEPING CHORES & CLOSE RASTER:
  DeleteHistogram(WATERMASK);
  CreateHistogram(WATERMASK,0);
  DeletePyramid(WATERMASK);
  CreatePyramid(WATERMASK,0);
  CloseRaster(WATERMASK);
  printf("TO SAVE THE CONSOLE WINDOW TEXT AS A REPORT:\n");
  printf("   1. RIGHT-CLICK IN THE CONSOLE WINDOW.\n");
  printf("   2. SELECT THE Save As... OPTION.\n");
  printf("   3. NAVIGATE TO THE DESIRED LOCATION.\n");
  printf("   4. PROVIDE A REPORT NAME (or OVERWRITE).\n");
  printf("   5. CLICK OK.");