Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TemperatureGradient and deprecate LapseRate #991

Merged
merged 6 commits into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions CodeGen/Generators/QuantityJsonFilesParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,19 @@ internal static class QuantityJsonFilesParser
public static Quantity[] ParseQuantities(string rootDir)
{
var jsonDir = Path.Combine(rootDir, "Common/UnitDefinitions");
var jsonFiles = Directory.GetFiles(jsonDir, "*.json");
return jsonFiles.Select(ParseQuantityFile).ToArray();
var jsonFileNames = Directory.GetFiles(jsonDir, "*.json");
return jsonFileNames
.OrderBy(fn => fn, StringComparer.InvariantCultureIgnoreCase)
.Select(ParseQuantityFile)
.ToArray();
}

private static Quantity ParseQuantityFile(string jsonFile)
private static Quantity ParseQuantityFile(string jsonFileName)
{
try
{
var quantity = JsonConvert.DeserializeObject<Quantity>(File.ReadAllText(jsonFile), JsonSerializerSettings)
?? throw new UnitsNetCodeGenException($"Unable to parse quantity from JSON file: {jsonFile}");
var quantity = JsonConvert.DeserializeObject<Quantity>(File.ReadAllText(jsonFileName), JsonSerializerSettings)
?? throw new UnitsNetCodeGenException($"Unable to parse quantity from JSON file: {jsonFileName}");

AddPrefixUnits(quantity);
FixConversionFunctionsForDecimalValueTypes(quantity);
Expand All @@ -51,7 +54,7 @@ private static Quantity ParseQuantityFile(string jsonFile)
}
catch (Exception e)
{
throw new Exception($"Error parsing quantity JSON file: {jsonFile}", e);
throw new Exception($"Error parsing quantity JSON file: {jsonFileName}", e);
}
}

Expand Down
2 changes: 1 addition & 1 deletion CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public override string Generate()

