Skip to content

Commit 428bf7f

Browse files
dglozanoangularsen
andauthored
Add TemperatureGradient and deprecate LapseRate (#991)
Co-authored-by: Andreas Gullberg Larsen <[email protected]>
1 parent 041c0f4 commit 428bf7f

File tree

26 files changed

+2548
-18
lines changed

26 files changed

+2548
-18
lines changed

CodeGen/Generators/QuantityJsonFilesParser.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,19 @@ internal static class QuantityJsonFilesParser
3333
public static Quantity[] ParseQuantities(string rootDir)
3434
{
3535
var jsonDir = Path.Combine(rootDir, "Common/UnitDefinitions");
36-
var jsonFiles = Directory.GetFiles(jsonDir, "*.json");
37-
return jsonFiles.Select(ParseQuantityFile).ToArray();
36+
var jsonFileNames = Directory.GetFiles(jsonDir, "*.json");
37+
return jsonFileNames
38+
.OrderBy(fn => fn, StringComparer.InvariantCultureIgnoreCase)
39+
.Select(ParseQuantityFile)
40+
.ToArray();
3841
}
3942

40-
private static Quantity ParseQuantityFile(string jsonFile)
43+
private static Quantity ParseQuantityFile(string jsonFileName)
4144
{
4245
try
4346
{
44-
var quantity = JsonConvert.DeserializeObject<Quantity>(File.ReadAllText(jsonFile), JsonSerializerSettings)
45-
?? throw new UnitsNetCodeGenException($"Unable to parse quantity from JSON file: {jsonFile}");
47+
var quantity = JsonConvert.DeserializeObject<Quantity>(File.ReadAllText(jsonFileName), JsonSerializerSettings)
48+
?? throw new UnitsNetCodeGenException($"Unable to parse quantity from JSON file: {jsonFileName}");
4649

4750
AddPrefixUnits(quantity);
4851
FixConversionFunctionsForDecimalValueTypes(quantity);
@@ -51,7 +54,7 @@ private static Quantity ParseQuantityFile(string jsonFile)
5154
}
5255
catch (Exception e)
5356
{
54-
throw new Exception($"Error parsing quantity JSON file: {jsonFile}", e);
57+
throw new Exception($"Error parsing quantity JSON file: {jsonFileName}", e);
5558
}
5659
}
5760

CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ public override string Generate()
5858
5959
namespace UnitsNet
6060
{");
61-
Writer.WLIfText(1, GetObsoleteAttributeOrNull(_quantity));
6261
Writer.WL($@"
6362
/// <inheritdoc />
6463
/// <summary>
@@ -70,6 +69,7 @@ namespace UnitsNet
7069
/// {_quantity.XmlDocRemarks}
7170
/// </remarks>");
7271

72+
Writer.WLIfText(1, GetObsoleteAttributeOrNull(_quantity));
7373
Writer.W(@$"
7474
[DataContract]
7575
public partial struct {_quantity.Name} : IQuantity<{_unitEnumName}>, ");

CodeGen/Generators/UnitsNetWrcGen/QuantityGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ public override string Generate()
5555
5656
namespace UnitsNet
5757
{");
58-
Writer.WLIfText(1, GetObsoleteAttributeOrNull(_quantity));
5958
Writer.WL($@"
6059
/// <summary>
6160
/// {_quantity.XmlDoc}
@@ -66,6 +65,7 @@ namespace UnitsNet
6665
/// {_quantity.XmlDocRemarks}
6766
/// </remarks>");
6867

68+
Writer.WLIfText(1, GetObsoleteAttributeOrNull(_quantity));
6969
Writer.WL($@"
7070
// Windows Runtime Component has constraints on public types: https://msdn.microsoft.com/en-us/library/br230301.aspx#Declaring types in Windows Runtime Components
7171
// Public structures can't have any members other than public fields, and those fields must be value types or strings.

Common/UnitDefinitions/LapseRate.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"Name": "LapseRate",
33
"BaseUnit": "DegreeCelsiusPerKilometer",
4+
"ObsoleteText": "Use TemperatureGradient instead.",
45
"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.",
56
"BaseDimensions": {
67
"L": -1,
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
{
2+
"Name": "TemperatureGradient",
3+
"BaseUnit": "KelvinPerMeter",
4+
"XmlDoc": "The rate of change of temperature with displacement in a given direction (as with increase of height)",
5+
"BaseDimensions": {
6+
"L": -1,
7+
"Θ": 1
8+
},
9+
"Units": [
10+
{
11+
"SingularName": "KelvinPerMeter",
12+
"PluralName": "KelvinsPerMeter",
13+
"BaseUnits": {
14+
"L": "Meter",
15+
"Θ": "Kelvin"
16+
},
17+
"FromUnitToBaseFunc": "x",
18+
"FromBaseToUnitFunc": "x",
19+
"Localization": [
20+
{
21+
"Culture": "en-US",
22+
"Abbreviations": [ "∆°K/m" ]
23+
}
24+
]
25+
},
26+
{
27+
"SingularName": "DegreeCelsiusPerMeter",
28+
"PluralName": "DegreesCelciusPerMeter",
29+
"BaseUnits": {
30+
"L": "Meter",
31+
"Θ": "DegreeCelsius"
32+
},
33+
"FromUnitToBaseFunc": "x",
34+
"FromBaseToUnitFunc": "x",
35+
"Localization": [
36+
{
37+
"Culture": "en-US",
38+
"Abbreviations": [ "∆°C/m" ]
39+
}
40+
]
41+
},
42+
{
43+
"SingularName": "DegreeFahrenheitPerFoot",
44+
"PluralName": "DegreesFahrenheitPerFoot",
45+
"BaseUnits": {
46+
"L": "Foot",
47+
"Θ": "DegreeFahrenheit"
48+
},
49+
"FromUnitToBaseFunc": "(x / 0.3048) * 5/9",
50+
"FromBaseToUnitFunc": "(x * 0.3048) * 9/5",
51+
"Localization": [
52+
{
53+
"Culture": "en-US",
54+
"Abbreviations": [ "∆°F/ft" ]
55+
}
56+
]
57+
},
58+
{
59+
"SingularName": "DegreeCelsiusPerKilometer",
60+
"PluralName": "DegreesCelciusPerKilometer",
61+
"BaseUnits": {
62+
"L": "Kilometer",
63+
"Θ": "DegreeCelsius"
64+
},
65+
"FromUnitToBaseFunc": "x / 1e3",
66+
"FromBaseToUnitFunc": "x * 1e3",
67+
"Localization": [
68+
{
69+
"Culture": "en-US",
70+
"Abbreviations": [ "∆°C/km" ]
71+
}
72+
]
73+
}
74+
]
75+
}

UnitsNet.NumberExtensions.Tests/GeneratedCode/NumberToTemperatureGradientExtensionsTest.g.cs

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet.NumberExtensions/GeneratedCode/NumberToTemperatureGradientExtensions.g.cs

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet.Tests/CustomCode/LapseRateTests.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,6 @@ public void TemperatureDeltaDividedByLapseRateEqualsLength()
3838
Assert.Equal(length, Length.FromKilometers(10));
3939
}
4040

41-
[Fact]
42-
public void TemperatureDeltaDividedByLengthEqualsLapseRate()
43-
{
44-
LapseRate lapseRate = TemperatureDelta.FromDegreesCelsius(50) / Length.FromKilometers(10);
45-
Assert.Equal(lapseRate, LapseRate.FromDegreesCelciusPerKilometer(5));
46-
}
47-
4841
[Fact]
4942
public void LengthMultipliedByLapseRateEqualsTemperatureDelta()
5043
{
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by \generate-code.bat.
4+
//
5+
// Changes to this file will be lost when the code is regenerated.
6+
// The build server regenerates the code before each build and a pre-build
7+
// step will regenerate the code on each local build.
8+
//
9+
// See https://github.com/angularsen/UnitsNet/wiki/Adding-a-New-Unit for how to add or edit units.
10+
//
11+
// Add CustomCode\Quantities\MyQuantity.extra.cs files to add code to generated quantities.
12+
// Add UnitDefinitions\MyQuantity.json and run generate-code.bat to generate new units or quantities.
13+
//
14+
// </auto-generated>
15+
//------------------------------------------------------------------------------
16+
17+
// Licensed under MIT No Attribution, see LICENSE file at the root.
18+
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.
19+
20+
using System;
21+
using Xunit;
22+
23+
namespace UnitsNet.Tests.CustomCode
24+
{
25+
public class TemperatureGradientTests : TemperatureGradientTestsBase
26+
{
27+
protected override bool SupportsSIUnitSystem => true;
28+
29+
protected override double DegreesCelciusPerKilometerInOneKelvinPerMeter => 1000;
30+
31+
protected override double DegreesCelciusPerMeterInOneKelvinPerMeter => 1;
32+
33+
protected override double DegreesFahrenheitPerFootInOneKelvinPerMeter => 0.54864;
34+
35+
protected override double KelvinsPerMeterInOneKelvinPerMeter => 1;
36+
37+
[Fact]
38+
public void TemperatureDeltaDividedByTemperatureGradientEqualsLength()
39+
{
40+
Length length = TemperatureDelta.FromDegreesCelsius(50) / TemperatureGradient.FromDegreesCelciusPerKilometer(5);
41+
Assert.Equal(length, Length.FromKilometers(10));
42+
}
43+
44+
[Fact]
45+
public void TemperatureDeltaDividedByLengthEqualsTemperatureGradient()
46+
{
47+
TemperatureGradient lapseRate = TemperatureDelta.FromDegreesCelsius(50) / Length.FromKilometers(10);
48+
Assert.Equal(lapseRate, TemperatureGradient.FromDegreesCelciusPerKilometer(5));
49+
}
50+
51+
[Fact]
52+
public void LengthMultipliedByTemperatureGradientEqualsTemperatureDelta()
53+
{
54+
TemperatureDelta temperatureDelta = Length.FromKilometers(10) * TemperatureGradient.FromDegreesCelciusPerKilometer(5);
55+
Assert.Equal(temperatureDelta, TemperatureDelta.FromDegreesCelsius(50));
56+
}
57+
58+
[Fact]
59+
public void TemperatureGradientMultipliedByLengthEqualsTemperatureDelta()
60+
{
61+
TemperatureDelta temperatureDelta = TemperatureGradient.FromDegreesCelciusPerKilometer(5) * Length.FromKilometers(10);
62+
Assert.Equal(temperatureDelta, TemperatureDelta.FromDegreesCelsius(50));
63+
}
64+
}
65+
}

UnitsNet.Tests/GeneratedCode/IQuantityTests.g.cs

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)