% read TAB file, remove values signalling invalid data, and output to new file
function remove_bad_data(infile)
    [clon, clat, ltim, t3, t4, t5, t6, t7, t8, t9, tbol] = ...
       textread(infile, '%f %f %f %f %f %f %f %f %f %f %f',...
       'headerlines', 1, 'delimiter', ',');
    ix = getGoodRows(horzcat(t3,t4,t5,t6,t7,t8,t9,tbol));
    A = [clon(ix), clat(ix), ltim(ix), t3(ix), t4(ix), t5(ix), t6(ix),...
         t7(ix), t8(ix), t9(ix), tbol(ix),];
    [pathstr,name,ext] = fileparts(infile);
    outfile = fopen(strcat(pathstr,name,'-nonan',ext), 'w');
    formatspec = ...
    '%7.2f,%6.2f,%6.2f,%9.4f,%9.4f,%9.4f,%9.4f,%9.4f,%9.4f,%9.4f,%9.4f\n';
    fprintf(outfile, formatspec, A');
    fclose(outfile);
end

function ix = getGoodRows(mat)
    invalidValue = (mat==-9999 | mat==-9998);
    ix = ~all(invalidValue,2); % one value per row, true if at least some of the values are not marked invalid
end