Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract point with precise coordinate with on a NetCDF file with Python

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 !

like image 863
Berenice Avatar asked Dec 06 '25 10:12

Berenice


1 Answers

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

like image 82
Adrian Tompkins Avatar answered Dec 08 '25 02:12

Adrian Tompkins



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!