Mercurial > forge
changeset 11783:cb452966068b octave-forge
optimization: reduce the number of calls to ncinfo
author | abarth93 |
---|---|
date | Wed, 12 Jun 2013 14:28:22 +0000 |
parents | d8604b26a254 |
children | b9165047d2b4 |
files | extra/ncArray/inst/ncCatArray.m extra/ncArray/inst/nccoord.m |
diffstat | 2 files changed, 44 insertions(+), 17 deletions(-) [+] |
line wrap: on
line diff
--- a/extra/ncArray/inst/ncCatArray.m Wed Jun 12 14:25:49 2013 +0000 +++ b/extra/ncArray/inst/ncCatArray.m Wed Jun 12 14:28:22 2013 +0000 @@ -92,9 +92,21 @@ range = 1:length(filenames); end -var = arr(dim,filenames,varname,SameAttributes); +% get all file information +finfos = cell(length(filenames),1); -[dims,coord] = nccoord(cached_decompress(filenames{1}),varname); +if SameAttributes + % assume all files have the same ncinfo as the first one + finfos(:) = ncinfo(cached_decompress(filenames{1})); +else + for i=1:length(filenames) + finfos{i} = ncinfo(cached_decompress(filenames{i})); + end +end + +var = arr(dim,filenames,varname,finfos); + +[dims,coord] = nccoord(finfos{1},varname); % add new dimension to coord if arrays are contatenated over a new dimension % and if coord information already exist @@ -107,8 +119,18 @@ for i=1:length(coord) - % coordinates do also depend on the dimension only which we concatenate - coord(i).val = arr(dim,filenames,coord(i).name,SameAttributes); + % the number of the dimension might be different + % find in coord(i).dims the index of the dimension called dims{dim} + dimc = find(strcmp(coord(i).dims,dims{dim})); + + if isempty(dimc) + vinfo = varinfo(finfos{1},varname); + coord(i).val = ncBaseArray(filenames{1},coord(i).name,'vinfo',vinfo); + else + % coordinates do also depend on the dimension over which we concatenate + coord(i).val = arr(dimc,filenames,coord(i).name,finfos); + end + if dim > length(coord(i).dims) coord(i).dims{dim} = catdimname; end @@ -119,24 +141,21 @@ end -function CA = arr(dim,filenames,varname,SameAttributes) +function CA = arr(dim,filenames,varname,finfos) arrays = cell(1,length(filenames)); -if SameAttributes - % assume every filename has the same metadata - vinfo = ncinfo(cached_decompress(filenames{1}),varname); -else - vinfo = []; -end - for i=1:length(filenames) - arrays{i} = ncBaseArray(filenames{i},varname,'vinfo',vinfo); + vinfo = varinfo(finfos{i},varname); + arrays{i} = ncBaseArray(filenames{i},varname,'vinfo',vinfo); end CA = CatArray(dim,arrays); end - +function vinfo = varinfo(fileinfo,varname) + index = find(strcmp({fileinfo.Variables(:).Name},varname)); + vinfo = fileinfo.Variables(index); +end % Copyright (C) 2012,2013 Alexander Barth <barth.alexander@gmail.com>
--- a/extra/ncArray/inst/nccoord.m Wed Jun 12 14:25:49 2013 +0000 +++ b/extra/ncArray/inst/nccoord.m Wed Jun 12 14:28:22 2013 +0000 @@ -1,9 +1,10 @@ % Coordinates of a NetCDF variable. % -% coord = nccoord(filename,varname) +% [dims,coord] = nccoord(filename,varname) % get coordinates of the variable varname in the % netcdf file called filename. The netcdf is assumed to % follow the CF convention. +% dims is a cell-array of the dimensions of varname % coord is an array of structures with the field 'name' % for the variable name and 'dims' with a cell-array of the % netcdf dimensions. @@ -15,8 +16,15 @@ function [dims,coord] = nccoord(filename,varname) -finfo = ncinfo(filename); -vinfo = ncinfo(filename,varname); +if ischar(filename) + finfo = ncinfo(filename); +else + finfo = filename; +end + +% get variable info +index = find(strcmp({finfo.Variables(:).Name},varname)); +vinfo = finfo.Variables(index); % determine coordinates % using CF convention