view FXAnalyse.c @ 10:239f2fe67cc0

Fix slope cancelling following Hg cavity What we really want to do is to keep constant the frequency of the beat note between comb and 1062 nm light from the Hg cavity, acting on the repetition rate by stearing the frequency of the 1542 nm light from the local cavity to which we lock the comb.
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Wed, 04 Jul 2012 14:39:50 +0200
parents 5549a69e1cfa
children 783407954cdf
line wrap: on
line source

#include <utility.h>
#include <ansi_c.h>
#include <cvirte.h>		
#include <userint.h>
#include <formatio.h>
#include <string.h>

#include "YLCStuff.h"
#include "FXAnalyse.h" 
#include "Plot.h"
#include "Allan.h"
#include "DDSBes.h"
#include "DDS4xAD9912.h"
#include "DDS_Fox.h" 
#include "muParserDLL.h"


#define FXLINELENGTH 123
#define DDSBESCLOCK (200.e6)	 // 200MHz de clock pour la DDS de Besancon...


#define LOGFILEPATH "C:\\Femto\\Software\\FXQE80"		
#define FREP_NORMAL_FORMULA "(0-15M+DDS-Ch1/200)/12+2M/3"
#define NUOL_NORMAL_FORMULA "(N*250M-194400G)+N*Math1-8*DDS1"
#define NUHG_NORMAL_FORMULA "(N*250M-282143G)+N*Math1-(DDS1-Ch2)"
#define NUSR_NORMAL_FORMULA "(N*250M-429228G)+N*Math1+(DDS2-Ch3)"
#define NU813_NORMAL_FORMULA "(N*766M-368554G)+N*Math1+000.000M"
#define NU1064_NORMAL_FORMULA "(N*766M-281630G)+(DDS_1064-Ch4)"

#define FACTORDDSBES 36*(8/770000)

//==============================================================================
// Static global variables

/* multi threading stuff */  // to be used when I will replace timer by thread for counder readout
//static int ghPool;
//static volatile int vgExiting = 0;

/* panel handling stuff */
static PanelHandle MainPanel ;
static PanelHandle CalcN1Panel ;
static PanelHandle CalcN2Panel ;
static PanelHandle CalcN3Panel ;

//==============================================================================
// Static functions

//static int CVICALLBACK ReadCounterThread(void *functionData);	   // to be used when I will replace timer by thread for counder readout  

//==============================================================================
// Global variables

char * LogFileName ;
char ExtraMathFileName[MAX_PATHNAME_LEN] ;

double utc = 0 ;
double mjd =0 ;

double Ch1, Ch2, Ch3, Ch4 ; // le type long double est identique au double (8 octets) sous CVI !!! 
double Math1, Math2, Math3, Math4, Math5; // N est en fait un entier en pratique...
double N1, N2, N3,N4 ;

Plot_Data PlotCh1, PlotCh2, PlotCh3, PlotCh4, PlotMath1, PlotMath2, PlotMath3, PlotMath4, PlotMath5 ;  
Allan_Data AllanCh1, AllanCh2, AllanCh3, AllanCh4, AllanMath1, AllanMath2, AllanMath3, AllanMath4, AllanMath5 ; 

DDSBes_Data DDSBes ;
DDS4xAD9912_Data DDS4xAD9912 ;

muParserHandle_t MathParser1, MathParser2, MathParser3, MathParser4, MathParser5 ;

volatile bool Acquiring = FALSE ;

long OldLogFilePtr = 0 ;

double NuOpt=0.0;
double Ndiv=8.0;

///////////////////
//Pour les calculs des N

volatile bool Measuring_1 = FALSE,Step1_1 = FALSE,Step2_1 = FALSE,Step3_1 = FALSE, Measuring_2 = FALSE,Step1_2 = FALSE,Step2_2 = FALSE,Step3_2 = FALSE;
volatile bool Measuring_3 = FALSE,Step1_3 = FALSE,Step2_3 = FALSE,Step3_3 = FALSE;
volatile bool TimetoBegin=FALSE;

double FrequDDS1=110000000.0,FrequDDS2=15300000.0,FrequDDS3=150400000.000000;
double Slope_1=0.0,Slope_2=0.0,Slope_3=0.0,Beatslope_2=0.0; 
double SlopeTime1=60.0,SlopeTime2=60.0;

double N_1=0.0,N_2=0.0,N_3=0.0;
double DeltaT_1=8.0,DeltakHz_1=1800.0,t1_1=0.0,t2_1=0.0,t3_1=0.0, Frepplus_1=0.0, Frepminus_1=0.0;
double DeltaT_2=8.0,DeltakHz_2=1500.0,t1_2=0.0,t2_2=0.0,t3_2=0.0, Frepplus_2=0.0, Frepminus_2=0.0;
double DeltaT_3=20.0,DeltakHz_3=100.0,t1_3=0.0,t2_3=0.0,t3_3=0.0, Frepplus_3=0.0, Frepminus_3=0.0;


int DeltaTMoy_1=3,n_1=0,DeltaTMoy_2=3,n_2=0,DeltaTMoy_3=3,n_3=0;
double Tmoy_1_1=0.0,Tmoy_2_1=0.0,Tmoy_1_2=0.0,Tmoy_2_2=0.0,Tmoy_1_3=0.0,Tmoy_2_3=0.0; 
double Frequ_slope_1=0.0,Moy_slope_1=0.0,Slope_slope_1=0.0,Frequ_slope_2=0.0,Moy_slope_2=0.0,Slope_slope_2=0.0,Frequ_slope_3=0.0,Moy_slope_3=0.0,Slope_slope_3=0.0;  
int N_slope_1=0,N_slope_2=0,N_slope_3=0;
double  Beat_slope_2=0.0 ,Moy_Beatslope_2=0.0,Slope_Beatslope_2=0.0; 
double Ch4_slope=0.0,Moy_Ch4slope_1=0.0,Slope_Ch4slope_1=0.0,Ch4Slope=0.0;

double FrequencyDDSBes=0.0,FrequencyDDSBesInit=0.0;
double FrequencyDDS3=0.0,FrequencyDDS3Init=0.0;
double DeltaFrep275=0.0,DeltaFrep10=0.0; ;
double DDSBesChanged1=FALSE,DDSBesChanged2=FALSE;

double DeltaDDS3=0.0,Delta10K_Plus=0.0,Delta10K_Minus=0.0;
double Nu1=0.0, Nu2= 200000-147000+282143746.557455e6;  

double Step1=800000.0,Step2=800000.0;

double Ch4Plus=0.0,Ch4Minus=0.0;

int NDEJAPASSE=0, nDDSChange=0, nstabilization=0  ;

//////
//Pour les signes de fb

double Frequencystep1=10000.0,tbegin1=0.0,Frepbefore1=0.0,Frequency1=0.0,Frequencystep2=10.0,tbegin2=0.0,Frepbefore2=0.0,Ch2before=0.0,Frequency2=0.0,Frequencystep3=100000.0,tbegin3=0.0,Frepbefore3=0.0,Frequency3=0.0;
volatile bool Getsign1=FALSE,Getsign2=FALSE,Getsign3=FALSE;
double Signe1=1.0,Signe2=1.0,Signe3=0.0;





/////////////////////////
// Slope Cancelling Variables

volatile bool SlopeMeasuring=FALSE;
volatile bool AutoStopSlopeCancellingIfDelock=TRUE;
volatile bool OnSlopeCancelling =FALSE;  
double SlopeMath2=0.0;
double TimetoSlope=60.0;
double SlopeMeasuringTimeBegin=0.0;
char * OnOffMessage="OFF" ;

char DDSFoxName[255] = "DDS Fox Control" ; 
double DDSFoxClock=200000000;
int DDSFoxSweepSign=0;
double DDSFoxFrequency=70000000.0;
char DDSFoxip[255]="145.238.205.58";
int DDSFoxPort=6665 ;
unsigned int DDSFoxProfil=7;
double DDSFoxSweeprate=0.0;// numéro de profil de la DDS : single freq=0, sweep=7  ;
double DDSFoxDeltaT=0.01;

double Slope_Math2slope=0.0,Math2_slope=0.0,Moy_Math2slope=0.0;
int N_Math2slope=0.0,nstabilisationSlopeMeasuring=0;

double LimitToDelock=5.0;
double SlopeCorrection=0.0;

int ratio=10; //Recentre la frequence tous les ratios

volatile bool FrequCorrec=FALSE,KeepFrequ=TRUE, KeepSlope=TRUE ;
int Nratio=-1;
double MoyMath2 = 0.0, CenterFrequencyCh2 = 0.0;
volatile bool CenterFrequencyCh2ToDetermine=FALSE;

double limitotakoff=70.0;

enum {
	SLOPE_REFERENCE_MICROWAVE = 0,
	SLOPE_REFERENCE_HG_CAVITY = 1,
};

int slopeReference = SLOPE_REFERENCE_MICROWAVE;

//////////////////////////////////
//  Keep Centered Variables

volatile bool KeepCentering=FALSE; 
double Timetorecenter275K=3600.0*10; 
double Timetorecenter10K=3600.0*3;  
double CenteringTimeBegin275K=0.0;
double CenteringTimeBegin10K=0.0; 



//==============================================================================
// Functions
 int LaunchDDSFOX (char *Name,char *ip,int Port,unsigned int Profil,double Clock,double DeltaT,double Frequency,double Sweeprate,int mode) ;
 double ReadDDSFOX (char *Name,char *ip,int Port,unsigned int Profil,double Clock,double DeltaT,double Frequency,double Sweeprate) ;

 
 
 

void initMathParser(muParserHandle_t * pMathParser) 
{
	(* pMathParser) = mupCreate() ;		// Math parser number 1, normally used for frep
	mupDefineOprtChars( * pMathParser, "abcdefghijklmnopqrstuvwxyzµ"
                              		   "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                            		   "+-*^/?<>=#!$%&|~'_");
	mupDefineVar( * pMathParser, "UTC", &utc);
	mupDefineVar( * pMathParser, "MJD", &mjd);
	mupDefineVar( * pMathParser, "Ch1", &Ch1);
	mupDefineVar( * pMathParser, "Ch2", &Ch2);
	mupDefineVar( * pMathParser, "Ch3", &Ch3);
	mupDefineVar( * pMathParser, "Ch4", &Ch4);
	mupDefineVar( * pMathParser, "DDS_Frep", &(DDSBes.Frequency));
	mupDefineVar( * pMathParser, "DDS1", &(DDS4xAD9912.Frequency1));   
	mupDefineVar( * pMathParser, "DDS2", &(DDS4xAD9912.Frequency2));
	mupDefineVar( * pMathParser, "DDS3", &(DDS4xAD9912.Frequency3));  
	mupDefineVar( * pMathParser, "DDS4", &(DDS4xAD9912.Frequency4));  
	mupDefineVar( * pMathParser, "N1", &N1);
	mupDefineVar( * pMathParser, "N2", &N2);
	mupDefineVar( * pMathParser, "N3", &N3);
	mupDefineVar( * pMathParser, "Nu1", &Nu1);
	mupDefineVar( * pMathParser, "Nu2", &Nu2);
	mupDefineVar( * pMathParser, "DeltaDDS3", &DeltaDDS3);
	mupDefineVar( * pMathParser, "Signe1", &Signe1);
	mupDefineVar( * pMathParser, "Signe2", &Signe2); 
	mupDefineVar( * pMathParser, "Ndiv", &Ndiv); 
	mupDefinePostfixOprt( * pMathParser, "P", &Peta , 1);
	mupDefinePostfixOprt( * pMathParser, "T", &Tera , 1);
	mupDefinePostfixOprt( * pMathParser, "G", &Giga , 1);
	mupDefinePostfixOprt( * pMathParser, "M", &Mega , 1);
	mupDefinePostfixOprt( * pMathParser, "k", &kilo , 1);
	mupDefinePostfixOprt( * pMathParser, "m", &milli, 1);
	mupDefinePostfixOprt( * pMathParser, "u", &micro, 1);
	mupDefinePostfixOprt( * pMathParser, "µ", &micro, 1);
	mupDefinePostfixOprt( * pMathParser, "n_1", &nano,  1);
	mupDefinePostfixOprt( * pMathParser, "p", &pico,  1);
	mupDefinePostfixOprt( * pMathParser, "f", &femto, 1);
}

int main (int argc, char *argv[])
{
	//int ThreadId ;   // to be used when I will replace timer by thread for counder readout
	
	char FileNamePrototype[MAX_PATHNAME_LEN] ;  
	double initfreqDDS ;
	char * MathString1 = "                                                             " ;
	char * MathString2 = "                                                             " ;
	char * MathString3 = "                                                             " ;
	char * MathString4 = "                                                             " ;  
	char * MathString5 = "                                                             " ;
	
	if (InitCVIRTE (0, argv, 0) == 0)
		return -1;	/* out of memory */
	if ((MainPanel = LoadPanel (0, "FXAnalyse.uir", PANEL)) < 0)
		return -1;
	if ((CalcN1Panel = LoadPanel (0, "FXAnalyse.uir", CALCN1)) < 0)
		return -1;
	if ((CalcN2Panel = LoadPanel (0, "FXAnalyse.uir", CALCN2)) < 0)
		return -1;
	if ((CalcN3Panel = LoadPanel (0, "FXAnalyse.uir", CALCN3)) < 0)
		return -1;
	
	DisplayPanel (MainPanel);
	
		// Initialize LogFileName pointer
	
	LogFileName = FileNamePrototype ;
	
	// Initialyze the DDS Besancon Output
	
	DDSBes_Initialize(&DDSBes) ;
	DDSBes_SetClockFrequency(&DDSBes, DDSBESCLOCK) ;	   // Peut etre passer tout ca dans DDSBes-Initialize ?
	DDSBes_SetAmplitude(&DDSBes, 4095) ;
	GetCtrlVal(MainPanel, PANEL_DDSFREPOUT,&initfreqDDS) ;
	DDSBes_SetFrequency(&DDSBes, initfreqDDS) ; 
	
	// Initialize the quadruple AD9959 DDS (Fox board interface)
	
	DDS4xAD9912_Reset(&DDS4xAD9912) ;
	GetCtrlVal(MainPanel, PANEL_DDS1, &initfreqDDS) ;
	DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, initfreqDDS) ;
	GetCtrlVal(MainPanel, PANEL_DDS2, &initfreqDDS) ;
	DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, initfreqDDS) ;
	GetCtrlVal(MainPanel, PANEL_DDS3, &initfreqDDS) ;
	DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, initfreqDDS) ;
	GetCtrlVal(MainPanel, PANEL_DDS4, &initfreqDDS) ;
	DDS4xAD9912_SetFrequency(&DDS4xAD9912, 4, initfreqDDS) ;
	
	
	// Initialyze the DDSFOX Output  
	LaunchDDSFOX (DDSFoxName,DDSFoxip,DDSFoxPort,DDSFoxProfil,DDSFoxClock,DDSFoxDeltaT,DDSFoxFrequency,DDSFoxSweeprate,1);  
	
	// Initialize the 5 (separate) math parsers for frep, Nu_Hg, Nu_Sr, N3 and ExtraMath

	Ch1=0.0 ; Ch2=0.0 ; Ch3=0.0 ; Ch4=0.0 ;
	GetCtrlVal(MainPanel, PANEL_N1CHOICE, &N1) ;
	GetCtrlVal(MainPanel, PANEL_N2CHOICE, &N2) ;

	GetCtrlVal(MainPanel, PANEL_N3CHOICE, &N3) ;
	
	initMathParser(&MathParser1) ;
	mupDefineVar(MathParser1, "DDS", &(DDSBes.Frequency));
	GetCtrlVal(MainPanel, PANEL_MATHSTRING1, MathString1) ;
	mupSetExpr(MathParser1, MathString1);
	
	initMathParser(&MathParser2) ;
	mupDefineVar( MathParser2, "Math1", &Math1);
	mupDefineVar( MathParser2, "DDS", &(DDS4xAD9912.Frequency1));
	GetCtrlVal(MainPanel, PANEL_MATHSTRING2, MathString2) ;
	mupSetExpr(MathParser2, MathString2);
	
	initMathParser(&MathParser3) ;
	mupDefineVar( MathParser3, "Math1", &Math1);
	mupDefineVar( MathParser3, "Math2", &Math2);
	mupDefineVar( MathParser3, "DDS", &(DDS4xAD9912.Frequency2));
	GetCtrlVal(MainPanel, PANEL_MATHSTRING3, MathString3) ;
	mupSetExpr(MathParser3, MathString3);
	
	initMathParser(&MathParser4) ;
	mupDefineVar( MathParser4, "Math1", &Math1);
	mupDefineVar( MathParser4, "Math2", &Math2);
	mupDefineVar( MathParser4, "Math3", &Math3);
	GetCtrlVal(MainPanel, PANEL_MATHSTRING4, MathString4) ;
	mupSetExpr(MathParser4, MathString4);
	
	initMathParser(&MathParser5) ;
	mupDefineVar( MathParser5, "Math1", &Math1);
	mupDefineVar( MathParser5, "Math2", &Math2);
	mupDefineVar( MathParser5, "Math3", &Math3);
	mupDefineVar( MathParser5, "Math4", &Math4);
	GetCtrlVal(MainPanel, PANEL_MATHSTRING5, MathString5) ;
	mupSetExpr(MathParser5, MathString5);
	
	
	RunUserInterface ();
	
	// Stops everything at the end
	
	DiscardPanel (MainPanel);
	return 0;
}


