
/******************************************************************
General definitions
******************************************************************/
string MSA112_Label() return "temDM MSA"

number MSA112_OutClusterValue() return 0.000000112

string MSA112_GetClusterLabel(number currCluster)	
	{
	if(CurrCluster<0) currCluster =  0

	string ClusterLabel = MSA112_Label()+":clusters:["+currCluster+"]"
	
	return ClusterLabel
	}


/******************************************************************
Version
******************************************************************/
number MSA112_VersionCode()		//get as  number
	{
	number versionCode
	getPersistentNumberNote(MSA112_MSApath()+":version",versionCode)
	
	return versionCode
	}

string MSA112_Version() 	//get as string
{
string version ="temDM MSA"
string versionCode =(" "+MSA112_VersionCode())

string main =versionCode.left(2)
string sub =versionCode.right(2)
if(sub.right(1) =="0") sub =sub.left(1)
version +=main
version +="."
version +=sub

return version
}

	/* ***************** OVERWRITE *********************** */
number currVersion =237	//2.37


if(MSA112_VersionCode()<currVersion)
	{
	setPersistentNumberNote(MSA112_MSApath()+":aniso:threshold",0.5)
	result("\n"+":aniso:threshold"+" overrided to 0.5")
	
	setPersistentNumberNote(MSA112_MSApath()+":outlier:find",0)	//default no outliers
	result("\n"+":outlier:find is overrided to 0")
	
	setPersistentNumberNote(MSA112_MSApath()+":outlier:correction shift",3)	//defolt shift
	result("\n"+":outlier:correction shift is overrided to 3")

	setPersistentNumberNote(MSA112_MSApath()+":PCA:accelerate",1)
	result("\n"+"PCA:accelerate is overrided to 1")
	}

setPersistentNumberNote(MSA112_MSApath()+":version",currVersion)



/******************************************************************
Functions: Direct to advanced version
******************************************************************/
string MSA112_AddressAdvanced() return "www.temdm.com/web/advanced"

void MSA112_ToAdvanced()
{
number size = 30
string font
number face = 0
number background = 255
string message = "Thank you for using temDM MSA!\nConsider the purchase of a licensed advanced version at \n"+MSA112_AddressAdvanced() 
result("\n"+message)
OKdialog(message)
}

/******************************************************************
Functions: Catch unautorized
******************************************************************/

void MSA112_NotAuthorized(string command)
{
number size = 30
string font
number face = 0
number background = 255
string message = "You are NOT autorized to use "+command+" command !\nConsider the purchase of a licensed advanced version in\n"+MSA112_AddressAdvanced()
image messageIm = MSA112_GraphicFromString(message,size,font,face,background)
messageIm.showimage()

result("\n"+message)
}


void MSA112_NotAuthorizedKnob(string option)
{
number size = 30
string font
number face = 0
number background = 255
string message = "The option '"+option+"' is not available in basic version.\nConsider the purchase of a licensed advanced version in\n"+MSA112_AddressAdvanced()
image messageIm = MSA112_GraphicFromString(message,size,font,face,background)
messageIm.showimage()

result("\n"+message)

exit(0)
}


/******************************************************************
function: Delete All MSA Tags
******************************************************************/
void MSA112_delete()
{
TagGroup Pers = GetPersistentTagGroup( ) 
Pers.TagGroupDeleteTagWithLabel("_temDM-Scripts:MSA")
}


/******************************************************************
Standard names
******************************************************************/


/*************Uloading of the parameter name is needed for smooth creation of setting line ******************** / 

/*********** Import tool ***********/

