netcdf file read and write

6 views (last 30 days)
Joydeb Saha
Joydeb Saha on 22 Aug 2022
Commented: Cris LaPierre on 3 Oct 2022
I have a netcdf file A.nc containing monthly data from 2011 to 2020. Say the main data is for specific humidity. The variables are :
time
Size: 432x1
Dimensions: time
Datatype: double
Attributes:
bounds = 'time_bnds'
axis = 'T'
long_name = 'time'
standard_name = 'time'
units = 'days since 1850-01-01'
calendar = 'julian'
time_bnds
Size: 2x432
Dimensions: bnds,time
Datatype: double
plev
Size: 19x1
Dimensions: plev
Datatype: double
Attributes:
units = 'Pa'
axis = 'Z'
positive = 'down'
long_name = 'pressure'
standard_name = 'air_pressure'
lat
Size: 32x1
Dimensions: lat
Datatype: double
Attributes:
bounds = 'lat_bnds'
units = 'degrees_north'
axis = 'Y'
long_name = 'Latitude'
standard_name = 'latitude'
lat_bnds
Size: 2x32x432
Dimensions: bnds,lat,time
Datatype: double
lon
Size: 192x1
Dimensions: lon
Datatype: double
Attributes:
bounds = 'lon_bnds'
units = 'degrees_east'
axis = 'X'
long_name = 'Longitude'
standard_name = 'longitude'
lon_bnds
Size: 2x192x432
Dimensions: bnds,lon,time
Datatype: double
hus
Size: 192x32x19x432
Dimensions: lon,lat,plev,time
Datatype: single
Attributes:
_FillValue = 1.000000020040877e+20
standard_name = 'specific_humidity'
long_name = 'Specific Humidity'
comment = 'Specific humidity is the mass fraction of water vapor in (moist) air.'
units = '1'
original_units = '1.0'
javaaddpath('D:\SHUM\NetCDF file read\netcdf souceforge\mexcdf\netcdfAll-4.2.jar');
javaaddpath('D:\SHUM\NetCDF file read\netcdf souceforge\mexcdf\snctools\classes');
addpath ('D:\SHUM\NetCDF file read\netcdf souceforge\mexcdf\snctools');
addpath ('D:\SHUM\NetCDF file read\netcdf souceforge\mexcdf\mexnc');
addpath ('D:\SHUM\NetCDF file read\netcdf souceforge\mexcdf\netcdf_toolbox')
addpath ('D:\SHUM\NetCDF file read\netcdf souceforge\mexcdf\netcdf_toolbox\netcdf\ncsource')
addpath('D:\SHUM\NetCDF file read\netcdf souceforge\mexcdf\netcdf_toolbox\netcdf\nctype')
addpath('D:\SHUM\NetCDF file read\netcdf souceforge\mexcdf\netcdf_toolbox\netcdf\ncutility')
addpath('D:\SHUM\NetCDF file read\netcdf souceforge\mexcdf\netcdf_toolbox\netcdf')
% ncdisp ('E:\ENSO Work\omega\omega.mon.mean.nc')
% info=ncinfo('omega.mon.mean.nc');
data_Wwind=netcdf('J:\JOYDEB_All_Data\SNSR\SH300\hus_Amon_IITM-ESM_ssp585_r1i1p1f1_gn_20150116-20501216_v20200915.nc'); % input address of data folder
ncid = netcdf.open('hus_Amon_IITM-ESM_ssp585_r1i1p1f1_gn_20150116-20501216_v20200915.nc','NC_NOWRITE');
%
% level_Wwind=data_Wwind{'level'}(:); %%%%level required for all level data
lat_Wwind=data_Wwind{'lat'}(:);
lon_Wwind=data_Wwind{'lon'}(:);
time_Wwind=data_Wwind{'time'}(:);
winddata_Wwind=data_Wwind{'hus'}(:);
time_datenum_Wwind = time_Wwind / 24 + datenum('1800-01-01 00:00:0');
Time_Wwind=datevec(time_datenum_Wwind);
>> How can I have the specific humidity data in 120x1 matrix, all months for this 10 years (monthly averaged for 10 years)? I have attached the code as far I could write.
  1 Comment
Cris LaPierre
Cris LaPierre on 3 Oct 2022
I would use the ncread function to extract the data from the *.nc file.
data_Wwind='J:\JOYDEB_All_Data\SNSR\SH300\hus_Amon_IITM-ESM_ssp585_r1i1p1f1_gn_20150116-20501216_v20200915.nc'); % input address of data folder
lat_Wwind=ncread(data_Wwind,'lat');
lon_Wwind=ncread(data_Wwind,'lon');
time_Wwind=ncread(data_Wwind,'time');
winddata_Wwind=ncread(data_Wwind,'hus');
At this point, you have extracted the lat, lon, time, and hus data from the nc file. hus is a 192x32x19x432 matrix, corresponding to lon x lat x plev x time. I'm not sure how your time is organized, but there are 432 measurements total over the 10 years. I'm not sure how that breaks down to months (use your time variable to find that out),
Do you want to average the humidity values across all plev values, or just the values at the same plev?

Sign in to comment.

Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!