Basic Konfusion...

Programmierung unter AOO/LO (StarBasic, Python, Java, ...)

Moderator: Moderatoren

AndresSolar
Beiträge: 2
Registriert: Do, 25.08.2016 18:24

Basic Konfusion...

Beitrag von AndresSolar »

Hallo zusammen,

irgendwie blicken ich bei ooBasic nicht ganz durch.
Im nachfolgenden Macro wird eine nicht gesetzte Variable bemängelt (y), die natürlich deklariert ist.
Habe schon alles mögliche versucht - ohne erfolg.

Wäre super, wenn mir ein Profi hier weiterhelfen könnte.

Besten Dank im Voraus


'

Code: Alles auswählen

// Main Subroutine ----------------------------------------
Sub CurrentEA
	'Input Values
	dim Timezone as object
	dim Longitude as object
	dim Latitude as object
	dim Time_i as object
	dim JulDay_i as double
	dim JulCen_i as double
	dim Pi as double
	dim i
	dim ti
	dim di
	dim w as double
	dim x as double
	dim z as double
	'dim CapPV as object
	'dim SimPV as object
	
	dim NewPos as string
	dim OutPV as object
	dim svc as object

	dim SunGeoMeLo as double	
	dim SunGeoMeAn as double
	dim EcEarthOrb as double   
	dim SunEquiCtr as double	
	dim SunTrueLon as double 
	dim SunTrueAnm as double	
	dim SunRadVect as double 
	dim SunAppLong as double
	dim MeOblEclip as double  	
	dim ObliqCorre as double  	
	dim SunRtAscen as double 	
	dim SunDeclina as double  	
	dim EquaOfTime as double	
	dim HA_SunRise as double 	
	dim SolarNoon  as double  	
	dim SunriseTim as double 
	dim SunsetTime as double 
	dim SunshHours as double
	dim TrueSolTim as double 
	dim AngleHours as double  
	dim SolarZenit as double   
	dim SolElevati as double   
	dim AtmRefract as double 
	dim SolEleva_i as double
	dim SolAzimu_i as double
	dim varYpsilon as double	
	dim rad as double
	dim deg as double
	
	Doc = ThisComponent 
	Pre = Doc.Sheets(0)
	svc = createUnoService( "com.sun.star.sheet.FunctionAccess" )
	
	'get input
	Timezone = Pre.getCellByPosition(1, 3) 
	Longitude = Pre.getCellByPosition(1, 4) 
	Timezone = Pre.getCellByPosition(1, 5)
	Pi = 3.1415926535897932384626433832795028841971
	
	For i = 0 to 184
		NewPos =  "H" & CSTR(2+i)
		Time_i = Pre.getCellRangeByName(NewPos)
		ti = Time_i.Value
		NewPos =  "F" & CSTR(2+i)		
		Date_i = Pre.getCellRangeByName(NewPos)
		di = Date_i.Value
		JulCen_i = (di - 2451545)/36525
		rad = PI/180
		deg = 180/PI
			SunGeoMeLo = 280.46646+JulCen_i*(36000.76983+JulCen_i*0.0003032) MOD 360 'Calculation of the medium geographic Sun longitude
 	 		SunGeoMeAn = 357.52911+JulCen_i*(35999-0.0001537*JulCen_i) 'Calculation of the medium geographic Sun Anom
			EcEarthOrb = 0.016708634-JulCen_i*(0.000042037+0.000001267*JulCen_i) 'Calculation of ecliptic of the earth's orbit
			SunEquiCtr = sin(SunGeoMeAn*rad)*(19.914602-JulCen_i*(0.004817+0.000014*SunGeoMeAn))+sin(2*SunGeoMeAn*rad)*(0.019993-0.00101*JulCen_i)+sin(3*SunGeoMeAn*rad)*0.000289 'Calculation of the Equidistance of the Sun		
			SunTrueLon = SunGeoMeLo+SunEquiCtr 'Calculation of the medium geographic Sun longitude
			SunTrueAnm = SunGeoMeAn+SunEquiCtr 'Calculation of the true Sun Anom in degree
			SunRadVect = 1.000001018*(1-EcEarthOrb*EcEarthOrb)/(1+EcEarthOrb*Cos(SunTrueAnm*rad)) 'Calculation of the Sun vector in radians
			SunAppLong = SunTrueLon-0.00569-0.00478*Sin((125.04-1934.136*JulCen_i*rad) 'Calculation of the Sun App Longitude in radians
			MeOblEclip = 23+(26+((21.448-JulCen_i*(46.815+JulCen_i*(0.00059-JulCen_i*0.001813))))/60)/60 'Calculation of the mean oblique ecliptic
			ObliqCorre = MeOblEclip+0.00256*Cos((125.04-1934.136*JulCet_i)*rad) 'Calculation of the oblique correction factor
			dim CosSALr as double
			dim CosOCr as double
			CosSALr = Cos(SunAppLong*rad)
			CosOCr = Cos(ObliqCorct*rad)*Sin(SunAppLong*rad)
			SunRtAscen = ArcTan2(CosSALr, CosOCr)*deg
			x = Sin(ObliqCorct*rad)*Sin(SunAppLong*rad)	'Calculation of the Sun declination angle in degree
			SunDeclina = ArcSin(x)*deg	
			varYpsilon = Tan(ObliqCorre/2*rad)^2 'Helper variable y
			'Calculation of the EquationOfTime 
			EquaOfTime = 4*deg*(varYpsilon*Sin(2*SunGeoMeLo*rad)-2*EcEarthOrb*Sin(SunGeoMeAn*rad)+4*EcEarthOrb*varYpsilon*Sin(SunGeoMeAn*rad)*Cos(2*SunGeoMeLo*rad)-0.5*(varYpsilon^2)*Sin(4*SunGeoMeLo*rad)-1.25*(EcEarthOrb^2)*Sin(2*SunGeoMeAn*rad))
			dim y as double	'Calculation of the height angle at Sunrise hour in degree
			y = Cos(90.833/Latitude*SunDeclina*rad)-Tan(Latitude*SunDeclina*rad)
			HA_SunRise = ArcCos(y)*deg
			SolarNoon  = (720-4*Latitude-EquaOfTime+Latitude*60)/1440 'Calculation of Sun noon time
			SunriseTim = SolarNoon-HA_SunRise*4/1440 'Calculation of Sunrise time
			SunsetTime = SolarNoon+HA_SunRise*4/1440 'Calculation of Sunset time
			SunshHours = HA_SunRise*8 'Calculation of the Sun hours
			TrueSolTim = (Time_i*1440+EquaOfTime+4*Latitude-60*Timezone) MOD 1440 'Calculation of the true Solar Time
			If TrueSolTim / 4 < 0 Then 'Calculation of Angle at current time
				AngleHours = TrueSolTim / 4 + 180
				Else
				AngleHours = TrueSolTim / 4 - 180
			End If
			z = (Sin(Latitude*rad)*Sin(SunDeclina*rad)+Cos(Latitude*rad)*Cos(SunDeclina*rad)*Cos(AngleHours*rad))*deg 'Calculate the Solar zenit angle in Degree
			SolarZenit = ArcCos(z)
			SolElevati = 90 - SolarZenit 'Calculate the uncorrected Sun Elevation (We will use the result of Atmospheric Refraction to optimize this value)
			If SolElevati > 85 Then 'Calculation of the Atmospheric Refraction
				AtmRefract = 0
				ElseIf SolElevati > 5 Then
					AtmRefract = 58.1/Tan(SolElevati*rad)-0.07/(Tan(SolElevati*rad)^3)+0.000086/(Tan(SolElevati*rad)^5)
				ElseIf SolElevati > -0.575 Then
					AtmRefract = (1735+SolElevati*(-518.2+SolElevati*(103.4+SolElevati*(-12.79+SolElevati*0.711)))-20.772/Tan(SolElevati*rad))/3600
			End If
			SolEleva_i = SolElevati + AtmRefract 'Calculate the corrected Sun Elevation in Degree
			w = ((Sin(Latitude*rad)*Cos(SolarZenit*rad))-Sin(SunDeclina*rad))/(Cos(Latitude*rad)*Sin(SolarZenit*rad)) 'Calculation of the corrected Sun Azimut Angel in Degree
			If AngleHours > 0 Then
				SolAzimu_i = ArcCos(w)*deg+180 MOD 360
				Else
					SolAzimu_i = 540-ArcCos(w)*deg MOD 360
			EndIf
			'Let's reutrn the results
			NewPos =  "H" & CSTR(2+i)
			OutPV = Pre.getCellByPosition(NewPos)
			OutPV.Value = SunEleva_i.Value
		Next i		
End sub

Sub ArcSin(x as double) as double
	ArcSin = Atn(x/Sqr(1-x^2))
End Sub

Sub ArcCos(x as double) as double
	dim y as double
	y = Sqr(1-x^2)
	ArcCos = ArcSin(y)
End Sub

Sub ArcTan2(x as double, y as double) as double
	dim PI as double
	PI = 3.1415926535897932384626433832795028841971
	If x > 0 Then
		ArcTan2 = Atn(y/x)
		ElseIf x < 0 Then ArcTan2 = Sgn(y) * (PI-Atn(y/x))
		ElseIf y = 0 Then ArcTan2 = 0
	Else
		ArcTan2 = PI/2*Sgn(y)
	End If
End Sub
clag
********
Beiträge: 3570
Registriert: Di, 27.01.2009 15:30

Re: Basic Konfusion...

Beitrag von clag »

Moin AndresSolar,

bin zwar nicht der Profi,
aber prüfe doch bitte einmal welchen Wert "Latitude" an der Stelle enthalten sollte!
LG
clag

nutzt: WinXP SP3 / AOO 4.1.10 / Firefox
AndresSolar
Beiträge: 2
Registriert: Do, 25.08.2016 18:24

Re: Basic Konfusion...

Beitrag von AndresSolar »

Hallo clag,

ja, besten Dank. Ich hab's grad bemerkt. Latitude.Value und Longitude.Value werden aus einer Zelle ausgelesen und war keiner Variablen zugewiesen.
Hab das entsprechend geändert.

Vielleicht kannst Du mir bei einem anderen Problem ebenfalls helfen:

Die UDF's am Ende des Listings funktionieren nicht.
Karolus
********
Beiträge: 7440
Registriert: Mo, 02.01.2006 19:48

Re: Basic Konfusion...

Beitrag von Karolus »

Hallo

Kleine Anmerkung am Rande:

Fliesskommazahlen mit 64bit (double) geben nicht mehr als ~15 signifikante Dezimalstellen Genauigkeit her, es ist völlig irrelevant zu versuchen PI mit 40 Stellen zu verwenden.
Du initialisierst irgendwo css.FunctionAccess benutzt den aber gar nicht.
Vielleicht kannst Du mir bei einem anderen Problem ebenfalls helfen:
Vielleicht räumst du deine gefühlt 1000 Zeilen Kraut-und-Rüben-Code erstmal auf, damit andere eine Chance haben durchzusteigen?
LO7.4.7.2 debian 12(bookworm) auf Raspberry4b 8GB (64bit)
LO7.6.2.1 flatpak debian 12(bookworm) auf Raspberry4b 8GB (64bit)
Antworten