get_STEREO.cpp 3.72 KB
/////////////////
// CPP HEADERS //
/////////////////
#include <iostream>
#include <fstream>
#include <stdlib.h> 
#include <string.h>

////////////
/// DDLIB //
////////////
#include "DD.h"

////////////////
// NAMESPACES //
///////////////
using namespace std;

int main  (int argc, char const *argv[]) throw(std::string)
{
	if (argc != 6)
	{
		 cout << "[ERROR] Usage : ./get_STEREO [a,b] (plasma filename) (mag filename) (ddStart) (ddInterval)" << endl;
		 exit(EXIT_FAILURE);
	}

	string stereo   = argv[1];
	string dataFilename   = argv[2];
	string dataFilename1  = argv[3];
	string ddStart        = argv[4];
	string ddInterval     = argv[5];
	
	string plasmaVi       = "sta_l2_pla";
	string timeParameter  = "Time";
	string DensParameter  = "Np";
	string TempParameter  = "Vth";
	string VNormParameter = "Vrtn";
	string magVi          = "sta_mag_mag";
	string magParameter   = "B";
	
	ofstream plasmaFile(dataFilename.c_str(), ios::out); 
	ofstream magFile(dataFilename1.c_str(), ios::out);
	
	memcpy(&(plasmaVi[2]),argv[1],1);
	memcpy(&(magVi[2]),argv[1],1);
	
	printf("PlasmaVi %s\n", plasmaVi.c_str());
	int id_sw = DD_SetVariable( const_cast<char*>( plasmaVi.c_str() ) );
	int id_mag = DD_SetVariable( const_cast<char*>( magVi.c_str() ) );
	
	char *st = const_cast<char*>( ddStart.c_str() );
	
	int error = DD_SetTime(id_sw, st);
	
	if (error < 0)
	{
		std::string ddSetTimeErr = "[ERROR] Bad time pointer init in DD_SetTime SW -> err value : " +  error;
		printf(" bad time pointer plasma %d %d\n", id_sw, error);
		throw ddSetTimeErr;
	}
	error = DD_SetTime(id_mag, st);
	
	if (error < 0)
	{
		std::string ddSetTimeErr = "[ERROR] Bad time pointer init in DD_SetTime Mag -> err value : " +  error;
		printf(" bad time pointer mag %d\n", error);
		throw ddSetTimeErr;
	}
	// GET DATA
	char *params[4];
	
	params[0] = const_cast<char*>( timeParameter.c_str() );
	params[1] = const_cast<char*>( DensParameter.c_str() );
	params[2] = const_cast<char*>( TempParameter.c_str() );
	params[3] = const_cast<char*>( VNormParameter.c_str() );
	 
	char *params_mag[2];
	params_mag[0] = const_cast<char*>( timeParameter.c_str() );
	params_mag[1] = const_cast<char*>( magParameter.c_str() );
	
	char *timeIntervall = const_cast<char*>( ddInterval.c_str() );
	
	DD_data_t *data;

	int status = 0;
 
	plasmaFile << "Time,Density,Vth,Vr,Vt,Vn" << endl;

	do {

		status = DD_GetMultiData(id_sw, 4, static_cast<char**>(params), timeIntervall, &data, 1);
	 	
		if (status < 0)
		{
			std::string ddGetDataErr = "[ERROR] Failed to get SW data -> status : " + status;
			throw std::string(ddGetDataErr);
		}
		
		for (int j = 0; j < data->VarNumber; ++j)
		{
			plasmaFile << static_cast<char*>(data[0].Variables[j]) << ","
			<< *(static_cast<float*>(data[1].Variables[j])) << ","
			<< *(static_cast<float*>(data[2].Variables[j])) << ","
			<< static_cast<float*>(data[3].Variables[j])[0] << ","
			<< static_cast<float*>(data[3].Variables[j])[1] << ","
			<< static_cast<float*>(data[3].Variables[j])[2] << endl;
		}

	} while (status == MOREDATA);
	
	DD_Close(id_sw);
	 
	status = 0;
	
	magFile << "Time,Bx,By,Bz" << endl;
	
	do {
		
		status = DD_GetMultiData(id_mag, 2, static_cast<char**>(params_mag), timeIntervall, &data, 1);
	 
		if (status < 0)
		{
			std::string ddGetDataErr = "[ERROR] Failed to get MAG data -> status : " + status;
			throw std::string(ddGetDataErr);
		}
		
		for (int j = 0; j < data->VarNumber; ++j)
		{
			magFile << static_cast<char*>(data[0].Variables[j]) << ","			
			<< static_cast<float*>(data[1].Variables[j])[0] << ","
  			<< static_cast<float*>(data[1].Variables[j])[1] << ","
  			<< static_cast<float*>(data[1].Variables[j])[2] << endl;
		}
 
	} while (status == MOREDATA);
	
    
    DD_Close(id_mag);
    
    plasmaFile.close();
    
    magFile.close();
    
	return 0;
}