


PUPROSE: evaluate and write GLIMMER configuration struct to .conf file
-------------------------------------------------------------------
USAGE: conftext = print_glimmer_conf(conf,fname,title)
where: conf is configuration struct as delivered by get_glimmer_default_config()
fname is filename to print out
title is optional title for header line of config file
EXAMPLE:
print_glimmer_conf(get_glimmer_default_config(),'default.conf','Default GLIMMER config for gen_pdd_glide')

0001 function conftext = print_glimmer_conf(conf,fname,title) 0002 % PUPROSE: evaluate and write GLIMMER configuration struct to .conf file 0003 % ------------------------------------------------------------------- 0004 % USAGE: conftext = print_glimmer_conf(conf,fname,title) 0005 % where: conf is configuration struct as delivered by get_glimmer_default_config() 0006 % fname is filename to print out 0007 % title is optional title for header line of config file 0008 % 0009 % EXAMPLE: 0010 % print_glimmer_conf(get_glimmer_default_config(),'default.conf','Default GLIMMER config for gen_pdd_glide') 0011 0012 % See also: get_glimmer_default_config() 0013 % 0014 % Note: Uses a struct with fieldnames equaling config file [Section] names 0015 % and struct variable names equalling config file section parameters. If section names 0016 % can not be used as fieldnames, first struct field called .section.names 0017 % can be used, with one name for each section to appear in the config file 0018 % (e.g. for two [CF output] sections the name has to appear twice) 0019 % 0020 % Felix Hebeler, Dept. of Geography, University Zurich, June 2007. 0021 0022 if exist('title','var') 0023 conftext=['#',title,'\n']; 0024 else 0025 conftext='#Autogenerated configure file for the GLIMMER ISM using MATLAB and print_glimmer_conf()\n'; 0026 end; 0027 0028 n1=fieldnames(conf); 0029 if sum(strcmp(n1,'sections'))==1; % if fieldnames contains sections, use those names instead of the fieldnames 0030 sectfield=true; 0031 end 0032 % read toplevel fields and loop through 0033 for i1 = 1:size(n1,1) % 0034 if ~strcmp(n1(i1),'sections'); % skip section field 0035 n2=eval(['fieldnames(conf.',char(n1(i1)),')']); % gives 2nd level fields 0036 if sectfield 0037 %i1 0038 conftext = [conftext,'[',char(conf.sections(i1-1).names),']','\n']; 0039 else 0040 conftext = [conftext,'[',char(n1(i1)),']','\n']; 0041 end 0042 for i2 = 1:size(n2,1) 0043 % read sublevel fields and loop through 0044 s = eval(['size(conf.',char(n1(i1)),',2)']); 0045 if s>1; % if there's more than one field, check whichones to use 0046 eval(['f1=conf.',char(n1(i1)),'(',num2str(1),').',char(n2(i2)),';']); % gives the value of field n2 0047 conftext = [conftext,char(n2(i2)),' = ',num2str(f1)]; 0048 for i3= 2:s %loop over varnames to check whether they have different entries 0049 eval(['f1=conf.',char(n1(i1)),'(',num2str(i3-1),').',char(n2(i2)),';']); 0050 eval(['f2=conf.',char(n1(i1)),'(',num2str(i3),').',char(n2(i2)),';']); 0051 if ~strcmp(num2str(f1),num2str(f2)); % if values differ, add them whitespace separated 0052 conftext = [conftext,' ',num2str(f2)]; 0053 end 0054 end 0055 conftext = [conftext,'\n']; % at end of variables insert a newline 0056 else 0057 val=eval(['conf.',char(n1(i1)),'.',char(n2(i2))]); 0058 conftext = [conftext,char(n2(i2)),' = ',num2str(val),'\n']; 0059 end % if s>1 loop 0060 end % loop over 2nd level fields 0061 end % skip sections 0062 conftext = [conftext,'\n']; 0063 end 0064 %% open file print out 0065 fid = fopen(fname,'wt'); 0066 conftext=sprintf(conftext); 0067 fprintf(fid,'%s',conftext); 0068 fclose(fid);