void OnCloseViewPanel(int panel){
	
	if (panel==PlotCh1.PlotPanel) 		{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_FREQ1PLOT, FALSE) ; } ; 
	if (panel==PlotCh2.PlotPanel) 		{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_FREQ2PLOT, FALSE) ; } ; 
	if (panel==PlotCh3.PlotPanel) 		{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_FREQ3PLOT, FALSE) ; } ; 
	if (panel==PlotCh4.PlotPanel) 		{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_FREQ4PLOT, FALSE) ; } ;
	if (panel==PlotMath1.PlotPanel) 	{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_MATH1PLOT, FALSE) ; } ; 
	if (panel==PlotMath2.PlotPanel) 	{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_MATH2PLOT, FALSE) ; } ;
	if (panel==PlotMath3.PlotPanel) 	{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_MATH3PLOT, FALSE) ; } ; 
	if (panel==PlotMath4.PlotPanel) 	{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_MATH4PLOT, FALSE) ; } ;
	if (panel==PlotMath5.PlotPanel) 	{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_MATH5PLOT, FALSE) ; } ;
	
	if (panel==AllanCh1.AllanPanel) 	{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_FREQ1ALLAN, FALSE) ; } ; 
	if (panel==AllanCh2.AllanPanel) 	{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_FREQ2ALLAN, FALSE) ; } ; 
	if (panel==AllanCh3.AllanPanel) 	{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_FREQ3ALLAN, FALSE) ; } ; 
	if (panel==AllanCh4.AllanPanel) 	{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_FREQ4ALLAN, FALSE) ; } ; 
	if (panel==AllanMath1.AllanPanel) 	{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_MATH1ALLAN, FALSE) ; } ; 
	if (panel==AllanMath2.AllanPanel) 	{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_MATH2ALLAN, FALSE) ; } ; 
	if (panel==AllanMath3.AllanPanel) 	{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_MATH3ALLAN, FALSE) ; } ; 
	if (panel==AllanMath4.AllanPanel) 	{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_MATH4ALLAN, FALSE) ; } ; 
	if (panel==AllanMath5.AllanPanel) 	{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_MATH5ALLAN, FALSE) ; } ;  
	
	return ;
}





int CVICALLBACK QuitCallback (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	switch (event)
		{
		case EVENT_COMMIT:
			QuitUserInterface (0);
			DDSBes_Close(&DDSBes);
			mupRelease(MathParser1);
			mupRelease(MathParser2); 
			mupRelease(MathParser3);
			mupRelease(MathParser4);
			mupRelease(MathParser5);
			break;
		case EVENT_RIGHT_CLICK:

			break;
		}
	return 0;
}

int  CVICALLBACK CB_OnEventMain(int panel, int event, void *callbackData, int eventData1, int eventData2)
{
	int VirtualKeyCode ;
	int ActiveControl ;
	int StepIndex ;
	double Step ;
	
	switch (event) {
		case EVENT_CLOSE:
			QuitUserInterface (0);
			DDSBes_Close(&DDSBes);
			mupRelease(MathParser1);
			mupRelease(MathParser2); 
			mupRelease(MathParser3);
			break;
		case EVENT_KEYPRESS:
			switch (eventData1)	// ie the code of the key which has been stroke 
				{
				case VAL_RIGHT_ARROW_VKEY :
					ActiveControl = GetActiveCtrl(panel);
					if (ActiveControl==PANEL_DDSFREPOUT || ActiveControl==PANEL_DDSFREPSTEP) {
						GetCtrlIndex(MainPanel, PANEL_DDSFREPSTEP, &StepIndex);
						if (StepIndex<14){
							SetCtrlIndex(MainPanel, PANEL_DDSFREPSTEP, ++StepIndex) ;
							GetCtrlVal(MainPanel, PANEL_DDSFREPSTEP, &Step);
							SetCtrlAttribute(MainPanel, PANEL_DDSFREPOUT, ATTR_INCR_VALUE, Step) ;
						};
					};
					if (ActiveControl==PANEL_DDS1 || ActiveControl==PANEL_DDS1STEP) {
						GetCtrlIndex(MainPanel, PANEL_DDS1STEP, &StepIndex);
						if (StepIndex<14){
							SetCtrlIndex(MainPanel, PANEL_DDS1STEP, ++StepIndex) ;
							GetCtrlVal(MainPanel, PANEL_DDS1STEP, &Step);
							SetCtrlAttribute(MainPanel, PANEL_DDS1, ATTR_INCR_VALUE, Step) ;
						};
					};
					if (ActiveControl==PANEL_DDS2 || ActiveControl==PANEL_DDS2STEP) {
						GetCtrlIndex(MainPanel, PANEL_DDS2STEP, &StepIndex);
						if (StepIndex<14){
							SetCtrlIndex(MainPanel, PANEL_DDS2STEP, ++StepIndex) ;
							GetCtrlVal(MainPanel, PANEL_DDS2STEP, &Step);
							SetCtrlAttribute(MainPanel, PANEL_DDS2, ATTR_INCR_VALUE, Step) ;
						};
					};
					if (ActiveControl==PANEL_DDS3|| ActiveControl==PANEL_DDS3STEP) {
						GetCtrlIndex(MainPanel, PANEL_DDS3STEP, &StepIndex);
						if (StepIndex<14){
							SetCtrlIndex(MainPanel, PANEL_DDS3STEP, ++StepIndex) ;
							GetCtrlVal(MainPanel, PANEL_DDS3STEP, &Step);
							SetCtrlAttribute(MainPanel, PANEL_DDS3, ATTR_INCR_VALUE, Step) ;
						};
					};
					if (ActiveControl==PANEL_DDS4|| ActiveControl==PANEL_DDS4STEP) {
						GetCtrlIndex(MainPanel, PANEL_DDS4STEP, &StepIndex);
						if (StepIndex<14){
							SetCtrlIndex(MainPanel, PANEL_DDS4STEP, ++StepIndex) ;
							GetCtrlVal(MainPanel, PANEL_DDS4STEP, &Step);
							SetCtrlAttribute(MainPanel, PANEL_DDS4, ATTR_INCR_VALUE, Step) ;
						};
					};
					break;
				case VAL_LEFT_ARROW_VKEY :
					ActiveControl = GetActiveCtrl(panel);
					if (ActiveControl==PANEL_DDSFREPOUT || ActiveControl==PANEL_DDSFREPSTEP) {
						GetCtrlIndex(MainPanel, PANEL_DDSFREPSTEP, &StepIndex);
						if (StepIndex>0){
							SetCtrlIndex(MainPanel, PANEL_DDSFREPSTEP, --StepIndex) ;
							GetCtrlVal(MainPanel, PANEL_DDSFREPSTEP, &Step);
							SetCtrlAttribute(MainPanel, PANEL_DDSFREPOUT, ATTR_INCR_VALUE, Step) ;
						};
					};
					if (ActiveControl==PANEL_DDS1 || ActiveControl==PANEL_DDS1STEP) {
						GetCtrlIndex(MainPanel, PANEL_DDS1STEP, &StepIndex);
						if (StepIndex>0){
							SetCtrlIndex(MainPanel, PANEL_DDS1STEP, --StepIndex) ;
							GetCtrlVal(MainPanel, PANEL_DDS1STEP, &Step);
							SetCtrlAttribute(MainPanel, PANEL_DDS1, ATTR_INCR_VALUE, Step) ;
						};
					};
					if (ActiveControl==PANEL_DDS2 || ActiveControl==PANEL_DDS2STEP) {
						GetCtrlIndex(MainPanel, PANEL_DDS2STEP, &StepIndex);
						if (StepIndex>0){
							SetCtrlIndex(MainPanel, PANEL_DDS2STEP, --StepIndex) ;
							GetCtrlVal(MainPanel, PANEL_DDS2STEP, &Step);
							SetCtrlAttribute(MainPanel, PANEL_DDS2, ATTR_INCR_VALUE, Step) ;
						};
					};
					if (ActiveControl==PANEL_DDS3 || ActiveControl==PANEL_DDS3STEP) {
						GetCtrlIndex(MainPanel, PANEL_DDS3STEP, &StepIndex);
						if (StepIndex>0){
							SetCtrlIndex(MainPanel, PANEL_DDS3STEP, --StepIndex) ;
							GetCtrlVal(MainPanel, PANEL_DDS3STEP, &Step);
							SetCtrlAttribute(MainPanel, PANEL_DDS3, ATTR_INCR_VALUE, Step) ;
						};
					};
					if (ActiveControl==PANEL_DDS4 || ActiveControl==PANEL_DDS4STEP) {
						GetCtrlIndex(MainPanel, PANEL_DDS4STEP, &StepIndex);
						if (StepIndex>0){
							SetCtrlIndex(MainPanel, PANEL_DDS4STEP, --StepIndex) ;
							GetCtrlVal(MainPanel, PANEL_DDS4STEP, &Step);
							SetCtrlAttribute(MainPanel, PANEL_DDS4, ATTR_INCR_VALUE, Step) ;
						};
					};
					break;
				case VAL_F1_VKEY :
					SetActiveCtrl(MainPanel, PANEL_DDSFREPOUT);
					break;
				case VAL_F2_VKEY :
					SetActiveCtrl(MainPanel, PANEL_DDS1);
					break;
				case VAL_F3_VKEY :
					SetActiveCtrl(MainPanel, PANEL_DDS2);
					break;
				case VAL_F4_VKEY :
					SetActiveCtrl(MainPanel, PANEL_DDS3);
					break;
				case VAL_F5_VKEY :
					SetActiveCtrl(MainPanel, PANEL_DDS4);
					break;
				};
			break;
			
	}
	return 0;
}

void FindCurrentFileName(void)
{
	char * date, day[3], month[3], year[3];	    // DD ; MM ; 20YY plus one space for NULL termination character
	
	date = DateStr();
	Scan(date, "%s>%s[w2]-%s[w2]-20%s[w2]", month, day, year);	   
	Fmt(LogFileName, "%s<%s\\%s%s%s_Frequ.txt", LOGFILEPATH, year, month, day);		 
	return ;
}



int CVICALLBACK CB_OnStart (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	int LogFile ;
	char CharacterBuffer[]="\r" ; 
	char LineBuffer[FXLINELENGTH+10] = "\r\n_1 ";  
	

	switch (event)
		{
		case EVENT_COMMIT:
			if (Acquiring) {
				PlotCh1.IndexPoint = 0 ;
				PlotCh2.IndexPoint = 0 ;
				PlotCh3.IndexPoint = 0 ;
				PlotCh4.IndexPoint = 0 ;
				PlotMath1.IndexPoint = 0 ;
				PlotMath2.IndexPoint = 0 ; 
				PlotMath3.IndexPoint = 0 ; 
				PlotMath4.IndexPoint = 0 ; 
				PlotMath5.IndexPoint = 0 ;
				Allan_Reset(&AllanCh1) ;
				Allan_Reset(&AllanCh2) ;
				Allan_Reset(&AllanCh3) ;
				Allan_Reset(&AllanCh4) ;
				Allan_Reset(&AllanMath1) ;
				Allan_Reset(&AllanMath2) ;
				Allan_Reset(&AllanMath3) ;
				Allan_Reset(&AllanMath4) ;
				Allan_Reset(&AllanMath5) ;
				} ;
			Acquiring = TRUE ;
			SetCtrlAttribute(MainPanel, PANEL_STARTBUTTON, ATTR_LABEL_TEXT, "__RESET"); 
			
			FindCurrentFileName() ; // LogFileName is the one corresponding to the date of today...
			GetFileInfo(LogFileName, &OldLogFilePtr) ;
			OldLogFilePtr -= OldLogFilePtr%FXLINELENGTH + FXLINELENGTH - 2;
			break;
		case EVENT_RIGHT_CLICK:

			break;
		}
	return 0;
}

int CVICALLBACK CB_OnStop (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	switch (event)
		{
		case EVENT_COMMIT:
			Acquiring = FALSE ;
			SetCtrlAttribute(MainPanel, PANEL_STARTBUTTON, ATTR_LABEL_TEXT, "__START");
			break;
		case EVENT_RIGHT_CLICK:

			break;
		}
	return 0;
}

