Project

General

Profile

Extracting data from different gridpoints and averaging

Added by Lyndon Mark Olaguera over 2 years ago

Is there a way to extract the values at different gridpoints and then take their average using cdo?

I have my sample netcdf data here (for example, the vorticity file):
https://www.dropbox.com/sh/t88oab9txej73u1/AAAkDCcsTC3_eHg8uBc0vhtda?dl=0

I have a text file containing the coordinates, which is attached to this post.

I am reading this text file using bash:

#!/bin/bash
while IFS=, read -r field1 field2 field3 field4 field5 field6 field7
do
    date=$field1
    lat=$field2
    lon=$field3
    lat_e=$field4
    lat_w=$field5
    lon_e=$field6
    lon_w=$field7

   cdo

echo $lon_e

done < test_dat_lat_lon.csv
~

I want to get the average of multiple grid points for the listed dates after extracting the values.
For example, average for the first line in the text file:

lat_e,lon_e
lat_w_lon_w
lat_e_lon_w
lat_w_lon_e

I have to do this for all the lines in the text file.

field1 and field2 are the central lat and lon respectively. Basically, I want to get the average of the values surrounding this gridpoints. Is this also possible in CDO?
My workaround solution is to extract the values from the four surrounding grid points then get their average. Not sure how to do this in CDO.

Any suggestions on how to do this using cdo?

I'll appreciate any help
--Lyndz

test_dat_lat_lon.csv (1.14 KB) test_dat_lat_lon.csv text file containing the coordinates

Replies (4)

RE: Extracting data from different gridpoints and averaging - Added by Ralf Mueller over 2 years ago

hi Lyndon!

There is not operators that does the selection of multiple point on one run. But I would loop over the points, extract each point into a separate file and compute the average of all files at the end. Since these output files should be small, there no big IO overhead.

  • selection:
    cdo -remapnn,lon=${lon}_lat=$lat <inputfile> out_${lon}_${lat}
  • average:
    cdo  -timmean -cat [ out_* ] all_mean

I used cat which out the values in a temporal order, but since you only the need the average, I think it does not matter

RE: Extracting data from different gridpoints and averaging - Added by Lyndon Mark Olaguera over 2 years ago

Hi Ralf,

Many thanks for the fast response. I am encountering a segmentation error.

Here's the modified script.

#!/bin/bash
while IFS=, read -r field1 field2 field3 field4 field5 field6 field7
do
    date=$field1
    lat=$field2
    lon=$field3

    latn=$field4
    lats=$field5
    lone=$field6
    lonw=${field7}

echo $lonw

    cdo remapnn,lon=${lon}_lat=${latn} -seldate,${date} vort.2008.850hPa.nc n_out_${date}_${lon}_${latn}
    cdo remapnn,lon=${lon}_lat=${lats} -seldate,${date} vort.2008.850hPa.nc s_out_${date}_${lon}_${lats}
    cdo remapnn,lon=${lone}_lat=${lat} -seldate,${date} vort.2008.850hPa.nc e_out_${date}_${lone}_${lat}
    cdo remapnn,lon=${lonw}_lat=${lat} -seldate,${date} vort.2008.850hPa.nc w_out_${date}_${lonw}_${lat}

    cdo timmean n_out_${date}_${lon}_${latn} s_out_${date}_${lon}_${lats} e_out_${date}_${lone}_${lat} w_out_${date}_${lonw}_${lat} test_${date}

    cdo outputtab,${date},${lat},{$lon},value test_${date} > out_$date.txt
#    dat=$(cdo output test_$date)
#    echo $dat > sample_$date
#    cdo -timmean -cat [ out_* ] all_mean

done < test.dat

I tried this for the first line of the input file

2008-01-19    12.5    127.5    13.5    11.5    128.5    126.5

Here's what I got:

126.5
cdo(1) seldate: Process started
Warning (cdfScanVarAttr): NetCDF: Variable not found - time_bnds
cdo    remapnn: Nearest neighbor weights from lonlat (144x73) to lonlat (1x1) grid
cdo(1) seldate: Processed 10512 values from 1 variable over 20 timesteps.
cdo    remapnn: Processed 10512 values from 1 variable over 1 timestep [0.00s 28MB].
cdo(1) seldate: Process started
Warning (cdfScanVarAttr): NetCDF: Variable not found - time_bnds
cdo    remapnn: Nearest neighbor weights from lonlat (144x73) to lonlat (1x1) grid
cdo(1) seldate: Processed 10512 values from 1 variable over 20 timesteps.
cdo    remapnn: Processed 10512 values from 1 variable over 1 timestep [0.00s 28MB].
cdo(1) seldate: Process started
Warning (cdfScanVarAttr): NetCDF: Variable not found - time_bnds
cdo    remapnn: Nearest neighbor weights from lonlat (144x73) to lonlat (1x1) grid
cdo(1) seldate: Processed 10512 values from 1 variable over 20 timesteps.
cdo    remapnn: Processed 10512 values from 1 variable over 1 timestep [0.00s 27MB].
cdo(1) seldate: Process started

_lat=12.5!apnn (Abort): Open failed on lon=126.5
read_csv.sh: line 30: 2897595 Segmentation fault      (core dumped) cdo remapnn,lon=${lonw}_lat=${lat} -seldate,${date} vort.200850hPa.nc w_out_${date}_${lonw}_${lat}

cdo (Abort): Unprocessed Input, could not process all Operators/Files

cdo    outputtab (Abort): Key 2008-01-19 unsupported!

I tried doing it manually for the first line:

 cdo remapnn,lon=126.5_lat=12.5 -seldate,2008-01-19 vort.2008.850hPa.nc test.nc

And this works! Im not sure what is causing the error.

Also I would like to save the values in a text file:

 cdo outputtab,${date},${lat},{$lon},value test_${date} > out_$date.txt

But it say that the date is unsupported.

Any ideas how to solve this?

-Lyndz

RE: Extracting data from different gridpoints and averaging - Added by Ralf Mueller over 2 years ago

hi Lyndz!

I checked the loop over the uploaded CSV file and got dates, which cannot be processed:

+ IFS=,
+ read -r field1 field2 field3 field4 field5 field6 field7
+ date=21/01/2008
+ lat=12.5
+ lon=120
+ latn=13.5
+ lats=11.5
+ lone=121
+ lonw=$'119\r'
+ echo '|21/01/2008|12.5|120|'
|21/01/2008|12.5|120|
+ cdo remapnn,lon=120_lat=13.5 -seldate,21/01/2008 vort.2008.850hPa.nc n_out_21/01/2008_120_13.5
cdo(1) seldate: Process started

cdo(1) seldate (Abort): Date string >21/01/2008< contains invalid character at position 3!
...

Since you seem to get a different date string from your input I think you might re-upload your csv file. I should at least be able to reproduce your results.

cheers
ralf

RE: Extracting data from different gridpoints and averaging - Added by Lyndon Mark Olaguera over 2 years ago

Hi Ralf,

the format changed when I uploaded the file. The date column should be YYYY-MM-DD format.

Attached is a text file with the correct format.

Sincerely,

    (1-4/4)