###################################################################################
# set the parameters and press Ctrl-ENTER
start = "2016-01-01" # included
end = "2016-01-02" # not included
#sampling = "PT60S" # set to None to use the original sampling
sampling = None
spacecraft = 'A' # A B C
models = ["CHAOS-6-Combined"]
variables=[
"B_NEC_res_CHAOS-6-Combined", "B_NEC", "B_NEC_CHAOS-6-Combined",
"F_res_CHAOS-6-Combined", "F", "F_CHAOS-6-Combined",
'dF_AOCS', 'dF_other', 'F_error', 'B_VFM', 'dB_Sun',
'dB_AOCS', 'dB_other', 'B_error', 'q_NEC_CRF', 'Att_error', 'Flags_F',
'Flags_B', 'Flags_q', 'Flags_Platform', 'ASM_Freq_Dev',
'Kp', 'Dst', 'F107', 'OrbitNumber', 'QDLat', 'QDLon', 'QDBasis', 'MLT',
'SunDeclination', 'SunHourAngle', 'SunRightAscension',
'SunAzimuthAngle', 'SunZenithAngle', 'SunLongitude',
]
output_filename_template = "CHAOS6_residuals_{spacecraft}_{start}_{end}.cdf"
###################################################################################
start_time = parse_datetime(start)
end_time = max(start_time, parse_datetime(end) + timedelta(microseconds=-1))
# split time into monthly chunks
# note that VirES will not allow more than 50 days of 1s samped data
time_windows = list(chop_timerange_by_months(start_time, end_time))
output_filenames = [
output_filename_template.format(
spacecraft=spacecraft,
start=chunk_start_time.strftime("%Y%m%d-%H%M%S"),
end=chunk_end_time.strftime("%Y%m%d-%H%M%S"),
) for chunk_start_time, chunk_end_time in time_windows
]
# validate sampling step
if sampling:
parse_duration(sampling)
# prepare collection identifiers
collections = {
spacecraft: [spacecraft_to_collections_mapping[spacecraft]]
}
# print request summary
print("request summary:")
print("\toutput_filenames:")
for filename in output_filenames:
print("\t\t%s" % filename)
print("\tstart time: %sZ" % start_time.isoformat())
print("\tend time: %sZ" % end_time.isoformat())
print("\tsampling: %s" % (sampling or "original data sampling"))
print("\tcollections:")
for spacecraft, colls in collections.items():
print("\t%s: %s" % (spacecraft, ", ".join(colls)))
print("\tmodels:")
for model in models:
print("\t\t%s" % model)
print("\tvariables:")
for variable in variables:
print("\t\t%s" % variable)
print()
# download data by chunks
for (chunk_start_time, chunk_end_time), output_filename in zip(time_windows, output_filenames):
print("processing %s" % output_filename)
sys.stdout.flush()
# XML WPS request
request = request_template.render(
begin_time=chunk_start_time,
end_time=chunk_end_time,
sampling_step=sampling,
variables=variables,
collection_ids=collections,
model_expressions=",".join(models),
response_type="application/x-cdf",
)
# download the data
if not exists(output_filename):
tmp_filename = output_filename + ".tmp"
try:
with open(tmp_filename, "wb") as fout:
response_handler=ClientRequest._response_handler(fout)
with ProgressBarProcessing() as progress_bar:
wps_proxy.retrieve_async(
request.encode('UTF-8'),
handler=response_handler,
status_handler=progress_bar.update
)
except Exception as error:
if exists(tmp_filename):
remove(tmp_filename)
print("ERROR: %s faled: %s" % (output_filename, error), file=sys.stderr)
else:
if exists(tmp_filename):
rename(tmp_filename, output_filename)
else:
print("%s exists, skipped ... " % output_filename)