function oi = roms_read_ocean_in(fn) % roms_read_ocean_in - Read a ROMS .in file and returns parameters % oi = roms_read_ocean_in( fn ) % % Input % fn - filename, optional; default = 'ocean.in', but it can also read % sediment.in % % Return % oi - structure with all info found in .in file % For convenience, the values of some important ones are % stored under their name in the structure. The rest are in an array of % structures oi.data, with fields: % oi.data(i).keyword % .numval % .strval % These have to be parsed appropriately. Examples at end of script. % csherwood@usgs.gov if(exist('fn','var')~=1), fn='ocean.in'; end fid = fopen( fn, 'r' ); if(fid == -1) fprintf(1,'Could not open %s\n',fn) error('punt'); end i=0; % counter for oi.data(i) structure while ~feof(fid) % get a line from the file, use strtrim to remove "insignificant" white space tline = strtrim( fgetl( fid ) ); if( ~isempty(tline) && ~strcmp(tline(1),'!') ) % has content and does not start w/ comment char, so process % first, truncate at first comment character k =strfind(tline,'!'); if( ~isempty(k) ) tline = tline(1:k-1); end % now look for equal signs. Note: there may be two. k = strfind(tline,'='); if( ~isempty(k) )% has '=' or '=='; looks like valid keyword i=i+1; % read the first part of the line as a keyword, removing blanks: oid(i).keyword = strtrim( tline(1:k(1)-1) ); % Try to read the remainder as numbers (danger here, because the % ROMS files use nnndn format, but sscanf expects nnnen format, so % real numbers may be truncated, and multiple numbers wont be % recognized. Try to handle this here. temp = strtrim(tline(k(end)+1:end)); kk = strfind(temp,'d'); if(~isempty(kk)), temp(kk)='e'; end oid(i).numval = sscanf(temp,'%g' ); % and also, try to read the remainder as strings, but first % check to see if there is a continuation character more_continuation = 1; temp = tline(k(end)+1:end); while more_continuation m = strfind(temp,'\'); % look for continuation line if( isempty(m) ) % no continuation line kk =strfind(temp,'!'); if(~isempty(kk)) % trim line at comment temp = temp(1:kk-1); end t1 = temp; if(exist('tt','var')) % concatenate with previous line tt = [tt,' ',t1]; else tt = t1; end more_continuation = 0; else % found continuation char, read another line t1 = temp(1:m-1); if(exist('tt','var')) % concatenate with previous line tt = [tt,' ',t1]; else tt = t1; end temp = fgetl(fid); more_continuation = 1; end end oid(i).strval = tt; clear tt; else % not a comment, not a continuation, no '=', what is it? fprintf(1,'Dont know how to handle this:\n%s\n',tline) end end end fclose(fid); if(i==0),error('did not find any keywords'), end % store structure array in return structure oi.data = oid; % extract a few keywords inportant for initialization % and duplicate them in the return structure for convenience % (also serves as an example of how to find others) % following assume we are reading ocean.in for j=1:i, if( strcmpi( oid(j).keyword, 'TITLE' )), oi.TITLE = strtrim(oi.data(j).strval); end if( strcmpi( oid(j).keyword, 'MyAppCPP' )), oi.MyAppCPP = strtrim(oi.data(j).strval); end if( strcmpi( oid(j).keyword, 'VARNAME' )), oi.VARNAME = strtrim(oi.data(j).strval); end if( strcmpi( oid(j).keyword, 'GRDNAME' )), oi.GRDNAME = strtrim(oi.data(j).strval); end if( strcmpi( oid(j).keyword, 'ININAME' )), oi.ININAME = strtrim(oi.data(j).strval); end if( strcmpi( oid(j).keyword, 'Lm' )), oi.Lm = oi.data(j).numval; end if( strcmpi( oid(j).keyword, 'Mm' )), oi.Mm = oi.data(j).numval; end if( strcmpi( oid(j).keyword, 'N' )), oi.N = oi.data(j).numval; end if( strcmpi( oid(j).keyword, 'Nbed' )), oi.Nbed = oi.data(j).numval; end if( strcmpi( oid(j).keyword, 'NAT' )), oi.NAT = oi.data(j).numval; end if( strcmpi( oid(j).keyword, 'NPT' )), oi.NPT = oi.data(j).numval; end if( strcmpi( oid(j).keyword, 'NCS' )), oi.NCS = oi.data(j).numval; end if( strcmpi( oid(j).keyword, 'NNS' )), oi.NNS = oi.data(j).numval; end if( strcmpi( oid(j).keyword, 'THETA_S' )), oi.THETA_S = oi.data(j).numval; end if( strcmpi( oid(j).keyword, 'THETA_B' )), oi.THETA_B = oi.data(j).numval; end if( strcmpi( oid(j).keyword, 'TCLINE' )), oi.TCLINE = oi.data(j).numval; end if( strcmpi( oid(j).keyword, 'NFFILES' )), oi.NFFILES = oi.data(j).numval; end if( strcmpi( oid(j).keyword, 'FRCNAME' )), oi.FRCNAME = oi.data(j).strval; end end %% following assume that we are reading sediment.in for j=1:i, if( strcmpi( oid(j).keyword, 'MUD_SD50' )),oi.MUD_SD50 = oi.data(j).numval; end if( strcmpi( oid(j).keyword, 'MUD_SRHO' )),oi.MUD_SRHO = oi.data(j).numval; end if( strcmpi( oid(j).keyword, 'MUD_WSED' )),oi.MUD_WSED = oi.data(j).numval; end if( strcmpi( oid(j).keyword, 'MUD_TAU_CE' )),oi.MUD_TAU_CE = oi.data(j).numval; end if( strcmpi( oid(j).keyword, 'MUD_ERATE' )),oi.MUD_ERATE = oi.data(j).numval; end if( strcmpi( oid(j).keyword, 'SAND_SD50' )),oi.SAND_SD50 = oi.data(j).numval; end if( strcmpi( oid(j).keyword, 'SAND_SRHO' )),oi.SAND_SRHO = oi.data(j).numval; end if( strcmpi( oid(j).keyword, 'SAND_WSED' )),oi.SAND_WSED = oi.data(j).numval; end if( strcmpi( oid(j).keyword, 'SAND_TAU_CE' )),oi.SAND_TAU_CE = oi.data(j).numval; end if( strcmpi( oid(j).keyword, 'SAND_ERATE' )),oi.SAND_ERATE = oi.data(j).numval; end end