%conversione dati di vento
%versione: 28/04/2009


%stampa il'avviso che verra cercato il campo del vento
%cerca il file contenente il campo di vento
ca
disp('Welcome to wind conversion system')
disp('_')
disp('You shall now be asked to search the files (both U and V)  and I will upload them. Lay back!')
disp('Manifest yourself when ready...')
pause
clc
%cerca la prima componente
[FileName1,PathName1] =uigetfile( ...
{  '*.dat','Data files (*.dat)'; ...
   '*.txt','Text Files (*.txt)'; ...
   '*.*',  'All Files (*.*)'}, ...
   'where the hell is the wind field??', ...
   'MultiSelect', 'on');
pause(1)

m=str2double(FileName1(10:11));
n=datenum(2008, m, 1);
disp(['You choose follwing files: ',FileName1])

disp(['And the month is: ',datestr(n,3)])
disp('Ok! file uploaded! Are you ready for uploading the second component? Press any Key')
pause

windc1=importdata(fullfile(PathName1,FileName1));%carica la prima componente
[FileName2,PathName2] =uigetfile( ... %cerca la seconda componente
{  '*.dat','Data files (*.dat)'; ...
   '*.txt','Text Files (*.txt)'; ...
   '*.*',  'All Files (*.*)'}, ...
   'where the hell is the wind field??', ...
   'MultiSelect', 'on');
windc2=importdata(fullfile(PathName2,FileName2)); %carica la seconda componente
list1=whos('-regexp','FileName1');
list2=whos('-regexp','FileName2');
c1=FileName1(1);
c2=FileName2(1);
if (c1==c2 | PathName1~=PathName2)
    error('You have been uploading the same component twice or been uploading files allocated in different folders! ')
else

disp('_')
disp(['You choose follwing files: ',FileName2])


disp(['And the month is: ',datestr(n,3)])

% windc=ans;
% clear ans
windc1=transpose(windc1);
windc2=transpose(windc2);
%linearized string of wind values whose number of elements equals the product of rows times coloumns 
string1=reshape(windc1,numel(windc1),1);%wind linearized!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
string2=reshape(windc2,numel(windc2),1);%wind linearized!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

disp('_')
disp('_')
disp('Nothing has gone wrong. You might want to execute some cross checks to confirm all is right!')

clear PathName1 PathName2
whos

disp('Now that wind matrix has been transformed into a line of numbers, I am going to split each component.')
disp('Error Flag is: 999. It delimitates end/start between tow different values (size=1*69)')
disp('Tell me when you are ready')
pause 
clc
% 
% 
% %sostituisco gli "NaN" con dei numeri altissimi (flag error)
[x]=isnan(string1);
[y]=find(x==1);
 string1(y)=999;
 string2(y)=999;
 clear x
 clear y
 clc
 disp('_')
disp('_')
disp('MM5 outputs vecotrs in following format: NR_of_Col=69+1NaN=70; Nr_of_lines=90')
fprintf(1,'Number of elements of the wind STRING:  %1.0f \n',numel(string1))
fprintf(1,'Assuming that each coloumn has 70 elemtns (=69+NaN ), the number of lines of the new matrix will be:  %1.0f \n',numel(string1)/70)
windM1=transpose(reshape(string1,70,numel(string1)/70));
windM2=transpose(reshape(string2,70,numel(string2)/70));

disp('Ok! The above descrbied matrix has been created! ')
fprintf(1,'Its size ("Nr. of lines" times "Nr. of coloumns") is, in fact, consistent:  %1.0f %1.0f \n',size(windM1))

whos
disp('-')
disp('===>>>The windmatrix are named: "windM1 and windM2"<<<=== ') 
disp('Press any key when ready')
pause
disp('-')
disp('-')
disp('-')

