function [nemis, efreq, seaemis] = cal_seaemis2(zang, wspeed);

%
% function [nemis, efreq, emis] = cal_seaemis2(zang, wspeed);
%
% Calculates the sea surface emissivity in the IR.
% Version2: includes wind speed as an input.
%
% Input:
%    zang   = (1 x n) desired zenith angles (0-85 degrees)
%    wspeed = (1 x n) desired wind speed (0-15 m/s or specular if < 0)
%
% Output:
%    nemis = (1 x n) number of emissivity points [always 19]
%    efreq = (nemis x n) frequencies for emissivity points (cm^-1)
%    emis  = (nemis x n) emissivity points
%
% Note: zang > 85 uses 85, and wspeed > 15 uses 15.
%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Sea emissivity data
%
% From "sea_emis.m" supplied by David Tobin, 12 November 2002
% Data re-arranged from 19 tables (one per wavelength) into seven
% tables (one per wind speed).

% SEA WATER EMISSIVITY AT  3.5 MICRON (REFRACTIVE INDEX=1.406-.0094i)
% SEA WATER EMISSIVITY AT  3.6 MICRON (REFRACTIVE INDEX=1.391-.0052i)
% SEA WATER EMISSIVITY AT  3.7 MICRON (REFRACTIVE INDEX=1.380-.0036i)
% SEA WATER EMISSIVITY AT  3.8 MICRON (REFRACTIVE INDEX=1.370-.0034i)
% SEA WATER EMISSIVITY AT  3.9 MICRON (REFRACTIVE INDEX=1.363-.0038i)
% SEA WATER EMISSIVITY AT  4.0 MICRON (REFRACTIVE INDEX=1.357-.0046i)
% SEA WATER EMISSIVITY AT  4.1 MICRON (REFRACTIVE INDEX=1.352-.0056i)
% SEA WATER EMISSIVITY AT  8.0 MICRON (REFRACTIVE INDEX=1.297-.0343i)
% SEA WATER EMISSIVITY AT  8.4 MICRON (REFRACTIVE INDEX=1.287-.0361i)
% SEA WATER EMISSIVITY AT  8.8 MICRON (REFRACTIVE INDEX=1.275-.0385i)
% SEA WATER EMISSIVITY AT  9.2 MICRON (REFRACTIVE INDEX=1.262-.0405i)
% SEA WATER EMISSIVITY AT  9.6 MICRON (REFRACTIVE INDEX=1.247-.0444i)
% SEA WATER EMISSIVITY AT 10.0 MICRON (REFRACTIVE INDEX=1.227-.0498i)
% SEA WATER EMISSIVITY AT 10.5 MICRON (REFRACTIVE INDEX=1.194-.0642i)
% SEA WATER EMISSIVITY AT 11.0 MICRON (REFRACTIVE INDEX=1.162-.0938i)
% SEA WATER EMISSIVITY AT 11.5 MICRON (REFRACTIVE INDEX=1.134-.1375i)
% SEA WATER EMISSIVITY AT 12.0 MICRON (REFRACTIVE INDEX=1.118-.1900i)
% SEA WATER EMISSIVITY AT 12.5 MICRON (REFRACTIVE INDEX=1.129-.2490i)
% SEA WATER EMISSIVITY AT 13.0 MICRON (REFRACTIVE INDEX=1.150-.2950i)

% Wavelength (um) and wavenumbers (cm^-1)
wl=[3.5 3.6 3.7 3.8 3.9 4.0 4.1 8.0 8.4 8.8 9.2 9.6 10 10.5 11 11.5 ...
    12 12.5 13]';
wn=1e4./wl;

% Angles
angles=[0 10 20 30 40 50 60 70 75 80 85];

