[Thread Prev][Thread Next][Index]

Re: [ferret_users] Fwd: Regarding ascii to netcdf conversion





On 2/25/2016 8:43 PM, Venu wrote:
Hi,

I have a huge data file extracted from different ascii files and saved in the order longitude,latitude,year,month,date,depth,temperature and salinity. The depth values are in the standard level i.e 0 to 700 or 1 to 41 levels. The data is not gridded. Please let me know how can I save the file to netcdf as a collection of profiles.

Hi Venu,
You bring up a topic that is coming up a lot recently.  This will be a long answer, and the start of more discussion on these general ideas.

Collections of profiles, ship track data, time series (such as surface mooring data), and other discrete data are a topic of development in our group and in the CF standards.  Look for more Ferret commands and scripts for handling non-gridded data in future releases.  Having made a profile collections dataset, tools for visualizing it and working with the data are being developed for LAS, and will be made available to general Ferret users as well.

The CF standard has defined a style of NetCDF files for this kind of data, "discrete sampling geometries": http://cfconventions.org/Data/cf-conventions/cf-conventions-1.7/build/ch09.html. It looks as if what you have is a collection of profiles, with "instance variables" that include location, time and perhaps more variables that might include a profile ID and other identifying information for each profile.

These could be stored as an Orthogonal Multidimensional Array representation,
http://cfconventions.org/Data/cf-conventions/cf-conventions-1.7/build/ch09s03.html#idp7637760

To use Ferret and make an individual profile file, you would save single-valued variables for those instance variables, perhaps in the L direction or the E direction (E for ensemble, a collected set of something).  You'll need to customize things for your data, but it might look something like this.  [I'm just typing commands here, and they are of course not tested, so beware of typo's.]

This assumes one ascii file per profile. Open the ascii file, listing the variable names from the different columns. 
yes? file/var="lon,lat,year,month,day,depth,tmp,sal" ascii_file.dat
Defining a Z axis from depth
yes? define axis/z/DEPTH/units=meters zdepth = depth 
yes? let zz = z[gz=zdepth]
Define a single-point axis in any direction, say the X axis, for the instance variables
yes? define axis/x=1:1:1 xax
yes? let xx= x[gx=xax]
Define variables to write out : temperature and salinity on the depth axis
yes? let/units="deg c"/title="temperature" temp = reshape(tmp, zz)
yes? let/units="xxx"/title="salinity" salt = reshape(sal, zz)
and the instance variables.  The standard says to put "coordinates" attributes on these, as they are used for geolocation.
http://cfconventions.org/Data/cf-conventions/cf-conventions-1.7/build/ch09s05.html
yes? let/units="degrees_east"/title="Longitude" longitude = reshape(lon[i=1], xx)
yes? let/units="degrees_north"/title="Latitude" latitude =  reshape(lat[i=1], xx)
yes? define attribute longitude.coordinates = "X"

yes? define attribute latitude.coordinates = "Y"
for time, here is how to define a time variable from year, month, day variables. http://ferret.pmel.noaa.gov/Ferret/faq/creating-a-time-axis-from-time-variables.  We're not going to define a time axis, just a single time for the profile. 
yes? let data_time_1900 = DAYS1900(year,month,day)
It may be convenient to define time in other units and a different time origin, more on this below
yes? let days_since_1970 = DAYS1900(1970,1,1)
yes? let time_seconds = (days_since_1970 - data_time_1900) *24*3600
yes? let/units="seconds since 1970"  time = reshape(time_seconds[i=1], xx)
yes? define attribute time.coordinates = "T"
Make an ID from the dataset name. This could be defined in some other way from metadata about the profile, and other relevant variables defined similarly, but only one variable has the cf_role attribute.
yes? define symbol idname = `temp,return=dset`
yes? let/title="profile id" ID = reshape("($idname)" , xx)
yes? define attribute id.cf_role = "profile_id"
Write the profile to a netCDF file. The depth coordinate will be written as the coordinate variable for salinity and time.
yes? save/file=($idname).nc longitude, latitude, time, id, salt, temp
The final thing that a Discrete Sampling Geometries file needs is the global featureType attribute. Ferret currently doesn't have a direct command-line method to write a custom global attribute to a file but we can call the NCO_ATTR function to write one.
yes? let status = NCO_ATTR("($idname).nc", "global", "featureType", "c", "o", "profile")
yes? load status
All of that to make each single profile dataset.  So, how to collect these into a single collection?  You could define a long axis in the "num_profiles" dimension (here perhaps the E direction) and use Ferret's SAVE/APPEND, with the logic to append slabs in the profiles dimension as is done here in example 4
http://ferret.pmel.noaa.gov/Ferret/documentation/users-guide/converting-to-netcdf/SIMPLE-CONVERSIONS-USING-FERRET#_VPINDEXENTRY_1071

