MATLAB Answers

extract value from a multidimentional array for exact value for two dimensions

3 views (last 30 days)
Galga
Galga on 7 Sep 2020
Answered: Galga on 16 Sep 2020
Hi everyone,
I'm a beginner in matlab and I'm facing a probably simple problem that I'm not able to solve.
I have a set of netcdf files with climate data and I need to extract values of variables for specific lon lat location.
Each files has the following information:
ncdisp(filename)
Format:
classic
Global Attributes:
Conventions = 'CF-1.6'
production = 'CNRM-RCSM4 coupled model'
nco_openmp_thread_number = 1
NCO = '"4.5.5"'
institution = 'CNRM(Centre National de Recherches Meteorologiques),Meteo-France,Toulouse'
contact = 'florence.sevault@meteo.fr'
experiment_id = 'RCSM4-3.03'
frequency = 'mon'
creation_date = '2018-11-05 11:00:27'
Dimensions:
time = 120 (UNLIMITED)
y = 160
x = 394
Variables:
time
Size: 120x1
Dimensions: time
Datatype: double
Attributes:
units = 'months since 2006-01-01 00:00:00'
calendar = 'standard'
longitude
Size: 394x160
Dimensions: x,y
Datatype: single
Attributes:
standard_name = 'longitude'
units = 'degrees_east'
latitude
Size: 394x160
Dimensions: x,y
Datatype: single
Attributes:
standard_name = 'latitude'
units = 'degrees_north'
zos
Size: 394x160x120
Dimensions: x,y,time
Datatype: single
Attributes:
units = 'm'
_FillValue = 1.000000020040877e+20
cell_methods = 'time_counter: mean'
coordinates = 'longitude latitude'
standard_name = 'sea_surface_height_above_geoid'
valid_min = -1.000000020040877e+20
valid_max = 1.000000020040877e+20
missing_value = 1.000000020040877e+20
cell_method = 'time:mean'
offline_operation = 'zos=zos+0.11'
For the "zos" variable (394x160x120) I need to extract all 120 time value for given location (i.e. lon=12.48 lat= 44.64). I've tried different approaches, based on solved problems, but none of them work.
For example, with getvalue:
lat=ncread(filename,'latitude');
lon=ncread(filename,'longitude');
zos=ncread(filename,'zos');
data=getValue(12.48, 44.64, lon, lat, zos);
I have the following error:
Check for missing argument or incorrect argument data type in call to function 'getValue'.
Any suggestion?
Thanks in advance!

  0 Comments

Sign in to comment.

Accepted Answer

Galga
Galga on 16 Sep 2020
Solved as follow:
latq=xxxx; %my lat
lonq=xxxx; %my lon
%finding the index for my latq and lonq
ilon = round( (lonq-lonmin)/delta_lon )+1;
ilat = round( (latq-latmin)/delta_lat)+1;
for i=1:120
z (:,i)= zos(ilon,ilat,i);
end
In case someone else has my same problem!

  0 Comments

Sign in to comment.

More Answers (1)

Anmol Dhiman
Anmol Dhiman on 11 Sep 2020
Hi Galga,
I understand that you want to extract all 120 time value for given location (lattitude , longitude) from zos where zos is a 3D variable having size 394x160x120.
You can use the following code
lat = ; % the latittude value
lon = ; % the longitude value
values = zos(lat,lon,:); % the values is of dimentsion [1x1x120] and we need to reshape it.
newValue = reshape(values,[1,120]); % new values contain all the time value for a given location.
If you want to use it many times consider creating a function.
Regards,
Anmol Dhiman

  5 Comments

Show 2 older comments
Anmol Dhiman
Anmol Dhiman on 14 Sep 2020
Hi Gaia,
I have checked the dataset given, The above error is coming as zos contains NaN values. Can you verify from your end.
Regards,
Anmol Dhiman
Galga
Galga on 15 Sep 2020
Thanks again for spending time on my issues...
Unfortunately, are not the NaNs the problem. Even with a "clean" variable zos (obtained simpliy substituting the NaN with 0), I obtain the same error, "Index in position 1 is invalid. Array indices must be positive integers or logical values".
So seems I need to identify the indexes corresponding to my lon lat... Any suggestion on this?
Gaia

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!