open-meteo / typescript

Open-Meteo Typescript API SDK
MIT License
10 stars 0 forks source link

Sunrise and sunset examples incorrect? #3

Open kenfehling opened 8 months ago

kenfehling commented 8 months ago

Maybe I'm doing something wrong, but I've been having trouble using sunrise and sunset with the TypeScript SDK. I figured a way to make it work; not sure if this is the right way of doing it. If so, I think it'd be useful to update the example code.

Steps to Reproduce the Problem

Use the example TypeScript code from the docs/demo site

sunrise: daily.variables(0)!.valuesArray()!

Fix

Instead, I got this code to work with a hint from the SDK schema docs

sunrise: Number(daily.variables(0)!.valuesInt64(0))

Environment

patrick-zippenfenig commented 8 months ago

Hi, thanks for the report. Yes, the demo code generator on the API documentation does not select the right array for sunrise and sunset. The ideal code would be daily.variables(0)!.valuesInt64Array()!

kenfehling commented 8 months ago

Thanks! Oh I see, yeah I'm only fetching for one day but I can see how it'd be useful to fetch an array if you're fetching forecasts for multiple days. It doesn't seem like valuesInt64Array exists though, I only have these:

export declare class VariableWithValues {
    bb: flatbuffers.ByteBuffer | null;
    bb_pos: number;
    __init(i: number, bb: flatbuffers.ByteBuffer): VariableWithValues;
    static getRootAsVariableWithValues(bb: flatbuffers.ByteBuffer, obj?: VariableWithValues): VariableWithValues;
    static getSizePrefixedRootAsVariableWithValues(bb: flatbuffers.ByteBuffer, obj?: VariableWithValues): VariableWithValues;
    variable(): Variable;
    unit(): Unit;
    value(): number;
    values(index: number): number | null;
    valuesLength(): number;
    valuesArray(): Float32Array | null;
    valuesInt64(index: number): bigint | null;
    valuesInt64Length(): number;
    altitude(): number;
    aggregation(): Aggregation;
    pressureLevel(): number;
    depth(): number;
    depthTo(): number;
    ensembleMember(): number;
}
beaugunderson commented 3 weeks ago

looks like the demo code generator is still broken, can't figure out how to get sunrise/sunset out... tried this:

    sunrise: daily.variables(2)!.valuesInt64(0)!,
    sunset: daily.variables(3)!.valuesInt64(0)!,

but no joy, I also can't access valuesInt64Array

beaugunderson commented 3 weeks ago

ended up using this for them:

function sunToArray(index: number) {
  const array: bigint[] = [];
  const count = daily.variables(index)?.valuesInt64Length() as number;

  for (let i = 0; i < count; i++) {
    array.push(daily.variables(index)?.valuesInt64(i) as bigint);
  }

  return array;
}

and then in daily:

    sunrise: sunToArray(2),
    sunset: sunToArray(3),