function hazard_example(inpath,outpath,infile,outfile,proj1,proj2)

%% HELP 
% ncuinput = string name of the nc file with model resutls e.g. xboutput,nc
% ncfile   = string with the name of the output file  e.g.  'hazard_test.nc';
% proj1    = EPSG database code of original model grid  Ria Formosa: 3763 
% proj2    = EPSG database code of output grid (usefull for the webviewer)
%               normally: 4326 (WGS84)
%% READ FILES FROM MODEL RESULTS (XBEACH)

%inpath  = ''
%outpath = ''
%infile  = 'xboutput.nc' 
%outfile = 'hazard_test.nc'

ncinput  = fullfile(inpath,infile);
ncfile   = fullfile(outpath,outfile);


%% READ DATA FROM XBEACH OUTPUT

x                = nc_varget(ncinput, 'globalx');  
y                = nc_varget(ncinput, 'globaly');
Zsmax            = nc_varget(ncinput, 'zs_max');       % VARIABLES MAY CHANGE DEPENDING ON BN SET UP
sederomin        = nc_varget(ncinput, 'sedero_min');
qxmax            = nc_varget(ncinput, 'qx_max');

%% DATA TRANSFORMATION 
time_units ='minutes since 1970-01-01 00:00:00.0 +0000'; % time in FEWS FORMAT (NETCDF CF convention )
time_now =now;                                           % dummy time to fill the analysis time
time= (time_now-datenum(time_units(15:end-6))).*(60*24);
analysis_time= (time_now-datenum(time_units(15:end-6))).*(60*24);
griddim = size(x);
row=[1:1:griddim(1)];
col =[1:1:griddim(2)]; 

% convert the grid in LAT LON files (Not important for the BN but may be needed for the WEB viewer)
[xWGS,yWGS]=convertCoordinates(reshape(x,griddim(1)*griddim(2),1),...
                               reshape(y,griddim(1)*griddim(2),1),...
                               'CS1.code',proj1,'CS2.code',proj2); % CS1 and CS2 codes are site spesific 
lon=reshape(xWGS,griddim(1),griddim(2));
lat=reshape(yWGS,griddim(1),griddim(2));

%% flipdata to match FEWS reading order 
%(IMPORTANT if posibble compare FEWS netcdf with matlab netcdf)
lat=flipud(lat);
lon=flipud(lon);
Zsmax=flipdim(Zsmax,2);
sederomin=flipdim(sederomin,2);
qxmax=flipdim(qxmax,2);

%% CREATE EMPTY NETCDF FILE
nc_create_empty(ncfile);


% Matlab script based on the netCDF kickstarter
% at 2015-12-14T12:20Z
% http://publicwiki.deltares.nl/display/OET/netCDF%20kickstarter


%% ADD DIMENSIONS%% ADD DIMENSIONS
nc_adddim(ncfile, 'time', 1);
nc_adddim(ncfile, 'analysis_time', 1);
nc_adddim(ncfile, 'row', length(row));
nc_adddim(ncfile, 'col', length(col));
%% ADD GLOBAL ATTRIBUTES
% see http://www.unidata.ucar.edu/software/thredds/current/netcdf-java/formats/DataDiscoveryAttConvention.html
nc_attput(ncfile, nc_global, 'Conventions', 'CF-1.6');
nc_attput(ncfile, nc_global, 'title', 'Data');
nc_attput(ncfile, nc_global, 'institution', 'UAlg');
nc_attput(ncfile, nc_global, 'source', 'Export NETCDF-CF_GRID that mimic FEWS output');
nc_attput(ncfile, nc_global, 'history', [datestr(now,31) ' exported']);
nc_attput(ncfile, nc_global, 'references', '');
nc_attput(ncfile, nc_global, 'Metadata_Conventions', 'Unidata Dataset Discovery v1.0');
nc_attput(ncfile, nc_global, 'summary', 'Data exported WITHOUT using FEWS');
nc_attput(ncfile, nc_global, 'date_created', datestr(now,31));

%% ADD VARIABLES
var = struct(...
    'Name', 'time',...
    'Datatype', 'double',...
    'Dimension', {{'time'}}); % note the double brackets
nc_addvar(ncfile, var);
nc_attput(ncfile, 'time', 'long_name', '');
nc_attput(ncfile, 'time', 'standard_name', 'time');
nc_attput(ncfile, 'time', 'units', 'minutes since 1970-01-01 00:00:00.0 +0000');
nc_attput(ncfile, 'time', 'axis', 'T');

var = struct(...
    'Name', 'analysis_time',...
    'Datatype', 'double',...
    'Dimension', {{'analysis_time'}}); % note the double brackets
nc_addvar(ncfile, var);
nc_attput(ncfile, 'analysis_time', 'long_name', 'forecast_reference_time');
nc_attput(ncfile, 'analysis_time', 'standard_name', 'forecasr_reference_time');
nc_attput(ncfile, 'analysis_time', 'units', 'minutes since 1970-01-01 00:00:00.0 +0000');

var = struct(...
    'Name', 'row',...
    'Datatype', 'double',...
    'Dimension', {{'row'}}); % note the double brackets
