% Script to read the HRXX_Amz.nc file. This binary file contains the
% daily discharge values of the Hydrological Reanalysis across the 
% 20th Century for the Amazon Basin in its v1.0 version. Information is
% related to the centroids of each catchment of the MGB hydrologic-hydrodynamic model, from 1/1/1910 to 31/12/2010.
% The matrix of daily discharge contains 36890 columns (one per day, starting at 01Jan/1910) and 12466 rows (one per catchment).

%% Authors:
% Sly Wongchuig Correa
% Rodrigo Cauduro Dias de Paiva
% Vinícius Siqueira
% Walter Collischonn
%% Institute of Hydraulic Research - (IPH/UFRGS) Brazil - November 2018
    
close
clear
clc

    ncid = netcdf.open('HRXX_Amz_v1.0.nc','NC_NOWRITE');
    [numdims, numvars, numglobalatts, unlimdimID] = netcdf.inq(ncid);
for i = 0:numvars-1
    [varname, xtype, dimids, numatts] = netcdf.inqVar(ncid,i);
    disp(['--------------------< ' varname ' >---------------------'])
    flag = 0;
    for j = 0:numatts - 1
        attname1 = netcdf.inqAttName(ncid,i,j);
        attname2 = netcdf.getAtt(ncid,i,attname1);
        disp([attname1 ':  ' num2str(attname2)])
        if strmatch('add_offset',attname1)
            offset = attname2;
        end
        if strmatch('scale_factor',attname1)
            scale = attname2;
            flag = 1;
        end        
    end
    disp(' ')
    
    if flag
        eval([varname '= double(double(netcdf.getVar(ncid,i))*scale + offset);'])
    else
        eval([varname '= double(netcdf.getVar(ncid,i));'])   
    end
end
    netcdf.close(ncid);
return