%%%%%%%%%%
% Specular
%      0    10    20    30    40    50    60    70    75    80    85
es=[ 97151 97150 97129 97022 96657 95596 92693 84831 76717 63130 40054
     97325 97324 97304 97201 96847 95813 92963 85182 77100 63509 40336
     97451 97449 97430 97329 96984 95970 93162 85446 77392 63800 40556
     97563 97561 97542 97444 97107 96112 93344 85691 77666 64076 40765
     97640 97639 97620 97524 97192 96211 93472 85866 77862 64276 40919
     97706 97704 97686 97591 97264 96296 93583 86018 78034 64452 41055
     97760 97759 97740 97647 97324 96366 93675 86146 78180 64602 41171
     98306 98305 98290 98211 97935 97095 94663 87581 79851 66364 42574
     98401 98400 98385 98309 98042 97227 94849 87868 80195 66738 42881
     98511 98510 98496 98423 98167 97381 95072 88218 80619 67205 43269
     98627 98626 98613 98544 98300 97548 95316 88611 81102 67743 43723
     98753 98752 98740 98676 98446 97732 95591 89063 81665 68378 44265
     98912 98911 98900 98841 98630 97969 95954 89678 82444 69272 45040
     99133 99133 99123 99073 98891 98309 96488 90615 83650 70681 46287
     99252 99251 99242 99196 99027 98477 96725 90960 84043 71080 46591
     99194 99193 99183 99131 98938 98312 96326 89946 82530 69089 44661
     98894 98893 98879 98807 98541 97694 95120 87450 79163 65064 41082
     98288 98287 98267 98161 97784 96632 93359 84472 75543 61149 37906
     97674 97673 97647 97514 97051 95687 91990 82499 73329 58928 36227];

%%%%%%%%%%%%%%%%%%
% Wind speed 0 m/s
e0=[ 97151 97149 97126 97016 96643 95566 92638 84800 76844 63932 44332
     97325 97324 97302 97194 96833 95783 92908 85149 77223 64307 44631
     97451 97449 97427 97323 96970 95940 93107 85410 77511 64594 44863
     97562 97561 97540 97438 97093 96083 93289 85654 77781 64866 45084
     97640 97638 97618 97518 97178 96182 93417 85827 77975 65063 45245
     97705 97704 97684 97586 97251 96267 93528 85978 78145 65237 45387
     97760 97758 97738 97641 97311 96337 93620 86105 78288 65384 45509
     98306 98305 98288 98206 97923 97069 94609 87529 79935 67113 46969
     98401 98399 98383 98305 98030 97200 94795 87813 80273 67479 47286
     98511 98509 98494 98419 98156 97356 95018 88160 80691 67935 47685
     98627 98626 98611 98540 98290 97523 95263 88550 81166 68460 48151
     98753 98752 98739 98672 98436 97708 95539 88999 81719 69080 48705
     98912 98911 98898 98837 98621 97946 95902 89609 82484 69949 49494
     99133 99132 99122 99070 98883 98288 96438 90538 83669 71319 50757
     99252 99251 99241 99193 99019 98456 96675 90881 84056 71712 51077
     99194 99193 99182 99128 98929 98289 96272 89873 82576 69797 49162
     98894 98893 98878 98802 98529 97665 95059 87400 79277 65895 45539
     98288 98287 98265 98154 97768 96597 93296 84453 75721 62064 42250
     97674 97672 97644 97506 97032 95648 91928 82499 73538 59873 40479];

