ESRI gridfile creation from OMG mapsheets


The ESRI grid file format consists of a small (6 line) header, followed by the depths (without positioning):

ncols 3200
nrows 2900
xllcenter 416000.0
yllcenter 5453000.0
cellsize 10.0
nodata_value -99999.99

This is followed by the z-data values, separated by spaces.
The xllcenter and yllcenter are the coordinates of the lower left pixel in the mapsheet.
The nodata_value is used to tell the importing software that pixels with this value have no data.
Some software packages prefer that each row of ascii data is separated by a carriage return.


You can run the OMG program r4toESRI to convert OMG mapsheets to ESRI ascii format.  Note that the format does NOT support
rotated mapsheets.  Syntax is:

	r4toESRI -in input.r4 -out output.asc

If this pukes for some reason, you can always build your own ESRI ascii file following the set of commands below.


# Dump the esri grid header (in this case, generates the file map125_3m.esri_grid)
# Note that you can use any of the mapsheets (.r4, .sun_315, .8bit, .mos, etc) to
# generate the header but note that the nodata value will depend on the mapsheet
# data type (floating point => -99999.99, 8-bit => 255)
edhead -dump_esri_grid map125_3m.r4

# this will be useful below
set num_cols = `fgrep 'ncols' map125_3m.esri_grid | awk '{print $2}' `

# Dump the r4 data into an ascii file
r4toASCII -withzeros map125_3m.r4 temp.ascii

# r4toASCII spits out lat long depth, we only want depth...
# use awk to filter out the first two columns 
# two possible sequences depending on the data type (float or 8-bit)
# make sure you comment out the appropriate one

# FLOAT FLOAT FLOAT
# FLOAT FLOAT FLOAT
# FLOAT FLOAT FLOAT
# FLOAT FLOAT FLOAT
# note that we multiply the third column by -1 to make depths
# negative
# also, we must replace zeros in the DTM with -99999.99 to flag them as
# pixels without data
# note that we multiply the third column by -1 to make depths
# We also feed a carriage return after each row of data.  Have been informed by
# Mike Brissette that imporating these files into ArcTools goes a LOT quicker if
# the rows are separated by carriage returns

awk '{if ($3 != 0.0) {printf("%.2f",-$3);} else {printf("-99999.99");}; if (NR%'$num_cols' == 0) {printf("\n"); } else {printf(" "); }  }' temp.ascii > temp2.ascii

# FLOAT FLOAT FLOAT
# FLOAT FLOAT FLOAT
# FLOAT FLOAT FLOAT
# FLOAT FLOAT FLOAT






# 8-BIT 8-BIT 8-BIT
# 8-BIT 8-BIT 8-BIT
# 8-BIT 8-BIT 8-BIT
# 8-BIT 8-BIT 8-BIT

awk '{printf("%.2f",$3); if (NR%'$num_cols' == 0) {printf("\n"); } else {printf(" "); }  }' temp.ascii > temp2.ascii

# 8-BIT 8-BIT 8-BIT
# 8-BIT 8-BIT 8-BIT
# 8-BIT 8-BIT 8-BIT
# 8-BIT 8-BIT 8-BIT



# Join the header file and depth file into an esri grid file
# I've seen these with the extension .dem, but I'm not sure if
# that's the standard naming convention.
# Arcview seems to enjoy .asc extensions...
cat map125_3m.esri_grid temp2.ascii > map125_3m.asc

# I've had Arcview whine at me if there wasn't a carriage return at the end of the file
echo '\n' >> map125_3m.asc

# gzip the bugger since they take up a bit of space...
gzip map125_3m.asc

# Clean up leftover files
rm map125_3m.esri_grid
rm temp.ascii
rm temp2.ascii

Last modified by J. Beaudoin, March 21st, 2005