//with uploading names
number MSA112P_DefUpsample(object frm){string Name = "import:default bin"; MSA112_UP(Name) ; number par = 4; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_DefScale(object frm){string Name = "import:default scale"; MSA112_UP(Name) ; number par = 1; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_DefSpatOrigin(object frm){string Name = "import:default spatial origin"; MSA112_UP(Name) ; number par = 0; return MSA112_LoadStoreParameter(frm,par,Name);}
string MSA112P_DefScaleUnit(object frm){string Name = "import:default scale units"; MSA112_UP(Name) ; string content = "nm"; return MSA112_LoadStoreString(frm,content,Name);}
number MSA112P_DefDispersion(object frm){string Name = "import:default dispersion"; MSA112_UP(Name) ; number par = 0.01; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_DefEnergyOrigin(object frm){string Name = "import:default energy origin"; MSA112_UP(Name) ; number par = 50; return MSA112_LoadStoreParameter(frm,par,Name);}
string MSA112P_DefEnergyUnit(object frm){string Name = "import:default dispersion units"; MSA112_UP(Name) ; string cont = "keV"; return MSA112_LoadStoreString(frm,cont,Name);}
number MSA112P_showHDF5(object frm){string Name = "Velox:show HDF5"; MSA112_UP(Name) ; number par = 0; return MSA112_LoadStoreParameter(frm,par,Name);}

//import  Velox standard names		No uploading
string MSA112P_branchName(object frm){string Name = "Velox:SpectrumStreamName"; string cont = "SpectrumStream"; return MSA112_LoadStoreString(frm,cont,Name);}
string MSA112P_streamName(object frm){string Name = "Velox:DataName"; string cont = "Data"; return MSA112_LoadStoreString(frm,cont,Name);}
string MSA112P_MetaName(object frm){string Name = "Velox:MetadataName"; string cont = "Metadata"; return MSA112_LoadStoreString(frm,cont,Name);}
string MSA112P_FLTname(object frm){string Name = "Velox:FrameLocationTableName"; string cont = "FrameLocationTable"; return MSA112_LoadStoreString(frm,cont,Name);}
string MSA112P_PathSize(object frm){string Name = "Velox:ScanSizeName"; string cont = "Scan:ScanSize"; return MSA112_LoadStoreString(frm,cont,Name);}
string MSA112P_PathArea(object frm){string Name = "Velox:ScanAreaName"; string cont = "Scan:ScanArea"; return MSA112_LoadStoreString(frm,cont,Name);}
string MSA112P_PathSettings(object frm){string Name = "Velox:SettingsName"; string cont = "AcquisitionSettings"; return MSA112_LoadStoreString(frm,cont,Name);}
string MSA112P_NumberChannelsName(object frm){string Name = "Velox:NoChannelsName"; string cont = "bincount"; return MSA112_LoadStoreString(frm,cont,Name);}
string MSA112P_PixelCalibration(object frm){string Name = "Velox:PixelSizeName"; string cont = "BinaryResult:PixelSize"; return MSA112_LoadStoreString(frm,cont,Name);}
string MSA112P_PixelOffset(object frm){string Name = "Velox:OffsetName"; string cont = "BinaryResult:Offset"; return MSA112_LoadStoreString(frm,cont,Name);}
string MSA112P_DetectorList(object frm){string Name = "Velox:DetectorName"; string cont = "Detectors:Detector-"; return MSA112_LoadStoreString(frm,cont,Name);}
string MSA112P_DispersionName(object frm){string Name = "Velox:DispersionName"; string cont = "Dispersion"; return MSA112_LoadStoreString(frm,cont,Name);}
string MSA112P_EnergyOriginName(object frm){string Name = "Velox:OffsetEnergyName"; string cont = "OffsetEnergy"; return MSA112_LoadStoreString(frm,cont,Name);}
string MSA112P_Company(object frm){string Name = "Velox:CompanyName"; string cont = "FEI"; return MSA112_LoadStoreString(frm,cont,Name);}

number MSA112P_MaxNumberEDXDetectors(object frm){string Name = "Velox:MaxNumberDetectors"; number par = 4; return MSA112_LoadStoreParameter(frm,par,Name);}

//depth of imported cube
number MSA112P_ImportVeloxDepth(object frm){string Name = "Velox:ByteDepth"; number par = 2; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_ImportEspritDepth(object frm){string Name = "import:ByteDepth"; number par = 1; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_ImportJeolDetectorIndex(object frm){string Name = "Jeol:DetectorIndex"; number par = 2; return MSA112_LoadStoreParameter(frm,par,Name);}

/*********** Filtering tool ***********/
//with uploading names
number MSA112P_DefBin(object frm){string Name = "filtering:default binning"; MSA112_UP(Name) ; number par = 2; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_DefSigmaX(object frm){string Name = "filtering:default spatial sigma"; MSA112_UP(Name) ; number par = 1.0; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_DefSigmaE(object frm){string Name = "filtering:default  energy sigma"; MSA112_UP(Name) ; number par = 1.0; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_clipX (object frm){string Name = "filtering:clip after spatial filtering"; MSA112_UP(Name) ; number par = 0; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_clipE (object frm){string Name = "filtering:clip after  energy filtering"; MSA112_UP(Name) ; number par = 1; return MSA112_LoadStoreParameter(frm,par,Name);}

// No uploading
string MSA112P_FilterLabel(object frm){string Name = "filtering:label"; string cont = "temDM Filter"; return MSA112_LoadStoreString(frm,cont,Name);}
number MSA112P_MinTail(object frm){string Name = "filtering:ignored tail"; number par = 0.1; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_MinSpar(object frm){string Name = "filtering:min sparsity"; number par = 0.02; return MSA112_LoadStoreParameter(frm,par,Name);}


/*********** Proceed tool ***********/
//with uploading names
number MSA112P_converg(object frm){string Name = "PCA:convergence"; MSA112_UP(Name) ; number par = 0; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_deviaSpec(object frm){string Name = "PCA:max deviation"; MSA112_UP(Name) ; number par = 0.02; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_MaxIttPCA(object frm){string Name = "PCA:max iterations"; MSA112_UP(Name) ; number par = 50; return MSA112_LoadStoreParameter(frm,par,Name);}
	//advanced
number MSA112P_OutTreat(object frm){string Name = "outlier:find"; MSA112_UP(Name) ; number par = 0; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_OutCorr(object frm){string Name = "outlier:remove"; MSA112_UP(Name) ; number par = 2; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_OutThres(object frm){string Name = "outlier:threshold"; MSA112_UP(Name) ; number par = 4; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_ShiftCor(object frm){string Name = "outlier:correction shift"; MSA112_UP(Name) ; number par = 3; return MSA112_LoadStoreParameter(frm,par,Name);}
//number MSA112P_Aver(object frm){string Name = "outlier:load averaging"; MSA112_UP(Name) ; number par = 7; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_LoadThres(object frm){string Name = "outlier:loadThreshold"; MSA112_UP(Name) ; number par = 20; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_SVDitterations(object frm){string Name = "SVD:iterations"; MSA112_UP(Name) ; number par = 5; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_anisoThreshold(object frm){string Name = "aniso:threshold"; MSA112_UP(Name) ; number par = 1.0; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_quadrant(object frm){string Name = "quadrant correction:on"; MSA112_UP(Name) ; number par = 0; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_accel(object frm){string Name = "PCA:accelerate"; MSA112_UP(Name) ; number par = 1; return MSA112_LoadStoreParameter(frm,par,Name);}


// No uploading
number MSA112P_Weight(object frm){string Name = "Weighting"; number par = 1; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_Cent(object frm){string Name = "Centering"; number par = 1; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_numberRotations(object frm){string Name = "aniso:projections"; number par = 12; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_defRECcomponents(object frm){string Name = "REC:default components"; number par = 2; return MSA112_LoadStoreParameter(frm,par,Name);}
	//advanced
number MSA112P_SVD(object frm){string Name = "SVD:do"; number par = 0; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_defPCAcomponents(object frm){string Name = "PCA:default components"; number par = 2; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_defSVDcomponents(object frm){string Name = "SVD:default components"; number par = 50; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_anisoDisp(object frm){string Name = "aniso:dispersion"; number par = 0.05; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_autoCutDelta(object frm){string Name = "aniso:autoCutDelta"; number par = 2; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_autoCutRange(object frm){string Name = "aniso:autoCutRange"; number par = 10; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_autoCutDescend(object frm){string Name = "aniso:autoCutDescend"; number par = 3.0; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_allowNeg(object frm){string Name = "allow negative"; number par = 0; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_quadrantAver(object frm){string Name = "quadrant correction:average"; number par = 8; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_quadrantPlain(object frm){string Name = "quadrant correction:plain"; number par = 0; return MSA112_LoadStoreParameter(frm,par,Name);}
//number MSA112P_assynch(object frm){string Name = "PCA:assynch"; number par = 0; return MSA112_LoadStoreParameter(frm,par,Name);}

/*********** Rotation tool ***********/
//with uploading names
number MSA112P_ICAdev(object frm){string Name = "ICA:max deviation"; MSA112_UP(Name) ; number par = 0.02; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_ICAitt(object frm){string Name = "ICA:max itterations"; MSA112_UP(Name) ; number par = 100; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_ICAcontr(object frm){string Name = "ICA:contrast function"; MSA112_UP(Name) ; number par = 1; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_VRMdev(object frm){string Name = "VRM:max deviation"; MSA112_UP(Name) ; number par = 0.02; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_VRMitt(object frm){string Name = "VRM:max itterations"; MSA112_UP(Name) ; number par = 10; return MSA112_LoadStoreParameter(frm,par,Name);}


/*********** View Results tool ***********/
//with uploading names
number MSA112P_sparsity(object frm){string Name = "sparsity in X-Y plot"; MSA112_UP(Name) ; number par = 10; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_maxLoadings(object frm){string Name = "max displayed loadings"; MSA112_UP(Name) ; number par = 10; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_ScoresCubed(object frm){string Name = "show scores as cubed"; MSA112_UP(Name) ; number par = 1; return MSA112_LoadStoreParameter(frm,par,Name);}



/*********** Clustering tool ***********/
//with uploading names
number MSA112P_gap(object frm){string Name = "clustering:ellipse gap"; MSA112_UP(Name) ; number par = 3; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_ellipsePoints(object frm){string Name = "clustering:ellipse points"; MSA112_UP(Name) ; number par = 10; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_MaskCubed(object frm){string Name = "clustering:mask cubed"; MSA112_UP(Name) ; number par = 1; return MSA112_LoadStoreParameter(frm,par,Name);}

//No uploading
number MSA112P_KmeansIter(object frm){string Name = "clustering:KmeansIter"; number par = 3; return MSA112_LoadStoreParameter(frm,par,Name);}

	
/*********** EndMembers tool ***********/
//with uploading names
number MSA112P_Rad(object frm){string Name = "EndMembers:pointer size"; MSA112_UP(Name) ; number par = 5; return MSA112_LoadStoreParameter(frm,par,Name);}			
number MSA112P_MapsCubed(object frm){string Name = "EndMembers:maps cube"; MSA112_UP(Name) ; number par = 0; return MSA112_LoadStoreParameter(frm,par,Name);}		
	//advanced
number MSA112P_lambda(object frm){string Name = "EndMembers:lambda"; MSA112_UP(Name) ; number par = 0.01; return MSA112_LoadStoreParameter(frm,par,Name);}	
number MSA112P_minPoints(object frm){string Name = "EndMembers:min points"; MSA112_UP(Name) ; number par = 10; return MSA112_LoadStoreParameter(frm,par,Name);}	
number MSA112P_VCAhits(object frm){string Name = "EndMembers:auto:VCAhits"; MSA112_UP(Name) ; number par = 50; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_hitsMethod(object frm){string Name = "EndMembers:auto:hits method"; MSA112_UP(Name) ; number par = 1; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_clustMethod(object frm){string Name = "EndMembers:auto:cluster method"; MSA112_UP(Name) ; number par = 1; return MSA112_LoadStoreParameter(frm,par,Name);}
	
// No uploading
number MSA112P_RadAuto(object frm){string Name = "EndMembers:auto:pointer size"; MSA112_UP(Name) ; number par = 3; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_DefaultPCnumber(object frm){string Name = "EndMembers:auto:defaultPCnumber"; number par = 2; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_SigmaTrack(object frm){string Name = "EndMembers:auto:sigmaTrack"; number par = 3; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_HistRange(object frm){string Name = "EndMembers:auto:histRange"; number par = 0.5; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_MDsurewidth(object frm){string Name = "EndMembers:auto:MDsureWidth"; number par = 1; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_RadiusStep(object frm){string Name = "EndMembers:auto:radiusStep"; number par = 0.2; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_RadiusMax(object frm){string Name = "EndMembers:auto:radiusMax"; number par = 3.0; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_RadiusIntermd(object frm){string Name = "EndMembers:auto:radiusIntermd"; number par = 1.0; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_MembersReserve(object frm){string Name = "EndMembers:auto:membersReserve"; number par = 2; return MSA112_LoadStoreParameter(frm,par,Name);}

/*********** LowCut tool ***********/
//with uploading names
number MSA112P_LBchannels(object frm){string Name = "lowCut:channels"; MSA112_UP(Name) ; number par = 10; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_LBmax(object frm){string Name = "lowCut:max"; MSA112_UP(Name) ; number par = 3; return MSA112_LoadStoreParameter(frm,par,Name);}


/*********** View spikes tool ***********/
//with uploading names : same as in proceed
//No uploading
number MSA112P_SpikesViewChannels(object frm){string Name = "outlier:ViewChannels"; number par = 100; return MSA112_LoadStoreParameter(frm,par,Name);}


/*********** histogram tool ***********/
//with uploading names
number MSA112P_HistChannels(object frm){string Name = "histogram:channels"; MSA112_UP(Name) ; number par = 50; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_HistMaxComps(object frm){string Name = "histogram:max displayed PCAs"; MSA112_UP(Name) ; number par = 10; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_HistCount(object frm){string Name = "histogram:use reconstructed counts"; MSA112_UP(Name) ; number par = 1; return MSA112_LoadStoreParameter(frm,par,Name);}

// No uploading
number MSA112P_HistSigmaRange(object frm){string Name = "histogram:sigma range"; number par = 3; return MSA112_LoadStoreParameter(frm,par,Name);}



/*********** denoise tool ***********/
//with uploading names
number MSA112P_AutoBin(object frm){string Name = "auto:import binning"; MSA112_UP(Name) ; number par = 1; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_AutoGausX(object frm){string Name = "auto:import gaussian X"; MSA112_UP(Name) ; number par = 0; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_AutoGausE(object frm){string Name = "auto:import gaussian E"; MSA112_UP(Name) ; number par = 0; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_AutoForceFiltering(object frm){string Name = "auto:force filtering"; MSA112_UP(Name) ; number par = 1; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_AutoFixed(object frm){string Name = "auto:default fixed components"; MSA112_UP(Name) ; number par = 5; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_AutoMax(object frm){string Name = "auto:maximum componrents"; MSA112_UP(Name) ; number par = 20; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_AutoTrunc(object frm){string Name = "auto:truncate method"; MSA112_UP(Name) ; number par = 1; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_AutoShowMSA(object frm){string Name = "auto:default showMSA"; MSA112_UP(Name) ; number par = 0; return MSA112_LoadStoreParameter(frm,par,Name);}
number MSA112P_AutoShowComp(object frm){string Name = "auto:default showCOMP"; MSA112_UP(Name) ; number par = 1; return MSA112_LoadStoreParameter(frm,par,Name);}



/*********** ViewSpec / TwinViewer ***********/
//this should be set from GMS1/GMS2 functions (without any tools opened)
number MSA112P_FastPixel(){string Name = "fast pixel"; number par = 625; getPersistentNumberNote(MSA112_MSApath()+":"+Name,Par); return Par;}
void MSA112P_FastPixel(number Par){string Name = "fast pixel"; number exPar; \
if(!getPersistentNumberNote(MSA112_MSApath()+":"+Name,exPar)) setPersistentNumberNote(MSA112_MSApath()+":"+Name,Par);}


	





