function [lon] = CN_assign( ref, Yr, lon_w, lon_e, lat_s, lat_n, Rain250Name, CN250_unassigned, CN250_assigned, season_code) formatIn = 'dd mmm yyyy'; startday = datenum(['1-jan-',num2str(Yr)], formatIn); endday = datenum(['31-dec-',num2str(Yr)], formatIn); D =(startday:endday)'; L = (1:yeardays(Yr)); % create season (dormant & growing) array season = NaN((yeardays(Yr)),1); % 1 is dormant season, 2 growing season % set all of 'season' to dormant season %season_code = string(table2array(t(18,2))); season(:,:) = season_code; %% Global Curve Number (gcn) - clip lon and lat to limits; assume ARCI, ARCII, ARCIII have same extents %load longitude array; Global Curve Number (gcn) lon_gcn = double(ncread(Rain250Name,'longitude')); % load latitude array; Global Curve Number (gcn) lat_gcn = double(ncread(Rain250Name,'latitude')); % determine which indices correspond to dimension 1: Global Curve Number (gcn) ind1_gcn = find(lon_gcn>= lon_w & lon_gcn <= lon_e); % determine which indices correspond to dimension 2: Global Curve Number (gcn) ind2_gcn = find(lat_gcn>= lat_s & lat_gcn <= lat_n); % Clip lat and lon to their specified range lat_gcn = lat_gcn(ind2_gcn); lon_gcn = lon_gcn(ind1_gcn); %% Output NC file, create variables and write attributes % create variable to hold Curve Number nccreate(CN250_assigned, 'CurveNumber', 'Dimensions',{'longitude',length(lon_gcn), 'latitude',length(lat_gcn),'time', (Inf)},'DataType', 'int8'); ncwriteatt(CN250_assigned, 'CurveNumber', "units", ("dimentionless")); % create dimensions nccreate(CN250_assigned, 'longitude', 'Dimensions',{'longitude', length(lon_gcn)}); % longitude nccreate(CN250_assigned, 'latitude', 'Dimensions',{'latitude', length(lat_gcn)}); % latitude nccreate(CN250_assigned, 'time', 'Dimensions',{'time', Inf}); % create time dimension % Output NC file, write dimensions ncwrite(CN250_assigned, 'longitude', lon_gcn); % write longitude dimension ncwrite(CN250_assigned, 'latitude', lat_gcn); % write latitude dimension ncwrite(CN250_assigned, 'time', D); % write time %% Calculate Curve Number % read Global Curve Number data ARCI = int8(ncread(CN250_unassigned, 'GCN_ARCI', [ind1_gcn(1), ind2_gcn(1)], [length(ind1_gcn), length(ind2_gcn)])); ARCII = int8(ncread(CN250_unassigned, 'GCN_ARCII', [ind1_gcn(1), ind2_gcn(1)], [length(ind1_gcn), length(ind2_gcn)])); ARCIII = int8(ncread(CN250_unassigned, 'GCN_ARCIII', [ind1_gcn(1), ind2_gcn(1)], [length(ind1_gcn), length(ind2_gcn)])); cn = int8(ones(length(lon_gcn), length(lat_gcn))*127); for i=1:yeardays(Yr) cn = int8(ones(length(lon_gcn), length(lat_gcn))*127); % read NC file rs = int16(ncread(Rain250Name, 'RainSum250', [ind1_gcn(1), ind2_gcn(1), i], [length(ind1_gcn), length(ind2_gcn), 1])); if season(i,1) == 1 % DORMANT season dry = find(rs<13); % dry cn(dry) = ARCI(dry); % set Curve Number clear dry; norm = find(rs>=13 & rs<=28); % normal cn(norm) = ARCII(norm); % set Curve NUmber clear norm wet = find(rs>28); % wet cn(wet) = ARCIII(wet); % set curve number clear wet; elseif season(i,1) == 2 % GROWING season dry = find(rs<36); % dry cn(dry) = ARCI(dry); % set curve number clear dry; norm = find(rs>=36 & rs<=53); % normal cn(norm) = ARCII(norm); % set curve number clear norm; wet = find(rs>53); % wet cn(wet) = ARCIII(wet); % set curve number clear wet; end % write to NC file ncwrite(CN250_assigned, 'CurveNumber', cn, [1 1 i]); clear cn; end % Reference: Silveira, et al., (2000) https://doi.org/10.1080/02626660009492302 % Total 5-day antecedent rainfall (mm) % for DORMANT season; Dry less than 13; normal 13 to 28; wet more than 28. % for GROWING season; Dry less than 36; normal 36 to 53; wet more than 53. end % created by robert g. delaney (2023) % This work is licensed under the Creative Commons Attribution-% % NonCommercial-ShareAlike 4.0 International License. % To view a copy of this license, visit % http://creativecommons.org/licenses/by-nc-sa/4.0/ % or send a letter to Creative Commons, PO Box 1866, % Mountain View, CA 94042, USA.