Skip to content

Instantly share code, notes, and snippets.

@gaelforget
Created April 2, 2018 19:27
Show Gist options
  • Save gaelforget/3fe88971ad978ea614ef0cadbdb4f311 to your computer and use it in GitHub Desktop.
Save gaelforget/3fe88971ad978ea614ef0cadbdb4f311 to your computer and use it in GitHub Desktop.
Extract the lat-lon part of ecco v4 grid variables (see doi.org/10.5194/gmd-8-3071-2015)
function []=grid_load_lonlat();
global mygrid;
mygrid=[];
do_gcmfaces=~isempty(which('gcmfaces'));
%defining the grid
mygrid.dirGrid='nctiles_grid/';
if do_gcmfaces;
mygrid.fileFormat='straight';
mygrid.nFaces=1;
mygrid.gcm2facesFast=0;
mygrid.memoryLimit=0;
mygrid.ioSize=[360 178];
mygrid.facesSize=[[360 178]];
mygrid.facesExpand=[];
mygrid.missVal=0;
end;
%load the grid variables
fldLoc=[mygrid.dirGrid 'GRID'];
mateLoc=fldLoc;
fil=[fldLoc '.0001.nc'];
list0={'XC','XG','YC','YG','RAC','RAZ','hFacC','hFacS','hFacW','Depth','AngleCS','AngleSN','RAC','RAZ'};
for ii=1:length(list0);
tmp1=eccov4_lonlat(fldLoc,list0{ii},[]);
if do_gcmfaces; tmp1=convert2gcmfaces(tmp1); end;
mygrid=setfield(mygrid,list0{ii},tmp1);
end;
list0={'DXC','DXG','DYC','DYG'};
[mygrid.DXC,mygrid.DYC]=eccov4_lonlat(fldLoc,'DXC',[],mateLoc,'DYC',1);
[mygrid.DYG,mygrid.DXG]=eccov4_lonlat(fldLoc,'DYG',[],mateLoc,'DXG',1);
if do_gcmfaces;
for ii=1:length(list0);
tmp1=convert2gcmfaces(getfield(mygrid,list0{ii}));
mygrid=setfield(mygrid,list0{ii},tmp1);
end;
end;
%list0={'mskC','mskW','mskS'};
mygrid.mskC=1*(mygrid.hFacC>0); mygrid.mskC(mygrid.mskC==0)=NaN;
mygrid.mskW=1*(mygrid.hFacW>0); mygrid.mskW(mygrid.mskW==0)=NaN;
mygrid.mskS=1*(mygrid.hFacS>0); mygrid.mskS(mygrid.mskS==0)=NaN;
list0={'RC','RF','DRC','DRF'};
for ii=1:length(list0);
tmp1=ncread(fil,list0{ii});
mygrid=setfield(mygrid,list0{ii},tmp1);
end;
%to allow convert2gcmfaces/doFast:
if do_gcmfaces;
tmp1=convert2gcmfaces(mygrid.XC);
tmp1(:)=[1:length(tmp1(:))];
nn=length(tmp1(:));
mygrid.gcm2faces=convert2gcmfaces(tmp1);
mygrid.faces2gcmSize=size(tmp1);
mygrid.faces2gcm=convert2gcmfaces(tmp1);
for iFace=1:mygrid.nFaces;
n=length(mygrid.gcm2faces{iFace}(:));
mygrid.faces2gcm{iFace}=mygrid.gcm2faces{iFace}(:);
mygrid.gcm2faces{iFace}=sparse([1:n],mygrid.gcm2faces{iFace}(:),ones(1,n),n,nn);
end;
mygrid.gcm2facesFast=true;
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment