1

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

......

ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86

2 Answers2

1

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
ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86
  • Good answer. You could also possibly create a grid file in the loop and just have one CDO call. Probably as short an answer as you can get for a command line solution. One line seems impossible with CDO – Robert Wilson Feb 11 '22 at 10:01
  • 1
    This code worked brilliantly. Thank you so much. Just to add a note, I needed a separate text file that contained all the values for the different points in a 'column wise' arrangement. `paste pt_*.txt | pr -t -e24 > output` got the job done. – Nyigam Bole Feb 14 '22 at 11:27
  • 1
    Oops, of course, I tested it to screen and forgot you wanted it in a file, I'll update the answer. thanks! – ClimateUnboxed Feb 14 '22 at 11:34
1

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.

Robert Wilson
  • 3,192
  • 11
  • 19
  • Thank You Robert! Pardon me for my novice question... can I pip install the toolkit? or do I have to get it some other way? – Nyigam Bole Feb 14 '22 at 11:30
  • pip install will work, so long as you have CDO. Best way is using conda as that will ensure dependencies are up to date: https://nctoolkit.readthedocs.io/en/latest/installing.html – Robert Wilson Feb 14 '22 at 11:56