function TETGAR_C(Alm,Prp,Grs,Sps,SPS,marker,string)
% color plotting in TETGAR_C is based on plot3c by Ulrich Theune; for testing whether a point plots in a convex hull see Inhull by John D'Errico
%calculation of coordinantes for the system Almandine-Pyrope-Grossular-Spessartine 
x = (Alm/100)*1+(Prp/100)*2+(Grs/100)*3+(Sps/100)*2;   % calculation of x- coordinates 
y = (Alm/100)*1+(Prp/100)*2.7322+(Grs/100)*1+(Sps/100)*1.57735; % calculation of y-coordinates 
z = (Alm/100)*1+(Prp/100)*1+(Grs/100)*1+(Sps/100)*2.633;     % calculation of z-coordinates 
data_array=transpose([x;y;z]); 
string='SPESSARTINE mol%'
subplot
x1=[3 2 1 2 3 2 2 1 3] ;                            %tetrahedron
y1=[1 2.732 1 1.577350 1 1.577350 2.732 1 1 ] ;      
z1=[1 1 1 2.633 1 2.633  1 1 1 ];        
x37=[3 2 1 3]                                        %tetrahedron base
y37=[1 2.732 1 1]
z37=[1 1 1 1]
x2=[0.9] ;                                           %annotation almandine
y2=[0.95];
z2=[1]   ;                                          
x3=[3.01];                                            %annotation grossular
y3=[0.95];
z3=[1];
x4=[2.03];                                         %annotation pyrope 
y4=[2.86];
z4=[1.0];
x5=[2.03] ;                                        %annotation spessartine    
y5=[ 1.62];
z5=[2.72];
x6=[1.5	2.5	2	1.5	2	2.5	1.5	2	2.5	2.5	2	1.5	1.5	1.75	2.25	2	1.75	1.25	1.5	1.25	1.25	1.5	2.5	2.75	2.75	2.5	3	2.25	1.75	2	2.25 2.125 1.875 2 2.125 2.375	1.625	2	2.375 2.625	2.25	2.625 2.625 3 2.875 2.875 2.75 2.875 3 1 1.25 1.125 1.125 1.25 1.75 1.375 1.375 1.75 1 2 2 1.875 2.125 2 2 2.375 1.625 2 2.375];
y6=[ 1.866025	1.866025	1	1.866025	2.154675	1.28867	1.28867	2.154675	1.866025	1.28867	1	1.28867	1.866025	2.299	2.299	2.4433375	2.299	1.433	1	1.14433	1.433	1	1	1.14433	1.433	1	1	1.433	1.433	1.85621625	1.433 1.5051 1.5051 1.72168 1.5051 1.36083	1.36083	2.01035	1.36083 1.2165	1	1.649 1.2165 1 1.07216 1.2165 1 1.07216 1 1 1 1.07216 1.2165 1 1 1.2165 1.649 1 1 2.732 2.5876 2.5155 2.5155 2.5876 2.732 2.0825125 2.0825125 2.2997 2.0825125];
z6=[1	1	1	1	1.8165	1.8165	1.8165	1.8165	1	1.8165	1	1.8165	1	1	1	1.40825	1	1	1	1.40825	1	1	1	1.40825	1	1	1	2.22475	2.22475	2.22475	2.22475 2.42887 2.42887 2.42887 2.42887 2.020625 2.020625 2.020625 2.020625 1.61237	1	1 1.61237 1 1.20412 1 1 1.20412 1 1 1 1.20412 1 1 1 1.61237 1 1 1 1 1.20412 1 1 1.20412 1 1 1 1.61237 1 ];
x7=[1.37];   % annotation    50%  
y7=[1.866025];
z7=[0.97];
x8=[2.55]; % annotation    50%  
y8=[1.866025];
z8=[0.97];
x9=[2];  % annotation    50%  
y9=[0.95];
z9=[0.97];
x10=[1.43]; % annotation    50%  
y10=[1.28866];
z10=[1.8165];
x11=[2.525];  % annotation    50%  
y11=[1.28866];
z11=[1.8165];
x12=[2];  % annotation    50%  
y12=[2.17];
z12=[1.8165];
x13=[1.65];  % annotation    75%  
y13=[1.433];
z13=[2.2275];
x14=[2.275];   % annotation    75%  
y14=[1.433]; 
z14=[2.2275];
x15=[2];   % annotation    75%  
y15=[1.88];
z15=[2.2275];
x16=[2];   % annotation    75%  
y16=[2.46];
z16=[1.40825];
x17=[1.62];    % annotation    75%  
y17=[2.31];
z17=[0.97];
x18=[2.30];  % annotation    75% 
y18=[2.31];
z18=[0.97];
x19=[1.5];% annotation    75% 
y19=[0.95];
z19=[0.97];
x20=[1.12];   % annotation    75%  
y20=[1.44];
z20=[0.97];
x21=[1.165];   % annotation    75%  
y21=[1.14433];
z21=[1.40825];
x22=[2.5];   % annotation    75%  
y22=[0.95];
z22=[0.97];
x23=[2.77];% annotation    75% 
y23=[1.14433];    
z23=[1.40825];
x24=[2.80];   % annotation    75%  
y24=[1.44];
z24=[0.97];
x25=[1.25]; % annotation    87.5%  
y25=[0.95];  
z25=[0.97];      
x26=[1.75];   % annotation    62.5%  
y26=[0.95];
z26=[0.97];
x27=[2.25] ;  % annotation    62.5%
y27=[0.95];
z27=[0.97];
x28=[2.75]; % annotation    87.5%
y28=[0.95];
z28=[0.97];
x29=[1.01];   % annotation    87.5%
y29=[1.24];
z29=[0.97];
x30=[2.91] ; % annotation    87.5%
y30=[1.24];
z30=[0.97];
x31=[1.255] ;    % annotation    62.5%
y31=[1.677]; 
z31=[0.97];
x32=[2.665];  % annotation    62.5%
y32=[1.677];
z32=[0.97];
x33=[1.50];   % annotation    62.5%
y33=[2.10];
z33=[0.97];
x34=[2.42] ;  % annotation    62.5%
y34=[2.10];
z34=[0.97];
x35=[1.76] ;  % annotation    87.5%
y35=[2.54];
z35=[0.97];
x36=[2.165];   % annotation    87.5%
y36=[2.54];
z36=[0.97];
x42=[1.51];                                      %annotation granites & pegmatites
y42=[1.40];
z42=[1.82];
x43=[1.91];                               %annotation greenschists
y43=[1.32];
z43=[1.71];
x44=[1.39] ;                              %annotation metasedimentary amphibolites
y44=[1.06];
z44=[1.10];
x45=[1.63] ;                              %annotation metasedimentary & metaigneous blueschists
y45=[1.04];
z45=[1.10];
x46=[2.34] ;                              %annotation metasomatic rocks
y46=[1.07];
z46=[1.10];
x47=[1.64] ;                             %annotation metasedimentary granulites
y47=[1.88];
z47=[1.07];
x48=[1.61] ;                            %annotation metigneous granulites
y48=[1.47];
z48=[1.04];
x49=[1.65] ;                           %annotation metaigneous amphibolites 
y49=[1.33];
z49=[1.07];
x50=[1.70] ;                           %annotation metasedimentary+metaigneous eclogites
y50=[1.65];
z50=[1.07];
x51=[1.78] ;                           %annotation ultramafic rocks
y51=[2.19];
z51=[1.04];
plot3(x1,y1,z1, 'k')                 %plot tetrahedron
hold on
grid 
whitebg('white')
axis equal 
 set(gcf, 'Color', [1,1,1])
