3

For my project I want to extract data from a netCDF file, my file represent the Ocean Surface Temperature in function of latitude/longitude. I want to select on point with precise coordinate and then do a mean for each months for several years.

import netCDF4 as nc
import numpy as np
import cdo
import os
import pandas as pd

from scipy.io import netcdf

from netCDF4 import Dataset
from netCDF4 import num2date

import matplotlib as mpl
import matplotlib.pylab as plt

from cdo import Cdo
cdo = Cdo()

data = "/Users/bf/Desktop/Stage
tos_Omon_EC-Earth3_historical_r5i1p1f1_gn_185001-201412.nc"

ds = nc.Dataset(data) 
infile = "/Users/bf/Desktop/Stage/tos_Omon_EC-Earth3_historical_r5i1p1f1_gn_185001-201412.nc"


#to select years : 1979-2014     
#new name for the file with the selected years : tos_1979-2014.nc 
cdo.seldate('1979-01-01,2014-12-31', input=infile, output="tos_1979-2014.nc")

#to select longitute and latitute -> no important now
#new name for the file with the selected longitudes/latitudes : tos_1979-2014_2.nc 
cdo.sellonlatbox('-26.45,65.03', input="tos_1979-2014.nc", output="tos_1979-2014_2.nc")

#ymonavg : multi-year monthly statistical values average
cdo.ymonavg(input="tos_1979-2014_2.nc", output="tos_1979-2014_mean_2.nc")

I thought of extract my temperature data (to do a mean of the temperature for each months since 1979 to 2014) and to put them in a data frame, to do graphics thereafter (with mathplolib). But while using CDO tool, I can't select just one point. Maybe I have to use another tool but I don't know how to do it.

Thanks for your help !

ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86
Berenice
  • 43
  • 5

1 Answers1

2

to extract a specific gridpoint you need to use remapping interpolation, with the nearest neighbor interpolation. I think with the python interface it would be

cdo.remapnn('lon=-26.45/lat=65.03', input="tos_1979-2014.nc", output="tos_1979-2014_2.nc")

(I think) -

I have a video guide on this topic in case it is helpful: https://www.youtube.com/watch?v=YdFo8YAbLTw

ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86
  • Can I plot directly NetCDF data to have temperature = f(time) ? Or I have to swift my time data ? Because it seems that the fact I have years with 12 months is a problem to plot, because I have this message : "ValueError: x and y can be no greater than 2D, but have shapes (12,) and (12, 1, 1)" – Berenice May 25 '22 at 12:41
  • yes you can, just extract with array[:,0,0] or use numpy.squeeze(array) – ClimateUnboxed May 25 '22 at 12:46
  • I have another question : is it possible to do the sum of one parameter for each time scale and them to create and other list with this new data ? I try to use "cdo.zonsum" but I do not think it considers the time so it doesn't work. – Berenice Jun 03 '22 at 09:55
  • Hi. This is a new question. Best to post it as a separate question. – ClimateUnboxed Jun 03 '22 at 19:33