// DDC control 
//123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456*/
//===================================-======================================*/
//                         DDC STATE & DATA CONTROL                         */
//-----------------------------------=--------------------------------------*/
// AUTHORs:        P.Ferrari & Heidi Sandaker
//
//   DESIGN ISSUEs:  Run state transitions
//
//   MODIF_HISTORY:  25.07.2003 - first version
//                   30.07.3003 - Preliminary - H. Sandaker
//                   01.08.2003 - startup of IV curve implemented- P.Ferrari
//								- Write files functions added by G.Codispoti
//===================================-======================================*/


main()
{
	string systemName = getSystemName();
	DebugN("Started requestIVCurve script");
	//this is IV curve command
	dpConnect("requestIVCurve", systemName+"requestIVCurve.trigger:_online.._value");
}

void requestIVCurve(string dpeTrigger, bool trigger)
{
	int crate, channel, Twait, response;
	unsigned startIVCurve;
	float Vstart, Vstop, Vstep, Vend, Imax, Iwarn;
	string parameters, allChannels, channelAddress,crateAddress;
	dyn_string para_split;
 
 	string systemName = getSystemName();
	//command executed on trigger value TRUE
	if(trigger)
		{
		  //get the command parameters
		  dpGet(systemName+"requestIVCurve.parameters:_online.._value", parameters);
		  para_split=strsplit(parameters,"|");
          
          //the format of parameters send by DDC (DAQ) is the following: Crate|Channel|HV_Start|HV_Stop|HV_Step|HV_End|Curr_Max|TimeWait
		  sscanf(para_split[1],"%d", crate);
		  sscanf(para_split[2],"%s", allChannels);
 		  sscanf(para_split[3],"%f", Vstart);
		  sscanf(para_split[4],"%f", Vstop);
		  sscanf(para_split[5],"%f", Vstep);
		  sscanf(para_split[6],"%f", Vend);
		  sscanf(para_split[7],"%f", Imax);
		  sscanf(para_split[8],"%f", Iwarn);
		  sscanf(para_split[9],"%d", Twait);

		  if(allChannels == "ALL")
			{
				//IV curve for all channels
	 	  		channel = 255;
			}
	  	  else
			{
	   			sscanf(allChannels,"%d", channel);
	   			
			}

		  DebugN("sParameters", crate, allChannels, Vstart, Vstop, Vstep, Vend, Imax, Iwarn , Twait);

		  // Read
		  //dpSetWait("Crate00.Controller.CCchRead.Send:_original.._value" , channel+64);

		
		  //make a 2 digit label for crates and channels
		  sprintf(channelAddress, "Crate%02d.Channel%02d", crate, channel);
		  sprintf(crateAddress, "Crate%02d", crate);		  
		  //sey the IV curve parameters
		  dpSet(systemName+channelAddress+".IVCurve.HV_Start.Send:_original.._value", Vstart);
		  dpSet(systemName+channelAddress+".IVCurve.HV_Stop.Send:_original.._value",  Vstop);
		  dpSet(systemName+channelAddress+".IVCurve.HV_Step.Send:_original.._value",  Vstep);
		  dpSet(systemName+channelAddress+".IVCurve.HV_End.Send:_original.._value",   Vend);
		  dpSet(systemName+channelAddress+".IVCurve.Curr_Max.Send:_original.._value", Imax);
		  dpSet(systemName+channelAddress+".IVCurve.Curr_Warn.Send:_original.._value", Iwarn);
		  dpSet(systemName+channelAddress+".IVCurve.TimeWait.Send:_original.._value", Twait);
		  
		  //here is setting of reponse parameter
		  response = 0; //(0 means cmmand successfull)
		  if(response != 0) response = 0-response; //response code in DDC is coded negatif
    	        dpSet(systemName+"requestIVCurve.response:_original.._value", response,
                systemName+"requestIVCurve.trigger:_original.._value", !trigger);
 
 	        //set HV channel (+64) on which IV curve should run
		  //dpSet(systemName+crateAddress+".Controller.CCchRead.Send:_original.._value", channel+64);  
 
		  // start time of the IV curve
		  dpSet(systemName+channelAddress+".IVCurve.StartTime:_original.._value", getCurrentTime());
		  //trigger the IV curve start
              channel = channel+64 | (( 1 & 0x00ff )<<8);
              dpSetWait("Crate00.Controller.CCchRead.Send:_original.._value" , channel);
   		  //dpSet(systemName+crateAddress+".Controller.StartIVCurve.Send:_original.._value", 1);
   		  dpSet("Info.Name:_original.._value", "IV scan has started!");
 	      delay(1);
          //set DAQ trigger to FALSE again
   		  dpSet(systemName+"requestIVCurve.response:_original.._value",0);
   		  dpSet(systemName+"requestIVCurve.trigger:_original.._value",0);

 		  //set the ELMB IV curve trigger to 0  			
   		  //dpSet(systemName+crateAddress+".Controller.StartIVCurve.Send:_original.._value", 0);
 
      	  //connect to the status to display it on the diagnostic pannel
      	  dpConnect("writeIVCurveInfo", systemName+channelAddress+".IVCurve.Status.Recv:_original.._value");  	

         //set DAQ trigger to FALSE again

   	    }//enf of(trigger)
}