The thing to be careful of is to get assign each profile its own E index of the grid.

Define an axis as long or longer than the number of profiles.  The first command saves empty space for the entire axis in the M direction.

yes? define axis/e=1:2000:1 profile_axis 
yes? let eindex = _e[ge=profile_axis]

yes? set list/file=profile_collection.nc  ! define the filename to write to

yes? use profile1.nc
yes? let/units="degrees_east"/title="longitude" elon = longitude + 0*eindex
yes? save/clobber/MLIMITS=1:2000/M=1 elon

yes? let/units="degrees_north"/title="latitude" elat = latitude + 0*eindex
yes? save/append/MLIMITS=1:2000/M=1 elat
...
Likewise for all of the instance variables.  For any variables that are strings, use a RESHAPE function to define the variable on the correct index in the E direction.

yes? let/title="`id,return=title`" eid = reshape(id, EINDEX[M=1])
yes? define attribute eid.cf_role = "profile_id"
yes? save/append
/MLIMITS=1:2000/M=1 eid

! temperature and salinity become 2-D variables, depth and profile
yes? let/units="`temp,return=units`"/title="`temp,return=title`" temp_prof = temp + 0*eindex
yes? let/units="`salt,return=units`"/title="`salt,return=title`" salt_prof = salt + 0*eindex

yes? save/append/MLIMITS=1:2000/M=1 temp_prof
yes? save/append/MLIMITS=1:2000/M=1 salt_prof
yes? cancel data profile1
yes? use profile2.nc

! the variable definitions above now apply to lon, lat, temp, ... from profile2.nc
! except for ID, and any other string variables which need to be redefined

yes? save/append/M=2 elon
yes? save/append/M=2 elat

yes? let/title="`id,return=title`" eid = reshape(id, EINDEX[M=2])
yes? save/append
/M=2 id
...
yes? save/append/M=2 temp_prof
yes? save/append/M=2 salt_prof


Another option for creating a collection is to install ERDDAP.  http://coastwatch.pfeg.noaa.gov/erddap/index.html. This is a tool that works beautifully for this purpose. It points to collections of data, leaving them in their underlying individual files, and then treats them as a single dataset.  The advantage of this is that to add new data, or make a correction (say you found that some of the files were duplicates of others, and should be removed), you just make the change to the collection of profiles, refresh the ERDDAP, and the new set is available.  ERDDAP allows for subset requests, say a subset in space and/or time, a subset of depths, one or multiple variables, and there are various file output types.  ERDDAP is the reason I defined time as seconds-since-1970 above.  That is required for ERDDAP time information.

Ansley



Thanks & Regards....!!

Venu
-----------

Mobile:+91-8712404538
           +91-9573547538


"Now or Never...."

---------- Forwarded message ----------
From: Ansley C. Manke <ansley.b.manke@xxxxxxxx>
Date: Thu, Feb 25, 2016 at 10:40 PM
Subject: Re: Regarding ascii to netcdf conversion
To: Venu <venux4@xxxxxxxxx>


Hi,
Please write to the Ferret list.  For following up with a question from the list, I'm happy to write back and forth, but for new questions, it's best to use the list so that everyone benefits from the exchange, and users can help contribute answers.  Meanwhile think about what you want the data to look like:  a collection of profiles?  gridded?

Ansley


On 2/25/2016 1:49 AM, Venu wrote:
Hi,

I have a huge data file extracted from different ascii files saved in the order longitude,latitude,year,month,date,depth,temperature and salinity. The depth values are in the standard level i.e 0 to 700 or 1 to 41 levels. The data is not gridded. Please let me know how can I save the file to netcdf.


Thanks & Regards....!!

Venu
-----------

Mobile:+91-8712404538
           +91-9573547538


"Now or Never...."




[Thread Prev][Thread Next][Index]
Contact Us
Dept of Commerce / NOAA / OAR / PMEL / Ferret

Privacy Policy | Disclaimer | Accessibility Statement