eliocamp / metR

Tools for Easier Analysis of Meteorological Fields
https://eliocamp.github.io/metR/
140 stars 22 forks source link

Derivate error "variable lengths differ" #113

Closed chrisdane closed 4 years ago

chrisdane commented 4 years ago

Hi

The code

library(pracma)
dxy_tair <- pracma::gradient(tair, lat, lon)
xy_mat <- pracma::meshgrid(lon, lat)
xy_mat$X <- t(xy_mat$X)
xy_mat$Y <- t(xy_mat$Y)
image(lon, lat, tair, xlim=c(200, 300), ylim=c(40, 70))
pracma::quiver(xy_mat$X, xy_mat$Y, dxy_tair$X, dxy_tair$Y, scale = 0.2, col="blue")

with

str(lon)
 num [1:720] 0.25 0.75 1.25 1.75 2.25
str(lat)
 num [1:360] -89.8 -89.2 -88.8 -88.2
str(tair)
 num [1:720, 1:360] NA NA NA NA NA NA NA NA NA  # not all are NA

works (at least something is happening), but the metR equivalent (according to the vignette https://cran.r-project.org/web/packages/metR/vignettes/Working-with-data.html)

library(metR)
dxy_tair <- metR::Derivate(formula=tair ~ lon + lat, 
                           order=1, cyclical=c(T, F), sphere=T)

gives the error

Error in model.frame.default(terms(formula, lhs = lhs, rhs = rhs, data = data,  : 
  variable lengths differ (found for 'lat')

Any help is very appreciated, stay healthy, Chris

pascaloettli commented 4 years ago

Hello,

Please make your example minimal and reproducible. Thank you.

chrisdane commented 4 years ago

Sorry I just realized that the input for metR::Derivate() needs to be a vector, and not a matrix.