1

Background: I am working with satellite observations of SST (time, lat, lon) from the CoRTAD SST dataset (netCDF file). I have a set of (lon,lat) coordinates following the coastal contour of Portugal (called below midshelf locations). I want to extract the SST time series at each of these midshelf locations, average them and subtract the SST at the same latitudes but a fixed longitude to give a coastal SST index.

The midshelf lon,lats were determined first from a nautical chart, which were then linearly interpolated to the lon,lats in the CoRTAD grid.

How can this be done using CDO? The lon,lats from the nautical map are given below.


midshelf lon,lats from the nautical map:

-8.000 43.860 -9.000 43.420 -9.350 43.220 -9.388 42.893 -9.000 42.067 -8.935 41.308 -9.000 40.692 -9.278 40.000 -9.324 39.550 -9.518 39.387 -9.777 38.883 -9.285 38.378 -8.909 38.293 -8.951 38.000 -8.965 37.953 -8.917 37.833 -8.913 37.667 -8.915 37.500 -8.975 37.333 -9.017 37.167 -9.045 37.000

ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86
Greg King
  • 13
  • 4
  • Hi Greg. As I mentioned on the YouTube channel comments it seems what you want to do is mask all the sea points that are next to coast and then average just those points to give you an index, is that correct? I can show you how to do this but just wanted to make sure this is what you require – ClimateUnboxed Jul 20 '22 at 22:34
  • So I think "masking" is probably one way to think about doing it. – Greg King Jul 20 '22 at 23:41
  • "There is a bit more to it." Please state your problem clearly in the question. People are to help, but it's better to not leave them guessing about what you are trying to do – Robert Wilson Jul 21 '22 at 06:15
  • Ok last question. The lat lon points that are in a file, how are they located/calculated? Are they a fixed distance from the coast? Because it may be easier to derive them from scratch, unless they are in the form of a netcdf mask? Or are they are list in a text file? – ClimateUnboxed Jul 21 '22 at 12:25
  • Adrian, I rewrote my question and added information about how the (midshelf) locations were determined. The nautical map midshelf locations are interpolated to the SST grid and stored in a file that is read into a matlab script to extract the time series. – Greg King Jul 22 '22 at 00:00
  • Robert Wilson: Sorry -- This is my first time submitting a question and took getting used to how to edit my question and respond to comments. I have tried to tidy things up. I think my question is pretty straightforward. But sounds like the solution (using cdo) is not straightforward. – Greg King Jul 23 '22 at 12:17
  • @GregKing What is your actual question? Do you simply want to interpolate SST to a set of lat/lon? That is a fairly easy procedure – Robert Wilson Jul 25 '22 at 07:12
  • @RobertWilson I think he wants to calculate the SST at that given set of lat lon points and then subtract the SST at the same lats, but lon=9E to create an index... I was thinking one could loop over the lat-lon pairs and extract those points, get the average with ensmean and then do the same for lon=9E, but I don't know how to loop pairwise... If those points are selected using a depth requirement, I think it would be easier to do it using masking and a topography dataset (also ETOPO built into CDO) but I don't understand the criteria used to define those points yet. – ClimateUnboxed Jul 26 '22 at 13:27
  • @AdrianTompkins That part of the question has been removed, hence my confusion. From your description it sounds more like something you'd want to do in Python/Matlab/R. – Robert Wilson Jul 26 '22 at 14:17
  • yes you are right, the question is written differently now... and it does seem more of a task suitabler for python, I would agree – ClimateUnboxed Jul 26 '22 at 14:22
  • @RobertWilson had to give it a go in bash/cdo , was too tempting not to try ;-) – ClimateUnboxed Jul 27 '22 at 13:26
  • @AdrianTompkins Nice. You could also have done it by creating an unstructured grid with the lat/lons, though that can be a bit tedious if you don't have a script to do it – Robert Wilson Jul 27 '22 at 13:36
  • @AdrianTompkins ... My apologies for removing some of the question. I got the idea that it made some confusion so I tried to give it better focus. I guess that was not necessary to do. Anyway, many thanks for the work you put in and solution you give. I will get right to work on it and give you feedback. – Greg King Jul 28 '22 at 15:08
  • @GregKing really no problem. Best just to ask what you want to know to avoid confusion. Hope the script works for you. Robert's approach is probably more efficient but I need to learn more on unstructured grids. But i think my script should work even if it is slow – ClimateUnboxed Jul 28 '22 at 20:26

1 Answers1

1

So here is my attempt to answer the question as it was stated in the comments (i.e. you wanted an index which was the midshelf locations averaged and then subtracting the same latitude SST sampled at Longitude=9E). I assume the locations are stored pair-wise in a text file called "locations.txt" as in your question above. The loop part of the answer is from one of this question's solutions.

# first loop over the pairs of indices in the text files.
while read -r -a fields; do
    for ((i=0; i < ${#fields[@]}; i += 2)); do
        # precise lon/lat for sampled mid-shelf
        cdo remapnn,"lon=${fields[i]}/lat=${fields[i+1]}" in.nc pt_${i}.nc
        # same lat but lon=9E (change as wanted)
        cdo remapnn,"lon=9/lat=${fields[i+1]}" in.nc 9E_${i}.nc
    done
done < example.txt

# now take the ensemble average over the points.
cdo ensmean pt_*.nc mid_shelf_sst.nc  
cdo ensmean 9E_*.nc mid_shelf_9E.nc

# and calculate the index
cdo sub mid_shelf_sst.nc mid_shelf_9E.nc SST_index.nc
ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86