function [lambda_d_lookup,lambda_q_lookup,Id_lookup,Iq_lookup,lambda_dq_I_dq_data]=Flux2Current_lookup(i_d,i_q,lambda_d,lambda_q,lambda_m,Input_refine,Output_refine,windowSize,bool)
%
%    Copyright (c) 2014 by Plexim GmbH All rights reserved.
%
%    A free license is granted to anyone to use this software for any legal
%    non safety-critical purpose, including commercial applications,
%    provided that: 1) IT IS NOT USED TO DIRECTLY OR INDIRECTLY COMPETE
%    WITH PLEXIM, and 2) THIS COPYRIGHT NOTICE IS PRESERVED in its
%    entirety.
%
%    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
%    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
%    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
%    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
%    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
%    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
%    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
%
% The Flux2Current function converts lambda_dq and I_dq data obtained from
% measurements or from an FEA tool to create look-up tables for the d- and
% q-axis currents as a function of the flux in the d- and q-axes. It is
% assumed that the saturation effects are symmetric for both Id and Iq i.e.
% lambda_d(I_d,I_q) = -lambda_d(-I_d,I_q)
% lambda_q(I_d,I_q) = -lambda_q(I_d,-I_q)
%
% Inputs:
%
% I_d (I_q): nxm matrix of d-axis (q-axis) currents that are obtained as a
% function of I_rms and advance angle. Advance angle should be in the range
% between 0 and pi/2 electrical angle. It is assumed that the columns
% correspond to different advance angle values and rows correspond to
% different I_rms values.
%
% lambda_d (lambda_q): nxm matrix of d-axis (q-axis) flux linkages that are
% obtained as a function of I_rms and advance angle. Advance angle should
% be in the range between 0 and pi/2 electrical angle. It is assumed that
% the columns correspond to different advance angle values and rows
% correspond to different I_rms values.
%
% lambda_m: Flux linkage of the permanent magnet. If the lambda_d value
% does not contain the PM flux then this value should be set to zero.
%
% Input_refine: Specified as a real, nonnegative, integer scalar. This
% value specifies the number of times to repeatedly divide the intervals of
% the refined grid in each dimension. This results in 2^k-1 interpolated
% points between d- and q-axis currents and flux linkage values. A value of
% 0 will result in the data entered as is. The refinement factor is limited
% to a maximum value of 3. Input_refine is used in an INTERP2 function on
% the input current and flux data provided by the user.
%
% Output_refine: Specified as a real, nonnegative, integer scalar. This
% value specifies the number of times to repeatedly divide the intervals of
% the refined grid in each dimension. This results in 2^k-1 interpolated
% points between d- and q-axis currents and flux linkage values. A value of
% 0 will result in the data entered as is. The refinement factor is limited
% to a maximum value of 3. Output_refine is used in an INTERP2 function on
% the output lookup table values of current and flux.
%
% windowSize: Corresponds to the averaging length of the averaging vector.
% A windowSize of 3 will create an averaging vector that averages three
% consecutive entries. Must be an odd integer. If an even integer is used
% then it is incremented by 1 to make it odd.
%
% WARNING: Chosing a large windowSize will cause significant deviation from
% the original data. A windowSize of 3,5,7 or 9 is recommended.
%
% bool: Boolean variable indicating an instant when the d-axis currents
% and flux linkages are in the negative axis. If this is the case and
% assuming symmetric saturation of the machine, the I_d and lambda_d values
% are converted to positive quantities. It is always assumed that the q-axis
% measurements are made in the positive axis.
%
%   See also CONV, INTERP2.
%
% Outputs:
%
% lambda_d_lookup (lambda_q_lookup): kx1 column vector the stores the
% d-axis (q-axis) flux values that will be used for the d-axis (q-axis)
% current look-up tables.
%
% Id_lookup (Iq_lookup): kxk matrix that stores the d-axis (q-axis)
% estimated current information as a function of flux in the d-axis and
% q-axis.
%
% Plexim, Inc. Author: Munadir A. Ahmed Date: 07/17/2014