%%%%%%%%%%%%%%%%%%
% Wind speed 1 m/s
e1=[ 97151 97148 97122 97004 96617 95514 92547 84766 77115 65445 50159
     97325 97323 97297 97184 96808 95731 92817 85110 77487 65814 50479
     97450 97448 97423 97313 96946 95889 93016 85369 77769 66097 50726
     97562 97560 97536 97428 97069 96032 93198 85609 78033 66364 50962
     97640 97637 97614 97508 97155 96132 93326 85780 78223 66558 51133
     97705 97703 97680 97576 97228 96217 93437 85929 78389 66727 51284
     97759 97757 97735 97631 97288 96288 93529 86055 78530 66871 51412
     98306 98304 98285 98198 97903 97023 94518 87459 80137 68556 52944
     98401 98399 98380 98296 98010 97155 94705 87739 80466 68911 53274
     98510 98509 98491 98411 98137 97312 94929 88081 80873 69353 53688
     98627 98625 98609 98532 98271 97481 95174 88466 81335 69861 54169
     98753 98751 98736 98665 98418 97667 95451 88908 81873 70458 54738
     98911 98910 98896 98831 98605 97907 95815 89510 82617 71295 55546
     99133 99132 99120 99064 98868 98252 96354 90427 83767 72610 56832
     99252 99251 99240 99188 99005 98421 96591 90763 84145 72995 57173
     99194 99193 99180 99122 98914 98249 96180 89769 82718 71188 55275
     98894 98892 98875 98794 98508 97615 94957 87337 79531 67460 51595
     98288 98286 98260 98143 97740 96536 93192 84442 76077 63744 48163
     97674 97671 97639 97492 97000 95582 91827 82519 73943 61591 46275];

%%%%%%%%%%%%%%%%%%
% Wind speed 3 m/s
e3=[ 97150 97146 97113 96982 96566 95410 92376 84777 77845 68402 57403
     97324 97320 97289 97161 96758 95628 92646 85113 78205 68765 57743
     97450 97445 97415 97291 96896 95788 92844 85365 78478 69042 58004
     97562 97558 97528 97407 97021 95932 93026 85599 78733 69302 58251
     97639 97635 97606 97487 97107 96032 93154 85766 78916 69491 58430
     97705 97701 97672 97555 97180 96118 93264 85911 79076 69655 58588
     97759 97755 97727 97611 97241 96189 93356 86034 79211 69795 58722
     98306 98302 98279 98180 97861 96931 94345 87401 80751 71420 60304
     98400 98397 98374 98279 97970 97065 94532 87673 81066 71760 60641
     98510 98507 98485 98395 98098 97224 94757 88005 81453 72181 61061
     98626 98623 98603 98517 98234 97395 95003 88379 81893 72664 61546
     98753 98750 98731 98650 98383 97584 95281 88808 82404 73229 62119
     98911 98909 98891 98817 98571 97827 95648 89393 83109 74019 62927
     99133 99131 99116 99053 98838 98178 96190 90283 84198 75254 64203
     99251 99249 99236 99177 98977 98350 96426 90609 84561 75629 64560
     99193 99191 99176 99109 98881 98168 96004 89646 83232 73980 62745
     98893 98890 98869 98776 98464 97514 94765 87298 80241 70507 59113
     98288 98283 98252 98119 97682 96415 92999 84503 76961 66957 55602
     97673 97668 97628 97463 96933 95451 91641 82638 74910 64856 53613];

%%%%%%%%%%%%%%%%%%
% Wind speed 5 m/s
e5=[ 97149 97143 97104 96958 96514 95307 92222 84889 78695 70827 61994
     97323 97317 97280 97138 96707 95527 92490 85218 79046 71184 62340
     97449 97443 97406 97268 96846 95687 92688 85465 79311 71456 62606
     97561 97555 97519 97385 96971 95832 92869 85694 79559 71712 62856
     97638 97632 97597 97465 97058 95933 92997 85857 79736 71896 63038
     97704 97698 97664 97533 97132 96019 93106 85999 79891 72057 63197
     97758 97752 97718 97590 97193 96091 93198 86118 80022 72194 63333
     98305 98300 98272 98162 97818 96839 94186 87450 81509 73772 64917
     98399 98395 98367 98261 97928 96975 94373 87715 81811 74099 65251
     98509 98505 98479 98378 98057 97135 94597 88038 82183 74505 65668
     98625 98621 98597 98500 98195 97308 94844 88401 82605 74968 66147
     98752 98748 98725 98634 98345 97500 95122 88819 83094 75510 66710
     98910 98907 98886 98803 98537 97747 95490 89386 83768 76264 67501
     99132 99129 99111 99040 98807 98104 96035 90250 84806 77439 68746
     99251 99248 99232 99165 98947 98277 96270 90568 85156 77805 69107
     99193 99190 99171 99096 98847 98086 95838 89637 83910 76274 67386
     98892 98888 98862 98758 98420 97412 94589 87372 81080 72995 63866
     98286 98280 98242 98093 97624 96295 92827 84668 77942 69577 60376
     97672 97664 97616 97432 96866 95324 91478 82856 75957 67519 58358];

