Home > swan_scripts > read_specfile.m

read_specfile

PURPOSE ^

Read the ascii-based spectral distribution output from SWAN for single station

SYNOPSIS ^

function [specstruct] = read_specfile(specfile,plotall)

DESCRIPTION ^

 Read the ascii-based spectral distribution output from SWAN for single station 

 DESCRIPTION:
   function [specstruct] = read_specfile(specfile,plotall) 

 INPUT 
   specfile = swan 40.72 spectral energy file 
   plotall  = [optional] plot all the spectral distributions

 OUTPUT:
   specstruct = matlab spectral distribution structure

 EXAMPLE USAGE
   read_specfile('skg4.3.spec',true) 

 Author(s):  
    Geoff Cowles (University of Massachusetts Dartmouth)

 Revision history
   
==============================================================================

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [specstruct] = read_specfile(specfile,plotall) 
0002 % Read the ascii-based spectral distribution output from SWAN for single station
0003 %
0004 % DESCRIPTION:
0005 %   function [specstruct] = read_specfile(specfile,plotall)
0006 %
0007 % INPUT
0008 %   specfile = swan 40.72 spectral energy file
0009 %   plotall  = [optional] plot all the spectral distributions
0010 %
0011 % OUTPUT:
0012 %   specstruct = matlab spectral distribution structure
0013 %
0014 % EXAMPLE USAGE
0015 %   read_specfile('skg4.3.spec',true)
0016 %
0017 % Author(s):
0018 %    Geoff Cowles (University of Massachusetts Dartmouth)
0019 %
0020 % Revision history
0021 %
0022 %==============================================================================
0023 
0024 
0025 %------------------------------------------------------------------------------
0026 % read in the spectral file
0027 %------------------------------------------------------------------------------
0028 
0029 % make sure file exists
0030 fid = fopen(specfile,'r');
0031 if(fid  < 0)
0032   error(['file: ' specfile ' does not exist']);
0033 end;
0034 fclose(fid);
0035 
0036 % determine number of spectral profiles
0037 cmd  = ['grep LOCATION ' specfile ' | grep -v LOCATIONS | wc -l > out'];
0038 system(cmd);
0039 fid = fopen('out','r');
0040 C=textscan(fid,'%d',1);
0041 nProfiles = C{1};
0042 fclose(fid);
0043 
0044 % read the header
0045 fid = fopen(specfile,'r');
0046 lin = fgetl(fid); %header
0047 lin = fgetl(fid); %header
0048 lin = fgetl(fid); %header
0049 lin = fgetl(fid); %header
0050 lin = fgetl(fid); %header
0051 lin = fgetl(fid); %header
0052 lin = fgetl(fid); %header
0053 
0054 
0055 % read the data location
0056 C = textscan(fid, '%f %f\n',1); 
0057 x = C{1};
0058 y = C{2};
0059 fprintf('probe location: %f %f\n',x,y);
0060 
0061 % number of frequencies
0062 C = textscan(fid, '%s %s %s %s %s',1);  %AFREQ  absolute frequencies in Hz
0063 C = textscan(fid, '%d %s %s %s',1); 
0064 nFreq = C{1}
0065 
0066 % report dimensions
0067 fprintf('number of profiles %d\n',nProfiles);
0068 fprintf('number of freqs    %d\n',nFreq);
0069 
0070 % allocate space for data
0071 freq = zeros(nFreq,1);          %frequencies
0072 time = zeros(nProfiles,1);      %time (modified julian day)
0073 dens = zeros(nProfiles,nFreq);  %spectral density in m^2/hz
0074 wdir = zeros(nProfiles,nFreq);  %average wave direction (Cartesian) at that freq
0075 sprd = zeros(nProfiles,nFreq);  %directional spreading
0076 
0077 % read the frequencies
0078 for i=1:nFreq
0079   C = textscan(fid, '%f', 1);
0080   freq(i) = C{1};
0081 end;
0082 
0083 % read in stuff
0084 C = textscan(fid, '%s', 1);
0085 lin = fgets(fid); 
0086 lin = fgets(fid); 
0087 lin = fgets(fid); 
0088 lin = fgets(fid); 
0089 lin = fgets(fid); 
0090 lin = fgets(fid); 
0091 lin = fgets(fid); 
0092 lin = fgets(fid); 
0093 lin = fgets(fid); 
0094 lin = fgets(fid); 
0095 lin = fgets(fid); 
0096 
0097 % loop over probes, reading profiles
0098 for i=1:nProfiles
0099   fprintf('reading profile %d\n',i)
0100   C = textscan(fid, '%s %s %s %s', 1);
0101   C = textscan(fid, '%s %d', 1);
0102   for j=1:nFreq
0103     C = textscan(fid, '%f %f %f', 1);
0104     pwr = C{1}; if(pwr < 0) pwr = NaN; end; 
0105     dir = C{2}; if(pwr < -900) dir = NaN; end; 
0106     spr = C{3}; if(pwr < 0) spr = NaN; end; 
0107     dens(i,j) = pwr;
0108     wdir(i,j) = dir;
0109     sprd(i,j) = spr;
0110   end;
0111 end;
0112 
0113 % plot option
0114 if(plotall)
0115   for i=1:2:nProfiles
0116     plot(1./freq,dens(i,:)); hold on;
0117   end;
0118 end;

Generated on Wed 20-Feb-2019 16:06:01 by m2html © 2005