+// write out the area of data covered by the specified bucket. Data
+// is written out column by column starting at the lower left hand
+// corner.
+int
+FGDem::write_area( const string& root, FGBucket& b, bool compress ) {
+ char tile_name[256];
+
+ // calculate some boundaries
+ double min_x = ( b.get_center_lon() - 0.5 * b.get_width() ) * 3600.0;
+ double max_x = ( b.get_center_lon() + 0.5 * b.get_width() ) * 3600.0;
+
+ double min_y = ( b.get_center_lat() - 0.5 * b.get_height() ) * 3600.0;
+ double max_y = ( b.get_center_lat() + 0.5 * b.get_height() ) * 3600.0;
+
+ cout << b << endl;
+ cout << "width = " << b.get_width() << " height = " << b.get_height()
+ << endl;
+
+ int start_x = (int)((min_x - originx) / col_step);
+ int span_x = (int)(b.get_width() * 3600.0 / col_step);
+
+ int start_y = (int)((min_y - originy) / row_step);
+ int span_y = (int)(b.get_height() * 3600.0 / row_step);
+
+ cout << "start_x = " << start_x << " span_x = " << span_x << endl;
+ cout << "start_y = " << start_y << " span_y = " << span_y << endl;
+
+ // Do a simple sanity checking. But, please, please be nice to
+ // this write_area() routine and feed it buckets that coincide
+ // well with the underlying grid structure and spacing.
+
+ if ( ( min_x < originx )
+ || ( max_x > originx + cols * col_step )
+ || ( min_y < originy )
+ || ( max_y > originy + rows * row_step ) ) {
+ cout << " ERROR: bucket at least partially outside DEM data range!" <<
+ endl;
+ return -1;
+ }
+
+ // generate output file name
+ string base = b.gen_base_path();
+ string path = root + "/Scenery/" + base;
+ string command = "mkdir -p " + path;
+ system( command.c_str() );
+
+ long int b_index = b.gen_index();
+ sprintf(tile_name, "%ld", b_index);
+ string demfile = path + "/" + tile_name + ".dem";
+ cout << "demfile = " << demfile << endl;
+
+ // write the file
+ FILE *fp;
+ if ( (fp = fopen(demfile.c_str(), "w")) == NULL ) {
+ cout << "cannot open " << demfile << " for writing!" << endl;
+ exit(-1);
+ }
+
+ fprintf( fp, "%d %d\n", (int)min_x, (int)min_y );
+ fprintf( fp, "%d %d %d %d\n", span_x + 1, (int)col_step,
+ span_y + 1, (int)row_step );
+ for ( int i = start_x; i <= start_x + span_x; ++i ) {
+ for ( int j = start_y; j <= start_y + span_y; ++j ) {
+ fprintf( fp, "%d ", (int)dem_data[i][j] );
+ }
+ fprintf( fp, "\n" );
+ }
+ fclose(fp);
+
+ if ( compress ) {
+ string command = "gzip --best -f " + demfile;
+ system( command.c_str() );
+ }
+}
+
+
+#if 0
+
+// return the current altitude based on grid data. We should rewrite