%%%%%%%%%%%%%%%%%%%
% Wind speed 10 m/s
e10=[97144 97132 97077 96895 96381 95059 91917 85437 80683 75087 68926
     97318 97307 97254 97077 96576 95281 92182 85752 81017 75432 69273
     97444 97433 97381 97208 96718 95443 92378 85987 81269 75693 69538
     97556 97546 97495 97326 96845 95589 92557 86205 81503 75938 69787
     97633 97623 97573 97407 96933 95692 92683 86361 81671 76114 69967
     97699 97689 97640 97476 97008 95779 92792 86496 81816 76268 70124
     97753 97743 97695 97533 97070 95852 92883 86609 81940 76398 70257
     98301 98293 98251 98113 97707 96613 93862 87870 83328 77883 71797
     98396 98388 98348 98214 97820 96752 94048 88119 83608 78187 72117
     98506 98498 98460 98331 97952 96916 94271 88423 83951 78562 72514
     98622 98615 98579 98456 98093 97093 94516 88764 84339 78989 72968
     98749 98742 98708 98593 98248 97290 94794 89155 84788 79486 73499
     98908 98902 98871 98765 98445 97546 95161 89685 85402 80173 74238
     99130 99125 99098 99006 98725 97915 95706 90492 86346 81237 75393
     99249 99244 99219 99134 98868 98091 95938 90791 86674 81584 75749
     99190 99185 99157 99060 98758 97880 95493 89937 85580 80261 74232
     98889 98881 98843 98709 98303 97164 94237 87852 83052 77341 71011
     98281 98270 98215 98026 97474 96010 92496 85340 80180 74187 67677
     97665 97651 97582 97352 96696 95022 91172 83634 78321 72226 65677];

%%%%%%%%%%%%%%%%%%%
% Wind speed 15 m/s
e15=[97136 97119 97047 96828 96246 94829 91729 86086 82264 77879 73043
     97310 97295 97224 97012 96444 95052 91991 86390 82586 78213 73385
     97436 97421 97352 97144 96587 95215 92185 86617 82827 78466 73644
     97548 97533 97466 97263 96715 95363 92362 86827 83052 78702 73888
     97626 97611 97545 97345 96805 95467 92486 86977 83212 78871 74062
     97692 97677 97613 97415 96881 95555 92594 87106 83352 79018 74215
     97746 97732 97668 97473 96944 95628 92683 87215 83469 79143 74345
     98295 98283 98228 98059 97592 96399 93651 88421 84787 80555 75828
     98390 98378 98325 98162 97707 96540 93834 88658 85050 80842 76133
     98500 98489 98439 98281 97843 96707 94055 88947 85373 81195 76509
     98617 98607 98558 98408 97987 96888 94298 89270 85737 81594 76939
     98744 98734 98689 98547 98146 97089 94572 89641 86156 82058 77439
     98903 98894 98853 98722 98348 97350 94936 90143 86729 82696 78133
     99126 99118 99083 98969 98637 97730 95477 90904 87605 83680 79210
     99245 99238 99205 99098 98783 97908 95705 91190 87916 84012 79555
     99187 99178 99141 99019 98662 97680 95257 90399 86930 82830 78193
     98884 98872 98820 98655 98180 96930 94010 88455 84616 80168 75227
     98273 98257 98183 97953 97321 95749 92299 86091 81938 77218 72065
     97655 97635 97544 97266 96525 94750 91001 84465 80172 75343 70118];

%emis = xx(:,2:12)/1e5;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Check input dimensions
ii=ndims(zang);
if (ii ~= 2)
   error('zang must by a (1 x n) vector or scaler')