writeIVCurveInfo(string dp,int iNewValue)
{ 

int channel,start,stop,step,last, crate;
file f;
string sParameter, filename, status,volt,curr,stat;
float V, Icurr,I;
int st,indx;
dyn_int probe;

	dpGet("requestIVCurve.parameters:_original.._value", sParameter);
	sscanf(sParameter,"%d|%d|%d|%d|%d|*", crate, channel, start, stop, step );
	sprintf(status,"SCTmops1:Crate%02d.Channel%02d.IVCurve.Status.Recv:_original.._value",crate,channel);	
	last = (stop - start) / step + 1;

	sprintf( volt , "SCTmops1:Crate%02d.Channel%02d.HVchVolt.Recv:_online.._value",crate, channel);
	sprintf( curr , "SCTmops1:Crate%02d.Channel%02d.HVchCurr.Recv:_online.._value",crate, channel);
      sprintf( stat , "SCTmops1:Crate%02d.Channel%02d.HVchStat.Recv:_online.._value",crate, channel);

	if((iNewValue>0)&&(iNewValue<=150)){
	  // the IV curve is running
	  dpSet("SCTmops1:Info.Name:_original.._value","IV curve is running");
	  if (iNewValue == 1) startGet(iNewValue, channel, crate);
	  dpGet( volt, V );
	  dpGet( curr, Icurr );
        dpGet( stat, st);
        I=Icurr;
	  probe=makeDynInt(62005,12005,2405,435);
	  indx=st&3; // this gives in indx 3&3=3 ; 2&3=2 ;1&3=1; 0&3 =0
        indx=indx+1;
        DebugN("Current before conversion",I);
	  I=I*2.5/4096/probe[indx]/0.000000001;
        DebugN("Current after conversion",I,"probe",probe[indx],indx);
	  DebugN(" IV curve running!!", "Status",iNewValue,"Volt",V,"Curr",I);
	  getValues(iNewValue, channel, crate);
/*	  if (iNewValue == last) {
		getLastValues(iNewValue, channel);
		dpDisconnect("EP_textFieldInCB1",status);
    		}*/
	  //DebugN(" IV curve running!!", iNewValue);
	  }
      if(iNewValue==255){
	  // the IV curve is started
	  dpSet("SCTmops1:Info.Name:_original.._value","IV curve is started");
	  //DebugN(" IV curve started!!", iNewValue);
	  }
	if(iNewValue==251){
	  dpSet("SCTmops1:Info.Name:_original.._value","IV curve is stopped");
	  dpGet( volt, V );
	  dpGet( curr, I );
        dpGet( stat, st);

	  probe=makeDynInt(62005,12005,2405,435);
	  indx=st&3; // this gives in indx 3&3=3 ; 2&3=2 ;1&3=1; 0&3 =0
        indx=indx+1;
	  I=I*2.5/4096/probe[indx]/0.000000001;

	  DebugN(" IV curve stopped!!","Status",iNewValue,"Volt",V,"Curr",I);
	  //DebugN(" IV curve stopped!!", iNewValue);
	  getValues(iNewValue, channel, crate);
	  getLastValues(iNewValue, channel, crate);
	  dpDisconnect("writeIVCurveInfo",status);
	  }
	if((iNewValue==0)){
	  // normal readout
	  dpSet("SCTmops1:Info.Name:_original.._value","Normal readout");
	  dpGet( volt, V );
	  dpGet( curr, I );
        dpGet( stat, st);

	  probe=makeDynInt(62005,12005,2405,435);
	  indx=st&3; // this gives in indx 3&3=3 ; 2&3=2 ;1&3=1; 0&3 =0
        indx=indx+1;
	  I=I*2.5/4096/probe[indx]/0.000000001;
	  DebugN(" Normal readout!!", "Status",iNewValue,"Volt",V,"Curr",I);
	  //DebugN(" Normal readout!!", iNewValue);
	  }

}
	
 
/* //Set Datapoints
   sprintf(dp1, "IVchannel%d.Start:_original.._value",channel);
   sprintf(dp2, "IVchannel%d.Stop:_original.._value",channel);
   sprintf(dp3, "IVchannel%d.Step:_original.._value",channel);
   sprintf(dp4, "IVchannel%d.End:_original.._value",channel);
   sprintf(dp5, "IVchannel%d.Imax:_original.._value",channel);
   sprintf(dp6, "IVchannel%d.Iwarn:_original.._value",channel);
   sprintf(dp7, "IVchannel%d.Twait:_original.._value",channel);


   dpSet(dp1,start);
   dpSet(dp2,stop);
   dpSet(dp3,step);
   dpSet(dp4,end);
   dpSet(dp5,Imax);
   dpSet(dp6,Iwarn);
   dpSet(dp7,Twait);
*/


