Commit dd6ae65b1317b07877f29a4712159e6ee2fad867

Authored by hitier
1 parent 57278073
Exists in rhitier-dev

Add new csvgenerator method from spz

Showing 1 changed file with 47 additions and 3 deletions   Show diff stats
@@ -11,8 +11,6 @@ import time @@ -11,8 +11,6 @@ import time
11 import urllib.request as urllib_request 11 import urllib.request as urllib_request
12 import requests 12 import requests
13 import re # regex 13 import re # regex
14 -import numpy as np  
15 -import sys  
16 14
17 from csv import writer as csv_writer, DictWriter as csv_dict_writer 15 from csv import writer as csv_writer, DictWriter as csv_dict_writer
18 from math import sqrt, isnan 16 from math import sqrt, isnan
@@ -30,6 +28,9 @@ from jinja2 import Environment, FileSystemLoader @@ -30,6 +28,9 @@ from jinja2 import Environment, FileSystemLoader
30 from jinja2.utils import markupsafe 28 from jinja2.utils import markupsafe
31 from yaml import load as yaml_load, dump 29 from yaml import load as yaml_load, dump
32 30
  31 +from speasy import amda
  32 +import pandas as pd
  33 +
33 try: 34 try:
34 from yaml import CLoader as Loader 35 from yaml import CLoader as Loader
35 except ImportError: 36 except ImportError:
@@ -874,6 +875,44 @@ def generate_csv_contents(target_slug, input_slug, started_at, stopped_at): @@ -874,6 +875,44 @@ def generate_csv_contents(target_slug, input_slug, started_at, stopped_at):
874 return si.getvalue() 875 return si.getvalue()
875 876
876 877
  878 +def generate_csv_contents_spz(target_slug, input_slug, started_at, stopped_at):
  879 + target_config = get_target_config(target_slug)
  880 + params_dict = target_config['models'][input_slug][0]['parameters']
  881 +
  882 + list_df = []
  883 + for _name, _id in params_dict.items():
  884 + _df = amda.get_data(_id, started_at, stopped_at).to_dataframe()
  885 + if _name == 'xy_v':
  886 + _df = _df.rename(columns={_df.columns[0]: 'vrad', _df.columns[1]: 'vtan'})
  887 + elif _name == 'xy_hee':
  888 + _df = _df.drop(_df.columns[2], axis=1)
  889 + _df = _df.rename(columns={_df.columns[0]: 'xhee', _df.columns[1]: 'yhee'})
  890 + else:
  891 + _df = _df.rename(columns={_df.columns[0]: _name})
  892 +
  893 + # _df = _df[~_df.index.duplicated()]
  894 +
  895 + # resample to frequency, for later concatenation
  896 + _df = _df.resample('1H').mean()
  897 +
  898 + # _df = _df.loc[_df.first_valid_index():_df.last_valid_index()]
  899 +
  900 + list_df.append(_df)
  901 +
  902 + from math import sqrt
  903 + final_df = pd.concat(list_df, axis=1)
  904 + # Is ther a vtot param ? else build it
  905 + if 'vtot' not in final_df.columns:
  906 + final_df['vtot'] = final_df.apply(lambda x: sqrt(x.vtan * x.vtan + x.vrad * x.vrad), axis=1)
  907 +
  908 + cols_ordered = ['vrad', 'vtan', 'vtot', 'btan', 'brad', 'temp', 'pdyn', 'dens', 'atse', 'xhee', 'yhee']
  909 + final_df = final_df[cols_ordered]
  910 + final_df.index.name = 'time'
  911 + return final_df.to_csv(date_format='%Y-%m-%dT%H:%M:%S+00:00', float_format="%.5f",
  912 + header=True,
  913 + sep=",")
  914 +
  915 +
877 def generate_csv_file_if_needed(target_slug, input_slug, 916 def generate_csv_file_if_needed(target_slug, input_slug,
878 started_at, stopped_at): 917 started_at, stopped_at):
879 filename = "%s_%s_%s_%s.csv" % (target_slug, input_slug, 918 filename = "%s_%s_%s_%s.csv" % (target_slug, input_slug,
@@ -892,11 +931,16 @@ def generate_csv_file_if_needed(target_slug, input_slug, @@ -892,11 +931,16 @@ def generate_csv_file_if_needed(target_slug, input_slug,
892 generate = False 931 generate = False
893 break 932 break
894 933
  934 + if target_slug == 'mars':
  935 + csv_generator = generate_csv_contents_spz
  936 + else:
  937 + csv_generator = generate_csv_contents
  938 +
895 if generate: 939 if generate:
896 log.info("Generating CSV '%s'..." % local_csv_file) 940 log.info("Generating CSV '%s'..." % local_csv_file)
897 try: 941 try:
898 with open(local_csv_file, mode="w+") as f: 942 with open(local_csv_file, mode="w+") as f:
899 - f.write(generate_csv_contents( 943 + f.write(csv_generator(
900 target_slug=target_slug, input_slug=input_slug, 944 target_slug=target_slug, input_slug=input_slug,
901 started_at=started_at, stopped_at=stopped_at 945 started_at=started_at, stopped_at=stopped_at
902 )) 946 ))