int CVICALLBACK CB_OnTimer (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	int LogFile, FileOpt, FileHg, FileSr, FileExtraMath ;
	long LogFileSize ;
	char CharacterBuffer[]="\r" ;
	char LineBuffer[FXLINELENGTH+10] = "\r\n_1 ";
	
	char * dateNow, * timeNow, day[4], month[4], year[4] ;
	char TimeTag[] = "100103 000000.000"  ;   // K+K time tag meaning here 2010 january the 3rd at 00:00:00.000
	char Date[] = "03/01/2010" ;
	char Time[] = "00:00:00.000" ;
	char Year[] = "2010";
	char ShortYear[] = "10";  // the last 2 digits of calendar year only
	char Month[] = "01";
	char Day[] = "03";
	char Hour[] = "00";
	char Min[] = "00" ;
	char Sec[] = "00.000";
	struct tm LocalTime ;
	time_t utcTime ;
	char * ReportString = "dd.mm.yy\t00:00:00\t3481610838.000\t0000000000000.00000000" ;

	int NotCare = 0 ;
	bool BoxChecked = FALSE; 
	
	double Target, NCalc ;

	double FoxFrequ=0.0;
	int Div = 1 ;  
	
	double FrequencyToChange=0.0;
	double DeltaCh2=0.0;
	double DeltaCh4=0.0;  
	
	double ActualFrequ=0.0;
	double ActualSlope=0.0;
	
	double DDS3Bize ;
	double DDS2Bize ;
	int N2Bize ;
	int Signe2Bize;
	double Math3Bize;
	double Math1Bize;
	
	
	
	switch (event)
		{
		case EVENT_TIMER_TICK:
			switch(Acquiring) 
				{
				case TRUE:
					
					GetFileInfo(LogFileName, &LogFileSize) ;
					
					if (LogFileSize > OldLogFilePtr+2*FXLINELENGTH-2) {  // if a complete newline has been written
						
						SuspendTimerCallbacks() ; 
						
						// Open Log file and get to the beginning of newly completed line
						LogFile = OpenFile(LogFileName, VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII) ;
						OldLogFilePtr += FXLINELENGTH;
						SetFilePtr(LogFile, OldLogFilePtr, 0) ;
					
						// return the last complete string from the log file and scan it for date and time information 
						
						// first, the time tag, and store it in various formats
						ReadFile(LogFile, TimeTag, 17);
						
						CopyBytes(Date,0,TimeTag,4,2);
						CopyBytes(Date,3,TimeTag,2,2);   
						CopyBytes(Date,8,TimeTag,0,2);
						CopyBytes(Time,0,TimeTag,7,2);
						CopyBytes(Time,3,TimeTag,9,2);
						CopyBytes(Time,6,TimeTag,11,6);
						SetCtrlVal(MainPanel, PANEL_DATE, Date) ;
						SetCtrlVal(MainPanel, PANEL_TIME, Time) ; 
						
						CopyBytes(Year,2,TimeTag,0,2); // first 2 bytes of year string remains "20"
						CopyBytes(ShortYear,0,TimeTag,0,2);
						CopyBytes(Month,0,TimeTag,2,2);
						CopyBytes(Day,0,TimeTag,4,2);
						CopyBytes(Hour,0,TimeTag,7,2);
						CopyBytes(Min,0,TimeTag,9,2);
						CopyBytes(Sec,0,TimeTag,11,6);
						Fmt(&LocalTime.tm_year, "%d<%s", Year);
						Fmt(&LocalTime.tm_mon,  "%d<%s", Month);
						Fmt(&LocalTime.tm_mday, "%d<%s", Day);
						Fmt(&LocalTime.tm_hour, "%d<%s", Hour);
						Fmt(&LocalTime.tm_min,  "%d<%s", Min);
						Fmt(&LocalTime.tm_sec,  "%d<%s", "00");    // special case to handle non integer number of UTC seconds
						LocalTime.tm_hour += 0;
						LocalTime.tm_min  -= 0;  
						LocalTime.tm_sec  -= 0;  
						LocalTime.tm_mon  -= 1 ;  	// january is month 0 for tm struct
						LocalTime.tm_year -= 1900 ; // year is number of years since 1900 for tm struct
						LocalTime.tm_isdst = -1;  // daylight saving flag MUST be set to -1 (unallocated is bugging and +1 is making 1 hour error in summer)
						utcTime = mktime (&LocalTime);
						utc = (double) utcTime + strtod(Sec,NULL) ;
						mjd=utc/86400.;		//nb de jours depuis l'origine d'UTC (01/01/1900 à 00h00 GMT)
						mjd+=15020;			//date MJD de la date origine d'UTC
						SetCtrlVal(MainPanel, PANEL_UTC, utc);
						SetCtrlVal(MainPanel, PANEL_MJD, mjd);
				
						// scan the line for counters's channels information
						
						ReadLine(LogFile, LineBuffer, FXLINELENGTH+9) ;
						CloseFile(LogFile);
						
						Scan(LineBuffer, "%f%f%f%f", &Ch1, &Ch2, &Ch3, &Ch4) ;
						Ch1 = 1000*Ch1;
						Ch2 = 1000*Ch2;
						Ch3 = 1000*Ch3;
						Ch4 = 1000*Ch4;
						
						NuOpt=N1*(250000000+Math1)-Signe1*8*FrequDDS1 ;
						
						SetCtrlVal(MainPanel, PANEL_FREQ1, Ch1) ;
						SetCtrlVal(MainPanel, PANEL_FREQ2, Ch2) ;
						SetCtrlVal(MainPanel, PANEL_FREQ3, Ch3) ;  
						SetCtrlVal(MainPanel, PANEL_FREQ4, Ch4) ;
						SetCtrlVal(MainPanel, PANEL_SIGN1, Signe1);
						SetCtrlVal(MainPanel, PANEL_SIGN2, Signe2); 
						SetCtrlVal(MainPanel, PANEL_SIGN3, Signe3); 
						SetCtrlVal(CalcN1Panel, CALCN1_N_1, N_1) ;
						SetCtrlVal(CalcN1Panel, CALCN1_SLOPE, Slope_1) ;
						SetCtrlVal(CalcN2Panel, CALCN2_N_2, N_2) ;
						SetCtrlVal(CalcN2Panel, CALCN2_SLOPE, Beatslope_2) ;
						SetCtrlVal(CalcN3Panel, CALCN3_N_3, N_3) ;
						SetCtrlVal(CalcN3Panel, CALCN3_SLOPE, Slope_3) ;
						SetCtrlVal(MainPanel, PANEL_SLOPETOCANCEL, SlopeMath2);
						/*SetCtrlVal(MainPanel, PANEL_SLOPECANCELONOFF, OnOffMessage); */
						SetCtrlVal(MainPanel, PANEL_CENTERFREQUENCY, CenterFrequencyCh2);  
						SetCtrlVal(MainPanel, PANEL_LED1, SlopeMeasuring); 	   
						SetCtrlVal(MainPanel, PANEL_LED2, OnSlopeCancelling);
						// Treat data
						
						Math1 = mupEval(MathParser1) ;
						SetCtrlVal(MainPanel,PANEL_MATH1, Math1) ;

						Math2 = mupEval(MathParser2) ;
						//Math2=NuOpt;
						SetCtrlVal(MainPanel,PANEL_MATH2, Math2) ;
						
						Math3 = mupEval(MathParser3) ;
						SetCtrlVal(MainPanel,PANEL_MATH3, Math3) ;
						
						Math4 = mupEval(MathParser4) ;
						SetCtrlVal(MainPanel,PANEL_MATH4, Math4) ;
						
						Math5 = mupEval(MathParser5) ;
						SetCtrlVal(MainPanel,PANEL_MATH5, Math5) ;
						
						// Plot Data and calculus if required
						
						GetCtrlVal(MainPanel, PANEL_CHECKBOX_FREQ1PLOT, &BoxChecked) ;
						if (BoxChecked) {
							Plot_AddFrequency(&PlotCh1, Ch1) ;
							}
						GetCtrlVal(MainPanel, PANEL_CHECKBOX_FREQ1ALLAN, &BoxChecked) ;
						if (BoxChecked) {
							Allan_AddFrequency(&AllanCh1, Ch1) ;
							}

						GetCtrlVal(MainPanel, PANEL_CHECKBOX_FREQ2PLOT, &BoxChecked) ;
						if (BoxChecked) {
							Plot_AddFrequency(&PlotCh2, Ch2) ;
							}
						GetCtrlVal(MainPanel, PANEL_CHECKBOX_FREQ2ALLAN, &BoxChecked) ;
						if (BoxChecked) {
							Allan_AddFrequency(&AllanCh2, Ch2) ;
							}
							
						GetCtrlVal(MainPanel, PANEL_CHECKBOX_FREQ3PLOT, &BoxChecked) ;
						if (BoxChecked) {
							Plot_AddFrequency(&PlotCh3, Ch3) ;
							}
						GetCtrlVal(MainPanel, PANEL_CHECKBOX_FREQ3ALLAN, &BoxChecked) ;
						if (BoxChecked) {
							Allan_AddFrequency(&AllanCh3, Ch3) ;
							}
							
						GetCtrlVal(MainPanel, PANEL_CHECKBOX_FREQ4PLOT, &BoxChecked) ;
						if (BoxChecked) {
							Plot_AddFrequency(&PlotCh4, Ch4) ;
							}
						GetCtrlVal(MainPanel, PANEL_CHECKBOX_FREQ4ALLAN, &BoxChecked) ;
						if (BoxChecked) {
							Allan_AddFrequency(&AllanCh4, Ch4) ;
							}
						
						GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH1PLOT, &BoxChecked) ;
						if (BoxChecked) {
							Plot_AddFrequency(&PlotMath1, Math1) ;
							}
						GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH1ALLAN, &BoxChecked) ;
						if (BoxChecked) {
							Allan_AddFrequency(&AllanMath1, Math1) ;
							}
	
						GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH2PLOT, &BoxChecked) ;
						if (BoxChecked) {
							Plot_AddFrequency(&PlotMath2, Math2) ;
							}
						GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH2ALLAN, &BoxChecked) ;
						if (BoxChecked) {
							Allan_AddFrequency(&AllanMath2, Math2) ;
							}
						
						GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH3PLOT, &BoxChecked) ;
						if (BoxChecked) {
							Plot_AddFrequency(&PlotMath3, Math3) ;
							}
						GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH3ALLAN, &BoxChecked) ;
						if (BoxChecked) {
							Allan_AddFrequency(&AllanMath3, Math3) ;
							}
						
						GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH4PLOT, &BoxChecked) ;
						if (BoxChecked) {
							Plot_AddFrequency(&PlotMath4, Math4) ;
							}
						GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH4ALLAN, &BoxChecked) ;
						if (BoxChecked) {
							Allan_AddFrequency(&AllanMath4, Math4) ;
							}
						
						GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH5PLOT, &BoxChecked) ;
						if (BoxChecked) {
							Plot_AddFrequency(&PlotMath5, Math5) ;
							}
						GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH5ALLAN, &BoxChecked) ;
						if (BoxChecked) {
							Allan_AddFrequency(&AllanMath5, Math5) ;
							}
						// Calcul de N
						
						if (Measuring_1==TRUE)
							{
								if (Step1_1==FALSE) {
									SetCtrlVal(MainPanel, PANEL_DDS1, FrequDDS1) ;
									DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, FrequDDS1);
									
									GetCtrlVal(MainPanel, PANEL_DDS2, &FrequencyDDSBesInit) ;
									t1_1=utc;
									Frequ_slope_1=Math1;
									Moy_slope_1= Frequ_slope_1;
									Ch4_slope=Ch4;
									Moy_Ch4slope_1= Ch4_slope;
									N_slope_1=1;
									Step1_1=TRUE ;
									}
								else {
									if(Step2_1==FALSE){
										N_slope_1=N_slope_1+1; 
										Frequ_slope_1=Math1;
										Ch4_slope=Ch4; 
										Moy_slope_1=((N_slope_1-1)*Moy_slope_1 + Frequ_slope_1)/N_slope_1;
										Moy_Ch4slope_1=((N_slope_1-1)*Moy_Ch4slope_1 + Ch4_slope)/N_slope_1; 
										Slope_slope_1 = (Slope_slope_1*(N_slope_1-2) + 6*(Frequ_slope_1-Moy_slope_1)/N_slope_1)/(N_slope_1+1) ;
										Slope_Ch4slope_1 = (Slope_Ch4slope_1*(N_slope_1-2) + 6*(Ch4_slope-Moy_Ch4slope_1)/N_slope_1)/(N_slope_1+1) ;
										if (utc-t1_1>SlopeTime1) {
											Slope_1 = Slope_slope_1;
											Ch4Slope =  Slope_Ch4slope_1;
											Step2_1=TRUE ;
											N_slope_1=0;
											Frequ_slope_1=0.0;
											Moy_slope_1=0.0;
											Slope_slope_1 =0.0;
											Ch4_slope=0.0;
											Moy_Ch4slope_1=0.0;
											Slope_Ch4slope_1=0.0;
											
											DDS4xAD9912_FrequencyRampe (&DDS4xAD9912,1, FrequDDS1,(FrequDDS1+DeltakHz_1*1000), Step1/Ndiv ) ;
											SetCtrlVal(MainPanel, PANEL_DDS1, (FrequDDS1+DeltakHz_1*1000)) ;
											DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, (FrequDDS1+DeltakHz_1*1000)); 
											
											}
										}
									else {
									  if (DDSBesChanged1==FALSE){
										  
										  		if (nDDSChange<3)
												{ nDDSChange=nDDSChange+1;}
												
												else
												{
												nDDSChange=0;
													
												DeltaFrep275=275000-Ch4;
												FrequencyDDSBes=FrequencyDDSBesInit+(DeltaFrep275);
												SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyDDSBes) ;
												DDS4xAD9912_SetFrequency(&DDS4xAD9912,2,FrequencyDDSBes);
												DDSBesChanged1=TRUE;
												t2_1=utc;
							
												}
										  	
									  	}
									   else{
										  
										if(Step3_1==FALSE){
										  if (nstabilization<3)
										  {nstabilization= nstabilization+1;
										  TimetoBegin=TRUE;}
										  else
										  {
											if (utc-t2_1<DeltaT_1) {
													if (TimetoBegin==TRUE) 
														{
															t2_1=utc;
															TimetoBegin=FALSE;
														}
													Frepplus_1=Frepplus_1 +Math1-Slope_1*(utc-t2_1);
													Ch4Plus=Ch4Plus + Ch4 -Ch4Slope*(utc-t2_1);
													n_1=n_1+1;
													}
											else{
												Frepplus_1=Frepplus_1/n_1;
												Ch4Plus=Ch4Plus/n_1; 
												n_1=0;
												Step3_1=TRUE ;
												nstabilization=0;
												DDS4xAD9912_FrequencyRampe ( &DDS4xAD9912,1, (FrequDDS1+DeltakHz_1*1000),(FrequDDS1-DeltakHz_1*1000), Step1/Ndiv ) ;  
												SetCtrlVal(MainPanel, PANEL_DDS1, (FrequDDS1-DeltakHz_1*1000)) ;  
												DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, (FrequDDS1-DeltakHz_1*1000)); 
												}
										  }
										}
										
										else {
											if (DDSBesChanged2==FALSE){
												
												if (nDDSChange<3)
												{ nDDSChange=nDDSChange+1;}
												
												else
												{
												nDDSChange=0;
										
												
												DeltaFrep275=275000-Ch4;
												FrequencyDDSBes=FrequencyDDSBes+(DeltaFrep275) ;
												SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyDDSBes) ;
												DDS4xAD9912_SetFrequency(&DDS4xAD9912,2,FrequencyDDSBes);   
												DDSBesChanged2=TRUE;
												t3_1=utc;

						
												}
											}
											
											else{
											  if (nstabilization<3)
											  { nstabilization=nstabilization+1;
											  TimetoBegin=TRUE;}
											  else
											  {
												if (utc-t3_1<DeltaT_1) {
													if (TimetoBegin==TRUE) 
														{
															t3_1=utc;
															TimetoBegin=FALSE;
														}
													
													Frepminus_1=Frepminus_1 +Math1-Slope_1*(utc-t3_1);
													Ch4Minus=Ch4Minus+Ch4-Ch4Slope*(utc-t3_1);
													n_1=n_1+1;
													}
												else{
													Frepminus_1=Frepminus_1/(n_1);
													Ch4Minus=Ch4Minus/(n_1); 
													N_1 = Signe1*(2*Ndiv*DeltakHz_1*1000 /*-(Ch4Plus-Ch4Minus)-Ch4Slope*(t3_1-t2_1)*/)/(Frepminus_1-Frepplus_1-Slope_1*(t3_1-t2_1));
													n_1=0;
													Frepminus_1=0.0;
													Frepplus_1=0.0;
													DDS4xAD9912_FrequencyRampe (&DDS4xAD9912, 1, FrequDDS1-DeltakHz_1*1000,FrequDDS1, Step1/Ndiv ) ;
													SetCtrlVal(MainPanel, PANEL_DDS1, FrequDDS1) ;
													DDS4xAD9912_SetFrequency(&DDS4xAD9912,1,FrequDDS1);   
													DDS4xAD9912_SetFrequency(&DDS4xAD9912,2,FrequencyDDSBesInit); 
													SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyDDSBesInit) ;
													Measuring_1=FALSE ;
													Step1_1=FALSE ;
													Step2_1=FALSE ;
													Step3_1=FALSE ;
													t1_1=0.0;
													t2_1=0.0;
													t3_1=0.0;
													DDSBesChanged1=FALSE;
													DDSBesChanged2=FALSE;
													FrequencyDDSBes=0.0;
													nstabilization=0;
													
													}
											  }
											}
										}
										}
										}
									}
							}
						
						
						
						
						
						
						if (Measuring_2==TRUE)
							{
								if (Step1_2==FALSE) {
									SetCtrlVal(MainPanel, PANEL_DDS1, FrequDDS1) ;
									DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, FrequDDS1);
									
								    GetCtrlVal(MainPanel, PANEL_DDS2, &FrequencyDDSBesInit) ;
									GetCtrlVal(MainPanel, PANEL_DDS3, &FrequencyDDS3Init) ;
									t1_2=utc;
									Frequ_slope_2=Math1;
									Beat_slope_2=Ch2;
									Moy_slope_2= Frequ_slope_2;
									Moy_Beatslope_2= Beat_slope_2;
									N_slope_2=1;
									Step1_2=TRUE ;
									Nu1=(250000000+Math1)*N1;
									}
								else {
									if(Step2_2==FALSE){
										N_slope_2=N_slope_2+1; 
										Frequ_slope_2=Math1;
										Beat_slope_2=Ch2;  
										Moy_slope_2=((N_slope_2-1)*Moy_slope_2 + Frequ_slope_2)/N_slope_2;
										Moy_Beatslope_2=((N_slope_2-1)*Moy_Beatslope_2 + Beat_slope_2)/N_slope_2;
										Slope_slope_2 = (Slope_slope_2*(N_slope_2-2) + 6*(Frequ_slope_2-Moy_slope_2)/N_slope_2)/(N_slope_2+1) ;
										Slope_Beatslope_2 = (Slope_Beatslope_2*(N_slope_2-2) + 6*(Beat_slope_2-Moy_Beatslope_2)/N_slope_2)/(N_slope_2+1) ;
										if (utc-t1_2>SlopeTime2) {
											Slope_2 = Slope_slope_2;
											Beatslope_2 = Slope_Beatslope_2;
											Step2_2=TRUE ;
											N_slope_2=0;
											Frequ_slope_2=0.0;
											Moy_slope_2=0.0;
											Slope_slope_2 =0.0;
											Moy_Beatslope_2=0.0;
											Slope_Beatslope_2 =0.0;
											Beat_slope_2=0.0;
											
											DDS4xAD9912_FrequencyRampe (&DDS4xAD9912,1, FrequDDS1,(FrequDDS1+DeltakHz_2*1000), Step2/Ndiv ) ;
											SetCtrlVal(MainPanel, PANEL_DDS1, (FrequDDS1+DeltakHz_2*1000)) ;   
											DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, (FrequDDS1+DeltakHz_2*1000));
											Delay(0.1); 
											DeltaDDS3 = -DeltakHz_2*1000*(-Signe1/Signe2)*Ndiv*(Nu2)/(Nu1) - Beatslope_2*(utc-t1_2);
											SetCtrlVal(MainPanel, PANEL_DDS3, (FrequencyDDS3Init+DeltaDDS3)) ; 
											DDS4xAD9912_SetFrequency (&DDS4xAD9912,3,(FrequencyDDS3Init+DeltaDDS3)) ;
											
											}
										}
									else {
									  if (DDSBesChanged1==FALSE){
										  
										  		if (nDDSChange<3)
												{ nDDSChange=nDDSChange+1;}
												
												else
												{
												nDDSChange=0;
													
												DeltaFrep275=275000-Ch4;
												DeltaFrep10=10000-Ch2;
												DeltaDDS3= DeltaDDS3 +(DeltaFrep10);
												FrequencyDDSBes=FrequencyDDSBesInit+(DeltaFrep275);
												FrequencyDDS3=FrequencyDDS3Init+DeltaDDS3;
												
												SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyDDSBes) ;
												DDS4xAD9912_SetFrequency(&DDS4xAD9912,2,FrequencyDDSBes);
												
												Delay(0.1); 
												SetCtrlVal(MainPanel, PANEL_DDS3, FrequencyDDS3) ;    
												DDS4xAD9912_SetFrequency(&DDS4xAD9912,3,FrequencyDDS3);
												 
												DDSBesChanged1=TRUE;
												t2_2=utc;
							
												}
										  	
									  }
									   else{
										  
										if(Step3_2==FALSE){
										  if (nstabilization<3)
										  {nstabilization= nstabilization+1;}
										  else
										  {
											if (utc-t2_2<DeltaT_2) {
													Frepplus_2=Frepplus_2 +Math1+250000000-Slope_2*(utc-t2_2);
													Delta10K_Plus= Delta10K_Plus + 10000 - (Ch2 -Beatslope_2*(utc-t2_2));
													n_2=n_2+1;
													}
											else{
												Frepplus_2=Frepplus_2/n_2;
												Delta10K_Plus=Delta10K_Plus/n_2;
												n_2=0;
												Step3_2=TRUE ;
												nstabilization=0;
												DDS4xAD9912_FrequencyRampe ( &DDS4xAD9912,1, (FrequDDS1+DeltakHz_2*1000),(FrequDDS1-DeltakHz_2*1000), Step2/Ndiv ) ;
												SetCtrlVal(MainPanel, PANEL_DDS1, (FrequDDS1-DeltakHz_2*1000)) ; 
												DDS4xAD9912_SetFrequency (&DDS4xAD9912,1, (FrequDDS1-DeltakHz_2*1000) ) ;
												
												Delay(0.1); 
												DeltaDDS3 = (FrequencyDDS3Init+DeltakHz_2*1000*(-Signe1/Signe2)*Ndiv*(Nu2)/(Nu1)) - FrequencyDDS3;
												SetCtrlVal(MainPanel, PANEL_DDS3, FrequencyDDS3+DeltaDDS3) ; 
												DDS4xAD9912_SetFrequency (&DDS4xAD9912,3, FrequencyDDS3+DeltaDDS3 ) ; 
												
												}
										  }
										}
										
										else {
											if (DDSBesChanged2==FALSE){
												
												if (nDDSChange<3)
												{ nDDSChange=nDDSChange+1;}
												
												else
												{
												nDDSChange=0;
										
												
												DeltaFrep275=275000-Ch4;
												DeltaFrep10=10000-Ch2;
												DeltaDDS3 = DeltaDDS3+(DeltaFrep10) ;
												FrequencyDDSBes=FrequencyDDSBes+(DeltaFrep275) ;
												FrequencyDDS3=FrequencyDDS3+DeltaDDS3 ; 
												DDS4xAD9912_SetFrequency(&DDS4xAD9912,2,FrequencyDDSBes);
												SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyDDSBes);
												
												Delay(0.1); 
												SetCtrlVal(MainPanel, PANEL_DDS3, FrequencyDDS3) ; 
												DDS4xAD9912_SetFrequency(&DDS4xAD9912,3,FrequencyDDS3);
												
												DDSBesChanged2=TRUE;
												t3_2=utc;

						
												}
											}
											
											else{
											  if (nstabilization<3)
											  { nstabilization=nstabilization+1;}
											  else
											  {
												if (utc-t3_2<DeltaT_2) {
													Frepminus_2=Frepminus_2 +Math1+250000000-Slope_2*(utc-t3_2);
													Delta10K_Minus=  Delta10K_Minus +10000 - ( Ch2 -Beatslope_2*(utc-t3_2));
													n_2=n_2+1;
													}
												else{
													Frepminus_2=Frepminus_2/(n_2);
													Delta10K_Minus= Delta10K_Minus/n_2;
													N_2 = (Signe2)*(-DeltaDDS3+Delta10K_Plus-Delta10K_Minus-Beatslope_2*(t3_2-t2_2) )/(Frepminus_2-Frepplus_2-Slope_2*(t3_2-t2_2));
													n_2=0;
													Frepminus_2=0.0;
													Frepplus_2=0.0;
													Delta10K_Minus=0.0;
													Delta10K_Plus=0.0;  
													DDS4xAD9912_FrequencyRampe (&DDS4xAD9912, 1, FrequDDS1-DeltakHz_2*1000,FrequDDS1, Step2/Ndiv ) ;
													SetCtrlVal(MainPanel, PANEL_DDS1, FrequDDS1) ;
													DDS4xAD9912_SetFrequency(&DDS4xAD9912,1,FrequDDS1);
													
													Delay(0.1); 
													
													SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyDDSBesInit) ;  
													DDS4xAD9912_SetFrequency (&DDS4xAD9912, 2, FrequencyDDSBesInit ) ;
													
													Delay(0.1); 
													
													SetCtrlVal(MainPanel, PANEL_DDS3, FrequencyDDS3Init-Beatslope_2*(utc-t1_2)) ;
													DDS4xAD9912_SetFrequency (&DDS4xAD9912, 3, FrequencyDDS3Init-Beatslope_2*(utc-t1_2) ) ;
													
													Measuring_2=FALSE ;
													Step1_2=FALSE ;
													Step2_2=FALSE ;
													Step3_2=FALSE ;
													t1_2=0.0;
													t2_2=0.0;
													t3_2=0.0;
													DDSBesChanged1=FALSE;
													DDSBesChanged2=FALSE;
													FrequencyDDSBes=0.0;
													nstabilization=0;
													
													}
											  }
											}
											}
									  }
										}
									}
							}
						

							
						/*	if (Measuring_3==TRUE)
							{
								if (Step1_3==FALSE) {
									DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, FrequDDS3);
									t1_3=utc;
									Frequ_slope_3=Math1;
									Moy_slope_3= Frequ_slope_3;
									N_slope_3=1;
									Step1_3=TRUE ;
									}
								else {
									if(Step2_3==FALSE){
										N_slope_3=N_slope_3+1; 
										Frequ_slope_3=Math1;
										Moy_slope_3=((N_slope_3-1)*Moy_slope_3 + Frequ_slope_3)/N_slope_3;
										Slope_slope_3 = (Slope_slope_3*(N_slope_3-2) + 6*(Frequ_slope_3-Moy_slope_3)/N_slope_3)/(N_slope_3+1) ;
										if (utc-t1_3>DeltaT_3) {
											Slope_3 = Slope_slope_3;
											GetCtrlVal(MainPanel, PANEL_DDSFREPOUT, &FrequencyDDSBes) ; 
											DDSBes_SetFrequency(&DDSBes, FrequencyDDSBes+(FACTORDDSBES*DeltakHz_3*1000));
											DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, FrequDDS3+DeltakHz_3*1000);
											t2_3=utc;
											Step2_3=TRUE ;
											N_slope_3=0;
											Frequ_slope_3=0.0;
											Moy_slope_3=0.0;
											Slope_slope_3 =0.0;
											}
										}
									else {
										if(Step2_3==FALSE){
											if (utc-t2_3<DeltaT_3) {
													Frepplus_3=Frepplus_3 +Math1+250000000-Slope_3*(utc-t2_3);
													n_3=n_3+1;
													}
											else{
												Frepplus_3=Frepplus_3/n_3;
												DDSBes_SetFrequency(&DDSBes, FrequencyDDSBes-(FACTORDDSBES*DeltakHz_3*1000)); 
												DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, FrequDDS3-DeltakHz_3*1000);
												n_3=0;
												t3_3=utc;
												Step3_3=TRUE ;
												}
											}
										else {
											if (utc-t3_3<DeltaT_3) {
													Frepminus_3=Frepminus_3 +Math1+250000000-Slope_3*(utc-t3_3);
													n_3=n_3+1;
													}
											else{
												Frepminus_3=Frepminus_3/(n_3);
												N_3 = (16*DeltakHz_3*1000 -Slope_3*(t3_3-t2_3))/(Frepplus_3-Frepminus_3);
												n_3=0;
												Frepminus_3=0.0;
												Frepplus_3=0.0;
												DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, FrequDDS3);  
												Measuring_2=FALSE ;
												Step1_3=FALSE ;
												Step2_3=FALSE ;
												Step3_3=FALSE ;
												t1_3=0.0;
												t2_3=0.0;
												t3_3=0.0;
												}
											}
										}
								}
							}		*/
						
					
						
						
						
						// Calcul du signe de fb
						
						if (Getsign1 ==TRUE) {
							if (utc> tbegin1+2){
								if (Math1> Frepbefore1) { Signe1=-1.0;}
								else {Signe1=1.0;}
								SetCtrlVal(MainPanel, PANEL_DDS1, Frequency1) ; 
								DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, Frequency1);
								Getsign1=FALSE;
								}
							}
						if (Getsign2 ==TRUE) {
							if (utc> tbegin2+2){
								if (Math1> Frepbefore2) { 
									if (Ch2>Ch2before) {Signe2=+1.0;}
									else {Signe2=-1.0;} 
								}
								else { 
									if (Ch2>Ch2before) {Signe2=-1.0;}
									else {Signe2=+1.0;}
								}
								SetCtrlVal(MainPanel, PANEL_DDS1, Frequency2) ; 
								DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, Frequency2);
								Getsign2=FALSE;
								}
							}
						if (Getsign3 ==TRUE) {
							if (utc> tbegin3+2){
								if (Ch1> Frepbefore3) { Signe3=-1.0;}
								else {Signe3=1.0;}
								SetCtrlVal(MainPanel, PANEL_DDS3, Frequency3) ; 
								DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, Frequency3);
								Getsign3=FALSE;
								}
							}
								
						
						
						
						/*
						 * Slope Cancelling 
						 */
						
						if (SlopeMeasuring == TRUE)
						{
							double currentFreq;
							
							/* select reference */
							switch (slopeReference) {
								case SLOPE_REFERENCE_MICROWAVE:
									currentFreq = Math2;
									break;
								case SLOPE_REFERENCE_HG_CAVITY:
									currentFreq = Ch2 * 1062.5 / 1542.2;
									break;
							}
							
							if  (utc-SlopeMeasuringTimeBegin > TimetoSlope)
							{
								Nratio = Nratio + 1;
								
								if (Nratio >= 1)
								{
									MoyMath2 = MoyMath2 + Moy_Math2slope;
								}
								SlopeMath2 = SlopeMath2 + Slope_Math2slope;
								N_Math2slope = 0.0;
								Math2_slope = 0.0;
								Moy_Math2slope = 0.0;
								Slope_Math2slope = 0.0;
								
								if (Nratio == 1 && CenterFrequencyCh2ToDetermine == TRUE)
								{
									CenterFrequencyCh2 = MoyMath2;
									CenterFrequencyCh2ToDetermine = FALSE;
								}

								OnOffMessage = "ON " ;
								OnSlopeCancelling = TRUE;
									
								FoxFrequ = ReadDDSFOX (DDSFoxName,DDSFoxip,DDSFoxPort,DDSFoxProfil,DDSFoxClock,DDSFoxDeltaT,DDSFoxFrequency,DDSFoxSweeprate);
								
								if (Nratio == ratio)
								{
									if (FrequCorrec == TRUE)
									{
										SlopeCorrection = (MoyMath2/ratio-CenterFrequencyCh2)/TimetoSlope;
										SlopeMath2 = SlopeMath2 + SlopeCorrection;
									}
									Nratio = 0;
									MoyMath2 = 0.0;
								}
								
								LaunchDDSFOX (DDSFoxName,DDSFoxip,DDSFoxPort,DDSFoxProfil,DDSFoxClock,DDSFoxDeltaT,FoxFrequ,SlopeMath2,0); 	
								
								nstabilisationSlopeMeasuring = 0;
								SlopeMeasuringTimeBegin = utc;
								
							}
							else
							{
								if (nstabilisationSlopeMeasuring < 5)
								{
									nstabilisationSlopeMeasuring = nstabilisationSlopeMeasuring + 1;
									Math2_slope = currentFreq; 
								}
								else
								{
									if ((currentFreq-Math2_slope) < limitotakoff && (currentFreq-Math2_slope) > -limitotakoff)
									{
										N_Math2slope = N_Math2slope + 1;
										Math2_slope = currentFreq;
										Moy_Math2slope = ((N_Math2slope-1)*Moy_Math2slope + Math2_slope)/N_Math2slope;
										Slope_Math2slope = (Slope_Math2slope*(N_Math2slope-2) + 6*(Math2_slope-Moy_Math2slope)/N_Math2slope)/(N_Math2slope+1) ;
									}
									else
									{
										if (AutoStopSlopeCancellingIfDelock)
										{
											
											/* 
											 * stop slope cancelling if the comb is not locked
											 */
											
											if (KeepFrequ == TRUE)
											{
												ActualFrequ = ReadDDSFOX (DDSFoxName,DDSFoxip,DDSFoxPort,DDSFoxProfil,DDSFoxClock,DDSFoxDeltaT,DDSFoxFrequency,DDSFoxSweeprate);
											}
											else
											{
												ActualFrequ = DDSFoxFrequency;
											}  
												
											if (KeepSlope == TRUE)
											{
												ActualSlope = SlopeMath2;
											}
											else
											{
												ActualSlope = DDSFoxSweeprate;
											    OnSlopeCancelling = FALSE;
											} 
												
											LaunchDDSFOX (DDSFoxName,DDSFoxip,DDSFoxPort,DDSFoxProfil,DDSFoxClock,DDSFoxDeltaT,ActualFrequ,ActualSlope,0);
											
											SlopeMeasuring = FALSE;
											OnOffMessage = "OFF " ;
											N_Math2slope = 0.0;
											Math2_slope = 0.0;
											MoyMath2 = 0.0; 
											Moy_Math2slope = 0.0;
											Slope_Math2slope = 0.0;
											CenterFrequencyCh2 = 0.0;
											CenterFrequencyCh2ToDetermine = TRUE;
											Nratio = -1;
											nstabilisationSlopeMeasuring = 0;
											
											SetCtrlVal(MainPanel, PANEL_STARTCANCEL, 0);
										}
									}
								}
							}
						}
						
						
						////   For re-centering
						
						if (KeepCentering) 
						{
							
							DeltaCh4=275000-Ch4;
							DeltaCh2=10000-Ch2;
							
							if (utc- CenteringTimeBegin275K > Timetorecenter275K && CenteringTimeBegin275K>10)
							{
								 GetCtrlVal(MainPanel, PANEL_DDS2, &FrequencyToChange) ;
								 SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyToChange+DeltaCh4) ;
					 		   	 DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, FrequencyToChange+DeltaCh4);
								 CenteringTimeBegin275K=utc;
								
							
							}
							
							if (utc- CenteringTimeBegin10K > Timetorecenter10K && CenteringTimeBegin10K>10)
							{
								  GetCtrlVal(MainPanel, PANEL_DDS3, &FrequencyToChange) ;
								  SetCtrlVal(MainPanel, PANEL_DDS3, FrequencyToChange+DeltaCh2) ; 
					 			  DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, FrequencyToChange+DeltaCh2);
								  CenteringTimeBegin10K=utc; 
								
								
							}
							
							
						}
						
						
						
						
						// Now, some special extra functions for some math value which have special 'default' meaning (like nu_Hg and nu_Sr)
						
						/*GetPanelAttribute(CalcNHgPanel, ATTR_VISIBLE, &BoxChecked) ;	 // correct name for answer would be more
						if (BoxChecked) {												 // something like "&PanelOpened" here
							N_1 = 0;	  // evaluate Math2 for N=0 gives 282143G-/+320M
							GetCtrlVal(CalcNHgPanel, CALCNHG_TARGET, &Target) ;
							NCalc = (Target-mupEval(MathParser2))/(Math1+766e6) ;
							SetCtrlVal(CalcNHgPanel, CALCNHG_NHGCALC,  (double) RoundRealToNearestInteger (NCalc) ) ;
							SetCtrlVal(CalcNHgPanel, CALCNHG_ERROR, NCalc-RoundRealToNearestInteger (NCalc) ) ; 
							GetCtrlVal(MainPanel, PANEL_N_HG, &N_1) ; // Set N_1 back to value user defined in main panel 
							} 
						
						GetPanelAttribute(CalcNSrPanel, ATTR_VISIBLE, &BoxChecked) ;	 // correct name for answer would be more
						if (BoxChecked) {												 // something like "&PanelOpened" here, but I reuse variable name for concision 
							N2 = 0;	  // evaluate Math3 for N=0 gives 429228G-/+DDS2-/+Ch3
							GetCtrlVal(CalcNSrPanel, CALCN2_TARGET, &Target) ;
							NCalc = (Target-mupEval(MathParser3))/(Math1+766e6) ;
							SetCtrlVal(CalcNSrPanel, CALCN2_NSRCALC,  (double) RoundRealToNearestInteger (NCalc) ) ;
							SetCtrlVal(CalcNSrPanel, CALCNSR_ERROR, NCalc-RoundRealToNearestInteger (NCalc) ) ; 
							GetCtrlVal(MainPanel, PANEL_N_SR, &N2) ; // Set N_2 back to value user defined in main panel 
							}	
						
						GetPanelAttribute(CalcN813Panel, ATTR_VISIBLE, &BoxChecked) ;	 // correct name for answer would be more
						if (BoxChecked) {												 // something like "&PanelOpened" here, but I reuse variable name for concision 
							N3 = 0;	  // evaluate Math4 for N=0 gives ??????G-/+DDS_813-/+Ch4
							GetCtrlVal(CalcN813Panel, CALCN83_TARGET, &Target) ;
							NCalc = (Target-mupEval(MathParser4))/(Math1+766e6) ;
							SetCtrlVal(CalcN813Panel, CALCN813_N813CALC,  (double) RoundRealToNearestInteger (NCalc) ) ;
							SetCtrlVal(CalcN813Panel, CALCN813_ERROR, NCalc-RoundRealToNearestInteger (NCalc) ) ; 
							GetCtrlVal(MainPanel, PANEL_N_813, &N3) ; // Set N3 back to value user defined in main panel 
							}*/	
							
						GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH2AUTOSAV, &BoxChecked) ;		   // AutoSave OL
						if (BoxChecked) {
							SetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH2SAVE, TRUE) ;   // so that it will try to write it (at next block) if it seems reasonnable, even though it was off before
							}
						
						GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH2SAVE, &BoxChecked) ;			   // Save OL (Math2)
						if (BoxChecked) {
							/*GetCtrlVal(CalcNHgPanel, CALCNHG_TARGET, &Target) ;
							if (abs(Math2-Target)>1e4) {	   // write it only if it's vaguely plausible (here outlier>10kHz ie approx 10000sigma but not dedrift applied !!!)
								SetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH2SAVE, FALSE) ;
								break ; 
								} ;*/
							FileOpt = OpenFile("z:\\MeasuresFifi1\\OptCavity.txt", VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII) ;
							Fmt(ReportString, "%s\t%s\t%f[p3]\t%f[p3]", Date, Time, utc, Math2);
							WriteLine(FileOpt, ReportString, -1) ;
							CloseFile(FileOpt) ;
							FileOpt = OpenFile("C:\\Femto\\Results\\OptCavity.txt", VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII) ;   // a local backup for debugging
							Fmt(ReportString, "%s\t%s\t%f[p3]\t%f[p3]", Date, Time, utc, Math2);
							WriteLine(FileOpt, ReportString, -1) ;
							CloseFile(FileOpt) ;
							}
						
						GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH3AUTOSAV, &BoxChecked) ;		   // AutoSave Hg (Math3)
						if (BoxChecked) {
							SetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH3SAVE, TRUE) ;   // so that it will try to write it (at next block) if it seems reasonnable, even though it was off before
							}
						
						GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH3SAVE, &BoxChecked) ;			   // Save Hg
						
						if (BoxChecked) {
							/*GetCtrlVal(CalcNSrPanel, CALCN3_TARGET, &Target) ;     
							if (abs(Math3-Target)>1.0e4) {	   // write it only if it's vaguely plausible (here outlier>10kHz ie approx 10000sigma but not dedrift applied !!!)
								SetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH3SAVE, FALSE) ;	// otherwise uncheck the save box
								break ; 
								} ;*/ 
							FileHg = OpenFile("z:\\MeasuresFifi1\\HgCavity.txt", VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII) ;
							Fmt(ReportString, "%s\t%s\t%f[p3]\t%f[p3]", Date, Time, utc, Math3);
							WriteLine(FileHg, ReportString, -1) ;
							CloseFile(FileHg) ;
							FileHg = OpenFile("C:\\Femto\\Results\\HgCavity.txt", VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII) ;
							Fmt(ReportString, "%s\t%s\t%f[p3]\t%f[p3]", Date, Time, utc, Math3);
							WriteLine(FileHg, ReportString, -1) ;
							CloseFile(FileHg) ;
							
							}
						
						GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH5SAVE, &BoxChecked) ;				// Save ExtraMath (Math5)
						if (BoxChecked) {
							FileExtraMath = OpenFile(ExtraMathFileName, VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII) ;
							Fmt(ReportString, "%s\t%s\t%f[p3]\t%f[p7]", Date, Time, utc, Math5);
							WriteLine(FileExtraMath, ReportString, -1) ;
							CloseFile(FileExtraMath) ;
							}
						
						// Special case to handle change of day at next second
						//if ( LocalTime.tm_hour==23 && LocalTime.tm_min==59 && strtod(Sec,NULL)>59 ) {
						if ( LocalTime.tm_hour==23 && LocalTime.tm_min==59 && strtod(Sec,NULL)>=58 ) { 
							printf("Entering change of date substructure\r\n"); 
							printf("old file : %s %s %s \r\n",Date, Time, LogFileName);
							Acquiring = FALSE ;
							do {
								Delay(5.1);
								FindCurrentFileName() ;
								}
							while (!GetFileInfo(LogFileName, &OldLogFilePtr)) ;
							printf("new file : %s \r\n\r\n",Date, Time, LogFileName);  
							Acquiring = TRUE ;
							OldLogFilePtr = 2;
							}
						
						ResumeTimerCallbacks() ; 
						
						} ;
					
					break;
				case FALSE:
					break;
				}
			break;
		}
	return 0;
}