startGet(int status, int channel, int crate)
{
time tm1;
string date, date1, time1, raw, path, sn;
file f;

	f = fopen("D:/SCT_DCS_DATA/IVcurve/temp.txt", "w");

	sprintf(date, "SCTmops1:Crate%02d.Channel%02d.IVCurve.StartTime:_online.._value",crate, channel);
	dpGet(date, tm1);
//	tm1 = getCurrentTime();
	date = formatTime("%Y%m%d_%H%M%S", tm1);
	path = "D:/SCT_DCS_DATA/IVcurve/";
	sprintf(sn, "CHAN%02d", channel);
	raw = path + sn + "_iv_" + date + ".txt";
	fputs(raw + "\n", f);

/*	date1 = formatTime("%d/%m/%Y", tm1);
	fputs(date1 + "\n", f);
	time1 = formatTime("%H:%M:%S", tm1);
	fputs(time1 + "\n", f);*/

	fclose(f);
}
//  Get IV curve values----------------------------------------------------------------------*
getValues(int status, int channel, int crate)
{
string volt, curr, warn, max,stat;
float temp, V, I, Iwarn, Imax;
int err; // error code
file f;
dyn_int probe;
int st,indx;
int problem, alert;

	if (status = 0) return;
	
	f = fopen("D:/SCT_DCS_DATA/IVcurve/temp.txt", "a"); // open for append
	err = ferror(f); // export error	
	
	sprintf( volt , "SCTmops1:Crate%02d.Channel%02d.HVchVolt.Recv:_online.._value",crate, channel);
	sprintf( curr , "SCTmops1:Crate%02d.Channel%02d.HVchCurr.Recv:_online.._value",crate, channel);
      sprintf( stat , "SCTmops1:Crate%02d.Channel%02d.HVchStat.Recv:_online.._value",crate, channel);

	  dpGet( volt, V );
	  dpGet( curr, I );
        dpGet( stat, st);

	  probe=makeDynInt(62005,12005,2405,435);
	  indx=st&3; // this gives in indx 3&3=3 ; 2&3=2 ;1&3=1; 0&3 =0
        indx=indx+1;
	  I=I*2.5/4096/probe[indx]/0.000000001;
	

	sprintf(warn, "SCTmops1:Crate%02d.Channel%02d.IVCurve.Curr_Warn.Send:_online.._value",crate, channel);
	sprintf(max, "SCTmops1:Crate%02d.Channel%02d.IVCurve.Curr_Max.Send:_online.._value",crate, channel);
	dpGet(warn, Iwarn);
	dpGet(max, Imax);
	if (I < Iwarn) problem = 0;
	else warn = 1;
	if (I < Imax) alert = 0;
	else alert = 1;

	fprintf(f, "%f\t%f\t%d\t%d\n", V, I, problem, alert);
//	fprintf(f, "%f\t%f\n", V, I);
//	DebugN("Write file", "Volt= ",V, " Icurr",I);

	if (err!=0) DebugN("Error no. ",err," occurred");
	fclose(f); // close file
}

