function [ReadNCEPwkData] = ReadNCEPwkData(prefix, last_jdate, file_nc_in, file_ascii_out)
%  function [ReadNCEPwkData] = ReadNCEPwkData(prefix, file_nc_in, file_ascii_out)
%
%         prefix: "sst" for SST or "err" for Standard Error
%     last_jdate: We only need the data when timej > last_jdate
%     file_nc_in: Original file with NCEP weekly data in netCDF format (input)
% file_ascii_out: Output data file in ascii format: time[jd], lon, lat, sst or err [oC]
% time is the Julian date from 1992-01-01
%
% This function requires the netCDF toolbox
%
% Francis Bringas, 2008Sep08

% Reading data in netCDF format:
format long g
nc = netcdf(file_nc_in,'nowrite');
times = nc{'time'}(:);                                % time in seconds since 1800-01-01 00:00:00
timej = int16( times - 70125 );                       % time in Julian Days since 1992-01-01 00:00:00
lon = nc{'lon'}(:);                                   % longitude
lat = nc{'lat'}(:);                                   % latitude
var = nc{prefix}(:);                                  % sea surface temperature or error
var_mv = nc{prefix}.missing_value(:);                 % sea surface temperature or error Missing Values (NaN)
var_ao = nc{prefix}.add_offset(:);                    % sea surface temperature or error Add Offset
var_sf = nc{prefix}.scale_factor(:);                  % sea surface temperature or error Scale Factor

kmin = 1e20;
kmax = -1e20;
for i = 1 : length(times)
    if ( timej(i) > last_jdate )
       if ( i < kmin )
          kmin = i;
       end
       if ( i > kmax )
          kmax = i;
       end
    end
end

varp = var(kmin:kmax,:,:);     esto no esta funcionando, no lo esta cortando
varp = permute(var,[2,3,1]);
varpv = varp(:);
timep = timej(kmin:kmax); 


kmax-kmin
disp(size(timep,1))
disp(size(varp,1))
disp(size(varp,1)/size(timep,1))
whos
return

j = 1;
tsz = size(timej,1);
sal = []
for i = 1 : size(varp,1)
   if ( varp(i) == var_mv )
      sal = [sal timej(j) 99999999];
   else
      var_uv = var_ao + ( varp(i) * var_sf );   % sea surface temperature or error unpacked value
      sal = [sal timej(j) var_uv];
      
   end
   if ( mod(i,64800) == 0 )
      j = j + 1;
      disp(j*1000000000000 + i)
   end
end

whos
return

fsal = fopen(file_ascii_out,'w');
fprintf(fsal,'%.12g,%.8g\n', sal);
fclose(fsal);

ReadJA2nc = 0;

%---------- end of code ----------------

%---------- end of file ----------------