Example #1
0
def test_aurora_syntax(json_string):

	try:

		#Create JSON Object
		json_object=json.loads(json_string)

		#Go Through JSON Object
		for ii in range(0,len(json_object)):

			#Test Predicted Date
			predicated_date=json_object[ii]["predicted_time"]

			predicted_date_test=date_util.valid_date(predicated_date,-1)

			if(predicted_date_test[0]==False):
				return predicted_date_test

			#Test Download Date
			download_date=json_object[ii]["download_time"]

			download_date_test=date_util.valid_date(download_date,-1)

			if(download_date_test[0]==False):
				return download_date_test

			#Test Kp
			kp_test=kp_util.valid_kp(json_object[ii]["kp"],-1)

			if(kp_test[0]==False):
				return kp_test

			#Test Forecast
			forecast=json_object[ii]["forecast"]

			if(forecast!="now" and forecast!="h1" and forecast!="d3" and forecast!="d28"):
				return (False,"Invalid forecast \""+forecast+"\" (expected \"now\", \"h1\", \"d3\", or \"d28\").")

	except Exception as e:
		return (False,str(e)[0:].capitalize()+".")

	#Good String
	return (True,"")
def parse_now(lexemes):

	try:
		#JSON Return String
		return_json=""

		#Retrieve Downloaded Time
		download_time=assemble_dict_current_time();
		found_data=False

		#Parse Lexemes
		for ii in range(0,len(lexemes)):

			#Ignore Comments
			if(len(lexemes[ii])>0 and lexemes[ii][0].startswith("#")==False and lexemes[ii][0].startswith(":")==False):

				#Valid Time Row
				if(len(lexemes[ii])==15):

					#Extract Predicted Time
					predicted_time={};
					predicted_time["year"]=int(lexemes[ii][0])
					predicted_time["month"]=int(lexemes[ii][1])
					predicted_time["day"]=int(lexemes[ii][2])
					predicted_time["hour"]=date_util.hhmm_to_hour(lexemes[ii][3])
					predicted_time["minute"]=date_util.hhmm_to_min(lexemes[ii][3])

					#Extract Predicted Kp
					kp=float(lexemes[ii][8])

					#Test Predicted Date
					date_test=date_util.valid_date(predicted_time,ii+1)

					if(date_test[0]==False):
						return date_test

					#Invalid Kp
					kp_test=kp_util.valid_kp(kp,ii+1)

					if(kp_test[0]==False):
						return kp_test

					#Only Add Good Data
					if(kp>=0):
						return_json+=assemble_json_forecast(predicted_time,download_time,"now",kp);
						return_json+="\t,\n"
						found_data=True

				#Unknown Symbol
				else:
					return (False,"Unexpected symbol \""+str(lexemes[ii][0])+"\" on line "+str(ii+1)+".")

		#No Data Means Error
		if(found_data==False):
			return (False,"Did not find any data.")

		#Return Passed and the JSON Object
		return (True,return_json[:-2]+"\n")

	except Exception as e:
		return (False,str(e)[0:].capitalize()+".")
def parse_d3(lexemes):

	try:
		#JSON Return String
		return_json=""

		#Retrieve Downloaded Time
		download_time=assemble_dict_current_time();

		#Default Predicted Time
		found_date=False
		found_data=False

		#Parse Lexemes
		for ii in range(0,len(lexemes)):

			#Ignore Comments
			if(len(lexemes[ii])>0 and lexemes[ii][0].startswith("#")==False and lexemes[ii][0].startswith(":")==False):

				#Valid MD Row
				if(len(lexemes[ii])==6 and date_util.month_to_int(lexemes[ii][0])>0 and string_util.is_int(lexemes[ii][1])):

					#Found a Date
					found_date=True
					date=lexemes[ii]

				#Valid Time Row
				elif(len(lexemes[ii])==4 and found_date==True):

					#Convert Hours
					hours=date_util.hhdashhhut_to_hours(lexemes[ii][0])

					#Extract Data
					if(hours[0]>=0 and hours[1]>=0):

						#3 Days Per Row
						for jj in range(0,3):

							#Extract Predicted Time, Minute is -1
							predicted_time={};
							predicted_time["year"]=download_time["year"]
							predicted_time["month"]=date_util.month_to_int(date[jj*2])
							predicted_time["day"]=int(date[jj*2+1])
							predicted_time["hour"]=hours[0]
							predicted_time["minute"]=-1

							#Extract Predicted Kp
							kp=float(lexemes[ii][1+jj])

							#Test Predicted Date
							date_test=date_util.valid_date(predicted_time,ii+1)

							if(date_test[0]==False):
								return date_test

							#Invalid Kp
							kp_test=kp_util.valid_kp(kp,ii+1)

							if(kp_test[0]==False):
								return kp_test

							#Only Add Good Data
							if(kp>=0):
								return_json+=assemble_json_forecast(predicted_time,download_time,"d3",kp);
								return_json+="\t,\n"
								found_data=True

		#No Data Means Error
		if(found_data==False):
			return (False,"Did not find any data.")

		#Return Passed and the JSON Object
		return (True,return_json[:-2]+"\n")

	except Exception as e:
		return (False,str(e)[0:].capitalize()+".")
def parse_h1(lexemes):

	try:
		#JSON Return String
		return_json=""

		#Retrieve Downloaded Time
		download_time=assemble_dict_current_time();

		#Default Predicted Time
		found_date=False
		found_data=False

		#Parse Lexemes
		for ii in range(0,len(lexemes)):

			#Ignore Comments
			if(len(lexemes[ii])>0 and lexemes[ii][0].startswith("#")==False and lexemes[ii][0].startswith(":")==False):

				#Valid YMD Row
				if(len(lexemes[ii])==3 and string_util.is_int(lexemes[ii][0])):
					#Found a Date
					found_date=True

					#Extract Predicted YMD
					predicted_time={};
					predicted_time["year"]=int(lexemes[ii][0])
					predicted_time["month"]=date_util.month_to_int(lexemes[ii][1])
					predicted_time["day"]=int(lexemes[ii][2])
					predicted_time["hour"]=0;
					predicted_time["minute"]=0;

				#Valid Planetary Estimated Ap Row
				elif(found_date==True and len(lexemes[ii])==11 and lexemes[ii][0].startswith("Planetary")):

					#Go Through The 8 Three Hour Segments
					for jj in range(0,8):

						#Create 3 Entries Per Three Hour Segment
						for kk in range(0,3):

							#Extract Predicted Hour, Minute is -1
							predicted_time["hour"]=jj*3+kk
							predicted_time["minute"]=0

							#Extract Predicted Kp
							kp=float(lexemes[ii][3+jj])

							#Test Predicted Date
							date_test=date_util.valid_date(predicted_time,ii+1)

							if(date_test[0]==False):
								return date_test

							#Invalid Kp
							kp_test=kp_util.valid_kp(kp,ii+1)

							if(kp_test[0]==False):
								return kp_test

							#Only Add Good Data
							if(kp>=0):
								return_json+=assemble_json_forecast(predicted_time,download_time,"h1",kp);
								return_json+="\t,\n"
								found_data=True

		#No Data Means Error
		if(found_data==False):
			return (False,"Did not find any data.")

		#Return Passed and the JSON Object
		return (True,return_json[:-2]+"\n")

	except Exception as e:
		return (False,str(e)[0:].capitalize()+".")