//  Get Last Values----------------------------------------------------------------------*
getLastValues(int status, int channel, int crate)
{

int err; // error code
file f;
int st,indx;
dyn_int probe;
string temp0, temp1, curr_det , volt_det, curr_cc , volt_cc, curr_dd , volt_dd,datapoint,stat;
float T0, T1, Vdet, Idet, Vcc, Icc, Vdd, Idd;

	if (status = 0) return;
	
	f = fopen("D:/SCT_DCS_DATA/IVcurve/temp.txt", "a"); // open for append
	err = ferror(f); // export error	

// Read LV's
    delay(0,800);
    sprintf(datapoint,"Crate%02d.Controller.CCchRead.Send:_original.._value",crate);
    dpSetWait(datapoint, channel);
    delay(0,200);
    DebugN(" ******************we have just read the value", crate, channel);
    
// T0 & T1
	sprintf(temp0, "SCTmops1:Crate%02d.Channel%02d.MOch_Tm0.Recv:_online.._value", crate, channel);
	sprintf(temp1, "SCTmops1:Crate%02d.Channel%02d.MOch_Tm1.Recv:_online.._value",crate, channel);
	dpGet( temp0, T0);
	dpGet( temp1, T1);
//	T0=1/(log(T0*2.5/800.)/3435.+0.0033557)-273.;
//	T1=1/(log(T1*2.5/800.)/3435.+0.0033557)-273.;
	fprintf(f, "T0 = %f\n", T0);
	fprintf(f, "T1 = %f", T1);
	
// VDET & IDET
	sprintf(curr_det, "SCTmops1:Crate%02d.Channel%02d.HVchCurr.Recv:_online.._value",crate, channel);
	sprintf(volt_det, "SCTmops1:Crate%02d.Channel%02d.HVchVolt.Recv:_online.._value",crate, channel);
      sprintf( stat , "SCTmops1:Crate%02d.Channel%02d.HVchStat.Recv:_online.._value",crate, channel);

	dpGet( curr_det, Idet);
	dpGet( volt_det, Vdet);
      dpGet( stat, st);

	probe=makeDynInt(62005,12005,2405,435);
	indx=st&3; // this gives in indx 3&3=3 ; 2&3=2 ;1&3=1; 0&3 =0
      indx=indx+1;
	Idet=Idet*2.5/4096/probe[indx]/0.000000001;
      fprintf(f, "\nVdet %f\tIdet %f\n", Vdet, Idet);


// VCC & ICC
	sprintf(curr_cc, "SCTmops1:Crate%02d.Channel%02d.LVch_Icc.Recv:_online.._value",crate, channel);
	sprintf(volt_cc, "SCTmops1:Crate%02d.Channel%02d.LVch_Vcc.Recv:_online.._value",crate, channel);
	dpGet( curr_cc, Icc);
	dpGet( volt_cc, Vcc);
	fprintf(f, "Vcc %f\tIcc %f\n", Vcc, Icc);

// VDD & IDD
	sprintf(curr_dd, "SCTmops1:Crate%02d.Channel%02d.LVch_Idd.Recv:_online.._value",crate, channel);
	sprintf(volt_dd, "SCTmops1:Crate%02d.Channel%02d.LVch_Vdd.Recv:_online.._value",crate, channel);
	dpGet( curr_dd, Idd);
	dpGet( volt_dd, Vdd);
	fprintf(f, "Vdd %f\tIdd %f\n", Vdd, Idd);
	
	if (err!=0) DebugN("Error no. ",err," occurred");
	fclose(f); // close file
	writeRaw(channel, crate);
	Upload(channel, crate);
}


