%FOR MAC USERS: COMMENT OUT LINE 6 AND COMMENT IN LINE 7
%ALSO MAC USERS: CHECK m_srbParseNC.m and fileFinder.m for file path
%adjustments.
function[timesAll,timesAllUTC,fullGrid,fullGridRaw,datesAll,cloud_fracs_all,heightvectorTot,stationNum,lat,lon,alt,filename]=createGRWPsnr(stationNum,year)
    [timesAll,timesAllUTC,fullGrid,fullGridRaw,datesAll,cloud_fracs_all,heightvectorTot,stationNum,lat,lon,alt,year,~]=buildInterpolatedSNRData(stationNum, year);
    [filename]=strcat('..\..\GRWP_SNR_NetCDFs\GRWP_SNRgrid_',int2str(stationNum),'_',int2str(year),'.nc');
    %[filename]=strcat('../../GRWP_SNR_NetCDFs/GRWP_SNRgrid_',int2str(stationNum),'_',int2str(year),'.nc');
    ncid = netcdf.create(filename,'NOCLOBBER');
    %global attributes
    netcdf.putAtt(ncid,netcdf.getConstant('GLOBAL'),'Title',strcat('GRWP-PBLH SNR Grid:', int2str(stationNum),',',int2str(year)));
    netcdf.putAtt(ncid,netcdf.getConstant('GLOBAL'),'Institution','Department of Geography and Environmental Science at Hunter College of CUNY');
    netcdf.putAtt(ncid,netcdf.getConstant('GLOBAL'),'Conventions','COARDS');
    netcdf.putAtt(ncid,netcdf.getConstant('GLOBAL'),'History',datestr(now));
    netcdf.putAtt(ncid,netcdf.getConstant('GLOBAL'),'Station Number',int2str(stationNum));
    netcdf.putAtt(ncid,netcdf.getConstant('GLOBAL'),'Station Latitude',num2str(lat));
    netcdf.putAtt(ncid,netcdf.getConstant('GLOBAL'),'Station Longitude',num2str(lon));
    netcdf.putAtt(ncid,netcdf.getConstant('GLOBAL'),'Station Altitude',strcat(num2str(alt),'m'));

    %dimensions
    lon_dimID = netcdf.defDim(ncid,'lon',1);
    lat_dimID = netcdf.defDim(ncid,'lat',1);
    lev_dimID = netcdf.defDim(ncid,'lev',size(heightvectorTot,2));
    time_dimID = netcdf.defDim(ncid,'time',size(timesAll,2));

    %variables
    %longitude
    varid = netcdf.defVar(ncid,'lon','NC_FLOAT',lon_dimID);
    netcdf.putAtt(ncid,varid,'long_name','Longitude');
    netcdf.putAtt(ncid,varid,'units','degrees_east');
    netcdf.endDef(ncid);
    netcdf.putVar(ncid,varid,lon);

    %latitude
    netcdf.reDef(ncid);
    varid = netcdf.defVar(ncid,'lat','NC_FLOAT',lat_dimID);
    netcdf.putAtt(ncid,varid,'long_name','Latitude');
    netcdf.putAtt(ncid,varid,'units','degrees_north');
    netcdf.endDef(ncid);
    netcdf.putVar(ncid,varid,lat);

    %height (lev)
    netcdf.reDef(ncid);
    varid = netcdf.defVar(ncid,'lev','NC_FLOAT',lev_dimID);
    netcdf.putAtt(ncid,varid,'long_name','Height');
    netcdf.putAtt(ncid,varid,'units','meters');
    netcdf.endDef(ncid);
    netcdf.putVar(ncid,varid,heightvectorTot');


    %time
    netcdf.reDef(ncid);
    varid = netcdf.defVar(ncid,'time','NC_FLOAT',time_dimID);
    netcdf.putAtt(ncid,varid,'long_name','time');
    netcdf.putAtt(ncid,varid,'units',"minutes since 2009-05-17 00:00:00");
    netcdf.putAtt(ncid,varid,'calendar',"standard");
    netcdf.endDef(ncid);
    netcdf.putVar(ncid,varid,timesAllUTC');

    %time_grads
    netcdf.reDef(ncid);
    varid = netcdf.defVar(ncid,'time_grads','NC_FLOAT',[lon_dimID lat_dimID time_dimID]);
    netcdf.putAtt(ncid,varid,'long_name','time vector (UTC) repeated for use in GRADS');
    netcdf.putAtt(ncid,varid,'units',"minutes since 2009-05-17 00:00:00 (UTC)");
    netcdf.endDef(ncid);
    netcdf.putVar(ncid,varid,timesAllUTC');

    %time_local
    netcdf.reDef(ncid);
    varid = netcdf.defVar(ncid,'time_local','NC_FLOAT',[lon_dimID lat_dimID time_dimID]);
    netcdf.putAtt(ncid,varid,'long_name','local time vector');
    netcdf.putAtt(ncid,varid,'units',"minutes since 2009-05-17 00:00:00 (local)");
    netcdf.endDef(ncid);
    netcdf.putVar(ncid,varid,timesAll');

    %cloud_fractions
    netcdf.reDef(ncid);
    varid = netcdf.defVar(ncid,'cloud_fraction','NC_FLOAT',[lon_dimID lat_dimID time_dimID]);
    netcdf.putAtt(ncid,varid,'long_name','total cloud area fraction (obtained from CERES_SYN1deg_Ed4.1 3-hourly)');
    netcdf.putAtt(ncid,varid,'units','1');
    netcdf.defVarFill(ncid,varid,false,1.e+15);
    netcdf.putAtt(ncid,varid,'missing_value',1.e+15);
    netcdf.putAtt(ncid,varid,'fmissing_value',1.e+15);
    netcdf.endDef(ncid);
    netcdf.putVar(ncid,varid,cloud_fracs_all);


    %SNR grid
    netcdf.reDef(ncid);
    varid = netcdf.defVar(ncid,'snr_grid','NC_FLOAT',[lon_dimID lat_dimID lev_dimID time_dimID]);
    netcdf.putAtt(ncid,varid,'long_name','interpolated SNR grid');
    netcdf.putAtt(ncid,varid,'units','dB');
    netcdf.defVarFill(ncid,varid,false,1.e+15);
    netcdf.putAtt(ncid,varid,'missing_value',1.e+15);
    netcdf.putAtt(ncid,varid,'fmissing_value',1.e+15);
    netcdf.endDef(ncid);
    netcdf.putVar(ncid,varid,fullGrid);

    %SNR raw data mask
    netcdf.reDef(ncid);
    varid = netcdf.defVar(ncid,'mask_for_raw_snr','NC_FLOAT',[lon_dimID lat_dimID lev_dimID time_dimID]);
    netcdf.putAtt(ncid,varid,'long_name','Same size as snr_grid. Contains 0s and 1s to indicate locations of raw SNR data values');
    netcdf.putAtt(ncid,varid,'units','1');
    netcdf.endDef(ncid);
    netcdf.putVar(ncid,varid,fullGridRaw);


    netcdf.close(ncid)
end