int CVICALLBACK CB_OnFreqPlot (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	int BoxChecked ;
	Plot_Data * pPlot = NULL;
	char PlotTitle[] = "Ch? Frequency Plot" ;
	double PlotMin = 10e6 ;
	double PlotMax = 65e6 ;
	
	switch (event)
		{
		case EVENT_COMMIT:
		
			GetCtrlVal(MainPanel, control, &BoxChecked);
			switch (control) {
				case PANEL_CHECKBOX_FREQ1PLOT :
					pPlot = &PlotCh1 ;
					Fmt(PlotTitle, "Ch1 Frequency Plot") ;
					PlotMin = 54.999e6 ; PlotMax = 55.001e6 ;
					break ;
				case PANEL_CHECKBOX_FREQ2PLOT :
					pPlot = &PlotCh2 ;
					Fmt(PlotTitle, "Ch2 Frequency Plot") ;
					PlotMin = 8.0e3 ; PlotMax = 12.0e3 ;
					break ;
				case PANEL_CHECKBOX_FREQ3PLOT :
					pPlot = &PlotCh3 ;
					Fmt(PlotTitle, "Ch3 Frequency Plot") ;
					PlotMin = 8.0e3 ; PlotMax = 12.0e3 ;
					break ;
				case PANEL_CHECKBOX_FREQ4PLOT :
					pPlot = &PlotCh4 ;
					Fmt(PlotTitle, "Ch4 Frequency Plot") ;
					break ;
				case PANEL_CHECKBOX_MATH1PLOT :
					pPlot = &PlotMath1 ;
					Fmt(PlotTitle, "Math1 Plot") ;
					PlotMin = 765.0e6 ; PlotMax = 775.0e6 ;
					break ;
				case PANEL_CHECKBOX_MATH2PLOT :
					pPlot = &PlotMath2 ;
					Fmt(PlotTitle, "Math2 Plot") ;
					PlotMin = -1.0e9 ; PlotMax = 1.0e9 ;
					break ;	
				case PANEL_CHECKBOX_MATH3PLOT :
					pPlot = &PlotMath3 ;
					Fmt(PlotTitle, "Math3 Plot") ;
					PlotMin = -1.0e9 ; PlotMax = 1.0e9 ;
					break ;	
				case PANEL_CHECKBOX_MATH4PLOT :
					pPlot = &PlotMath4 ;
					Fmt(PlotTitle, "Math4 Plot") ;
					PlotMin = -1.0e9 ; PlotMax = 1.0e9 ;
					break ;	
				case PANEL_CHECKBOX_MATH5PLOT :
					pPlot = &PlotMath5 ;
					Fmt(PlotTitle, "Math5 Plot") ;
					PlotMin = -1.0e9 ; PlotMax = 1.0e9 ;
					break ;
			}
			
			if (BoxChecked) {
				Plot_InitPanel(pPlot, PlotTitle, PlotMin, PlotMax, &OnCloseViewPanel) ;	
				}
			else {
				Plot_ClosePanel(pPlot) ;
				} ;
			break;
			
		case EVENT_RIGHT_CLICK:

			break;
			
		}
	return 0;
}