// Write Raw Data File ----------------------------------------------------------------------*
writeRaw(int channel, int crate)
{
int err, probl, alert;
string time1, date1, filename, date;
//string date, path, sn, temp;
string channelAddress;
int Twait;
float Vstart, Vstop, Vstep, Vend, Imax, Iwarn;
time tm1;
float T, V, I;
file f, g;
dyn_float volt, curr;
int i, j;

sprintf(channelAddress, getSystemName()+"Crate%02d.Channel%02d", crate, channel);

/* open temp file */
	g = fopen("D:/SCT_DCS_DATA/IVcurve/temp.txt", "r"); // open for read

/*	get Time */
	date =  channelAddress+".IVCurve.StartTime:_online.._value";
	dpGet(date, tm1);

/* set file name */
	fgets(filename, 100, g);

/* create raw file */
	f = fopen( strrtrim(filename, "\n"), "w"); // create for write
	err = ferror(f); // export error
	
/* header */
//	fgets(date1, 11, g);
	date1 = formatTime("%d/%m/%Y", tm1);
	fputs("TEST_DATE\t: " + date1, f);
//	fgets(time1, 9, g);
	time1 = formatTime("%H:%M:%S", tm1);
	fputs("\nTEST_TIME\t: " + time1, f);

/* IV Values */ 
	i=1;
	while( fscanf(g, "%f\t%f\t%d\t%d\n", V, I, probl, alert) ) {
		volt[i] = V;
		curr[i] = I;
		i++;
		}
		
/* temperature */
	fscanf(g, "T0 = %f", T);
	fprintf(f, "\nTEMPERATURE\t: %f ", T);
	fputs("\n#\n#V\tI\n", f);
	for (j=1; j<i; j++)	fprintf(f, "%f\t%f\n", volt[j], curr[j]);
	
fputs("#V\tmicroA\n#\n", f);

fprintf(f, "\nCrate\t: %f ", crate);
fprintf(f, "\nChannel\t: %f ", channel);
dpGet(channelAddress+".IVCurve.HV_Start.Send:_original.._value", Vstart);
fprintf(f, "\nVstart\t: %f ", Vstart);
dpGet(channelAddress+".IVCurve.HV_Stop.Send:_original.._value",  Vstop);
fprintf(f, "\nVstop\t: %f ", Vstop);
dpGet(channelAddress+".IVCurve.HV_Step.Send:_original.._value",  Vstep);
fprintf(f, "\nVstep\t: %f ", Vstep);
dpGet(channelAddress+".IVCurve.HV_End.Send:_original.._value",   Vend);
fprintf(f, "\nVend\t: %f ", Vend);
dpGet(channelAddress+".IVCurve.Curr_Max.Send:_original.._value", Imax);
fprintf(f, "\nImax\t: %f ", Imax);
dpGet(channelAddress+".IVCurve.Curr_Warn.Send:_original.._value", Iwarn);
fprintf(f, "\nIwarn\t: %f ", Iwarn);
dpGet(channelAddress+".IVCurve.TimeWait.Send:_original.._value", Twait);
fprintf(f, "\nTwait\t: %d ", Twait);
	     


/* close file */
fclose(f); // close file
DebugN("Raw Data file written");
if (err!=0) DebugN("Error no. ",err," occurred");
fclose(g);
}

