%% setting convertion

s2y=3600*24*365;
tran2C=(12.0107/1000000000000*6.625); % 1/1000Gt C, teragram
tran2N=1/1000000000000000*14; %Pg N

tran=1%tran2C%1;


%% loading data from txt files

%% ODZ only
filename = '/Volumes/BM_2017_36/UVicWorking/2.9_ln_iron/transient_4_cali/featureNumberScrips/regions/omz/NoFe_O_ind.txt';
formatSpec = '%*15s%11f%9f%10f%10f%11f%9f%11f%10f%9f%9f%10f%11f%9f%f%11f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'TextType', 'string',  'ReturnOnError', false);
fclose(fileID);
dataArray{:,16}=[];
NoFe_omz_ind=cell2mat(dataArray).*tran.*s2y;
clearvars filename formatSpec fileID dataArray ans;
filename = '/Volumes/BM_2017_36/UVicWorking/2.9_ln_iron/transient_4_cali/featureNumberScrips/regions/omz/NoFe_O_pa.txt';
formatSpec = '%*15s%11f%9f%10f%10f%11f%9f%11f%10f%9f%9f%10f%11f%9f%f%11f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'TextType', 'string',  'ReturnOnError', false);
fclose(fileID);
dataArray{:,16}=[];
NoFe_omz_pa=cell2mat(dataArray).*tran.*s2y;
clearvars filename formatSpec fileID dataArray ans;
filename = '/Volumes/BM_2017_36/UVicWorking/2.9_ln_iron/transient_4_cali/featureNumberScrips/regions/omz/NoFe_O_atl.txt';
formatSpec = '%*15s%11f%9f%10f%10f%11f%9f%11f%10f%9f%9f%10f%11f%9f%f%11f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'TextType', 'string',  'ReturnOnError', false);
fclose(fileID);
dataArray{:,16}=[];
NoFe_omz_atl=cell2mat(dataArray).*tran.*s2y;
clearvars filename formatSpec fileID dataArray ans;

filename = '/Volumes/BM_2017_36/UVicWorking/2.9_ln_iron/transient_4_cali/featureNumberScrips/regions/omz/FeMask_O_ind.txt';
formatSpec = '%*15s%11f%9f%10f%10f%11f%9f%11f%10f%9f%9f%10f%11f%9f%f%11f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'TextType', 'string',  'ReturnOnError', false);
fclose(fileID);
dataArray{:,16}=[];
FeMask_omz_ind=cell2mat(dataArray).*tran.*s2y;
clearvars filename formatSpec fileID dataArray ans;
filename = '/Volumes/BM_2017_36/UVicWorking/2.9_ln_iron/transient_4_cali/featureNumberScrips/regions/omz/FeMask_O_pa.txt';
formatSpec = '%*15s%11f%9f%10f%10f%11f%9f%11f%10f%9f%9f%10f%11f%9f%f%11f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'TextType', 'string',  'ReturnOnError', false);
fclose(fileID);
dataArray{:,16}=[];
FeMask_omz_pa=cell2mat(dataArray).*tran.*s2y;
clearvars filename formatSpec fileID dataArray ans;
filename = '/Volumes/BM_2017_36/UVicWorking/2.9_ln_iron/transient_4_cali/featureNumberScrips/regions/omz/FeMask_O_atl.txt';
formatSpec = '%*15s%11f%9f%10f%10f%11f%9f%11f%10f%9f%9f%10f%11f%9f%f%11f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'TextType', 'string',  'ReturnOnError', false);
fclose(fileID);
dataArray{:,16}=[];
FeMask_omz_atl=cell2mat(dataArray).*tran.*s2y;
clearvars filename formatSpec fileID dataArray ans;


