//------------------------------------------------------------------------- /// <summary> /// Checks whether the source is a CSV format sensitivities file. /// <para> /// This parses the headers as CSV and checks that mandatory headers are present. /// /// </para> /// </summary> /// <param name="charSource"> the CSV character source to check </param> /// <returns> true if the source is a CSV file with known headers, false otherwise </returns> public bool isKnownFormat(CharSource charSource) { try { using (CsvIterator csv = CsvIterator.of(charSource, true)) { if (!csv.containsHeader(TENOR_HEADER) && !csv.containsHeader(DATE_HEADER)) { return(false); } if (csv.containsHeader(REFERENCE_HEADER) && csv.containsHeader(TYPE_HEADER) && csv.containsHeader(VALUE_HEADER)) { return(true); // standard format } else if (csv.containsHeader(REFERENCE_HEADER) || csv.containsHeader(TYPE_HEADER)) { return(true); // list or grid format } else { return(csv.headers().Any(SensitivityCsvLoader.knownReference)); // implied grid format } } } catch (Exception) { return(false); } }
//------------------------------------------------------------------------- // parses the file in grid format private void parseGridFormat(CsvIterator csv, ListMultimap <string, CurveSensitivities> parsed, IList <FailureItem> failures) { // find the applicable reference columns IDictionary <string, CurveName> references = new LinkedHashMap <string, CurveName>(); foreach (string header in csv.headers()) { string headerLowerCase = header.ToLower(Locale.ENGLISH); if (!REF_HEADERS.contains(headerLowerCase) && !resolver.isInfoColumn(headerLowerCase)) { references[header] = CurveName.of(header); } } // loop around all rows, peeking to match batches with the same identifier // no exception catch at this level to avoid infinite loops while (csv.hasNext()) { CsvRow peekedRow = csv.peek(); PortfolioItemInfo info = parseInfo(peekedRow); //JAVA TO C# CONVERTER TODO TASK: Method reference arbitrary object instance method syntax is not converted by Java to C# Converter: string id = info.Id.map(StandardId::toString).orElse(""); // process in batches, where the ID is the same CurveSensitivitiesBuilder builder = CurveSensitivities.builder(info); IList <CsvRow> batchRows = csv.nextBatch(r => matchId(r, id)); foreach (CsvRow batchRow in batchRows) { try { ParameterMetadata metadata = parseMetadata(batchRow, true); CurveSensitivitiesType type = batchRow.findValue(TYPE_HEADER).map(str => CurveSensitivitiesType.of(str)).orElse(CurveSensitivitiesType.ZERO_RATE_DELTA); foreach (KeyValuePair <string, CurveName> entry in references.SetOfKeyValuePairs()) { CurveName reference = entry.Value; CurveName resolvedCurveName = resolver.checkCurveName(reference); string valueStr = batchRow.getField(entry.Key); Currency currency = parseCurrency(batchRow, reference); if (valueStr.Length > 0) { double value = LoaderUtils.parseDouble(valueStr); builder.add(type, resolvedCurveName, currency, metadata, value); } } } catch (System.ArgumentException ex) { failures.Add(FailureItem.of(PARSING, "CSV file could not be parsed at line {}: {}", batchRow.lineNumber(), ex.Message)); } } CurveSensitivities sens = builder.build(); if (!sens.TypedSensitivities.Empty) { parsed.put(sens.Id.map(object.toString).orElse(""), sens); } } }
// loads a single CSV file, filtering by trade type private ValueWithFailures <IList <T> > parseFile <T>(CharSource charSource, Type <T> tradeType) where T : com.opengamma.strata.product.Trade { try { using (CsvIterator csv = CsvIterator.of(charSource, true)) { if (!csv.headers().contains(TYPE_FIELD)) { return(ValueWithFailures.of(ImmutableList.of(), FailureItem.of(FailureReason.PARSING, "CSV file does not contain '{header}' header: {}", TYPE_FIELD, charSource))); } return(parseFile(csv, tradeType)); } } catch (Exception ex) { return(ValueWithFailures.of(ImmutableList.of(), FailureItem.of(FailureReason.PARSING, ex, "CSV file could not be parsed: {exceptionMessage}: {}", ex.Message, charSource))); } }