Source code structure

As stated before this is my first python program so please bear with me :-).

The USR4 file format is a binary file format. The length of each field is given.

Main structure
The main structure of a USR4 file is
  1. Header with information on version, creation date etc. 
  2. Number of waypoints
  3. Waypoint structures
  4. Number of routes
  5. Route structures (mainly reference to the already read waypoints)
  6. Number of tracks
  7. Track structures
Waypoint, route and track structures

The main structure used is the OrderedDict which is used to map the Waypoint, Route and Track structures from the file to data structures in python.
As a first step I created the OrderedDict for waypoints, routes and tracks:. Each item in the OrderedDict consists of:
  • The first field in each of the entries you will find the field name, e.g. UID_unit_number 
  • The list afterwards comprises of two elements: 
    • the lenght/type of the field and the
    • data of the field itself
An excerpt is shown here:

 wpt = OrderedDict([  
     ('UID_unit_number'                , ['uint32', 0]),   
     ('UID_sequence_number'            , ['int64', 0]),   
     ('Waypoint_stream_version_number' , ['uint16', 0]), .........


After reading for example the waypoint count the program initialises a list of waypoints with exactly the structure of the respective OrderedDict using deepcopy (I assume there are more elegant ways to do this?)
Using the for x in y: the program iterates through the waypoints, routes and tracks
 wpts = []  
   for i in range(waypointcount):  
     wpts.append(deepcopy(wpt))  
   for w in wpts:  
     for k, v in w.items():     
       """ binary read each field and distinguish e.g. for a descrition   
       if k == 'Description':  
         if w['Description_length_in_bytes'][1] > 0:       
           w['Description'][1] = binaryReader.readstring.....  
          more elif...statements....  
       else:    
         v[1] = binaryReader.read(v[0])  

Thus all data is filled into the data structures at the right places and fields like descriptions with variable length are covered through the if statements (a case structure would be more readable here)

Writing the output file
After reading in the USR4 file the GPX output file is written based on the structures created.
The GPX output file has the main structure
  1. Header
  2. Waypoints
  3. Routes
  4. Tracks

No comments:

Post a Comment