function [output]=raw2xyz(filename,instr,area);
% This script reads the raw data file of the total station 
% or the RTKGPS and saves it in a XYZ matlab variable. 
%
%
%
%INPUT:  The name of the file you want to read (entry is a string) 
%
%        the type of instrument used for the data collection. 
%        instr = 1 for the Total station
%        instr = 2 for the RTKGPS
%
%        the area where the data were collected (4 characters string)
%        that surves as a key for the area (i.e camp for camposoto and 
%        cort for Cortadura).

%OUTPUT: A matlab variable in the form of XYZ that is stored as  
%        a .mat file 

%   ***********************************************************************
%   *** IMPORTANT: THE SCRIPT READS AND WRITES IN THE CURRENT DIRECTORY ***
%   ***********************************************************************


switch instr
    case 1
        [num,x,y,z,code]=textread(filename ,'%f %f %f %f %s');
        output = [x,y,z+0.355]; % +0.355 elevation adjustment for the Cadiz hydrographic zero
    case 2
        A = xlsread(filename);
        pts=find(A(:,7)>0 & A(:,12)<=0.05);  % eliminate data with large error and position of the antenna 
        output = [A(pts,3),A(pts,2),A(pts,4)+43.556]; % +43.556 elevation adjustment for the Cadiz hydrographic zero
end

dir = pwd;
f = filesep;
lchar = max(strfind(dir,f));
fw = dir(lchar+3:end);

name = [area num2str(fw)];
makename = [name '= output;'];
eval(makename)

comsave = ['save(''' name '.mat'', ''' name ''');'];
eval(comsave)