function nc_hydro_postprocess(d)
%% read metadata file
fileID = fopen('metadata_hydro_4_nc.csv');
MD=textscan(fileID,'%s %d %s %s %s %s %s %s %s %s %s %d %d %f','delimiter',',');
fclose(fileID);

fileList = dir(d);
dirIndex = [fileList.isdir];
ncFiles  = {fileList(~dirIndex).name};

OPT.timezone            = '+01:00';
OPT.refdatenum          = datenum(1970,1,1);

% varsName                = {'Survey ID','Survey Code','Area Name','Survey Begin Date','Survey End Date','Organisation','Ship','Primary Position System','Echo Sounder Type','Seafloor Mapping System','NR_POINTS','AREA','PNTS_P_M2'};
varsName                = {'sid','surveycode','areaname','begindate','enddate','organisation','ship','positionsystem','echosounder','seafloormapping','nrpoints','area','density'};
varsDatatype            = {'int','char'      ,'char'    ,'double'   ,'double' ,'char'        ,'char','char'          ,'char'       ,'char'           ,'int'     ,'int' ,'double' };

varsAttribute{1}.Name   = {'standard_name','long_name','definition'};
varsAttribute{1}.Value  = {'sid'          ,'Survey ID','Unique ID number of survey, given by the Hydrographic Office'};

varsAttribute{2}.Name   = {'standard_name','long_name'  ,'definition'};
varsAttribute{2}.Value  = {'surveycode'   ,'Survey Code','Code of survey starting with HY, given by the Hydrographic Office'};

varsAttribute{3}.Name   = {'standard_name','long_name','definition'};
varsAttribute{3}.Value  = {'areaname'     ,'Area Name','Topographic name of area covered by survey'};

varsAttribute{4}.Name   = {'standard_name','long_name','units','definition'};
varsAttribute{4}.Value  = {'begindate'    ,'Survey Begin Date',['days since ',datestr(OPT.refdatenum,'yyyy-mm-dd'),' 00:00:00 ',OPT.timezone],'Begin date of survey'};

varsAttribute{5}.Name   = {'standard_name','long_name','units','definition'};
varsAttribute{5}.Value  = {'enddate'      ,'time'     ,['days since ',datestr(OPT.refdatenum,'yyyy-mm-dd'),' 00:00:00 ',OPT.timezone],'End date of survey'};

varsAttribute{6}.Name   = {'standard_name','long_name'   ,'definition'};
varsAttribute{6}.Value  = {'organisation' ,'organisation',''};

varsAttribute{7}.Name   = {'standard_name','long_name'          ,'definition'};
varsAttribute{7}.Value  = {'ship'         ,'Name of survey-ship',''};

varsAttribute{8}.Name   = {'standard_name' ,'long_name'              ,'definition'};
varsAttribute{8}.Value  = {'positionsystem','Primary Position System','Technique used to determine geographic position during survey'};

varsAttribute{9}.Name   = {'standard_name','long_name'        ,'definition'};
varsAttribute{9}.Value  = {'echosounder'  ,'Echo Sounder Type',''};

varsAttribute{10}.Name  = {'standard_name'  ,'long_name'              ,'definition'};
varsAttribute{10}.Value = {'seafloormapping','Seafloor Mapping System',''};

varsAttribute{11}.Name  = {'standard_name','long_name'       ,'definition'};
varsAttribute{11}.Value = {'nrpoints'     ,'Number of points','Number of points in xyz file of this survey used for interpolation'};

varsAttribute{12}.Name  = {'standard_name','long_name'   ,'definition'                                                         ,'units'};
varsAttribute{12}.Value = {'area'         ,'surface area','Surface area obtained from concave hulls around input xyz locations','m2'};

