Re: Netcdf files: creation and merge

From: Mary Haley <haley_at_nyahnyahspammersnyahnyah>
Date: Sun Nov 27 2011 - 07:35:51 MST

Hi again,

I realized after sending this routine that I should have used
"cd_inv_calendar" instead of constructing the time array myself. See
the attached slight variation on the previous script I sent you.

You could also use "cd_calendar" to construct the chartime array, but
this function doesn't currently support the format you were requesting
it in.

--Mary

On Nov 23, 2011, at 12:13 PM, Mary Haley wrote:

> Louis,
>
> For starters, it's better to have a "time" coordinate array that are
> numeric values rather than strings (array of characters on a NetCDF
> file).
>
> If you want a string time array, then the way to do this is have
> both: a time numeric array, and a "chartime" (or whatever you want
> to call it) character array.
>
> With that in mind, please see the attached "write_netcdf.ncl" script.
>
> It's set up to read the data off your ASCII files, and then there's
> a big block of code for defining all the variables, including both
> the "time" and "chartime" arrays.
>
> I don't know what format you want the "time" array in, so I just
> made it a double with the value YYYYMMDDHH.minute_fraction.
>
> Note that I used "str_get_cols" to extract pieces of the input file
> names for the output NetCDF file name.
>
> --Mary
>
>
> <write_netcdf.ncl>
>
> On Nov 23, 2011, at 12:29 AM, louis Vonder wrote:
>
>> Many thanks Mary,
>>
>> Sorry for few information I given in my last email.
>>
>> The NCEP/NCAR time that I am talking about look like this
>> 2010:06:01:00:30:00
>> (Year=2010, Month=06, Day=01, LST=00:30:00).
>>
>> Here attached *.txt files that I listed. Each file contain a
>> parameter recorded every 30 min during a day starting at 00:30 LST.
>>
>> So, i want to generated date from the order of files.
>> For example
>>
>> XCut_025_FLFC_01_213K.txt -> 2010:06:01:00:30:00
>> XCut_025_FLFC_02_213K.txt -> 2010:06:01:01:00:00
>>
>> After this operation the unique file generated should have three
>> dimension (time, lat, lon).
>> With these I can manipulate my netcdf data more easily.
>>
>> I guess that informations I send now can better give you some idea
>> on what I am trying to do?
>>
>> Regards
>>
>> Louis
>>
>>
>> --- En date de : Mar 22.11.11, Mary Haley <haley@ucar.edu> a écrit :
>>
>> De: Mary Haley <haley@ucar.edu>
>> Objet: Re: [ncl-talk] Netcdf files: creation and merge
>> À: "louis Vonder" <appopson@yahoo.fr>
>> Cc: "ncl-talk@ucar.edu forum" <ncl-talk@ucar.edu>
>> Date: Mardi 22 novembre 2011, 22h16
>>
>> Louis,
>>
>> I need some clarification. Exactly what problem are you having
>> generating the date? You said you want it to look like the one in
>> the NCEP/NCAR dataset. What do these dates look like?
>>
>> Also I'm not sure what your question is about writing the data to
>> one file. Right now, you have all the NetCDF file stuff inside the
>> loop. If you want to write your data to one file, you'll need to
>> move the file definition code outside the loop, but then add a
>> third dimension along with "lat" and "lon", called something like
>> "date" or "time".
>>
>> It would help if you can provide the *.txt files that you list below.
>>
>> --Mary
>>
>>
>> On Nov 22, 2011, at 10:15 AM, louis Vonder wrote:
>>
>>> Dear all,
>>>
>>> I have a ncl code (inspired from ncl examples) that I am using to
>>> save my data in netcdf format.
>>> But this code creates for each ascii file a netcdf data,
>>> unfortunately this procedure is not nice !!!
>>> Now I want to better customize this script so that i can save all
>>> my data in a unique netcdf file.
>>>
>>> But I am facing a problem to generate the date.
>>> For example, my files are named as the following:
>>>
>>> XCut_025_FLFC_01_213K.txt
>>> XCut_025_FLFC_02_213K.txt
>>> XCut_025_FLFC_03_213K.txt
>>> XCut_025_FLFC_04_213K.txt
>>>
>>> Where 01, 02, 03, 04 correspond to 00:30 LST, 01:00 LST, 01:30 LST
>>> and 02:00 LST (LST = Local Solar Time).
>>>
>>> The year can be for example 2010 during the month of August.
>>>
>>> So I want that the generated file contain date such as in netcdf
>>> file generated by NCEP/NCAR dataset.
>>>
>>> Below the script I am using
>>>
>>> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
>>> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
>>> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
>>> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/shea_util.ncl"
>>>
>>> begin
>>>
>>>
>>>
>>> nlat = 58
>>> ini_lat = 1.
>>> lat = ini_lat + (ispan(0, nlat-1, 1)*0.25)
>>>
>>>
>>> nlon = 66
>>> ini_lon = 1.5
>>> lon = ini_lon + (ispan(0, nlon-1, 1)*0.25)
>>>
>>>
>>> lat@units = "degrees_north"
>>> lat@long_name = "latitude"
>>> lon@units = "degrees_east"
>>> lon@long_name = "longitude"
>>>
>>>
>>>
>>> fili = systemfunc("ls XCut_025_FLFC_*_235K.txt")
>>> nfil = dimsizes( fili )
>>>
>>> do n=0, nfil - 1
>>> name = stringtocharacter(fili(n))
>>> name1 = name(0:20)
>>> name2 = chartostring(name1)
>>>
>>> dummy_data = asciiread(fili(n), (/nlat, nlon/), "float")
>>>
>>> ; Set up coordinate arrays so we can overlay on map.
>>> dummy_data!0 = "lat"
>>> dummy_data!1 = "lon"
>>> dummy_data&lat = lat
>>> dummy_data&lon = lon
>>> dummy_data@units = "%"
>>> dummy_data@_FillValue = -9999.
>>> dummy_data@missing_value = dummy_data@_FillValue
>>> dummy_data@long_name = "Fractional coverage"
>>>
>>> diro = "./" ; Output directory
>>> filo = name2+".nc" ; Output file
>>> system("/bin/rm -f " + diro + filo) ; remove if exists
>>> fout = addfile (diro + filo, "c") ; open output file
>>>
>>>
>>> ;=
>>> ==================================================================
>>> ; explicitly declare file definition mode. Improve efficiency.
>>> ;=
>>> ==================================================================
>>> setfileoption(fout,"DefineMode",True)
>>>
>>> ;=
>>> ==================================================================
>>> ; create global attributes of the file
>>> ;=
>>> ==================================================================
>>> fAtt = True ; assign file
>>> attributes
>>> fAtt@title = "NCL Efficient Approach to netCDF Creation"
>>> fAtt@source_file = "original-file.nc"
>>> fAtt@Conventions = "None"
>>> fAtt@creation_date = systemfunc ("date")
>>> fileattdef( fout, fAtt ) ; copy file attributes
>>> ;=
>>> ==================================================================
>>> ; predefine the coordinate variables and their dimensionality
>>> ; note: to get an UNLIMITED record dimension, we set the
>>> dimensionality
>>> ; to -1 and set the unlimited array to True.
>>> ;=
>>> ==================================================================
>>> dimNames = (/"lat", "lon"/)
>>> dimSizes = (/nlat, nlon/)
>>> dimUnlim = (/False, False/)
>>> filedimdef(fout, dimNames, dimSizes, dimUnlim)
>>>
>>>
>>>
>>> filevardef(fout, "lat" ,typeof(lat), "lat")
>>> filevardef(fout, "lon" ,typeof(lon), "lon")
>>> filevardef(fout, "FC", typeof(dummy_data), (/"lat" ,"lon"/))
>>>
>>>
>>> filevarattdef(fout, "lat", lat) ;
>>> copy lat attributes
>>> filevarattdef(fout, "lon", lon) ;
>>> copy lon attributes
>>> filevarattdef(fout, "FC", dummy_data) ;
>>> copy TOPOG attributes
>>>
>>> setfileoption(fout, "DefineMode", False)
>>>
>>>
>>> fout->lat = (/lat/)
>>> fout->lon = (/lon/)
>>> fout->FC = (/dummy_data/)
>>>
>>>
>>> delete(dummy_data)
>>> delete(fout)
>>>
>>> end do
>>>
>>>
>>> end
>>>
>>>
>>> Regards
>>>
>>>
>>>
>>>
>>> _______________________________________________
>>> ncl-talk mailing list
>>> List instructions, subscriber options, unsubscribe:
>>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>
>> <
>> XCut_025_FLFC_01_213K
>> .txt
>> >
>> <
>> XCut_025_FLFC_02_213K
>> .txt><XCut_025_FLFC_03_213K.txt><XCut_025_FLFC_04_213K.txt>
>
> _______________________________________________
> ncl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Sun Nov 27 07:36:00 2011

This archive was generated by hypermail 2.1.8 : Wed Nov 30 2011 - 19:52:47 MST