Home > atmlab > arts > scenegen > asg_rndmz.m

asg_rndmz

PURPOSE ^

ASG_RNDMZ Adds random disturbances of ASG variables

SYNOPSIS ^

function G = asg_rndmz( G )

DESCRIPTION ^

 ASG_RNDMZ   Adds random disturbances of ASG variables

    All data in G having the RNDMZ field set are disturbed. This field
    is a structure, having these general fields:

       FORMAT     String giving the format used to describe the disturbances.
                  Valid options listed below.
       TYPE       Can either be 'abs' or 'rel'. Determines if given data 
                  specifies an absolute or relative disturbance.  In the
                  first case, standard deviations are given in same unit as
                  the data, while in the second case relative values are 
                  given (such as 0.5 for a 50% disturbance).
       DATALIMS   Lower and upper limit for data. A vector. The first value 
                  gives lower limit, and second value upper limit. Data 
                  below/above a limit, is set to the limit value. 
                  The field is not mandatory. Upper limit can be left out.
                  If a limit is set to NaN, all values will be accepted.

    The following formats are handled

    FORMAT = 'param'
    -----------------
    The format of *covmat3d* is here used to specify the disturbances. 
    That is, the covariance matrix for selected variability is described
    in a parametrised way.

 FORMAT   G = asg_rndmz( G )
        
 OUT   G   Modified ASG data. 
 IN    
       G            ASG data.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

DOWNLOAD ^

asg_rndmz.m

SOURCE CODE ^

0001 % ASG_RNDMZ   Adds random disturbances of ASG variables
0002 %
0003 %    All data in G having the RNDMZ field set are disturbed. This field
0004 %    is a structure, having these general fields:
0005 %
0006 %       FORMAT     String giving the format used to describe the disturbances.
0007 %                  Valid options listed below.
0008 %       TYPE       Can either be 'abs' or 'rel'. Determines if given data
0009 %                  specifies an absolute or relative disturbance.  In the
0010 %                  first case, standard deviations are given in same unit as
0011 %                  the data, while in the second case relative values are
0012 %                  given (such as 0.5 for a 50% disturbance).
0013 %       DATALIMS   Lower and upper limit for data. A vector. The first value
0014 %                  gives lower limit, and second value upper limit. Data
0015 %                  below/above a limit, is set to the limit value.
0016 %                  The field is not mandatory. Upper limit can be left out.
0017 %                  If a limit is set to NaN, all values will be accepted.
0018 %
0019 %    The following formats are handled
0020 %
0021 %    FORMAT = 'param'
0022 %    -----------------
0023 %    The format of *covmat3d* is here used to specify the disturbances.
0024 %    That is, the covariance matrix for selected variability is described
0025 %    in a parametrised way.
0026 %
0027 % FORMAT   G = asg_rndmz( G )
0028 %
0029 % OUT   G   Modified ASG data.
0030 % IN
0031 %       G            ASG data.
0032 
0033 % 2007-10-22   Created by Patrick Eriksson.
0034 
0035 
0036 function G = asg_rndmz( G )
0037 
0038   
0039 
0040 for ig = 1 : length( G )
0041   
0042   %Something to do?
0043   if isempty( G(ig).RNDMZ )
0044     continue;  % ---->
0045   end
0046   
0047   if ~isfield( G(ig).RNDMZ, 'FORMAT' )
0048     error( sprintf( 'The field FORMAT is missing in G(%d).RNDMZ.', ig ) );
0049   end
0050   
0051   
0052   switch lower( G(ig).RNDMZ.FORMAT )
0053   
0054    case 'param'
0055     %
0056     G(ig) = do_param( G(ig), ig );
0057     
0058    otherwise
0059     error( sprintf( 'Not recognised choice for G(ig).RNDMZ.FORMAT.', ig ) );
0060   end
0061   
0062 
0063   %- Remove too low values
0064   %
0065   if isfield( G(ig).RNDMZ, 'DATALIMS' )
0066     if ~isnan( G(ig).RNDMZ.DATALIMS(1) )
0067       ind             = find( G(ig).DATA < G(ig).RNDMZ.DATALIMS(1) );
0068       G(ig).DATA(ind) = G(ig).RNDMZ.DATALIMS(1);
0069     end
0070     if length(G(ig).RNDMZ.DATALIMS) > 2  &  ~isnan( G(ig).RNDMZ.DATALIMS(1) )
0071       ind             = find( G(ig).DATA > G(ig).RNDMZ.DATALIMS(2) );
0072       G(ig).DATA(ind) = G(ig).RNDMZ.DATALIMS(2);
0073     end
0074   end
0075 
0076 end
0077 
0078 return
0079 %------------------------------------------------------------------------------
0080 
0081 
0082 function G = do_param( G, ig )
0083   
0084   %- Only fields are handled (so far?)
0085   if G.SURFACE
0086     error( sprintf( ['G(%d).RNDMZ.FORMAT equals ''param'', which so far ',...
0087                                  'only works for atmospheric fields.'], ig ) );
0088   end
0089 
0090   %- Check G.DIM and determine dimensionality
0091   %
0092   dim = G.DIM;
0093   %
0094   if isempty(dim)
0095     error( sprintf('0D data not handled (found in G(%d)).', ig ) );
0096   end
0097   %if length(dim) ~= dim(end)
0098   %  error( sprintf('Pressure or latitude is missing in G(%d).DIMS', ig ) );
0099   %end
0100   %
0101   %dim = dim(end);
0102   
0103   %- Create covariance matrix for disturbance
0104   %
0105   % Use try-ctch for more informative error message
0106   %
0107   try
0108     S = covmat3d( dim, G.RNDMZ, G.GRID1, G.GRID2, G.GRID3, 'atm' );
0109   catch 
0110     fprintf( '%s\n\n', lasterr );
0111     error( sprintf('Incorrect covariance definition for item %d.',ig) );
0112   end
0113 
0114   %- Create disturbance (loop around cases)
0115   %
0116   if strcmp( G.RNDMZ.TYPE, 'rel' )
0117     for ic = 1 : size( G.DATA, 4 )
0118       G.DATA(:,:,:,ic) = G.DATA(:,:,:,ic) .* ...
0119               reshape( randmvar_normal2( 1, S, 1 ), size(G.DATA(:,:,:,ic)) );
0120     end
0121   elseif strcmp( G.RNDMZ.TYPE, 'abs' )
0122     for ic = 1 : size( G.DATA, 4 )
0123       G.DATA(:,:,:,ic) = G.DATA(:,:,:,ic) + ...
0124               reshape( randmvar_normal2( 0, S, 1 ), size(G.DATA(:,:,:,ic)) );
0125     end
0126     
0127   else
0128     error( sprintf( 'Unknown selection in G(ig).RNDMZ.TYPE.', ig ) );
0129   end
0130   
0131 return
0132

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005