Reading the ascii file and filling the vectors

HI all,
I’m new in root and i need a help.

I have a problem with reading the data from ascii file and puting them into separate vectords. The thing is that i’m not sure, since all data are read out from ascii file, how to fill vectors just with values that i need.
My ascii file have description:


Header Record Format:

Variable Name Columns Description


Header Record Indicator 1- 1 # character

Station Number 2- 6 WMO station number

Year 7- 10

Month 11- 12

Day 13- 14

Observation Hour 15- 16 00-23 UTC

Release Time 17- 20 0000-2359 UTC, 9999 = missing

Number of levels 21- 24 number of subsequent data records


Data Record Format:

Variable Name Columns Description


Major Level Type 1- 1 1 = standard pressure level
2 = significant thermodynamic level
3 = additional wind level

Minor Level Type 2- 2 1 = surface, 2 = tropopause, 0 = other

Pressure 3- 8 units of Pa (mb * 100)

Pressure Flag 9- 9 A, B, or blank (see note 4 above)

Geopotential Height 10- 14 units of meters

Geopotential Height Flag 15- 15 A, B, or blank (see note 4 above)

Temperature 16- 20 units of degrees C * 10

Temperature Flag 21- 21 A, B, or blank (see note 4 above)

Dewpoint Depression 22- 26 units of degrees C * 10

Wind Direction 27- 31 units of degrees (0-360, inclusive)

Wind Speed 32- 36 units of (m/s)*10

I need just pressure, temperature and height (without flags) and this is (wrong) code that i wrote:

int main () {
// OPEN THE TEXT FILE
ifstream file_to_read(“udine.txt”);

// Maximum number of characters expected in a single line in the header
const int max_num_of_char_in_a_line = 24;
//Number of header files to skip
int num_of_header_lines = 1;

// SKIP ALL THE HEADER LINES

for(int i = 0; i < num_of_header_lines; ++i)
file_to_read.ignore(max_num_of_char_in_a_line, ‘\n’);

vector Pressure_array;
vector Temperature_array;
vector Altitude_array;

while(!file_to_read.eof()) {
if (!file_to_read.good()) break;

double Pressure;
double Altitude;
double Temperature;
//read the data
file_to_read.ignore(2);//dont read first 2 characters
file_to_read >> setw(5) >>Pressure ; //fill next 5 char
file_to_read.ignore(1);//dont read nex one char
file_to_read >> setw(5) >> Altitude;
file_to_read.ignore(1);
file_to_read >> setw(5) >> Temperature;
file_to_read.ignore(16);
//fill the data
Pressure_array.push_back(Pressure);
Altitude_array.push_back(Altitude);
Temperature_array.push_back(Temperature);
}

for(int i = 0; i < Pressure_array.size(); ++i){
cout << Pressure_array << ‘\n’;
double Pressure = Pressure_array.size[i];
}

for(int i = 0; i < Altitude_array.size(); ++i){
cout << “\naltitude are:\n” <<altitude_array << ‘\n’;
double Altitude = Altitude_array.size[i];
}

for(int i = 0; i < Temperature_array.size(); ++i){
cout << Temperature_array << ‘\n’;
double Temperature = Temperature_array.size[i];
}

return 0;

}

Could someone help me with this and tell me where i’m wrong?
Sorry for such a long question

Hi,

I don’t see any ROOT features in your code. Maybe would be better post in a c++
forum. But I think it is just matter of keep debbuging your code. Try print all the lines
you are reading… I’m pretty sure you are going to find out what is wrong. Anyway,
you can always use root files instead of ascii files because they offer many advantages for data analysis and storage of objects like graphs, histograms…

[quote=“one_f”]HI all,
I’m new in root and i need a help.

I have a problem with reading the data from ascii file and puting them into separate vectords. The thing is that i’m not sure, since all data are read out from ascii file, how to fill vectors just with values that i need.
My ascii file have description:


Header Record Format:

Variable Name Columns Description


Header Record Indicator 1- 1 # character

Station Number 2- 6 WMO station number

Year 7- 10

Month 11- 12

Day 13- 14

Observation Hour 15- 16 00-23 UTC

Release Time 17- 20 0000-2359 UTC, 9999 = missing

Number of levels 21- 24 number of subsequent data records


Data Record Format:

Variable Name Columns Description


Major Level Type 1- 1 1 = standard pressure level
2 = significant thermodynamic level
3 = additional wind level

Minor Level Type 2- 2 1 = surface, 2 = tropopause, 0 = other

Pressure 3- 8 units of Pa (mb * 100)

Pressure Flag 9- 9 A, B, or blank (see note 4 above)

Geopotential Height 10- 14 units of meters

Geopotential Height Flag 15- 15 A, B, or blank (see note 4 above)

Temperature 16- 20 units of degrees C * 10

Temperature Flag 21- 21 A, B, or blank (see note 4 above)

Dewpoint Depression 22- 26 units of degrees C * 10

Wind Direction 27- 31 units of degrees (0-360, inclusive)

Wind Speed 32- 36 units of (m/s)*10

I need just pressure, temperature and height (without flags) and this is (wrong) code that i wrote:

int main () {
// OPEN THE TEXT FILE
ifstream file_to_read(“udine.txt”);

// Maximum number of characters expected in a single line in the header
const int max_num_of_char_in_a_line = 24;
//Number of header files to skip
int num_of_header_lines = 1;

// SKIP ALL THE HEADER LINES

for(int i = 0; i < num_of_header_lines; ++i)
file_to_read.ignore(max_num_of_char_in_a_line, ‘\n’);

vector Pressure_array;
vector Temperature_array;
vector Altitude_array;

while(!file_to_read.eof()) {
if (!file_to_read.good()) break;

double Pressure;
double Altitude;
double Temperature;
//read the data
file_to_read.ignore(2);//dont read first 2 characters
file_to_read >> setw(5) >>Pressure ; //fill next 5 char
file_to_read.ignore(1);//dont read nex one char
file_to_read >> setw(5) >> Altitude;
file_to_read.ignore(1);
file_to_read >> setw(5) >> Temperature;
file_to_read.ignore(16);
//fill the data
Pressure_array.push_back(Pressure);
Altitude_array.push_back(Altitude);
Temperature_array.push_back(Temperature);
}

for(int i = 0; i < Pressure_array.size(); ++i){
cout << Pressure_array << ‘\n’;
double Pressure = Pressure_array.size[i];
}

for(int i = 0; i < Altitude_array.size(); ++i){
cout << “\naltitude are:\n” <<altitude_array << ‘\n’;
double Altitude = Altitude_array.size[i];
}

for(int i = 0; i < Temperature_array.size(); ++i){
cout << Temperature_array << ‘\n’;
double Temperature = Temperature_array.size[i];
}

return 0;

}

Could someone help me with this and tell me where i’m wrong?
Sorry for such a long question[/quote]

Try the things like:

ifstream fin( “InputFile” );
char buffer[256];
while ( !fin.eof() )
{
fin.getline( buffer, sizeof(buffer) );
if ( buffer[0] == ‘\0’ ) continue;
TString buf_str = buffer ;
if ( buf_str.Contains(“temperature”) ) {
// do some cuts or replace to get your values here
}
if ( buf_str.Contains(“height”) ) {
///
}

}

I think it is much easier to use shell/python script to get what you want.