m_read_CLS_SLA.F90 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. module m_read_CLS_SLA
  2. ! Reads CLS SLA data after having read the grid in read_CLS_SST_grid
  3. contains
  4. subroutine read_CLS_SLA(fname,gr,data)
  5. use mod_measurement
  6. use mod_grid
  7. use m_spherdist
  8. use netcdf
  9. use m_nf90_err
  10. implicit none
  11. type (measurement), intent(inout) :: data(:)
  12. type (grid), intent(inout) :: gr ! CLS measurement grid
  13. character(len=80), intent(in) :: fname
  14. !dimension ids
  15. integer :: NbLatitudes_ID, NbLongitudes_ID, LatLon_ID
  16. ! Variable ids
  17. integer :: vNbLatitudes_ID, vNbLongitudes_ID, vGrid0001_ID
  18. ! Array dimensions
  19. integer :: LatLon, NbLatitudes, NbLongitudes
  20. ! Data arrays
  21. real,allocatable :: sla(:,:), lon(:),lat(:)
  22. ! utilitary
  23. integer ncid, ijmax(2)
  24. real undef,undef_lat, undef_lon
  25. integer i, j,k
  26. logical valid
  27. real, parameter :: eps = 0.01 ! test for undefined values
  28. ! Open file
  29. ! filename='sst_topaz_19510.nc'
  30. call nf90_err(NF90_OPEN(trim(fname),NF90_NOCLOBBER,ncid))
  31. !call nfw_open(trim(fname), nf_nowrite, ncid)
  32. ! Get dimension id in netcdf file ...
  33. call nf90_err(nf90_Inq_Dimid(ncid,'LatLon',LatLon_ID))
  34. call nf90_err(nf90_Inq_Dimid(ncid,'NbLatitudes',NbLatitudes_ID))
  35. call nf90_err(nf90_Inq_Dimid(ncid,'NbLongitudes',NbLongitudes_ID))
  36. ! Get dimension length from id
  37. call nf90_err(nf90_Inquire_Dimension(ncid,LatLon_ID,len=LatLon))
  38. call nf90_err(nf90_Inquire_Dimension(ncid,NbLatitudes_ID,len=NbLatitudes))
  39. call nf90_err(nf90_Inquire_Dimension(ncid,NbLongitudes_ID,len=NbLongitudes))
  40. print*, 'Dimensions:', NbLatitudes, NbLongitudes, LatLon
  41. ! State which variable you want here.. Available vars are shown when you do
  42. ! "ncdump -h " on the netcdf file. This is for SSH
  43. allocate(lon(NbLongitudes))
  44. allocate(lat(NbLatitudes))
  45. allocate(sla(NbLatitudes,NbLongitudes))
  46. ! Variable ids in netcdf file
  47. call nf90_err(nf90_inq_varid(ncid,'NbLatitudes' ,vNbLatitudes_ID),'NbLatitudes')
  48. call nf90_err(nf90_inq_varid(ncid,'NbLongitudes' ,vNbLongitudes_ID),'NbLongitudes')
  49. call nf90_err(nf90_inq_varid(ncid,'Grid_0001' ,vGrid0001_ID),'Grid_0001')
  50. ! Variable _FillValue attributes
  51. call nf90_err(nf90_get_att(ncid,vNbLatitudes_ID , '_FillValue',undef_lat))
  52. call nf90_err(nf90_get_att(ncid,vNbLongitudes_ID ,'_FillValue',undef_lon))
  53. call nf90_err(nf90_get_att(ncid,vGrid0001_ID , '_FillValue',undef))
  54. print*, 'Undefined values are ', undef_lat, undef_lon, undef
  55. gr%undef = undef
  56. ! actual variable values (for dimensions of var -- see ncdump, or improve this program)
  57. ! NB: note that index dimensions are different between fortran and C internals.
  58. ! "ncdump" gives C internals.
  59. print *,'test'
  60. call nf90_err(nf90_get_var(ncid,vNbLongitudes_ID ,lon))
  61. !lon = ang180(lon)
  62. print *,'Range Lon', minval(lon), maxval(lon)
  63. call nf90_err(nf90_get_var(ncid,vNbLatitudes_ID ,lat))
  64. print *,'Range Lat', minval(lat), maxval(lat)
  65. call nf90_err(nf90_get_var(ncid,vGrid0001_ID ,sla))
  66. print *,'Range SLA in cm ', minval(sla), maxval(sla)
  67. print '(4a10)','Lat','Lon','SLA[cm]'
  68. ijmax = minloc(sla)
  69. do i=ijmax(1)-5, ijmax(1)+5
  70. j = ijmax(2)
  71. print '(4f10.3)', lat(i), lon(j), sla(i,j)
  72. enddo
  73. call nf90_err (nf90_close(ncid))
  74. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  75. ! Fill the data(:) vector
  76. do j=1,NbLongitudes ! gr%ny
  77. do i=1,NbLatitudes ! gr%nx
  78. k=(j-1)*gr%nx+i
  79. data(k)%id = 'SLA'
  80. data(k)%d = sla(i,j) * 0.01 ! Conversion to meters
  81. data(k)%ipiv = i
  82. data(k)%jpiv = j
  83. data(k)%lat=lat(i)
  84. data(k)%lon=ang180(lon(j))
  85. !LB: Data support is assumed = a square grid cell
  86. !support diameter in meters stored in %a1 (tricky, isn't it ?)
  87. data(k)%a1 = spherdist(lon(j)-.5*gr%dx,lat(i)-.5*gr%dy, &
  88. lon(j)+.5*gr%dx,lat(i)+.5*gr%dy)
  89. data(k)%ns = 1
  90. !data(k)%var = 0.01 ! 30cm temporarily, 10 cm by default
  91. !PS
  92. data(k)%var = 0.001 ! 30cm temporarily, 10 cm by default
  93. data(k)%depth = 0.0
  94. valid = (abs( (lon(j)-undef_lon) / undef_lon ) > eps &
  95. .and. abs( (lat(i)-undef_lat) / undef_lat ) > eps &
  96. .and. abs( (sla(i,j)-undef) / undef ) > eps )
  97. data(k)%status = valid
  98. enddo
  99. enddo
  100. print*, 'Number of data read:', k, gridpoints(gr)
  101. deallocate(lat,lon,sla)
  102. end subroutine read_CLS_SLA
  103. end module m_read_CLS_SLA