UW FE FRIS2FVS/SVS Conversion Program AML

/* arc2fvs.aml
/*------------------------------------------------------
/* arc2fvs.aml written by:
/*
/*         Luke Rogers
/*         Forest Engineering
/*         College of Forest Resources
/*         University of Washington
/*         e-mail: lwrogers@u.washington.edu
/*         
/*         Created: May 24, 1998
/*
/*-------------------------------------------------------
/*
/* calls:
/*       amls:
/*       menus:
/*
/* returns:
/*
/*-------------------------------------------------------
&args info_main info_tree info_expan port_name health_status

&if [null %health_status%] &then
   &return Usage: ARC2FVS <FIU.MAIN> <PLT.TREE> <FIU.EXPAN> <SLF_FILE> <ALIVE | DEAD | BOTH>

&if %:program% ne ARCPLOT &then
   &return Please run from ArcPlot

/* set some essential variables
/*&sv cov_name [getcover . -POLYGON 'Stand Coverage' -SORT]
/*&setvar cover [entryname %cov_name%]
/*&sv info_main [getfile * -INFO 'FIU.MAIN FILE' -SORT]
/*&sv info_tree [getfile * -INFO 'PLT.TREE FILE' -SORT]
/*&sv info_expan [getfile * -INFO 'FIU.EXPAN FILE' -SORT]
/*&sv port_name [response 'Stand Line File Name' arc2fvs.slf] 
&setvar file %port_name%.slf
&if %health_status% = 'ALIVE' or %health_status% = 'alive' &then
&do
   &ty Creating Live Plot Data for FVS...
   &sv health = 1
   &call set_up
   &call do_fvs_files
&end
&else &if %health_status% = 'DEAD' or %health_status% = 'dead' &then
&do
   &ty Creating Dead Plot Data for FVS...
   &sv health = 2
   &call set_up
   &call do_fvs_files
&end
&else &if %health_status% = 'BOTH' or %health_status% = 'both' &then
&do
   &ty Creating Live and Dead plot data for FVS...
   &sv health = 3
   &call set_up
   &return
   &sv health = 1
   &sv health_status = alive
   &call do_fvs_files
   &sv health = 2
   &sv health_status = dead
   &call do_fvs_files
&end
&else
   &return Must select LIVE, DEAD or BOTH plot type


/* create attributes Slope, Aspect and
/* Elevation if the DEM method is selected
/*&if [calc %.method% = 1] &then
   /*&do
      /*arc grid
      /*&r avg_elev.aml %dem% %cover%
      /*&r avg_slp.aml %dem% %cover%
      /*&r avg_asp.aml %dem% %cover%
      /*quit
   /*&end

&call clean_up

&return
/***********************************************************
&routine set_up

/* close any open files, delete any strays
&setvar closeall [close -all]
&if [exists %file% -file] &then
   &setvar del [delete %file% -file]

/* create a file of the unique stand numbers in the coverage
/* so that each stand number will only be written once
clearselect
/*reselect %cover% poly area > 0 /* don't write the global poly
/*&setvar list &list [listunique %cover% -poly riu.id fvs_temp]
&setvar list &list [listunique %info_main% -info riu.id fvs_temp]

/* open the 2 files
&setvar in_file [open fvs_temp stand_openstat -read]
&setvar out_file [open %file% fvs_openstat -write]

/* read the first record and write the .slf file
&setvar rec [unquote [read %in_file% readstat]]
&do &until %readstat% = 102 /* end-of-file
  &call make_slf
&end

&return
/************************************************************
&routine do_fvs_files

/* write the .fvs files
/* for each unique riu.id
clearselect
&do riu_num &list [listunique %info_tree% -info riu.id]
   clearselect %info_tree% info
   /* reselect only the live/dead trees
   reselect %info_tree% info riu.id = %riu_num% and plt.data.ty.cd = %health%
   /*  open an fvs file for writing
   &setvar fvs_file [open %health_status%%riu_num%.fvs openstat -write]
   &do num = 1 &to [extract 1 [show select %info_tree% info]] &by 1
      clearselect %info_tree% info
      reselect %info_tree% info riu.id = %riu_num% and plt.data.ty.cd = %health%
      /* write to the file
      &call make_fvs
   &end
   &sv close [close -all]
&end

&return
/************************************************************
&routine clean_up

/* all the work is done, except...
/* close all open files
&setvar closeall [close -all]
&setvar del_temp [delete fvs_temp -file]
&setvar del_xx [delete xx*.* -file]
&watch &off
&ty
&ty
&ty Thanks for using Arc2fvs!
&ty
&ty                        -Luke
&ty
&ty
&return

/*----------------------------------------
&routine make_fvs

&sv stand_no [show select %info_tree% info %num% item riu.id]
&sv spt_no [show select %info_tree% info %num% item spt.no]
&sv tree_no [show select %info_tree% info %num% item tree.no]
&sv tree_cnt [show select %info_tree% info %num% item tree.cnt]
/* tree_status should be 1 if alive and 8 if dead
&sv tree_health [show select %info_tree% info %num% item plt.data.ty.cd]
&if %tree_health% = 1 &then
   &sv tree_stat = 1
&else
   &sv tree_stat = 8
&sv species [show select %info_tree% info %num% item spec.cd]
&sv tree_dbh [truncate [calc 10 * [show select %info_tree% info %num% item tree.dbh]]]
&sv tree_gro = [truncate [calc 10 * [show select %info_tree% info %num% item tree.gro.10.lng]]]
&sv tree_height = [show select %info_tree% info %num% item tree.smp.hgt]
&sv blc_height = [show select %info_tree% info %num% item tree.blc.hgt]
&sv tree_crn_pc = 0
&if %tree_height% ne 0 and %blc_height% ne 0 &then
   &sv tree_crn_pc = [calc [calc [calc %tree_height% - %blc_height%] / %tree_height%] * 100]
&else
   &sv tree_crn_pc = 0
&sv tree_crn = 0
&if %tree_crn_pc% gt 0 and %tree_crn_pc% le 10 &then
   &sv tree_crn = 1
&else &if %tree_crn_pc% gt 10 and %tree_crn_pc% le 20 &then
   &sv tree_crn = 2
&else &if %tree_crn_pc% gt 20 and %tree_crn_pc% le 30 &then
   &sv tree_crn = 3
&else &if %tree_crn_pc% gt 30 and %tree_crn_pc% le 40 &then
   &sv tree_crn = 4
&else &if %tree_crn_pc% gt 40 and %tree_crn_pc% le 50 &then
   &sv tree_crn = 5
&else &if %tree_crn_pc% gt 50 and %tree_crn_pc% le 60 &then
   &sv tree_crn = 6
&else &if %tree_crn_pc% gt 60 and %tree_crn_pc% le 70 &then
   &sv tree_crn = 7
&else &if %tree_crn_pc% gt 70 and %tree_crn_pc% le 80 &then
   &sv tree_crn = 8
&else &if %tree_crn_pc% gt 80 and %tree_crn_pc% le 100 &then
   &sv tree_crn = 9
&sv tree_kill = 0
&sv write1 [write %fvs_file% [quote [unquote [format '%1,-4%%2,-3%%3,-6%%4,-1%%5,3%%6,-4%%7,-3%%8,-3%' ~
%spt_no% %tree_no% %tree_cnt% %tree_stat% %species% %tree_dbh% %tree_gro% %tree_height%] ~
[format '%1,-2%%2,-3%%3,-2%%4,-2%%5,-2%%6,-2%%7,-2%%8,-2%' %tree_kill% 0 %tree_crn% 0 0 0 0 0]]]]

&return
/*----------------------------------------
&routine make_slf
clearselect
/*reselect %cover% poly area gt 0
/*reselect %cover% poly riu.id = [calc [extract 1 %rec%]]
reselect %info_main% info riu.id = [calc [extract 1 %rec%]]
&setvar stand_no [show select %info_main% info 1 item riu.id]
&setvar samp_yr [show select %info_main% info 1 item fiu.smp.yr]
&setvar latitude 47
&setvar longitude 122
&setvar location 609
&setvar habitat @
&setvar age [show select %info_main% info 1 item fiu.pri.age]
&setvar origin = [date -year] - %age%
&setvar aspect [show select %info_main% info 1 item fiu.aspect.deg]
&setvar slope [show select %info_main% info 1 item fiu.slope.pc]
&setvar elev [show select %info_main% info 1 item fiu.elev] / 100
reselect %info_expan% info riu.id = [calc [extract 1 %rec%]]
/* select Trees and Fixed Rad plot if doing Live Plots or Both
&if %health_status% = 'both' or %health_status% = 'alive' &then
&do
   reselect %info_expan% info plt.data.ty.cd = 1 and plt.type.cd = 1
   &setvar fixed_expan [show select %info_expan% info 1 item plt.expan.fac]
&end
&else
   &setvar fixed_expan = 0
clearselect %info_expan% info
reselect %info_expan% info riu.id = [calc [extract 1 %rec%]]
/* select Live Trees and Var Rad plot
reselect %info_expan% info plt.data.ty.cd = 1 and plt.type.cd = 2
&setvar var_expan [show select %info_expan% info 1 item plt.expan.fac]
/* set break point very low if dead plot
&if %health% = 1 or %health% = 3 &then
   &setvar break_pt_dbh = 5.5
&else
   &setvar break_pt_dbh = 0
&setvar si_index_species [show select %info_main% info 1 item fiu.si.spec.cd]
&setvar site_index [show select %info_main% info 1 item fiu.avg.si]

/* write to the file now
&if %health_status% = 'both' &then
   &sv fvs_files = alive%stand_no%.fvs+dead%stand_no%.fvs
&else
   &sv fvs_files = %health_status%%stand_no%.fvs

&sv writea [write %out_file% [quote A %stand_no% %fvs_files% NoPointData pn @]]
&sv writeb [write %out_file% [quote B %stand_no% %samp_yr% %latitude% %longitude% %location% %habitat% ~
   %origin% %aspect% %slope% %elev% %var_expan% %fixed_expan% %break_pt_dbh% @ @ @ @ @ @ @ @ @ @ @ ~
   %si_index_species% %site_index% @ @ @]]
&sv writec [write %out_file% [quote C %stand_no% %health_status%]]

/* read the next record
&setvar rec [unquote [read %in_file% readstat]]

&return
/*----------------------------------------
&routine cleanup
&setvar closeall [close -all]
&return