How to process NC file in matlab

59 views (last 30 days)
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:
Global Attributes:
_NCProperties = 'version=1|netcdflibversion=|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
Wed Oct 18 15:12:08 2017: ncks -d time,,-3
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
Converted to chunked, deflated non-packed NetCDF4 Jul 2014'
NCO = '4.6.9'
References = ''
lat = 360
lon = 720
time = 886 (UNLIMITED)
Size: 360x1
Dimensions: lat
Datatype: single
long_name = 'Latitude'
units = 'degrees_north'
actual_range = [8.98e+01 -8.98e+01]
standard_name = 'latitude'
axis = 'Y'
coordinate_defines = 'point'
Size: 720x1
Dimensions: lon
Datatype: single
long_name = 'Longitude'
units = 'degrees_east'
actual_range = [2.50e-01 3.60e+02]
standard_name = 'longitude'
axis = 'X'
coordinate_defines = 'point'
Size: 720x360x886
Dimensions: lon,lat,time
Datatype: single
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]
Size: 886x1
Dimensions: time
Datatype: double
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
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?
Muhammad Usman Saleem
Muhammad Usman Saleem on 13 Dec 2021
@Walter Roberson Hahaha, you can signed with your company 4 Hour Response Time Contract. Actually Time is importantance for me, I'm end user of the NC files and all time of the research spent in proccessing the raw dataset.. I would like to thank you very much for your timely response..

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 12 Dec 2021
file = '';
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]);
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) );
output = table(Date, Soilw);
Walter Roberson
Walter Roberson on 6 Jan 2022
file = '';
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;
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' );

Sign in to comment.

More Answers (1)

KSSV on 12 Dec 2021
  1 Comment
Muhammad Usman Saleem
Muhammad Usman Saleem on 12 Dec 2021
Many thanks for your reply @KSSV. I've read your number of answers on NC file proccessing in Matlab but I not able to form code according to my requirement. I spent one week fully to solve my this problem under your replies on different questions but not able to solve that why I post here a question. I not expert in coding will you please write code dear? Thanks for this

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!