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
web/run.py
... ... @@ -11,8 +11,6 @@ import time
11 11 import urllib.request as urllib_request
12 12 import requests
13 13 import re # regex
14   -import numpy as np
15   -import sys
16 14  
17 15 from csv import writer as csv_writer, DictWriter as csv_dict_writer
18 16 from math import sqrt, isnan
... ... @@ -30,6 +28,9 @@ from jinja2 import Environment, FileSystemLoader
30 28 from jinja2.utils import markupsafe
31 29 from yaml import load as yaml_load, dump
32 30  
  31 +from speasy import amda
  32 +import pandas as pd
  33 +
33 34 try:
34 35 from yaml import CLoader as Loader
35 36 except ImportError:
... ... @@ -874,6 +875,44 @@ def generate_csv_contents(target_slug, input_slug, started_at, stopped_at):
874 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 916 def generate_csv_file_if_needed(target_slug, input_slug,
878 917 started_at, stopped_at):
879 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 931 generate = False
893 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 939 if generate:
896 940 log.info("Generating CSV '%s'..." % local_csv_file)
897 941 try:
898 942 with open(local_csv_file, mode="w+") as f:
899   - f.write(generate_csv_contents(
  943 + f.write(csv_generator(
900 944 target_slug=target_slug, input_slug=input_slug,
901 945 started_at=started_at, stopped_at=stopped_at
902 946 ))
... ...