// Write the Upload file ---------------------------------------------------------------------*
Upload( int channel, int crate )
{
int err, probl, alert; 
string date, date1, time1, filename, path, sn, dr, loc, run, dut;
string passed, problem, host, version, def, raw;
file f,g;
time tm1;
string curr, max, warn;
float Icurr, Imax, Iwarn, Itrip, T0, T1, Vdet, Idet, Vcc, Icc, Vdd, Idd, V, I;
float Ileak150, Ileak350, Ileak500;


/* hardcode values ********************************/
	path = "D:/SCT_DCS_DATA/IVcurve/";
	sprintf(sn, "CHAN%02d", channel); // SERIAL NUMBER
	dr = "sct_dcs";			// TEST MADE BY
	loc = "Oxford";			// LOCATION NAME
	run = 111;				// Run Number
	host = "PCEF";			// DAQ_INFO
	version = "7.0";		// DAQ_INFO
	dut = "Barrel_Module";	// DAQ_INFO
	def = "No";				// DetModIV defects

// set file name ***********************************/
//	tm1 = getCurrentTime();
	sprintf(date, "SCTmops1:Crate%02d.Channel%02d.IVCurve.StartTime:_online.._value", crate, channel);
	dpGet(date, tm1);
	date = formatTime("%Y%m%d", tm1);
	filename = path + sn + "_" + date + ".txt";

/* open file  ***********************************/
	f = fopen(filename,"a"); // open for append
	err = ferror(f); // export error
	if (err!=0) DebugN("Error no. ",err," occurred");
	
/* read temp file  ***********************************/
	g = fopen("D:/SCT_DCS_DATA/IVcurve/temp.txt", "r");// Open temporary file
	fgets(raw, 100, g);
	raw = strrtrim(raw, "\n");
//	fgets(date1, 11, g);
//	fgets(time1, 9, g);
	Ileak150 = Ileak350 = Ileak500 = -1;
	while( fscanf(g, "%f\t%f\t%d\t%d\n", V, I, probl, alert) ) {
	if( V > 149.5 && V < 150.5 ) Ileak150 = I;
	if( V > 349.5 && V < 350.5 ) Ileak350 = I;
	if( V > 499.5 && V < 500.5 ) Ileak500 = I;
	if(probl) problem = "YES"; else problem = "NO";
	if(alert) passed = "NO"; else passed ="YES";
	}
	fscanf(g, "T0 = %f\n", T0);
	fscanf(g, "T1 = %f\n", T1);
	fscanf(g, "Vdet %f\tIdet %f\n", Vdet, Idet);
	fscanf(g, "Vcc %f\tIcc %f\n", Vcc, Icc);
	fscanf(g, "Vdd %f\tIdd %f\n", Vdd, Idd);
	fclose(g);// Close temporary file
		DebugN("raw", raw);
		DebugN("date1", date1);
		DebugN("time1", time1);
		DebugN("Ileak150", Ileak150);
		DebugN("Ileak350", Ileak350);
		DebugN("Ileak500", Ileak500);
		DebugN("T0", T0);
		DebugN("T1", T1);
		DebugN("Vdet", Vdet, "Idet", Idet);		
		DebugN("Vcc", Vcc, "Icc", Icc);	
		DebugN("Vdd", Vdd, "Idd", Idd);	
		
		
// header -------------------------------------
	fputs("#\n%NewTest\n#", f);
	fputs("\nSERIAL NUMBER\t: " + sn, f);	// SERIAL NUMBER
	fputs("\nTEST MADE BY\t: " + dr, f);	// TEST MADE BY
	fputs("\nLOCATION NAME\t: " + loc, f);	// LOCATION NAME
	fputs("\nRun Number\t: " + run, f);		// Run Number
	date1 = formatTime("%d/%m/%Y", tm1);
	fputs("\nTEST_DATE\t: " + date1, f);	// TEST_DATE

/*  PASSED & PROBLEM ********************************/
/*	sprintf(curr, "SCTmops1:Crate00.Channel00.HVchCurr.Recv:_online.._value", channel);
	sprintf(warn, "SCTmops1:Crate00.Channel00.IVCurve.Curr_Warn.Send:_online.._value", channel);
	sprintf(max, "SCTmops1:Crate00.Channel00.IVCurve.Curr_Max.Send:_online.._value", channel);
	dpGet(curr, Icurr);
	dpGet(warn, Iwarn);
	dpGet(max, Imax);

	if (Icurr < Imax) passed ="YES";
	else passed = "NO";
	if (Icurr < Iwarn) problem = "NO";
	else problem = "YES";*/

	fputs("\nPASSED\t\t: " + passed, f);
	fputs("\nPROBLEM\t\t: " + problem, f);

/* DAQ_INFO ******************************************/
	fputs("\n#\n%DAQ_INFO\n#\n#HOST\n\"" + host + "\"", f);	// HOST
	fputs("\n#VERSION\n\"" + version + "\"", f);			// VERSION
	fputs("\n#DUT\n\"" + dut + "\"", f);					// DUT
	time1 = formatTime("%H:%M:%S", tm1);
	fputs("\n#TIME\n\"" + strrtrim(time1, "\n") + "\"", f);	// TIME

/* DCS_INFO ******************************************/
	fputs("\n#\n%DCS_INFO\n#\n#T0\tT1\n", f);

// T0 & T1
	fprintf(f, "%f\t%f ", T0, T1);


// VDET & IDET
	fputs("\n#VDET\tIDET\n", f);
	fprintf(f, "%f\t%f ", Vdet, Idet);

// VCC & ICC
	fputs("\n#VCC\tICC\n", f);
	fprintf(f, "%f\t%f ", Vcc, Icc);

// VDD & IDD
	fputs("\n#VDD\tIDD\n", f);
	fprintf(f, "%f\t%f ", Vdd, Idd);

// TIME POWERED
	fputs("\n#TIME_POWERED\n", f);
	fputs(".", f);
	
/* DCS_INFO ******************************************/
	fputs("\n#\n%DetModIV\n", f);				// 
	fprintf(f, "#\nTEMPERATURE\t: %f", T0);		// TEMPERATURE
	fprintf(f, "\nI LEAK 150\t: %f", Ileak150);	// I LEAK 150
	fprintf(f, "\nI LEAK 350\t: %f", Ileak350);	// I LEAK 350
	fprintf(f, "\n#I LEAK 500\t: %f", Ileak500);// I LEAK 500
	
	
// DEFECTS
	if( probl || alert ) def = "";
	else def = "No";
	fputs("\n#" + def + " defects found!", f);

/* RAWDATA File ******************************************/
//	dpGet("SCTmops1:IVfile.Name:_online.._value", raw);
	fputs("\n#\n%TEST Rawdata\n", f);
	fputs("FILENAME\t: " + raw, f);
	fputs("\n#\n", f);

/* close file*/
	DebugN("Upload file written");
	fclose(f); // close file
}


