% convert_input_sounding.m
% Reads data from LASSO input sounding (CRM format) and converts it
% to SCM format.
% Requires mmddstr (mmdd) and simulation number string (xx) 
% as input to find the right files.

ter_hgt = 320;   % Terrain height (ARM SGP = 320m)

% Input data
addpath(['2015', mmddstr, '/sim00', simstr, '/config']);
disp(['2015', mmddstr, '/sim00', simstr, '/config']);
% Read first line
ifid = fopen('input_sounding','r');
ofid = fopen(['input_sounding_', mmddstr, '_sim00', simstr],'w');
ibuf1 = fscanf(ifid,'%f %f %f',[1 3]);

% Read rest of file
ibuf = fscanf(ifid,'%f %f %f %f %f\n',[5 inf]);
ibuf = ibuf';
[nr,nc] = size(ibuf);

% Write first line (rearranged)
fprintf(ofid, '%5.1f %4.1f %4.1f %6.2f %6.4f %7.1f\n', ter_hgt, ibuf(1,4), ibuf(1,5), ibuf1(1,2), ibuf1(1,3)./1000, ibuf1(1,1).*100);

% Write rest of file (rearranged)
for i = 1:nr
	fprintf(ofid, '%7.2f %4.1f %4.1f %6.2f %6.4f\n', ibuf(i,1)+ter_hgt, ibuf(i,4), ibuf(i,5), ibuf(i,2), ibuf(i,3)./1000);
end

fclose(ifid);
fclose(ofid);
rmpath(['2015', mmddstr, '/sim00', simstr, '/config']);
