Changeset 26042


Ignore:
Timestamp:
03/05/21 15:37:46 (4 years ago)
Author:
Mathieu Morlighem
Message:

CHG: added some documentation and made sure that interpBedmachine Ant and Green are similar

Location:
issm/trunk-jpl/src/m/contrib/morlighem/modeldata
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/m/contrib/morlighem/modeldata/interpBedmachineAntarctica.m

    r26041 r26042  
    11function output = interpBedmachineAntarctica(X,Y,string,method,ncdate),
     2%INTERPBEDMACHINEANTARCTICA - interpolate BedMachine data onto X and Y
     3%
     4%   Examples:
     5%      bed       = interpBedmachineAntarctica(X,Y,'bed');
     6%      surface   = interpBedmachineAntarctica(X,Y,'surface');
     7%      thickness = interpBedmachineAntarctica(X,Y,'thickness');
     8%      mask      = interpBedmachineAntarctica(X,Y,'mask');
     9%      mask      = interpBedmachineAntarctica(X,Y,'mask','nearest','../Data/BedMachineAntarctica_2020-07-15_v02.nc');
     10%
     11%   - mask:   0 ocean, 1 land (ice free), 2 grounded ice, 3 floating ice
     12%   - source: 1 IBCSO/RTopo-2, 2 MC, 3 interpolation, 4 hydrostatic eq,
     13%             5 Streamline diffusion, 6 Gravity inversion
     14%   - optional 4th input argument: interpolation method.
     15%             Supported interpolation methos: 'linear','cubic','nearest'
     16%   - optional 5th input argument: path to dataset.
     17%
     18% Version 11/30/2018 Mathieu Morlighem mmorligh@uci.edu
    219
    320if nargin<3, string = 'bed'; end
    421if nargin<4
    522        if strcmp(string,'mask') | strcmp(string,'source')
    6       method='nearest'; % default method
    7    else
    8       method='cubic'; % default method
    9    end
     23                method='nearest'; % default method
     24        else
     25                method='cubic'; % default method
     26        end
    1027end
    1128if nargin<5
    1229        ncdate='2020-07-15'; %BedMachine v2
    1330end
    14 
    1531basename = 'BedMachineAntarctica';
    1632
    17 %read data
    18 switch (oshostname()),
    19         case {'ronne'}
    20                 nc=['/home/ModelData/Antarctica/BedMachine/' basename '-' ncdate '.nc'];
    21         case {'thwaites','murdo','astrid'}
    22                 nc=['/u/astrid-r1b/ModelData/BedMachine/' basename '-' ncdate '.nc'];
    23         otherwise
    24                 if nargin==5
    25                         nc = ncdate;
    26                         disp(['Using provided path: ' nc]);
    27                 else
    28                         error('hostname not supported and path not provided');
     33if nargin==5
     34        ncfile = ncdate;
     35else
     36        %List of common paths to try
     37        paths = {...
     38                ['/u/astrid-r1b/ModelData/BedMachine/' basename '-' ncdate '.nc'],...
     39                ['/home/ModelData/Greenland/BedMachine/' basename '-' ncdate '.nc'],...
     40                ['/Users/larour/ModelData/BedMachine/' basename '-' ncdate '.nc'],...
     41                ['./' basename '-' ncdate '.nc'],...
     42                };
     43
     44        found = 0;
     45        for i=1:numel(paths)
     46                if exist(paths{i},'file')
     47                        ncfile = paths{i};
     48                        found = 1;
     49                        break;
    2950                end
    30 end
     51        end
    3152
    32 if nargout==2,
    33         string = 'bed';
     53        if ~found
     54                error(['Could not find ' basename '-' ncdate '.nc, you can add the path to the list or provide its path as a 5th argument']);
     55        end
    3456end
    3557
    3658disp(['   -- BedMachine Antarctica version: ' ncdate]);
    37 xdata = double(ncread(nc,'x'));
    38 ydata = double(ncread(nc,'y'));
     59xdata = double(ncread(ncfile,'x'));
     60ydata = double(ncread(ncfile,'y'));
    3961
    4062offset=2;
     
    4264xmin=min(X(:)); xmax=max(X(:));
    4365posx=find(xdata<=xmax);
     66if isempty(posx), posx=numel(xdata); end
    4467id1x=max(1,find(xdata>=xmin,1)-offset);
    4568id2x=min(numel(xdata),posx(end)+offset);
     
    4770ymin=min(Y(:)); ymax=max(Y(:));
    4871posy=find(ydata>=ymin);
     72if isempty(posy), posy=numel(ydata); end
    4973id1y=max(1,find(ydata<=ymax,1)-offset);
    5074id2y=min(numel(ydata),posy(end)+offset);
     
    5276if strcmp(string,'icemask'),
    5377        disp(['   -- BedMachine Antarctica: loading ' string]);
    54         %data  = double(ncread(nc,'mask'))';
    55         data  = double(ncread(nc,'mask',[id1x id1y],[id2x-id1x+1 id2y-id1y+1],[1 1]))';
     78        %data  = double(ncread(ncfile,'mask'))';
     79        data  = double(ncread(ncfile,'mask',[id1x id1y],[id2x-id1x+1 id2y-id1y+1],[1 1]))';
    5680        xdata=xdata(id1x:id2x);
    5781        ydata=ydata(id1y:id2y);
     
    6084else
    6185        disp(['   -- BedMachine Antarctica: loading ' string]);
    62         %data  = double(ncread(nc,string))';
    63         data  = double(ncread(nc,string,[id1x id1y],[id2x-id1x+1 id2y-id1y+1],[1 1]))';
     86        %data  = double(ncread(ncfile,string))';
     87        data  = double(ncread(ncfile,string,[id1x id1y],[id2x-id1x+1 id2y-id1y+1],[1 1]))';
    6488        xdata=xdata(id1x:id2x);
    6589        ydata=ydata(id1y:id2y);
     
    7094if strcmp(string,'mask') | strcmp(string,'source'),
    7195        %Need nearest neighbor to avoid interpolation between 0 and 2
    72         tic
    7396        output = InterpFromGrid(xdata,ydata,data,double(X),double(Y),'nearest');
    74         toc
    7597        %tic
    7698        %output = FastInterp(xdata,ydata,data,X,Y,'nearest');
  • issm/trunk-jpl/src/m/contrib/morlighem/modeldata/interpBedmachineGreenland.m

    r25794 r26042  
    11function output = interpBedmachineGreenland(X,Y,string,ncdate),
     2%INTERPBEDMACHINEGREENLAND - interpolate BedMachine data onto X and Y
     3%
     4%   Examples:
     5%      bed       = interpBedmachineGreenland(X,Y,'bed');
     6%      surface   = interpBedmachineGreenland(X,Y,'surface');
     7%      thickness = interpBedmachineGreenland(X,Y,'thickness');
     8%      mask      = interpBedmachineGreenland(X,Y,'mask');
     9%      mask      = interpBedmachineGreenland(X,Y,'mask','nearest','../Data/BedMachineGreenland_2020-07-15_v03.nc');
     10%
     11%   - mask:   0 ocean, 1 land (ice free), 2 grounded ice, 3 floating ice
     12%   - source: 1 IBCSO/RTopo-2, 2 MC, 3 interpolation, 4 hydrostatic eq,
     13%             5 Streamline diffusion, 6 Gravity inversion
     14%   - optional 4th input argument: interpolation method.
     15%             Supported interpolation methos: 'linear','cubic','nearest'
     16%   - optional 5th input argument: path to dataset.
     17%
     18% Version 11/30/2018 Mathieu Morlighem mmorligh@uci.edu
    219
    320if nargin<3, string = 'bed'; end
    4 if nargin<4,
     21if nargin<4
     22        if strcmp(string,'mask') | strcmp(string,'source')
     23                method='nearest'; % default method
     24        else
     25                method='cubic'; % default method
     26        end
     27end
     28if nargin<5
    529        %ncdate='2015-04-27'; %BedMachine v2
    630        ncdate='2017-09-25'; %BedMachine v3
    731        ncdate='2020-04-14';
    832end
    9 
    1033basename = 'BedMachineGreenland';
    1134
    12 %List of common paths to try
    13 paths = {...
    14         ['/u/astrid-r1b/ModelData/ModelData/MCdataset-' ncdate '.nc'],...
    15         ['/home/ModelData/Greenland/BedMachine/' basename '-' ncdate '.nc'],...
    16         ['/Users/larour/ModelData/BedMachine/' basename '-' ncdate '.nc'],...
    17         ['./' basename '-' ncdate '.nc'],...
    18         };
     35if nargin==5
     36        ncfile = ncdate;
     37else
     38        %List of common paths to try
     39        paths = {...
     40                ['/u/astrid-r1b/ModelData/ModelData/MCdataset-' ncdate '.nc'],...
     41                ['/home/ModelData/Greenland/BedMachine/' basename '-' ncdate '.nc'],...
     42                ['/Users/larour/ModelData/BedMachine/' basename '-' ncdate '.nc'],...
     43                ['./' basename '-' ncdate '.nc'],...
     44                };
    1945
    20 found = 0;
    21 for i=1:numel(paths)
    22         if exist(paths{i},'file')
    23                 ncfile = paths{i};
    24                 found = 1;
    25                 break;
     46        found = 0;
     47        for i=1:numel(paths)
     48                if exist(paths{i},'file')
     49                        ncfile = paths{i};
     50                        found = 1;
     51                        break;
     52                end
     53        end
     54
     55        if ~found
     56                error(['Could not find ' basename '-' ncdate '.nc, you can add the path to the list or provide its path as a 5th argument']);
    2657        end
    2758end
    28 
    29 if ~found
    30         error(['Could not find ' basename '-' ncdate '.nc, you can add the path to the list']);
    31 end
    32 
    3359
    3460disp(['   -- BedMachine Greenland version: ' ncdate]);
     
    5783
    5884disp(['   -- BedMachine Greenland: interpolating ' string]);
     85disp(['       -- Interpolation method: ' method]);
    5986if strcmp(string,'mask') | strcmp(string,'source'),
    6087        %Need nearest neighbor to avoid interpolation between 0 and 2
     
    6491end
    6592
    66 %TEST https://www.mathworks.com/matlabcentral/fileexchange/10772-fast-2-dimensional-interpolation
     93end
     94function zi = FastInterp(x,y,data,xi,yi,method)
     95
     96        %get data size
     97        [M N] = size(data);
     98
     99        % Get X and Y library array spacing
     100        ndx = 1/(x(2)-x(1));    ndy = 1/(y(2)-y(1));
     101        % Begin mapping xi and yi vectors onto index space by subtracting library
     102        % array minima and scaling to index spacing
     103
     104        xi = (xi - x(1))*ndx;       yi = (yi - y(1))*ndy;
     105
     106        % Fill Zi with NaNs
     107        zi = NaN(size(xi));
     108
     109        if strcmpi(method,'nearest'),
     110                % Find the nearest point in index space
     111                rxi = round(xi)+1;  ryi = round(yi)+1;
     112                % Find points that are in X,Y range
     113                flag = rxi>0 & rxi<=N & ~isnan(rxi) & ryi>0 & ryi<=M & ~isnan(ryi);
     114                % Map subscripts to indices
     115                ind = ryi + M*(rxi-1);
     116                zi(flag) = data(ind(flag));
     117
     118        else %Bilinear
     119
     120                % Transform to unit square
     121                fxi = floor(xi)+1;  fyi = floor(yi)+1; % x_i and y_i
     122                dfxi = xi-fxi+1;    dfyi = yi-fyi+1;   % Location in unit square
     123
     124                % flagIn determines whether the requested location is inside of the data arrays
     125                flagIn = fxi>0 & fxi<N & ~isnan(fxi) & fyi>0 & fyi<M & ~isnan(fyi);
     126
     127                %Toss all out-of-bounds variables now to save time
     128                fxi  = fxi(flagIn);  fyi  = fyi(flagIn);
     129                dfxi = dfxi(flagIn); dfyi = dfyi(flagIn);
     130
     131                %Find bounding vertices
     132                ind1 = fyi + M*(fxi-1);     % indices of (  x_i  ,  y_i  )
     133                ind2 = fyi + M*fxi;         % indices of ( x_i+1 ,  y_i  )
     134                ind3 = fyi + 1 + M*fxi;     % indices of ( x_i+1 , y_i+1 )
     135                ind4 = fyi + 1 + M*(fxi-1); % indices of (  x_i  , y_i+1 )
     136
     137                % Bilinear interpolation
     138                zi(flagIn) = ...
     139                        data(ind1).*(1-dfxi).*(1-dfyi) + ...
     140                        data(ind2).*dfxi.*(1-dfyi) + ...
     141                        data(ind4).*(1-dfxi).*dfyi + ...
     142                        data(ind3).*dfxi.*dfyi;
     143        end
     144end
Note: See TracChangeset for help on using the changeset viewer.