MATLAB Answers

1

Best tool for analysing/altering netcdf data (beyond read-only)

Asked by mashtine on 17 Jun 2015
Latest activity Commented on by mashtine on 17 Jun 2015
Hello,
I have a large netcdf file that I would like to do some processing on. CDO software is not adequate enough and I need to resort to using matlab. I can access the data easily with the 'ncread' commands etc but I would like to know how to apply an analysis (say, an equation using a few varibles of the dataset) on the netcdf file using matlab. For instance, can I use matlab to create a new variable in the netcdf file? I have seen the NCToolBox add on for Matlab but this is read-only.
I have a netcdf file with about 10 climate variables and I would like to implement a scheme to calculate a new variable directly within the netcdf. I can use ncread to read in the data but then I am not too sure how I keep other attributes such as time when performing the calculations. My scheme will involve a few steps as well as boolean analysis, so I am a bit confused there.
Thank you!
Hope I explained that well enough,

  0 Comments

Sign in to comment.

2 Answers

Answer by Ashish Uthama on 17 Jun 2015

There are more functions in the ncread family - Have a look at all of them here.
Look at ncwrite for write capability, the documentation has some examples to help you get started.
Once you get the data into MATLAB using ncread, then you can leverage any of MATLAB's analysis functionality on it (its just numbers to MATLAB at this point).

  4 Comments

Show 1 older comment
Can you attach a small sample data file and say what you want to do, like extract the rainfall, and identify and count the dates with rainfall over 5 mm? Or whatever...
I am not sure I understand what you mean by - "calculate a new variable directly within the netcdf... ", "keep other attributes"... "boolean analysis". My interpretation - "I want to read multiple variables from a netcdf file, do some computation using all of them and write a new variable back to the same file". Yes, this can be done with ncread, all of MATLAB and ncwrite respectively.
You might get a more helpful answer with a more specific detailed question.
Thanks Ashish,
I believe my interpretation of how matlab handles the netcdf file was different, hence the brevity in my question but I think I understand what you mean now. I guess my question stemmed from being able to retain the timestamps (1980-01-01 00:00:10 etc) while doing the calculations but I think that this can be done with ncread as well and stored separately if I am not mistaken.
Thank you

Sign in to comment.


Answer by Chad Greene
on 17 Jun 2015

I'm not sure what the problem is. NetCDF files contain matrices, which are well suited for Matlab (MatrixLaboratory). To get a variable like sea surface temperature,
sst = ncread('myfile.nc','sst');
Then you can do whatever analysis you want on the sst matrix. After you've used ncread the analysis part of your question has nothing to do with the fact that the matrix originally came from a NetCDF file. When you're satisfied with your analysis, use ncwrite to write a NetCDF file with whatever variables you'd like.

  1 Comment

Thanks Chad,
As mentioned in my comment above, I was not too sure how to retain the timestamp associated with the variables in the netcdf file while carrying out my calculations if that makes any sense. I think I have a clearer picture now and perhaps I thought it needed to be more complex than it really is.
Thanks again,

Sign in to comment.