function [y,Status,ErrorStr,HeaderStr] = ExportTransformData(z,Transformation,CurrVariable)
% ExportTransformData: applies the export transformation function to a variable.
%
% USAGE:
%
%       [y,Status,ErrorStr,HeaderStr] = ExportTransformData(z,Transformation,CurrVariable)
%
% REQUIRED iNPUT:  z (vector) with T observations.
%
%                  Transformation (structure) with field names. The field name to use is determined
%                                 by CurrVariable. This field name has subfields "exportfcn",
%                                 "exportinitial", and "exportx" that are used for the transformation of z into y.
%
%                  CurrVariable (string vector) with the name of the variable to transform. This
%                                 name should correspond to the field name in the structure Transformation
%                                 that will be used.
%
% REQUIRED OUTPUT: y (vector) with T1 observations.
%
%                  Status (boolean) that takes the value 0 if the transformation was performed without
%                                 error and 1 otherwise.
%
%                  ErrorStr (string vector) with error messages (if any).
%
%                  HeaderStr (string vector) with a suitable header message (if any).
%
%
%                       Written by: Anders Warne
%                                   New Area Wide Model Project
%                                   DG-R/EMO
%                                   European Central Bank (ECB)
%                                   Email: anders.warne@ecb.europa.eu
%                                   Copyright © 2006-2008 European Central Bank.
%
%                       First version: November 9, 2007.
%                        This version: December 18, 2008.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% LICENSE INFORMATION:
%
%      YADA is free software: you can redistribute it and/or modify
%      it under the terms of the GNU General Public License as published by
%      the Free Software Foundation, either version 3 of the License, or
%      (at your option) any later version.
%
%      This program is distributed in the hope that it will be useful,
%      but WITHOUT ANY WARRANTY; without even the implied warranty of
%      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%      GNU General Public License for more details.
%
%      You should have received a copy of the GNU General Public License
%      along with this program.  If not, see <http://www.gnu.org/licenses/>.
%
%      YADA is released under the GNU General Public License, Version 3,
%      29 June 2007 <http://www.gnu.org/licenses/>. The current release of
%      the program was last modified by the ECB on the "This version" date
%      above.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% CHANGELOG:
%
% * 14-11-2007: Added the input variables "FirstObs" and "LastObs" and took the new fields "exportx.data"
%               and "exportx.numinitialobs" into account.
%
% * 20-11-2007: The fields "data" and "numinitialobs" are no longer used. The inputs "FirstObs" and "LastObs"
%               have also been removed.
%
% * 23-5-2008:  Updated the documentation.
%
% * 30-5-2008:  Updated the documentation.
%
% * 18-12-2008: Updated the documentation.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%

%
% initialize
%
y = [];
Status = 0;
ErrorStr = '';
HeaderStr = '';
%
% get the structure with the required fields
%
CurrField = getfield(Transformation,CurrVariable);
%
% remove any NaN's at the beginning of z
%
StartPos = NaN;
for i=1:length(z);
   if isnan(z(i))==0;
      StartPos = i;
      break;
   end;
end;
if isnan(StartPos)==1;
   ErrorStr = ['The data for ' CurrVariable ' has only NaN''s. YADA is therefore unable to perform the export transformation.'];
   HeaderStr = 'Bad Export Function';
   Status = 1;
   return;
end;
z = z(StartPos:length(z));
%
% check how many input arguments the inline function
%
NumInputs = length(symvar(CurrField.exportfcn));
%
% make sure that time dimension is given by the rows
%
if size(CurrField.exportx,2)>size(CurrField.exportx,1);
   CurrField.exportx = CurrField.exportx';
end;
if size(CurrField.exportx,2)~=NumInputs-1;
   CurrField.exportx = [];
end;
%
% deal with initial values for the variable to be exported
%
if isempty(CurrField.exportinitial)==0;
   z = [vec(CurrField.exportinitial);z];
end;
RunArg = 'z';
%
% check if we should create the additional variables for the command line
%
data = CurrField.exportx;
for i=1:size(data,2);
   eval(['x' int2str(i) ' = RemovePreNaNs(data(:,' int2str(i) '));']);
   RunArg = [RunArg ',x' int2str(i)];
end;
%
% now we try to create the inline function
%
try;
   if isempty(data)==1;
      YADAf = inline(CurrField.exportfcn);
   else;
      YADAf = YADAInline(CurrField.exportfcn);
   end;
catch;
   ErrorStr = ['The was an error creating the inline function for ' CurrVariable '. The following message was caught: ' lasterr];
   HeaderStr = 'Bad Inversion Function';
   Status = 1;
   y = z;
   return;
end;
%
% check if the inline function executes correctly
%
ErrorStr = '';
try;
   eval(['y = YADAf(' RunArg ');']);
catch;
   ErrorStr = ['YADA caught the following error message for the export function for the variable ' CurrVariable ': ' lasterr];
   HeaderStr = 'Bad Export Function';
   Status = 1;
   y = z;
end;

%
% end of ExportTransformData.m
%
