/*****************************************************************************\ This module is public domain software that was developed by the U.S. Naval Oceanographic Office. This software 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. \*****************************************************************************/ #include "read_srtm_topo.h" static NV_INT32 three_open = 0; static NV_INT32 thirty_open = 0; /***************************************************************************\ * * * Module Name: read_srtm_topo_one_degree * * * * Programmer(s): Jan C. Depner * * * * Date Written: October 2006 * * * * Purpose: Reads the 1, 3, and/or 30 second SRTM compressed * * topographic elevation files (*.cte) and returns a * * one-degree single dimensioned array containing the * * elevations in the same format as the srtm3 data * * files (see Caveats below). The width/height of the * * array is returned, that is, 3600 for 1 second data, * * 1200 for 3 second data, or 120 for 30 second data. * * * * Arguments: lat - degree of latitude, S negative * * lon - degree of longitude, W negative * * array - topo array * * * * Returns: 0 for all water cell, 2 for undefined cell, or * * 120/1200/3600 (width/height of array, it's square). * * * * Caveats: The array is one dimensional so the user/caller * * must index into the array accordingly. The data is * * stored in order from the northwest corner of the * * cell, west to east, then north to south so the last * * point in the returned array is the southeast * * corner. See pointlat and pointlon in the following * * example code: * * * * * * #include "read_srtm_topo.h" * * * * NV_INT16 *array; * * NV_INT32 size; * * NV_FLOAT64 inc, pointlat, pointlon; * * * * size = read_srtm_topo_one_degree (lat, lon, &array);* * if (size > 2) * * { * * inc = 1.0L / size; * * for (i = 0 ; i < size ; i++) * * { * * pointlat = (lat + 1.0L) - i * inc; * * for (j = 0 ; j < size ; j++) * * { * * pointlon = lon + j * inc; * * //DO SOMETHING WITH array[i * size + j] * * } * * } * * } * * * * * * You should also call cleanup_srtm_topo after you * * are finished using the database in order to close * * the open files and free the associated memory. * * * * * \***************************************************************************/ NV_INT32 read_srtm_topo_one_degree (NV_INT32 lat, NV_INT32 lon, NV_INT16 **array) { NV_INT32 size; size = read_srtm1_topo_one_degree (lat, lon, array); if (size == 2) { size = read_srtm3_topo_one_degree (lat, lon, array); if (size == 2) { size = read_srtm30_topo_one_degree (lat, lon, array); thirty_open = 1; } three_open = 1; } return (size); } /***************************************************************************\ * * * Module Name: read_srtm_topo * * * * Programmer(s): Jan C. Depner * * * * Date Written: October 2006 * * * * Purpose: Reads the 1, 3, and/or 30 second SRTM compressed * * topographic elevation files (*.cte) and returns the * * elevation value. If the value is undefined at that * * point it will return -32768. For water it will * * return 0. * * * * Arguments: lat - latitude degrees, S negative * * lon - longitude degrees, W negative * * * * Returns: 0 = water, -32768 undefined, or elevation * * * \***************************************************************************/ NV_INT16 read_srtm_topo (NV_FLOAT64 lat, NV_FLOAT64 lon) { static NV_INT16 *array; static NV_INT32 prev_ilat = -1, prev_ilon = -1, size = 0; static NV_FLOAT64 inc = 0.0; NV_INT32 ilat, ilon, lat_index, lon_index; ilat = (NV_INT32) lat; ilon = (NV_INT32) lon; /* No point in calling the function if we didn't change cells. */ if (ilat != prev_ilat || ilon != prev_ilon) { size = read_srtm1_topo_one_degree (ilat, ilon, &array); if (size == 0) return (0); if (size == 2) { size = read_srtm3_topo_one_degree (ilat, ilon, &array); if (size == 0) return (0); if (size == 2) { size = read_srtm30_topo_one_degree (ilat, ilon, &array); thirty_open = 1; } three_open = 1; } inc = 1.0L / (NV_FLOAT64) size; } if (size == 0) return (0); if (size == 2) return (-32768); /* Get the cell index. */ lat_index = size - (NV_INT32) ((lat - (NV_FLOAT64) ilat) / inc); lon_index = (NV_INT32) ((lon - (NV_FLOAT64) ilon) / inc); return (array[lat_index * size + lon_index]); } /***************************************************************************\ * * * Module Name: cleanup_srtm_topo * * * * Programmer(s): Jan C. Depner * * * * Date Written: October 2006 * * * * Purpose: Closes the open srtm3/30 topo files and frees * * memory. * * * * Arguments: None * * * * Returns: Nada * * * \***************************************************************************/ void cleanup_srtm_topo () { cleanup_srtm1_topo (); if (three_open) cleanup_srtm3_topo (); if (thirty_open) cleanup_srtm30_topo (); three_open = 0; thirty_open = 0; }