Warning, /verification/tutorial_global_oce_latlon/diags_matlab/mit_getparm.m is written in an unsupported language. File is not indexed.
view on githubraw file Latest commit add29e06 on 2018-01-31 20:35:05 UTC
051ee7f715 Jean*0001 function y = mit_getparm(fname,pname);
0002 %function y = mit_getparm(fname,pname);
0003
0004 y = [];
0005 [fp, msg] = fopen(fname,'r');
0006 if fp > 0
0007 str{1} = [];
0008 notfound = 0;
0009 while isempty(mystrfind(str{1},pname))
0010 str{1} = fgetl(fp);
0011 if ~ischar(str{1}); % this catches the end of file (not very clean)
0012 notfound = 1;
0013 break;
0014 end
0015 % clear again if commented
0016 if ~isempty(mystrfind(str{1},'#'))
0017 str{1} = [];
0018 end
0019 end
0020 if notfound
0021 disp(['Warning: ' pname ' not found in ' fname])
0022 y=[];
0023 % disp([' setting ' pname ' to zero'])
0024 % y = 0;
0025 else
0026 teststr = [];
0027 % find the termination of parameter pname
0028 % (next line with an '=' sign
0029 % or with a namelist termination character '&' or '/')
0030 n = 1;
0031 while ( isempty(mystrfind(teststr,'=')) & ...
0032 isempty(mystrfind(teststr,'&')) & ...
0033 isempty(mystrfind(teststr,'/')) )
0034 n = n + 1;
0035 teststr = fgetl(fp);
0036 str{n} = teststr;
0037 if ~ischar(teststr); break; end
0038 end
0039 eqind = findstr(str{1},'=');
0040 y = str{1}(eqind+1:end-1);
0041 % check whether it is a string in quotes or something else
0042 quotes = findstr(y,'''');
0043 if ~isempty(quotes)
0044 y(quotes(2):end) = [];
0045 y(1:quotes(1)) = [];
0046 else
0047 if ~( strcmpi(y,'.TRUE.') | strcmpi(y,'.FALSE.') )
0048 y = convert_str2num(str{1}(eqind+1:end-1));
0049 for k=2:n-1
0050 y = [y; convert_str2num(str{k}(eqind+1:end-1))];
0051 end
0052 % $$$ y = str2num(str{1}(eqind+1:end-1))';
0053 % $$$ for k=2:n-1
0054 % $$$ y = [y; str2num(str{k}(eqind+1:end-1))'];
0055 % $$$ end
0056 end
0057 end
0058 end
0059 fclose(fp);
0060 else
0061 error([fname ' could not be opened: ' msg])
0062 end
0063
0064 return
0065
0066 function y = convert_str2num(str)
0067
0068 % take care of the n*value format
0069 stars = findstr(str,'*');
0070 if isempty(stars)
0071 y = str2num(str)';
0072 else
0073 if length(stars)==1
0074 y=repmat(str2num(str(stars+1:end)),[str2num(str(1:stars-1)) 1]);
0075 else
0076 warning('The format n*x1,m*x2, ... cannot be handled')
0077 % $$$ for k=1:length(stars)
0078 % $$$ % find the beginnin and termination of the set
0079 % $$$ end
0080 end
0081 end
0082
0083 return
0084
0085 function [ind] = mystrfind(str,pattern)
0086
0087 if length(pattern) > length(str)
0088 ind = [];
0089 return
0090 else
0091 ind = findstr(str,pattern);
0092 end