Appendix C Arc/Info macros--AMLs

This Appendix contains the following Arc/Info procedures written in Arc/Info Macro Language (AML):

1) WSHGS.AML - Creates the following grids: stream network, watersheds, and watershed outlets;

2) NEXTWSH.AML - Creates an info file that contains two items: ID and the downstream watershed ID. Also creates a grid of watershed outlets that contains the ID of downstream watershed;

3) RAININFO.AML- Based on the time series stored in the attribute table of the point coverage, and the grid that specify the spatial division into zones, this AML calculates zonal averages and stores them in Arc/Info INFO table;

4) RAINMAP.AML - Creates grids of average monthly precipitation;

5) RAINM2.AML - Creates an INFO table from the grids of average monthly precipitation;

6) SELDATA.AML - Creates point coverage of stations with complete record of flow rate/precipitation depth in selected months;

7) FD4Y.AML - Supports the process of redistribution of the flow rate recorded in theUSGS stations;

8) SLOPE3.AML - Calculates the slope along a flow path;

9) DEMALB - Converts a grid into Albers Equal Area coordinate system;

Appendix C1 WSHGS.AML


/* -----------------------------------------------------------------

/*   Program: WSHGS.AML

/*   Purpose: Creates the following grids: stream network,

/*            watersheds, and watershed outlets. 

/*            The watershed outlet grid contains:

/*            the most upstream cell of the first order stream,

/*            the most downstream cell of each reach, and

the cells

/*            created from a point coverage of USGS stations.

/*            Runs only in GRID

/* -----------------------------------------------------------------

/*     Usage: &r WSHINFO <fdir> <facc> <fbas>

<trsh> <fout> <item> 

/*                   <fwsh> <flpp> <fstr>

/* Arguments: (input)

/*            fdir = (grid) flow direction

/*            facc = (grid) flow accumulation

/*            fbas = (grid) area under investigation (basin,mask)

/*            trsh = (value) threshold for stream delineation

/*                   ( if grids are in meters, then trsh is in km2 )

/*            fout = (point) selected flow gauging stations

/*               (additional pour points for watersheds delineation)

/*            item = (text) name of the item from <fout>which will

/*                   be used in "pointgrid" conversion.

/*

/*            (output, grids that will be created)

/*            fstr = (grid) stream network

/*            fwsh = (grid) drainage areas

/*            flpp = (grid) outlets

/*

/* Temporary: lso, lsl, lmx, lpd, lmn, lpu, lgs, lpx, (grids)

/*   Globals: none (see Temporary)

/*   Locals:  tresh (variable)

/* -----------------------------------------------------------------

/*     Notes: The first order watersheds (and the respective outlets)

/*            have ID number in the range 100000 < ID < 200000.

/*            Cellsize and Window of <fdir> are assumed.

/*            This AML checks neither for the existence and

/*            correctness of the input files nor for the existence

/*            of files that have the same names as the temporary

/*             files and the files to be created.

/*    If an error occurs then all grids and all info files that

/*    have the same name as output grids and temporary grids are

/     erased !!!

/* ----------------------------------------------------------------

/*   History: Author Pawel Mizgalewicz  11/21/93

/*            updated for point coverage  05/15/95

/*            working name net4.aml ( tested for the Cedar River

/*            - Iowa River basin, grid = 3000^2 cells, basin =

/*            3.2*10e6 cells of size 100m*100m).

/*            edited 6/13/95, 6/21/95, 2/19/96

/*=================================================================

&args fdir facc fbas trsh fout item fwsh flpp fstr

&severity &error &routine bailout

&if [SHOW PROGRAM] <> GRID &then ; &return

This only runs in GRID.

&if [null %fstr%] &then

 &do

  &call usage

  &return

 &end

&messages &off



/* GRID settings:

&s xxcell = [show setcell]

&s xxwindow = [show setwindow]

setwindow %fdir%

setcell %fdir%

/* -----------------------------------------------------------------

/* Creating a stream network

/* -----------------------------------------------------------------

/* calculate threshold in number of cells

&sv trsh = %trsh% * 1000000    /* conversion into meters

(map units)

&sv tresh = %trsh% / ( $$cellsize * $$cellsize )

 &type stream delineation ... [date -vfull]

%fstr% = con ( %facc% >= %tresh% AND %fbas% > 0 , 1 )

/* -----------------------------------------------------------------

/* Finding the most downstream cell in each reach (watershed outlet)

/* -----------------------------------------------------------------

 &type streamorder [date -vfull]

lso = streamorder ( %fstr% , %fdir% )

 &type streamlink [date -vfull]

lsl = streamlink ( %fstr% , %fdir% )

 &type zonalmax [date -vfull]

lmx = zonalmax (lsl , %facc% )

 &type lpd condition [date -vfull]

lpd = con ( lmx == %facc% , lsl, 0 )

kill lmx all

/* lpd contains cells that have been selected by finding cell with

/* the largest value of the flowaccumulation within each partial

/*  drainage area.

/* lpd cells have value equal to the stream_ID and the watershed-ID.

/* -----------------------------------------------------------------

/* Finding the most upstream cell in each first order reach

(wshed outlet)

/* -----------------------------------------------------------------

 &type zonalmin [date -vfull]

lmn = zonalmin (lsl , %facc% )

 &type lpu condition [date -vfull]

lpu = con ( lso == 1 AND lmn == %facc%, lsl + 100000 , 0 )

 &type lfs focalsum for first order wshds [date -vfull]

kill lso all ; kill lsl all ; kill lmn all

/* lpu contains cells that have been selected by finding cell with 

/* the smallest value of the flowaccumulation within first order 

/* partial watersheds.

/* lpu cells have value equal to the stream_ID (and the watershed-ID)

/* increased by 100 000. Numbers >100 000 and <200 000

are used to

/* identify first order watersheds

/* (flow gauging station_ids are large numbers, for example 5448500)

/* -----------------------------------------------------------------

/* Converting point coverage into grid. 

/* Cell value = value from %item%

/* -----------------------------------------------------------------

lgs = pointgrid ( %fout% , %item%, #, #, #, ZERO )

/* -----------------------------------------------------------------

/* Creating grid with all potential outlets

/* -----------------------------------------------------------------

 &type all outlets [date -vfull]

lpx = con ( lpu == 0 , con ( lgs == 0 , lpd, lgs ) , lpu )

kill lpu all    /* upper end of first order reach

kill lgs all    /* usgs gauging stations

kill lpd all    /* down (lowest) end of the reach

 &type setnull cells == 0  [date -vfull]

%flpp% = setnull ( lpx < 1, lpx )

&if not [ exists %flpp%.vat -info ] &then ; buildvat %flpp%

kill lpx all

/* all watershed outlets must be located on the stream network.

/* Gauging stations, which are located on the first cell of

the first

/* order stream will not affect the value of the cell e.g.

node number

/* will not be changed.

/* -----------------------------------------------------------------

/* Delineating watersheds

/* -----------------------------------------------------------------

 &type watershed [date -vfull]

%fwsh% = watershed ( %fdir% , %flpp% )

&if not [exists %fwsh%.vat -info] &then ; buildvat %fwsh%

 &type watershed end [date -vfull]

/* restore GRID settings:

setcell %xxcell%

setwindow %xxwindow%

&messages &on

&return

/*-------------

&routine USAGE

/*-------------

&type Usage: &r WSHGS <fdir_grid> <facc_grid>

<basin_grid> <tresh_num>

&type              <fout_point> <item> <watershed>

<pour_points> <streams>

&return &inform

/*-------------

&routine EXIT

/*-------------

/* delete all temporary files:

&if [exists lso -grid ] &then ; kill lso all

&if [exists lsl -grid ] &then ; kill lsl all

&if [exists lmx -grid ] &then ; kill lmx all

&if [exists lpd -grid ] &then ; kill lpd all

&if [exists lmn -grid ] &then ; kill lmn all

&if [exists lpu -grid ] &then ; kill lpu all

&if [exists lgs -grid ] &then ; kill lgs all

&if [exists lpx -grid ] &then ; kill lpx all

/* on error erase grids that exist or that have been created

&if [exists %fwsh% -grid ] &then ; kill %fwsh% all

&if [exists %flpp% -grid ] &then ; kill %flpp% all

&if [exists %fstr% -grid ] &then ; kill %fstr% all

/* restore GRID settings:

setcell %xxcell%

setwindow %xxwindow%

&messages &on

&return

/*--------------

&routine BAILOUT

/*--------------

&severity &error &ignore

&call exit

&return &warning An error has occurred in WSHGS.AML

/* -----------------------------------------------------------------

/*     EXAMPLE OF APPLICATION

/* -----------------------------------------------------------------

/* grid

/*      /* input

/* &s fdir = $HOME/iowa/data/crfdr     /* crfdr = flowdirection(elev_grid)

/* &s facc = $HOME/iowa/data/crfac     /* crfac = flowaccumulation(crfdr)

/* &s fbas = $HOME/iowa/data/crbas     /* basin or watershed

/* &s trsh = 25                        /* units in km2

/* &s fout = $home/iowa/test90/stations/gsflow  /* point coverage

/* &s item = station_id                /* item in %fout% coverage

/*      /* output

/* &s fwsh = crwsh                     /* watersheds

/* &s flpp = crlpp                     /* pour points

/* &s fstr = crstr                     /* stream network

/*

/* &r wshgs %fdir% %facc% %fbas% %trsh% %fout% %item% %fwsh% %flpp% %fstr%

/*

/* q                                   /* quit from GRID

/*

/* &return                             /* return fromexample AML

/* -----------------------------------------------------------------

Appendix C2 NEXTWSH.AML


/* -----------------------------------------------------------------

/*   Program: NEXTWSH.AML

/*   Purpose: Creates an info file that contains two items: ID and

/*            the downstream watershed ID. Also creates a grid of

/*            watershed outlets that contains the ID of downstream

/*            watershed.

/*            Runs only in GRID

/* -----------------------------------------------------------------

/* Usage: &r NEXTWSH <fdir> <fwsh> <flpp>

<infn> <idin> <nxin> <fnxt>

/* Arguments: (input)

/*            fdir = (grid) flow direction

/*            fwsh = (grid) watersheds (partial drainage areas)

/*                          value in VAT = unit_id number

/*            flpp = (grid) pour points ( watershed outlets)

/*                          value in VAT = unit_id number

/*            (output)

/*            infn = (info file) name of the info file to

be created

/*            idin = (item) name of the item in which the

unit_id 

/*                          number will be stored

/*            nxin = (item) name of the item in which the

downstream

/*                          unit_id will be stored

/*            fnxt = (grid) similar to %flpp% (watershed outlets):

/*                     the values in VAT are not equal to

the unit_id

/*                     number, they equal to the downstream

unit

/*                     ID number.

/* Temporary: xxxtmp, xxxcomnx, (GRIDS)

/*            xxxtmp1 (INFO)

/*   Globals:

/*    Locals: see Temporary

/* ----------------------------------------------------------------

/*     Notes: The ID of the next watershed for the most downstream

/*            watershed equals 0.

/*            If the flowdirection in watershed pour point can't be

/*            determined, the next watershed ID = -1

/*        This AML checks neither for the existence and correctness

/*        of the input files nor for the existence of files that have

/*        the same names as the temporary files and the files to be

/*        created.

/*        If an error occurs then all grids and all info files that

/*        have the same name as output grids/info files and temporary

/*            grids/info are erased !!!

/* ---------------------------   ----------------------------------

/*   History: coded by Pawel Mizgalewicz  12/20/93

/*         and converted into a stand alone procedure

/*         6/10 - 6/21/95, 2/19/96

/*  ================================================================

&args fdir fwsh flpp infn idin nxin fnxt

&severity &error &routine bailout

&if [SHOW PROGRAM] <> GRID &then ; &return

This only runs in GRID.

&if [null %fnxt%] &then

 &do

  &call usage

  &return

 &end

&messages &off

/* GRID settings:

&s xxcell = [show setcell]

&s xxwindow = [show setwindow]

setcell %fwsh%

setwindow %fwsh%

/* -----------------------------------------------------------------

&type searching for next (downstream) watershed ...  [date -vfull]

/* -----------------------------------------------------------------

&sv wsh = xxxtmp

%wsh% = con ( isnull ( %fwsh%), 0, %fwsh% )

%fnxt% = con (%flpp% > 0, con (%fdir% == 1, %wsh%(1,0),~

        con (%fdir% == 2, %wsh%(1,1), ~

        con (%fdir% == 4, %wsh%(0,1), ~

        con (%fdir% == 8, %wsh%(-1,1), ~

        con (%fdir% == 16, %wsh%(-1,0), ~

        con (%fdir% == 32, %wsh%(-1,-1), ~

        con (%fdir% == 64, %wsh%(0,-1), ~

        con (%fdir% == 128, %wsh%(1,-1), -1)))))))))

kill xxxtmp all

&if not [exists %fnxt%.vat -info] &then ; buildvat %fnxt%

xxxcomnx = combine ( %flpp% , %fnxt% )

/* the %fnxt% grid is similar to the %flpp%, watershed outlets grid.

/* The cell value is equal to the next watershed ID number

/* -----------------------------------------------------------------

&type Creating the info file  ...  [date -vfull]

/* -----------------------------------------------------------------

arc additem xxxcomnx.vat xxxtmp1 %idin% 4 8 B

arc additem xxxtmp1 xxxtmp1 %nxin% 4 8 B

cursor xxnext declare xxxtmp1 info rw

cursor xxnext open

&do &while %:xxnext.aml$next%

  &s :xxnext.%idin% = [value :xxnext.%flpp%]

  &s :xxnext.%nxin% = [value :xxnext.%fnxt%]

  cursor xxnext next

&end

cursor xxnext close

cursor xxnext remove

arc pullitems xxxtmp1 %infn%

%idin%

%nxin%

end

kill xxxcomnx all

&s x = [delete xxxtmp1 -info]

/* restore GRID settings:

setcell %xxcell%

setwindow %xxwindow%

&messages &on

&return

/*-------------

&routine USAGE

/*-------------

&type Usage: &r NEXTWSH <fdir_grid> <wsh_grid>

<pourpt_grid> <newINF_name>

&type               <id_item_name> <next_id_item_name>

<nextwsh_grid>

&return &inform

/*-------------

&routine EXIT

/*-------------

/* delete all temporary files:

&if [exists xxxtmp1 -info ] &then ; &s x = [delete

xxxtmp1 -info]

&if [exists xxxtmp -GRID] &then ; kill xxxtmp all

&if [exists xxxcomnx -GRID] &then ; kill xxxcomnx all

&if [exists %fnxt% -GRID] &then ; kill %fnxt% all

/* restore GRID settings:

setcell %xxcell%

setwindow %xxwindow%

&messages &on

&return

/*--------------

&routine BAILOUT

/*--------------

&severity &error &ignore

&call exit

&return &warning An error has occurred in NEXTWSH.AML

/* -----------------------------------------------------------------

/*     EXAMPLE OF APPLICATION

/* -----------------------------------------------------------------

/* grid

/*      /* input

/* &s fdir = $HOME/iowa/data/crfdr/* crfdr = flowdirection(elev_grid)

/* &s fwsh = crwsh                /* watersheds

/* &s flpp = crlpp                /* pour points

/*     /* output

/* &s fnxt = crnxt                /* outflow,value=downstream wshd ID

/* &s infn = %fnxt%.dat           /* new info file

/* &s idin = unit_id              /* item (unit ID)

/* &s nxin = next_id              /* item ( downstream unit ID)

/*

/* &run nextwsh %fdir% %fwsh% %flpp% %infn% %idin% %nxin% %fnxt%

/*

/* q                              /* quit from GRID

/*

/* &return                        /* return from example AML

/* -----------------------------------------------------------------



Appendix C3 RAININFO.AML


/* -----------------------------------------------------------------

/*   Program: RAININFO.AML

/*   Purpose: Based on the time series stored in the attribute table

/*            of the point coverage, and the grid that specify the

/*            spatial division into zones, this AML calculates 

/*            zonal averages and stores them in Arc/Info INFO table

/*   Method:  For a given month of the selected period this AML 

/*            creates a grid of spatially distributed feature 

/*            such as rainfall, temperature, or evaporation. 

/*            The Inverse Distance Weighting IDW procedure is 

/*            applied. To use different method only one line

/*            need to be changed. In the next step this AML

/*            calculates average values for all zones, specified by

/*            and stores them in INFO file. The procedure is

/*            repeated for all months from the specified time

/*            Runs only in GRID

/* -----------------------------------------------------------------

/*     Usage: &r RAININFO <fy> <fm> <ty> <tm> <p_data> <zone> 

/*                       <outinf> {id_itm} {prc} {kcell} {prx}

/* Arguments: (input)

/*            fy, fm = from year, from month

/*            ty, tm = to year, to month

/*            p_data = (coverage) point coverage that contains time

/*                     series

/*            zone   = (grid) defines zones and their IDs

/*            outinf = (info) output info file to be created

/*            id_itm = (item) name of the item in which the zone ID

/*                     will be stored, default {id_itm} = zone_id

/*            prc    = (number) the values must be converted

/*                     into integer numbers to create VAT.

/*                     Before conversion they are multiplied by 

/*                     <prc> to preserve decimal part of value.

/*                     default {prc} = 1000 (three decimal places)

/*            kcell  = (number) cell size multiplier. 

/*                     The grid that is created from point coverage 

/*                     will assume the size of the cell from

/*                     <zone> grid multiplied by {kcell}

/*                     default {kcell} = 1

/*            prx    = (prefix) Items that are created

/*                     in <outinf> info file will named as follows:

/*                     <prx> + item name from input coverage <p_data>

/*                     default {prx} = "p".

/* Temporary: xxx, xxx2, xxxcom (grid), xxxtmp1 (info)

/*    Locals: ab, bb, cc, nitem, prfnitem, yr,mt  (variables)

/* -----------------------------------------------------------------

/*     Notes: The following convention is used for naming the items

/*            in PAT (table that stores original time series)

/*            myyyymm,     where:  m indicates monthly values,

/*            yyyy = year, mm = month, for example item m197603

/*            carries data for March 1976.

/* -----------------------------------------------------------------

/*    History: Coded by Pawel Mizgalewicz  05/05/95

/*             Modified 06/18/1995 - 6/21/95,

/*             02,20,96 - defaults added for id_itm prc kcell.

/* -----------------------------------------------------------------

&args fy fm ty tm p_data zone outinf id_itm prc kcell prx

&severity &error &routine bailout

&if [SHOW PROGRAM] <> GRID &then ; &return This only runs in GRID.

&if [null %outinf%] &then

 &do

  &call usage

  &return

 &end

&if [null %prx%] &then ; &s prx = p

&if [null %kcell%] &then ; &s kcell = 1

&if [null %prc%] &then ; &s prc = 1000

&if [null %id_itm%] &then ; &s id_itm = zone_id

&messages &off

/* GRID settings:

&s xxcell = [show setcell]

&s xxwindow = [show setwindow]

&describe %zone%

&sv size = [calc %grd$dx% * %kcell%]

&type cell size = %size%

/* -----------------------------------------------------------------

/*       Make a copy of %zone%.VAT file and add item %zone%

/* -----------------------------------------------------------------

arc additem %zone%.vat xxxtmp1 %zone% 4 5 B

cursor xxcur1 declare xxxtmp1 info rw

cursor xxcur1 open

&do &while %:xxcur1.aml$next%

  &s :xxcur1.%zone% = %:xxcur1.VALUE%

  cursor xxcur1 next

&end

cursor xxcur1 close

cursor xxcur1 remove

/* -----------------------------------------------------------------

/*       Do it for all selected years and months

/* -----------------------------------------------------------------

&DO yr = %ty% &to %fy% &by -1

  &DO mt = 12 &to 1 &by -1

    &s ab = [calc %yr% = %fy%] AND [calc %mt% lt %fm%]

    &s bb = [calc %yr% = %ty%] AND [calc %mt% gt %tm%]

    &s cc = %ab% OR %bb%

    &IF NOT %cc%  &THEN

      &do

        &s templ = mxxxx0y

        &s nitem = [subst %templ% xxxx %yr% ]

        &if %mt% lt 10 &then

            &sv nitem = [subst %nitem% y %mt% ]

          &else

            &sv nitem = [subst %nitem% 0y %mt% ]

        &type  processing item %nitem% ... [date -vfull]

        setcell %size%

        setwindow MAXOF

        xxx = idw ( %p_data% , %nitem%  )

        setcell %zone%

        setwindow %zone%

        xxx2 = int ( %prc% * zonalmean ( %zone% , xxx  ))

        kill xxx all

        xxxcom = combine (%zone%, xxx2)

        kill xxx2 all

        &s prfnitem = %prx%%nitem%

        arc additem xxxcom.vat xxxcom.vat %prfnitem% 4 12 F 4 xxx2

        cursor xxcur1 declare xxxcom.vat info rw

        cursor xxcur1 open

        &do &while %:xxcur1.aml$next%

           &s :xxcur1.%prfnitem% = [calc %:xxcur1.xxx2% / %prc%]

           cursor xxcur1 next

        &end

        cursor xxcur1 close

        cursor xxcur1 remove

        arc joinitem xxxtmp1 xxxcom.vat xxxtmp1 %zone% %zone%

        arc dropitem xxxtmp1 xxxtmp1 xxx2

        kill xxxcom all

      &end

 &end

&end

/* -----------------------------------------------------------------

&type Adding ID item ... [date -vfull]

/* -----------------------------------------------------------------

arc additem xxxtmp1 xxxtmp1 %id_itm% 4 8 B # %zone%

cursor xxcur2 declare xxxtmp1 info rw

cursor xxcur2 open

&do &while %:xxcur2.aml$next%

           &s :xxcur2.%id_itm% = [value :xxcur2.%zone%]

           cursor xxcur2 next

&end

cursor xxcur2 close

cursor xxcur2 remove

/* -----------------------------------------------------------------

&type Cleaning INFO table (dropping redundant items) .. [date -vfull]

/* -----------------------------------------------------------------

arc dropitem xxxtmp1 xxxtmp1 value

arc dropitem xxxtmp1 xxxtmp1 count

arc dropitem xxxtmp1 %outinf% %zone%

&s x = [delete xxxtmp1 -info]

/* restore GRID settings:

setcell %xxcell%

setwindow %xxwindow%

&messages &on

&return

/*-------------

&routine USAGE

/*-------------

&type Usage: &r RAININFO <fy> <fm> <ty> <tm> <pnt_data> <zone>

&type                   <out_inf> <ID_item> <prc> <kcell> {prefix}

&return &inform

/*-------------

/* &routine CHECK

/*-------------

/* IF temporary file exists, inform and exit

/* If file to be build exists , inform and exit

/* If input file is not correct or does not exist, inform and exit

/* return

/*-------------

&routine EXIT

/*-------------

/* delete all temporary files:

&if [exists xxxtmp1 -info ] &then;   &s x = [delete xxxtmp1 -info]

&if [exists xxx -GRID] &then ; kill xxx all

&if [exists xxx2 -GRID] &then ; kill xxx2 all

&if [exists xxxcom -GRID] &then ; kill xxxcom all

/* restore GRID settings:

setcell %xxcell%

setwindow %xxwindow%

&messages &on

&return

/*--------------

&routine BAILOUT

/*--------------

&severity &error &ignore

&call exit

&return &warning An error has occurred in RAININFO.AML

/* -----------------------------------------------------------------

/*     EXAMPLE OF APPLICATION

/* -----------------------------------------------------------------

/* grid

/* &s fy = 1990

/* &s fm = 1

/* &s ty = 1990

/* &s tm = 12

/* &s pdat = ../stations/gsrain

/* &s zone = crwsh

/* &s id unit_id

/*

/* &r raininfo %fy% %fm% %ty% %tm% %pdat% %zone% prcinf2 %id% 10000 10 r

/*

/* q

/* &return

/* -----------------------------------------------------------------

Appendix C4 RAINMAP.AML


/* -----------------------------------------------------------------

/*   Program: RAINMAP.AML

/*   Purpose: Creates GRIDS of average monthly precipitation 

/*            (10e-3 cm/d -- value from PAT is multiplied by 1000,

/*             assumed cell size = 1000)

/*            Stations that have complete record of precipitation for

/*            a given month (from specified time period) are used.

/*            rainm2.aml uses output to put values into an INFO file

/*            Runs only in GRID

/* -----------------------------------------------------------------

/*     Usage: &r RAINMAP <fy> <fm> <ty> <tm> <xxxdat> {YES|NO}

/* Arguments: (input)

/*            fy, fm = from year, from month

/*            ty, tm = to year, to month

/*            xxxdat = info file that contains precipitation depth

/*            yes|no = yes - include records with values = 0

/*                     no - exclude records and stations that have 0

/*                          (default = no)

/* Temporary: xxxxx1

/*    Locals: templ, nitem, nit, yr,mt, selec (variables)

/* -----------------------------------------------------------------

/*     Notes: Data are stored in PAT (point). Each record is related

/*            to gauging station. The following naming convention is 

/*            used for items: myyyymm, where:

/*            m = indicates monthly values,

/*            yyyy = year, mm = month, for example, item m197603

/*            contains records for March 1976

/*            Info file is created by rainm2.aml

/* -----------------------------------------------------------------

/*    History: Coded by Pawel Mizgalewicz  08/08/95

/*             edited 02/20/96

/* -----------------------------------------------------------------

&args fy fm ty tm xxxdat zero

&severity &error &routine bailout

&if [SHOW PROGRAM] <> GRID &then ; &return This only runs in GRID.

&if [null %xxxdat%] &then

 &do

  &call usage

  &return

 &end

&if [null %zero%] &then ; &s zero = YES

&s xx = [translate %zero%]

&select %xx%

  &when NO

    &s klm = gt

  &when YES

    &s klm = ge

  &otherwise

    &do

      &call usage

      &return

    &end

&end /* select

&messages &off

setwindow maxoff

setcell 1000

&DO yr = %fy% &to %ty%

  &DO mt = 1 &to 12

    &s ab = [calc %yr% = %fy%] AND [calc %mt% lt %fm%]

    &s bb = [calc %yr% = %ty%] AND [calc %mt% gt %tm%]

    &s cc = %ab% OR %bb%

    &IF NOT %cc%  &THEN

      &do

        /* &s nit = %nit% + 1

        &s templ = mxxxx0y

        &s nitem = [subst %templ% xxxx %yr% ]

        &if %mt% lt 10 &then

             &sv nitem = [subst %nitem% y %mt% ]

           &else

             &sv nitem = [subst %nitem% 0y %mt% ]

       &type %nitem%

       /* &s namit%nit% = %nitem%

       &sv selec = res %nitem% %klm% 0

       &type selecting complete records %selec%

arc reselect %xxxdat% xxxxx1 point

%selec%

~

n

n

      &end

    arc build xxxxx1 point

    p%nitem% = int ( 1000 * idw ( xxxxx1, %nitem% ) )

    kill xxxxx1 all

  &end

&end

&messages &on

&return

/*-------------

&routine USAGE

/*-------------

&type Usage: &r RAINMAP <from_yr> <from_mt> <to_yr> <to_mt> <data_point>

&type               {YES|NO}

&return &inform

/*-------------

&routine EXIT

/*-------------

/* delete all temporary files:

&if [exists xxxxx1 -COVER] &then ; kill xxxxx1 all

&messages &on

&return

/*--------------

&routine BAILOUT

/*--------------

&severity &error &ignore

&call exit

&return &warning An error has occurred in RAINMAP.AML

/* -----------------------------------------------------------------

/*     EXAMPLE OF APPLICATION

/* -----------------------------------------------------------------

/*      /* input

/* &s data = $HOME/iowa/data/gsprec    /* point coverage and data

/* grid

/* &run rainmap 1940 1 1993 6 %data% YES

/* q

/* &return                             /* return to Arc/Info prompt

/* -----------------------------------------------------------------

Appendix C5 RAINM2.AML


/* -----------------------------------------------------------------

/*   Program: RAINM2.AML

/*   Purpose: Creates an INFO table from the grids of average 

/*            monthly precipitation (needs grid of zones)

/*            (10e-3 cm/d -- value from PAT was multiplied by 1000,

/*             in original rainmap.aml assumed cell size = 1000 )

/*            (rainmap.aml creates grids of precipitation)

/*            Runs only in GRID, output: integer values !!!

/* -----------------------------------------------------------------

/*     Usage: &r RAINM2 <fy> <fm> <ty> <tm> <ppath> <zones> <idname>

/*                      <frominfo> <toinfo>

/* Arguments: (input)

/*            fy, fm = from year, from month

/*            ty, tm = to year, to month

/*            ppath = path to directory that contains grids of 

/*                    precipitation

/*            zones = zones for calculatuing average prec. depth

/*            idname = same as zones, contains identification

/*                      number for zones (unit_id, modeling units)

/*            frominfo = info file that contains station id numbers

/*            toinfo = output info file

/*                     copy of frominfo + precipitation items

/* Temporary: xxxcom

/*    Locals: templ, nitem, nit, yr,mt,(variables)

/* -----------------------------------------------------------------

/*     Notes: Post processor for RAINMAP.AML

/*            RAINMAP.aml changes units, here means are converted to

/*            integer values !!!

/*            The following naming convention is 

/*            used for items: myyyymm, where:

/*            m = indicates monthly values,

/*            yyyy = year, mm = month, for example, item m197603

/*            contains records for March 1976

/*            Info file is created by rainm2.aml

/* -----------------------------------------------------------------

/*    History: Coded by Pawel Mizgalewicz  95

/*             edited 02/20/96

/* -----------------------------------------------------------------

&args fy fm ty tm ppath zones idname frominfo toinfo

/* &s ppath = ../rain25/

/* &s zones = crwsh

/* &s idname = unit_id

/* &s frominfo = crwshc.dat

/* &s toinfo = crwshc.datp

/* &sv fy = 1990

/* &sv fm = 1

/* &sv ty = 1990

/* &sv tm = 12

copyinfo %frominfo% %toinfo%

&type ...aml, start  [date -vfull]

grid

&messages &off

setwindow %zones%

setcell %zones%

/* %idname% = %zones%

/* repeat in reverse order

&DO yr = %ty% &to %fy% &by -1

  &DO mt = 12 &to 1 &by -1	

    &IF %mt% ge %fm% and %mt% le %tm% &THEN

      &do

        &s templ = mxxxx0y

        &s nitem = [subst %templ% xxxx %yr% ]

        &if %mt% lt 10 &then

             &sv nitem = [subst %nitem% y %mt% ]

           &else

             &sv nitem = [subst %nitem% 0y %mt% ]

&type p%nitem% processing ... [date -vfull]

p%nitem% = zonalmean ( %zones% , %ppath%p%nitem%

xxxcom = combine ( %idname% , p%nitem% )

kill p%nitem% all

&sys arc joinitem %toinfo% xxxcom.vat %toinfo% %idname% %idname%

kill xxxcom all

     &end

  &end

&end

&type rain series - point cover created, exiting ... [date -vfull]

&return

Appendix C6 SELDATA.AML


/* -----------------------------------------------------------------

/*   Program: SELDATA.AML

/*   Purpose: Selects the items that contain data for specified

/*            time period. Then, selects gauging stations that

/*            have complete record (no missing data).

/*            Creates point coverage of selected stations with data

/*            for specified months

/*            Runs only in ARC

/* -----------------------------------------------------------------

/*     Usage: &r SELDATA <xxxdat> <xxxhed> <joinit> <xxxout> {YES|NO}

/* Arguments: (input)

/*            fy, fm = from year, from month

/*            ty, tm = to year, to month

/*            xxxdat = info file that contains flow database

/*            xxxhed = point coverage that represents location of

/*                     observation stations. xxxdat and xxxhed must

/*                     contain common item to relate these tables

/*                    (for example item station_id)

/*            joinit = name of the common item for both tables

/*            (output)

/*            xxxout = point coverage which contains selected flow 

/*                     records or precipitation measurements,

/*                     as well as selected gauging stations.

/*            yes|no = yes - include records with values = 0

/*                     no - exclude records and stations that have 0

/*                          (default = no)

/* Temporary: %xxxout%tmp (cover), xxxxx1 (info)

/*    Locals: temp1, nitem, nit, yr,mt, selec (variables)

/* -----------------------------------------------------------------

/*     Notes: Data file is an INFO file. Each record is related to

/*            gauging station. The following naming convention is

/*            used for items:

/*            myyyymm, where m indicates monthly values,

/*            yyyy = year, mm = month, for example item m197603

/*            contains records for March 1976

/*            Due to the Arc/Info restrictions only about 65-month

/*            period can be selected in single run of this AML

/* -----------------------------------------------------------------

/*    History: Coded by Pawel Mizgalewicz  05/05/95

/*             Modified 06/15/1995, edited 2/20/96

/* -----------------------------------------------------------------

&args fy fm ty tm xxxdat xxxhed joinit xxxout zero

&severity &error &routine bailout

&if [SHOW PROGRAM] <> ARC &then ; &return This only runs in ARC.

&if [null %xxxout%] &then

 &do

  &call usage

  &return

 &end

&if [null %zero%] &then ; &s zero = no

&s xx = [translate %zero%]

&select %xx%

  &when NO

    &s klm = gt

  &when YES

    &s klm = ge

  &otherwise

    &do

      &call usage

      &return

    &end

&end /* select

/* -----------------------------------------------------------------

/*       Make a copy of point coverage

/* -----------------------------------------------------------------

copy %xxxhed% %xxxout%tmp   /* create temporary file

&sv pultmp = xxxxx1         /* temporary info file

/* -----------------------------------------------------------------

&type Selecting years and months  ...          [date -vfull]

/* -----------------------------------------------------------------

PULLITEMS %xxxdat% %pultmp%

%joinit%

&DO yr = %fy% &to %ty%

  &DO mt = 1 &to 12

    &s ab = [calc %yr% = %fy%] AND [calc %mt% lt %fm%]

    &s bb = [calc %yr% = %ty%] AND [calc %mt% gt %tm%]

    &s cc = %ab% OR %bb%

    &IF NOT %cc%  &THEN

      &do

        &s templ = mxxxx0y

        &s nitem = [subst %templ% xxxx %yr% ]

        &if %mt% lt 10 &then

            &sv nitem = [subst %nitem% y %mt% ]

          &else

            &sv nitem = [subst %nitem% 0y %mt% ]

        &type  selecting item %nitem%

%nitem%

      &end

 &end

&end

end

/* join selected items with header file (point coverage)

joinitem %xxxout%tmp.pat xxxxx1 %xxxout%tmp.pat %joinit% %joinit%

&sv st = [delete xxxxx1 -info]

/* -----------------------------------------------------------------

&type Selecting gauging stations that have full record  ...  [date -vfull]

/* -----------------------------------------------------------------

reselect %xxxout%tmp %xxxout% point

&DO yr = %fy% &to %ty%

  &DO mt = 1 &to 12

    &s ab = [calc %yr% = %fy%] AND [calc %mt% lt %fm%]

    &s bb = [calc %yr% = %ty%] AND [calc %mt% gt %tm%]

    &s cc = %ab% OR %bb%

    &IF NOT %cc%  &THEN

      &do

        /* &s nit = %nit% + 1

        &s templ = mxxxx0y

        &s nitem = [subst %templ% xxxx %yr% ]

        &if %mt% lt 10 &then

             &sv nitem = [subst %nitem% y %mt% ]

           &else

             &sv nitem = [subst %nitem% 0y %mt% ]

       &type %nitem%

       /* &s namit%nit% = %nitem%

       &sv selec = res %nitem% %klm% 0

       &type selecting complete records %selec%

%selec%

~

n

y

      &end

  &end

&end

&type end

%selec%

~

n

n

build %xxxout% point

kill %xxxout%tmp all

&return

/*-------------

&routine USAGE

/*-------------

&type Usage: &r SELDATA <from_yr> <from_mt> <to_yr> <to_mt> <data_info>

&type              <station_cover> <join_item> <out_cover> {YES|NO}

&return &inform

/*-------------

&routine EXIT

/*-------------

/* delete all temporary files:

&if [exists %pultmp% -info ] &then ; &s x = [delete %pultmp% -info]

&if [exists %xxxout%tmp -COVER] &then ; kill %xxxout%tmp all

&messages &on

&return

/*--------------

&routine BAILOUT

/*--------------

&severity &error &ignore

&call exit

&return &warning An error has occurred in SELDATA.AML

/* -----------------------------------------------------------------

/*     EXAMPLE OF APPLICATION

/*------------------------------------------------------------------

/*      /* input

/* &s data = $HOME/iowa/data/gsflow.pat /* Info file of monthly flows

/* &s head = $HOME/iowa/data/crfhd    /* coverage of gauging stations

/* &s cname  = station_id             /* common item

/*     /* output

/* &s fout = mflow91                  /* output coverage

/*

/* &run seldata 1991 1 1991 12 %data% %head% %cname% %fout% NO

/*

/* &return                           /* return to Arc/Info prompt

/* -----------------------------------------------------------------

Appendix C7 FD4Y.AML


/* -----------------------------------------------------------------

/* fd4y.aml 

/*          1) Creates ASCII files that contain data required

/*             for C program fd4y. ( fd4y calculates flow rate in

/*             all modeling units based on the flow recorded in

/*             USGS gauging stations). 

/*          2) Stores the results obtained from fd4y program in

/*             PAT--polygon attribute table

&s prc = unprec.pat

&s flw = gsfl28.pat

&s out = unflow.pat

&s fy = 1960

&s fm = 1

&s ty = 1992

&s tm = 9

/*  input1 xxxgsin, ASCII file

/*  input2 xxxunin, ASCII file

/*  output xxxunout, ASCII file

tables

&DO yr = %ty% &to %fy% &by -1

  &DO mt = 12 &to 1 &by -1

    &s ab = [calc %yr% = %fy%] AND [calc %mt% lt %fm%]

    &s bb = [calc %yr% = %ty%] AND [calc %mt% gt %tm%]

    &s cc = %ab% OR %bb%

    &IF NOT %cc%  &THEN

      &do

        &s templ = mxxxx0y

        &s nitem = [subst %templ% xxxx %yr% ]

        &if %mt% lt 10 &then

            &sv nitem = [subst %nitem% y %mt% ]

          &else

            &sv nitem = [subst %nitem% 0y %mt% ]

        &type  processing item %nitem% ... [date -vfull]

/* =====================================================

select %flw%

unload xxxgsin station_id station_nx %nitem% DELIMITED INIT

select %prc%

unload xxxunin unit_id unit_nx gswsh area_km2 order p%nitem% DELIMITED INIT

&sys fdy4 xxxgsin xxxunin xxxunout

&s i = 0

&do &until [exists xxxunout -file]

  &s i = %i% + 1

&end

define %nitem%.dat

unit_id,4,8,B

q%nitem%,4,12,F,4

~

add from xxxunout

select %nitem%.dat

&s x = [delete xxxunout -file]

&if %x% eq 0 &then

   &type file xxxunout has been deleted

&else

   &type ERROR: could not delete file xxxunout

&s x = [delete xxxgsin -file]

&if %x% eq 0 &then

   &type file xxxgsin has been deleted

&else

   &type ERROR: could not delete file xxxgsin

&s x = [delete xxxunin -file]

&if %x% eq 0 &then

   &type file xxxunin has been deleted

&else

   &type ERROR: could not delete file xxxunin

&sys arc joinitem %out% %nitem%.dat %out% unit_id order

kill %nitem%.dat

      &end

 &end

&end

q

q

&return

Appendix C8 SLOPE3.AML


/* -----------------------------------------------------------------

/*   Program: FSLOPE.AML

/*   Purpose: Calculates slope along the flow path. Calculates the

/*            difference between the elevation of the current cell

/*            and the elevation of the next cell on the flow pathand

/*            divides the result by the distance between these cells

/*            Runs only in GRID

/* -----------------------------------------------------------------

/*     Usage: &r FSLOPE <fdir> <dem> <fslp>

/* Arguments: (input)

/*            fdir = (grid) flow direction

/*            dem  = (grid) elevation

/*            (output)

/*            fslp = (grid) slope grid 

/* Temporary: none

/* -----------------------------------------------------------------

/*     Notes: If flow direction is

/*            different than 1, 2, 4, 8, 16, 32, 64, and 128, 

/*            the slope  0 is assumed. 

/*            Vertical and horizontal units shuld be the same

/* -----------------------------------------------------------------

/*   History: Coded by Pawel Mizgalewicz  10/02/94 (slope3.aml)

/*            edited 16/06/95, 21/06/95, 02/24/96

/*            ( version with DOCELL -> slope3.aml)

/*            ( version with CONdition -> slope2.aml )

/*  =================================================================

&args fdir dem fslp

&severity &error &routine bailout 

&if [SHOW PROGRAM] <> GRID &then ; &return This only runs in GRID.

&if [null %fslp%] &then 

 &do

  &call usage

  &return

 &end

&messages &off

/* GRID settings:

&s xxcell = [show setcell]

&s xxwindow = [show setwindow]

setcell minof

setwindow maxof

/* -----------------------------------------------------------------

&type calculating slope ... [date -vfull]  

/* -----------------------------------------------------------------

&describe %dem%

&sv diag = 1.414213562 * %grd$dx%

DOCELL

  if (%fdir% == 1)

    %fslp% = ( %dem% - %dem%(1,0) ) / %grd$dx%

  else

    if (%fdir% == 2)

      %fslp% = ( %dem% - %dem%(1,1) ) / %diag%

    else

      if (%fdir% == 4)

        %fslp% = ( %dem% - %dem%(0,1) ) / %grd$dx%

      else

        if (%fdir% == 8)

            %fslp% = ( %dem% - %dem%(-1,1) ) /  %diag%

        else

          if (%fdir% == 16)

              %fslp% = ( %dem% - %dem%(-1,0) ) / %grd$dx%

          else

            if (%fdir% == 32)

               %fslp% = ( %dem% - %dem%(-1,-1) ) / %diag%

            else

              if (%fdir% == 64)

                 %fslp% = ( %dem% - %dem%(0,-1) ) / %grd$dx%

              else

                if (%fdir% == 128)

                   %fslp% = ( %dem% - %dem%(1,-1) ) / %diag%

                else

                   %fslp% = 0 

END

&type end of fslope [date -vfull]

/* restore GRID settings:

setcell %xxcell%

setwindow %xxwindow%

&messages &on

&return

/*-------------

&routine USAGE

/*-------------

&type Usage: &r FSLOPE <fdir_grid> <elevation_grid> <slope_grid>

&return &inform 

/*-------------

&routine EXIT

/*-------------

/* on error delete slope grid:

&if [exists %fslp% -GRID] &then ; kill %fslp% all

/* restore GRID settings:

setcell %xxcell%

setwindow %xxwindow%

&return 

/*--------------

&routine BAILOUT

/*--------------

&severity &error &ignore

&call exit

&return &warning An error has occurred in FSLOPE.AML

/* -----------------------------------------------------------------

/*     EXAMPLE OF APPLICATION slope of stream reaches

/* -----------------------------------------------------------------

/* grid                                /* start GRID

/*      /* input

/* &s fdir = $HOME/iowa/data/crfdr

/*                             /* crfdr = flowdirection(elev_grid)

/* &s dem  = $HOME/iowa/data/crdem     /* elevation grid

/* &s fstr = crstr

/*     /* output

/* &s fslp = crslp                     /* slope grid

/* &s strs = strslp                    /* stream slope

/*

/* /* create slope grid    

/* &run fslope %fdir% %dem% %fslp%

/*

/* /* assign unique number to each reach

/* xxxlsl = streamlink(%fstr%)

/*

/* /* calculate average slope

/* %strs% = zonalmean ( xxxlsl , %fslp% )

/*

/* kill xxxlsl all                     /* delete temporary grid 

/* kill %fslp% all                     /* delete slope grid

/* q                                   /* quit from GRID

/*

/* &return                             /* return from example AML

/* -----------------------------------------------------------------

Appendix C9 DEMALB.AML


/* DEMALB.AML - converts a grid into Albers Equal Area coordinates

&args input output csize

%output% = project (%input% , #, NEAREST, %csize% )

OUTPUT

Projection    ALBERS

Zunits        NO

Units         METERS

Datum         NAD83

Spheroid      GRS1980

Xshift        0.0000000000

Yshift        0.0000000000

Parameters

 29 30  0.000 /* 1st standard parallel

 45 30  0.000 /* 2nd standard parallel

-96  0  0.000 /* central meridian

 23  0  0.000 /* latitude of projection's origin

0.00000 /* false easting (meters)

0.00000 /* false northing (meters)

END

&return