Kevin_Q
Kevin_Q

Reputation: 73

Is there any way to easily convert .mat file to .nc file?

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:

The matrix is 225x61, for each timestep. There are 2920 timesteps. Each grid cell contains a wattage value. So there are 2920 matrices of dimensions 225x61.

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

Upvotes: 2

Views: 5694

Answers (1)

Ashish Uthama
Ashish Uthama

Reputation: 1331

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

Upvotes: 1

Related Questions