/*  Program:  image2grid.aml
/*
/*  Purpose:  Converts an Arc-supported image into a grid.
/*
/*  Usage:    &r image2grid <in_image> <output_grid> <output_res> 
/*                          <rotation_angle> {clip_cover} {datum} {UTM_Zone}
/*     Ex:    &r image2grid clipg6.tif g6_drg 5 1.682 g6n nad27 13 (clockwise)
/*            &r image2grid golden.bil golgrd 5 -0.119 golnt NAD83 (countclkws)
/*            &r image2grid golden.tif golgrd 5 0 golnt NAD83 13 (no rotation)
/*            &r image2grid golden.bip golgrd 5 0    (no clipping or rotation)
/*
/*  Input:  Any image supported by Arc
/*
/*  Output: Output grid is resampled to the provided %outres% resolution,
/*          clipped, and rotated to the provided %angle%.
/*
/*          A colormap file named <output_grid>.colmap is also produced.
/*
/*  Notes:  Run this program without arguments to see the usage and examples.
/*
/*          If an angle of 0 is entered, the output grid will not be rotated.
/*          If no clip cover is entered, output grid will not be clipped.
/*          If no datum is given, the output grid's datum will be guessed from
/*          the type of input image; BIL = NAD83 (DOQs), TIFF = NAD27 (DRGs)
/*          If no UTM zone is passed in, no zone information will exist
/*          in the output grid's projection file.
/*
/*  History:  JKosovich  02/20/97  Coding for abandoned mine lands, based on 
/*                                 PGE AMLs drg2grid, doq2grid
/*            JKosovich  04/23/97  Modified usage return message
/*            JKosovich  09/10/97  Modified comments
/*
/*******************************************************************************
/*
&args imgfile outgrid outres angle clipcov datum zone
&severity &error &routine bailout
&s prog = [ENTRYNAME %aml$file% -NOEXT -FILE]     /* current AML being run

/*
/***** Check for correct usage and correct argument types                  *****
/*
&if [NULL %angle%] &then
  &do
    &s error_case = 1
    &call error_cond
  &end

&if ^ [EXISTS %imgfile% -FILE] &then
  &do
    &s error_case = 2
    &call error_cond
  &end
&else &if ^ [EXISTS %imgfile% -IMAGE] &then
  &do
    &s error_case = 3
    &call error_cond
  &end

&if [LENGTH %outgrid%] > 12 &then
  &do
    &s error_case = 4
    &call error_cond
  &end

&do xx &list outres angle                         /* these can be int or real
  &if [TYPE [VALUE %xx%]] > 0 &then
    &do
      &s error_case = 5
      &call error_cond
    &end
&end

&if ^ [NULL %clipcov%] &then
  &do
    &if %clipcov% = # &then
      &s clipcov = 
  &end
&if ^ [NULL %clipcov%] &then
  &do
    &s clip = .TRUE.
    &if ^ [EXISTS %clipcov% -COVER] &then
      &do
        &s error_case = 6
        &call error_cond
      &end
    &else &if ^ [EXISTS %clipcov% -POLYGON] &then
      &do
        &s error_case = 7
        &call error_cond
      &end
  &end
&else
  &s clip = .FALSE.

&if ^ [NULL %datum%] &then                         /* if # passed in to skip
  &do
    &if %datum% = # &then
      &s datum = 
  &end
&if ^ [NULL %datum%] &then
  &do
    &s datum = [UPCASE %datum%]
    &if [TYPE %datum%] ^= 1 &then
      &do
        &s error_case = 8
        &call error_cond
      &end
    &else &if %datum% ^= NAD27 & %datum% ^= NAD83 &then
      &do
        &s error_case = 8
        &call error_cond
      &end
  &end

/*
/***** Set variables                                                       *****
/*
&s proj = UTM
&s units = meters
&s resample = .FALSE.
&s rotate = .TRUE.                                    /* Don't rotate if 0 ang
&if %angle% = 0 &then &s rotate = .FALSE.
&if %angle% = 0.0 &then &s rotate = .FALSE.

&describe %imgfile%
&s format = [UPCASE %img$format%]                     /* eg BIL, TIFF, etc.
&if [NULL %datum%] &then
  &do
    &if %format% in {'BIL','BIP'} &then
      &s datum = NAD83                                /* BIL & BIP = DOQ = NAD83
    &else &if %format% = TIFF &then
      &s datum = NAD27                                /* TIFF = DRG = NAD27
    &else
      &s datum = UNKNOWN
  &end

/* 
/***** Convert image to Arc grid                                           *****
/*
&if [EXISTS xxgrd1 -GRID] &then KILL xxgrd1 ALL
IMAGEGRID %imgfile% xxgrd1  %outgrid%.colmap

/*
/***** Define projection for grid                                          *****
/*
PROJECTDEFINE GRID xxgrd1
  PROJECTION %proj%
  DATUM %datum%
  UNITS %units%
  &if [VARIABLE zone] &then
    ZONE %zone%
  PARAMETERS
  &if [UPCASE [SHOW program]] = PRJDEF &then END

/* 
/***** Determine input grid's resolution, resample if necessary            *****
/*
&describe xxgrd1
&s ingridres = %grd$dx%       /* resolution of incoming grid
&if %ingridres% ^= %outres% &then
  &s resample = .TRUE.

&if %resample% &then
  &do
    DISPLAY 0
    GRID
      &if [EXISTS xxgrd2 -GRID] &then KILL xxgrd2 ALL
      xxgrd2 = RESAMPLE(xxgrd1,%outres%)
    QUIT
  &end

&if [EXISTS xxgrd2 -GRID] &then
  &do
    PROJECTCOPY GRID xxgrd1 GRID xxgrd2
    KILL xxgrd1 ALL
  &end
&else
  RENAME xxgrd1 xxgrd2

/* 
/***** Clip grid to %clipcov% extent                                       *****
/*
&if [EXISTS xxgrd3 -GRID] &then KILL xxgrd3 ALL

&if %clip% &then
  LATTICECLIP xxgrd2 %clipcov% xxgrd3 MINIMUM 1

&if [EXISTS xxgrd3 -GRID] &then
  &do
    PROJECTCOPY GRID xxgrd2 GRID xxgrd3
    KILL xxgrd2 ALL
  &end
&else
  RENAME xxgrd2 xxgrd3

/*
/***** Rotate grid to due N-S orientation for plotting                     *****
/*
&if [EXISTS %outgrid% -GRID] &then KILL %outgrid% ALL
&if %rotate% &then
  GRIDROTATE xxgrd3 %outgrid% %angle%
&if [EXISTS %outgrid% -GRID] &then
  &do
    PROJECTCOPY GRID xxgrd3 GRID %outgrid%
    KILL xxgrd3 ALL
  &end
&else
  RENAME xxgrd3 %outgrid%

&type \\*** %prog% successful. %format% image converted to grid %outgrid%\\
/*
/*
&return

/*
/*------------------------------------------------------------------------------
/*------------------------------  ROUTINE LIST  --------------------------------
/*------------------------------------------------------------------------------
/*
/*
/*------------------
&routine ERROR_COND
/*------------------
/*
&severity &error &routine bailout
&if [UPCASE [SHOW PROGRAM]] = TABLES &then QUIT
&messages &on

&type \\***************  PROGRAM [TRANSLATE %prog%] HALTED  *****************
/*
&if %error_case% = 1 &then  /***** Incorrect usage
  &do
    &type USAGE: &r %prog% <image_file> <output_grid> <output_res>
    &type                      <rotation_angle> {clip_cover} {datum} {UTM_Zone}
    &type    Ex: &r %prog% clipg6.tif g6_drg 5 1.682 g6n nad27 13 (clockwise)
    &type        &r %prog% golden.tif golgrd 5 -0.119 golnt NAD83 (countclkws)
    &type        &r %prog% golden.bil goldoq 5 0 golnt NAD83 13 (no rotation)
    &type        &r %prog% golden.bip golgrd 5 0    (no clipping or rotation)
    &type \OUTPUT GRID NAME must be 12 characters or less.
    &type OUTPUT RESOLUTION is in ground units (eg. meters).
    &type Use positive ANGLE to rotate output grid clockwise to grid north,
    &type Use negative ANGLE to rotate output grid counterclockwise.
    &type Use ANGLE of 0 for no rotation.
    &type If no CLIP COVER is entered, output grid will not be clipped.
    &type If clipping, CLIP COVER must have POLYGON topology.
    &type If no DATUM is given, the output grid's datum will be guessed from
    &type the type of input image: BIL = NAD83 (DOQs), TIFF = NAD27 (DRGs).
    &type If used, DATUM must be either NAD27 or NAD83 (upper or lower case OK).
    &type UTM ZONE is optional.
  &end
&if %error_case% = 2 &then  /***** If image file not found
  &type          Image file %imgfile% does not exist!!!
&if %error_case% = 3 &then  /***** If image file entered is not valid image
  &type          Image file %imgfile% format is not supported by ARC/INFO!!!
&if %error_case% = 4 &then  /***** If output grid name too long
  &type          Output grid name is too long - must be 12 characters or less!!!
&if %error_case% = 5 &then  /***** Argument must be int or real
  &type          Argument %xx% must be an integer or real value!!!
&if %error_case% = 6 &then  /***** If clip cover not found
  &type          Clip coverage %clipcov% does not exist!!!
&if %error_case% = 7 &then  /***** If clip cover not a POLY cover
  &type          Clip coverage %clipcov% does not have POLYGON topology!!!
&if %error_case% = 8 &then  /***** Datum must be NAD27 or NAD83
  &type          Datum argument must be NAD27 or NAD83 (upper or lower case OK)!
/*
&type      Process halted [DATE -DOW] [DATE -CAL] [DATE -AMPM]
&type *********************************************************\\
/*
&return; &return &error
/*
/*
/*------------------
&routine BAILOUT
/*------------------
/*
&severity &error &ignore
&type \*** Error occurred at line %aml$errorline% in %aml$errorfile%\
&return; &return &error
/*
/*