Make meta hs6.m

From Pnb
Revision as of 15:56, 2 August 2010 by 128.111.101.185 (talk) (Created page with '<pre> function make_meta_hs6(cruise) %Function to automatyically construct the metafile.txt %needed for HS-6 data processing %Shpuld work on PC and UNIX %Author: Tihomir Kostadi…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

function make_meta_hs6(cruise)
%Function to automatyically construct the metafile.txt
%needed for HS-6 data processing
%Shpuld work on PC and UNIX
%Author: Tihomir Kostadinov
%Date: November 20, 2003
%modified:

%if isunix
%   eval(strcat(['cd /home/data65/pb/HYDROSCAT/', cruise]));  
% ctd_path = strcat(['/home/data65/pb/CTD/', cruise,'/hdr/']); 
% 
% else %PC
%     eval(strcat([cd ,'k:\links\PB\HYDROSCAT\', cruise]));
%     ctd_path = strcat(['k:\links\PB\CTD', cruise,'\hdr\']);
% end
%Open metafile for wrting and print header; 


%ctd_path = strcat(['/home/eriks/',cruise,'/CTD/',cruise,'/hdr/'])
% temp ctd_path for pb215
ctd_path = strcat(['/home/eriks/pb218/215_test/',cruise,'/CTD/',cruise,'/hdr/']);


fid = fopen('metafile.txt', 'w'); 
fprintf(fid,'%s', 'File Cruise_ID Date Station Latitude Longitude'); %print header; 
fprintf(fid, '\n');
%Extract current directory info. 
%k = dir ----------------------------------------original
%altered above if isunix look at original when ready to insert

pattern = fullfile(pwd,'*.dat')
k = dir(pattern)
filenames = {};
for i = 1:size(k,1)
    
    filenames(i) =  cellstr(k(i).name)
 
end




%Filter for .asc files only;
asc_files = {}; 
for i = 1:length(filenames)
    if ~isempty(findstr(char(filenames(i)),'dat'))
        asc_files = [asc_files, filenames(i)];
    end
end


%Loop thropugh each h*.asc file to create a line for it in the metafile
%Extract LAT/LON from the CTD .asc files (NMEA) 
 %cd ../..
 pwd
for i = 1:length(asc_files)
    line = '';
    file = char(asc_files(i));
    cdate = file(2:7);
    line = strcat([line,file(1:end-4),' ',cruise,' ',cdate, ' PnB', num2str(pb_let2num(file(8)))]); %used to have 's' before file
    
    %Extract lat/lon and append to line
    ctd_file = strcat(['X',cdate,upper(file(8)),'.hdr']);
    ctd_fullfile = strcat(ctd_path, ctd_file);
    
    disp(['Reading NMEA lat/lon from CTD file ', ctd_fullfile])

    
    ctdfid = fopen(ctd_fullfile,'r')
    ctdline = '';
    
    
    
    while isempty(findstr(ctdline,'NMEA Latitude'))
        ctdline = fgetl(ctdfid);
    end
    
    
    %Note that the line '* NMEA Latitude = 34 04.95 N' ansd the follwoing
    %line have to have equal sign '=' brigh tbeore lat/lon in degrees,
    %space, decimal minutes format. 
    equal_sign = findstr(ctdline, '=');
    lat_raw = ctdline(equal_sign+1:end);
    filter = isletter(lat_raw)
    
    
    lat = '';
    for i = 1:length(lat_raw)
        if filter(i)==0
            lat = [lat, lat_raw(i)];
        end
    end
    
    
    lat = str2num(lat);
    latitude = lat(1) + lat(2)/60;
    
    
   
    %Longitude processing
    ctdline = fgetl(ctdfid);
    equal_sign = findstr(ctdline, '=');
    lon_raw = ctdline(equal_sign+1:end);
    filter = isletter(lon_raw); 
    lon = '';
    for i = 1:length(lon_raw)
        if filter(i)==0
            lon = [lon, lon_raw(i)];
        end
    end
    lon = str2num(lon);
    longitude = lon(1) + lon(2)/60;
    fclose(ctdfid); 
    line = strcat([line, ' ',num2str(latitude),' ',num2str(longitude)]);
    fprintf(fid,'%s',char(line)); 
    fprintf(fid,'\n');
   atend = pwd
end
fclose(fid); 
%end


make_meta_hs6