How to process NC file in matlab
59 views (last 30 days)
Show older comments
Muhammad Usman Saleem
on 11 Dec 2021
Commented: Walter Roberson
on 6 Jan 2022
I've monthly soil monisture NC file. This file contains monthly soil monisture data since 1948. I want to extract soil data within my shapefile in text file (Shapefile has also attached). I google alot to proccess NetCDF file in matlab find not solution, The ncdisp of my nc file is given below:
Format:
netcdf4_classic
Global Attributes:
_NCProperties = 'version=1|netcdflibversion=4.4.1.1|hdf5libversion=1.10.1'
Conventions = 'CF-1.0'
title = 'CPC Soil Moisture'
institution = 'NOAA/ESRL PSD'
dataset_title = 'CPC Soil Moisture'
history = 'Wed Oct 18 15:13:37 2017: ncks -d time,,-2 soilw.mon.mean.x.nc soilw.mon.mean.xx.nc
Wed Oct 18 15:12:08 2017: ncks -d time,,-3 soilw.mon.mean.nc soilw.mon.mean.x.nc
CPC Soil Moisture
Obtained on Nov 2004 from CPC's website
and written to netCDF by Cathy Smith 12/2004.
he CPC Global monthly soil moisture dataset is a 1/2 degree resolution grid from 1948 to the present.
The file is written in COARDS and CF compliant netCDF at NOAA ESRL/PSD https://www.esrl.noaa.gov/psd/
Converted to chunked, deflated non-packed NetCDF4 Jul 2014'
NCO = '4.6.9'
References = 'https://www.psl.noaa.gov/data/gridded/data.cpcsoil.html'
Dimensions:
lat = 360
lon = 720
time = 886 (UNLIMITED)
Variables:
lat
Size: 360x1
Dimensions: lat
Datatype: single
Attributes:
long_name = 'Latitude'
units = 'degrees_north'
actual_range = [8.98e+01 -8.98e+01]
standard_name = 'latitude'
axis = 'Y'
coordinate_defines = 'point'
lon
Size: 720x1
Dimensions: lon
Datatype: single
Attributes:
long_name = 'Longitude'
units = 'degrees_east'
actual_range = [2.50e-01 3.60e+02]
standard_name = 'longitude'
axis = 'X'
coordinate_defines = 'point'
soilw
Size: 720x360x886
Dimensions: lon,lat,time
Datatype: single
Attributes:
long_name = 'Model-Calculated Monthly Mean Soil Moisture'
missing_value = -9.97e+36
units = 'mm'
valid_range = [0.00e+00 1.00e+03]
dataset = 'CPC Monthly Soil Moisture'
var_desc = 'Soil Moisture'
level_desc = 'Surface'
statistic = 'Monthly Mean'
parent_stat = 'Other'
standard_name = 'lwe_thickness_of_soil_moisture_content'
cell_methods = 'time: mean (monthly from values)'
actual_range = [0.00e+00 1.00e+30]
time
Size: 886x1
Dimensions: time
Datatype: double
Attributes:
long_name = 'Time'
units = 'days since 1800-01-01 00:00:0.0'
delta_t = '0000-01-00 00:00:00'
avg_period = '0000-01-00 00:00:00'
standard_name = 'time'
axis = 'T'
bounds = 'time_bnds'
coordinate_defines = 'start'
prev_avg_period = '0000-00-01 00:00:00'
actual_range = [5.41e+04 8.10e+04]
I've tied
file='data.nc';
ncdisp(file)
long = ncread(file,'lon');
latt = ncread(file,'lat');
time = ncread(file,'time');
I want output of Soilw in this format in text file
Date Soilw
01/01/2015 10
01/02/2015 20
01/03/2015 0.5
Please help me to convert monthly netcdf datainto text file using matlab please?
7 Comments
Accepted Answer
Walter Roberson
on 12 Dec 2021
file = 'data.nc';
shapefile = 'lahore.shp';
long = ncread(file, 'lon');
latt = ncread(file, 'lat');
time = ncread(file, 'time');
soilw = ncread(file, 'soilw');
S = shaperead(shapefile);
%ncread sometimes returns the transpose of what we expect
if size(soilw, 1) ~= length(lat)
soilw = permute(soilw, [2 1 3]);
end
Ntime = length(time);
Date = datetime('1800-01-01 00:00:00') + days(time(:));
mask = inpolygon(lat, lon, S.Y, S.X); %caution, lat is Y not X !
Soilw = zeros(Ntime, 1);
for K = 1 : Ntime
sl = soilw(:,:,K);
Soilw(K) = mean( sl(mask) );
end
output = table(Date, Soilw);
17 Comments
Walter Roberson
on 6 Jan 2022
file = 'soilw.mon.mean.v2.nc';
shapefile = 'Shapefile/lahore.shp';
long = ncread(file, 'lon');
latt = ncread(file, 'lat');
time = ncread(file, 'time');
soilw = ncread(file, 'soilw');
info = ncinfo(file, 'soilw');
soil_fill = info.FillValue;
[found, idx] = ismember('missing_value', {info.Attributes.Name});
if found
soil_fill(end+1) = info.Attributes(idx).Value;
end
mask = ismember(soilw, soil_fill);
soilw(mask) = nan;
Then near the bottom
for K = 1 : Ntime
sl = soilw(:,:,K);
Soilw(K) = mean( sl(ind), 'omitnan' );
end
More Answers (1)
See Also
Categories
Find more on Geographic Coordinate Reference Systems in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!