#!/usr/bin/python #ddfex1.py - DDF Example#1 1.0 - 3 Feb 01 #Basic cross-platform script to view a DDF file's DDR/DR leaders. #See http://www.3dartist.com/WP/sdts/sdtsnotes.htm#pleng # for an explanation. #Update at http://www.3dartist.com/WP/python/pycode.htm#ddfex #Created by Bill Allen #HISTORY: #2001-02-03: First posted. #PURPOSE: #This script is an example to show only how to skim through # an SDTS DDF file's DDR and DR records. #PLATFORM: This code should run on any machine that has Python # 2.0 installed, but has been tested only under Windows ME. # Visit http://www.python.org to learn more about Python, a # cross-platform freeware language. #LICENSE: This code is free software provided as is and without # warranty for fitness for any purpose. Use it at your own risk. # This code and derivations may be used anywhere, including for # commercial application. If used for public purposes, an # acknowledgment and a link back would be appreciated. #NOTE: A print statement means to print to the screen ("standard output"). ### BEGIN PROGRAM ### fn = 'c:/python/demtest/1168CEL0.DDF' #put your DDF file's name here print '\nScanning',fn rc = 0 #record counter f = open(fn,'rb') #may contain binary data, so read binary r = f.read(5) #read 1st 5 bytes with record length rid = f.read(2) #read 2 more bytes if rid[1] != 'L': #if this isn't "L", DDF isn't usable print '\tNot a DDF file! ID = \\',ord(rid[1]),'\n' else: #is ordinal so can read non-alphanumeric while 1: #loop forever, exit with break rlen = int(r) #convert rec length to a number r = r + rid + f.read(rlen-7) #get & append rest of the record rc = rc + 1 if ord(r[-1]) != 30: print '\tError: End of rec#',rc,'@',rlen,'=',ord(r[-1]),'\n' break ### depart from the loop here to do something with the record if rc < 4 or rc > 462: # * uncomment only one of these 3 * # if rc < 4 or rc > 1390: #use on a 10m DEM CEL0.DDF # if rc < 21: #use on other DDFs print '%4d'%rc,':',r[:24] #print record leader, not too many ### resume loop r = f.read(5) #start another record if not r: print '\tEnd of file\n' break rid = f.read(2) if rid[1] not in ['D','L']: #has to be a D or L print '\tError: Bad rec#',rc+1,'ID = \\',ord(rid[1]),'\n' break f.close()