hold on
% representative metasedimentary and metaigneous blueschist garnets (within the range of +/- SD of the mean)  
bluex=[1.697;   1.715284715;    1.626252505;    1.698698699;    1.653923541;    1.652347652;    1.698396794;    1.692;  1.675702811;    1.609;  1.689;  1.605779154;    1.646292585;    1.636734694;    1.618618619;    1.59;   1.693239152;    1.623;  1.63;   1.603045685;    1.594;  1.624624625;    1.675;  1.728;  1.632;  1.598796389;    1.599;  1.629;  1.637637638;    1.765765766;    1.636;  1.664937759;    1.62;   1.634441088;    1.648;  1.641;  1.642;  1.622851365;    1.647;  1.677;  1.640280561;    1.655;  1.627;  1.667;  1.713;  1.693; 1.632820513; 1.716716717;    1.744723618;    1.66838843; 1.599;  1.557459677;    1.725430598;    1.712712713;    1.597;  1.692;  1.692;  1.718592965;    1.577435897;    1.675510204;    1.654;  1.57;   1.705;  1.651651652;    1.698698699;    1.532;  1.681;  1.69035533; 1.659;  1.787;  1.647590361;    1.74;   1.737;  1.72983871; 1.707];
bluey=[1.2072634;   1.200712388;    1.147516132;    1.141011812;    1.065053119;    1.150535664;    1.233134168;    1.1293256;  1.217372791;    1.222277;   1.166853;   1.21270743; 1.240079359;    1.210909388;    1.18724985; 1.1027723;  1.1893444;  1.176094;   1.167434;   1.222735025;    1.2113117;  1.176848549;    1.2407557;  1.2771277;  1.2240134;  1.219475827;    1.1899511;  1.1788271;  1.172071171;    1.231750851;    1.2805928;  1.274141909;    1.1743642;  1.070940785;    1.2667379;  1.2303659;  1.1662819;  1.231176845;    1.2326756;  1.1745196;  1.167193988;    1.2253253;  1.1699024;  1.1326441;  1.2938755;  1.2585046;  1.17631241; 1.171499499;    1.236170553;    1.216517045;    1.1098656;  1.133719355;    1.26951844; 1.250101301;    1.1742209;  1.1932751;  1.1932751;  1.238499598;    1.145531077;    1.246707449;    1.1543287;  1.2182606;  1.1472486;  1.161265866;    1.154909209;    1.1466731;  1.1911285;  1.167666802;    1.1118832;  1.2400512;  1.111910743;    1.1655796;  1.1668893;  1.118774597;    1.1842423];
bluez=[1.00326; 1.004885115;    1.0048998;  1.006526527;    1.006559356;    1.00977023; 1.011432866;    1.01304;    1.014728916;    1.0163; 1.0163; 1.025232198;    1.026132265;    1.026612245;    1.029369369;    1.03097;    1.031251261;    1.0326; 1.0326; 1.033096447;    1.03423;    1.034264264;    1.03423;    1.03423;    1.03586;    1.035967904;    1.03749;    1.03849;    1.038528529;    1.037527528;    1.03912;    1.041618257;    1.04238;    1.042678751;    1.04401;    1.04401;    1.04401;    1.044499494;    1.04564;    1.04464;    1.045731463;    1.04627;    1.05116;    1.05479;    1.05705;    1.06294;    1.064553846;    1.066266266;    1.067165829;    1.075774793;    1.07724;    1.079879032;    1.085238095;    1.092372372;    1.09391;    1.10369;    1.10369;    1.10320603; 1.108020513;    1.115785714;    1.11473;    1.12714;    1.12714;    1.127267267;    1.128898899;    1.13529;    1.13855;    1.155553299;    1.15748;    1.15748;    1.168564257;    1.17704;    1.17767;    1.216895161;    1.32437];
%representative metasedimentary and metaigneous eclogite garnets (within the range of +/- SD of the mean)
ecx=[1.842157842;   1.8;    1.765593561;    1.782217782;    1.751;  1.755;  1.948948949;    1.963;  1.96;   1.851851852;    1.79258517; 1.745235707;    1.771;  1.788;  1.805805806;    1.735;  1.793;  1.9168357;  1.725274725;    1.849150849;    1.85985986; 1.812625251;    1.841683367;    1.825;  1.824;  1.713;  1.824;  1.843;  1.695783133;    1.834;  1.854563691;    1.789795918;    1.774358974;    1.737874097;    1.728915663;    1.828396323;    1.787;  1.666666667;    1.749;  1.783;  1.783367556;    1.675];
ecy=[1.712295105;   1.5115188;  1.60347163; 1.396233966;    1.4243422;  1.4763022;  1.624146346;    1.6287182;  1.5842639;  1.728748649;    1.58717024; 1.62250341; 1.3423616;  1.4687976;  1.543817417;    1.6333376;  1.6471936;  1.566796755;    1.463715584;    1.479865135;    1.555376376;    1.594113226;    1.58196493; 1.3492907;  1.4099107;  1.4636027;  1.4999747;  1.5425427;  1.486332028;    1.5958124;  1.56212678; 1.568343265;    1.661854769;    1.417226419;    1.398805321;    1.473979673;    1.5120998;  1.378481263;    1.4589866;  1.4716883;  1.683441786;    1.602166];
ecz=[1.003256743;   1.00652;    1.006559356;    1.00977023; 1.00978;    1.00978;    1.00978979; 1.00978 ;1.01141;   1.011421421;    1.011432866;    1.011444333;    1.01304;    1.01304;    1.013053053;    1.01304;    1.01304;    1.013225152;    1.014655345;    1.016283716;    1.016316316;    1.016332665;    1.016332665;    1.01793;    1.01793;    1.01793;    1.01793;    1.01893;    1.018002008;    1.01956;    1.020621866;    1.020979592;    1.021087179;    1.019153767;    1.0212751;  1.022665986;    1.02282;    1.027650104;    1.02934;    1.03097;    1.031796715;    1.0326];  
%representative pegmatite garnets (within the range of +/- SD of the mean)
pex=[1.449; 1.39217653; 1.384;  1.385614386;    1.412;  1.4;    1.557;  1.431343134;    1.574;  1.4418; 1.577;  1.592;  1.571;  1.478;  1.590590591;    1.602602603;    1.61;   1.611;  1.629;  1.5677; 1.616480163];
pey=[1.2616805; 1.233476128;    1.2264309;  1.227361139;    1.246065;   1.2472237;  1.4036902;  1.24720154; 1.3881044;  1.25486034; 1.3956145;  1.4008193;  1.391005;   1.2841987;  1.388509409;    1.379263564;    1.3765823;  1.3898639;  1.3898672;  1.32113342; 1.351424517];
pez=[1.66015;   1.492106319;    1.51671;    1.529220779;    1.5379; 1.55583;    1.59658;    1.604138414;    1.60636;    1.626246;   1.62755;    1.66667;    1.6683; 1.66993;    1.68039039; 1.683653654;    1.71557;    1.72861;    1.74328;    1.861618;   1.926927772];
%representative metasedimentary greenschist garnets (within the range of +/- SD of the mean)
grex=[1.580958096;	1.64;	1.768039672;	1.714585834;	1.77722063;	1.96039604;	1.7698;	1.7727;	1.85;	1.8288;	1.817;	1.8242;	1.81;	1.882414152];
grey=[1.123274637;	1.1385625;	1.126149939;	1.13336225;	1.142209972;	1.211503465;	1.240292245;	1.245546005;	1.2598065;	1.27181572;	1.2823235;	1.289482665;	1.3406345;	1.303994017];
grez=[1.161029903;	1.24495;	1.246743143;	1.258607343;	1.346085039;	1.501217822;	1.5988211;	1.6014339;	1.63687;	1.7041496;	1.73485;	1.7575487;	1.76751;	1.773168574]; 
%representative metaigneous granulite garnets (within the range of +/- SD of the mean)
metiggrax=[ 1.73026973;	1.609;	1.664994985;	1.751794872;	1.68;	1.636;	1.663663664;	1.692;	1.681;	1.705521472;	1.57957958;	1.709;	1.632632633;	1.695;	1.649350649;	1.677710843;	1.685055165;	1.633165829;	1.683;	1.65965966;	1.674;	1.633633634;	1.656312625;	1.665;	1.681318681;	1.669;	1.691];
metiggray=[1.419302198;	1.39547355;	1.469626429;	1.444694923;	1.5103629;	1.46417625;	1.527633233;	1.5219096;	1.51902295;	1.358915082;	1.369285586;	1.546735;	1.396447447;	1.51786835;	1.448716983;	1.539656325;	1.539115045;	1.471150804;	1.54442575;	1.420141892;	1.4306911;	1.474465566;	1.467999098;	1.48322845;	1.475247952;	1.4497432;	1.4774553];
metiggraz=[1.016313686;	1.021229;	1.021292879;	1.021773333;	1.022862;	1.024495;	1.026154154;	1.026128;	1.027761;	1.028385481;	1.029423423;	1.03266;	1.032692693;	1.034293;	1.03589011;	1.036070281;	1.036034102;	1.037747739;	1.040825;	1.040865866;	1.042458;	1.042500501;	1.042543086;	1.044091;	1.052203796;	1.052256;	1.062054];
%representative metaigneous amphibolite garnets (within the range of +/- SD of the mean) 
metigamx=[1.726452906;	1.637396694;	1.57;	1.674;	1.677385892;	1.697;	1.685110664;	1.696696697;	1.663654618;	1.73;	1.686686687;	1.557575758;	1.548155738;	1.630390144;	1.630390144;	1.637014315;	1.591331269];
metigamy=[1.3314751;	1.314312862;	1.3821949;	1.38334965;	1.270101297;	1.2811617;	1.282858853;	1.257748849;	1.249830472;	1.2124592;	1.248502452;	1.202942323;	1.26441501;	1.244212012;	1.244212012;	1.255609969;	1.265133695];
metigamz=[1.014726453;	1.018556818;	1.022862;	1.031027;	1.032185685;	1.035926;	1.036142857;	1.042500501;	1.047547189;	1.052256;	1.060481481;	1.089072727;	1.105408811;	1.117361396;	1.117361396;	1.121890593;	1.123022704];
% representative metasedimentary granulite garnets (within the range of +/- SD of the mean)
sedgrax=[1.464;	1.552;	1.612676056;	1.578;	1.612;	1.516;	1.407407407;	1.353646354;	1.44044044;	1.624;	1.508682329;	1.524;	1.41;	1.42;	1.513;	1.54;	1.58041958;	1.477522478;	1.368686869;	1.545734841;	1.411;	1.539;	1.473473473;	1.565;	1.575;	1.454;	1.44;	1.451354062;	1.477948718;	1.441;	1.598;	1.463;	1.548;	1.496;	1.546;	1.422;	1.437;	1.571571572;	1.639558233;	1.581;	1.485;	1.495;	1.62;	1.431863727;	1.582164329;	1.357862903;	1.46;	1.554;	1.594;	1.444;	1.462;	1.609829488;	1.560247168;	1.503;	1.375375375;	1.47464503;	1.355;	1.389167503;	1.38804458;	1.394;	1.61;	1.46680286;	1.59;	1.495486459;	1.478439425;	1.445240532;	1.465163934;	1.401422764;	1.548;	1.47;	1.426;	1.482;	1.410876133;	1.396;	1.49389002;	1.407815631;	1.523906409];
sedgray=[1.7355227;	1.6904907;	1.875293159;	1.82905075;	1.88793875;	1.47918675;	1.639747848;	1.519081019;	1.523587688;	1.8140401;	1.732429111;	1.85791745;	1.64314945;	1.63622145;	1.79383345;	1.84059745;	1.862251199;	1.607901548;	1.588413586;	1.630735303;	1.6367988;	1.8515668;	1.665175976;	1.8099988;	1.8099988;	1.5969628;	1.5068988;	1.644660782;	1.607965949;	1.66262415;	1.87219615;	1.57948815;	1.68514015;	1.42607215;	1.8544535;	1.6292935;	1.6233655;	1.54497047;	1.672551707;	1.89933085;	1.62987085;	1.44627885;	1.63779885;	1.586010872;	1.842860571;	1.527438357;	1.6643562;	1.8071122;	1.8729282;	1.6062002;	1.6131282;	1.865865797;	1.475225747;	1.71069755;	1.541503053;	1.746395081;	1.4809189;	1.508424173;	1.422910081;	1.5149816;	1.8717736;	1.737305005;	1.90179495;	1.730787312;	1.622382238;	1.602152508;	1.593896516;	1.568532876;	1.8621137;	1.7116977;	1.5535084;	1.6031591;	1.576752367;	1.45220715;	1.528536813;	1.460633768;	1.511984232];
sedgraz=[1.003266;	1.003266;	1.006571429;	1.008165;	1.008165;	1.008165;	1.009807808;	1.009788212;	1.009807808;	1.009798;	1.010008172;	1.011431;	1.011431;	1.011431;	1.011431;	1.011431;	1.01141958;	1.01141958;	1.011546465;	1.011748201;	1.013064;	1.013064;	1.013077077;	1.013064;	1.013064;	1.013064;	1.013064;	1.012100301;	1.014424615;	1.015697;	1.015697;	1.015697;	1.015697;	1.014697;	1.01633;	1.01633;	1.01733;	1.016346346;	1.015391566;	1.018963;	1.017963;	1.017963;	1.018963;	1.017998998;	1.017998998;	1.017099798;	1.020596;	1.019596;	1.019596;	1.019596;	1.019596;	1.018651956;	1.01915139;	1.021229;	1.02125025;	1.022544625;	1.022862;	1.022930792;	1.024817629;	1.026128;	1.026128;	1.025667007;	1.027761;	1.027844534;	1.030178645;	1.033428864;	1.033463115;	1.03485061;	1.034926;	1.036926;	1.040192;	1.043458;	1.042757301;	1.046357;	1.048225051;	1.048086172;	1.050854527];
%representative metamafic garnets (within the range of +/- SD of the mean)
mafx=[1.929;	1.938;	2.032064128;	2.116116116;	1.9749499;	2.021042084;	2.021105528;	1.921588595;	1.90392562;	2.055323591;	2.024784483;	1.936063936;	1.921;	1.992992993;	1.862587763;	2.093467337;	1.93800813;	1.884928717;	1.920326864;	1.929519918;	1.92642487;	1.886694387;	1.901041667;	1.863541667;	1.905337362;	1.948640483;	1.936491935;	1.868287741;	1.870050761;	1.873096447;	1.925888325;	1.87398374;	1.87804878;	1.887983707;	1.915392457;	1.882772681;	1.886734694;	1.897854954;	1.949948927;	1.923391216;	1.918116684;	1.907786885;	1.939425051;	1.926879506;	1.890965732;	1.87987988;	1.887887888;	1.912912913;	1.926853707;	1.938816449;	1.952858576;	1.881526104;	2.008040201;	1.873239437;	1.868686869;	1.885858586;	1.916751269;	1.884028484;	1.929735234;	1.922528033;	1.856994819;	1.895119418;	1.858627859;	1.898216159;	1.836;	1.839;	1.903711133;	2.003024194;	1.878419453;	1.880658436;	1.877846791;	1.986528497;	1.834024896;	1.893193717;	1.816176471;	1.835835836;	1.895791583;	1.846693387;	1.80761523;	1.928427419;	1.952620968;	2.032454361;	1.913705584;	1.885683761;	1.889247312;	1.890928726;	1.77;	1.89;	1.833833834;	1.80980981;	1.85985986;	1.853413655;	1.947791165;	1.942190669;	1.899799599;	1.872361809;	1.891891892;	1.905905906;	1.875251509;	1.797174571;	1.957446809;	1.897795591;	1.827447023;	1.852941176];
mafy=[2.2025854;	2.2181734;	1.94641022;	1.936794194;	2.142518437;	1.847488377;	1.95970794;	2.224628717;	2.270968388;	1.984118372;	2.015932543;	2.217533217;	2.17371875;	1.895762513;	2.161615597;	1.97943593;	2.196323933;	2.181122963;	2.230740296;	2.22543284;	2.257569689;	2.24168685;	2.291182031;	2.168498698;	2.104084693;	2.137224673;	2.136625101;	2.163440831;	2.151736142;	2.146461015;	2.220312792;	2.164483841;	2.158187093;	2.191547963;	2.250006218;	2.182915494;	2.210632755;	2.215407661;	2.278823391;	2.227791726;	2.221441249;	2.220168135;	2.26151961;	2.262879609;	2.258982451;	2.124037487;	2.13443989;	1.872646096;	2.152197846;	2.141193029;	1.832703561;	2.074250452;	2.132037638;	2.123727817;	2.128268131;	2.14825197;	2.12594665;	2.174048271;	2.260649134;	2.211752752;	2.17799943;	2.275008775;	2.230284252;	2.271749685;	2.0542108;	2.0462828;	1.906245537;	2.085410081;	2.203216616;	2.177237449;	2.19737764;	2.218083731;	2.176986307;	2.221770471;	2.116460924;	2.044708859;	2.200946042;	2.088140431;	2.016986122;	2.108689667;	2.136625151;	2.092600558;	2.178854975;	2.263059989;	2.268271129;	2.276699946;	1.9483735;	2.1142535;	2.068558058;	2.017278779;	1.822367868;	2.003957329;	2.010913153;	2.131831136;	1.914748347;	2.163950603;	2.073181381;	2.070446647;	2.04125171;	1.989215136;	1.847574671;	1.949881313;	1.893672603;	2.14939711];
mafz=[1.006532;	1.006532;	1.00654509;	1.006538539;	1.00654509;	1.00654509;	1.006564824;	1.006651731;	1.006747934;	1.006818372;	1.007038793;	1.008156843;	1.008165;	1.008173173;	1.008189569;	1.00820603;	1.008297764;	1.008314664;	1.008340143;	1.008340143;	1.00846114;	1.008487526;	1.008505208;	1.008505208;	1.009867069;	1.009867069;	1.009877016;	1.009927052;	1.009947208;	1.009947208;	1.009947208;	1.010973577;	1.009957317;	1.010995927;	1.009987768;	1.009987768;	1.009997959;	1.010008172;	1.007965271;	1.010008172;	1.010028659;	1.011063525;	1.009032854;	1.010090628;	1.010174455;	1.011442442;	1.011442442;	1.011442442;	1.012455912;	1.012468405;	1.011465396;	1.010472892;	1.011488442;	1.012506036;	1.012556566;	1.011546465;	1.011605076;	1.011628688;	1.0106222;	1.011652396;	1.011845596;	1.012908619;	1.011882536;	1.010945435;	1.013064;	1.012064;	1.01310331;	1.013169355;	1.013236069;	1.013440329;	1.015594203;	1.013537824;	1.012514523;	1.013679581;	1.013722689;	1.015712713;	1.014726453;	1.014726453;	1.014726453;	1.014815524;	1.014815524;	1.01490568;	1.013905584;	1.016770299;	1.015803226;	1.016951404;	1.00633;	1.01633;	1.016346346;	1.015345345;	1.016346346;	1.016395582;	1.016395582;	1.016561866;	1.016996994;	1.018053266;	1.019615616;	1.018614615;	1.020720322;	1.019773966;	1.019854103;	1.021271543;	1.021421796;	1.021530426];
%representative metasedimentary amphibolite garnets (within the range of +/- SD of the mean)  
sedamx=[1.400599401;	1.378;	1.409;	1.319556452;	1.376;	1.42042042;	1.528;	1.393;	1.421;	1.513;	1.515;	1.46953047;	1.34;	1.436;	1.448;	1.398398398;	1.434565435;	1.365365365;	1.468468468;	1.34;	1.262677485;	1.54;	1.423423423;	1.339679359;	1.353367876;	1.431;	1.456;	1.449;	1.43;	1.272;	1.307;	1.399;	1.475;	1.466;	1.408;	1.454;	1.326;	1.358;	1.346;	1.447;	1.52;	1.502;	1.285;	1.352;	1.358739837;	1.290581162;	1.475475475;	1.485485485;	1.29;	1.384;	1.35959596;	1.355;	1.387;	1.36;	1.473;	1.459839357;	1.403;	1.371;	1.461;	1.275;	1.539;	1.45;	1.476;	1.314;	1.274390244;	1.282;	1.38;	1.432;	1.506024096;	1.281;	1.336;	1.380853278;	1.261;	1.314;	1.34;	1.333;	1.449;	1.341;	1.424;	1.466;	1.501;	1.385;	1.37;	1.462;	1.461923848;	1.38;	1.361;	1.334337349;	1.318;	1.329;	1.311043566;	1.355;	1.274;	1.381;	1.382382382;	1.357;	1.319;	1.497;	1.548;	1.368;	1.364;	1.564;	1.547;	1.623];
sedamy=[1.202441608;	1.2979041;	1.2407481;	1.356759526;	1.2193868;	1.07744024;	1.2609548;	1.33081215;	1.29732685;	1.3377402;	1.1853242;	1.137268282;	1.23901625;	1.13509625;	1.13509625;	1.34327953;	1.315486114;	1.192444745;	1.291267568;	1.35217365;	1.326140974;	1.23208835;	1.313806156;	1.317591533;	1.177687409;	1.2984817;	1.2697697;	1.3273484;	1.2182324;	1.31060575;	1.2411711;	1.3562151;	1.1795511;	1.2967498;	1.29559515;	1.16396315;	1.2320885;	1.2071085;	1.22516055;	1.32115255;	1.3494419;	1.13856065;	1.2518727;	1.3429367;	1.367874695;	1.321641333;	1.207470871;	1.203270671;	1.28751275;	1.31595675;	1.346400758;	1.2188101;	1.2264701;	1.16669545;	1.1922528;	1.20519759;	1.21534615;	1.33889555;	1.15876755;	1.2857809;	1.19456225;	1.2159236;	1.2401716;	1.33600895;	1.289411535;	1.2786983;	1.1027663;	1.137829;	1.18085241;	1.30887435;	1.21634635;	1.207103382;	1.2622651;	1.3471331;	1.2020678;	1.28000785;	1.31307055;	1.25533725;	1.29370925;	1.21130525;	1.1599226;	1.26268795;	1.12166395;	1.27943065;	1.176864379;	1.267884;	1.28000805;	1.235919729;	1.16742815;	1.26730695;	1.279016819;	1.288091;	1.2155017;	1.38235105;	1.383734785;	1.27958575;	1.23613115;	1.15068595;	1.14202595;	1.3156488	;1.17089285;	1.1593462;	1.19614095;	1.2476783];
sedamz=[1.004894106;	1.009798;	1.009798;	1.011523185;	1.013064;	1.013077077;	1.013064;	1.014697;	1.017963;	1.019596;	1.019596;	1.021207792;	1.024495;	1.024495;	1.024495;	1.02451952;	1.026101898;	1.029423423;	1.029423423;	1.031027;	1.033123732;	1.034293;	1.034327327;	1.034361723;	1.035536788;	1.035926;	1.034926;	1.039192;	1.039192;	1.040825;	1.043458;	1.042458;	1.042458;	1.045724;	1.047357;	1.047357;	1.04899;	1.04999;	1.053889;	1.052889;	1.054522;	1.063687;	1.067586;	1.068586;	1.06970122;	1.070359719;	1.071923924;	1.072924925;	1.073485;	1.072485;	1.074227273;	1.075118;	1.074118;	1.077751;	1.078384;	1.078698795;	1.080017;	1.086549;	1.086549;	1.088182;	1.089815;	1.091448;	1.091448;	1.093081;	1.093578252;	1.095714;	1.094714;	1.09898;	1.098373494;	1.099613;	1.100613;	1.10469615;	1.106778;	1.106778;	1.111044;	1.115943;	1.118209;	1.121475;	1.123475;	1.122475;	1.124108;	1.125741;	1.126741;	1.129007;	1.130267535;	1.13064;	1.135539;	1.136083333;	1.145337;	1.158401;	1.158832827;	1.1633;	1.165566;	1.167199;	1.168367367;	1.170465;	1.177997;	1.191061;	1.191061;	1.210024;	1.213923;	1.215556;	1.224721;	1.225354];
%representative calc-silicate rocks (within the range of +/- SD of the mean)
calx= [2.948421053;	2.91391129;	2.781059063;	2.475748194;	2.132725431;	2.119119119;	2.136831276;	2.11663286;	2.046487603;	2.134556575;	2.119096509];
caly= [1.088119316;	1.027935534;	1.026456415;	1.076263364;	1.197712513;	1.209207558;	1.217988632;	1.218406744;	1.210539773;	1.207749847;	1.218134138];
calz= [1.001718947;	1.004938508;	1.019955193;	1.053927761;	1.355719352;	1.361254254;	1.36456893;	1.369329615;	1.388006198;	1.392852192;	1.400705339];
hold on 
if mod(length(x)+length(y)+length(z)+length(SPS),4)
    disp('All vectors must be of same length')
    return