int CVICALLBACK CB_OnAllanPlot (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	int BoxChecked ;
	Allan_Data * pAllan = NULL;
	char AllanTitle[] = "Ch? Allan Deviation  " ;
	double Normalizer = 300e12 ;
	
	switch (event)
		{
		case EVENT_COMMIT:
			
			GetCtrlVal(MainPanel, control, &BoxChecked);
			switch (control) {
				case PANEL_CHECKBOX_FREQ1ALLAN :
					pAllan = &AllanCh1 ;
					Fmt(AllanTitle, "Ch1 Allan Deviation") ;
					Normalizer = 1.84e12 ;
					break ;
				case PANEL_CHECKBOX_FREQ2ALLAN :
					pAllan = &AllanCh2 ;
					Fmt(AllanTitle, "Ch2 Allan Deviation") ;
					Normalizer = 10.e3 ; 
					break ;
				case PANEL_CHECKBOX_FREQ3ALLAN :
					pAllan = &AllanCh3 ;
					Fmt(AllanTitle, "Ch3 Allan Deviation") ;
					Normalizer = 429.228e12 ; 
					break ;
				case PANEL_CHECKBOX_FREQ4ALLAN :
					pAllan = &AllanCh4 ;
					Fmt(AllanTitle, "Ch4 Allan Deviation") ;
					Normalizer = 275.0e3 ; 
					break ;
				case PANEL_CHECKBOX_MATH1ALLAN :
					pAllan = &AllanMath1 ;
					Fmt(AllanTitle, "Math1 Allan Deviation") ;
					Normalizer = 250.0e6 ;
					break ;
				case PANEL_CHECKBOX_MATH2ALLAN :
					pAllan = &AllanMath2 ;
					Fmt(AllanTitle, "Math2 Allan Deviation") ;
					Normalizer = 194.395e12 ;
					break ;
				case PANEL_CHECKBOX_MATH3ALLAN :
					pAllan = &AllanMath3 ;
					Fmt(AllanTitle, "Math3 Allan Deviation") ;
					Normalizer = 282.143e12 ;
					break ;
				case PANEL_CHECKBOX_MATH4ALLAN :
					pAllan = &AllanMath4 ;
					Fmt(AllanTitle, "Math4 Allan Deviation") ;
					Normalizer = 429.228e12 ;
					break ;
				case PANEL_CHECKBOX_MATH5ALLAN :
					pAllan = &AllanMath5 ;
					Fmt(AllanTitle, "Math5 Allan Deviation") ;
					Normalizer = 429.228e12 ;
					break ;
			}			
			
			if (BoxChecked) {
				Allan_InitPanel(pAllan, AllanTitle, Normalizer, &OnCloseViewPanel) ;	
				}
			else {
				Allan_ClosePanel(pAllan) ;
				} ;
			break;
			
		case EVENT_RIGHT_CLICK:

			break;
			
		}
	return 0;
}

