Home > atmlab > geographical > standardize_geodata.m

standardize_geodata

PURPOSE ^

STANDARDIZE_GEODATA sort and rearrange lat,lon, data to common format

SYNOPSIS ^

function [flags,lat,lon,data] = standardize_geodata(lat,lon,data)

DESCRIPTION ^

 STANDARDIZE_GEODATA sort and rearrange lat,lon, data to common format

                  For gridded data.
   
  PURPOSE  Makes sure lat lons are ascending, lons are in -180:180 regime, 
           and makes sure that data has the dimensions data(lat,lon)

  IN       lat      vector
           lon      vector
  OPT      data     matrix      dims = (lat,lon,...) or (lon,lat,...) 

  OUT
           flags     struct     A structure of flags to indicate which
                                issue the function addressed.
           lat       vector     Ascending lats
           lons      vector     Ascending lons and 0:360 -> -180:180
           data      matrix     data(lat,lon,....) and rearranged to new
                                lat/lons

 NOTE: 1) If lat and lon are the same length the first dimesion of data is
       assumed to correspond to lat.
       2) If data is a region that has lons in the 0:360 regime AND wraps
       over 360, this will cause an error. e.g. [350 360 10 20]

 USAGE:    [flags,lat,lon,data] = standardize_geodata(lat,lon,data)
           or
           [flags,lat,lon] = standardize_geodata(lat,lon)

 created by Salomon Eliasson
 $Id: homogenize_datalatlon.m 7163 2011-09-21 19:42:48Z seliasson $

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

standardize_geodata.m

SOURCE CODE ^

0001 function [flags,lat,lon,data] = standardize_geodata(lat,lon,data)
0002 % STANDARDIZE_GEODATA sort and rearrange lat,lon, data to common format
0003 %
0004 %                  For gridded data.
0005 %
0006 %  PURPOSE  Makes sure lat lons are ascending, lons are in -180:180 regime,
0007 %           and makes sure that data has the dimensions data(lat,lon)
0008 %
0009 %  IN       lat      vector
0010 %           lon      vector
0011 %  OPT      data     matrix      dims = (lat,lon,...) or (lon,lat,...)
0012 %
0013 %  OUT
0014 %           flags     struct     A structure of flags to indicate which
0015 %                                issue the function addressed.
0016 %           lat       vector     Ascending lats
0017 %           lons      vector     Ascending lons and 0:360 -> -180:180
0018 %           data      matrix     data(lat,lon,....) and rearranged to new
0019 %                                lat/lons
0020 %
0021 % NOTE: 1) If lat and lon are the same length the first dimesion of data is
0022 %       assumed to correspond to lat.
0023 %       2) If data is a region that has lons in the 0:360 regime AND wraps
0024 %       over 360, this will cause an error. e.g. [350 360 10 20]
0025 %
0026 % USAGE:    [flags,lat,lon,data] = standardize_geodata(lat,lon,data)
0027 %           or
0028 %           [flags,lat,lon] = standardize_geodata(lat,lon)
0029 %
0030 % created by Salomon Eliasson
0031 % $Id: homogenize_datalatlon.m 7163 2011-09-21 19:42:48Z seliasson $
0032 
0033 
0034 % -------    CHECK for errors
0035 errID = ['gmtlab:' mfilename ':badInput'];
0036 dexist = exist('data','var');
0037 
0038 assert(isvector(lat) && isvector(lon),errID,'lat and lons must be vectors')
0039 
0040 assert(all(lat>=-90 & lat<=90) && all(lon>=-180 & lon<=360),...
0041     errID,'Lats and lons must have physical values')
0042 
0043 if dexist
0044     sz = size(data);
0045     
0046     assert(isequal(prod(sz(1:2)),numel(lat)*numel(lon)),...
0047         errID,'data does not appear to be gridded')
0048 end
0049 
0050 assert(all(lon(2:end)<lon(1:end-1)) || all(lon(2:end)>lon(1:end-1)),...
0051     errID,'Longitudes must be ordered ascending or descending');
0052 
0053 assert(all(lat(2:end)<lat(1:end-1)) || all(lat(2:end)>lat(1:end-1)),...
0054     errID,'Latitudes must be ordered ascending or descending');
0055 
0056 % --------  SET FLAGS
0057 if dexist
0058     %undesired dimensions
0059     flags.permute = (length(lon)==sz(1) && length(lat)==sz(2)) && ...
0060         length(lat) ~= length(lon);
0061 end
0062 
0063 %undesired lon orientation
0064 flags.lon_descend = all(lon(2:end)<lon(1:end-1)); 
0065 
0066 %undesired latorientation
0067 flags.lat_descend = all(lat(2:end)<lat(1:end-1));
0068 
0069 flags.lon360 = any(lon > 180);
0070 
0071 flags.duplicate = flags.lon360 & all(ismember(lon,[0,360]));
0072 
0073 % --------  HOMOGENIZE the input
0074 
0075 % PERMUTE data so that dimensions are data(lat,lon)
0076 if dexist
0077     if flags.permute
0078         x = 1:ndims(data);
0079         x(1)=2;x(2)=1;
0080         data = permute(data,x);
0081     end
0082 end
0083 
0084 % If any of the following flags are true the data matrix will be flattened
0085 % on 3rd dimesion. A reshape at the end of this function will unflatten the
0086 % data again later
0087 
0088 % make LONS ASCENDING
0089 if flags.lon_descend
0090     lon = lon(end:-1:1);
0091     if dexist
0092         data = data(:,end:-1:1,:);
0093     end
0094 end
0095 
0096 % make LATS ASCEND
0097 if flags.lat_descend
0098     lat = lat(end:-1:1);
0099     if dexist
0100         data = data(end:-1:1,:,:);
0101     end
0102 end
0103 
0104 % make sure LONGITUDES ARE IN -180:180 REGIME
0105 if flags.lon360
0106     lon = lon-(lon > 180)*360;
0107     [lon,lnindex] = sort(lon);
0108     if dexist
0109         data = data(:,lnindex,:);
0110     end
0111 end
0112 
0113 
0114 % DUPLICATE data
0115 % Need to remove duplicate data at zeros and instead introduce duplicate
0116 % data at lon = -180 and 180 if either -180 or 180 are amongst the lon
0117 % values (if so).
0118 
0119 % Since, by removing duplicate data, I change the size of the lon vector
0120 % and data matrix which is undesireable. By adding either -180 or 180 as
0121 % duplicates the sizes remain the same. The caveat is that if your original lons
0122 % contain both 0 and 360 (duplicate), but don't contain the longitude: 180,
0123 % the lon vector and data matrix will be shortened.
0124 
0125 if flags.duplicate
0126     [sv,index] = unique(lon);
0127     lon = lon(index);
0128     if dexist
0129         data = data(:,index,:);
0130     end
0131     if lon(end)==180
0132         lon = [-180 lon];
0133         if dexist
0134             data = [data(:,end,:) data];
0135         end
0136     else
0137         warning(sprintf('atmlab:%s:ChangedDataDimensions',mfilename),...
0138             'Duplicate lons removed changing the size of the lon vector (and data)')
0139     end
0140 end
0141 
0142 if dexist
0143     data = reshape(data,[size(data,1),size(data,2),sz(3:end)]);
0144 end

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