filename = '/Volumes/BM_2017_36/UVicWorking/2.9_ln_iron/transient_4_cali/featureNumberScrips/regions/omz/FeDyn1_O_ind.txt';
formatSpec = '%*15s%11f%9f%10f%10f%11f%9f%11f%10f%9f%9f%10f%11f%9f%f%11f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'TextType', 'string',  'ReturnOnError', false);
fclose(fileID);
dataArray{:,16}=[];
FeDyn1_omz_ind=cell2mat(dataArray).*tran.*s2y;
clearvars filename formatSpec fileID dataArray ans;
filename = '/Volumes/BM_2017_36/UVicWorking/2.9_ln_iron/transient_4_cali/featureNumberScrips/regions/omz/FeDyn1_O_pa.txt';
formatSpec = '%*15s%11f%9f%10f%10f%11f%9f%11f%10f%9f%9f%10f%11f%9f%f%11f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'TextType', 'string',  'ReturnOnError', false);
fclose(fileID);
dataArray{:,16}=[];
FeDyn1_omz_pa=cell2mat(dataArray).*tran.*s2y;
clearvars filename formatSpec fileID dataArray ans;
filename = '/Volumes/BM_2017_36/UVicWorking/2.9_ln_iron/transient_4_cali/featureNumberScrips/regions/omz/FeDyn1_O_atl.txt';
formatSpec = '%*15s%11f%9f%10f%10f%11f%9f%11f%10f%9f%9f%10f%11f%9f%f%11f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'TextType', 'string',  'ReturnOnError', false);
fclose(fileID);
dataArray{:,16}=[];
FeDyn1_omz_atl=cell2mat(dataArray).*tran.*s2y;
clearvars filename formatSpec fileID dataArray ans;


%% fefluxes
s2y2=365*3600*24/1e6;
s2y_fe=[1e-6,1e-6,s2y2,s2y2,s2y2,s2y2,s2y2,s2y2,s2y2,s2y2,1,1,1,1,1];

filename = '/Volumes/BM_2017_36/UVicWorking/2.9_ln_iron/transient_4_cali/featureNumberScrips/regions/omz/FeDyn1_O_ind_invFe.txt';
formatSpec = '%*15s%11f%9f%10f%10f%11f%9f%11f%10f%9f%9f%10f%11f%9f%f%11f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'TextType', 'string',  'ReturnOnError', false);
fclose(fileID);
dataArray{:,16}=[];
FeDyn1_omz_ind_invFe=cell2mat(dataArray).*s2y_fe;
clearvars filename formatSpec fileID dataArray ans;
filename = '/Volumes/BM_2017_36/UVicWorking/2.9_ln_iron/transient_4_cali/featureNumberScrips/regions/omz/FeDyn1_O_pa_invFe.txt';
formatSpec = '%*15s%11f%9f%10f%10f%11f%9f%11f%10f%9f%9f%10f%11f%9f%f%11f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'TextType', 'string',  'ReturnOnError', false);
fclose(fileID);
dataArray{:,16}=[];
FeDyn1_omz_pa_invFe=cell2mat(dataArray).*s2y_fe;
clearvars filename formatSpec fileID dataArray ans;
filename = '/Volumes/BM_2017_36/UVicWorking/2.9_ln_iron/transient_4_cali/featureNumberScrips/regions/omz/FeDyn1_O_atl_invFe.txt';
formatSpec = '%*15s%11f%9f%10f%10f%11f%9f%11f%10f%9f%9f%10f%11f%9f%f%11f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'TextType', 'string',  'ReturnOnError', false);
fclose(fileID);
dataArray{:,16}=[];
FeDyn1_omz_atl_invFe=cell2mat(dataArray).*s2y_fe;
clearvars filename formatSpec fileID dataArray ans;


%% OMZ only inventory of phyt, diaz,zoop, detr

mN2MmolFe=tran2C%(1e-6)*(10.e-6)*6.625;
filename = '/Volumes/BM_2017_36/UVicWorking/2.9_ln_iron/transient_4_cali/featureNumberScrips/regions/omz/NoFe_O_ind_inv.txt';
formatSpec = '%*15s%11f%9f%10f%10f%11f%9f%11f%10f%9f%9f%10f%11f%9f%f%11f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'TextType', 'string',  'ReturnOnError', false);
fclose(fileID);
dataArray{:,16}=[];
NoFe_omz_ind_inv=cell2mat(dataArray).*mN2MmolFe;
clearvars filename formatSpec fileID dataArray ans;
filename = '/Volumes/BM_2017_36/UVicWorking/2.9_ln_iron/transient_4_cali/featureNumberScrips/regions/omz/NoFe_O_pa_inv.txt';
formatSpec = '%*15s%11f%9f%10f%10f%11f%9f%11f%10f%9f%9f%10f%11f%9f%f%11f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'TextType', 'string',  'ReturnOnError', false);
fclose(fileID);
dataArray{:,16}=[];
NoFe_omz_pa_inv=cell2mat(dataArray).*mN2MmolFe;
clearvars filename formatSpec fileID dataArray ans;
filename = '/Volumes/BM_2017_36/UVicWorking/2.9_ln_iron/transient_4_cali/featureNumberScrips/regions/omz/NoFe_O_atl_inv.txt';
formatSpec = '%*15s%11f%9f%10f%10f%11f%9f%11f%10f%9f%9f%10f%11f%9f%f%11f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'TextType', 'string',  'ReturnOnError', false);
fclose(fileID);
dataArray{:,16}=[];
NoFe_omz_atl_inv=cell2mat(dataArray).*mN2MmolFe;
clearvars filename formatSpec fileID dataArray ans;