varsAttribute{13}.Name  = {'standard_name','long_name'    ,'definition'};
varsAttribute{13}.Value = {'density'      ,'point density','Density of measurement points, equals nrpoints/area'};
%% loop over all nc files
for i=1:length(ncFiles)
    ids = nc_cf_time([d ncFiles{i}]);
    idx = ismember(MD{2},ids);
    
    %add dimension stringsize
    nc_adddim([d ncFiles{i}],'stringsize',100)
    
    %add all new metadata variables and add the data
    for n=1:length(varsName)
        D = MD{n+1};
        
        clear('v')
        v.Name      = varsName{n};
        v.Datatype  = varsDatatype{n};
        V = varsAttribute{n}.Value;
        N = varsAttribute{n}.Name;
        for t=1:length(N)
            v.Attribute(t) = struct('Name',N{t},'Value',V{t});
        end
        if strcmp(v.Datatype,'char')
            v.Dimension = {'time','stringsize'};
            nc_addvar([d ncFiles{i}],v);
            
            D = D(idx,:);
            for m=1:length(D)
                tmp = D{m};
                if isempty(tmp); tmp = '-'; end
                nc_varput([d ncFiles{i}],v.Name,tmp,[m-1 0],[1 length(tmp)])
            end
        else
            v.Dimension = {'time'};
            
            D = D(idx);
            if n==1; D(D>100000)=NaN; end
            if n==4
                D=datenum(D,'yyyymmdd')-OPT.refdatenum;
                timeB=D;
                v.Attribute(end+1) = struct('Name', 'actual_range'   ,'Value', [datestr(min(D(:)+OPT.refdatenum),'yyyy-mm-ddTHH:MM:SS') ' - ' datestr(max(D(:)+OPT.refdatenum),'yyyy-mm-ddTHH:MM:SS')]);
            elseif n==5
                D=datenum(D,'yyyymmdd')-OPT.refdatenum;
                timeE=D;
                v.Attribute(end+1) = struct('Name', 'actual_range'   ,'Value', [datestr(min(D(:)+OPT.refdatenum),'yyyy-mm-ddTHH:MM:SS') ' - ' datestr(max(D(:)+OPT.refdatenum),'yyyy-mm-ddTHH:MM:SS')]);
            end
            nc_addvar([d ncFiles{i}],v);
            nc_varput([d ncFiles{i}],v.Name,D)
        end
    end
    
    % adjust time variable values
    new_time = (timeB+timeE)./2;
    ncwriteatt([d ncFiles{i}],'time','actual_range',[datestr(min(new_time(:)+OPT.refdatenum),'yyyy-mm-ddTHH:MM:SS') ' - ' datestr(max(new_time(:)+OPT.refdatenum),'yyyy-mm-ddTHH:MM:SS')])
    ncwriteatt([d ncFiles{i}],'time','long_name','Average time of begin and end date of survey');
    nc_varput([d ncFiles{i}],'time',new_time)
    
    % update attributes
    ncwriteatt([d ncFiles{i}],'z','definition','Lowest Astronomical Tide (LAT), the lowest tide level which can be predicted to occur under average meteorological conditions and under any combination of astronomical conditions');
    ncwriteatt([d ncFiles{i}],'/','timeCoverage',[datestr(min(timeB(:)+OPT.refdatenum),'yyyy-mm-ddTHH:MM:SS') ' - ' datestr(max(timeE(:)+OPT.refdatenum),'yyyy-mm-ddTHH:MM:SS')]);
    ncwriteatt([d ncFiles{i}],'/','date_modified',datestr(now,'yyyy-mm-ddTHH:MM:SSZ'));
    ncwriteatt([d ncFiles{i}],'/','source_data','https://repos.deltares.nl/repos/ODyn/trunk/RawData/CorrectPointData/Mariene/, revision 47');
    ncwriteatt([d ncFiles{i}],'/','processing_software','https://repos.deltares.nl/repos/ODyn/trunk/Tools/Java/Sourcecode/GridSplitBatch/, revision 47');
    ncwriteatt([d ncFiles{i}],'/','processing_method','Inverse Distance Weight interpolation of LOV2 data with radius 100 m'); 
end