end
delete(gca)
if nargin <5
    marker='.';
end
if nargin < 6
    string='SPESSARTINE %'
end
% Define the data range
miv=min(0.1);
mav=max(100);
% Get the current colormap
map= jet;
hold off
grid on
axis equal
% Re-format the colorbar
h=colorbar;
set(h,'ylim',[1 length(map)]);
yal=linspace(1,length(map),10);
set(h,'ytick',yal);
% Create the yticklabels
ytl=linspace(miv,mav,10);
s=char(10,4);
for i=1:10
    if abs(min(log10(abs(ytl)))) <= 3
        B=sprintf('%-4.3f',ytl(i));
    else
        B=sprintf('%-4.2E',ytl(i));
    end
    s(i,1:length(B))=B;
end
set(h,'yticklabel',s,'fontsize',9);
grid 
set(get(h,'title'),'string',string,'fontweight','bold')
view(3)
hold on
interiorPoints= [bluex,bluey,bluez];
DT = DelaunayTri(interiorPoints);  %# create the tetrahedral mesh
hullFacets = convexHull(DT);       %# find the facets of the convex hull
check_blue=inhull(data_array, interiorPoints) %# heck if points are subfield 
center_blue=mean(interiorPoints,1); %# mean of subfield
trisurf(hullFacets,DT.X(:,1),DT.X(:,2),DT.X(:,3),'FaceColor','b','EdgeAlpha',.5,'LineStyle',':')
axis equal; 
hold on 
interiorPoints= [pex,pey,pez]; 
DT = DelaunayTri(interiorPoints);  %# create the tetrahedral mesh
hullFacets = convexHull(DT);       %# find the facets of the convex hull
check_pe=inhull(data_array, interiorPoints)      %# check if points are subfield 
center_pe=mean(interiorPoints,1);               %# mean of subfield
trisurf(hullFacets,DT.X(:,1),DT.X(:,2),DT.X(:,3),'FaceColor','y','EdgeAlpha',.5,'LineStyle',':') 
hold on
interiorPoints= [ecx,ecy,ecz];
DT = DelaunayTri(interiorPoints);  %# create the tetrahedral mesh
hullFacets = convexHull(DT);       %# find the facets of the convex hull
check_ec=inhull(data_array, interiorPoints)  %# check if points are subfield 
center_ec=mean(interiorPoints,1);  %# mean of subfield 
trisurf(hullFacets,DT.X(:,1),DT.X(:,2),DT.X(:,3), 'FaceColor',[0.2 0.8 0.6],'EdgeAlpha',.5,'LineStyle',':')
axis equal; 
interiorPoints= [grex,grey,grez];
DT = DelaunayTri(interiorPoints);  %# create the tetrahedral mesh
hullFacets = convexHull(DT);       %# find the facets of the convex hull
check_gre=inhull(data_array, interiorPoints) %# check if points are subfield
center_gre=mean(interiorPoints,1);  %# mean of subfield 
trisurf(hullFacets,DT.X(:,1),DT.X(:,2),DT.X(:,3),'FaceColor','g','EdgeAlpha',.5,'LineStyle',':')
axis equal; 
hold on
interiorPoints= [metiggrax,metiggray,metiggraz];
DT = DelaunayTri(interiorPoints);  %# create the tetrahedral mesh
hullFacets = convexHull(DT);       %# find the facets of the convex hull
check_metiggra=inhull(data_array, interiorPoints) %# check if points are subfield
center_metiggra=mean(interiorPoints,1); %# mean of subfield
trisurf(hullFacets,DT.X(:,1),DT.X(:,2),DT.X(:,3),'FaceColor',[0 0.5 1],'EdgeAlpha',.5,'LineStyle',':')
axis equal;
hold on 
interiorPoints= [metigamx,metigamy,metigamz];
DT = DelaunayTri(interiorPoints);  %# create the tetrahedral mesh
hullFacets = convexHull(DT);       %# find the facets of the convex hull
check_metigam=inhull(data_array, interiorPoints) %# create the tetrahedral mesh
center_metigam=mean(interiorPoints,1);  %# mean of subfield
trisurf(hullFacets,DT.X(:,1),DT.X(:,2),DT.X(:,3),'FaceColor','m','EdgeAlpha',.5,'LineStyle',':')
axis equal;
hold on
interiorPoints= [mafx,mafy,mafz];
DT = DelaunayTri(interiorPoints);  %# create the tetrahedral mesh
hullFacets = convexHull(DT);       %# find the facets of the convex hull
check_maf=inhull(data_array, interiorPoints) %# check if points are subfield
center_maf=mean(interiorPoints,1);   %#  mean of subfield
trisurf(hullFacets,DT.X(:,1),DT.X(:,2),DT.X(:,3),'FaceColor','r','EdgeAlpha',.5,'LineStyle',':')
axis equal;
hold on
interiorPoints= [sedamx,sedamy,sedamz];
DT = DelaunayTri(interiorPoints);  %# create the tetrahedral mesh
hullFacets = convexHull(DT);       %# find the facets of the convex hull
check_sedam=inhull(data_array, interiorPoints) %# check if points are subfield
center_sedam=mean(interiorPoints,1);%# mean of subfield 
trisurf(hullFacets,DT.X(:,1),DT.X(:,2),DT.X(:,3),'FaceColor','k','EdgeAlpha',.5,'LineStyle',':')
axis equal; 
hold on
interiorPoints= [calx,caly,calz];
DT = DelaunayTri(interiorPoints);  %# create the tetrahedral mesh
hullFacets = convexHull(DT);       %# find the facets of the convex hullmean of subfield
check_cal=inhull(data_array, interiorPoints) %# Check if points are subfield
center_cal=mean(interiorPoints,1); %# mean of subfield
trisurf(hullFacets,DT.X(:,1),DT.X(:,2),DT.X(:,3),'FaceColor','c','EdgeAlpha',.5,'LineStyle',':') 
axis equal; 
hold on
interiorPoints= [sedgrax,sedgray,sedgraz];
DT = DelaunayTri(interiorPoints);  %# Create the tetrahedral mesh
hullFacets = convexHull(DT);       %# Find the facets of the convex hull
check_sedgra=inhull(data_array, interiorPoints) %# Check if points are subfield
center_sedgra=mean(interiorPoints,1); %# mean of subfield
trisurf(hullFacets,DT.X(:,1),DT.X(:,2),DT.X(:,3),'FaceColor','w','EdgeAlpha',.5,'LineStyle',':')
alpha(0.12)
grid 
axis off

