Commit 83f05f91 authored by das's avatar das
Browse files

Adding files added during JC159

On branch JC159_post_cruise
Changes to be committed:
	new file:   source/mextras/ctd_apply_celltm.m
	new file:   source/unfinished/m_editheader_fix.m
	new file:   source/unfinished/m_editheader_old.m
	new file:   source/unfinished/m_recolor.m
	new file:   source/unfinished/mmerge_old.m

Untracked files:
	source/mtechsas/mtchoosefiles.m~
	source/unfinished/mcontrnew.m~
	source/unfinished/mmerge2.m~
parent 7463a2f5
function cond = ctd_apply_celltm(time,temp,cond);
% dy040 bak 21 dec 2015
% code up celltm algorithm
alpha = 0.03; % sbe default values are alpha = 0.03; 1/beta = 7;
beta = 1/7;
num = numel(time);
ctm = zeros(size(cond));
kfirst = min(find(isfinite(time+temp)));
if isempty(kfirst)
msg = ['No finite time+temp data found for celltm calculation in ctd_apply_celltm'];
fprintf(2,'%s\n',msg);
cond = cond+nan;
return
end
timelast = time(kfirst);
templast = temp(kfirst);
ctmlast = ctm(kfirst);
for kl = kfirst+1:num
if ~isfinite(time(kl)+temp(kl))
ctm(kl) = ctm(kl-1); % keep ctm fixed if not possible to update it due to missing temperature
ctmlast = ctm(kl);
continue
end
dtime = time(kl)-timelast;
dtemp = temp(kl)-templast;
a = 2 * alpha/(dtime*beta + 2);
b = 1 - (2 * a/alpha);
dcdt = 0.1*(1+0.006*(temp(kl)-20));
ctm(kl) = -1 * b * ctmlast + a*dcdt*dtemp;
timelast = time(kl);
templast = temp(kl);
ctmlast = ctm(kl);
end
ctm = 10 * ctm;
cond = cond+ctm;
return
function h = m_editheader(h,ncfile)
% function h = m_editheader(h,ncfile)
%
% edit mstar header
m_common
endflag = 0;
while endflag == 0
fprintf(MEXEC_A.Mfidterm,'%s\n\n',' ');
if ~MEXEC_G.quiet; m_print_header(h); end
m1 = ' Which option ? ';
m2 = '-1, ''/'' or return: finished (default)';
m3 = ' 0 all fields';
m4 = ' 1 dataname';
m5 = ' 2 platform details';
m6 = ' 3 instrument/recording interval';
m7 = ' 4 data time origin';
m8 = ' 5 position';
m9 = ' 6 depths';
m10 = ' 7 comments';
m11 = ' 8 var names and units';
m = sprintf('%s\n ',' ',m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11);
var = m_getinput(m,'s');
if strcmp(var,' ') == 1; var = '-1'; end
if strcmp(var,'/') == 1; var = '-1'; end
if strcmp(var,'-1') == 1;
endflag = 1;
break;
end
if strcmp(var,'1') == 1 | strcmp(var,'0') == 1
eflag = 0;
while eflag == 0
m = ['Type new dataname (return or / to keep as ' h.dataname ') : '];
var2 = m_getinput(m,'s');
if strcmp(var2,' ') == 1;
elseif strcmp(var2,'/') == 1;
else
h.dataname = m_remove_spaces(var2);
end
m_write_header(ncfile,h);
fprintf(MEXEC_A.Mfidterm,'%s\n\n',' ');
if ~MEXEC_G.quiet; m_print_header(h); end
m1 = ['Move on to next question ?'];
m2 = ['type return or / to move on, 1 to repeat'];
m = sprintf('%s\n',m1,m2);
var3 = m_getinput(m,'s');
if strcmp(var3,' ') == 1; eflag = 1; end
if strcmp(var3,'/') == 1; eflag = 1; end
if strcmp(var3,'1') == 1; eflag = 0; end
end
end
if strcmp(var,'2') == 1 | strcmp(var,'0') == 1
eflag = 0;
while eflag == 0
m = ['Type new platform type (return or / to keep as ' h.platform_type ') : '];
var2 = m_getinput(m,'s');
if strcmp(var2,' ') == 1;
elseif strcmp(var2,'/') == 1;
else
h.platform_type = m_remove_outside_spaces(var2);
end
m = ['Type new platform identifier (return or / to keep as ' h.platform_identifier ') : '];
var2 = m_getinput(m,'s');
if strcmp(var2,' ') == 1;
elseif strcmp(var2,'/') == 1;
else
h.platform_identifier = m_remove_outside_spaces(var2);
end
m = ['Type new platform number (return or / to keep as ' h.platform_number ') : '];
var2 = m_getinput(m,'s');
if strcmp(var2,' ') == 1;
elseif strcmp(var2,'/') == 1;
else
h.platform_number = m_remove_outside_spaces(var2);
end
m_write_header(ncfile,h);
fprintf(MEXEC_A.Mfidterm,'%s\n\n',' ');
if ~MEXEC_G.quiet; m_print_header(h); end
m1 = ['Move on to next question ?'];
m2 = ['type return or / to move on, 1 to repeat'];
m = sprintf('%s\n',m1,m2);
var3 = m_getinput(m,'s');
if strcmp(var3,' ') == 1; eflag = 1; end
if strcmp(var3,'/') == 1; eflag = 1; end
if strcmp(var3,'1') == 1; eflag = 0; end
end
end
if strcmp(var,'3') == 1 | strcmp(var,'0') == 1
eflag = 0;
while eflag == 0
m = ['Type new instrument identifier (return or / to keep as ' h.instrument_identifier ') : '];
var2 = m_getinput(m,'s');
if strcmp(var2,' ') == 1;
elseif strcmp(var2,'/') == 1;
else
h.instrument_identifier = m_remove_outside_spaces(var2);
end
m = ['Type new recording interval (return or / to keep as ' h.recording_interval ') : '];
var2 = m_getinput(m,'s');
if strcmp(var2,' ') == 1;
elseif strcmp(var2,'/') == 1;
else
h.recording_interval = m_remove_outside_spaces(var2);
end
m_write_header(ncfile,h);
fprintf(MEXEC_A.Mfidterm,'%s\n\n',' ');
if ~MEXEC_G.quiet; m_print_header(h); end
m1 = ['Move on to next question ?'];
m2 = ['type return or / to move on, 1 to repeat'];
m = sprintf('%s\n',m1,m2);
var3 = m_getinput(m,'s');
if strcmp(var3,' ') == 1; eflag = 1; end
if strcmp(var3,'/') == 1; eflag = 1; end
if strcmp(var3,'1') == 1; eflag = 0; end
end
end
if strcmp(var,'4') == 1 | strcmp(var,'0') == 1
eflag = 0;
while eflag == 0
m = ['Type new data_time_origin (return or / to keep as [' sprintf('%d ',h.data_time_origin) '] ) : '];
var2 = m_getinput(m,'s');
if strcmp(var2,' ') == 1;
elseif strcmp(var2,'/') == 1;
else
cmd = ['h.data_time_origin = [' var2 '];']; %convert char response to number
eval(cmd);
h.data_time_origin_string = datestr(datenum(h.data_time_origin),31);
end
m_write_header(ncfile,h);
fprintf(MEXEC_A.Mfidterm,'%s\n\n',' ');
if ~MEXEC_G.quiet; m_print_header(h); end
m1 = ['Move on to next question ?'];
m2 = ['type return or / to move on, 1 to repeat'];
m = sprintf('%s\n',m1,m2);
var3 = m_getinput(m,'s');
if strcmp(var3,' ') == 1; eflag = 1; end
if strcmp(var3,'/') == 1; eflag = 1; end
if strcmp(var3,'1') == 1; eflag = 0; end
end
end
if strcmp(var,'5') == 1 | strcmp(var,'0') == 1
eflag = 0;
while eflag == 0
m = ['Type new lat (return or / to keep as ' sprintf('%12.6f',h.latitude) ' ) : '];
var2 = m_getinput(m,'s');
if strcmp(var2,' ') == 1;
elseif strcmp(var2,'/') == 1;
else
cmd = ['lat = [' var2 '];']; %convert char response to number
eval(cmd);
h.latitude = lat;
end
m = ['Type new lon (return or / to keep as ' sprintf('%12.6f',h.longitude) ' ) : '];
var2 = m_getinput(m,'s');
if strcmp(var2,' ') == 1;
elseif strcmp(var2,'/') == 1;
else
cmd = ['lon = [' var2 '];']; %convert char response to number
eval(cmd);
h.longitude = lon;
end
m_write_header(ncfile,h);
fprintf(MEXEC_A.Mfidterm,'%s\n\n',' ');
if ~MEXEC_G.quiet; m_print_header(h); end
m1 = ['Move on to next question ?'];
m2 = ['type return or / to move on, 1 to repeat'];
m = sprintf('%s\n',m1,m2);
var3 = m_getinput(m,'s');
if strcmp(var3,' ') == 1; eflag = 1; end
if strcmp(var3,'/') == 1; eflag = 1; end
if strcmp(var3,'1') == 1; eflag = 0; end
end
end
if strcmp(var,'6') == 1 | strcmp(var,'0') == 1
eflag = 0;
while eflag == 0
m = ['Type new water_depth_metres (return or / to keep as ' sprintf('%9.1f',h.water_depth_metres) ' ) : '];
var2 = m_getinput(m,'s');
if strcmp(var2,' ') == 1;
elseif strcmp(var2,'/') == 1;
else
cmd = ['water_depth_metres = [' var2 '];']; %convert char response to number
eval(cmd);
h.water_depth_metres = water_depth_metres;
end
m = ['Type new instrument_depth_metres (return or / to keep as ' sprintf('%9.1f',h.instrument_depth_metres) ' ) : '];
var2 = m_getinput(m,'s');
if strcmp(var2,' ') == 1;
elseif strcmp(var2,'/') == 1;
else
cmd = ['instrument_depth_metres = [' var2 '];']; %convert char response to number
eval(cmd);
h.instrument_depth_metres = instrument_depth_metres;
end
m_write_header(ncfile,h);
fprintf(MEXEC_A.Mfidterm,'%s\n\n',' ');
if ~MEXEC_G.quiet; m_print_header(h); end
m1 = ['Move on to next question ?'];
m2 = ['type return or / to move on, 1 to repeat'];
m = sprintf('%s\n',m1,m2);
var3 = m_getinput(m,'s');
if strcmp(var3,' ') == 1; eflag = 1; end
if strcmp(var3,'/') == 1; eflag = 1; end
if strcmp(var3,'1') == 1; eflag = 0; end
end
end
if strcmp(var,'7') == 1 | strcmp(var,'0') == 1
eflag = 0;
while eflag == 0
disp('Accept or change comments line by line')
c = h.comment;
delim = h.comment_delimiter_string;
h.comment_delimiter_string = delim;
delimindex = strfind(c,delim); % start locations of delim strings
ncoms = length(delimindex)-1;
% review/delete/modify existing comments
eflag2 = 0;
while eflag2 == 0
m1 = 'Review existing comments (/ or return) or ';
m2 = 'skip the review and start adding new comments (-1) ?';
m = sprintf('%s\n',m1,m2);
var2 = m_getinput(m,'s');
if strcmp(var2,'-1') == 1; break; end
% review existing comments
if ncoms == 0
m = 'No exisiting comments to review';
fprintf(MEXEC_A.Mfidterm,'%s\n',m);
eflag2 = 1;
else
nreview = ncoms; %initial number of comments to review
while nreview > 0 % number of comments remaining to review
k1 = delimindex(ncoms+1-nreview)+length(delim);
k2 = delimindex(ncoms+1-(nreview-1))-1;
cstring = ['comment : ' sprintf('%s',c(k1:k2))];
m = sprintf('%s\n%s',cstring, 'Type d to delete, return or / to accept, or any other string to replace: ');
var2 = m_getinput(m,'s');
if strcmp(var2,'d') == 1;
c(k1-length(delim):k2) = [];
delimindex = strfind(c,delim); % start locations of delim strings
ncoms = length(delimindex)-1; % number of comments remaning after one has been deleted
nreview = nreview-1;
continue
elseif strcmp(var2,' ') == 1 | strcmp(var2,'/') == 1;
nreview = nreview-1;
continue
else
cstringnew = var2;
cold1 = c(1:k1-1);
cold2 = c(k2+1:end);
c = [cold1 cstringnew cold2];
delimindex = strfind(c,delim); % start locations of delim strings
ncoms = length(delimindex)-1; % number of comments remaning after one has been deleted
nreview = nreview-1;
continue
end
end
eflag2 = 1;
end
end
% end of review, now add new comments
m = 'Add new comments, / or return to end ';
% fprintf(MEXEC_A.Mfidterm,'%s\n',m);
eflag2 = 0;
while eflag2 == 0
var2 = m_getinput(m,'s');
% if you want a comment consisting of space or slash you need to put something like ' ' or '/ '
if strcmp(var2,' ') == 1;
eflag2 = 1;
elseif strcmp(var2,'/') == 1;
eflag2 = 1;
else
cstringnew = var2;
c = [c cstringnew delim];
end
end
h.comment = c;
m_write_header(ncfile,h);
fprintf(MEXEC_A.Mfidterm,'%s\n\n',' ');
if ~MEXEC_G.quiet; m_print_header(h); end
m1 = ['Move on to next question ?'];
m2 = ['type return or / to move on, 1 to repeat'];
m = sprintf('%s\n',m1,m2);
var3 = m_getinput(m,'s');
if strcmp(var3,' ') == 1; eflag = 1; end
if strcmp(var3,'/') == 1; eflag = 1; end
if strcmp(var3,'1') == 1; eflag = 0; end
end
end
if strcmp(var,'8') == 1 | strcmp(var,'0') == 1
eflag = 0;
while eflag == 0
m0 = 'Variable names and units: ';
m1 = ' Which option ? ';
m2 = '-1 finished (default)';
m3 = ' 0 all fields';
m4 = ' varlist : just those variable numbers';
m = sprintf('%s\n ',' ',m0,m1,m2,m3,m4);
var = m_getinput(m,'s');
if strcmp(var,' ') == 1; var = '-1'; end
if strcmp(var,'/') == 1; var = '-1'; end
if strcmp(var,'-1') == 1;
eflag = 1;
break;
end
if strcmp(var,'0') == 1; var = '/'; end % var is now the list of var numbers
vlist = m_getvlist(var,h);
vlist = unique(vlist); % sort the list
for kl = 1:length(vlist)
kvar = vlist(kl);
eflag3 = 0;
while eflag3 == 0
m1 = [' ' h.fldnam{kvar} ' ' h.fldunt{kvar}];
m2 = ['Type new name (return or / to keep as ' h.fldnam{kvar} ') : '];
m = sprintf('%s\n%s',m1,m2);
var2 = m_getinput(m,'s');
if strcmp(var2,' ') == 1;
elseif strcmp(var2,'/') == 1;
else
oldname = h.fldnam{kvar};
newname = m_check_nc_varname(var2);
kclash = strmatch(newname,h.fldnam,'exact');
% jc159 bak 5 march 2018
% there is a problem if this branch is entered in a
% script, because the program keeps prompting for a
% name until an acceptable one is found. This is a
% problem when called from a script. Solution:
% assume the user knows what they want, and just
% warn that name isn't changed. This enables a
% script to 'rename' variables, even when the
% newname is the same as the old name. If the new
% name is a clash with an existing name for another
% variable, then prompt the user again.
if ~isempty(kclash)
if (length(kclash) == 1) & (kclash(1) == kvar)
m1 = 'Warning: renaming with the same name';
fprintf(MEXEC_A.Mfider,'%s\n',' ',m1);
% no call to rename
else
m1 = 'That name already exists in the file';
m2 = 'Choose another name';
fprintf(MEXEC_A.Mfider,'%s\n',' ',m1,m2);
% prompt the user again
continue
end
else
% no clash, so go ahead and rename
nc_varrename(ncfile.name,oldname,newname);
end
h = m_read_header(ncfile);
end
m2 = ['Type new units (return or / to keep as ' h.fldunt{kvar} ') : '];
var2 = m_getinput(m2,'s');
if strcmp(var2,' ') == 1;
elseif strcmp(var2,'/') == 1;
else
newunits = m_remove_outside_spaces(var2);
nc_attput(ncfile.name,h.fldnam{kvar},'units',newunits);
h = m_read_header(ncfile);
end
eflag3 = 1;
end
end
end
end
end
function h = m_editheader(h,ncfile)
% function h = m_editheader(h,ncfile)
%
% edit mstar header
m_common
endflag = 0;
while endflag == 0
fprintf(MEXEC_A.Mfidterm,'%s\n\n',' ');
if ~MEXEC_G.quiet; m_print_header(h); end
m1 = ' Which option ? ';
m2 = '-1, ''/'' or return: finished (default)';
m3 = ' 0 all fields';
m4 = ' 1 dataname';
m5 = ' 2 platform details';
m6 = ' 3 instrument/recording interval';
m7 = ' 4 data time origin';
m8 = ' 5 position';
m9 = ' 6 depths';
m10 = ' 7 comments';
m11 = ' 8 var names and units';
m = sprintf('%s\n ',' ',m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11);
var = m_getinput(m,'s');
if strcmp(var,' ') == 1; var = '-1'; end
if strcmp(var,'/') == 1; var = '-1'; end
if strcmp(var,'-1') == 1;
endflag = 1;
break;
end
if strcmp(var,'1') == 1 | strcmp(var,'0') == 1
eflag = 0;
while eflag == 0
m = ['Type new dataname (return or / to keep as ' h.dataname ') : '];
var2 = m_getinput(m,'s');
if strcmp(var2,' ') == 1;
elseif strcmp(var2,'/') == 1;
else
h.dataname = m_remove_spaces(var2);
end
m_write_header(ncfile,h);
fprintf(MEXEC_A.Mfidterm,'%s\n\n',' ');
if ~MEXEC_G.quiet; m_print_header(h); end
m1 = ['Move on to next question ?'];
m2 = ['type return or / to move on, 1 to repeat'];
m = sprintf('%s\n',m1,m2);
var3 = m_getinput(m,'s');
if strcmp(var3,' ') == 1; eflag = 1; end
if strcmp(var3,'/') == 1; eflag = 1; end
if strcmp(var3,'1') == 1; eflag = 0; end
end
end
if strcmp(var,'2') == 1 | strcmp(var,'0') == 1
eflag = 0;
while eflag == 0
m = ['Type new platform type (return or / to keep as ' h.platform_type ') : '];
var2 = m_getinput(m,'s');
if strcmp(var2,' ') == 1;
elseif strcmp(var2,'/') == 1;
else
h.platform_type = m_remove_outside_spaces(var2);
end
m = ['Type new platform identifier (return or / to keep as ' h.platform_identifier ') : '];
var2 = m_getinput(m,'s');
if strcmp(var2,' ') == 1;
elseif strcmp(var2,'/') == 1;
else
h.platform_identifier = m_remove_outside_spaces(var2);
end
m = ['Type new platform number (return or / to keep as ' h.platform_number ') : '];
var2 = m_getinput(m,'s');
if strcmp(var2,' ') == 1;
elseif strcmp(var2,'/') == 1;
else
h.platform_number = m_remove_outside_spaces(var2);
end
m_write_header(ncfile,h);
fprintf(MEXEC_A.Mfidterm,'%s\n\n',' ');
if ~MEXEC_G.quiet; m_print_header(h); end
m1 = ['Move on to next question ?'];
m2 = ['type return or / to move on, 1 to repeat'];
m = sprintf('%s\n',m1,m2);
var3 = m_getinput(m,'s');
if strcmp(var3,' ') == 1; eflag = 1; end
if strcmp(var3,'/') == 1; eflag = 1; end
if strcmp(var3,'1') == 1; eflag = 0; end
end
end
if strcmp(var,'3') == 1 | strcmp(var,'0') == 1
eflag = 0;
while eflag == 0
m = ['Type new instrument identifier (return or / to keep as ' h.instrument_identifier ') : '];
var2 = m_getinput(m,'s');
if strcmp(var2,' ') == 1;
elseif strcmp(var2,'/') == 1;
else
h.instrument_identifier = m_remove_outside_spaces(var2);
end
m = ['Type new recording interval (return or / to keep as ' h.recording_interval ') : '];
var2 = m_getinput(m,'s');
if strcmp(var2,' ') == 1;
elseif strcmp(var2,'/') == 1;
else
h.recording_interval = m_remove_outside_spaces(var2);
end
m_write_header(ncfile,h);
fprintf(MEXEC_A.Mfidterm,'%s\n\n',' ');
if ~MEXEC_G.quiet; m_print_header(h); end
m1 = ['Move on to next question ?'];
m2 = ['type return or / to move on, 1 to repeat'];
m = sprintf('%s\n',m1,m2);
var3 = m_getinput(m,'s');
if strcmp(var3,' ') == 1; eflag = 1; end
if strcmp(var3,'/') == 1; eflag = 1; end
if strcmp(var3,'1') == 1; eflag = 0; end
end
end
if strcmp(var,'4') == 1 | strcmp(var,'0') == 1
eflag = 0;
while eflag == 0
m = ['Type new data_time_origin (return or / to keep as [' sprintf('%d ',h.data_time_origin) '] ) : '];
var2 = m_getinput(m,'s');
if strcmp(var2,' ') == 1;
elseif strcmp(var2,'/') == 1;
else
cmd = ['h.data_time_origin = [' var2 '];']; %convert char response to number
eval(cmd);
h.data_time_origin_string = datestr(datenum(h.data_time_origin),31);
end
m_write_header(ncfile,h);
fprintf(MEXEC_A.Mfidterm,'%s\n\n',' ');
if ~MEXEC_G.quiet; m_print_header(h); end
m1 = ['Move on to next question ?'];
m2 = ['type return or / to move on, 1 to repeat'];
m = sprintf('%s\n',m1,m2);
var3 = m_getinput(m,'s');
if strcmp(var3,' ') == 1; eflag = 1; end
if strcmp(var3,'/') == 1; eflag = 1; end
if strcmp(var3,'1') == 1; eflag = 0; end
end
end
if strcmp(var,'5') == 1 | strcmp(var,'0') == 1
eflag = 0;
while eflag == 0
m = ['Type new lat (return or / to keep as ' sprintf('%12.6f',h.latitude) ' ) : '];
var2 = m_getinput(m,'s');
if strcmp(var2,' ') == 1;
elseif strcmp(var2,'/') == 1;
else
cmd = ['lat = [' var2 '];']; %convert char response to number
eval(cmd);
h.latitude = lat;
end
m = ['Type new lon (return or / to keep as ' sprintf('%12.6f',h.longitude) ' ) : '];
var2 = m_getinput(m,'s');
if strcmp(var2,' ') == 1;
elseif strcmp(var2,'/') == 1;
else
cmd = ['lon = [' var2 '];']; %convert char response to number
eval(cmd);
h.longitude = lon;
end
m_write_header(ncfile,h);
fprintf(MEXEC_A.Mfidterm,'%s\n\n',' ');
if ~MEXEC_G.quiet; m_print_header(h); end
m1 = ['Move on to next question ?'];
m2 = ['type return or / to move on, 1 to repeat'];
m = sprintf('%s\n',m1,m2);
var3 = m_getinput(m,'s');
if strcmp(var3,' ') == 1; eflag = 1; end
if strcmp(var3,'/') == 1; eflag = 1; end
if strcmp(var3,'1') == 1; eflag = 0; end
end
end
if strcmp(var,'6') == 1 | strcmp(var,'0') == 1
eflag = 0;
while eflag == 0
m = ['Type new water_depth_metres (return or / to keep as ' sprintf('%9.1f',h.water_depth_metres) ' ) : '];
var2 = m_getinput(m,'s');
if strcmp(var2,' ') == 1;
elseif strcmp(var2,'/') == 1;
else
cmd = ['water_depth_metres = [' var2 '];']; %convert char response to number
eval(cmd);
h.water_depth_metres = water_depth_metres;
end
m = ['Type new instrument_depth_metres (return or / to keep as ' sprintf('%9.1f',h.instrument_depth_metres) ' ) : '];
var2 = m_getinput(m,'s');
if strcmp(var2,' ') == 1;
elseif strcmp(var2,'/') == 1;
else
cmd = ['instrument_depth_metres = [' var2 '];']; %convert char response to number
eval(cmd);
h.instrument_depth_metres = instrument_depth_metres;
end
m_write_header(ncfile,h);
fprintf(MEXEC_A.Mfidterm,'%s\n\n',' ');
if ~MEXEC_G.quiet; m_print_header(h); end
m1 = ['Move on to next question ?'];
m2 = ['type return or / to move on, 1 to repeat'];
m = sprintf('%s\n',m1,m2);
var3 = m_getinput(m,'s');
if strcmp(var3,' ') == 1; eflag = 1; end
if strcmp(var3,'/') == 1; eflag = 1; end
if strcmp(var3,'1') == 1; eflag = 0; end
end
end
if strcmp(var,'7') == 1 | strcmp(var,'0') == 1
eflag = 0;
while eflag == 0
disp('Accept or change comments line by line')
c = h.comment;
delim = h.comment_delimiter_string;
h.comment_delimiter_string = delim;
delimindex = strfind(c,delim); % start locations of delim strings
ncoms = length(delimindex)-1;
% review/delete/modify existing comments
eflag2 = 0;
while eflag2 == 0
m1 = 'Review existing comments (/ or return) or ';
m2 = 'skip the review and start adding new comments (-1) ?';
m = sprintf('%s\n',m1,m2);
var2 = m_getinput(m,'s');
if strcmp(var2,'-1') == 1; break; end
% review existing comments
if ncoms == 0
m = 'No exisiting comments to review';
fprintf(MEXEC_A.Mfidterm,'%s\n',m);
eflag2 = 1;
else
nreview = ncoms; %initial number of comments to review
while nreview > 0 % number of comments remaining to review
k1 = delimindex(ncoms+1-nreview)+length(delim);
k2 = delimindex(ncoms+1-(nreview-1))-1;
cstring = ['comment : ' sprintf('%s',c(k1:k2))];
m = sprintf('%s\n%s',cstring, 'Type d to delete, return or / to accept, or any other string to replace: ');
var2 = m_getinput(m,'s');
if strcmp(var2,'d') == 1;
c(k1-length(delim):k2) = [];
delimindex = strfind(c,delim); % start locations of delim strings
ncoms = length(delimindex)-1; % number of comments remaning after one has been deleted
nreview = nreview-1;
continue
elseif strcmp(var2,' ') == 1 | strcmp(var2,'/') == 1;
nreview = nreview-1;
continue
else
cstringnew = var2;
cold1 = c(1:k1-1);
cold2 = c(k2+1:end);
c = [cold1 cstringnew cold2];
delimindex = strfind(c,delim); % start locations of delim strings
ncoms = length(delimindex)-1; % number of comments remaning after one has been deleted
nreview = nreview-1;
continue
end
end
eflag2 = 1;
end
end
% end of review, now add new comments
m = 'Add new comments, / or return to end ';
% fprintf(MEXEC_A.Mfidterm,'%s\n',m);
eflag2 = 0;
while eflag2 == 0
var2 = m_getinput(m,'s');
% if you want a comment consisting of space or slash you need to put something like ' ' or '/ '
if strcmp(var2,' ') == 1;
eflag2 = 1;
elseif strcmp(var2,'/') == 1;
eflag2 = 1;
else
cstringnew = var2;
c = [c cstringnew delim];
end
end
h.comment = c;
m_write_header(ncfile,h);
fprintf(MEXEC_A.Mfidterm,'%s\n\n',' ');
if ~MEXEC_G.quiet; m_print_header(h); end
m1 = ['Move on to next question ?'];
m2 = ['type return or / to move on, 1 to repeat'];
m = sprintf('%s\n',m1,m2);
var3 = m_getinput(m,'s');
if strcmp(var3,' ') == 1; eflag = 1; end
if strcmp(var3,'/') == 1; eflag = 1; end
if strcmp(var3,'1') == 1; eflag = 0; end
end
end
if strcmp(var,'8') == 1 | strcmp(var,'0') == 1
eflag = 0;
while eflag == 0
m0 = 'Variable names and units: ';
m1 = ' Which option ? ';
m2 = '-1 finished (default)';
m3 = ' 0 all fields';
m4 = ' varlist : just those variable numbers';
m = sprintf('%s\n ',' ',m0,m1,m2,m3,m4);
var = m_getinput(m,'s');
if strcmp(var,' ') == 1; var = '-1'; end
if strcmp(var,'/') == 1; var = '-1'; end
if strcmp(var,'-1') == 1;
eflag = 1;
break;
end
if strcmp(var,'0') == 1; var = '/'; end % var is now the list of var numbers
vlist = m_getvlist(var,h);
vlist = unique(vlist); % sort the list
for kl = 1:length(vlist)
kvar = vlist(kl);
eflag3 = 0;
while eflag3 == 0
m1 = [' ' h.fldnam{kvar} ' ' h.fldunt{kvar}];
m2 = ['Type new name (return or / to keep as ' h.fldnam{kvar} ') : '];
m = sprintf('%s\n%s',m1,m2);
var2 = m_getinput(m,'s');
if strcmp(var2,' ') == 1;
elseif strcmp(var2,'/') == 1;
else
oldname = h.fldnam{kvar};
newname = m_check_nc_varname(var2);
kclash = strmatch(newname,h.fldnam,'exact');
if ~isempty(kclash)
if (length(kclash) == 1) & (kclash(1) == kvar)
m1 = 'Attempt to rename with the same name';
m2 = 'Use the slash option to leave name unchanged';
fprintf(MEXEC_A.Mfider,'%s\n',' ',m1,m2);
else
m1 = 'That name already exists in the file';
m2 = 'Choose another name';
fprintf(MEXEC_A.Mfider,'%s\n',' ',m1,m2);
end
continue
end
nc_varrename(ncfile.name,oldname,newname);
h = m_read_header(ncfile);
end
m2 = ['Type new units (return or / to keep as ' h.fldunt{kvar} ') : '];
var2 = m_getinput(m2,'s');
if strcmp(var2,' ') == 1;
elseif strcmp(var2,'/') == 1;
else
newunits = m_remove_outside_spaces(var2);
nc_attput(ncfile.name,h.fldnam{kvar},'units',newunits);
h = m_read_header(ncfile);
end
eflag3 = 1;
end
end
end
end
end
% jc032 script to allow changes of colormap
hall = handles;
hc = get(hall,'children');
keeplev = nan+ones(length(hc),1);
for k = 1:length(hc)
keeplev(k) = get(hc(k),'CData');
end
%
% hc1 = hc(1);
%
% hc1_properties = get(hc1);
%
% lev = hc1_properties.CData;
% k_levindex = find(clev == lev)
% k_cmapindex = k_levindex+1
%
% set(hc1,'CDataMapping','direct')
% set(hc1,'CData',5)
% set(hc1,'linewidth',10)
clev_temp = ca(1):blocksize:ca(end);
% keyboard
for k = 1:length(hc)
% set(hc(k),'linewidth',3)
newlev = max(find(clev-1e-10 < keeplev(k)));
% if isempty(newlev)
% newlev = min(find(clev > keeplev(k)))-2; % find index level for contour whose value is equal to datamin
% end
set(hc(k),'CDataMapping','direct');
set(hc(k),'CData',newlev+ctabstart-1);
% fprintf(MEXEC_A.Mfidterm,'%6.3f %d\n',keeplev(k),newlev+ctabstart-1);
% keyboard
end
% keyboard
\ No newline at end of file
function mmerge(varargin)
% merge vars from a second file onto the first, using a control variable
% if the control var in both files is 1-D, no problem
% the control var in either one of the files can be 2-D
% something will surely crash if the control var in both files is 2-D
m_common
m_margslocal
m_varargs
MEXEC_A.Mprog = 'mmerge';
m_proghd
if length(MEXEC_A.MARGS_IN_LOCAL)==0
fprintf(MEXEC_A.Mfidterm,'%s\n','Enter name of output disc file')
ncfile_ot.name = m_getfilename;
else
ncfile_ot.name = m_getfilename(MEXEC_A.MARGS_IN_LOCAL{1}); MEXEC_A.MARGS_IN_LOCAL(1) = [];
end
ncfile_ot = m_openot(ncfile_ot);
if length(MEXEC_A.MARGS_IN_LOCAL)==0
fprintf(MEXEC_A.Mfidterm,'%s\n','Enter name of input disc file')
ncfile_in.name = m_getfilename;
else
ncfile_in.name = m_getfilename(MEXEC_A.MARGS_IN_LOCAL{1}); MEXEC_A.MARGS_IN_LOCAL(1) = [];
end
ncfile_in = m_openin(ncfile_in);
h = m_read_header(ncfile_in);
if ~MEXEC_G.quiet; m_print_header(h); end
hist = h;
hist.filename = ncfile_in.name;
MEXEC_A.Mhistory_in{1} = hist;
% --------------------
% Now do something with the data
% first write the same header; this will also create the file
h.openflag = 'W'; %ensure output file remains open for write, even though input file is 'R';
m_write_header(ncfile_ot,h);
%copy selected vars from the infile
m = sprintf('%s\n','Type variable names or numbers to copy (return for none, ''/'' for all): ');
var = m_getinput(m,'s');
if strcmp(' ',var) == 1;
vlist = [];
else
vlist = m_getvlist(var,h);
if ~MEXEC_G.quiet; disp(['list is ' sprintf('%d ',vlist) ]); end
end
for k = vlist
vname = h.fldnam{k};
numdc = h.dimrows(k)*h.dimcols(k);
if ~MEXEC_G.quiet
m = ['Copying ' sprintf('%8d',numdc) ' datacycles for variable ' vname ];
fprintf(MEXEC_A.Mfidterm,'%s\n',m);
end
m_copy_variable(ncfile_in,vname,ncfile_ot,vname);
end
ok = 0;
while ok == 0;
m = sprintf('%s\n','Type variable name or number for control variable for merge : ');
var = m_getinput(m,'s');
if strcmp(' ',var) == 1;
vlistc = [];
else
vlistc = m_getvlist(var,h);
end
if length(vlistc) ~= 1
m = 'You must choose precisely one control variable. try again';
fprintf(MEXEC_A.Mfider,'%s\n',m)
continue
end
vcontrol = vlistc;
ok = 1;
end
% get control data
vname_c_1 = h.fldnam{vcontrol};
data_c_1 = nc_varget(ncfile_in.name,vname_c_1);
if length(MEXEC_A.MARGS_IN_LOCAL)==0
m = 'Now get details of next input file ';
fprintf(MEXEC_A.Mfidterm,'%s\n',m)
fprintf(MEXEC_A.Mfidterm,'%s\n','Enter name of input disc file')
ncfile_in2.name = m_getfilename;
else
ncfile_in2.name = m_getfilename(MEXEC_A.MARGS_IN_LOCAL{1}); MEXEC_A.MARGS_IN_LOCAL(1) = [];
end
ncfile_in2 = m_openin(ncfile_in2);
h2 = m_read_header(ncfile_in2);
if ~MEXEC_G.quiet; m_print_header(h2); end
hist = h2;
hist.filename = ncfile_in2.name;
MEXEC_A.Mhistory_in{2} = hist;
ok = 0;
while ok == 0;
m = sprintf('%s\n','Type variable name or number for control variable for merge : ');
var = m_getinput(m,'s');
if strcmp(' ',var) == 1;
vlistc2 = [];
else
vlistc2 = m_getvlist(var,h2);
end
if length(vlistc2) ~= 1
m = 'You must choose precisely one control variable. try again';
fprintf(MEXEC_A.Mfider,'%s\n',m)
continue
end
vcontrol2 = vlistc2;
ok = 1;
end
data_c_2 = nc_varget(ncfile_in2.name,h2.fldnam{vcontrol2});
if m_isvartime(h2.fldnam{vcontrol2}); data_c_2 = m_adjtime(h2.fldnam{vcontrol2},data_c_2,h2,h); end % adjust time to data time origin of first input file
numdims_c = m_numdims(data_c_2);
contmerge = 0;
if numdims_c == 2
% 2-d control var
m = 'Your control var has 2 dimensions, do you want to use a row or column ';
m2 = 'as the independent variable for the interpolation ?';
fprintf(MEXEC_A.Mfidterm,'%s\n',m,m2);
ok = 0;
while ok == 0;
m3 = sprintf('%s','type r for row, c for column : ');
reply = m_getinput(m3,'s');
if strcmp(reply,'r'); rc = 2; break; end % ind var is a row; colindex varies
if strcmp(reply,'c'); rc = 1; break; end % ind var is a col; rowindex varies
fprintf(MEXEC_A.Mfider,'\n%s\n','You must reply r or c : ');
end
str = {'column' 'row'};
strtext = str{rc};
% find row/column number
m1 = 'Interpolation only works when the independent variable is 1-D ?';
m2 = ['Which ' strtext ' do you want to use ?'];
fprintf(MEXEC_A.Mfidterm,'%s\n',m1,m2);
maxd = size(data_c_2,1);
ok = 0;
while ok == 0;
m3 = sprintf('%s',['type number in range 1 (default) to ' sprintf('%d',maxd) ' ']);
reply = m_getinput(m3,'s');
if strcmp(reply,' '); contindex = 1; break; end
cmd = ['contindex = [' reply '];']; %convert char response to number
eval(cmd);
if length(contindex) ~= 1; continue; end
ok = 1;
end
m1 = ['Do you want to merge on the other ' strtext 's of the control variable ?'];
fprintf(MEXEC_A.Mfidterm,'%s\n',m1);
maxd = size(data_c_2,1);
ok = 0;
while ok == 0;
m3 = sprintf('%s',['reply no (n, default) or yes (y) ']);
reply = m_getinput(m3,'s');
if strcmp(reply,' '); contmerge = 0; break; end
if strcmp(reply,'n'); contmerge = 0; break; end
if strcmp(reply,'y'); contmerge = 1; break; end
end
if rc == 1
x = data_c_2(:,contindex);
else
x = data_c_2(contindex,:);
end
else
% 1-D var
x = data_c_2;
end
xdm = min(diff(x));
if xdm <= 0
m = 'Control variable was not monotonic; it will be sorted';
fprintf(MEXEC_A.Mfider,'%s\n',m)
[xsort ksort] = sort(x);
else
xsort = x; ksort = 1:length(x);
end
xbad = isnan(xsort);
if sum(xbad) > 0
m = ['Control variable contained ' sprintf('%d',sum(xbad)) ' nan values; these are not valid in interp1 and have been removed'];
fprintf(MEXEC_A.Mfider,'%s\n',m)
xsort(xbad) = [];
end
% now we've got the two control variables: data_c_1 and xsort.
% xsort has been adjusted for data time origin if necessary
% if they're both time variables but one is days and the other is seconds,
% scale xsort into same units as data_c_1
%vname_c_1 = vname_c_1
unit_c_1 = h.fldunt(vlistc);
vname_c_2 = h2.fldnam{vcontrol2};
unit_c_2 = h2.fldunt{vcontrol2};
if (m_isvartime(vname_c_1) == 1) & (m_isvartime(vname_c_2) == 1)
% both recognised as time variables
if m_isunitsecs(unit_c_2) == 1
if m_isunitdays(unit_c_1) == 1
xsort = xsort/86400;
end
end
if m_isunitdays(unit_c_2) == 1
if m_isunitsecs(unit_c_1) == 1
xsort = xsort*86400;
end
end
end
% check whether data_c_1 is in range of xsort
xmin = min(xsort);
xmax = max(xsort);
ximin = min(min(data_c_1));
ximax = max(max(data_c_1));
if ximin < xmin
m = ['warning file 1 control variable is not contained within file 2 at low end'];
fprintf(MEXEC_A.Mfider,'%s\n',m)
end
if ximax > xmax
m = ['warning file 1 control variable is not contained within file 2 at high end'];
fprintf(MEXEC_A.Mfider,'%s\n',m)
end
% control variable ok, now get variables for merge
m = sprintf('%s\n','Type variable names or numbers for variables for merge (return for none, ''/'' for all with matching dimensions): ');
var = m_getinput(m,'s');
if strcmp(' ',var) == 1;
vlist2 = [];
else
vlist2 = m_getvlist(var,h2);
if ~MEXEC_G.quiet; disp(['list is ' sprintf('%d ',vlist2) ]); end
end
ok = 0;
m1 = sprintf('%s',['If NaNs are found in the merging variable, do you want them filled first (f, default)']);
m2 = sprintf('%s',['or do you want them kept in place (k) so that NaNs may appear in the output ?']);
fprintf(MEXEC_A.Mfidterm,'%s\n',m1,m2);
while ok == 0;
m3 = sprintf('%s','reply ''f'' or return for fill or ''k'' for keep : ');
reply = m_getinput(m3,'s');
if strcmp(reply,' '); absfill = 1; break; end
if strcmp(reply,'f'); absfill = 1; break; end
if strcmp(reply,'k'); absfill = 0; break; end
end
% find vars with 'matching' dimensions
crows = h2.dimrows(vcontrol2);
ccols = h2.dimcols(vcontrol2);
ccycles = crows*ccols;
nrows = h2.dimrows;
ncols = h2.dimcols;
ncycles = nrows.*ncols;
if ccols == 1; rc = 1; end; % only one col so use it as independent var;
if crows == 1; rc = 2; end; % only one row so use it as independent var;
if(rc == 1) % we're working down columns
kmat = find(nrows == crows);
end
if(rc == 2) % we're working along rows
kmat = find(ncols == ccols);
end
if contmerge == 1; vlist2 = [vcontrol2 vlist2]; end% since the user asked for the merging variable, make sure it is in the list
kmat = intersect(kmat,vlist2); % find vars that are in user's list and have suitable dims
if contmerge == 0; kmat = setdiff(kmat,vcontrol2); end % remove control var from action list
%leave control var in if contmerge == 1
if rc == 2;
% need to transpose so that interp1 works on gridded data
xsort = xsort';
end
if ~MEXEC_G.quiet; disp(['list of matching vars is ' sprintf('%d ',kmat) ]); end
for k = 1:length(kmat)
vname = h2.fldnam{kmat(k)};
kmat2 = strmatch(vname,h.fldnam(vlist),'exact');
if ~isempty(kmat2) % attempting to copy a variable that has already been taken from first file
m1 = ['attempting to merge a variable ' vname ];
m2 = ['that has already been copied from first input file'];
m3 = ['you need to rename the variable for output'];
fprintf(MEXEC_A.Mfider,'%s\n',m1,m2,m3)
ok = 0;
while ok == 0;
m3 = sprintf('%s',['type new variable name for output : ']);
newname = m_getinput(m3,'s');
if strcmp(newname,' ') | strcmp(newname,'/');
m = 'try again';
fprintf(MEXEC_A.Mfider,'%s\n',m)
continue
end
newname = m_remove_outside_spaces(newname);
newname = m_check_nc_varname(newname);
ok = 1;
end
else
newname = vname;
end
if ~MEXEC_G.quiet
m = ['Merging ' h2.fldnam{kmat(k)}];
fprintf(MEXEC_A.Mfidterm,'%s\n',m);
end
z = nc_varget(ncfile_in2.name,h2.fldnam{kmat(k)});
if m_isvartime(h2.fldnam{kmat(k)}); z = m_adjtime(h2.fldnam{kmat(k)},z,h2,h); end % adjust time to data time origin of first input file
% rearrange data according to sort of control variable
if rc == 2;
% need to transpose so that interp1 works on gridded data
z = z';
end
zsort = z(ksort,:);
zsort(xbad,:) = [];
if absfill == 1;
% fill any nans in z by interpolation
for kfill = 1:size(zsort,2);
ok = ~isnan(zsort(:,kfill));
if sum(ok) < 2 ; continue; end % not enough good data to fill with interp1
if sum(ok) == size(zsort,1); continue; end % all data are good; skip interp1
zsort(:,kfill) = interp1(xsort(ok),zsort(ok,kfill),xsort);
end
end
[dontcare iuni juni]=unique(xsort);
zi = interp1(xsort(iuni),zsort(iuni),data_c_1); % zi has same dimensions as data_c_1
% yi = interp1(x,y,xi)
% if y is 2-D then yi is always a column, so we need to fix its shape
% to match xi
% if y is 1-D, then yi is the same shape as xi
if m_numdims(zsort) == 1 % zsort is 1-D
ktranspose = 0; % dimensions of zi will match data_c_1;
else % zsort is 2-D and zi will have merge index varying down a column
if size(data_c_1,1) == 1
% data_c_1 was a row vector. Force z to be a row vector
ktranspose = 1;
else
ktranspose = 0;
end
end
if ktranspose == 1
zi = zi';
end
% write the output
clear v
v.name = newname;
v.data = zi;
m_write_variable(ncfile_ot,v,'nodata'); %write the variable information into the header but not the data
% next copy the attributes
vinfo = nc_getvarinfo(ncfile_in2.name,vname);
va = vinfo.Attribute;
for k2 = 1:length(va)
vanam = va(k2).Name;
vaval = va(k2).Value;
nc_attput(ncfile_ot.name,newname,vanam,vaval);
end
% now write the data, using the attributes already saved in the output file
% this provides the opportunity to change attributes if required, eg fillvalue
nc_varput(ncfile_ot.name,newname,v.data);
m_uprlwr(ncfile_ot,newname);
end
% finish up
m_finis(ncfile_ot);
h = m_read_header(ncfile_ot);
if ~MEXEC_G.quiet; m_print_header(h); end
hist = h;
hist.filename = ncfile_ot.name;
MEXEC_A.Mhistory_ot{1} = hist;
m_write_history;
return
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment