## parseDEM.py -- A class that will read the header of a DEM and output it
##   to a text file.

##      Copyright (C) 2000  CWRES-IDS Group, Colorado State University

##      This program is free software; you can redistribute it and/or modify
##      it under the terms of the GNU General Public License as published by
##      the Free Software Foundation; either version 1, or (at your option)
##      any later version.

##      This program is distributed in the hope that it will be useful,
##      but WITHOUT ANY WARRANTY; without even the implied warranty of
##      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##      GNU General Public License for more details.

##      You should have received a copy of the GNU General Public License
##      along with this program; if not, write to the Free Software
##      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

# Format is (taken from DEMtools http://www.arq.com/~kasten/demtools/
##     filename[40]              0
##     freetext[40]              40
##     fill01[55]                80
##     process                   135
##     fill02                    136
##     sectind[3]                137
##     origin[4]                 140 
##     level[6]                  144
##     pattern[6]                150
##     refcode[6]                156
##     zone[6]                   162
##     mapprojparams[360]        168
##     groundunits[6]            528
##     elevationunits[6]         534
##     sides[6]                  540
##     coords[192]               546
##     elevs[48]                 738
##     angle[24]                 786
##     accuracycode[6]           810
##     spatialresx[12]           816
##     spatialresy[12]           828
##     spatialresz[12]           840
##     rows[6]                   852
##     cols[6]                   858
##     maxcontour[5]             864
##     maxcontourunits           869
##     mincontour[5]             870
##     mincontourunits           875
##     sourcedate[4]             876
##     inspectiondate[4]         880
##     revflag                   884
##     valflag                   885
##     suspectflag[2]            886
##     vertdatum[2]              888
##     horzdatum[2]              890
##     dataedition[4]            892
##     percentvoid[4]            896
##     fill03[124]               900

import string				# miscellaneous string functions

import sys, getopt
import os

class parseDEM:
    """Create a text file that can be read by something like ArcView that
    contains the dimensions and resolution of all the dem files in the
    argument list.  Note that this should be run in a UNIX command shell
    so that wildcards can be used (ie, 'python parseDEM.py *.dem').
    """
    def __init__(self, outfile, fileName):
        # Open file and read header data.
        fp = open(fileName, 'r')
        fline = fp.read(1000)

        filename = fline[0:40]
        spatialresx = float(fline[816:828])
        spatialresy = float(fline[828:840])
        spatialresz = float(fline[840:852])

##          print 'DEM ' + string.strip(filename) + ' has resolution ' + `(
##              spatialresx, spatialresy, spatialresz)`
##          print 'DEM ' + string.strip(filename) + ' has ground and elev units ' + `(fline[528:534], fline[534:540])`
##          print 'DEM ' + string.strip(filename) + ' has coords ' + fline[546:738]
        
        outfile.write(
            string.strip(filename) + '\n' + fileName + '\n' + `spatialresx`
            + ',' + ` spatialresy` + ',' + `spatialresz` + '\n')

        coords = string.split(fline[546:738])
        for c in range(len(coords)):
            outfile.write(`float(coords[c])` + " ")
        outfile.write("\n")
                       
        #outfile.write(string.replace(fline[546:738], "D", "e") + '\n')

        fp.close()

        return

if __name__ == '__main__':
    if len(sys.argv) == 1:
        print 'usage: ' + sys.argv[0] + ' <dem1> <dem2> ... <demN>'
    else:
        print 'Output will be in file "res.txt" in the current directory.'
    
        outfile = open("res.txt", 'w')
        for fname in sys.argv[1:]:
            parseDEM(outfile, fname)
        outfile.close()
