Home > Back-end >  Extract multi value points using CDO into a single text file
Extract multi value points using CDO into a single text file

Time:02-11

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

......

CodePudding user response:

You can solve this in Python using my package nctoolkit, which uses CDO as a backend. Code would be something like the following:

import pandas as pd
import nctoolkit as nc
# load data
ds = nc.open_data("in.nc")
# define coords you want to regrid to
coords = pd.DataFrame({"lon":[0, 10, 20], "lat":[0, 10, 20]})
# regrid using nn
ds.regrid(coords, "nn")
# convert to pandas df
df = ds.to_dataframe()

Under the hood this will generate a single grid file based on the specified coordinates and then call CDO once to do the regridding, so should be computationally efficient.

CodePudding user response:

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
  • Related