Home > ctdcal > old > cnv2mat.m

cnv2mat

PURPOSE ^

CNV2MAT Reads the SeaBird BINARY or ASCII.CNV file format

SYNOPSIS ^

function [cnv_struct]=cnv2mat(cnv_file,varargin);

DESCRIPTION ^

 CNV2MAT Reads the SeaBird BINARY or ASCII.CNV file format

  Usage:   [cnv_struct]=cnv2mat(cnv_file);

     Input:  CNV_file = name of .CNV file  (e.g. 'STC1002.CNV')

     Output:cnv_struct with fields:
             lon = longitude in decimal degrees, West negative
             lat = latitude in decimal degrees, North positive
             gtime = Gregorian time vector in UTC
             sensors = string matrix containing the names of the sensors
             names = cell of string matrix containing the names of the columns
            data = matrix containing all the columns of data in the .CNV file

  NOTE: The format of CNV header file must be:

    * System UpLoad Time = Mar 21 2001 12:56:38
    * NMEA Latitude = 06 19.96 N
    * NMEA Longitude = 043 59.96 W
    * NMEA UTC (Time) = 12:57:07
    # name 0 = .....
    # sensor 0 = .....
    *end*


  Modify the lat,lon and date string handling if your .CNV files are different.
  Also, There are several possibilities for the way time is stored.  NMEA time 
  has been used as time and it is in GMT.

  
  BUGS: 

  CALLER: user

  DEPENDENCIES:

  AUTHOR: 29 Nov 2001 Sonia Cavalcante (Sonia.Cavalcante@noaa.gov)
          based on btl2mat by Derrick Snowden (Derrick.Snowden@noaa.gov)
          (wich was based on Rich Signell).  
  4-8-98  Rich Signell (rsignell@usgs.gov)  
     incorporates ideas from code by Derek Fong & Peter Brickley

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [cnv_struct]=cnv2mat(cnv_file,varargin);
0002 % CNV2MAT Reads the SeaBird BINARY or ASCII.CNV file format
0003 %
0004 %  Usage:   [cnv_struct]=cnv2mat(cnv_file);
0005 %
0006 %     Input:  CNV_file = name of .CNV file  (e.g. 'STC1002.CNV')
0007 %
0008 %     Output:cnv_struct with fields:
0009 %             lon = longitude in decimal degrees, West negative
0010 %             lat = latitude in decimal degrees, North positive
0011 %             gtime = Gregorian time vector in UTC
0012 %             sensors = string matrix containing the names of the sensors
0013 %             names = cell of string matrix containing the names of the columns
0014 %            data = matrix containing all the columns of data in the .CNV file
0015 %
0016 %  NOTE: The format of CNV header file must be:
0017 %
0018 %    * System UpLoad Time = Mar 21 2001 12:56:38
0019 %    * NMEA Latitude = 06 19.96 N
0020 %    * NMEA Longitude = 043 59.96 W
0021 %    * NMEA UTC (Time) = 12:57:07
0022 %    # name 0 = .....
0023 %    # sensor 0 = .....
0024 %    *end*
0025 %
0026 %
0027 %  Modify the lat,lon and date string handling if your .CNV files are different.
0028 %  Also, There are several possibilities for the way time is stored.  NMEA time
0029 %  has been used as time and it is in GMT.
0030 %
0031 %
0032 %  BUGS:
0033 %
0034 %  CALLER: user
0035 %
0036 %  DEPENDENCIES:
0037 %
0038 %  AUTHOR: 29 Nov 2001 Sonia Cavalcante (Sonia.Cavalcante@noaa.gov)
0039 %          based on btl2mat by Derrick Snowden (Derrick.Snowden@noaa.gov)
0040 %          (wich was based on Rich Signell).
0041 %  4-8-98  Rich Signell (rsignell@usgs.gov)
0042 %     incorporates ideas from code by Derek Fong & Peter Brickley
0043 %
0044 if nargin > 1;
0045     type = varargin{1};
0046     if type== 1 | strcmp(upper(type(1)),'B')
0047         button = 'B';
0048     elseif type== 0 | strcmp(upper(type(1)),'A')
0049         button = 'A';
0050     else
0051         error('cnv2mat error: Unrecognized input file type.  {[B (binary) default] or A (ascii)}');
0052     end
0053 elseif nargin > 2
0054     error('cnv2mat error: Too many input variables.');
0055 else
0056     button = 'A';
0057 end
0058 
0059 % Open the .CNV file as read-only text
0060 %
0061 fid=fopen(cnv_file,'rt');
0062 if fid == -1
0063     error(['Unable to open file: ',cnv_file]);
0064 end
0065 
0066 %
0067 % Read the header.
0068 % Start reading header lines of .CNV file,
0069 % Stop at line that starts with '*END*'
0070 %
0071 % Pull out NMEA lat & lon along the way and look
0072 % at the '# name' fields to see how many variables we have.
0073 %
0074 str='*START*';
0075 
0076 % This may change in different configurations.  I suppose it depends on your
0077 % shipboard processing scripts/procedures.  look through your files for
0078 %something that denots the end of the header but not the line containing the names ofthe columns.
0079 %
0080 choose_nmea = 0;
0081 while (~strncmp(str,'*END*',5));
0082     str=fgetl(fid);
0083     %-----------------------------------
0084     %
0085     %    Read the NMEA latitude string.  This may vary with CTD setup.
0086     %
0087     if (strncmp(str,'* NMEA Lat',10))
0088         choose_nmea = 1;
0089         is=findstr(str,'=');
0090         isub=is+1:length(str);
0091         dm=sscanf(str(isub),'%f',2);
0092         if(findstr(str(isub),'N'));
0093             lat=dm(1)+dm(2)/60;
0094         else  
0095             lat=-(dm(1)+dm(2)/60); 
0096         end
0097         %-------------------------------
0098         %
0099         %    Read the NMEA longitude string.  This may vary with CTD setup.
0100         %
0101     elseif (strncmp(str,'* NMEA Lon',10))
0102         choose_nmea = 1;
0103         is=findstr(str,'=');
0104         isub=is+1:length(str);
0105         dm=sscanf(str(isub),'%f',2);
0106         if(findstr(str(isub),'E'));
0107             lon=dm(1)+dm(2)/60;
0108         else  
0109             lon=-(dm(1)+dm(2)/60); 
0110         end
0111         %-----------------------------------
0112         %
0113         %    Read the NON NMEA latitude string.  This may vary with CTD setup.
0114         %
0115     elseif (strncmp(str,'** Lat',6))
0116         if choose_nmea == 0
0117             is=findstr(str,':');
0118             isub=is+1:length(str);
0119             dm=sscanf(str(isub),'%f',2);
0120             if(findstr(str(isub),'N'));
0121                 if length(dm) == 2
0122                     lat=dm(1)+dm(2)/60;
0123                 else
0124                     lat = dm(1); % assume they have used decimal degrees
0125                 end
0126             else 
0127                 if length(dm) == 2
0128                     lat=-(dm(1)+dm(2)/60);
0129                 else
0130                     lat = -(dm(1)); % assume they have used decimal degrees
0131                 end
0132                 
0133             end
0134         end
0135         %-------------------------------
0136         %
0137         %    Read the NON NMEA longitude string.  This may vary with CTD setup.
0138         %
0139     elseif (strncmp(str,'** Lon',6))
0140         if choose_nmea == 0
0141             is=findstr(str,':');
0142             isub=is+1:length(str);
0143             dm=sscanf(str(isub),'%f',2);
0144             if(findstr(str(isub),'E'));
0145                  if length(dm) == 2
0146                     lon =dm(1)+dm(2)/60;
0147                 else
0148                     lon = dm(1); % assume they have used decimal degrees
0149                 end
0150             else 
0151                 if length(dm) == 2
0152                     lon=-(dm(1)+dm(2)/60);
0153                 else
0154                     lon = -(dm(1)); % assume they have used decimal degrees
0155                 end
0156                 
0157            end
0158         end
0159         %------------------------
0160         %
0161         %    Read the 'System upload time' to get the date.
0162         %           This may vary with CTD setup.
0163         %
0164         %    I'm reading this in to get the date, since the NMEA time string
0165         %    does not contain date.  Unfortunately, the system upload time is
0166         %    in local time (here, EST), so I need to convert to UTC by adding
0167         %    5 hours (5/24 days).
0168         %
0169     elseif (strncmp(str,'* System UpLoad',15))
0170         is=findstr(str,'=');
0171         %    pick apart date string and reassemble in DATEFORM type 0 form
0172         datstr=[str(is+6:is+7) '-' str(is+2:is+4) '-' str(is+9:is+12)];
0173         datstr=[datstr ' ' str(is+14:is+21)];
0174         %    convert datstr to Julian time, add 5 hours to convert from EST to GMT
0175         %n=datenum(datstr)+5/24;
0176         %CAF
0177         n=datenum(datstr);
0178         %CAF
0179         gtime=datevec(n);
0180         %----------------------------
0181         %
0182         %    Read the NMEA TIME string.  This may vary with CTD setup.
0183         %
0184         %      replace the System upload time with the NMEA time
0185     elseif (strncmp(str,'* NMEA UTC',10))
0186         is=findstr(str,':');
0187         isub=is(1)-2:length(str);
0188         gtime([4:6])=sscanf(str(isub),'%2d:%2d:%2d');
0189         %------------------------------
0190         %
0191         %    Read the variable names & units into a cell array
0192         %
0193     elseif (strncmp(str,'# name',6))  
0194         var=sscanf(str(7:10),'%d',1);
0195         var=var+1;  % .CNV file counts from 0, Matlab counts from 1
0196         %      stuff variable names into cell array
0197          isin=findstr(str,'=');
0198          is=findstr(str,':');
0199          names{var}=str(isin+1:is-1);
0200         %------------------------------
0201         %
0202         %    Read the sensor names into a cell array
0203         %
0204     elseif (strncmp(str,'# sensor',8))  
0205         sens=sscanf(str(10:11),'%d',1);
0206         sens=sens+1;  % .CNV file counts from 0, Matlab counts from 1
0207         %      stuff sensor names into cell array
0208         sensors{sens}=str;
0209         %
0210         %  pick up bad flag value
0211     elseif (strncmp(str,'# bad_flag',10))  
0212         isub=13:length(str);
0213         bad_flag=sscanf(str(isub),'%g',1);
0214     end
0215 end
0216 %==============================================
0217 %
0218 %  Done reading header.  Now read the data!
0219 %  It can be read as binary onto matrix data
0220 %==============================================
0221 %button=questdlg(' What is the data format? BINARY (B) or ASCII(A)?','B','A');
0222 switch button
0223 case 'B'
0224     data=fread(fid,[length(names) Inf],'float');
0225 case 'A'
0226     data=fscanf(fid,'%f',[length(names) Inf]); 
0227 end
0228     data=data';
0229 % Begin to assemble the output matrix/structure.
0230 
0231 cnv_struct.lat = lat;
0232 cnv_struct.lon = lon;
0233 cnv_struct.gtime = gtime;
0234 cnv_struct.sensors = char(sensors');
0235 % remove the spaces in the names cell
0236 for i = 1:length(names)
0237     str = names{i};
0238     idxspc = isspace(str);
0239     str(idxspc) = [];
0240     names{i} = str;
0241 end
0242 cnv_struct.names = names;
0243 cnv_struct.data = data;
0244 disp([cnv_file,' loaded'])
0245 fclose(fid)
0246

Generated on Fri 08-Oct-2004 11:57:17 by m2html © 2003