annotation('textbox', [.68 .50 .16 .40 ], 'String','(1)*granites & pegmatites (2)*greenschist-facies metasedimentary rocks (3)*amphibolite-facies metasedimentary rocks (4)*amphibolite-facies metaigneous rocks (5)*blueschist-facies metaigneous & metasedimentary rocks (6)*calc-silicate rocks (7)*granulite-facies metasedimentary rocks (8)*granulite-facies metaigneous rocks (9)*eclogite-facies metaigneous & metasedimentary rocks (10)*ultramafic rocks', 'FontSize', 16,'BackgroundColor',[1  1 1],'EdgeColor','none','LineStyle',':','FontWeight','bold');
%annotation('textbox', [.68 .50 .19 .40 ], 'String', '(1) Granites & pegmatites (2) Greenschist-facies metasedimentary rocks (3) Amphibolite-facies metasedimentary rocks (4) Amphibolite-facies metaigneous rocks (5) Blueschist-facies metaigneous & metasedimentary rocks (6) Calc-silicate rocks (7)Granulite-facies metasedimentary rocks (8) Granulite-facies metaigneous rocks (9) Eclogite-facies metaigneous & metasedimentary rocks (10) Ultramafic rocks', 'FontSize', 17,'BackgroundColor',[1  1 1],'EdgeColor','none','LineStyle',':','FontWeight','bold');
%annotation('textbox', [.65 .55 .18 .35 ], 'String', '(1) Granites & pegmatites (2) Metasedimentary greenschists (3) Metasedimentary amphibolites (4) Metaigneous amphibolites (5) Metaigneous & metasedimentary blueschists (6) Calc-silicate rocks (7) Metasedimentary granulites (8) Metaigneous granulites (9) Metaigneous & metasedimentary eclogites (10) Ultramafic rocks', 'FontSize', 17,'BackgroundColor',[1  1 1],'EdgeColor','none','LineStyle',':','FontWeight','bold');

