[ncl-talk] Help required to add a variable to geogrid file using NCL

ali mughal mughalali655 at gmail.com
Mon May 10 06:17:46 MDT 2021


Dear Dennis

Thank you for your kind response.

When replying I thought I have copied ncl_talk but was not in cc.

Thank you for the code it solves the problem.

If you allow can I ask a related question in this mail or should I write a
separate email with a new subject.



On Monday, May 10, 2021, Dennis Shea <shea at ucar.edu> wrote:

> The utility 'dos2unix' must be used on the text file from excel.
> ----
> %> ncl AliMughal.WRF_csv.ncl  | less
> ----
>
> Please ***ALL*** comments should go through ncl_talk.
>
> Otherwise, I become your personal programmer.
>
> Good luck
>
> On Fri, May 7, 2021 at 7:40 PM Dennis Shea <shea at ucar.edu> wrote:
>
>> [1] "excel file"   .... I think there are special characters embedded in
>> the csv file. Possibly 'dos' characters.
>>      ****These must be removed before anything will work.**** This is
>> your responsibility
>>
>> [2] There are 27091 lines in the file.  one header + 27090 lines with data
>>
>> [3] data = *readAsciiTable*
>> <http://www.ncl.ucar.edu/Document/Functions/Contributed/readAsciiTable.shtml>(pth_csv,
>> ncol, "double", 1)
>>
>> [4] This looks like gridded data. If so, do you know the dimensions:
>> (nlat,mlon) ?
>>
>> possibly:   nlat=210, mlon=129     ?????
>>
>>     N   = 27090
>>     lat  = LAT(0:nlat-1)
>>     lon = LON(::mlon)        ; same as (0:N-1:mlon)
>>
>>     lat!0 = "lat"
>>     lon!0 = "lon"
>>
>>     lat at long_name = "latitude"
>>     lon:@ong_name = "longitude"
>>
>>     lat at units = "degrees_north" :
>>     lon at units = "degrees_east"
>>
>>     delete([/LAT, LON/])
>>
>>     print(lat)
>>     print(lon)
>>
>>     h1 = *onedtond*
>> <http://www.ncl.ucar.edu/Document/Functions/Built-in/onedtond.shtml>
>> (tofloat(H1),(/nlat,mlon/))
>>     h1 at long_name = "..."
>>     h1 at units  = "..."
>>     h1!0 = "lat"
>>     h1!1 = "lon"
>>
>> =====
>>
>>
>>
>>
>>
>>
>> On Fri, May 7, 2021 at 3:18 AM ali mughal <mughalali655 at gmail.com> wrote:
>>
>>> Dear Dennis
>>>
>>> Thank you for the reply.
>>>
>>> I am getting two warning when executing the above lines
>>>
>>> One is after the line
>>>
>>> cell    = *toint*( data(:,0) )
>>>
>>> warning:toint: there are 967 doubles larger than INT_MAX, which has been
>>> flagged missing.
>>>
>>> and the second one comes after the line  H24 =data(:,26)
>>> warning:asciiread: End of file reached and only (731430) elements were
>>> read from  the file, filling remaining elements with the default missing
>>> value for the req
>>>
>>> Apart from these warnings the data structure is different to what I
>>> want. Each of the variables shows similar dimensions as below.
>>> Variable: LAT
>>> Type: double
>>> Total Size: 216720 bytes
>>>             27090 values
>>> Number of Dimensions: 1
>>> Dimensions and sizes:   [27090]
>>> Coordinates:
>>> Number Of Attributes: 1
>>>   _FillValue :  9.969209968386869e+36
>>>
>>> The new sh variable which I intend to create should have the dimensions
>>> (LAT,LON, H-H24) similar to the excel file.
>>>
>>>
>>> Still I cannot add the variable sh to netcdf file. I have tried to add H
>>> values one by one to the variable sh using the following command but then I
>>> get the following error.
>>>
>>> sh=(/(/LAT/),(/LON/), (/H1/)/)
>>>
>>> fatal:["NclFile.c":432]:FileAddVar: an error occurred while adding a
>>> variable to a file, check to make sure data type is supported by the output
>>> format
>>> fatal:["Execute.c":8578]:Execute: Error occurred at or near line 43 in
>>> file test.ncl
>>>
>>> Could you kindly help. So sorry for the trouble.
>>>
>>>
>>>
>>>
>>> On Wed, May 5, 2021 at 9:17 PM Dennis Shea <shea at ucar.edu> wrote:
>>>
>>>> ; WRF_input_worstcase.csv
>>>> ; 1st 3 lines
>>>> ; cell ID,Longtiude,Latitude,H1,H2,H3,H4,H5,H6,H7,H8,H9,H10,H11,
>>>> H12,H13,H14,H15,H16,H17,H18,H19,H20,H21,H22,H23,H24
>>>> ; 0,103.551796,1.187599182,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
>>>> 0,0,0,0,0,0,0
>>>> ; 1,103.551796,1.190299988,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
>>>> 0,0,0,0,0,0,0
>>>>
>>>>   dir_csv   = "./"
>>>>   fil_csv    = "WRF_input_worstcase.csv"
>>>>   pth_csv  = dir_csv+fil_csv
>>>>
>>>>   ncol = 28
>>>>   data = *readAsciiTable*
>>>> <http://www.ncl.ucar.edu/Document/Functions/Contributed/readAsciiTable.shtml>(pth_csv,
>>>> ncol, "float", 1)      ; <== maybe double wood be better
>>>>   printVarSummary(data)
>>>>
>>>>   cell    = *toint*( data(:,0) )
>>>>   LON  = data(:,1)
>>>>   LAT   = data(:,2)
>>>>   H1     = data(:,3)
>>>> ;      :
>>>>   H24   = data(:,27)
>>>>
>>>>
>>>>
>>>> On Wed, May 5, 2021 at 2:11 AM ali mughal via ncl-talk <
>>>> ncl-talk at mailman.ucar.edu> wrote:
>>>>
>>>>>
>>>>> Dear All
>>>>>
>>>>> I am using the following script to convert the data contained in this (
>>>>> https://we.tl/t-lS4dhtyDs4)  csv file  to add as a 3 dimensional
>>>>> variable to geo_em.d01.nc.
>>>>>
>>>>> filename = "WRF_input_worstcase.csv"
>>>>> a=addfile("../wrfout_d05_2016-03-31_00:00:00.nc","w")  ; I am adding
>>>>> this file only to import structure of variable with W/m^2
>>>>> ac_heat=a->SF_AC_URB3D
>>>>> lines = asciiread(filename,-1,"string")
>>>>> nlines = dimsizes(lines)-1
>>>>> ncols     = dimsizes(str_split(lines(0),","))
>>>>> nrows     = dimsizes(lines)
>>>>>
>>>>> print("This file has " + nrows + " rows and " + ncols + " columns.")
>>>>>
>>>>>   This file has 27091 rows and 27 columns.
>>>>>
>>>>>    lat =  str_get_field(lines , 2, ",")
>>>>>     lon =  str_get_field(lines , 1, ",")
>>>>>
>>>>>     lat := stringtofloat(lat)
>>>>>     lon := stringtofloat(lon)
>>>>>
>>>>>    sh=   traffic=readAsciiTable(filename, 24, "float", 1)            *I
>>>>> want to read 24 columns here from H1-H24 but instead it selects the first
>>>>> 24 columns. The     structure of the file should be latxlonx24*
>>>>>    f = addfile("geo_em.d01.nc","w")
>>>>>
>>>>>    sh at long_name  = "sensible heat flux"
>>>>>    sh at short_name = "heat flux"
>>>>>    sh at units      = "W/m^2"
>>>>>    filevardef(a, "SH_FLUX", typeof(ac_heat), getvardims(ac_heat))
>>>>>    f->SH_FLUX = sh
>>>>>
>>>>> I would be really grateful for help.
>>>>> _______________________________________________
>>>>> ncl-talk mailing list
>>>>> ncl-talk at mailman.ucar.edu
>>>>> List instructions, subscriber options, unsubscribe:
>>>>> https://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>>>
>>>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20210510/e79a0b79/attachment.html>


More information about the ncl-talk mailing list