end
n=length(zang);

ii=ndims(wspeed);
if (ii ~= 2)
   error('wspeed must by a (1 x n) vector or scaler')
end
ii=length(wspeed);
if (ii ~= n)
   error('zang and wspeed must be the same length')
end


% Check zang values
sang=abs(zang);
ii=find(sang > 85);
sang(ii)=85;
[irow,icol]=size(sang);
if (n > 1 & irow == 1)
   sang=sang';  %  want sang to be (n x 1) for interpolation
end


% Check zang values
ws=wspeed;
ii=find(ws > 15);
ws(ii)=15;
[irow,icol]=size(ws);
if (n > 1 & icol == 1)
   ws=ws';  %  want ws to be (1 x n) for interpolation
end


% Convert angles to secants
secang=1./cos(angles*pi/180)';
secsang=1./cos(sang*pi/180);


% Create output arrays
nemis=19*ones(1,n);
efreq=wn(19:-1:1)*ones(1,n);
seaemis=zeros(19,n);


% Do specular cases
ii=find(wspeed < 0);
nii=length(ii);
if (nii > 0)
   ey=es(19:-1:1,:)'/1E+5;
   seaemis(:,ii)=interp1( secang, ey, secsang(ii), 'linear')';
end

% Do wind speed 0-1
ii=find(wspeed >= 0 & wspeed < 1);
nii=length(ii);
if (nii > 0)
   ey=e0(19:-1:1,:)'/1E+5;
   ylo=interp1( secang, ey, secsang(ii), 'linear')';
   ey=e1(19:-1:1,:)'/1E+5;
   yhi=interp1( secang, ey, secsang(ii), 'linear')';
   seaemis(:,ii)=(yhi - ylo).*(ones(19,1)*ws(ii)) + ylo;
end

% Do wind speed 1-3
ii=find(wspeed >= 1 & wspeed < 3);
nii=length(ii);
if (nii > 0)
   ey=e1(19:-1:1,:)'/1E+5;
   ylo=interp1( secang, ey, secsang(ii), 'linear')';
   ey=e3(19:-1:1,:)'/1E+5;
   yhi=interp1( secang, ey, secsang(ii), 'linear')';
   seaemis(:,ii)=(yhi - ylo).*(ones(19,1)*(ws(ii) - 1))/2 + ylo;
end

% Do wind speed 3-5
ii=find(wspeed >= 3 & wspeed < 5);
nii=length(ii);
if (nii > 0)
   ey=e3(19:-1:1,:)'/1E+5;
   ylo=interp1( secang, ey, secsang(ii), 'linear')';
   ey=e5(19:-1:1,:)'/1E+5;
   yhi=interp1( secang, ey, secsang(ii), 'linear')';
   seaemis(:,ii)=(yhi - ylo).*(ones(19,1)*(ws(ii) - 3))/2 + ylo;
end

% Do wind speed 5-10
ii=find(wspeed >= 5 & wspeed < 10);
nii=length(ii);
if (nii > 0)
   ey=e5(19:-1:1,:)'/1E+5;
   ylo=interp1( secang, ey, secsang(ii), 'linear')';
   ey=e10(19:-1:1,:)'/1E+5;
   yhi=interp1( secang, ey, secsang(ii), 'linear')';
   seaemis(:,ii)=(yhi - ylo).*(ones(19,1)*(ws(ii) - 5))/5 + ylo;
end

% Do wind speed 10-15
ii=find(wspeed >= 10 & wspeed <= 15);
nii=length(ii);
if (nii > 0)
   ey=e10(19:-1:1,:)'/1E+5;
   ylo=interp1( secang, ey, secsang(ii), 'linear')';
   ey=e15(19:-1:1,:)'/1E+5;
   yhi=interp1( secang, ey, secsang(ii), 'linear')';
   seaemis(:,ii)=(yhi - ylo).*(ones(19,1)*(ws(ii) - 10))/5 + ylo;
end


%%% end of function %%%