hold on
centers=[center_blue;center_pe;center_ec;center_gre;center_metiggra;center_metigam;center_maf;center_sedam;center_cal;center_sedgra]
subfield_names=[{'blue'};{'pe'};{'ec'};{'gre'};{'metiggra'};{'metigam'};{'maf'};{'sedam'};{'cal'};{'sedgra'}]
dist_to_centers=zeros(length(x),length(centers));
dist_mins=cell(length(x),2);
marker='o';
% Plot the points
hold on
for i=1:length(x)
    in=round((SPS(i)-miv)*(length(map)-1)/(mav-miv));
    %--- Catch the out-of-range numbers
    if in==0;in=1;end
    if in > length(map);in=length(map);end
    %plot3(x(i),y(i),z(i),marker,'color',map(in,:),'markerfacecolor', map(in,:),'MarkerSize',10)
    if check_blue(i)==1 || check_pe(i)==1 || check_ec(i)==1 || check_gre(i)==1 || check_metiggra(i)==1 || check_metigam(i)==1 || check_maf(i)==1 || check_sedam(i)==1 || check_cal(i)==1 || check_sedgra(i)==1
        plot3(x(i),y(i),z(i),marker,'color',map(in,:),'markerfacecolor', map(in,:),'MarkerSize',7)
    else plot3(x(i),y(i),z(i),marker,'color',map(in,:),'MarkerSize',7,'LineWidth',3)
        colormap jet
    end
    %calculate the distance to the centers of all subfields and determine
    %the closest for each data point
    for c=1:length(centers)
        dist=data_array(i,:)-centers(c,:);
        dist_to_centers(i,c)=sqrt(sum(dist.^2,2));        
    end
    [dist_min, dist_min_idx]=min(dist_to_centers(i,:));
    dist_mins(i,1)=subfield_names(dist_min_idx);
    dist_mins(i,2)={dist_min}; 
    %plot a red line between each data point and the closest subfield
    %center
    plot3([x(i),centers(dist_min_idx,1)],[y(i),centers(dist_min_idx,2)],[z(i),centers(dist_min_idx,3)],':r','LineWidth',1.9)       
    end  
