#!/bin/sh
# the next line restarts using tclsh \
exec tclsh "$0" "$@"

# convert.tcl --
#
#	This script converts data from the National Weather Service AWIPS web
#	site from Arcinfo to xdr format.

# It uses the shapelib library and shptobin application to read ESRI shapefiles.
# See http://www.tkgeomap.org/tkgm/shape.html
#
# Copyright (c) 2002 Gordon D. Carrie
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# Please submit bug reports and suggestions to:
# tkgeomap@users.sourceforge.net

package require geolinearray 2

# Location of the files downloaded from the AWIPS web site.  Adjust this
# for your file system.

set DataDir /home/data/geography/data/AWIPS/state_prov

# Convert U.S. states.

set US $DataDir/US/s_24ja01
set dbf [open "|dbfdump $US"]
gets $dbf
while {[gets $dbf line] >= 0} {
    set state [string trim [string range $line 14 38]]
    lappend States $state
}
close $dbf

set i 0
foreach state $States {
    set xdr_file ${state}.xdr
    if ![file exists $xdr_file] {
	puts "Reading $state"
	geolinearray fmbin $state "|shptobin -s -i $i $US"
	$state descr $state
	puts "Writing $state"
	$state toxdr $xdr_file
    }
    incr i
}

# Convert Mexican states.

set xdr_file Mexico.xdr
if ![file exists $xdr_file] {
    set Mexico $DataDir/Mexico/MX_STATE
    puts "Reading Mexico"
    geolinearray fmbin Mexico "|shptobin -s $Mexico"
    Mexico descr Mexico
    puts "Writing Mexico"
    Mexico toxdr $xdr_file
}

# Convert Canadian provinces.

set xdr_file Canada.xdr
if ![file exists $xdr_file] {
    set Canada $DataDir/Canada/PROVINCE
    puts "Reading Canada"
    geolinearray fmbin Canada "|shptobin -s $Canada"
    Canada descr Canada
    puts "Writing Canada"
    Canada toxdr $xdr_file
}
