Skip to content

FAQ/ Conversion from ASC format to importable NetCDF format

Technical Reference Input/Output function/ Terrain, roughness, and embankment import function/ NetCDF format How do I create an importable netCDF file?

response

There are several ways to create a netCDF.

  • How to convert a text file to netCDF format using a tool called ncgen.exe
  • How to export mesh data in ASC format using GIS software such as QGIS and netCDF format using programming languages such as PowerShell

Here we will talk about the second method. For the first method, please refer to this article.

  1. Data such as topography, roughness, embankment, etc. are created using GIS software and created in ESRI ASC Raster format.
    Here is some sample data: Save these files to your desktop.

    substance Sample
    Elevation data glev_asc.txt
    Roughness data roughness_asc.txt
    Embankment top data leveeHeight_asc.txt
    Embankment flag data leveeFlag_asc.txt
  2. Install PowerShell.
    From the Microsoft Store, install PowerShell.
    If you want to install without using the Microsoft Store, see Install PowerShell on Windows.

  3. Install the netCDF library.
    Get the netCDF4 64-bit version from the Installing and Using netCDF-C Libraries in Windows website of the official NetCDF website.
    The latest version as of October 2021 is netCDF4.8.0-NC4-64.exe.
    The following instructions assume that this version 4.8.0 is installed.
    By defaultC:\Program Files\netCDF 4.8.0\, the netCDF library is installed.

  4. Install the netCDF library for PowerShell.
    Download NetcdfCs.dll and C:\Program Files\netCDF 4.8.0\bin copy it to .
    If necessary, use the . NET 5.0. Install .NET on Windows

  5. Create a PowerShell script. Here is a sample:

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    # 入力ファイル
    $GlevAscFile = Get-Item "glev_asc.txt"
    $roughnessAscFile = Get-Item "roughness_asc.txt"
    $leveeHeightAscFile = Get-Item "leveeheight_asc.txt"
    $leveeFlagAscFile = Get-Item "leveeflag_asc.txt"
    
    # 出力ファイル
    $ncFileName = Join-Path (Get-Location) "DiovistaDomainData.nc"
    
    # dllファイル
    $dllFileName = Get-Item "C:\Program Files\netCDF 4.8.0\bin\netcdfCs.dll"
    [void]([System.Reflection.Assembly]::LoadFile($dllFileName))
    
    # エラー時停止
    $ErrorActionPreference = "Stop"
    
    enum NcType {
        NC_BYTE = 1
        NC_SHORT = 3
        NC_INT = 4
    }
    class AscData {
        [System.IO.FileInfo]$file
        [int]$ncols
        [int]$nrows
        [double]$xllcorner
        [double]$yllcorner
        [double]$dx
        [double]$dy
        [double]$nodataValue
        [double[]] $data
        [int[]] $intData
        [int16[]] $shortData
        [byte[]] $byteData
        [int]$validMin
        [double]$scaleFactor
    
        # ctor
        AscData([System.IO.FileInfo]$file, [NcType]$ncType, [int]$validMin, [double]$scaleFactor) {
            $this.file = $file
            $this.validMin = $validMin
            $this.scaleFactor = $scaleFactor
            $scaleFactorInverse = 1.0 / $scaleFactor
            $headerLines = 7
            $lines = Get-Content -Encoding utf8 $this.file.FullName
            foreach ($headerLine in $lines[0..($headerLines - 1)]) {
                $header = $headerLine -split " +"
                if ($header[0] -like "NCOLS") {
                    $this.ncols = $header[1]
                }
                elseif ($header[0] -like "NROWS") {
                    $this.nrows = $header[1]
                }
                elseif ($header[0] -like "XLLCORNER") {
                    $this.xllcorner = $header[1]
                }
                elseif ($header[0] -like "YLLCORNER") {
                    $this.yllcorner = $header[1]
                }
                elseif ($header[0] -like "DX") {
                    $this.dx = $header[1]
                }
                elseif ($header[0] -like "DY") {
                    $this.dy = $header[1]
                }
                elseif ($header[0] -like "NODATA_VALUE") {
                    $this.nodataValue = $header[1]
                }
                else {
                    throw [System.Exception] (("{0}: found not supported header: {1}" -f $this.file, $header[0]))
                }
            }
            $this.DumpHeader()
            $this.data = New-Object double[] ($this.ncols * $this.nrows)
            for ($j = 0; $j -lt $this.nrows; $j++) {
                $ws = $lines[$j + $headerLines] -split " +"
                $i0 = $j * $this.ncols
                for ($i = 0; $i -lt $this.ncols; $i++) {
                    $this.data[$i0 + $i] = $ws[$i]
                }
            }
            if ($ncType -eq [NcType]::NC_BYTE) {
                $this.byteData = New-Object byte[] ($this.ncols * $this.nrows)
                for ($k = 0; $k -lt $this.data.Length; $k++) {
                    $value = $this.data[$k]
                    if ($value -eq $this.nodataValue) {
                        $this.byteData[$k] = 0;
                    } else {
                        $this.byteData[$k] = $value
                    }
                }
            }
            elseif($ncType -eq [NcType]::NC_SHORT) {
                $this.shortData = New-Object int16[] ($this.ncols * $this.nrows)
                for ($k = 0; $k -lt $this.data.Length; $k++) {
                    $value = $this.data[$k]
                    if ($value -eq $this.nodataValue) {
                        $this.shortData[$k] = $validMin - 1;
                    } else {
                        $this.shortData[$k] = [int16]($value * $scaleFactorInverse)
                    }
                }
            }
            elseif($ncType -eq [NcType]::NC_INT) {
                $this.intData = New-Object int[] ($this.ncols * $this.nrows)
                for ($k = 0; $k -lt $this.data.Length; $k++) {
                    $value = $this.data[$k]
                    if ($value -eq $this.nodataValue) {
                        $this.intData[$k] = $validMin - 1;
                    } else {
                        $this.intData[$k] = [int]($value * $scaleFactorInverse)
                    }
                }
            }
    
        }
        [void] DumpHeader() {
            $message = "file: {0}`n" -f $this.file.FullName
            $message += "ncols: {0}`n" -f $this.ncols
            $message += "nrows: {0}`n" -f $this.nrows
            $message += "xllcorner: {0}`n" -f $this.xllcorner
            $message += "yllcorner: {0}`n" -f $this.yllcorner
            $message += "dx: {0}`n" -f $this.dx
            $message += "dy: {0}`n" -f $this.dy
            $message += "nodataValue: {0}`n" -f $this.nodataValue
            Write-Host $message
        }
    }
    
    # load asc files
    $GlevAsc = New-Object AscData($GlevAscFile, [NcType]::NC_INT, -100000, 0.01)
    $roughnessAsc = New-Object AscData($roughnessAscFile, [NcType]::NC_SHORT, 0, 0.001)
    $leveeHeightAsc = New-Object AscData($leveeHeightAscFile, [NcType]::NC_INT, -100000, 0.01)
    $leveeFlagAsc = New-Object AscData($leveeFlagAscFile, [NcType]::NC_BYTE, 0, 1.0)
    $asc = $GlevAsc
    
    # create netcdf file
    $ncFile = New-Object netcdfCs.NcFile
    if ((Test-Path $ncFileName) -eq $true) {
        Remove-Item $ncFileName
    }
    # Options(bool isNetCDF4, bool forceOverwrite, int compressionLevel
    $ncOption = New-Object NetcdfCs.NcFile+Options($true, $true, 6)
    $ncFile.Create($ncFileName, $ncOption)
    Write-Host ("Create {0}" -f $ncFileName)
    
    # global attributes
    # 表 9(1)ファイル識別子
    [void]$ncFile.AddAttribute("title", [netcdfCs.NcType]::NC_CHAR, "DioVISTA calculation domain data")
    # 使用するnetcdf規約のバージョン(CF-1.6)
    [void]$ncFile.AddAttribute("Conventions", [netcdfCs.NcType]::NC_CHAR, "CF-1.6")
    # その他コメント
    [void]$ncFile.AddAttribute("comment", [netcdfCs.NcType]::NC_CHAR, ("generated on {0} by netcdfPs1_Convert-From-Asc-To-DiovistaDomainNc.ps1" -f (Get-Date)))
    
    # dimensions
    $dimLon = $ncFile.AddDimension("lon", $asc.ncols)
    $dimLat = $ncFile.AddDimension("lat", $asc.nrows)
    
    # variables
    $varLon = $ncFile.AddVariable("lon", [netcdfCs.NcType]::NC_SHORT, $dimLon.DimId)
    [void]$varLon.AddAttribute("add_offset", [netcdfCs.NcType]::NC_DOUBLE, $asc.xllcorner + $asc.dx * 0.5)
    [void]$varLon.AddAttribute("scale_factor", [netcdfCs.NcType]::NC_DOUBLE, $asc.dx)
    [void]$varLon.AddAttribute("units", [netcdfCs.NcType]::NC_CHAR, "degrees_east")
    
    $varLat = $ncFile.AddVariable("lat", [netcdfCs.NcType]::NC_SHORT, $dimLat.DimId)
    [void]$varLat.AddAttribute("add_offset", [netcdfCs.NcType]::NC_DOUBLE, $asc.yllcorner + $asc.dy * 0.5)
    [void]$varLat.AddAttribute("scale_factor", [netcdfCs.NcType]::NC_DOUBLE, $asc.dy)
    [void]$varLat.AddAttribute("units", [netcdfCs.NcType]::NC_CHAR, "degrees_north")
    
    $varGlev = $ncFile.AddVariable("glev", [netcdfCs.NcType]::NC_INT, ($dimLat.DimId, $dimLon.DimId))
    [void]$varGlev.AddAttribute("valid_min", [netcdfCs.NcType]::NC_INT, [int]$GlevAsc.validMin)
    [void]$varGlev.AddAttribute("scale_factor", [netcdfCs.NcType]::NC_DOUBLE, $GlevAsc.scaleFactor)
    [void]$varGlev.AddAttribute("units", [netcdfCs.NcType]::NC_CHAR, "m")
    
    $varRoughness = $ncFile.AddVariable("roughness", [netcdfCs.NcType]::NC_SHORT, ($dimLat.DimId, $dimLon.DimId))
    [void]$varRoughness.AddAttribute("valid_min", [netcdfCs.NcType]::NC_SHORT, [int16]$roughnessAsc.validMin)
    [void]$varRoughness.AddAttribute("scale_factor", [netcdfCs.NcType]::NC_DOUBLE, $roughnessAsc.scaleFactor)
    [void]$varRoughness.AddAttribute("units", [netcdfCs.NcType]::NC_CHAR, "m^(-1/3).s")
    
    $varLeveeHeight = $ncFile.AddVariable("leveeHeight", [netcdfCs.NcType]::NC_INT, ($dimLat.DimId, $dimLon.DimId))
    [void]$varLeveeHeight.AddAttribute("valid_min", [netcdfCs.NcType]::NC_INT, [int]$leveeHeightAsc.validMin)
    [void]$varLeveeHeight.AddAttribute("scale_factor", [netcdfCs.NcType]::NC_DOUBLE, $leveeHeightAsc.scaleFactor)
    [void]$varLeveeHeight.AddAttribute("units", [netcdfCs.NcType]::NC_CHAR, "m")
    
    $varLeveeFlag = $ncFile.AddVariable("leveeFlag", [netcdfCs.NcType]::NC_BYTE, ($dimLat.DimId, $dimLon.DimId))
    [void]$varLeveeFlag.AddAttribute("flag_meanings", [netcdfCs.NcType]::NC_CHAR, "east north")
    $flagMasks = New-Object byte[] (2)
    $flagMasks[0] = 1
    $flagMasks[1] = 2
    [void]$varLeveeFlag.AddAttribute("flag_masks", [netcdfCs.NcType]::NC_BYTE, $flagMasks)
    
    $ncFile.SaveDefinitions()
    # Write-Host "Save Definitions"
    
    # set variable data
    # x
    $shortData = New-Object int16[] $dimLon.Length
    for ($i = 0; $i -lt $asc.ncols; $i++) {
        $shortData[$i] = $i
    }
    $varLon.Values = $shortData
    $varLon.SaveData($ncFile.NcId)
    
    # y
    $shortData = New-Object int16[] $dimLat.Length
    for ($j = 0; $j -lt $asc.nrows; $j++) {
        $shortData[$j] = $asc.nrows - 1 - $j
    }
    $varLat.Values = $shortData
    $varLat.SaveData($ncFile.NcId)
    
    # Glev
    $varGlev.Values = $GlevAsc.intData
    $varGlev.SaveData($ncFile.NcId)
    
    # roughness
    $varRoughness.Values = $roughnessAsc.shortData
    $varRoughness.SaveData($ncFile.NcId)
    
    # leveeheight
    $varLeveeHeight.Values = $leveeheightAsc.intData
    $varLeveeHeight.SaveData($ncFile.NcId)
    
    # leveeheight
    $varLeveeFlag.Values = $leveeflagAsc.byteData
    $varLeveeFlag.SaveData($ncFile.NcId)
    
    $ncFile.Close()
    # Write-Host ("Close {0}" -f $ncFileName)
    
    • In lines 2~5, specify the file to be entered.
    • On line 8, specify the full path of the file to output. Here, DiovistaDomainData.nc we have set to output to the desktop.
    • On line 11, specify the path to the netCDF library for PowerShell that you installed in step 2. Here,"C:\Program Files\netCDF 4.8.0\bin\netcdfCs.dll" we specify .
    • Paste the script into a text editor and save it as a file name on your desktop netcdfPs1_Convert-From-Asc-To-DiovistaDomainNc.ps1 .
  6. Run the script in PowerShell.
    Press + R and type in the dialog that appears [Run] pwsh .
    When PowerShell starts, copy and paste the following string to run it:

    1
    2
    3
    cd $env:USERPROFILE\Desktop
    Set-ExecutionPolicy RemoteSigned -Scope Process
    .\netcdfPs1_Convert-From-Asc-To-DiovistaDomainNc.ps1
    

is created on your desktop DiovistaDomainData.nc .

  1. Bring the created netCDF into DioVISTA.

    1. Launch DioVISTA.
    2. Select Menu [file] > > [Create new from template] [Latitude and longitude coordinates] >.

    3. Select Project > [Calculation area] > [Import from NetCDF] .

    4. Specify for the file name of the dialog that appears DiovistaDomainData.nc .
      Specify a mesh size of 25 m.

    5. Data has been ingested into DioVISTA.

Last update: 2023-03-20