[ncl-talk] Read Matlab file and shift data longitudinally

Giorgio Graffino g.graffino at tim.it
Mon Dec 18 03:10:52 MST 2023


Hi Dave,
 
Thanks for your reply. My first attempt at solving the problem was with 
ncwrite, but it was always giving me this error:
 
Error using netcdflib
The number of input elements does not match the variable size.
 
This is why I had to use low-level functions. I did some research and 
found more functions to define dimensions and attributes, and now the 
data look all right. I'm attaching the script in case anyone needs it.
 
Cheers,
Giorgio
 
------ Messaggio Originale ------
Da: dave.allured at noaa.gov
A: g.graffino at tim.it Cc: ncl-talk at ucar.edu
Inviato: venerdì, dicembre 15º 2023, 04:32 PM
Oggetto: Re: [ncl-talk] Read Matlab file and shift data longitudinally
 
Giorgio, I think you are right, there may be something wrong with the 
way the Netcdf file is created.  Your link pointed to "Write Data to 
NetCDF File Using Low-Level Functions".  Try using the Matlab high-level 
Netcdf export functions instead, such as ncwrite.  Those do a better job 
of preserving dimensions and data alignment.
 
It will be more constructive to debug your Matlab export process, than 
to try to manipulate a damaged data file.  Check your work by comparing 
dimensions and data alignment with Matlab and Netcdf diagnostic 
functions.  Dimensions before and after export should match exactly.  
Data samples at the same indices should match exactly.  I find that NCL 
is convenient for examining small data samples, such as:
 
    print (temp(24089,20,0:3))
 
Matlab has something similar.
 

 
On Fri, Dec 15, 2023 at 5:47 AM Giorgio Graffino via ncl-talk 
<ncl-talk at mailman.ucar.edu <mailto:ncl-talk at mailman.ucar.edu> > wrote:
 
Dear NCL experts,
 
Hope you're all well. As per the subject, I want to read a .mat file 
containing daily SST data from 1940 to 2005 and run some analysis I've 
already written in NCL. If I try to directly read the file with NCL I 
get Segmentation fault, so that's not the right way to go. I converted 
the file into NetCDF using Matlab 
(https://uk.mathworks.com/help/matlab/import_export/exporting-to-network-common-data-form-netcdf-files.html?s_tid=answers_rc2-3_p6_MLT#bsxb70y-1 
<https://uk.mathworks.com/help/matlab/import_export/exporting-to-network-common-data-form-netcdf-files.html?s_tid=answers_rc2-3_p6_MLT#bsxb70y-1> 
), but there is a problem with the data. 
 
At some point in the time series (around 1962), the data get shifted in 
the longitude sense by about 40 degrees. I checked this in NCL and in 
Ferret, as shown in the attached screenshot: the left-hand side is SST 
on a random day before 1962, while the right-hand side is after 1962. 
It's like there is an offset in the data longitudes. The plots are drawn 
using only four lines in Ferret, so the error can't be due to the the 
software. There must be something wrong going on when the file is 
created. It would be great to know what happens to the data in the first 
place, but maybe it's a discussion that doesn't belong here.
 
This is the printVarSummary of the full data. The longitudes array does 
not change over time, but the data are shifted.
 
Variable: temp
Type: float
Total Size: 710269560 bytes
           177567390 values
Number of Dimensions: 3
Dimensions and sizes:   [time | 24090] x [lats | 120] x [lons | 180]
Coordinates: 
           time: [350616..929160]
           lats: [ -90..88.5]
           lons: [   0.. 358]
Number Of Attributes: 1
 _FillValue :  -9e+33
 
I'd like to know how I can correct the second part of the time series to 
make it like the first part. I tried with LonFlip and LonPivot but I 
couldn't make it work. I'm thinking about a way to shift the data back 
by that longitude offset, but I can't wrap my head around it.
 
Please let me know.
 Cheers,
Giorgio
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20231218/fb39ba8d/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: MarcosCounterfactualSST.m
Type: text/x-objcsrc
Size: 1470 bytes
Desc: not available
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20231218/fb39ba8d/attachment-0001.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Screenshot_20231215_200928.png
Type: image/png
Size: 580423 bytes
Desc: not available
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20231218/fb39ba8d/attachment-0001.png>


More information about the ncl-talk mailing list