Skip to content

Commit

Permalink
Adding read_hrrr.m
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-dixon committed Nov 14, 2024
1 parent 083fa28 commit 884859d
Show file tree
Hide file tree
Showing 2 changed files with 270 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
quality='qc1';
freqData='10hz_combined';
qcVersion='v1.0';
whichModel='era5';
whichModel='hrrr'; % era5 or hrrr

infile=['~/git/HCR_configuration/projDir/qcDualPRTground/dataProcessing/scriptsFiles/iops_',project,'.txt'];

Expand All @@ -29,7 +29,7 @@
indir=HCRdir(project,quality,qcVersion,freqData);

%% Go through flights
for ii=3:size(caseList,1)
for ii=1:size(caseList,1)
disp(['IOP ',num2str(ii)]);

startTime=datetime(caseList(ii,1:6));
Expand Down Expand Up @@ -99,12 +99,44 @@
modelData=read_ecmwf(modeldir,data.time(1),data.time(end),getSST);
elseif strcmp(whichModel,'narr')
modelData=read_narr(modeldir,data.time(1),data.time(end),getSST);
elseif strcmp(whichModel,'hrrr')
modelData=read_hrrr(modeldir,data.time(1),data.time(end),getSST);
end

%% Trimm model data
hcrLat=unique(data.latitude);
hcrLon=unique(data.longitude);

[r,c]=find(modelData.lon>hcrLon-1 & modelData.lon<hcrLon+1 & ...
modelData.lat>hcrLat-1 & modelData.lat<hcrLat+1);

cInds=[min(c),max(c)];
rInds=[min(r),max(r)];

if strcmp(whichModel,'hrrr')
modelOrig=modelData;
modelData=[];
modelData.lon=modelOrig.lon(rInds(1):rInds(2),cInds(1):cInds(2));
modelData.lat=modelOrig.lat(rInds(1):rInds(2),cInds(1):cInds(2));
modelData.Temperature=modelOrig.Temperature(rInds(1):rInds(2),cInds(1):cInds(2),:,:);
modelData.rh=modelOrig.rh(rInds(1):rInds(2),cInds(1):cInds(2),:,:);
modelData.z=modelOrig.z(rInds(1):rInds(2),cInds(1):cInds(2),:,:);
modelData.u=modelOrig.u(rInds(1):rInds(2),cInds(1):cInds(2),:,:);
modelData.v=modelOrig.v(rInds(1):rInds(2),cInds(1):cInds(2),:,:);
modelData.p=modelOrig.p(rInds(1):rInds(2),cInds(1):cInds(2),:,:);
modelData.pSurf=modelOrig.pSurf(rInds(1):rInds(2),cInds(1):cInds(2),:);
modelData.tSurf=modelOrig.tSurf(rInds(1):rInds(2),cInds(1):cInds(2),:);
modelData.rhSurf=modelOrig.rhSurf(rInds(1):rInds(2),cInds(1):cInds(2),:);
modelData.uSurf=modelOrig.uSurf(rInds(1):rInds(2),cInds(1):cInds(2),:);
modelData.vSurf=modelOrig.vSurf(rInds(1):rInds(2),cInds(1):cInds(2),:);
modelData.time=modelOrig.time;
end