filename = '/Volumes/BM_2017_36/UVicWorking/2.9_ln_iron/transient_4_cali/featureNumberScrips/regions/omz/FeMask_O_ind_inv.txt';
formatSpec = '%*15s%11f%9f%10f%10f%11f%9f%11f%10f%9f%9f%10f%11f%9f%f%11f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'TextType', 'string',  'ReturnOnError', false);
fclose(fileID);
dataArray{:,16}=[];
FeMask_omz_ind_inv=cell2mat(dataArray).*mN2MmolFe;
clearvars filename formatSpec fileID dataArray ans;
filename = '/Volumes/BM_2017_36/UVicWorking/2.9_ln_iron/transient_4_cali/featureNumberScrips/regions/omz/FeMask_O_pa_inv.txt';
formatSpec = '%*15s%11f%9f%10f%10f%11f%9f%11f%10f%9f%9f%10f%11f%9f%f%11f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'TextType', 'string',  'ReturnOnError', false);
fclose(fileID);
dataArray{:,16}=[];
FeMask_omz_pa_inv=cell2mat(dataArray).*mN2MmolFe;
clearvars filename formatSpec fileID dataArray ans;
filename = '/Volumes/BM_2017_36/UVicWorking/2.9_ln_iron/transient_4_cali/featureNumberScrips/regions/omz/FeMask_O_atl_inv.txt';
formatSpec = '%*15s%11f%9f%10f%10f%11f%9f%11f%10f%9f%9f%10f%11f%9f%f%11f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'TextType', 'string',  'ReturnOnError', false);
fclose(fileID);
dataArray{:,16}=[];
FeMask_omz_atl_inv=cell2mat(dataArray).*mN2MmolFe;
clearvars filename formatSpec fileID dataArray ans;

filename = '/Volumes/BM_2017_36/UVicWorking/2.9_ln_iron/transient_4_cali/featureNumberScrips/regions/omz/FeDyn1_O_ind_inv.txt';
formatSpec = '%*15s%11f%9f%10f%10f%11f%9f%11f%10f%9f%9f%10f%11f%9f%f%11f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'TextType', 'string',  'ReturnOnError', false);
fclose(fileID);
dataArray{:,16}=[];
FeDyn1_omz_ind_inv=cell2mat(dataArray).*mN2MmolFe;
clearvars filename formatSpec fileID dataArray ans;
filename = '/Volumes/BM_2017_36/UVicWorking/2.9_ln_iron/transient_4_cali/featureNumberScrips/regions/omz/FeDyn1_O_pa_inv.txt';
formatSpec = '%*15s%11f%9f%10f%10f%11f%9f%11f%10f%9f%9f%10f%11f%9f%f%11f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'TextType', 'string',  'ReturnOnError', false);
fclose(fileID);
dataArray{:,16}=[];
FeDyn1_omz_pa_inv=cell2mat(dataArray).*mN2MmolFe;
clearvars filename formatSpec fileID dataArray ans;
filename = '/Volumes/BM_2017_36/UVicWorking/2.9_ln_iron/transient_4_cali/featureNumberScrips/regions/omz/FeDyn1_O_atl_inv.txt';
formatSpec = '%*15s%11f%9f%10f%10f%11f%9f%11f%10f%9f%9f%10f%11f%9f%f%11f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'TextType', 'string',  'ReturnOnError', false);
fclose(fileID);
dataArray{:,16}=[];
FeDyn1_omz_atl_inv=cell2mat(dataArray).*mN2MmolFe;
clearvars filename formatSpec fileID dataArray ans;


%% organize matrix for iron fluxes.

% regional biological

FeDyn1_omz_atl_fe=FeDyn1_omz_atl*(10.e-6)*6.625 *365*24*3600/tran/1e6;
FeDyn1_omz_pa_fe=FeDyn1_omz_pa*(10.e-6)*6.625 *365*24*3600/tran/1e6;
FeDyn1_omz_ind_fe=FeDyn1_omz_ind*(10.e-6)*6.625 *365*24*3600/tran/1e6;

% iron matrix


