%FOR MAC USERS: COMMENT OUT LINE 9 AND COMMENT IN LINE 10
%ALSO MAC USERS: CHECK THE FILE PATH IN loadSNRdata.m (INSTRUCTIONS IN
%FILE)
%Generates 12 consecutive years of PBLH estimates for selected station,
%given the availability of SNR data. User selects start year.
function[pblHeightVector,timeVector,utcTimeVector,vertResolutionVector,NegSnrIndicatorVector,cldIndicatorVector,diurnalCyclesAll2,datesAll2,cldIndicators2,cycHours,stationNum,lat,lon,alt]=createGRWPpblh(stationNum,startYear)
    [heightVec_nc1,heightVec_nc2,heightVec_nc3,heightVec_nc4,heightVec_nc5,heightVec_nc6,heightVec_nc7,heightVec_nc8,heightVec_nc9,heightVec_nc10,heightVec_nc11,heightVec_nc12,utcTime_nc1,utcTime_nc2,utcTime_nc3,utcTime_nc4,utcTime_nc5,utcTime_nc6,utcTime_nc7,utcTime_nc8,utcTime_nc9,utcTime_nc10,utcTime_nc11,utcTime_nc12,localTime_nc1,localTime_nc2,localTime_nc3,localTime_nc4,localTime_nc5,localTime_nc6,localTime_nc7,localTime_nc8,localTime_nc9,localTime_nc10,localTime_nc11,localTime_nc12,cloud_fracs_nc1,cloud_fracs_nc2,cloud_fracs_nc3,cloud_fracs_nc4,cloud_fracs_nc5,cloud_fracs_nc6,cloud_fracs_nc7,cloud_fracs_nc8,cloud_fracs_nc9,cloud_fracs_nc10,cloud_fracs_nc11,cloud_fracs_nc12,snr_grid_nc1,snr_grid_nc2,snr_grid_nc3,snr_grid_nc4,snr_grid_nc5,snr_grid_nc6,snr_grid_nc7,snr_grid_nc8,snr_grid_nc9,snr_grid_nc10,snr_grid_nc11,snr_grid_nc12,mask_for_snr1,mask_for_snr2,mask_for_snr3,mask_for_snr4,mask_for_snr5,mask_for_snr6,mask_for_snr7,mask_for_snr8,mask_for_snr9,mask_for_snr10,mask_for_snr11,mask_for_snr12,stationNum,lat,lon,alt]=loadStationRecordNC(stationNum,startYear);
    [diurnalCyclesAll2,datesAll2,cldIndicators2,vertResolutionVector,NegSnrIndicatorVector,cycHours,timeVector,utcTimeVector,pblHeightVector,cldIndicatorVector,stationNum,lat,lon,alt,~,~,~,~,~,not30IndSum]=buildDiurnalCyclesDatabaseNC(heightVec_nc1,heightVec_nc2,heightVec_nc3,heightVec_nc4,heightVec_nc5,heightVec_nc6,heightVec_nc7,heightVec_nc8,heightVec_nc9,heightVec_nc10,heightVec_nc11,heightVec_nc12,utcTime_nc1,utcTime_nc2,utcTime_nc3,utcTime_nc4,utcTime_nc5,utcTime_nc6,utcTime_nc7,utcTime_nc8,utcTime_nc9,utcTime_nc10,utcTime_nc11,utcTime_nc12,localTime_nc1,localTime_nc2,localTime_nc3,localTime_nc4,localTime_nc5,localTime_nc6,localTime_nc7,localTime_nc8,localTime_nc9,localTime_nc10,localTime_nc11,localTime_nc12,cloud_fracs_nc1,cloud_fracs_nc2,cloud_fracs_nc3,cloud_fracs_nc4,cloud_fracs_nc5,cloud_fracs_nc6,cloud_fracs_nc7,cloud_fracs_nc8,cloud_fracs_nc9,cloud_fracs_nc10,cloud_fracs_nc11,cloud_fracs_nc12,snr_grid_nc1,snr_grid_nc2,snr_grid_nc3,snr_grid_nc4,snr_grid_nc5,snr_grid_nc6,snr_grid_nc7,snr_grid_nc8,snr_grid_nc9,snr_grid_nc10,snr_grid_nc11,snr_grid_nc12,mask_for_snr1,mask_for_snr2,mask_for_snr3,mask_for_snr4,mask_for_snr5,mask_for_snr6,mask_for_snr7,mask_for_snr8,mask_for_snr9,mask_for_snr10,mask_for_snr11,mask_for_snr12,startYear,stationNum,lat,lon,alt);
    ncid = netcdf.create(strcat('..\..\GRWP_PBLH_NetCDFs\GRWPPBLH_',int2str(stationNum),'.nc'),'NOCLOBBER');
    %ncid = netcdf.create(strcat('../../GRWP_PBLH_NetCDFs/GRWPPBLH_',int2str(stationNum),'.nc'),'NOCLOBBER');
    %global attributes
    netcdf.putAtt(ncid,netcdf.getConstant('GLOBAL'),'Title',strcat('GRWP-PBLH Data: Station No.', int2str(stationNum)));
    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',1);
    time_dimID = netcdf.defDim(ncid,'time',size(timeVector,1));

    %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);

    %altitude (lev)
    netcdf.reDef(ncid);
    varid = netcdf.defVar(ncid,'lev','NC_FLOAT',lev_dimID);
    netcdf.putAtt(ncid,varid,'long_name','Altitude');
    netcdf.putAtt(ncid,varid,'units','meters');
    netcdf.endDef(ncid);
    netcdf.putVar(ncid,varid,alt);


    %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',"hours since 2009-05-17 00:00:00");
    netcdf.putAtt(ncid,varid,'calendar',"standard");
    netcdf.endDef(ncid);
    netcdf.putVar(ncid,varid,utcTimeVector);

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

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


    %PBL height
    netcdf.reDef(ncid);
    varid = netcdf.defVar(ncid,'pblh','NC_FLOAT',[lon_dimID lat_dimID lev_dimID time_dimID]);
    netcdf.putAtt(ncid,varid,'long_name','pbl height estimate');
    netcdf.putAtt(ncid,varid,'units','meters');
    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,pblHeightVector);


    %cloud_indicators
    netcdf.reDef(ncid);
    varid = netcdf.defVar(ncid,'cloud_fraction','NC_FLOAT',[lon_dimID lat_dimID lev_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,cldIndicatorVector);

    %vert_resolution_2x
    netcdf.reDef(ncid);
    varid = netcdf.defVar(ncid,'vert_resolution_2x','NC_FLOAT',[lon_dimID lat_dimID lev_dimID time_dimID]);
    netcdf.putAtt(ncid,varid,'long_name','2x smoothed vertical resolution of profile (error estimate)');
    netcdf.putAtt(ncid,varid,'units','meters');
    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,vertResolutionVector);

    %negativeSNR_indicator
    netcdf.reDef(ncid);
    varid = netcdf.defVar(ncid,'negativeSNR_indicator','NC_FLOAT',[lon_dimID lat_dimID lev_dimID time_dimID]);
    netcdf.putAtt(ncid,varid,'long_name','negative indicator (0: no negative SNR value below absolute max of SNR, 1: has negative SNR value below absolute max of SNR');
    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,NegSnrIndicatorVector);

    netcdf.close(ncid)
end