int  CVICALLBACK CB_ChangeDDSOut (int panel, int control, int event, 
		 void *callbackData, int eventData1, int eventData2)
{
	double Frequency ;
	
	switch (event)
		{
		case EVENT_COMMIT:
			GetCtrlVal(MainPanel, control, &Frequency) ;
			if (control==PANEL_DDSFREPOUT) { DDSBes_SetFrequency(&DDSBes, Frequency) ;}
			if (control==PANEL_DDS1)   { DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, Frequency);}
			if (control==PANEL_DDS2)   { DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, Frequency);} 
			if (control==PANEL_DDS3)   { DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, Frequency);}
			if (control==PANEL_DDS4)   { DDS4xAD9912_SetFrequency(&DDS4xAD9912, 4, Frequency);} 
		case EVENT_RIGHT_CLICK:

			break;
			
		}
	return 0;
}

int  CVICALLBACK CB_ChangeDDSStep (int panel, int control, int event, 
		 void *callbackData, int eventData1, int eventData2)
{
	double Step ;
	
	switch (event)
		{
		case EVENT_COMMIT:
			GetCtrlVal(MainPanel, control, &Step) ;
			if (control==PANEL_DDSFREPSTEP) { SetCtrlAttribute(panel, PANEL_DDSFREPOUT, ATTR_INCR_VALUE, Step) ;}
			if (control==PANEL_DDS1STEP)   { SetCtrlAttribute(panel, PANEL_DDS1,   ATTR_INCR_VALUE, Step) ;}   
			if (control==PANEL_DDS2STEP)   { SetCtrlAttribute(panel, PANEL_DDS2,   ATTR_INCR_VALUE, Step) ;}
			if (control==PANEL_DDS3STEP)   { SetCtrlAttribute(panel, PANEL_DDS3,   ATTR_INCR_VALUE, Step) ;}   
			if (control==PANEL_DDS4STEP)   { SetCtrlAttribute(panel, PANEL_DDS4,   ATTR_INCR_VALUE, Step) ;} 
			break;
		case EVENT_RIGHT_CLICK:

			break;
			
		}
	return 0;
}

int  CVICALLBACK CB_ChangeDDSAmpli (int panel, int control, int event, 
		 void *callbackData, int eventData1, int eventData2)
{
	unsigned short int Amplitude ;
	
	switch (event)
		{
		case EVENT_COMMIT:
			GetCtrlVal(MainPanel, control, &Amplitude) ;
			if (control==PANEL_DDSFREPAMP) { DDSBes_SetAmplitude(&DDSBes, Amplitude) ;}
		case EVENT_RIGHT_CLICK:

			break;
			
		}
	return 0;
}

int CVICALLBACK CB_ChangeMath (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	
	char * MathString = "                                                             " ;
						// should be long enough for any reasonnable math expression...
		
	switch (event)
	{
		case EVENT_COMMIT:
				GetCtrlVal(panel, control, MathString) ;
				if (control==PANEL_MATHSTRING1) { mupSetExpr(MathParser1, MathString) ; }
				if (control==PANEL_MATHSTRING2) { mupSetExpr(MathParser2, MathString) ; }
				if (control==PANEL_MATHSTRING3) { mupSetExpr(MathParser3, MathString) ; }
				if (control==PANEL_MATHSTRING4) { mupSetExpr(MathParser4, MathString) ; }
				if (control==PANEL_MATHSTRING5) { mupSetExpr(MathParser5, MathString) ; } 
			break;
	}
	return 0;
}

int CVICALLBACK CB_ChangeN (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{

	switch (event)
		{
		case EVENT_COMMIT:
			if (control==PANEL_N1CHOICE) GetCtrlVal(MainPanel, control, &N1) ;
			if (control==PANEL_N2CHOICE) GetCtrlVal(MainPanel, control, &N2) ;
			if (control==PANEL_N3CHOICE) GetCtrlVal(MainPanel, control, &N3) ;
			break;
		}
	return 0;
}

int CVICALLBACK CB_OnPlus275k (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	double Frequency ;
	
	switch (event)
		{
		case EVENT_COMMIT:
				GetCtrlVal(MainPanel, PANEL_DDSFREPOUT, &Frequency) ; 
				Frequency += 275000.0 ;
				DDSBes_SetFrequency(&DDSBes, Frequency) ;	
				SetCtrlVal(MainPanel, PANEL_DDSFREPOUT, Frequency) ; 
				/*SetCtrlVal(MainPanel, PANEL_MATHSTRING1, FREP_NORMAL_FORMULA) ;
				mupSetExpr(MathParser1, FREP_NORMAL_FORMULA) ;
				SetCtrlVal(MainPanel, PANEL_MATHSTRING2, NUHG_NORMAL_FORMULA) ;
				mupSetExpr(MathParser2, NUHG_NORMAL_FORMULA) ;
				SetCtrlVal(MainPanel, PANEL_MATHSTRING4, NU813_NORMAL_FORMULA) ;
				mupSetExpr(MathParser4, NU813_NORMAL_FORMULA) ;
				*/
			break;
		}
	return 0;
}

int CVICALLBACK CB_OnMinus275k (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	double Frequency ;

	switch (event)
		{
		case EVENT_COMMIT:
			GetCtrlVal(MainPanel, PANEL_DDSFREPOUT, &Frequency) ; 
			Frequency -= 275000.0 ;
			DDSBes_SetFrequency(&DDSBes, Frequency) ;	
			SetCtrlVal(MainPanel, PANEL_DDSFREPOUT, Frequency) ; 
			/*SetCtrlVal(MainPanel, PANEL_MATHSTRING1, "0") ;
			mupSetExpr(MathParser1, "0") ;
			SetCtrlVal(MainPanel, PANEL_MATHSTRING2, "0") ;
			mupSetExpr(MathParser2, "0") ;
			SetCtrlVal(MainPanel, PANEL_MATHSTRING4, "0") ;
			mupSetExpr(MathParser4, "0") ;
			*/
			break;
		}
	return 0;
}

int CVICALLBACK CB_OnPlus10k (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	double Frequency ;
	
	switch (event)
		{
		case EVENT_COMMIT:
			if (control==PANEL_PLUS10KDDS1) {
			GetCtrlVal(MainPanel, PANEL_DDS1, &Frequency) ;
			Frequency += 10000.0 ;
			DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, Frequency);	
			SetCtrlVal(MainPanel, PANEL_DDS1, Frequency) ;
			/*SetCtrlVal(MainPanel, PANEL_MATHSTRING3, NUHG_NORMAL_FORMULA) ;
			mupSetExpr(MathParser3, NUHG_NORMAL_FORMULA) ;
			*/
			}
			if (control==PANEL_PLUS10KDDS2) {
			GetCtrlVal(MainPanel, PANEL_DDS2, &Frequency) ;
			Frequency += 10000.0 ;
			DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, Frequency);	
			SetCtrlVal(MainPanel, PANEL_DDS2, Frequency) ;
			/*SetCtrlVal(MainPanel, PANEL_MATHSTRING3, NUSR_NORMAL_FORMULA) ;
			mupSetExpr(MathParser3, NUSR_NORMAL_FORMULA) ;
			*/
			}
			if (control==PANEL_PLUS10KDDS3) {
			GetCtrlVal(MainPanel, PANEL_DDS3, &Frequency) ;
			Frequency += 10000.0 ;
			DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, Frequency);	
			SetCtrlVal(MainPanel, PANEL_DDS3, Frequency) ;
			}
			if (control==PANEL_PLUS10KDDS4) {
			GetCtrlVal(MainPanel, PANEL_DDS4, &Frequency) ;
			Frequency += 10000.0 ;
			DDS4xAD9912_SetFrequency(&DDS4xAD9912, 4, Frequency);	
			SetCtrlVal(MainPanel, PANEL_DDS4, Frequency) ;
			}
			break;
		}
	return 0;
}

int CVICALLBACK CB_OnMinus10k (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	double Frequency ;
	
	switch (event)
		{
		case EVENT_COMMIT:
			if (control==PANEL_MINUS10KDDS1) {
			GetCtrlVal(MainPanel, PANEL_DDS1, &Frequency) ;
			Frequency -= 10000.0 ;
			DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, Frequency);	
			SetCtrlVal(MainPanel, PANEL_DDS1, Frequency) ;
			/*SetCtrlVal(MainPanel, PANEL_MATHSTRING3, NUHG_NORMAL_FORMULA) ;
			mupSetExpr(MathParser3, NUHG_NORMAL_FORMULA) ;
			*/
			}
			if (control==PANEL_MINUS10KDDS2) {
			GetCtrlVal(MainPanel, PANEL_DDS2, &Frequency) ;
			Frequency -= 10000.0 ;
			DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, Frequency);	
			SetCtrlVal(MainPanel, PANEL_DDS2, Frequency) ;
			/*SetCtrlVal(MainPanel, PANEL_MATHSTRING3, NUSR_NORMAL_FORMULA) ;
			mupSetExpr(MathParser3, NUSR_NORMAL_FORMULA) ;
			*/
			}
			if (control==PANEL_MINUS10KDDS3) {
			GetCtrlVal(MainPanel, PANEL_DDS3, &Frequency) ;
			Frequency -= 10000.0 ;
			DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, Frequency);	
			SetCtrlVal(MainPanel, PANEL_DDS3, Frequency) ;
			}
			if (control==PANEL_MINUS10KDDS4) {
			GetCtrlVal(MainPanel, PANEL_DDS4, &Frequency) ;
			Frequency -= 10000.0 ;
			DDS4xAD9912_SetFrequency(&DDS4xAD9912, 4, Frequency);	
			SetCtrlVal(MainPanel, PANEL_DDS4, Frequency) ;
			}
			break;
		}
	return 0;
}




int CVICALLBACK CB_OnSaveOpt (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	bool BoxChecked = FALSE ;
	
	switch (event)
		{
		case EVENT_COMMIT:
			
			GetCtrlVal(MainPanel, control, &BoxChecked);
			
			if (BoxChecked) {
				SetCtrlAttribute(MainPanel, PANEL_CHECKBOX_MATH2AUTOSAV, ATTR_DIMMED, FALSE) ;
				}
			
			if (!BoxChecked) {
				SetCtrlAttribute(MainPanel, PANEL_CHECKBOX_MATH2AUTOSAV, ATTR_DIMMED, TRUE) ;
				}
			
			break;
		}
	return 0;
}



 
int CVICALLBACK CB_OnSaveHg (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	bool BoxChecked = FALSE ;
	
	switch (event)
		{
		case EVENT_COMMIT:
			
			GetCtrlVal(MainPanel, control, &BoxChecked);
			
			if (BoxChecked) {
				SetCtrlAttribute(MainPanel, PANEL_CHECKBOX_MATH3AUTOSAV, ATTR_DIMMED, FALSE) ;
				}
			
			if (!BoxChecked) {
				SetCtrlAttribute(MainPanel, PANEL_CHECKBOX_MATH3AUTOSAV, ATTR_DIMMED, TRUE) ;
				}
			
			break;
		}
	return 0;
}

