Home > reduc > alpha > writeAlphaDatabase.m

writeAlphaDatabase

PURPOSE ^

function writeAlphaDatabase(x,MODE)

SYNOPSIS ^

function fnames = writeAlphaDatabase(x,MODE)

DESCRIPTION ^

 function writeAlphaDatabase(x,MODE)

 This function appends the inputs values to the database of alpha values 
 on disk. x is formatted as specified in the header of readAlphaDatabase.m
   MODE: if == REPLACE, then replace all alpha database values. 
         if == APPEND then add the values to the end

 OGK, 13 March 2012

 Edited: OGK, 19 November 2012
         Now writes the values to the appropriate database file.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function fnames = writeAlphaDatabase(x,MODE)
0002 % function writeAlphaDatabase(x,MODE)
0003 %
0004 % This function appends the inputs values to the database of alpha values
0005 % on disk. x is formatted as specified in the header of readAlphaDatabase.m
0006 %   MODE: if == REPLACE, then replace all alpha database values.
0007 %         if == APPEND then add the values to the end
0008 %
0009 % OGK, 13 March 2012
0010 %
0011 % Edited: OGK, 19 November 2012
0012 %         Now writes the values to the appropriate database file.
0013 %
0014 
0015 % what files are we going to have to overwrite/append to?
0016 fnames = getAlphaDatabaseName(x{1});
0017 
0018 % Need to create the formatting string
0019 
0020 % This string tells which precision setting to use when writing to file.
0021 % One for each element of x (which are matrices). It is the number of
0022 % decimal points to use in exponential notation.
0023 precision = [   11; % t
0024                 4; % aDD
0025                 4; % gDD
0026                 4; % VaDD
0027                 4; % VgDD
0028                 4; % gPO
0029                 4; % VgPO
0030                 4; % gFILT
0031                 4; % aFILT
0032                 4; % VgFILT
0033                 4; % VaFILT
0034                 2; % r
0035                 3; % T
0036                 3; % point
0037                 3; % temps
0038                 0; % TYPE
0039                 0]; % FLAGGED
0040 % expected width of elements of x:
0041 wdth = [    1; % t
0042             5; % aDD
0043             5; % gDD
0044             5; % VaDD
0045             5; % VgDD
0046             2; % gPO
0047             2; % VgPO
0048             5; % gFILT
0049             3; % aFILT
0050             5; % VgFILT
0051             3; % VaFILT
0052             2; % r
0053             2; % T
0054             4; % point
0055             7; % temps
0056             1; % TYPE
0057             1]; % FLAGGED
0058 format_string = '';
0059 for k=1:length(precision)
0060     if precision(k) > 0
0061         newS = sprintf('%%%d.%dE ',precision(k)+1,precision(k));
0062     else
0063         newS = '%d ';
0064     end
0065     % add this new string once for each column of x{k}
0066     for m=1:size(x{k},2)
0067         format_string = [format_string newS];
0068     end
0069 end
0070 format_string = [format_string '\n'];
0071 
0072 for k=1:size(fnames,1)
0073     % Decision tree:
0074     %   Identify all the time samples for relevant month.
0075     %   Either append or replace the data.
0076     
0077     if strcmp(MODE,'APPEND')
0078         tfile = fopen(fnames{k},'a+');
0079     elseif strcmp(MODE,'REPLACE')
0080         tfile = fopen(fnames{k},'w+');
0081     else
0082         error('writeAlphaDatabase:WrongFlag',...
0083             'Invalid FLAG specification: Only APPEND and REPLACE accepted.')
0084     end
0085 
0086     % Need to create the matrix to write:
0087     year = str2double(fnames{k}(end-10:end-7));
0088     month = str2double(fnames{k}(end-5:end-4));
0089     mjdstart = date2mjd(year,month,1,0,0,0);
0090     if month < 12
0091         mjdstop = date2mjd(year,month+1,1,0,0,0);
0092     else
0093         mjdstop = date2mjd(year+1,1,1,0,0,0);
0094     end
0095     I = (x{1} >= mjdstart) & (x{1} < mjdstop);
0096     data_matrix = [];
0097     for k=1:length(x)
0098         if size(x{k},2) ~= wdth(k)
0099             disp(sprintf('writeAlphaDatabase:: Warning: element %d of x has width %d, expected %d',...
0100                 k,size(x{k},2),wdth(k)))
0101         end
0102         data_matrix = cat(2,data_matrix,x{k}(I,1:wdth(k)));
0103     end
0104     % and finally, actually write the string to disk.
0105     fprintf(tfile,format_string,data_matrix');
0106     fclose(tfile);
0107 end
0108 
0109 end

Generated on Sun 14-Jun-2015 17:12:45 by m2html © 2005