1

I have a csv file with two columns- wind speed and cp (wind turbine power coefficient). I also have various netCDF files (each representing 1 year) with 3hrly wind speed at each grid point. (lat 424, lon 412).

I want to add the corresponding cp value to the netCDF file at each grid point, i.e. add the cp as a variable to the netCDF file, preferable in R but I also have a little bit of experience using python.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • i think this post will be helpful [convert-csv-to-netcdf](https://stackoverflow.com/questions/22933855/convert-csv-to-netcdf) – Biomedical engineer Jun 30 '17 at 13:29
  • 1
    Thanks for your comment. I had a look at that but I do not have lat lon values in my csv file so I didn't think that method would work? The trouble is that I need to assign the cp values to the grid points based on the wind speeds already there in the netCDF file... – Charlie Leaman Jun 30 '17 at 14:16
  • How do you want to map `cp` to the velocity? Are there ranges in wind speed corresponding to a certain `cp` value, e.g. `0 <= u < 1` gives `cp=10`, `1 <= u < 2` gives `cp=20`, et cetera? – Bart Jun 30 '17 at 18:16
  • Yes exactly that, so I considered using the IF statements in R but the speed and cp values in the csv file create a power curve so in an ideal world instead of using ranges of speeds to equal a cp, each speed value say 0.6 would not equal 10 (as your example) but would correspond to the exact cp value on the power curve at that speed, say cp=6... – Charlie Leaman Jul 01 '17 at 12:18

1 Answers1

0

Can't you do a simple interpolation to get the cp value given (1) the V vs cp curve (CSV file) and (2) the velocities from the NetCDF file? Simple interpolations are quite fast in Python (and I guess also in R), and (almost..) certainly faster than implementing something yourself.

If the V vs cp curve from your CSV file is very detailed you could get away with something as simple as a nearest neighbour interpolation, if not there are better alternatives. For example:

import numpy as np
import matplotlib.pylab as pl
from scipy import interpolate

# Given combinations of velocity and cp (from CSV file):
V_csv  = np.linspace(0,10,4)
cp_csv = V_csv**2.

# Random velocities in the range {0..10} (from NetCDF file):
V = np.random.random(5)*10

# Create linear, nearest neighbour and quadratic interpolation functions
f_near = interpolate.interp1d(V_csv, cp_csv, kind='nearest')
f_lin  = interpolate.interp1d(V_csv, cp_csv, kind='linear')
f_quad = interpolate.interp1d(V_csv, cp_csv, kind='quadratic')

# Interpolate:
cp_near = f_near(V)
cp_lin  = f_lin(V)
cp_quad = f_quad(V)

# Visulalization
pl.figure()
pl.plot(np.linspace(0,10,256), np.linspace(0,10,256)**2., label='Real V vs cp curve')
pl.plot(V_csv, cp_csv, '-x', label='Given (discrete) V vs. cp curve')
pl.scatter(V, cp_near, label='nearest')
pl.scatter(V, cp_lin,  label='linear')
pl.scatter(V, cp_quad, label='quadratic')
pl.legend(frameon=False, loc='best')
pl.xlabel('V')
pl.ylabel('cp')

enter image description here

Bart
  • 9,825
  • 5
  • 47
  • 73
  • Thanks for your reply. The trouble is that the equation of the graph has a variable which also changes with the value of wind speed but I do not have the equation showing that relationship... – Charlie Leaman Jul 03 '17 at 11:38
  • Yes sorry I didn't explain that well. In short I do not have the equation for the graph, I am trying to attach the graph but am new to this site. – Charlie Leaman Jul 03 '17 at 13:49