source: inundation/pyvolution/ermapper_grids.py @ 1794

Last change on this file since 1794 was 1794, checked in by tdhu, 19 years ago

Initial version of functions to read and write ERMapper grids. Includes the ability to write data and headers and to read ers data (but not yet headers).

File size: 3.7 KB
Line 
1#!/usr/bin/env python
2
3# from os import open, write, read
4import Numeric
5
6
7def write_ermapper_header(ofile, datum = 'AGD66', projection = 'GEOGRAPHIC',
8                          coordinate_type = 'LL', rotation = '0:0:0.0',
9                          cell_type = 'IEEE4ByteReal', null_value = '-99999',
10                          x_dim = '100', x_ll = '0:0:0', y_dim = '100', y_ll = '0:0:0',
11                          num_lines = '3', num_cells = '4', num_bands = '1',
12                          band_id = 'band1'):
13
14    # Determine if the dataset is in lats/longs or eastings/northings and set header parameters
15    # accordingly
16    if coordinate_type is 'LL':
17        X_Class = 'Longitude'
18        Y_Class = 'Latitude'
19    elif coordinate_type is 'EN':
20        X_Class = 'Eastings'
21        Y_Class = 'Northings'
22
23    # open the header file for writing to
24    fid = open(ofile,'wt')
25
26    # Begin writing the header
27    fid.write('DatasetHeader Begin\n')
28    fid.write('\tVersion\t\t\t= "6.4"\n')
29    fid.write('\tDatasetType\t\t= ERStorage\n')
30    fid.write('\tDataType\t\t= Raster\n')
31    fid.write('\tByteOrder\t\t= LSBFirst\n')
32
33    # Write the coordinate space information
34    fid.write('\tCoordinateSpace Begin\n')
35    fid.write('\t\tDatum\t\t\t = "' + datum + '"\n')
36    fid.write('\t\tProjection\t\t = "' + projection + '"\n')
37    fid.write('\t\tCoordinateType\t = ' + coordinate_type + '\n')
38    fid.write('\t\tRotation\t\t = ' + rotation + '\n')
39    fid.write('\tCoordinateSpace End\n')
40
41    # Write the raster information
42    fid.write('\tRasterInfo Begin\n')
43    fid.write('\t\tCellType\t\t\t = ' + cell_type + '\n')
44    fid.write('\t\tNullCellValue\t\t = ' + null_value + '\n')
45    fid.write('\t\tRegistrationCellX\t\t = 0\n')
46    fid.write('\t\tRegistrationCellY\t\t = ' + str(int(num_lines) - 1) +'\n')
47    # Write the cellsize information
48    fid.write('\t\tCellInfo Begin\n')
49    fid.write('\t\t\tXDimension\t\t\t = ' + x_dim + '\n')
50    fid.write('\t\t\tYDimension\t\t\t = ' + y_dim + '\n')
51    fid.write('\t\tCellInfo End\n')
52    # Continue with wrting the raster information
53    fid.write('\t\tNrOfLines\t\t\t = ' + num_lines + '\n')
54    fid.write('\t\tNrOfCellsPerLine\t = ' + num_cells + '\n')
55    # Write the registration coordinate information
56    fid.write('\t\tRegistrationCoord Begin\n')
57    fid.write('\t\t\t' + X_Class + '\t\t\t = ' + x_ll + '\n')
58    fid.write('\t\t\t' + Y_Class + '\t\t\t = ' + y_ll + '\n')
59    fid.write('\t\tRegistrationCoord End\n')
60    # Continue with wrting the raster information
61    fid.write('\t\tNrOfBands\t\t\t = ' + num_bands + '\n')
62    fid.write('\t\tBandID Begin\n')
63    fid.write('\t\t\tValue\t\t\t\t = "' + band_id + '"\n')
64    fid.write('\t\tBandID End\n')
65    fid.write('\tRasterInfo End\n')
66    fid.write('DatasetHeader End\n')
67   
68    fid.close
69                         
70
71def write_ermapper_data(grid, ofile, data_format = Numeric.Float32):
72    # Convert the array to data_format (default format is Float32)
73    grid_as_float = grid.astype(data_format)
74
75    # Convert array to a string for writing to output file
76    output_string = grid_as_float.tostring()
77
78    # open output file in a binary format and write the output string
79    fid = open(ofile,'wb')
80    fid.write(output_string)
81    fid.close()
82
83
84def read_ermapper_data(ifile, data_format = Numeric.Float32):
85    # open input file in a binary format and read the input string
86    fid = open(ifile,'rb')
87    input_string = fid.read()
88    fid.close()
89
90    # convert input string to required format (Note default format is Numeric.Float32)
91    grid_as_float = Numeric.fromstring(input_string,data_format)
92    return grid_as_float
93
Note: See TracBrowser for help on using the repository browser.