Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting a matrix from scientific to floating point

Status
Not open for further replies.

chep80

Technical User
Jul 22, 2005
10
US
I have the following script where I am trying to manipulate portions of the headers in the beginning and then I want to convert the matrix from scientific to floating point:

#!/usr/bin/perl
use strict;

############################################################
#Insert name and location of file from STK after "my $input"
############################################################

my $input = 'C:\Documents and Settings\My Documents\Stk 6.0\STK scripts\OCO_acs.e';
my @output;
open (FILE, "$input");
my @array = <FILE>;
for (@array) {
my ($col1, $col2, $col3, $col4, $col5, $col6, $col7, $col8) = split(/\s+/, $_);
my $line = "$col1 $col2 $col3 $col4 $col5 $col6 $col7 $col8";
if ($line=~m/^\d/) {
push (@output, $line);
}
}

my $NoAtP=@output;

#######################################################
#Insert desired name of the STK .a output file after >
#Change ScenarioEpoch as necessary
#######################################################

open (OUTFILE, ">28day_ephemeris_stk3p0_format.e");
print OUTFILE "stk.v.3.0\n";
print OUTFILE "\n";
print OUTFILE "BEGIN Ephemeris\n";
print OUTFILE "\n";
print OUTFILE " NumberofAttitudePoints $NoAtP\n";
print OUTFILE " 15 Oct 2008 12:00:00.00\n";
print OUTFILE " EphemerisEciTimePosVel\n";

foreach my $entry(@output) {
print OUTFILE "$entry\n";
}

Here is an example of what the file looks like before modifying it:

stk.v.3.0

BEGIN Ephemeris

NumberOfEphemerisPoints 40321
ScenarioEpoch 15 Oct 2008 12:00:00.000
EphemerisEciTimePosVel
0.00000000000000e+000 -5.10704934943446e+006 -4.30468742388535e+006 2.34134338660962e+006 1.14622110187997e+003 2.44791644659563e+003 7.00081996659693e+003
6.00000000000000e+001 -5.02804956947231e+006 -4.14946215282220e+006 2.75587684053564e+006 1.48771794293052e+003 2.73281630226811e+003 6.82905617631048e+003


But I want it to look like this:

stk.v.3.0

BEGIN Ephemeris

NumberOfEphemerisPoints 40321
ScenarioEpoch 15 Oct 2008 12:00:00.000
EphemerisEciTimePosVel
0 -5107049.34943446 -4304687.42388535 2341343.38660962 1146.22110187997 2447.91644659563 2447.91644659563
 
What do you think of this?
Code:
use strict;
use Math::BigFloat;
my $convert;
while (<DATA>) {
    if ($convert) {
        chomp;
        my @vals = split(/ /);
        foreach my $num (@vals) {
            my $x = Math::BigFloat->new($num);
            print $x->bstr() . " ";
        }
        print "\n";
    }
    if (/ephemerisecitimeposvel/i) {
        $convert = 1;
    }
    print $_;
}
    
__DATA__
stk.v.3.0

BEGIN Ephemeris

    NumberOfEphemerisPoints 40321
    ScenarioEpoch 15 Oct 2008 12:00:00.000
    EphemerisEciTimePosVel
0.00000000000000e+000 -5.10704934943446e+006 -4.30468742388535e+006 2.34134338660962e+006 1.14622110187997e+003 2.44791644659563e+003 7.00081996659693e+003
6.00000000000000e+001 -5.02804956947231e+006 -4.14946215282220e+006 2.75587684053564e+006 1.48771794293052e+003 2.73281630226811e+003 6.82905617631048e+003
 
Oops, small bug in the code.

Code:
use strict;
use Math::BigFloat;
my $convert;
while (<DATA>) {
    if ($convert) {
        my @vals = split(/ /);
        foreach my $num (@vals) {
            my $x = Math::BigFloat->new($num);
            print $x->bstr() . " ";
        }
        print "\n";
    }
    if (/ephemerisecitimeposvel/i) {
        $convert = 1;
    }
    print $_ unless $convert;
}
    
__DATA__
stk.v.3.0

BEGIN Ephemeris

    NumberOfEphemerisPoints 40321
    ScenarioEpoch 15 Oct 2008 12:00:00.000
    EphemerisEciTimePosVel
0.00000000000000e+000 -5.10704934943446e+006 -4.30468742388535e+006 2.34134338660962e+006 1.14622110187997e+003 2.44791644659563e+003 7.00081996659693e+003
6.00000000000000e+001 -5.02804956947231e+006 -4.14946215282220e+006 2.75587684053564e+006 1.48771794293052e+003 2.73281630226811e+003 6.82905617631048e+003
 
Thanks, but I dont really want to have to add the DATA into the code since its actually 40,321 lines. Thats why the original code reads in the text file and the main task I'd like it to do is convert all those numbers to floating point and then create a new file.
 
No problem. The __DATA__ is there just for testing and demonstration purposes. I don't have the file, so I can't code it exactly. Here is an approximation:

Code:
use strict;
use Math::BigFloat;
open(IN, "c:/temp/data.txt");
my $convert;
while (<IN>) {
    if ($convert) {
        my @vals = split(/ /);
        foreach my $num (@vals) {
            my $x = Math::BigFloat->new($num);
            print $x->bstr() . " ";
        }
        print "\n";
    }
    if (/ephemerisecitimeposvel/i) {
        $convert = 1;
    }
    print $_ unless $convert;
}

What comes after this part of the file:
stk.v.3.0

BEGIN Ephemeris

NumberOfEphemerisPoints 40321
ScenarioEpoch 15 Oct 2008 12:00:00.000
EphemerisEciTimePosVel

Is it all numbers from that point on? Or are there more descriptive elements with dates, times, etc? If there are only numbers, then the code should work as is. If not, then more modifications would have to be made to it. I would need you to post a larger sampling of your file that is representative of how the data is broken up.

 
To print the file back out, use this code (other minor code corrections also included):

Code:
use strict;
use Math::BigFloat;
open(IN, "c:/temp/data.txt");
open(OUT, ">c:/temp/newdata.txt");
my $convert;
while (<IN>) {
    if ($convert) {
        my @vals = split(/ /);
        foreach my $num (@vals) {
            my $x = Math::BigFloat->new($num);
            print OUT $x->bstr() . " ";
        }
        print OUT "\n";
    }
    if (/ephemerisecitimeposvel/i) {
        $convert = 1;
        print OUT $_;
    }
    print OUT $_ unless $convert;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top