FeDyn1_omz_atl_femat=[FeDyn1_omz_atl_invFe(:,1:8),...
    FeDyn1_omz_atl_invFe(:,9)-FeDyn1_omz_atl_invFe(:,5),...
    FeDyn1_omz_atl_invFe(:,10),...
    FeDyn1_omz_atl_fe(:,1)+FeDyn1_omz_atl_fe(:,2),...
    FeDyn1_omz_atl_fe(:,11),FeDyn1_omz_atl_fe(:,12),...
    FeDyn1_omz_atl_fe(:,8)+FeDyn1_omz_atl_fe(:,9)+FeDyn1_omz_atl_fe(:,10)+FeDyn1_omz_atl_fe(:,12)-FeDyn1_omz_atl_fe(:,7),...
    [0;diff(FeDyn1_omz_atl_invFe(:,1))],...
    -[0;diff(FeDyn1_omz_atl_invFe(:,1))]+FeDyn1_omz_atl_fe(:,1)+FeDyn1_omz_atl_fe(:,2)+FeDyn1_omz_atl_invFe(:,3)+FeDyn1_omz_atl_invFe(:,4)-FeDyn1_omz_atl_invFe(:,9)-FeDyn1_omz_atl_fe(:,12)-FeDyn1_omz_atl_fe(:,11)-FeDyn1_omz_atl_invFe(:,10),...
    FeDyn1_omz_atl_inv(:,1)+FeDyn1_omz_atl_inv(:,2)+FeDyn1_omz_atl_inv(:,4)];
FeDyn1_omz_pa_femat=[FeDyn1_omz_pa_invFe(:,1:8),...
    FeDyn1_omz_pa_invFe(:,9)-FeDyn1_omz_pa_invFe(:,5),...
    FeDyn1_omz_pa_invFe(:,10),...
    FeDyn1_omz_pa_fe(:,1)+FeDyn1_omz_pa_fe(:,2),...
    FeDyn1_omz_pa_fe(:,11),FeDyn1_omz_pa_fe(:,12),...
    FeDyn1_omz_pa_fe(:,8)+FeDyn1_omz_pa_fe(:,9)+FeDyn1_omz_pa_fe(:,10)+FeDyn1_omz_pa_fe(:,12)-FeDyn1_omz_pa_fe(:,7),...
    [0;diff(FeDyn1_omz_pa_invFe(:,1))],...
    -[0;diff(FeDyn1_omz_pa_invFe(:,1))]+FeDyn1_omz_pa_fe(:,1)+FeDyn1_omz_pa_fe(:,2)+FeDyn1_omz_pa_invFe(:,3)+FeDyn1_omz_pa_invFe(:,4)-FeDyn1_omz_pa_invFe(:,9)-FeDyn1_omz_pa_fe(:,12)-FeDyn1_omz_pa_fe(:,11)-FeDyn1_omz_pa_invFe(:,10),...
    FeDyn1_omz_pa_inv(:,1)+FeDyn1_omz_pa_inv(:,2)+FeDyn1_omz_pa_inv(:,4)];
FeDyn1_omz_ind_femat=[FeDyn1_omz_ind_invFe(:,1:8),...
    FeDyn1_omz_ind_invFe(:,9)-FeDyn1_omz_ind_invFe(:,5),...
    FeDyn1_omz_ind_invFe(:,10),...
    FeDyn1_omz_ind_fe(:,1)+FeDyn1_omz_ind_fe(:,2),...
    FeDyn1_omz_ind_fe(:,11),FeDyn1_omz_ind_fe(:,12),...
    FeDyn1_omz_ind_fe(:,8)+FeDyn1_omz_ind_fe(:,9)+FeDyn1_omz_ind_fe(:,10)+FeDyn1_omz_ind_fe(:,12)-FeDyn1_omz_ind_fe(:,7),...
    [0;diff(FeDyn1_omz_ind_invFe(:,1))],...
    -[0;diff(FeDyn1_omz_ind_invFe(:,1))]+FeDyn1_omz_ind_fe(:,1)+FeDyn1_omz_ind_fe(:,2)+FeDyn1_omz_ind_invFe(:,3)+FeDyn1_omz_ind_invFe(:,4)-FeDyn1_omz_ind_invFe(:,9)-FeDyn1_omz_ind_fe(:,12)-FeDyn1_omz_ind_fe(:,11)-FeDyn1_omz_ind_invFe(:,10),...
    FeDyn1_omz_ind_inv(:,1)+FeDyn1_omz_ind_inv(:,2)+FeDyn1_omz_ind_inv(:,4)];

