Skip to content

Commit

Permalink
updated buoy files
Browse files Browse the repository at this point in the history
  • Loading branch information
thesser1 committed Apr 30, 2013
1 parent b27f425 commit f3ac786
Show file tree
Hide file tree
Showing 29 changed files with 772 additions and 81 deletions.
36 changes: 36 additions & 0 deletions Buoy_analysis/adjust_canada_spec.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
function aa = adjust_canada_spec(aa)

aa.efold = aa.ef;

for ii = 1:size(aa.ef,2);
dum = 0;
nfreq = size(aa.freq,1);
for jj = nfreq:-1:1
dum = dum + aa.efold(jj,ii).*aa.bw(jj,ii);
if dum == aa.hsb(ii).^2/16
aa.ef(1:jj,ii) = 0;
break
elseif dum > aa.hsb(ii).^2/16
aa.ef(1:jj+1,ii) = 0;
break
end
end

aa.hs(ii) = 4.0*sqrt(sum(aa.ef(:,ii).*aa.bw(:,ii)));
ip = find(aa.ef(:,ii) == max(aa.ef(:,ii)));

if length(ip) > 1
if ip(1) == ip(2)-1
ip = ip(1);
else
ip = ip(2);
end
end
if ip < 12
ip = 12;
end

aa.tp(ii) = 1.0/aa.freq(ip,ii);
end


30 changes: 23 additions & 7 deletions Buoy_analysis/cdip_2_onlns.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
function cdip_2_onlns(cdip,ndbc,year,mon)

