有什么方法可以轻松地将.mat文件转换为.nc文件吗? [英] Is there any way to easily convert .mat file to .nc file?
问题描述
尝试将数据从 .mat
文件转换为 .nc
文件,因此我可以使用NCVIEW快速查看数据.
我要保存到 .nc
文件中的变量如下所示:
- 变量名称:
Watts_Map
- 变量描述:
在一组地理网格单元上绘制的功率
-
可变尺寸:
< 225x61x2920 double>
因此,这将是一个3-D netCDF文件.
每个时间步长的矩阵为 225
x 61
.有 2920
个时间步.每个网格单元包含一个瓦数值.所以有 2920
个维度的矩阵 225
x61
.
-
经度:
0W
至140W
,分辨率为0.625 deg
,由225
-
纬度:
0N
至30N
,0.5度
分辨率,由61
似乎没有一种直观的方法来执行此操作.尝试 nccreate
和 ncwrite
.
有关如何执行此操作的任何建议?
这是我正在尝试的代码:
%清除所有数据并清理工作区clc;清除所有;%加载要转换为netcdf文件的.mat文件%load('2000_ATL_Watts_Maps_inc_Land.mat');要从 .mat 文件中获取并保存为 netcdf 文件的 % 变量%Watts_Map = W/平方米%尺寸:225x61x2920(lon x lat x time)%% 换句话说,有 2920 张大小为 225x61 的地图,包含 W/sq m每个网格单元的%值%%分辨率:经度= 0.625度纬度百分比= 0.5度%时间=从每年的1月1日午夜开始,以增量为单位每3小时%.因此,时间步1是1月1日午夜,%timestep 2是1月1日凌晨3点,依此类推.%创建数据并将其写入netcdf文件nccreate('test_files.nc','Watts','Dimensions',{'time'2920'lon'225'lat'61});
希望这可以帮助您入门:
%%创建!rm test_files.ncnccreate('test_files.nc','Watts','Dimensions',{'time'2920'lon'225'lat'61});nccreate('test_files.nc','lat','Dimensions',{'lat'61}));nccreate('test_files.nc','lon','Dimensions',{'lon'225});nccreate('test_files.nc','time','Dimensions',{'time'2920});ncdisp('test_files.nc');%%写入尺寸%https://www.unidata.ucar.edu/software/netcdf/docs/netcdf/Dimensions.html纬度百分比:0N至30N,0.5度分辨率,用61表示ncwrite('test_files.nc','lat',0:.5:30);%经度:0W至140W,0.625度分辨率,用225表示ncwrite('test_files.nc','lon',0:.625:140);% 时间?%%写入数据ncwrite('test_files.nc','Watts',rand(2920,225,61));
Trying to convert data from a .mat
file into a .nc
file, so I can view the data quickly using NCVIEW.
The variable I want to save into a .nc
file looks like this:
- Variable name:
Watts_Map
- Variable description:
Wattages plotted over a set of geographic grid cells
Variable dimensions:
<225x61x2920 double>
So this would be a 3-D netCDF file.
The matrix is 225
x61
, for each timestep. There are 2920
timesteps. Each grid cell contains a wattage value. So there are 2920
matrices of dimensions 225
x61
.
Longitude:
0W
to140W
,0.625 deg
resolution, denoted by225
Latitude:
0N
to30N
,0.5 deg
resolution, denoted by61
There does not seem to be an intuitive way to do this. Trying nccreate
and ncwrite
.
Any advice on how to do this?
Here is the code I am trying:
% Clear out all data and clean up workspace
clc;
clear all;
% load the .mat file to be converted to netcdf file
%load('2000_ATL_Watts_Maps_inc_Land.mat');
% variable to be taken from .mat file and saved as netcdf file
% Watts_Map = W/sq m
% Dimensions: 225x61x2920 (lon x lat x time)
%
% Put another way, there are 2920 maps of size 225x61, containing W/sq m
% values for each grid cell
%
% Resolution: longitude = 0.625 deg
% latitude = 0.5 degree
% time = starts at midnight on Jan 1st of the year, increments
% every 3 hours. So timestep 1 is midnight on Jan 1st,
% timestep 2 is 3am on Jan 1st, and so on.
% Create and write data to netcdf file
nccreate('test_files.nc','Watts','Dimensions',{'time' 2920 'lon' 225 'lat' 61});
Hope this gets you started:
%% Create
!rm test_files.nc
nccreate('test_files.nc','Watts','Dimensions',{'time' 2920 'lon' 225 'lat' 61});
nccreate('test_files.nc','lat','Dimensions',{'lat' 61});
nccreate('test_files.nc','lon','Dimensions',{'lon' 225});
nccreate('test_files.nc','time','Dimensions',{'time' 2920});
ncdisp('test_files.nc');
%% write dimensions
% https://www.unidata.ucar.edu/software/netcdf/docs/netcdf/Dimensions.html
%Latitude: 0N to 30N, 0.5 deg resolution, denoted by 61
ncwrite('test_files.nc','lat',0:.5:30);
%Longitude: 0W to 140W, 0.625 deg resolution, denoted by 225
ncwrite('test_files.nc','lon',0:.625:140);
% Time?
%% write data
ncwrite('test_files.nc','Watts',rand(2920,225,61));
这篇关于有什么方法可以轻松地将.mat文件转换为.nc文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!