namespace UnitsNet
{");
Writer.WLIfText(1, GetObsoleteAttributeOrNull(_quantity));
Writer.WL($@"
/// <inheritdoc />
/// <summary>
Expand All @@ -70,6 +69,7 @@ namespace UnitsNet
/// {_quantity.XmlDocRemarks}
/// </remarks>");

Writer.WLIfText(1, GetObsoleteAttributeOrNull(_quantity));
Writer.W(@$"
[DataContract]
public partial struct {_quantity.Name} : IQuantity<{_unitEnumName}>, ");
Expand Down
2 changes: 1 addition & 1 deletion CodeGen/Generators/UnitsNetWrcGen/QuantityGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public override string Generate()

namespace UnitsNet
{");
Writer.WLIfText(1, GetObsoleteAttributeOrNull(_quantity));
Writer.WL($@"
/// <summary>
/// {_quantity.XmlDoc}
Expand All @@ -66,6 +65,7 @@ namespace UnitsNet
/// {_quantity.XmlDocRemarks}
/// </remarks>");

Writer.WLIfText(1, GetObsoleteAttributeOrNull(_quantity));
Writer.WL($@"
// Windows Runtime Component has constraints on public types: https://msdn.microsoft.com/en-us/library/br230301.aspx#Declaring types in Windows Runtime Components
// Public structures can't have any members other than public fields, and those fields must be value types or strings.
Expand Down
1 change: 1 addition & 0 deletions Common/UnitDefinitions/LapseRate.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"Name": "LapseRate",
"BaseUnit": "DegreeCelsiusPerKilometer",
"ObsoleteText": "Use TemperatureGradient instead.",
"XmlDoc": "Lapse rate is the rate at which Earth's atmospheric temperature decreases with an increase in altitude, or increases with the decrease in altitude.",
"BaseDimensions": {
"L": -1,
Expand Down
75 changes: 75 additions & 0 deletions Common/UnitDefinitions/TemperatureGradient.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"Name": "TemperatureGradient",
"BaseUnit": "KelvinPerMeter",
"XmlDoc": "The rate of change of temperature with displacement in a given direction (as with increase of height)",
"BaseDimensions": {
"L": -1,
"Θ": 1
},
"Units": [
{
"SingularName": "KelvinPerMeter",
"PluralName": "KelvinsPerMeter",
"BaseUnits": {
"L": "Meter",
"Θ": "Kelvin"
},
"FromUnitToBaseFunc": "x",
"FromBaseToUnitFunc": "x",
"Localization": [
{
"Culture": "en-US",
"Abbreviations": [ "∆°K/m" ]
}
]
},
{
"SingularName": "DegreeCelsiusPerMeter",
"PluralName": "DegreesCelciusPerMeter",
"BaseUnits": {
"L": "Meter",
"Θ": "DegreeCelsius"
},
"FromUnitToBaseFunc": "x",
"FromBaseToUnitFunc": "x",
"Localization": [
{
"Culture": "en-US",
"Abbreviations": [ "∆°C/m" ]
}
]
},
{
"SingularName": "DegreeFahrenheitPerFoot",
"PluralName": "DegreesFahrenheitPerFoot",
"BaseUnits": {
"L": "Foot",
"Θ": "DegreeFahrenheit"
},
"FromUnitToBaseFunc": "(x / 0.3048) * 5/9",
"FromBaseToUnitFunc": "(x * 0.3048) * 9/5",
"Localization": [
{
"Culture": "en-US",
"Abbreviations": [ "∆°F/ft" ]
}
]
},
{
"SingularName": "DegreeCelsiusPerKilometer",
"PluralName": "DegreesCelciusPerKilometer",
"BaseUnits": {
"L": "Kilometer",
"Θ": "DegreeCelsius"
},
"FromUnitToBaseFunc": "x / 1e3",
"FromBaseToUnitFunc": "x * 1e3",
"Localization": [
{
"Culture": "en-US",
"Abbreviations": [ "∆°C/km" ]
}
]
}
]
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 0 additions & 7 deletions UnitsNet.Tests/CustomCode/LapseRateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,6 @@ public void TemperatureDeltaDividedByLapseRateEqualsLength()
Assert.Equal(length, Length.FromKilometers(10));
}

[Fact]
public void TemperatureDeltaDividedByLengthEqualsLapseRate()
{
LapseRate lapseRate = TemperatureDelta.FromDegreesCelsius(50) / Length.FromKilometers(10);
Assert.Equal(lapseRate, LapseRate.FromDegreesCelciusPerKilometer(5));
}

[Fact]
public void LengthMultipliedByLapseRateEqualsTemperatureDelta()
{
Expand Down
65 changes: 65 additions & 0 deletions UnitsNet.Tests/CustomCode/TemperatureGradientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by \generate-code.bat.
//
// Changes to this file will be lost when the code is regenerated.
// The build server regenerates the code before each build and a pre-build
// step will regenerate the code on each local build.
//
// See https://github.com/angularsen/UnitsNet/wiki/Adding-a-New-Unit for how to add or edit units.
//
// Add CustomCode\Quantities\MyQuantity.extra.cs files to add code to generated quantities.
// Add UnitDefinitions\MyQuantity.json and run generate-code.bat to generate new units or quantities.
//
// </auto-generated>
//------------------------------------------------------------------------------

// Licensed under MIT No Attribution, see LICENSE file at the root.
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.

using System;
using Xunit;

namespace UnitsNet.Tests.CustomCode
{
public class TemperatureGradientTests : TemperatureGradientTestsBase
{
protected override bool SupportsSIUnitSystem => true;

protected override double DegreesCelciusPerKilometerInOneKelvinPerMeter => 1000;

protected override double DegreesCelciusPerMeterInOneKelvinPerMeter => 1;

protected override double DegreesFahrenheitPerFootInOneKelvinPerMeter => 0.54864;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


protected override double KelvinsPerMeterInOneKelvinPerMeter => 1;

[Fact]
public void TemperatureDeltaDividedByTemperatureGradientEqualsLength()
{
Length length = TemperatureDelta.FromDegreesCelsius(50) / TemperatureGradient.FromDegreesCelciusPerKilometer(5);
Assert.Equal(length, Length.FromKilometers(10));
}

[Fact]
public void TemperatureDeltaDividedByLengthEqualsTemperatureGradient()
{
TemperatureGradient lapseRate = TemperatureDelta.FromDegreesCelsius(50) / Length.FromKilometers(10);
angularsen marked this conversation as resolved.
Show resolved Hide resolved
Assert.Equal(lapseRate, TemperatureGradient.FromDegreesCelciusPerKilometer(5));
}

[Fact]
public void LengthMultipliedByTemperatureGradientEqualsTemperatureDelta()
{
TemperatureDelta temperatureDelta = Length.FromKilometers(10) * TemperatureGradient.FromDegreesCelciusPerKilometer(5);
Assert.Equal(temperatureDelta, TemperatureDelta.FromDegreesCelsius(50));
}

[Fact]
public void TemperatureGradientMultipliedByLengthEqualsTemperatureDelta()
{
TemperatureDelta temperatureDelta = TemperatureGradient.FromDegreesCelciusPerKilometer(5) * Length.FromKilometers(10);
Assert.Equal(temperatureDelta, TemperatureDelta.FromDegreesCelsius(50));
}
}
}
4 changes: 4 additions & 0 deletions UnitsNet.Tests/GeneratedCode/IQuantityTests.g.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading