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

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!

 採用された回答

Galga
Galga 2020 年 9 月 16 日
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!

その他の回答 (1 件)

Anmol Dhiman
Anmol Dhiman 2020 年 9 月 11 日
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 件のコメント

Galga
Galga 2020 年 9 月 11 日
Hi Anmol,
thanks for your answer!
Unfortunately, it doesn't work, with the following error:
Index in position 1 is invalid. Array indices must be positive integers or logical values.
Error in extract_value (line 29)
values =zos(lons, lats,:);
Error in run (line 91)
evalin('caller', strcat(script, ';'));
I suppose because in lons lats it wants the index and not the value of lon lat;
I'm able to find the index of lat (or lon) corresponding to my value but I don't know how to find the index for the lon/lat position (where both lon and lat meet my condition).
For example with
latindex = find(lat = 44.64)
I find the indexes in which my lat is what I'm searching for, but I don't know which one corresponds to "my" lon.
...
Anmol Dhiman
Anmol Dhiman 2020 年 9 月 11 日
Hi Gaia,
Can you share a the input file so as to solve the issue.
Anmol Dhiman
Anmol Dhiman
Anmol Dhiman 2020 年 9 月 14 日
編集済み: Anmol Dhiman 2020 年 9 月 14 日
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 2020 年 9 月 15 日
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

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeWeather and Atmospheric Science についてさらに検索

質問済み:

2020 年 9 月 7 日

回答済み:

2020 年 9 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by