Home > Enterprise >  Errors while getting data in rpy query
Errors while getting data in rpy query

Time:11-04

I need to get nasapower data with use rpy. In documentation r code works in this way:

library("nasapower")
daily_ag <- get_power(community = "ag",
                      lonlat = c(151.81, -27.48),
                      pars = c("RH2M", "T2M", "PRECTOTCORR"),
                      dates = "1985-01-01",
                      temporal_api = "daily"
                      )

I try to do this thing in rpy:

!pip3 install rpy2==3.5.1
import rpy2
import rpy2.robjects as robjects

from rpy2.robjects.packages import importr, data

utils = importr('utils')
base = importr('base')

utils.chooseCRANmirror(ind=1)
utils.install_packages('nasapower')
nasapower = importr('nasapower')


nasapower.get_power("ag",
lonlat=[151.81, -27.48],
pars=["RH2M", "T2M", "PRECTOTCORR"],
dates="1985-01-01",                      
temporal_api = "daily"
                      )

and I got the error:

RRuntimeError: Error: You have entered an invalid value for `lonlat`. Valid values are `global` with `climatology` or a string of lon and lat values.

How should I solve my problem?

CodePudding user response:

You need to convert the list to FloatSexpVector:

!pip3 install rpy2==3.5.1
import rpy2
import rpy2.robjects as robjects

from rpy2.robjects.packages import importr, data
from rpy2.rinterface import FloatSexpVector # <<<

utils = importr('utils')
base = importr('base')

utils.chooseCRANmirror(ind=1)
utils.install_packages('nasapower')
nasapower = importr('nasapower')


nasapower.get_power("ag",
    lonlat=FloatSexpVector([151.81, -27.48]), # <<<
    pars=["RH2M", "T2M", "PRECTOTCORR"],
    dates="1985-01-01",                      
    temporal_api = "daily"
)

CodePudding user response:

It seems like you need change lonlat=[151.81, -27.48] to lonlat = robjects.vectors.FloatVector([151.81, -27.48]) based on the documentation.

https://rpy2.github.io/doc/v2.9.x/html/vector.html#rpy2.robjects.vectors.FloatVector

  • Related