cdir = 'X:\CDIP\';
if isunix
%cdir = '/mnt/CHL_WIS_1/CDIP/';
cdir = '/home/thesser1/NODC/';
else
cdir = 'X:\CDIP\';
end
if ischar(cdip)
cdipc = cdip;
else
Expand Down Expand Up @@ -34,10 +39,21 @@ function cdip_2_onlns(cdip,ndbc,year,mon)
end
monc = mont(mon,:);
end
cdiry = [cdir,'spc_',yearc,'\',cdipc,'\',monc,'\01\'];
if isunix
cdiry = [cdir,'spc_',yearc,'/',cdipc,'/',monc,'/01/'];
else
cdiry = [cdir,'spc_',yearc,'\',cdipc,'\',monc,'\01\'];
end
if exist(cdiry,'dir')
ndbcd = ['X:\NDBC_CDIP\'];
ndbct = [ndbcd,yearc,'\',monc,'\'];
if isunix
%ndbcd = ['/mnt/CHL_WIS_1/NDBC_CDIP'];
ndbcd = ['/home/thesser1/NDBC/'];
ndbct = [ndbcd,yearc,'/',monc,'/'];
else
ndbcd = ['X:\NDBC_CDIP\'];
ndbct = [ndbcd,yearc,'\',monc,'\'];
end


nfile = [ndbct,'n',ndbcc,'_',yearc,'_',monnc,'.onlns'];
if exist(nfile,'file')
Expand All @@ -54,8 +70,8 @@ function cdip_2_onlns(cdip,ndbc,year,mon)
bb1(:,3) = str2num(time(:,7:8));
bb1(:,4) = str2num(time(:,9:10));
bb1(:,5) = str2num(time(:,11:12));
[latd,latm,lats] = deg2dms(aa.lat);
[lond,lonm,lons] = deg2dms(aa.lon);
[latd,latm,lats] = ddeg2dms(aa.lat);
[lond,lonm,lons] = ddeg2dms(aa.lon);
loc = [latd,abs(latm),round(abs(lats)),lond,abs(lonm),round(abs(lons))];
bb1(:,6:11) = repmat(loc,size(bb1,1),1);
bb1(:,12) = aa.dep;
Expand All @@ -65,7 +81,7 @@ function cdip_2_onlns(cdip,ndbc,year,mon)
bb1(:,19) = -999.9;

ab.c11 = aa.ef;
ab.a0 = aa.ef/pi;
ab.a0 = aa.ef./pi;
if any(strcmp('a1',fieldnames(aa)))
ab.a1=aa.a1;ab.a2=aa.a2;
ab.b1 = aa.b1;ab.b2 = aa.b2;
Expand Down
5 changes: 3 additions & 2 deletions Buoy_analysis/convert_2_onlns.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@

if mp > 0
if any(strcmp('a1',fieldnames(aa)))
[ef2d] = mlm(aa.freq,aa.c11(:,zz),aa.a0(:,zz),aa.a1(:,zz), ...
aa.b1(:,zz),aa.a2(:,zz),aa.b2(:,zz));

ef2d = twodgen_mem(aa.freq,aa.c11(:,zz),aa.a1(:,zz), ...
aa.b1(:,zz),aa.a2(:,zz),aa.b2(:,zz),aa.dep);
[wavdvt,wavdvfm,wavpd,wavdmx,wavfmx, ...
sumefx,sumefy,summfx,summfy] = ....
directns(ef2d,aa.freq,aa.df,360,1,mp,aa.dep);
Expand Down
95 changes: 95 additions & 0 deletions Buoy_analysis/frf_2_onlns.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
function frf_2_onlns(cdip,ndbc,year,mon)

if isunix
%cdir = '/mnt/CHL_WIS_1/CDIP/';
cdir = '/home/thesser1/NODC/';
else
cdir = 'X:\CDIP\';
end
if ischar(cdip)
cdipc = cdip;
else
if cdip < 100
cdipc = ['0',num2str(cdip)];
else
cdipc = num2str(cdip);
end
end
if ischar(ndbc)
ndbcc = ndbc;
else
ndbcc = num2str(ndbc);
end
if ischar(year)
yearc = year;
else
yearc = num2str(year);
end
mont = ['jan';'feb';'mar';'apr';'may';'jun';'jul';'aug';'sep';'oct'; ...
'nov';'dec'];
if ischar(mon)
monnc = mon;
monn = str2num(mon);
monc = mont(monn,:);
else
if mon < 10
monnc = ['0',num2str(mon)];
else
monnc = num2str(mon);
end
monc = mont(mon,:);
end
if isunix
fname = ['waverdr3630_',yearc,'_',monnc,'.spec'];
cdiry = [cdir,fname];
else
cdiry = [cdir,'spc_',yearc,'\',cdipc,'\',monc,'\01\'];
end
if exist(cdiry,'file')
if isunix
%ndbcd = ['/mnt/CHL_WIS_1/NDBC_CDIP'];
ndbcd = ['/home/thesser1/NDBC/'];
ndbct = [ndbcd,yearc,'/',monc,'/'];
else
ndbcd = ['X:\NDBC_CDIP\'];
ndbct = [ndbcd,yearc,'\',monc,'\'];
end


nfile = [ndbct,'n',ndbcc,'_',yearc,'_',monnc,'.onlns'];
if exist(nfile,'file')
return
end

[aa]= read_frf_spc(cdir,fname);
% if status == 0
% return
% end
time = num2str(aa.date);
bb1(:,1) = str2num(time(:,1:4));
bb1(:,2) = str2num(time(:,5:6));
bb1(:,3) = str2num(time(:,7:8));
bb1(:,4) = str2num(time(:,9:10));
bb1(:,5) = str2num(time(:,11:12));
[latd,latm,lats] = ddeg2dms(aa.lat);
[lond,lonm,lons] = ddeg2dms(aa.lon);
loc = [latd,abs(latm),round(abs(lats)),lond,abs(lonm),round(abs(lons))];
bb1(:,6:11) = repmat(loc,size(bb1,1),1);
bb1(:,12) = aa.dep;
bb1(:,13:15) = -99.99;
bb1(:,16) = -999.9;
bb1(:,17:18) = -99.99;
bb1(:,19) = -999.9;

ab.c11 = aa.ef;
ab.a0 = aa.ef./pi;
if any(strcmp('a1',fieldnames(aa)))
ab.a1=aa.a1;ab.a2=aa.a2;
ab.b1 = aa.b1;ab.b2 = aa.b2;
end
ab.df = aa.bw(:,1);
ab.freq = aa.freq(:,1);ab.dep = aa.dep;
bb2 = convert_2_onlns(ab);
bb = horzcat(bb1,bb2);
create_onlns(ndbcc,yearc,monnc,bb,ndbct);
end
30 changes: 30 additions & 0 deletions Buoy_analysis/read_canada_csv.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function aa = read_canada_csv(fdir,fname)

fid = fopen([fdir,fname]);

data = textscan(fid,'%s%s%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f', ...
'headerlines',2,'delimiter',',');

aa.station = data{1}{1};
for jj = 1:length(data{2})
date = data{2}{jj};
mon(jj,:) = date(1:2);
day(jj,:) = date(4:5);
year(jj,:) = date(7:10);
hour(jj,:) = date(12:13);
minu(jj,:) = date(15:16);
end
aa.date = [year,mon,day,hour,minu];
aa.date(:,13:14) = '00';
aa.date = str2num(aa.date);

aa.timemat = datenum(str2num(year),str2num(mon),str2num(day),...
str2num(hour),str2num(minu),0);

aa.qflag = data{3};
aa.lat = data{4};
aa.lon = data{5};
aa.dep = data{6};


fclose(fid);
16 changes: 11 additions & 5 deletions Buoy_analysis/read_canada_sp.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
% dtmean : Total mean wave direction (len) (deg) (met) (Numeric)
% -------------------------------------------------------------------------
%clear all
fname = ls([fdir,'*',year,'.FB']);
fname = dir([fdir,'*',year,'.fb']);
if isempty(fname)
fnamezip = ls([fdir,'*',year,'.zip']);
if isempty(fnamezip)
Expand All @@ -46,11 +46,11 @@
cd(fdir)
unzip(fnamezip(zz,:));
end
fname = ls([fdir,'*',year,'.FB']);
fname = ls([fdir,'*',year,'.fb']);
end
dfold = 0;zf = 0;status = 1;
for zfile = 1:size(fname,1)
fid = fopen([fdir,fname(zfile,:)]); % open file
fid = fopen([fdir,fname(zfile).name]); % open file
data = fgetl(fid);
nn = 0;
while data ~= -1;
Expand Down Expand Up @@ -97,8 +97,12 @@
end

for ii = 1:numpar
data = fscanf(fid,'%f',1);
data = fscanf(fid,'%4c',1);
datan = fscanf(fid,'%f',1);
datac = fscanf(fid,'%4c',1);
if strcmp(datac(4),'$')
datac(4) = 'S';
end
eval(['bb.',datac,' = datan;']);
end
for ii = 1:numfreq
try
Expand All @@ -124,6 +128,8 @@
etot = sum(aa.ef(:,nn).*aa.bw(:,nn));
fmean = etot/(fmean + 1.0e-10);
aa.tm(:,nn) = 1./fmean;
aa.hsb(:,nn) = bb.VCAR;
aa.tpb(:,nn) = bb.VTPK;
data = fgetl(fid);data = fgetl(fid);
clear freq df ef
end
Expand Down
4 changes: 2 additions & 2 deletions Buoy_analysis/read_cdip_sp.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@
% dtmean : Total mean wave direction (len) (deg) (met) (Numeric)
% -------------------------------------------------------------------------
%clear all
fname = ls([fdir,'sp*']);
fname = dir([fdir,'sp*']);
if isempty(fname)
status = 0;aa = 0;
return
end
dfold = 0;zf = 0;status = 1;
for zfile = 1:size(fname,1)
fid = fopen([fdir,fname(zfile,:)]); % open file
fid = fopen([fdir,fname(zfile).name]); % open file
data = fgetl(fid);fname2 = data(12:30);aa.analyzed = data(51:69); % read 1st line
aa.stnum = str2double(data(14:16));
aa.sennum = str2double(data(17:18));
Expand Down
19 changes: 13 additions & 6 deletions Buoy_analysis/read_frf_spc.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function aa = read_frf_spc(fname)
function aa = read_frf_spc(cdir,fname)

fid = fopen(fname);
fid = fopen([cdir,fname]);
data = textscan(fid,'%f%f%f%f%f%f%f%f%f%f',1);
nn = 0;
while ~isempty(data{1});
Expand All @@ -21,7 +21,10 @@
dayc = num2str(day);
end
hminc = num2str(hmin);
if hmin < 1000
if hmin < 100
hourc1 = '0';
minc = '00';
elseif hmin < 1000
hourc1 = hminc(1);
minc = hminc(2:3);
else
Expand All @@ -46,7 +49,11 @@

if nn == 1
%load('/home/thesser1/My_Matlab/Buoy_analysis/frf_loc.mat');
load('C:\matlab\My_Matlab\Buoy_analysis\frf_loc.mat');
if isunix
load('/home/thesser1/My_Matlab/Buoy_analysis/frf_loc.mat');
else
load('C:\matlab\My_Matlab\Buoy_analysis\frf_loc.mat');
end
ii = timeref > timemat(nn);
if ~isempty(timeref(ii))
jj = find((ii == 1), 1 );
Expand Down Expand Up @@ -84,6 +91,6 @@
aa.timemat = timemat;
aa.dep = depth;
aa.hs = hmo';aa.tp = tp';aa.wdir = idir';
aa.freq = freq';aa.df = repmat(df,size(freq))';
aa.c11 = ef;aa.a1 = a1;aa.a2 = a2;
aa.freq = freq';aa.bw = repmat(df,size(freq))';
aa.ef = ef;aa.a1 = a1;aa.a2 = a2;
aa.b1 = b1;aa.b2 = b2;
39 changes: 39 additions & 0 deletions Extra_files/kNearestNeighbors.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function [neighborIds neighborDistances] = kNearestNeighbors(dataMatrix, queryMatrix, k)
%--------------------------------------------------------------------------
% Program to find the k - nearest neighbors (kNN) within a set of points.
% Distance metric used: Euclidean distance
%
% Usage:
% [neighbors distances] = kNearestNeighbors(dataMatrix, queryMatrix, k);
% dataMatrix (N x D) - N vectors with dimensionality D (within which we search for the nearest neighbors)
% queryMatrix (M x D) - M query vectors with dimensionality D
% k (1 x 1) - Number of nearest neighbors desired
%
% Example:
% a = [1 1; 2 2; 3 2; 4 4; 5 6];
% b = [1 1; 2 1; 6 2];
% [neighbors distances] = kNearestNeighbors(a,b,2);
%
% Output:
% neighbors =
% 1 2
% 1 2
% 4 3
%
% distances =
% 0 1.4142
% 1.0000 1.0000
% 2.8284 3.0000
%--------------------------------------------------------------------------

neighborIds = zeros(size(queryMatrix,1),k);
neighborDistances = neighborIds;

numDataVectors = size(dataMatrix,1);
numQueryVectors = size(queryMatrix,1);
for i=1:numQueryVectors,
dist = sum((repmat(queryMatrix(i,:),numDataVectors,1)-dataMatrix).^2,2);
[sortval sortpos] = sort(dist,'ascend');
neighborIds(i,:) = sortpos(1:k);
neighborDistances(i,:) = sqrt(sortval(1:k));
end
Loading

0 comments on commit f3ac786

Please sign in to comment.