Demo entry 6640992

matlab

   

Submitted by anonymous on Sep 14, 2017 at 17:25
Language: Matlab. Code size: 3.0 kB.

function modify_nml(iodir,casename,cycle,NML_VARS,NML_VALUES)
%--------Setup---------%
 %iodir='/media/tomchen/Ubuntu/FVCOM/Work/run/bh_nml';
 %casename='bhice_1211_1303_1hour_depthplus1';
 %cycle=1;
    %-------Define:Vars&Value------%   ##MODIFY!!
    %   NML_VARS={'START_DATE','END_DATE'}; 
    % NML_VALUES={'2012-11-01 01:00:00','2013-01-01 22:00:00'};
    %=======Define:Vars&Value======%

nml_name=[casename,'_run.nml'];
new_nml_name=[casename,sprintf('%03d',cycle),'_run.nml'];
cd (iodir);
%========Setup=========%




%------Read Namelist------%
disp(['Old Namelist',': ',nml_name]);
ffid = fopen(nml_name,'r');
i = 1;
while feof(ffid) == 0
	tline{i,1} = fgetl(ffid);
	i = i+1;
end
fclose(ffid);
%======Read Namelist======%

%------Main Work----------%

%1.FIND NML_VARS
for j=1:length(NML_VARS)
	for i=1:length(tline)
		if (strfind(tline{i},NML_VARS{j}) >0)
			NML_VARS_LINE(j)=i;
			break;
		end
	end
end

%2.CHANGE NML_VALUES

for i=1:length(NML_VARS)

disp(['Old LINE',num2str(i),': ',tline{NML_VARS_LINE(i)}]);

	for j=1:length(tline{NML_VARS_LINE(i)})
		if strcmp(tline{NML_VARS_LINE(i)}(j),'=') 
			NML_VALUES_EQUAL(i)=j;		%LOCATION "=" 
			break;
		end
	end

		LEN_LINE=length(tline{NML_VARS_LINE(i)});
	start_modify=NML_VALUES_EQUAL(i)+2; 	%INCLUDE: SPACE
	if isstr(NML_VALUES{i})				%JUDGE str or number
		end_modify=start_modify+1+length(NML_VALUES{i}); 			%str:INCLUDE  2:' ' 
	else
		end_modify=start_modify-1+length( num2str(NML_VALUES{i}) );	%num
	end

	if isstr(NML_VALUES{i})				%JUDGE str or number 

		if LEN_LINE> end_modify		%'str'
			tline{NML_VARS_LINE(i)}(start_modify:end_modify)=['''',NML_VALUES{i},''''];					%modify
			tline{NML_VARS_LINE(i)}(end_modify+1:end)=' ';												%fill space last
		elseif LEN_LINE< end_modify
			tline{NML_VARS_LINE(i)}=[tline{NML_VARS_LINE(i)},repmat(' ',1,end_modify-LEN_LINE)];		%fill space first
			tline{NML_VARS_LINE(i)}(start_modify:end_modify)=['''',NML_VALUES{i},''''];					%modify
		else
			tline{NML_VARS_LINE(i)}(start_modify:end_modify)=['''',NML_VALUES{i},''''];					%modify
		end

	else
		if LEN_LINE> end_modify		%num
			tline{NML_VARS_LINE(i)}(start_modify:end_modify)= [num2str( NML_VALUES{i} )];					%modify
			tline{NML_VARS_LINE(i)}(end_modify+1:end)=' ';												%fill space last
		elseif LEN_LINE< end_modify
			tline{NML_VARS_LINE(i)}=[tline{NML_VARS_LINE(i)},repmat(' ',1,end_modify-LEN_LINE)];		%fill space first
			tline{NML_VARS_LINE(i)}(start_modify:end_modify)= [num2str( NML_VALUES{i} )];					%modify
		else
			tline{NML_VARS_LINE(i)}(start_modify:end_modify)= [num2str( NML_VALUES{i} )];					%modify
		end

	end

disp(['NEW LINE',num2str(i),': ',tline{NML_VARS_LINE(i)}]);

end


%======Main Work==========%



%------Write Namelist------%
disp(['New Namelist',': ',new_nml_name]);
ffid = fopen(new_nml_name,'w');
for i=1:length(tline)
    fprintf(ffid,[tline{i},'\n']);
end
fclose(ffid);
%======Write New Namelist======%

end

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).