Google Answers Logo
View Question
 
Q: Simple program to add a value to a elevation set ( No Answer,   3 Comments )
Question  
Subject: Simple program to add a value to a elevation set
Category: Computers > Programming
Asked by: alakon2-ga
List Price: $2.00
Posted: 27 Aug 2003 08:50 PDT
Expires: 10 Sep 2003 07:01 PDT
Question ID: 249242
I have a file of 35mb in the following format:

(Longitude), (latitude), [elevation * (-1)]

In other words, the following:

-74.4594595	40.0472973	43.03
Reveals a point of 40 degrees N and 74 degrees W with a depth below
the sea of 43 meters

-74.4594595	40.0472973	-5
Reveals a point of 40 degrees N and 74 degrees W with a height above
the sea of 5
meters

I would like a program to be created that ignores the latitude and
longitude, but will add X meters (please make this code commented so I
can change it when necessary) to the elevation. So, continuing with
the two examples above, they would be transformed to:

-74.4594595	40.0472973	43.03
Reveals a point of 40 degrees N and 74 degrees W with a depth below
the sea of 44 meters

-74.4594595	40.0472973	-5
Reveals a point of 40 degrees N and 74 degrees W with a height above
the sea of 6
meters

Another point – make the program so that I can change the number
easily, and I have the choice of addition or subtraction.

You may create the program so that it can be recompiled (if necessary
for type of program) and ran on a Linux platform with standard Red Hat
8.0 packages, a Windows 2000 machine, or a DEC Alpha (in order of
preference). It can also be created with MatLab or Fortran 90, but I
would prefer that it can be run with freely available software that I
may already have.

Clarification of Question by alakon2-ga on 28 Aug 2003 14:09 PDT
Thank you for your help Eadfrith!

A sample line of before and after:

Before:
      -74.08600      40.10001     0.80

After:
-74.085999 40.100010 1.680000

So now the question becomes: how can we modify this code so the
columns are separated by a tab rather than a space, and more
importantly, limit the significant figures of the output to that of
the input?

Clarification of Question by alakon2-ga on 28 Aug 2003 15:12 PDT
Eadfrith,

Thank you again for the insight into C!

The program seems to take it without complaining, so no complicated
variable precision is needed.

You really seem to be intent on putting Google out of business! I want
to change my "question" to the paid researcher: how can I optimize
this code? It will be eventually used as part of a series of scripts
and programs to do weather forecasting in real time, and this might be
a little cog in that wheel.
Answer  
There is no answer at this time.

Comments  
Subject: Re: Simple program to add a value to a elevation set
From: eadfrith-ga on 28 Aug 2003 11:11 PDT
 
I think the best option given your need for portability is to stick to
standard C. I'm very rusty at C, but I think the following code should
do what you want.

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
  FILE *fpin, *fpout;
  float diff;

  if(argc != 4)
  {
    printf("Usage: elev_change <input_file> <output_file> <elev_diff>
\n");
    return 1;
  }

  if((fpin = fopen(argv[1], "r")) == NULL)
  {
    printf("Cannot open input file: %s \n", argv[1]);
    return 1;
  }

  if((fpout = fopen(argv[2], "w")) == NULL)
  {
    printf("Cannot create output file: %s \n", argv[2]);
    return 1;
  }

  diff = atof(argv[3]);

  while(1)
  {
    float lat, lon, elev;
    if(fscanf(fpin, "%f %f %f", &lat, &lon, &elev) < 3)
    {
      break;
    }
    fprintf(fpout, "%f %f %f\n", lat, lon, (elev+diff));
  }
  fclose(fpin);
  fclose(fpout);
  return 0;
}

You'll have to compile it yourself on each intended platform. Usage is
then:

elev_change <input_file> <output_file> <elev_diff>

where:

elev_change   - the name you give to the program
<input_file>  - the file containing the data
<output_file> - the name of the file to hold the modified data
<elev_diff>   - the value by which to change elevations (eg. 23.2,
-34.77 etc).

Cheers,

Eadfrith
Subject: Re: Simple program to add a value to a elevation set
From: eadfrith-ga on 28 Aug 2003 14:35 PDT
 
Alkalon2,

The tabs are easy. Setting the significant figures is easy if it's the
same for all elevations. For example, to get tabbed output with 2
significant figures replace the line:

fprintf(fpout, "%f %f %f\n", lat, lon, (elev+diff)); 

with this:

fprintf(fpout, "\t%f \t%f \t%.2f\n", lat, lon, (elev+diff));

This will give you 2 places after the decimal point. Change the ".2"
to
whatever format you wish. Eg. to get 4 places you'd used ".4"

Now, if the input has variable precision, eg. some elevations are to 2
places while others are to 5, and you wish to match this line by line,
then it's a little more tricky. If this is the case then let me know
and I'll have a go.

Cheers,

Eadfrith
Subject: Re: Simple program to add a value to a elevation set
From: yosarian-ga on 07 Sep 2003 09:57 PDT
 
At the risk of opening a language war, I suggest the following:
we have here a program which needs to be very portable,
which does a lot of file I/O and a little arithmetic. 
In my book, this is a reasonable candidate for a PERL script.

Here's a reference saying it may be fast enough:
http://www.developer.com/lang/perl/article.php/1554501

Here is my version:

#!/usr/local/bin/perl -w

if($#ARGV != 2){ 
      die("Usage: elev_change <input_file> <output_file> <elev_diff>\n");
  } 

$infile = shift(@ARGV);
open(INFILE,"$infile") || die "Cannot open $infile";

$outfile = shift(@ARGV);
open(OUTFILE,">$outfile") || die "Cannot open $outfile";

$diff = shift(@ARGV);

while(<INFILE>){
    ($lat,$lon,$elev) = ($_ =~ /^(-?\d+.\d*)\s+(-?\d+.\d*)\s+(-?\d+.\d*)\s*$/);
    printf OUTFILE "\t %f \t%f \t%.2f\n",$lat,$lon,$elev+$diff;
}

close(INFILE);
close(OUTFILE);

In UNIX, write this into a text file named
elev_change and use eadfrith-ga's command line
(You may have to dig a little to find the place
of the perl interpreter defined in the first line 
of the script.
I also stole eadfrith's output formatting.
The strange line in the middle is a regular expression
to identify three floats - I guess this could be more readable).

This will run anywhere with a PERL interpreter - 
I believe all the platforms you have mentioned 
and many more.

Cheers,
yosarian-ga

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy