Creating NetCDF file with new variables

12 vues (au cours des 30 derniers jours)
Felipe Torquato
Felipe Torquato le 16 Juin 2021
Jello MatLab users,
I have a NetCDF file whose resolution is 9 km (1/12˚).
(Dimensions = 489 x 376 x 20)
Variables: 'Longitude', 'Latitude', 'Depth', 'Time', 'zu', 'zv' and 'zw'
Now I created new a new mesh that is 2 km.
(Dimensions = 1501 x 1953 x 20)
Variables: 'lon', 'lat, 'Depth', 'Time', 'zuc', 'zvc' and 'zwc'
QUESTION
How can I create a NEW.nc with 'lon', 'lat', 'Depth', 'Time', 'zuc', 'zvc' and 'zwc'?
SCRIPT
filename='mydata.nc';
ncdisp(filename);
% Load in variables
Time = ncread(filename,'Time');
Longitude=ncread(filename,'Longitude');
Latitude=ncread(filename,'Latitude');
Depth=ncread(filename,'Depth');
zu=ncread(filename,'zu');
zv=ncread(filename,'zv');
zw=ncread(filename,'zw');
% Permute matrices to be interpolated and create a mesh
[lo, la, dep]=meshgrid(Longitude,Latitude,Depth);
Au=permute(zu,[2 1 3]);
Av=permute(zv,[2 1 3]);
Aw=permute(zw,[2 1 3]);
% Define new resolution @ 0.02
lon=Longitude(1):.02:Longitude(end); lon=lon';
lat=Latitude(1):.02:Latitude(end); lat=lat';
[X, Y, Z]=meshgrid(lon,lat,Depth); % New mesh
zuc = interp3(lo,la,dep,Au,X,Y,Z); zuc=permute(zuc,[2 1 3]);
zvc = interp3(lo,la,dep,Av,X,Y,Z); zvc=permute(zvc,[2 1 3]);
zwc = interp3(lo,la,dep,Aw,X,Y,Z); zwc=permute(zwc,[2 1 3]);

Réponses (1)

SALAH ALRABEEI
SALAH ALRABEEI le 16 Juin 2021
It depends on your data structure, ( NEMO data, projected coordinates, data gird). Anyway, here is a simple example where ur lon and lat are just arrays not matrices, your main variable temp is 4D ( lon,lat,depth, time). You can play with the code to fit it to your data structure.
%% read the older file
file = 'global-reanalysis-phy_2012_Jan_Apr_fill_domain_SST_currents_dep.nc';
lon = ncread(file,'longitude');
lat = ncread(file,'latitude');
time = ncread(file,'time');
depth = ncread(file,'depth');
temp = ncread(file,'thetao');
%% create the new file
ncnc=netcdf.create(['new','.nc'] ,'NC_WRITE')
lonID=netcdf.defDim(ncnc,'lon', length(lon));
vlongID=netcdf.defVar(ncnc,'lon','double',lonID);
netcdf.putAtt(ncnc,vlongID,'units','degrees');
netcdf.putAtt(ncnc,vlongID,'axis','long');
depID=netcdf.defDim(ncnc,'depth',length(depth));
vdepID=netcdf.defVar(ncnc,'depth','double',depID);
netcdf.putAtt(ncnc,vdepID,'units','m');
latID=netcdf.defDim(ncnc,'lat', length(lat));
vlatID=netcdf.defVar(ncnc,'lat','double',latID);
netcdf.putAtt(ncnc,vlatID,'units','degrees');
netcdf.putAtt(ncnc,vlatID,'axis','lat');
tID=netcdf.defDim(ncnc,'time',length(time));
vtID=netcdf.defVar(ncnc,'time','double',tID);
netcdf.putAtt(ncnc,vtID,'units','days since 1950-1-1');
veddyID=netcdf.defVar(ncnc,'temp','double',[lonID,latID,depID, tID]);
netcdf.putAtt(ncnc,veddyID,'units','C');
netcdf.endDef(ncnc)
% start put data
netcdf.putVar(ncnc,vdepID,depth);
netcdf.putVar(ncnc,vtID,time);
netcdf.putVar(ncnc,vlatID,lat);
netcdf.putVar(ncnc,vlongID,lon);
netcdf.putVar(ncnc,veddyID,temp);
netcdf.close(ncnc)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by