int CVICALLBACK CB_OnAutoSaveHg (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	bool BoxChecked = FALSE ;
	
	switch (event)
		{
		case EVENT_COMMIT:
			
			GetCtrlVal(MainPanel, control, &BoxChecked);
			
			if (BoxChecked) {
				SetCtrlAttribute(MainPanel, PANEL_CHECKBOX_MATH2SAVE, ATTR_DIMMED, TRUE) ;
				}
			
			if (!BoxChecked) {
				SetCtrlAttribute(MainPanel, PANEL_CHECKBOX_MATH2SAVE, ATTR_DIMMED, FALSE) ;
				}

			break;
		}
	return 0;
}

int CVICALLBACK CB_OnSaveSr (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	bool BoxChecked = FALSE ;
	
	switch (event)
		{
		case EVENT_COMMIT:
			
			GetCtrlVal(MainPanel, control, &BoxChecked);
			
			if (BoxChecked) {
				SetCtrlAttribute(MainPanel, PANEL_CHECKBOX_MATH3AUTOSAV, ATTR_DIMMED, FALSE) ;
				}
			
			if (!BoxChecked) {
				SetCtrlAttribute(MainPanel, PANEL_CHECKBOX_MATH3AUTOSAV, ATTR_DIMMED, TRUE) ;        
				}
			break;
		}
	return 0;
}

int CVICALLBACK CB_OnAutoSaveSr (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	bool BoxChecked = FALSE ; 
	
	switch (event)
		{
		case EVENT_COMMIT:

			GetCtrlVal(MainPanel, control, &BoxChecked);
			
			if (BoxChecked) {
				SetCtrlAttribute(MainPanel, PANEL_CHECKBOX_MATH3SAVE, ATTR_DIMMED, TRUE) ;
				}
			
			if (!BoxChecked) {
				SetCtrlAttribute(MainPanel, PANEL_CHECKBOX_MATH3SAVE, ATTR_DIMMED, FALSE) ;
				}
			
			break;
		}
	return 0;
}


int CVICALLBACK OnAcceptN (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	double N;
	
	switch (event)
		{
		case EVENT_COMMIT:
			if (panel == CalcN1Panel) {
				N = floor(N_1);
					if (N_1-N<0.5)
					{
						N1=N;
					}
					else
					{
						N1=N+1;
					}
				SetCtrlVal(MainPanel, PANEL_N1CHOICE, N1) ;
				} 
			
			if (panel == CalcN2Panel) {
				N = floor(N_2);
					if (N_2-N<0.5)
					{
						N2=N;
					}
					else
					{
						N2=N+1;
					}
				SetCtrlVal(MainPanel, PANEL_N2CHOICE, N2) ;
				} 

			if (panel == CalcN3Panel) {
				GetCtrlVal(CalcN3Panel, CALCN3_N_3, &N) ;
				SetCtrlVal(MainPanel, PANEL_N3CHOICE, N) ;
				N3=N;
				} 
			break;
		}
	return 0;
}

/*int CVICALLBACK OnFSRStep (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	double Target, FSR ;
	
	switch (event)
		{
		case EVENT_COMMIT:
			if (panel==CalcNHgPanel) {
				if (control==CALCNHG_MINUSFSR) {
					GetCtrlVal(CalcNHgPanel, CALCNHG_FSR, &FSR) ;
					GetCtrlVal(CalcNHgPanel, CALCNHG_TARGET, &Target) ;
					Target -= FSR ;
					SetCtrlVal(CalcNHgPanel, CALCNHG_TARGET, Target) ;
					} 
				if (control==CALCNHG_PLUSFSR) {
					GetCtrlVal(CalcNHgPanel, CALCNHG_FSR, &FSR) ;
					GetCtrlVal(CalcNHgPanel, CALCNHG_TARGET, &Target) ;
					Target += FSR ;
					SetCtrlVal(CalcNHgPanel, CALCNHG_TARGET, Target) ;
					}
				}   
			if (panel==CalcNSrPanel) {
				if (control==CALCNSR_MINUSFSR) {
					GetCtrlVal(CalcNSrPanel, CALCNSR_FSR, &FSR) ;
					GetCtrlVal(CalcNSrPanel, CALCNSR_TARGET, &Target) ;
					Target -= FSR ;
					SetCtrlVal(CalcNSrPanel, CALCNSR_TARGET, Target) ;
					}
				if (control==CALCNSR_PLUSFSR) {
					GetCtrlVal(CalcNSrPanel, CALCNSR_FSR, &FSR) ;
					GetCtrlVal(CalcNSrPanel, CALCNSR_TARGET, &Target) ;
					Target += FSR ;
					SetCtrlVal(CalcNSrPanel, CALCNSR_TARGET, Target) ;
					}
				}
			break;
		}
	return 0;
} */ 

 /*int CVICALLBACK OnExtrapolateTarget (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	bool BoxChecked = FALSE ;  
	double extrapolFreq = 745967614;
	time_t utcTime ; 
	double utc ;
	
	switch (event)
		{
		case EVENT_COMMIT:
			GetCtrlVal(CalcNHgPanel, CALCNHG_EXTRAPOLATE, &BoxChecked) ;
			if (BoxChecked) {
				SetCtrlAttribute(CalcNHgPanel, CALCNHG_TARGET,     ATTR_DIMMED, TRUE) ;  
				SetCtrlAttribute(CalcNHgPanel, CALCNHG_OFFSETFREQ, ATTR_DIMMED, TRUE) ;
				SetCtrlAttribute(CalcNHgPanel, CALCNHG_PLUSFSR,    ATTR_DIMMED, TRUE) ;
				SetCtrlAttribute(CalcNHgPanel, CALCNHG_MINUSFSR,   ATTR_DIMMED, TRUE) ; 
				utc = time(&utcTime) ;
				//extrapolFreq = 746127081 + 0.032*(utc-3491304000) ; // linear data extrapolation with slope from 18/08/2010-12/08/2010 
				extrapolFreq = 746108328.707542 + 3.144737e-002 * (utc-3490779388.183) - 9.527612e-010 * (utc-3490779388.183) ;  // quad extrapol from fit of data in HgCavity.txt 5/10/2010
				
				
				SetCtrlVal(CalcNHgPanel, CALCNHG_TARGET, extrapolFreq) ;
				}
			else {
				SetCtrlAttribute(CalcNHgPanel, CALCNHG_TARGET,     ATTR_DIMMED, FALSE) ;
				SetCtrlAttribute(CalcNHgPanel, CALCNHG_OFFSETFREQ, ATTR_DIMMED, FALSE) ;
				SetCtrlAttribute(CalcNHgPanel, CALCNHG_PLUSFSR,    ATTR_DIMMED, FALSE) ; 
				SetCtrlAttribute(CalcNHgPanel, CALCNHG_MINUSFSR,   ATTR_DIMMED, FALSE) ; 
				}
			break;
		}
	return 0;
}  */


int CVICALLBACK OnChooseSaveFile (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	switch (event)
		{
		case EVENT_COMMIT:

			FileSelectPopup("D:\\Manip Femto", "*.txt", "*.txt", "Selection of the ExtraMath saving file", VAL_SAVE_BUTTON, 0, 0, 0, FALSE, ExtraMathFileName) ;
			SetCtrlAttribute(MainPanel, PANEL_CHECKBOX_MATH5SAVE, ATTR_DIMMED, FALSE) ;
			
			break;
		}
	return 0;
}


// Changements pour Calcul de N



int CVICALLBACK CB_OnNCalculus (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{

	int PanelIsVisible, PanelTop, PanelLeft, PanelWidth ;
	
	switch (event)
		{
		case EVENT_COMMIT:
			
			if (control==PANEL_N1CALCULUS){
				GetPanelAttribute (CalcN1Panel, ATTR_VISIBLE , &PanelIsVisible);
				if (PanelIsVisible) {
				}
				else {
					GetPanelAttribute (MainPanel, ATTR_TOP,   &PanelTop) ;
					GetPanelAttribute (MainPanel, ATTR_LEFT,  &PanelLeft) ;
					GetPanelAttribute (MainPanel, ATTR_WIDTH, &PanelWidth) ;
					SetPanelAttribute (CalcN1Panel, ATTR_TOP, PanelTop+170 ) ;
					SetPanelAttribute (CalcN1Panel, ATTR_LEFT, PanelLeft+PanelWidth+4 ) ;
					DisplayPanel(CalcN1Panel) ;
				} 
				}
			if (control==PANEL_N2CALCULUS){
				GetPanelAttribute (CalcN2Panel, ATTR_VISIBLE , &PanelIsVisible);
				if (PanelIsVisible) {
				}
				else {
					GetPanelAttribute (MainPanel, ATTR_TOP,   &PanelTop) ;
					GetPanelAttribute (MainPanel, ATTR_LEFT,  &PanelLeft) ;
					GetPanelAttribute (MainPanel, ATTR_WIDTH, &PanelWidth) ;
					SetPanelAttribute (CalcN2Panel, ATTR_TOP, PanelTop+310 ) ;
					SetPanelAttribute (CalcN2Panel, ATTR_LEFT, PanelLeft+PanelWidth+4 ) ;
					DisplayPanel(CalcN2Panel) ;
				} 
				}
			if (control==PANEL_N3CALCULUS){
				GetPanelAttribute (CalcN3Panel, ATTR_VISIBLE , &PanelIsVisible);
				if (PanelIsVisible) {
				}
				else {
					GetPanelAttribute (MainPanel, ATTR_TOP,   &PanelTop) ;
					GetPanelAttribute (MainPanel, ATTR_LEFT,  &PanelLeft) ;
					GetPanelAttribute (MainPanel, ATTR_WIDTH, &PanelWidth) ;
					SetPanelAttribute (CalcN3Panel, ATTR_TOP, PanelTop+480 ) ;
					SetPanelAttribute (CalcN3Panel, ATTR_LEFT, PanelLeft+PanelWidth+4 ) ;
					DisplayPanel(CalcN3Panel) ;
				} 
				}
			break;
		}
	
	return 0;
}


int CVICALLBACK CB_OnStartNCalculus (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	
	switch (event)
		{
		case EVENT_COMMIT:
			if (control==CALCN1_COMMANDBUTTON)
			{
					Measuring_1=TRUE;
			}
			if (control==CALCN2_COMMANDBUTTON) 
			{
					Measuring_2=TRUE;
			}
		break;
		}
			 

	
	return 0;
}

int CVICALLBACK CB_OnStartN3Calculus (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	
	switch (event)
	{
		case EVENT_COMMIT:
			Measuring_3=TRUE;
			break;
	}
	return 0;
}


int CVICALLBACK CB_OnNStop (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	int PanelIsVisible;
	
	switch (event)
		{
		case EVENT_COMMIT:
			//switch (control)
			if (control==CALCN1_COMMANDBUTTON_2)
			{
			GetPanelAttribute (CalcN1Panel, ATTR_VISIBLE , &PanelIsVisible);
					if (PanelIsVisible) {
						HidePanel(CalcN1Panel) ;
						}
			
						Measuring_1=FALSE;
						Step1_1=FALSE ;
						Step2_1=FALSE ;
						Step3_1=FALSE ;
						Frepminus_1=0.0;
						Frepplus_1=0.0;
			  		   t1_1=0.0;
			 		   t2_1=0.0;
			 		   t3_1=0.0; 
						N_slope_1=0;
						Frequ_slope_1=0.0;
						Moy_slope_1=0.0;
						Slope_slope_1 =0.0;
						Ch4_slope=0.0;
						Moy_Ch4slope_1=0.0;
						Slope_Ch4slope_1=0.0;
						DDSBesChanged1=FALSE;
						DDSBesChanged2=FALSE; 
						nDDSChange=0.0;
						
						SetCtrlVal(MainPanel, PANEL_DDS1, FrequDDS1) ;
						DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, FrequDDS1);
						SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyDDSBesInit) ;
						DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, FrequencyDDSBesInit); 
						nstabilization=0;	
			}
			
			
			if (control==CALCN2_STOP2)
			{  
			GetPanelAttribute (CalcN2Panel, ATTR_VISIBLE , &PanelIsVisible);
					if (PanelIsVisible) {
						HidePanel(CalcN2Panel) ;
						}
			
						Measuring_2=FALSE;
						Step1_2=FALSE ;
						Step2_2=FALSE ;
						Step3_2=FALSE ;
						Frepminus_2=0.0;
						Delta10K_Minus=0.0;
						Frepplus_2=0.0;
						Delta10K_Plus=0.0; 
						DeltaDDS3=0.0; 
			 		    t1_2=0.0;
			  	  	    t2_2=0.0;
			  		    t3_2=0.0; 
						N_slope_2=0;
						Frequ_slope_2=0.0;
						Moy_slope_2=0.0;
						Beat_slope_2=0.0;
						Moy_Beatslope_2=0.0;
						Slope_Beatslope_2 =0.0;
						Slope_slope_2 =0.0;
						DDSBesChanged1=FALSE;
						DDSBesChanged2=FALSE; 
						nDDSChange=0.0;
						SetCtrlVal(MainPanel, PANEL_DDS1, FrequDDS1) ;
						DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, FrequDDS1);
						SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyDDSBesInit) ;
						DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, FrequencyDDSBesInit);
						SetCtrlVal(MainPanel, PANEL_DDS3, FrequencyDDS3Init) ;
						DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, FrequencyDDS3Init); 
						nstabilization=0;
			}
				
			
		break;
		}
	return 0;
}

int CVICALLBACK CB_OnN3Stop (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	int PanelIsVisible;
	
	switch (event)
		{
		case EVENT_COMMIT:
					GetPanelAttribute (CalcN3Panel, ATTR_VISIBLE , &PanelIsVisible);
					if (PanelIsVisible) {										  
						HidePanel(CalcN3Panel) ;
						}
			
						Measuring_3=FALSE;
						Step1_3=FALSE ;
						Step2_3=FALSE ;
						Step3_3=FALSE ;
						Frepminus_3=0.0;
						Frepplus_3=0.0;
			   		 t1_3=0.0;
			  		  t2_3=0.0;
			   		 t3_3=0.0; 
						N_slope_3=0;
						Frequ_slope_3=0.0;
						Moy_slope_3=0.0;
						Slope_slope_3 =0.0;
			break;
		}
	return 0;
}


int  CVICALLBACK CB_OnIntegrationTime (int panel, int control, int event, 
		 void *callbackData, int eventData1, int eventData2)
{
	double Time ;
	
	switch (event)
		{
		case EVENT_COMMIT:
			if (control==CALCN1_INTEGRATIONTIME){
				GetCtrlVal(CalcN1Panel, CALCN1_INTEGRATIONTIME, &Time) ;
				DeltaT_1=Time;
			}
			if (control==CALCN2_INTEGRATIONTIME){
				GetCtrlVal(CalcN2Panel, CALCN2_INTEGRATIONTIME, &Time) ;
				DeltaT_2=Time;
			}
			if (control==CALCN3_INTEGRATIONTIME){
				GetCtrlVal(CalcN3Panel, CALCN3_INTEGRATIONTIME, &Time) ;
				DeltaT_3=Time;
			}

			break;
			
		}
	return 0;
}