plot3(x1,y1,z1,'k')
%plot the centers of the subfields as red points
for i=1:length(centers(:,1))
    plot3(centers(i,1),centers(i,2),centers(i,3),'.r','MarkerSize',14)
plot3 (x37,y37,z37, 'k', 'linewidth',1.5)
text(x2,y2,z2,'Alm','FontWeight','bold','FontSize',14)
text(x3,y3,z3,'Grs','FontWeight','bold','FontSize',14)
text(x4,y4,z4, 'Prp ','FontWeight','bold','FontSize',14)
text(x5,y5,z5, 'Sps','FontWeight','bold','FontSize',14)
plot3(x6,y6,z6, '--k', 'linewidth',0.2)
text(x7,y7,z7, '50%','Color','k', 'FontSize',11)
text(x8,y8,z8, '50%','Color','k', 'FontSize',11)
text(x9,y9,z9, '50%','Color','k', 'FontSize',11)
text(x10,y10,z10, '50%','Color','k', 'FontSize',11)
text(x11,y11,z11, '50%','Color','k', 'FontSize',11)
text(x12,y12,z12, '50%','Color','k', 'FontSize',11)
text(x13,y13,z13, '75%','Color','k', 'FontSize',11)
text(x14,y14,z14, '75%','Color','k', 'FontSize',11)
text(x15,y15,z15, '75%','Color','k', 'FontSize',11)
text(x16,y16,z16, '75%','Color','k', 'FontSize',11)
text(x17,y17,z17, '75%','Color','k', 'FontSize',11)
text(x18,y18,z18, '75%','Color','k', 'FontSize',11)
text(x19,y19,z19, '75%','Color','k', 'FontSize',11)
text(x20,y20,z20, '75%','Color','k', 'FontSize',11)
text(x21,y21,z21, '75%','Color','k', 'FontSize',11)
text(x22,y22,z22, '75%','Color','k', 'FontSize',11)
text(x23,y23,z23, '75%','Color','k', 'FontSize',11)
text(x24,y24,z24, '75%','Color','k', 'FontSize',11)
text(x25,y25,z25, '87.5%','Color','k', 'FontSize',8)
text(x26,y26,z26,'62.5%','Color','k', 'FontSize',8)
text(x27,y27,z27,'62.5%','Color','k', 'FontSize',8)
text(x28,y28,z28,'87.5%','Color','k', 'FontSize',8)
text(x29,y29,z29,'87.5%','Color','k', 'FontSize',8)
text(x30,y30,z30,'87.5%','Color','k', 'FontSize',8)
text(x31,y31,z31,'62.5%','Color','k', 'FontSize',8)
text(x32,y32,z32,'62.5%','Color','k', 'FontSize',8)
text(x33,y33,z33,'62.5%','Color','k', 'FontSize',8)
text(x34,y34,z34,'62.5%','Color','k', 'FontSize',8)
text(x35,y35,z35,'87.5%','Color','k', 'FontSize',8)
text(x36,y36,z36,'87.5%','Color','k', 'FontSize',8)
text(x42,y42,z42, '1','Color','k','FontSize',14,'FontWeight','bold')
text(x43,y43,z43, '2','Color','k','FontSize', 14,'FontWeight','bold' )
text(x44,y44,z44,'3','Color','k','FontSize', 14,'FontWeight','bold')
text(x45,y45,z45,'5','Color','k','FontSize', 14,'FontWeight','bold')
text(x46,y46,z46,'6','Color','k','FontSize', 14,'FontWeight','bold')
text(x47,y47,z47,'7','Color','k','FontSize', 14,'FontWeight','bold')
text(x48,y48,z48,'8','Color','k','FontSize', 14,'FontWeight','bold')
text(x49,y49,z49,'4','Color','k','FontSize', 14,'FontWeight','bold')
text(x50,y50,z50,'9','Color','k','FontSize', 14,'FontWeight','bold')
text(x51,y51,z51,'10','Color','k','FontSize', 14,'FontWeight','bold')
grid 
caxis([1, 100])
colorbar
h = colorbar;
ylabel(h, '');
title(h,'Spessartine mol. %','FontSize', 11,'FontWeight','bold')
end
 
 
 
 