%% Set up grid
if strcmp(whichModel,'narr')
if strcmp(whichModel,'narr') | strcmp(whichModel,'hrrr')
lonMat=double(repmat(modelData.lon,1,1,size(modelData.z,4)));
latMat=double(repmat(modelData.lat,1,1,size(modelData.z,4)));
lonMat=wrapTo360(lonMat);
else
lonMat=double(repmat(modelData.lon,1,size(modelData.z,2),size(modelData.z,4)));
latMat=double(repmat(fliplr(modelData.lat'),size(modelData.z,1),1,size(modelData.z,4)));
Expand Down Expand Up @@ -232,7 +264,45 @@
surfData.sstHCR=nan(length(data.time),1);
surfData.sstHCR(goodIndXQ)=Vq;
end
elseif strcmp(whichModel,'hrrr')
for jj=1:size(modelData.z,3)
thisVar=squeeze(modelData.Temperature(:,:,jj,:));
F=scatteredInterpolant(lonMat(:),latMat(:),timeMat(:),thisVar(:));
Vq =F(data.longitude(timeInd),data.latitude(timeInd),datenum(data.time(timeInd)));
int.tempHCR=cat(1,int.tempHCR,Vq);
thisVar=squeeze(modelData.z(:,:,jj,:));
F=scatteredInterpolant(lonMat(:),latMat(:),timeMat(:),thisVar(:));
Vq =F(data.longitude(timeInd),data.latitude(timeInd),datenum(data.time(timeInd)));
int.zHCR=cat(1,int.zHCR,Vq);
thisVar=squeeze(modelData.p(:,:,jj,:));
F=scatteredInterpolant(lonMat(:),latMat(:),timeMat(:),thisVar(:));
Vq =F(data.longitude(timeInd),data.latitude(timeInd),datenum(data.time(timeInd)));
int.pHCR=cat(1,int.pHCR,Vq);
thisVar=squeeze(modelData.rh(:,:,jj,:));
F=scatteredInterpolant(lonMat(:),latMat(:),timeMat(:),thisVar(:));
Vq =F(data.longitude(timeInd),data.latitude(timeInd),datenum(data.time(timeInd)));
int.rhHCR=cat(1,int.rhHCR,Vq);
thisVar=squeeze(modelData.u(:,:,jj,:));
F=scatteredInterpolant(lonMat(:),latMat(:),timeMat(:),thisVar(:));
Vq =F(data.longitude(timeInd),data.latitude(timeInd),datenum(data.time(timeInd)));
int.uHCR=cat(1,int.uHCR,Vq);
thisVar=squeeze(modelData.v(:,:,jj,:));
F=scatteredInterpolant(lonMat(:),latMat(:),timeMat(:),thisVar(:));
Vq =F(data.longitude(timeInd),data.latitude(timeInd),datenum(data.time(timeInd)));
int.vHCR=cat(1,int.vHCR,Vq);
end

% 2D variables
F=scatteredInterpolant(lonMat(:),latMat(:),timeMat(:),modelData.pSurf(:));
surfData.pHCR=F(data.longitude,data.latitude,datenum(data.time));
F=scatteredInterpolant(lonMat(:),latMat(:),timeMat(:),modelData.tSurf(:));
surfData.tempHCR=F(data.longitude,data.latitude,datenum(data.time));
F=scatteredInterpolant(lonMat(:),latMat(:),timeMat(:),modelData.rhSurf(:));
surfData.rhHCR=F(data.longitude,data.latitude,datenum(data.time));
F=scatteredInterpolant(lonMat(:),latMat(:),timeMat(:),modelData.uSurf(:));
surfData.uHCR=F(data.longitude,data.latitude,datenum(data.time));
F=scatteredInterpolant(lonMat(:),latMat(:),timeMat(:),modelData.vSurf(:));
surfData.vHCR=F(data.longitude,data.latitude,datenum(data.time));
else
for jj=1:size(modelData.z,3)
Vq = interpn(lonMat,latMat,timeMat,squeeze(modelData.Temperature(:,:,jj,:)),...
Expand Down Expand Up @@ -273,7 +343,8 @@
end

% Interpolate topo
surfData.zHCR=data.altitude;
surfData.zHCR=nan(size(surfData.tempHCR));
surfData.zHCR(:)=unique(data.altitude);

if size(surfData.zHCR,1)==1
surfData.zHCR=surfData.zHCR';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
function hrrrData = read_hrrr(indir,startTime,endTime,SSTyes)
% Find the right time span for era5 data and read them in

refTime=datetime(1900,1,1,0,0,0);

%Initialize output
hrrrData=[];

% % Get intime hours
% inhoursAll=datetime(year(intime),month(intime),day(intime),hour(intime),0,0);
% inhoursTemp=unique(inhoursAll);
% inhoursLast=inhoursTemp(end)+hours(1);
%
% ll=1;
% inhours=inhoursTemp(1);
% while inhours(end)~=inhoursLast
% inhours=[inhours inhoursTemp(1)+hours(ll)];
% ll=ll+1;
% end

startHour=datetime(year(startTime),month(startTime),day(startTime),hour(startTime),0,0);
endHour=datetime(year(endTime),month(endTime),day(endTime),hour(endTime)+1,0,0);

inhours=startHour:hours(1):endHour;

pHours=[];
tHours=[];
rhHours=[];
zHours=[];
uHours=[];
vHours=[];
pSurf=[];
tSurf=[];
rhSurf=[];
zSurf=[];
uSurf=[];
vSurf=[];

lonlatFile=dir([indir,'netcdf/*.nc']);
lon=ncread([lonlatFile.folder,'/',lonlatFile.name],'gridlon_0');
hrrrData.lon=flipud(lon');
lat=ncread([lonlatFile.folder,'/',lonlatFile.name],'gridlat_0');
hrrrData.lat=flipud(lat');

for kk=1:length(inhours) %Loop through all hours
%% Pressure level data
% Find ecmwf files
roundTime=inhours(kk);
hourStr=datestr(roundTime,'HH');
dayStr=datestr(roundTime,'yyyymmdd');

hourFile=dir([indir,'/',dayStr,'/hrrr.t',hourStr,'z.wrfprsf00.grib2']);
hourFile=[hourFile.folder,'/',hourFile.name];

info=georasterinfo(hourFile);
elem=info.Metadata.Element;
descr=info.Metadata.Description;

pressLevs=find(contains(descr,'ISBL'));
m0levs=find(contains(descr,'0[-] SFC'));
m2levs=find(contains(descr,'2[m] HTGL'));
m10levs=find(contains(descr,'10[m] HTGL'));

allT=find(elem=='TMP');
bandTpress=ismember(allT,pressLevs);
bandTpress=allT(bandTpress==1);
bandTsurf=ismember(allT,m2levs);
bandTsurf=allT(bandTsurf==1);

allRH=find(elem=='RH');
bandRHpress=ismember(allRH,pressLevs);
bandRHpress=allRH(bandRHpress==1);
bandRHsurf=ismember(allRH,m2levs);
bandRHsurf=allRH(bandRHsurf==1);

allZ=find(elem=='HGT');
bandZpress=ismember(allZ,pressLevs);
bandZpress=allZ(bandZpress==1);
% bandZsurf=ismember(allZ,m2levs);
% bandZsurf=allZ(bandZsurf==1);

allU=find(elem=='UGRD');
bandUpress=ismember(allU,pressLevs);
bandUpress=allU(bandUpress==1);
bandUsurf=ismember(allU,m10levs);
bandUsurf=allU(bandUsurf==1);

allV=find(elem=='VGRD');
bandVpress=ismember(allV,pressLevs);
bandVpress=allV(bandVpress==1);
bandVsurf=ismember(allV,m10levs);
bandVsurf=allV(bandVsurf==1);

allP=find(elem=='PRES');
bandPsurf=ismember(allP,m0levs);
bandPsurf=allP(bandPsurf==1);

tIn=readgeoraster(hourFile,Bands=bandTpress);
rhIn=readgeoraster(hourFile,Bands=bandRHpress);
zIn=readgeoraster(hourFile,Bands=bandZpress);
uIn=readgeoraster(hourFile,Bands=bandUpress);
vIn=readgeoraster(hourFile,Bands=bandVpress);

zIn=zIn./9.806;

tps=descr(bandTpress);
rhps=descr(bandRHpress);
zps=descr(bandZpress);
ups=descr(bandUpress);
vps=descr(bandVpress);

tp=nan(size(tps));
zp=nan(size(zps));
rhp=nan(size(rhps));
up=nan(size(ups));
vp=nan(size(vps));
for ll=1:length(tp)
str=char(tps(ll));
pa=strfind(str,'[Pa]');
tp(ll)=str2num(str(1:pa-1));

str=char(rhps(ll));
pa=strfind(str,'[Pa]');
rhp(ll)=str2num(str(1:pa-1));

str=char(zps(ll));
pa=strfind(str,'[Pa]');
zp(ll)=str2num(str(1:pa-1));

str=char(ups(ll));
pa=strfind(str,'[Pa]');
up(ll)=str2num(str(1:pa-1));

str=char(vps(ll));
pa=strfind(str,'[Pa]');
vp(ll)=str2num(str(1:pa-1));
end

[pCol,it]=sort(tp);
t=tIn(:,:,it);

[~,irh]=sort(rhp);
rh=rhIn(:,:,irh);

[~,iz]=sort(zp);
z=zIn(:,:,iz);

[~,iu]=sort(up);
u=uIn(:,:,iu);

[~,iv]=sort(vp);
v=vIn(:,:,iv);

p=repmat(pCol,1,size(t,1),size(t,2));
p=permute(p,[2,3,1]);

pHours=cat(4,pHours,p);
tHours=cat(4,tHours,t);
rhHours=cat(4,rhHours,rh);
zHours=cat(4,zHours,z);
uHours=cat(4,uHours,u);
vHours=cat(4,vHours,v);

%% Surface data
tS=readgeoraster(hourFile,Bands=bandTsurf);
rhS=readgeoraster(hourFile,Bands=bandRHsurf);
uS=readgeoraster(hourFile,Bands=bandUsurf);
vS=readgeoraster(hourFile,Bands=bandVsurf);
pS=readgeoraster(hourFile,Bands=bandPsurf);

pSurf=cat(3,pSurf,pS);
tSurf=cat(3,tSurf,tS);
rhSurf=cat(3,rhSurf,rhS);
uSurf=cat(3,uSurf,uS);
vSurf=cat(3,vSurf,vS);

%% SST
if SSTyes
warning('No SST data available.')
end
end

hrrrData.Temperature=tHours;
hrrrData.rh=rhHours;
hrrrData.z=zHours;
hrrrData.u=uHours;
hrrrData.v=vHours;
hrrrData.p=pHours;
hrrrData.pSurf=pSurf;
hrrrData.tSurf=tSurf;
hrrrData.rhSurf=rhSurf;
hrrrData.uSurf=uSurf;
hrrrData.vSurf=vSurf;
hrrrData.time=inhours;
end

0 comments on commit 884859d

Please sign in to comment.