% ------------------------------------------------------------------------------
% Check meta-data structure contents
%
% SYNTAX :
%  [o_ok] = check_json_meta_data(a_metaDataStruct, a_floatNum)
%
% INPUT PARAMETERS :
%   a_metaDataStruct : meta-data structure
%   a_floatNum       : float WMO number
%
% OUTPUT PARAMETERS :
%   o_ok : ok flag (1 if in the contents is ok, 0 otherwise)
%
% EXAMPLES :
%
% SEE ALSO :
% AUTHORS  : Jean-Philippe Rannou (Altran)(jean-philippe.rannou@altran.com)
% ------------------------------------------------------------------------------
% RELEASES :
%   03/19/2019 - RNU - creation
% ------------------------------------------------------------------------------
function [o_ok] = check_json_meta_data(a_metaDataStruct, a_floatNum)

% output parameters initialization
o_ok = 1;


% retrieve max length of each variable
lenInfo = init_length_info;

% check input structure data
fieldNames = fields(a_metaDataStruct);
for idField = 1:length(fieldNames)
   fieldName = fieldNames{idField};
   if (any(strcmp(fieldName, lenInfo(:, 1))))
     idF = find(strcmp(fieldName, lenInfo(:, 1)), 1);
     maxLength = lenInfo{idF, 2};
     info = a_metaDataStruct.(fieldName);
     if (~isempty(info))
        if (ischar(info))
           if (length(info) > maxLength)
              fprintf('ERROR: Float #%d: max length of ''%s'' is %d (''%s'' cannot be accepted) - no json file generated\n', ...
                 a_floatNum, fieldName, maxLength, info);
              o_ok = 0;
           end
        elseif (iscell(info))
           for idL = 1:size(info, 1)
              info2 = info{idL, :};
              if (length(info2) > maxLength)
                 fprintf('ERROR: Float #%d: max length of ''%s'' is %d (''%s'' cannot be accepted) - no json file generated\n', ...
                    a_floatNum, fieldName, maxLength, info2);
                 o_ok = 0;
              end
           end
        end
     end
   end
end

return

% ------------------------------------------------------------------------------
% Retrieve max length of meta-data variables
%
% SYNTAX :
%  [o_lenInfo] = init_length_info
%
% INPUT PARAMETERS :
%
% OUTPUT PARAMETERS :
%   o_lenInfo : retrieved information
%
% EXAMPLES :
%
% SEE ALSO :
% AUTHORS  : Jean-Philippe Rannou (Altran)(jean-philippe.rannou@altran.com)
% ------------------------------------------------------------------------------
% RELEASES :
%   03/19/2019 - RNU - creation
% ------------------------------------------------------------------------------
function [o_lenInfo] = init_length_info

% output parameters initialization
o_lenInfo = [ ...
   {'PLATFORM_NUMBER'}, {8}; ...
   {'PTT'}, {256}; ...
   {'TRANS_SYSTEM'}, {16}; ...
   {'TRANS_SYSTEM_ID'}, {32}; ...
   {'TRANS_FREQUENCY'}, {16}; ...
   {'POSITIONING_SYSTEM'}, {8}; ...
   {'PLATFORM_FAMILY'}, {256}; ...
   {'PLATFORM_TYPE'}, {32}; ...
   {'PLATFORM_MAKER'}, {256}; ...
   {'FIRMWARE_VERSION'}, {32}; ...
   {'MANUAL_VERSION'}, {16}; ...
   {'FLOAT_SERIAL_NO'}, {32}; ...
   {'STANDARD_FORMAT_ID'}, {16}; ...
   {'DAC_FORMAT_ID'}, {16}; ...
   {'WMO_INST_TYPE'}, {4}; ...
   {'PROJECT_NAME'}, {64}; ...
   {'DATA_CENTRE'}, {2}; ...
   {'PI_NAME'}, {64}; ...
   {'ANOMALY'}, {256}; ...
   {'BATTERY_TYPE'}, {64}; ...
   {'BATTERY_PACKS'}, {64}; ...
   {'CONTROLLER_BOARD_TYPE_PRIMARY'}, {32}; ...
   {'CONTROLLER_BOARD_TYPE_SECONDARY'}, {32}; ...
   {'CONTROLLER_BOARD_SERIAL_NO_PRIMARY'}, {32}; ...
   {'CONTROLLER_BOARD_SERIAL_NO_SECONDARY'}, {32}; ...
   {'SPECIAL_FEATURES'}, {1024}; ...
   {'FLOAT_OWNER'}, {64}; ...
   {'OPERATING_INSTITUTION'}, {64}; ...
   {'CUSTOMISATION'}, {1024}; ...
   {'LAUNCH_QC'}, {1}; ...
   {'START_DATE_QC'}, {1}; ...
   {'STARTUP_DATE_QC'}, {1}; ...
   {'DEPLOYMENT_PLATFORM'}, {32}; ...
   {'DEPLOYMENT_CRUISE_ID'}, {32}; ...
   {'DEPLOYMENT_REFERENCE_STATION_ID'}, {256}; ...
   {'END_MISSION_STATUS'}, {1}; ...
   {'SENSOR'}, {32}; ...
   {'SENSOR_MAKER'}, {256}; ...
   {'SENSOR_MODEL'}, {256}; ...
   {'SENSOR_SERIAL_NO'}, {16}; ...
   {'PARAMETER'}, {64}; ...
   {'PARAMETER_SENSOR'}, {128}; ...
   {'PARAMETER_UNITS'}, {32}; ...
   {'PARAMETER_ACCURACY'}, {32}; ...
   {'PARAMETER_RESOLUTION'}, {32}; ...
   {'PREDEPLOYMENT_CALIB_EQUATION'}, {4096}; ...
   {'PREDEPLOYMENT_CALIB_COEFFICIENT'}, {4096}; ...
   {'PREDEPLOYMENT_CALIB_COMMENT'}, {4096} ...
   ];

return
