function exportZtdLittleR(ztd_map, lat_map, lon_map, h_map, time, out_dir, file_name, qc_ztd)
% Export a ZTD map into little_R format (input of 3Dvar)
%
% EXAMPLE
%   exportZtdLittleR(ztd_map, lat_map, lon_map, h_map, time, 'reserved/tmp', 'sar_map', 0.015)
%
% SYNTAX
%   exportZtdLittleR(ztd_map, lat_map, lon_map, h_map, time, out_dir, file_name, qc_ztd)
%
%
% info on LITTLE R export
% reference: http://www2.mmm.ucar.edu/wrf/users/wrfda/OnlineTutorial/Help/littler.html#FM

% qc_ztd = 0.015;
% ztd_map = ones(1,1);
n_row = size(ztd_map, 1);
n_col = size(ztd_map, 2);
% lat_map = randn(n_row,n_col);
% lon_map = 10 + randn(n_row,n_col);
% h_map = 100 + randn(n_row,n_col);
% time = GPS_Time(now);
% out_dir = 'reserved/tmp';
% file_name = 'sar_map';
qc_ztd = uint32(qc_ztd * 1e4); % expressed in tenths of millimeters

log = Core.getLogger();
save_on_disk = true;
no_data = -888888.00000;
qc_no_data = -888888;
end_data = -777777.00000;
end_qc = 0;
empty_data_line = sprintf('%13.5f%7d%13.5f%7d%13.5f%7d%13.5f%7d%13.5f%7d%13.5f%7d%13.5f%7d%13.5f%7d%13.5f%7d%13.5f%7d\n', ...
    no_data, qc_no_data, ...
    no_data, qc_no_data, ...
    no_data, qc_no_data, ...
    no_data, qc_no_data, ...
    no_data, qc_no_data, ...
    no_data, qc_no_data, ...
    no_data, qc_no_data, ...
    no_data, qc_no_data, ...
    no_data, qc_no_data, ...
    no_data, qc_no_data);
end_data_line = sprintf('%13.5f%7d%13.5f%7d%13.5f%7d%13.5f%7d%13.5f%7d%13.5f%7d%13.5f%7d%13.5f%7d%13.5f%7d%13.5f%7d\n', ...
    end_data, end_qc, ...
    end_data, end_qc, ...
    no_data, end_qc, ...
    no_data, end_qc, ...
    no_data, end_qc, ...
    no_data, end_qc, ...
    no_data, end_qc, ...
    no_data, end_qc, ...
    no_data, end_qc, ...
    no_data, end_qc);
tail_integers = sprintf('%7d%7d%7d\n',1,0,0);

% LITTLE R export
txt = '';
if save_on_disk
    if ~exist(out_dir, 'dir')
        log.newLine();
        log.addWarning(sprintf('The out folder does not exists\n Creating %s', out_dir));
        mkdir(out_dir);
    end
end

if save_on_disk
    file_name = sprintf('%s%c%s', out_dir, filesep, file_name);
    fid = fopen(file_name,'w');
end
i = 0;
fprintf('\n');
wb = Go_Wait_Bar.getInstance(n_col * n_row);
wb.createNewBar(sprintf('Exporting ZTD map into LITTLE_R format (%s)', time.toString('yyyy - mm - dd')));
for c = 1 : n_col
    % get the closest epoch to the sar passage
    for r = 1 : n_row
        i = i+1;
        % reference: http://www2.mmm.ucar.edu/wrf/users/wrfda/OnlineTutorial/Help/littler.html#FM
        lat = lat_map(r,c);
        lon = lon_map(r,c);
        id_dtr = n_row * (c-1) + (r-1);
        name = sprintf('CALSAR_%05d', id_dtr);
        platform = 'FM-114 GPSZD';
        source = 'goGPS software';
        h_ortho = h_map(r, c);
        obs_time = time.toString('yyyymmddHHMMSS');
        ztd = ztd_map(r,c) * 100; % expressed in cm
        
        if ~isnan(ztd)
            %Latitude	Longitude	ID	Name	Platform (FM�Code)note	Source	Elevation	Valid fields	Num. errors	Num. warnings	Sequence number	Num. duplicates	Is sounding?	Is bogus?	Discard?	Unix time	Julian day	Date	SLP, QC	Ref Pressure, QC	Ground Temp, QC	SST,   QC	SFC Pressure, QC	Precip, QC	Daily Max T, QC	Daily Min T, QC	Night Min T, QC	3hr Pres Change, QC	24hr Pres Change, QC	Cloud cover, QC	Ceiling, QC	Precipitable water, QC (see note)
            %F20.5	    F20.5	    A40	A40	    A40	                    A40	    F20.5	    I10            	I10	        I10	            I10	            I10	            L	            L	        L	        I10	        I10	        A20	    F13.5,  I7	F13.5,    I7	F13.5,       I7	F13.5, I7	F13.5,        I7	F13.5,  I7	F13.5,       I7	F13.5,       I7	F13.5,       I7	F13.5,           I7	F13.5,            I7	F13.5,       I7	F13.5,   I7	F13.5,              I7
            
            txt = sprintf('%s%20.5f%20.5f%-40s%-40s%-40s%-40s%20.5f%10d%10d%10d%10d%10d%10s%10s%10s%10d%10d%20s%13.5f%7d%13.5f%7d%13.5f%7d%13.5f%7d%13.5f%7d%13.5f%7d%13.5f%7d%13.5f%7d%13.5f%7d%13.5f%7d%13.5f%7d%13.5f%7d%13.5f%7d%13.5f%7d\n%s%s%s', ...
                txt, ...
                lat, ...
                lon, ...
                num2str(id_dtr), ...
                name, ...
                platform, ...
                source, ...
                h_ortho, ...
                1, ...
                0, ...
                0, ...
                i, ...
                0, ...
                'F', ...
                'T', ...
                'F', ...
                uint32(time.getUnixTime), ...
                uint32(time.getJD), ...
                obs_time, ...
                no_data, qc_no_data, ...
                no_data, qc_no_data, ...
                no_data, qc_no_data, ...
                no_data, qc_no_data, ...
                no_data, qc_no_data, ...
                no_data, qc_no_data, ...
                no_data, qc_no_data, ...
                no_data, qc_no_data, ...
                no_data, qc_no_data, ...
                no_data, qc_no_data, ...
                no_data, qc_no_data, ...
                no_data, qc_no_data, ...
                no_data, qc_no_data, ...
                ztd, qc_ztd, ...
                empty_data_line, ...
                end_data_line, ...
                tail_integers);
        end
        if save_on_disk && (length(txt) > (4 * 1024))
            wb.go(r + (c-1) * n_row);
            fprintf(fid,'%s', txt);
            txt = [];
        end
    end
    wb.go(r + (c-1) * n_row);    
    if save_on_disk
        fprintf(fid,'%s', txt);
        txt = [];
    end
end
wb.close();
if save_on_disk
    fclose(fid);
end