nc_addvar(ncfile, var);
nc_attput(ncfile, 'row', 'long_name', 'row indices');
nc_attput(ncfile, 'row', 'standard_name', 'projection_y_coordinate');
nc_attput(ncfile, 'row', 'units', '1');
nc_attput(ncfile, 'row', 'axis', 'Y');

var = struct(...
    'Name', 'col',...
    'Datatype', 'double',...
    'Dimension', {{'col'}}); % note the double brackets
nc_addvar(ncfile, var);
nc_attput(ncfile, 'col', 'long_name', 'column indices');
nc_attput(ncfile, 'col', 'standard_name', 'projection_x_coordinate');
nc_attput(ncfile, 'col', 'units', '1');
nc_attput(ncfile, 'col', 'axis', 'X');

var = struct(...
    'Name', 'lat',...
    'Datatype', 'double',...
    'Dimension', {{'row' 'col'}}); % note the double brackets
nc_addvar(ncfile, var);
nc_attput(ncfile, 'lat', 'long_name', 'latitude');
nc_attput(ncfile, 'lat', 'standard_name', 'latitude');
nc_attput(ncfile, 'lat', 'units', 'degrees_north');
nc_attput(ncfile, 'lat', '_FillValue', 9.96921e+03);

var = struct(...
    'Name', 'lon',...
    'Datatype', 'double',...
    'Dimension', {{'row' 'col'}}); % note the double brackets
nc_addvar(ncfile, var);
nc_attput(ncfile, 'lon', 'long_name', 'longitude');
nc_attput(ncfile, 'lon', 'standard_name', 'longitude');
nc_attput(ncfile, 'lon', 'units', 'degrees_east');
nc_attput(ncfile, 'lon', '_FillValue', 9.96920996838687e+036);

var = struct(...
    'Name', 'crs',...
    'Datatype', 'int32',...
    'Dimension', {{}}); % note the double brackets
nc_addvar(ncfile, var);
nc_attput(ncfile, 'crs', 'long_name', 'coordinate referencesystem');
nc_attput(ncfile, 'crs', 'grid_mapping_name', 'latitude_longitude');
nc_attput(ncfile, 'crs', 'longitude_of_prime_meriduan', 0);
nc_attput(ncfile, 'crs', 'semi_major_axis', 6378127);
nc_attput(ncfile, 'crs', 'inverse_flattening', 298.257223563);
nc_attput(ncfile, 'crs', 'crs_wkt', ['GEOGCS["WGS 84",' char(10) 'DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],' char(10) 'PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],' char(10) 'UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],' char(10) 'AUTHORITY["EPSG","4326"]]']);
nc_attput(ncfile, 'crs', 'proj4_params', '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs');
nc_attput(ncfile, 'crs', 'epsg_code', 'EPSG:4326');

var = struct(...
    'Name', 'Zsmax',...
    'Datatype', 'double',...
    'Dimension', {{'time','row','col'}}); % note the double brackets
nc_addvar(ncfile, var);
nc_attput(ncfile, 'Zsmax', 'long_name', 'Zsmax');
nc_attput(ncfile, 'Zsmax', 'units', 'm');
nc_attput(ncfile, 'Zsmax', 'coordinates', 'lat lon analysis_time');
nc_attput(ncfile, 'Zsmax', 'grid_mapping', 'crs');
nc_attput(ncfile, 'Zsmax', '_FillValue', 9.96920996838687e+036);

var = struct(...
    'Name', 'sederomin',...
    'Datatype', 'double',...
    'Dimension', {{'time','row','col'}}); % note the double brackets
nc_addvar(ncfile, var);
nc_attput(ncfile, 'sederomin', 'long_name', 'sederomin');
nc_attput(ncfile, 'sederomin', 'units', 'm');
nc_attput(ncfile, 'sederomin', 'coordinates', 'lat lon analysis_time');
nc_attput(ncfile, 'sederomin', 'grid_mapping', 'crs');
nc_attput(ncfile, 'sederomin', '_FillValue', 9.96920996838687e+036);

var = struct(...
    'Name', 'qxmax',...
    'Datatype', 'double',...
    'Dimension', {{'time','row','col'}}); % note the double brackets
nc_addvar(ncfile, var);
nc_attput(ncfile, 'qxmax', 'long_name', 'qxmax');
nc_attput(ncfile, 'qxmax', 'units', 'm2/s');
nc_attput(ncfile, 'qxmax', 'coordinates', 'lat lon analysis_time');
nc_attput(ncfile, 'qxmax', 'grid_mapping', 'crs');
nc_attput(ncfile, 'qxmax', '_FillValue', 9.96920996838687e+036);

%% Store Variables
nc_varput(ncfile, 'time', time)
nc_varput(ncfile, 'analysis_time', analysis_time)
nc_varput(ncfile, 'row', row)
nc_varput(ncfile, 'col', col)
nc_varput(ncfile, 'lat', lat)
nc_varput(ncfile, 'lon', lon)
nc_varput(ncfile, 'Zsmax', Zsmax)
nc_varput(ncfile, 'sederomin', sederomin)
nc_varput(ncfile, 'qxmax', qxmax)

