
function [lat,lon,time,freq,mat,unit,scale,fillv,MAPSTA] ...
         =read_WWNCf_noscale(filename,var,date1,ntimes,lon1,lat1,freq1)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% This functions reads all or a subsed of a NetCDF file. 
% If date1 is specified: takes only the closest dates in the file
% if lon1 is specificied: takes only the closest longitude ... 

%
% 1. Opens file and gets dimensions of arrays
%
fid=netcdf.open(filename,'NC_NOWRITE');
  [ndims,nvars,ngatts,unlimdimid]=netcdf.inq(fid); 
  dimlon = netcdf.inqDimID(fid,'longitude');
  dimlat = netcdf.inqDimID(fid,'latitude');
  dimtime = netcdf.inqDimID(fid,'time');
  dimf = netcdf.inqDimID(fid,'f');
  [d0,nx]=netcdf.inqDim(fid,dimlon);
  [d1,ny]=netcdf.inqDim(fid,dimlat);
  [d3,nt]=netcdf.inqDim(fid,dimtime);
  [d4,nf]=netcdf.inqDim(fid,dimf);
  varlon = netcdf.inqVarID(fid,'longitude');
  varlat = netcdf.inqVarID(fid,'latitude');
  varf   = netcdf.inqVarID(fid,'f');
  varid=netcdf.inqVarID(fid,var);
  MAPSTAid=netcdf.inqVarID(fid,'MAPSTA');

  lon=netcdf.getVar(fid,varlon);
  lat=netcdf.getVar(fid,varlat);
  freq=netcdf.getVar(fid,varf);
% We assume that the date reference is 1 Jan 1990. 
% This is normally written in the time attributes        
        time0=datenum(1990,1,1);
        vartime = netcdf.inqVarID(fid,'time');
        time=netcdf.getVar(fid,vartime)+time0;
        var1=netcdf.inqVar(fid,4);
%
%  Reads the status map (land / sea / cut-out areas): needed for
%  multiple-grid set-ups
%
        if (MAPSTAid > -1 )
            MAPSTA=netcdf.getVar(fid,MAPSTAid);
            nmapsta=1;
        else
            MAPSTA=zeros(nx,ny);
            nmapsta=0;
        end
        scale=netcdf.getAtt(fid,varid,'scale_factor');
        fillv=netcdf.getAtt(fid,varid,'_FillValue');
%
% 2.  defines the indices for the data subset 
%
if exist('freq1','var')  
    [fdist,kk]=min(abs(freq-freq1));
    freq=freq(kk);
    KK=kk;
    nk=1;
else
    KK=1;
    nk=nf;
end
%
%
%
if exist('date1','var') 
    [timedist,ll]=min(abs(time-date1));
    time=time(ll);
    LL=ll;
    if exist('ntimes','var') 
       nl=min(ntimes,nt-LL+1);
    else
       nl=1;
    end
else
    LL=1;
    nl=nt;
end
%
%
%
if exist('lon1','var') 
    [xdist,ii]=min(abs(lon-lon1));
    lon=lon(ii);
    II=ii;
    ni=1;
else
    II=1;
    ni=nx;
end
if exist('lat1','var') 
    [ydist,jj]=min(abs(lat-lat1));
    lat=lat(jj);
    JJ=jj;
    nj=1;
else
    JJ=1;
    nj=ny;
end
%[KK LL II JJ]
%[ni nj nk nl]
%
% 3. Extracts attributes and data
%
unit =netcdf.getAtt(fid,varid,'units');
scale=netcdf.getAtt(fid,varid,'scale_factor');
fillv=netcdf.getAtt(fid,varid,'_FillValue');
mat  =netcdf.getVar(fid,varid,[II-1 JJ-1 KK-1  LL-1],[ni nj nk nl]);
netcdf.close(fid);    

    
