Search code examples
pythonbashcdo-climate

Extract multi value points using CDO into a single text file


I have come across the code

cdo -outputtab, date,value -remapnn,lon=X/lat=Y infile.nc > Outfile.txt

which very nicely extracts for a single point only. Is there any way I can extract time series data from netcdf file for multiple points using a single command line or by using some script and get the output in a single text file? Something like this -

lat-lon1, lat-lon2, lat-lon3

235, 256, 254

264, 246, 249

289, 278, 259

......


Solution

  • I'm not sure why you tagged a cdo command equiry with python, are you looking for a bash command script solution or a python code?

    If you want a simple bash script then you can do this using a loop over lat lon pairs to produce a set of text files and then combine then column-wise using this solution here.

    Note 1: I drop the "date" otherwise you will have the date repeated for each entry - if you must have the date then pull out the first cdo remap command from the loop and do that one including "date".

    Note 2: This will be space separated and not comma separated - I'm assuming that is not an issue

    # these are LON/LAT pairs:
    for i in "10 3" "2 5" "3 7"; do 
       a=( $i )
       lon=${a[0]}
       lat=${a[1]}
       cdo -outputtab,value -remapnn,lon=${lon}/lat=${lat} infile.nc > pt_lon${lon}_lat${lat}.txt
       # change column title from "value" to "lon-lat vals"  
       sed -i -e "s/value/${lon}-${lat}/" pt_lon${lon}_lat${lat}.txt
    done
    # now combine the columns - set the e24 to the width that is appropriate
    paste pt_*.txt  | pr -t -e24 > output.txt