fprintf(1,'Note the tail of each line of the matrix (eg. last elements):  %1.4f %1.4f %1.4f %1.4f %1.4f %1.4f \n',windM1(2,65:70))
fprintf(1,'Note the tail of each line of the matrix (eg. last elements):  %1.4f %1.4f %1.4f %1.4f %1.4f %1.4f \n',windM1(3,65:70))
fprintf(1,'Note the tail of each line of the matrix (eg. last elements):  %1.4f %1.4f %1.4f %1.4f %1.4f %1.4f \n',windM1(4,65:70))
fprintf(1,'Note the tail of each line of the matrix (eg. last elements):  %1.4f %1.4f %1.4f %1.4f %1.4f %1.4f \n',windM1(5,65:70))
fprintf(1,'Note the tail of each line of the matrix (eg. last elements):  %1.4f %1.4f %1.4f %1.4f %1.4f %1.4f \n',windM1(6,65:70))
disp('Flag error----------->---------->------------->----------->--------->---------->----------->-----------|________|')
disp('_')
disp('_')
disp('_')
disp('_')
disp('The error flag shown above needs to be deletd and AFTER THAT we will obtain the clean matrix')
disp('containing the set of x- (or y-) components. Cutting this matrix at multiple integers of "Nr_of_line"')
disp('We will split the matrix down into each component ')
disp('_')
disp('_')
disp('_')
disp('Let us do that!!')
disp('Press any key when ready')
pause
clc
%windM=windM(:,1:69);
disp('_')
disp('_')
disp('_')
disp('Ok. Matrix has been unpadded!')
fprintf(1,'Note the tail of each line of the matrix (eg. last elements):  %1.4f %1.4f %1.4f %1.4f %1.4f %1.4f \n',windM1(1,64:69))
disp('Matrix is no longer 9999 padded and ready to be split. ')
disp('Recall: MM5 outputs vecotrs in following format: NR_of_Col=69; Nr_of_lines=90. No more Nan-s ')
disp('Given matrix windM will be split with steps of 90 lines. ')
fprintf(1,'Number of lines (y) of windM matrix:  %1.0f \n',numel(windM1)/69)
fprintf(1,'Number of submatrixes (compononets) I will create:  %1.0f \n',numel(windM1)/(69*90))
disp('_')
disp('_')
disp('_')
disp('I am going to begin creation of each component. Tell me when you are ready by pressing any key')
pause
clc
disp('_')
disp('_')
disp('_')
disp('_')
disp('_')
disp('_')




      
for i=91:90:20970
st1 = [c1,'_',datestr(n,3),'_1','=windM1(1:90,:);'];%componente 1: prima matrice costruita manualemnte
s1 = [c1,'_',datestr(n,3),'_',int2str(((i-1)/90)+1),'=windM1(',int2str(i),':',int2str(i+89),',:);'];%le restanti sono create in automatico
%    "nome-componente"_"nome-mese"_"numero-SUBmatrice" = " windM(ogni 90 linee: ogni colonna (x=1:end))


st2 = [c2,'_',datestr(n,3),'_1','=windM2(1:90,:);']; %componente 2
s2 = [c2,'_',datestr(n,3),'_',int2str(((i-1)/90)+1),'=windM2(',int2str(i),':',int2str(i+89),',:);'];


%creating matrixes for preallocation of both components (NaN padded)
createm=['UandV_',datestr(n,3),'_1=NaN(180,70);']; %create first matrix manually (number of coloumns=70, of lines=U+V=2*90=180)
create=[c1,'and',c2,'_',datestr(n,3),'_',int2str(((i-1)/90)+1),'=NaN(180,70);'];%and other automatically 

fill_U_Man=['UandV',datestr(n,3),'(1:90,:)=U_',datestr(n,3),'_1;'];
fill_V_Man=['UandV',datestr(n,3),'(91:180,:)=V_',datestr(n,3),'_1;'];
fill_with_U=[c1,'and',c2,'_',datestr(n,3),'_',int2str(((i-1)/90)+1),'(1:90,:)=',c1,'_',datestr(n,3),'_',int2str(((i-1)/90)+1),';'];%U component pads the top
fill_with_V=[c1,'and',c2,'_',datestr(n,3),'_',int2str(((i-1)/90)+1),'(91:180,:)=',c2,'_',datestr(n,3),'_',int2str(((i-1)/90)+1),';'];%V component pads the Bottom

%generating a list of names (to be then saved as ASCII file for SWAN): 
preallocate_list=['names=NaN(1,233)'];


%strat execution (i.e. eval) of set of couples implemented above 


eval(st1) 
eval(s1)


eval(s2)%valuta componente 2
eval(st2)
eval(createm)%execute (i.e. create) first matrix
eval(create)%execute (i.e. create) remaning set of matrixes for preallocation (NaN padded)

%evaluate (i.e. execute) merging of U and V components, for each month for each couple of components.  
eval(fill_U_Man)
eval(fill_V_Man)
eval(fill_with_U)
eval(fill_with_V)

%evaluate (i.e. execute) creation of list of names (saved as an ASCII file for SWAN).  


%eval(listingtm)
eval(listing)
end

disp('_')
disp('_')
disp('_')
disp('_')
disp('_')
disp('_')
disp('Congratulations! You have just been  partitioning the given matrix and splitting it up into many pieces. ')
disp('Dont fear! It sounds odd but that was exactely what you wanted to do. Use these data wisely!')
disp(['You have been converting BOTH components for:   ',datestr(n,3)])

disp('_')
disp('_')
disp('_')
disp('_')
disp('_')
disp('_')
disp('Your files are still and ONLY in MatLabs workspace: they still need to be exported to Hard Disk! ')
disp('Note! If you want to see a list of your variables input "whos" and press enter ')

clear c1 i list1 m1 n1 s1 st1 string1 windM1 windc1 FileName1      
clear c2 i list2 m2 n2 s2 st2 string2 windM2 windc2 FileName2     

end% chiude il ciclo: in caso di differenti componenti uploadate continua e fa il suo lavoro arrivando fun qui. 


