
function func_saveasnetcdf(lat2d,lon2d,array,array_name)
%% Save as netcdf file. 
% array_name is the matrix name that will be saved. 
% array is the matrix that is going to be saved.
% 
%% 
% [lat2d, lon2d]= meshgrid(lat,lon);
array_save_name = [array_name, '.nc'];
array(isnan(array)) = -9999;


ncid = netcdf.create(array_save_name,'NC_WRITE');
% define dimension
lat_dimid = netcdf.defDim(ncid,'lat',size(lat2d,2));
lon_dimid = netcdf.defDim(ncid,'lon',size(lat2d,1));
case_dimid = netcdf.defDim(ncid,'case',5);
sson_dimid = netcdf.defDim(ncid,'sson',5 );

% 
% define variables
% latid = netcdf.defVar(ncid,'lat','NC_DOUBLE',lat_dimid);
% lonid = netcdf.defVar(ncid,'lon','NC_DOUBLE',lon_dimid);
lon2did = netcdf.defVar(ncid,'lon2d','NC_DOUBLE',[lon_dimid,lat_dimid]);
lat2did = netcdf.defVar(ncid,'lat2d','NC_DOUBLE',[lon_dimid,lat_dimid]);

var1id = netcdf.defVar(ncid, array_name,'NC_DOUBLE',[lon_dimid lat_dimid case_dimid sson_dimid]);


% netcdf.putAtt(ncid,lonid,'standard_name','longitude');
% netcdf.putAtt(ncid,lonid,'long_name','longitude');
% netcdf.putAtt(ncid,lonid,'units','degrees_east');
% netcdf.putAtt(ncid,lonid,'axis','X');
% 
% netcdf.putAtt(ncid,latid,'standard_name','latitude');
% netcdf.putAtt(ncid,latid,'long_name','latitude');
% netcdf.putAtt(ncid,latid,'units','degrees_north');
% netcdf.putAtt(ncid,latid,'axis','Y');

netcdf.putAtt(ncid,lon2did,'standard_name','longitude');
netcdf.putAtt(ncid,lon2did,'long_name','longitude');
netcdf.putAtt(ncid,lon2did,'units','degrees_east');
netcdf.putAtt(ncid,lon2did,'axis','X');

netcdf.putAtt(ncid,lat2did,'standard_name','latitude');
netcdf.putAtt(ncid,lat2did,'long_name','latitude');
netcdf.putAtt(ncid,lat2did,'units','degrees_north');
netcdf.putAtt(ncid,lat2did,'axis','Y');

netcdf.putAtt(ncid,var1id,'_FillValue',-9999);

netcdf.endDef(ncid);

% netcdf.putVar(ncid,latid,lat);
% netcdf.putVar(ncid,lonid,lon);
netcdf.putVar(ncid,lat2did,lat2d);
netcdf.putVar(ncid,lon2did,lon2d);
netcdf.putVar(ncid,var1id,array);

netcdf.close(ncid)

end
