有没有办法轻松地将.mat文件转换为.nc文件?

时间:2014-11-19 19:15:39

标签: matlab netcdf

尝试将.mat文件中的数据转换为.nc文件,以便我可以使用NCVIEW快速查看数据。

我要保存到.nc文件中的变量如下所示:

  • 变量名称:Watts_Map
  • 变量描述:Wattages plotted over a set of geographic grid cells
  • 可变尺寸:<225x61x2920 double>

    所以这将是一个3-D netCDF文件。

对于每个时间步,矩阵为225 x 61。有2920个时间步长。每个网格单元包含一个瓦特值。因此,维度为2920 x 225的矩阵61

  • 经度:0W140W0.625 deg分辨率,由225

  • 表示
  • 纬度:0N30N0.5 deg分辨率,由61

  • 表示

似乎没有直观的方法来做到这一点。试用nccreatencwrite

有关如何执行此操作的建议吗?


以下是我正在尝试的代码:

% 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});

1 个答案:

答案 0 :(得分:1)

希望这能让你开始:

%% 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));