% writes float data in seaBASS-required format for uploading

% loads:
  % float0005.mat (generated by applyCalProfile.m)  

% no mat files are saved

% written by L. Taylor, UMaine, 2008
% email: lisa.taylor@umit.maine.edu

close all
clear all

%load data
load float0005 % float0005 contains:
profileNum=float0005(:,1); %profile number
date=float0005(:,2); %date
lat=float0005(:,3); %latitude (degrees)
lon=float0005(:,4); %longitude (degrees)
P=float0005(:,5); %depth (m)
theta=float0005(:,6); %potential temperature (degrees C)
S=float0005(:,7); %salinity (psu)
sigma=float0005(:,8); %density-1000 (kg/m3)
B_LSS_b_corr=float0005(:,9); %bb (m-1, calibrated using MODIS data downloaded 01/11/08)
Chl_b_corr=float0005(:,10); %chlorophyll (mg/m3, calibrated using MODIS data downloaded 01/11/08) 
O2sat=float0005(:,11); %oxygen (% saturation) !oxygen sensor not working
O2=float0005(:,12); %oxygen (um/kg) !oxygen sensor not working
POC_b=float0005(:,13);
C_phy=float0005(:,14);
profile=float0005(:,15); % (in days from start)

% get date and time in yyyy/mm/dd hh:mm:ss format
date_yyyy=datestr(date,10);
date_mm=datestr(date,5);
date_dd=datestr(date,7);
hour=datestr(date,13);
date_yyyymmdd=datestr(date,'yyyymmdd');

% seaBASS won't take negative depths
I=find(P>=0);

fid = fopen('C:\Documents and Settings\Lisa Taylor\My Documents\Data\0005\APEX\float0005edf.txt', 'wt');

% print header
fprintf(fid,'/begin_header\n');
fprintf(fid,'/investigators=Emmanuel_Boss,Dana_Swift,Lisa_Taylor\n');
fprintf(fid,'/affiliations=UMaine-MISC_Lab,UW-School_of_Oceanography,UMaine-MISC_Lab\n');
fprintf(fid,'/contact=emmanuel.boss@maine.edu\n');
fprintf(fid,'/experiment=Pilot_Study_for_the_Inclusion_of_Active_Optical_Sensors_into_Apex_Floats\n');
fprintf(fid,'/cruise=North_Atlantic\n');
fprintf(fid,'/station=Float0005\n');
fprintf(fid,'/data_file_name=float0005edf.txt\n');
fprintf(fid,'/documents=float0005Doc.txt\n');
fprintf(fid,'/calibration_files=float0005Doc.txt\n');
fprintf(fid,'/data_type=cast\n');
fprintf(fid,'/data_status=final\n');
fprintf(fid,'/start_date=%4s%2s%2s\n',date_yyyymmdd(1,:));
fprintf(fid,'\n');
fprintf(fid,'/end_date=%4s%2s%2s\n',date_yyyymmdd(length(profileNum),:));
fprintf(fid,'\n');
fprintf(fid,'/start_time=%8s[GMT]\n',hour(1,:));
fprintf(fid,'/end_time=%8s[GMT]\n',hour(length(profileNum),:));
fprintf(fid,'/north_latitude=%5.3f[DEG]\n',max(lat));
fprintf(fid,'/south_latitude=%5.3f[DEG]\n',min(lat));
fprintf(fid,'/east_longitude=%5.3f[DEG]\n',max(lon));
fprintf(fid,'/west_longitude=%5.3f[DEG]\n',min(lon));
fprintf(fid,'/water_depth=NA\n');
% fprintf(fid,'/measurement_depth= up to %5.3f\n',max(P));
% fprintf(fid,'/measurement_depth=NA\n'); % not allowed if depth is in the data
fprintf(fid,'/secchi_depth=NA\n');
fprintf(fid,'/cloud_percent=NA\n');
fprintf(fid,'/wind_speed=NA\n');
fprintf(fid,'/wave_height=NA\n');
fprintf(fid,'!\n');
fprintf(fid,'! Station number refers to profile number.\n');
fprintf(fid,'! Wt is potential temperature.\n');
fprintf(fid,'!\n');
fprintf(fid,'/missing=-9999\n');
fprintf(fid,'/delimiter=comma\n');
fprintf(fid,'/fields=station,date,time,lat,lon,depth,Wt,sal,sigmaT,bb,CHL\n');
fprintf(fid,'/units=none,yyyymmdd,hh:mm:ss,degrees,degrees,m,degreesC,PSU,kg/m^3,1/m,mg/m^3\n');
fprintf(fid,'/end_header\n');

% print data
for i=1:length(I)
     fprintf(fid, '%3.0f,',profileNum(I(i),:));
     fprintf(fid, '%8s,',date_yyyymmdd(I(i),:));
%      fprintf(fid, '%s,',hour(I(i),:)); % how to write if doing a subset
     fprintf(fid,'%8s,',hour(I(i),:));
     fprintf(fid,'%5.3f,',lat(I(i),:));
     fprintf(fid,'%5.3f,',lon(I(i),:));
     fprintf(fid,'%6.1f,',P(I(i),:));
     fprintf(fid,'%6.3f,',theta(I(i),:));
     fprintf(fid,'%6.3f,',S(I(i),:));
     fprintf(fid,'%6.4f,',sigma(I(i),:));
     fprintf(fid,'%6.4f,',B_LSS_b_corr(I(i),:));
     fprintf(fid,'%6.4f',Chl_b_corr(I(i),:));
     fprintf(fid,'\n');
end     
fclose(fid);