int  CVICALLBACK CB_OnDeltaFreq (int panel, int control, int event, 
		 void *callbackData, int eventData1, int eventData2)
{
	double Frequ ;
	
	switch (event)
		{
		case EVENT_COMMIT:
			if (control==CALCN1_DELTAFREQ){
				GetCtrlVal(CalcN1Panel, CALCN1_DELTAFREQ, &Frequ) ;
				DeltakHz_1=Frequ;
			}
			if (control==CALCN2_DELTAFREQ){
				GetCtrlVal(CalcN2Panel, CALCN2_DELTAFREQ, &Frequ) ;
				DeltakHz_2=Frequ;
			}
			if (control==CALCN3_DELTAFREQ){
				GetCtrlVal(CalcN3Panel, CALCN3_DELTAFREQ, &Frequ) ;
				DeltakHz_3=Frequ;
			}

			break;
			
		}
	return 0;
}


int  CVICALLBACK CB_OnFindSign (int panel, int control, int event, 
		 void *callbackData, int eventData1, int eventData2)
{
	
	switch (event)
		{
		case EVENT_COMMIT:
			if (control==PANEL_FINDSIGN1){
				tbegin1=utc;
				Frepbefore1=Math1;
				GetCtrlVal(MainPanel, PANEL_DDS1, &Frequency1) ;
				SetCtrlVal(MainPanel, PANEL_DDS1, Frequency1+Frequencystep1) ;  
				DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, Frequency1+Frequencystep1);
				Getsign1=TRUE;
			}
			if (control==PANEL_FINDSIGN2){
				tbegin2=utc;
				Frepbefore2=Math1;
				Ch2before=Ch2;
				GetCtrlVal(MainPanel, PANEL_DDS1, &Frequency2) ;
				SetCtrlVal(MainPanel, PANEL_DDS1, Frequency2+Frequencystep2) ;
				DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, Frequency2+Frequencystep2);
				Getsign2=TRUE;
			}
			if (control==PANEL_FINDSIGN3){
				tbegin3=utc;
				Frepbefore3=Ch1;
				GetCtrlVal(MainPanel, PANEL_DDS3, &Frequency3) ;
				SetCtrlVal(MainPanel, PANEL_DDS3, Frequency3+Frequencystep3) ; 
				DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, Frequency3+Frequencystep3);
				Getsign3=TRUE;
			}


			break;
			
		}
	return 0;
} 



int  CVICALLBACK CB_OnFind275K (int panel, int control, int event, 
		 void *callbackData, int eventData1, int eventData2)
 {
	 
	 
	double FrequencyToChange,DeltaCh4,DeltaCh2;
	
	DeltaCh4=275000-Ch4;
	DeltaCh2=10000-Ch2;
	
	switch (event)
		{
		case EVENT_COMMIT:
			 if (control==PANEL_FIND275K2){
					 GetCtrlVal(MainPanel, PANEL_DDS2, &FrequencyToChange) ;
					 SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyToChange+DeltaCh4) ;  
					 DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, FrequencyToChange+DeltaCh4);
					 
			 }
			 
			 if (control==PANEL_FIND10K3){
					 GetCtrlVal(MainPanel, PANEL_DDS3, &FrequencyToChange) ;
					 SetCtrlVal(MainPanel, PANEL_DDS3, FrequencyToChange+DeltaCh2) ; 
					 DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, FrequencyToChange+DeltaCh2);
					 
			 }
			
		break;
			
		}
	return 0;
} 


int  CVICALLBACK CB_OnSlopeTime (int panel, int control, int event, 
		 void *callbackData, int eventData1, int eventData2)
{
	double Time ;
	
	switch (event)
		{
		case EVENT_COMMIT:
			if (control==CALCN1_SLOPETIME){
				GetCtrlVal(CalcN1Panel, CALCN1_SLOPETIME, &Time) ;
				SlopeTime1=Time;
			}

			if (control==CALCN2_SLOPETIME){
				GetCtrlVal(CalcN2Panel, CALCN2_SLOPETIME, &Time) ;
				SlopeTime2=Time;
			}

			break;
			
		}
	return 0;
}


 int  CVICALLBACK CB_OnChangeNdiv (int panel, int control, int event, 
		 void *callbackData, int eventData1, int eventData2)
{
	double NewN ;
	
	switch (event)
		{
		case EVENT_COMMIT:
			   if (control==PANEL_CHANGENDIV){ 
				GetCtrlVal(MainPanel, PANEL_CHANGENDIV, &NewN) ;
				Ndiv=NewN;
				FrequDDS1=880000000.0/Ndiv;
				DeltakHz_1=1800.0*8/Ndiv;
				DeltakHz_2=1500.0*8/Ndiv;
				SetCtrlVal(MainPanel, PANEL_DDS1, FrequDDS1) ;
				DDS4xAD9912_SetFrequency(&DDS4xAD9912,1,FrequDDS1);
				SetCtrlVal(CalcN1Panel, CALCN1_DELTAFREQ, DeltakHz_1) ;
				SetCtrlVal(CalcN2Panel, CALCN2_DELTAFREQ, DeltakHz_2) ; 
			   }

			break;
			
		}
	return 0;
}


 int  CVICALLBACK CB_OnStartSlopeCancelling (int panel, int control, int event, 
		 void *callbackData, int eventData1, int eventData2)
{

	double   ActualFrequ=0.0;
	double   ActualSlope=0.0; 
	int buttonstatu=0;
	
	switch (event)
		{
		case EVENT_COMMIT:
			   if (control==PANEL_STARTCANCEL){ 
				   
				   GetCtrlVal(MainPanel, PANEL_STARTCANCEL, &buttonstatu) ; 
				   
				    if (buttonstatu==1)
					{
						SlopeMeasuringTimeBegin=utc; 
						SlopeMeasuring=TRUE;
						ActualFrequ=ReadDDSFOX (DDSFoxName,DDSFoxip,DDSFoxPort,DDSFoxProfil,DDSFoxClock,DDSFoxDeltaT,DDSFoxFrequency,DDSFoxSweeprate);
						ActualSlope= SlopeMath2;
						LaunchDDSFOX (DDSFoxName,DDSFoxip,DDSFoxPort,DDSFoxProfil,DDSFoxClock,DDSFoxDeltaT,ActualFrequ,ActualSlope,0);
						Nratio=-1;
					 	CenterFrequencyCh2ToDetermine=TRUE ;
						CenterFrequencyCh2=0.0; 

					}
					else
					{
						if (KeepFrequ==TRUE) {ActualFrequ=ReadDDSFOX (DDSFoxName,DDSFoxip,DDSFoxPort,DDSFoxProfil,DDSFoxClock,DDSFoxDeltaT,DDSFoxFrequency,DDSFoxSweeprate);}
						else {  ActualFrequ= DDSFoxFrequency;}
						if (KeepSlope==TRUE) {ActualSlope= SlopeMath2;
											  OnSlopeCancelling =TRUE; }
						else {  ActualSlope= DDSFoxSweeprate;
								OnSlopeCancelling =FALSE;     }
						 LaunchDDSFOX (DDSFoxName,DDSFoxip,DDSFoxPort,DDSFoxProfil,DDSFoxClock,DDSFoxDeltaT,ActualFrequ,ActualSlope,0);
						 SlopeMeasuring=FALSE; 
						 OnOffMessage="OFF " ;
						 N_Math2slope=0.0;
						 Math2_slope=0.0;
						 Moy_Math2slope=0.0;
						 Slope_Math2slope=0.0;
						 MoyMath2=0.0; 
						 Slope_Math2slope=0.0;
						 CenterFrequencyCh2= 0.0;
						 Nratio=-1;
						 nstabilisationSlopeMeasuring=0;
					}
			   }

			break;
			
		}
	return 0;
}

 
  int  CVICALLBACK CB_OnResetSlope (int panel, int control, int event, 
		 void *callbackData, int eventData1, int eventData2)
{

	switch (event)
		{
		case EVENT_COMMIT:
			   if (control==PANEL_RESETSLOPE){ 
				   
				     SlopeMath2=0.0; 
					 CenterFrequencyCh2=0.0;
					 MoyMath2=0.0;
					 Nratio=-1;
					 CenterFrequencyCh2ToDetermine=TRUE ;
					 OnSlopeCancelling =FALSE; 
					 LaunchDDSFOX (DDSFoxName,DDSFoxip,DDSFoxPort,DDSFoxProfil,DDSFoxClock,DDSFoxDeltaT,DDSFoxFrequency,DDSFoxSweeprate,0);
			   }

			break;
			
		}
	return 0;
}
  
  
    int  CVICALLBACK CB_ChangeSlopeTime (int panel, int control, int event, 
		 void *callbackData, int eventData1, int eventData2)
{

	switch (event)
		{
		case EVENT_COMMIT:
			   if (control==PANEL_SLOPETIME){ 
				   
				     GetCtrlVal(MainPanel, PANEL_SLOPETIME, &TimetoSlope); 
			   }

			break;
			
		}
	return 0;
}


  int LaunchDDSFOX (char *Name,char *ip,int Port,unsigned int Profil,double Clock,double DeltaT,double Frequency,double Sweeprate,int mode)
{
			  //mode =0 pour normal
			  //mode =1 pour reset 
	DDSParameter DDSState;
			strcpy(DDSState.Name ,Name) ; 
			DDSState.Profil = Profil ;
			strcpy(DDSState.ip,ip) ; 
			DDSState.Port = Port;
			DDSState.Clock = Clock;
			DDSState.Delta_T = DeltaT;
			DDSState.Frequency = Frequency; 
			DDSState.SweepRate = Sweeprate ; 
			if (mode ==  1) {  DDS_Initialize (&DDSState) ; }
			else {	DDS_ReInitialize (&DDSState) ; }
	
	return 0;
}
  
    double ReadDDSFOX (char *Name,char *ip,int Port,unsigned int Profil,double Clock,double DeltaT,double Frequency,double Sweeprate)
{

	DDSParameter DDSState;
	double FrequDDSFox;
	
			strcpy(DDSState.Name ,Name) ;
			DDSState.Profil = Profil ;
			strcpy(DDSState.ip,ip) ; 
			DDSState.Port = Port;
			DDSState.Clock = Clock;
			DDSState.Delta_T = DeltaT;
			DDSState.Frequency = Frequency; 
			DDSState.SweepRate = Sweeprate ; 

			FrequDDSFox=DDSFox_ReadFreq(&DDSState);

	
	return FrequDDSFox;
}


int  CVICALLBACK CB_OnCROX (int panel, int control, int event, 
		 void *callbackData, int eventData1, int eventData2)
{
		bool BoxChecked = FALSE; 
		double ActualFrequ=0.0;

	switch (event)
		{
		case EVENT_COMMIT:
			
			if (control== PANEL_CHECKBOX_CORRFREQU)  
			{
				   	GetCtrlVal(MainPanel, PANEL_CHECKBOX_CORRFREQU, &BoxChecked) ;
						if (BoxChecked) {
							FrequCorrec=TRUE ;
							}
						else {FrequCorrec=FALSE ;}
							
			}
			
			if (control== PANEL_CHECKBOX_KEEP)  
			{
				   	GetCtrlVal(MainPanel, PANEL_CHECKBOX_KEEP, &BoxChecked) ;
						if (BoxChecked) {
							KeepFrequ=TRUE ;
							}
						else {KeepFrequ=FALSE ;}
							
			}
			
			
			if (control== PANEL_CHECKBOX_KEEPSLOPE)  
			{
				   	GetCtrlVal(MainPanel, PANEL_CHECKBOX_KEEPSLOPE, &BoxChecked) ;
						if (BoxChecked) {
							KeepSlope=TRUE ;
							if (SlopeMeasuring==FALSE && OnSlopeCancelling==FALSE)
							  {
								    ActualFrequ=ReadDDSFOX (DDSFoxName,DDSFoxip,DDSFoxPort,DDSFoxProfil,DDSFoxClock,DDSFoxDeltaT,DDSFoxFrequency,DDSFoxSweeprate);
									LaunchDDSFOX (DDSFoxName,DDSFoxip,DDSFoxPort,DDSFoxProfil,DDSFoxClock,DDSFoxDeltaT,ActualFrequ,SlopeMath2,0);
							 		OnSlopeCancelling=TRUE;
								  
								  
							  }
							}
						else {KeepSlope=FALSE ;
							  if (SlopeMeasuring==FALSE && OnSlopeCancelling==TRUE)
							  {
								  	  ActualFrequ=ReadDDSFOX (DDSFoxName,DDSFoxip,DDSFoxPort,DDSFoxProfil,DDSFoxClock,DDSFoxDeltaT,DDSFoxFrequency,DDSFoxSweeprate);
									  LaunchDDSFOX (DDSFoxName,DDSFoxip,DDSFoxPort,DDSFoxProfil,DDSFoxClock,DDSFoxDeltaT,ActualFrequ,DDSFoxSweeprate,0);
							 		  OnSlopeCancelling=FALSE;
							  }
						
						}
							
			}
			
 			break;
			
		}
	return 0;
}



int  CVICALLBACK CB_OnReCentering (int panel, int control, int event, 
		 void *callbackData, int eventData1, int eventData2)
{
		bool BoxChecked = FALSE; 

	switch (event)
		{
		case EVENT_COMMIT:
			
			if (control== PANEL_CHECKBOX_RECENTER)  
			{
				   	GetCtrlVal(MainPanel, PANEL_CHECKBOX_RECENTER, &BoxChecked) ;
						if (BoxChecked) {
							KeepCentering=TRUE ;
							CenteringTimeBegin275K=utc;
							CenteringTimeBegin10K=utc;
							}
						else {KeepCentering=FALSE ;}
							
			}
			
			
 			break;
			
		}
	return 0;
}


int  CVICALLBACK CB_OnAutoStopSlopeCancellingIfDelock (int panel, int control, int event, 
		 void *callbackData, int eventData1, int eventData2)
{
		bool BoxChecked = FALSE; 
		double   ActualFrequ=0.0;  

	switch (event)
		{
		case EVENT_COMMIT:
			
			if (control== PANEL_CHECKBOX_STOPIFAUTODE)  
			{
				   	GetCtrlVal(MainPanel, PANEL_CHECKBOX_STOPIFAUTODE, &BoxChecked) ;
						if (BoxChecked) {
							AutoStopSlopeCancellingIfDelock=TRUE ;
							}
						else {AutoStopSlopeCancellingIfDelock=FALSE ;}
							
			}
			
			
 			break;
			
		}
	return 0;
}

int CVICALLBACK SlopeReference_cb (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	switch (event)
	{
		case EVENT_COMMIT:
			GetCtrlVal(MainPanel, PANEL_SLOPE_REFERENCE, &slopeReference);
			break;
	}
	return 0;
}